Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomgLine2D.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 #include "HomgLine2D.hh"
22 #include <limits>
23 
24 using namespace BIAS;
25 using namespace std;
26 
27 bool HomgLine2D::
28 GetIntersectionsWithImage(unsigned int width, unsigned int height,
29  unsigned int coo[4])
30 {
31  //const bool debug = false;
32 
33  int intersectioncount = 0;
34  HomgPoint2D edges[4]; // 0=left, 1=right, 2=top, 3=bottom
35  HomgLine2D left(-1.0, 0.0, 0.0);
36  HomgLine2D right(-1.0, 0.0, width-1);
37  HomgLine2D top(0.0, -1.0, 0.0);
38  HomgLine2D bottom(0.0, -1.0, height-1);
39 
40  edges[0] = Intersection(left);
41  edges[1] = Intersection(right);
42  edges[2] = Intersection(top);
43  edges[3] = Intersection(bottom);
44 
45 // if (debug){
46 // cerr << setprecision(25);
47 // cout << setprecision(25);
48 // cerr << "HomghLine2D.cc:" << endl;
49 // for (int i =0; i<4; i++){
50 // cerr << "edges[" << i << "] : " << edges[i] << endl;
51 // }
52 // }
53 
54  // deal with compiler precision
55  for (unsigned int i=0; i<4; i++){
56  if (!edges[i].IsAtInfinity()) {
57  edges[i].Homogenize();
58  if (edges[i][0] < 0.0 && edges[i][0] > -HOMGLINE2D_EPS)
59  edges[i][0] = 0.0;
60  if (edges[i][1] < 0.0 && edges[i][1] > -HOMGLINE2D_EPS)
61  edges[i][1] = 0.0;
62  if (edges[i][0] > (double)width-1.0 &&
63  edges[i][0] < (double)width-1.0 +HOMGLINE2D_EPS)
64  edges[i][0] = (double)width-1.0;
65  if (edges[i][1] > (double)height-1.0 &&
66  edges[i][1] < (double)height-1.0 +HOMGLINE2D_EPS)
67  edges[i][1] = (double)height-1.0;
68  } else {
69  edges[i][0] = edges[i][1] = -numeric_limits<double>::max();
70  }
71  }
72 
73 // if (debug){
74 // cerr << setprecision(25);
75 // cout << setprecision(25);
76 // cerr << "HomghLine2D.cc:" << endl;
77 // for (int i =0; i<4; i++){
78 // cerr << "edges[" << i << "] : " << edges[i] << endl;
79 // }
80 // }
81 
82  // left
83  if ((edges[0][1]>=0.0 && edges[0][1]<=(double)height-1.0)){
84  intersectioncount++;
85  coo[0] = 0;
86  coo[1] = (unsigned int)rint(edges[0][1]);
87 // if (debug) cout << "int with left: "<<coo[0]<<", "<<coo[1]<<endl;
88  }
89  // right
90  if ((edges[1][1]>=0.0 && edges[1][1]<=(double)height-1.0)){
91  coo[intersectioncount*2] = width - 1;
92  coo[intersectioncount*2+1] = (unsigned int)rint(edges[1][1]);
93 // if (debug) cout << "int with right: "<<coo[intersectioncount*2]
94 // <<", "<<coo[intersectioncount*2+1]<<endl;
95  intersectioncount++;
96  } else {
97 // if (debug) cout <<"not right: "<<edges[1][1]<<endl
98 // <<"\t"<<(double)(height)-1.0<<" "<<boolalpha
99 // <<(edges[1][1]<=(double)height-1.0)<<" "
100 // <<(edges[1][1]>=0.0)<<endl
101 // <<(double)(height)-1.0-edges[1][1]<<endl;
102  }
103  // top
104  if ((edges[2][0]>=0.0 && edges[2][0]<=(double)width-1.0)){
105 // if (debug) cout << "int with top: ";
106  switch (intersectioncount){
107  case 0:
108  coo[0] = (unsigned int)rint(edges[2][0]);
109  coo[1] = 0;
110 // if (debug) cout << "first: "<< coo[0] << ", "<<coo[1]<<endl;
111  break;
112  case 1:
113  coo[2] = (unsigned int)rint(edges[2][0]);
114  coo[3] = 0;
115 // if (debug) cout << "second: "<< coo[2] << ", "<<coo[3]<<endl;
116  break;
117  default:
118 // if (debug) cout << "already two intersections\n";
119  break;
120  }
121  intersectioncount++;
122  }
123  // bottom
124  if ((edges[3][0]>=0.0 && edges[3][0]<=(double)width-1.0)){
125 // if (debug) cout << "int with bottom: ";
126  switch (intersectioncount){
127  case 0:
128  coo[0] = (unsigned int)rint(edges[3][0]);
129  coo[1] = height -1;
130 // if (debug) cout << "first: "<<coo[0] << ", "<<coo[1]<<endl;
131  break;
132  case 1:
133  coo[2] = (unsigned int)rint(edges[3][0]);
134  coo[3] = height -1;
135 // if (debug) cout << "second: "<< coo[2] << ", "<<coo[3]<<endl;
136  break;
137  case 2:
138  if (sqrt(pow((double)(coo[0]-coo[2]),2) +
139  pow((double)(coo[1]-coo[3]),2))<
140  sqrt(pow((double)(coo[0]-(unsigned int)rint(edges[3][0])),2) +
141  pow((double)(coo[1]-(height -1)),2))){
142  coo[2] = (unsigned int)rint(edges[3][0]);
143  coo[3] = height -1;
144  // if (debug) cout << "replacing second intersection: "
145 // << coo[2] << ", "<<coo[3]<<endl;
146  }
147  break;
148  default:
149 // if (debug) cout << "already three intersections\n";
150  break;
151  }
152  intersectioncount++;
153  }
154 
155 
156  return (intersectioncount >1);
157 }
158 
159 
160 
161 
162 
163 
164 
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
bool GetIntersectionsWithImage(unsigned int width, unsigned int height, unsigned int coo[4])
! assumes line is given in pixel coo ! returns true if line intersects with image of given size retur...
Definition: HomgLine2D.cpp:28
a line l = (a b c)^T is a form of the implicit straight line equation 0 = a*x + b*y + c if homogenize...
Definition: HomgLine2D.hh:48