Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomgLine2D.hh
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 #ifndef __HOMGLINE2D_HH__
22 #define __HOMGLINE2D_HH__
23 #include "bias_config.h"
24 
25 #include <Base/Debug/Error.hh>
26 #include <Base/Debug/Debug.hh>
27 
28 #include <Base/Math/Vector3.hh>
29 
30 #include "HomgPoint2D.hh"
31 
32 #define HOMGLINE2D_TYPE double
33  // values whose abs is < epsilon are handled as zero
34 #define HOMGLINE2D_EPS 1E-12
35 
36 namespace BIAS {
37  /**
38  @class HomgLine2D
39  @ingroup g_geometry
40  a line l = (a b c)^T is a form of the implicit straight line equation
41  0 = a*x + b*y + c
42  if homogenized, a^2 + b^2 = 1
43  beware: (a b) is the vector perpendicular to the line direction
44  therefor b could be interpreted as dx,
45  but then a is equal to -dy
46  @author woelk 08 2002
47  */
48  class BIASGeometryBase_EXPORT HomgLine2D : public Vector3 <HOMGLINE2D_TYPE>
49  {
50  public:
51 
52  inline HomgLine2D();
53 
54  explicit inline HomgLine2D(const Vector3<HOMGLINE2D_TYPE>& vec);
55 
56  inline HomgLine2D(HOMGLINE2D_TYPE a, HOMGLINE2D_TYPE b, HOMGLINE2D_TYPE c);
57 
58  /// copy constructor
59  inline HomgLine2D(const HomgLine2D& line);
60 
61  /** constructing a line through two points */
62  inline HomgLine2D(const HomgPoint2D& p1, const HomgPoint2D& p2);
63 
64  /// destructor
66 
67  /** constructing a line through two points */
68  inline void Set(const HomgPoint2D& p1, const HomgPoint2D& p2);
69 
70  /** returns length of hypothenuse in gradient triangle */
71  inline HOMGLINE2D_TYPE GetW();
72 
73  /** aequivalent to homogenize for points
74  the gradient triangle is normed to hypothenuse length of 1 */
75  inline void Homogenize();
76 
77  /** calculates distance of a point from line */
78  inline HOMGLINE2D_TYPE Distance(const HomgPoint2D& point)
79  { return sqrt(DistanceSquared(point)); };
80 
81  /** calculates squared distance of a point from line */
82  inline HOMGLINE2D_TYPE DistanceSquared(const HomgPoint2D& point);
83 
84  /** calculates homogenized intersection of two lines */
85  inline void Intersection(const HomgLine2D& line, HomgPoint2D& intersect) const;
86 
87  /** calls the above */
88  inline HomgPoint2D Intersection(const HomgLine2D& line) const;
89 
90  /** ! assumes line is given in pixel coo !
91  returns true if line intersects with image of given size
92  returns intersections with image ( x1 = coo[0], y1 = coo[1],
93  x2 = coo[2], y2 = coo[3] ) for usage with DrawLine */
94  bool GetIntersectionsWithImage(unsigned int width, unsigned int height,
95  unsigned int coo[4]);
96 
97  /** return perpendicular line throug point */
98  inline void GetPerpendicularLine(HomgPoint2D& point, HomgLine2D& perpline);
99 
100 
101 
102  };
103 
104  ///////////////////////////////////////////////////////////////////
105  // inline implementations
106  //////////////////////////////////////////////////////////////////
107 
108 
110  { data_[0] = 0; data_[1] = -1; data_[2] = 0.0; }
111 
113  { data_[0] = vec[0]; data_[1] = vec[1]; data_[2] = vec[2]; }
114 
115  inline HomgLine2D::HomgLine2D(HOMGLINE2D_TYPE a,
116  HOMGLINE2D_TYPE b,
117  HOMGLINE2D_TYPE c)
118  { data_[0] = a; data_[1] = b; data_[2] = c; }
119 
120  /// copy constructor
121  inline HomgLine2D::HomgLine2D(const HomgLine2D& line)
122  { data_[0] = line[0]; data_[1] = line[1]; data_[2] = line[2]; }
123 
124  inline
126  {
127  Set(p1, p2);
128  }
129 
130  inline void
132  {
133  p1.CrossProduct(p2, *this);
134  Homogenize();
135  // deal with compiler precision
136  if (data_[0] < HOMGLINE2D_EPS && data_[0] > -HOMGLINE2D_EPS)
137  data_[0] = 0.0;
138  if (data_[1] < HOMGLINE2D_EPS && data_[1] > -HOMGLINE2D_EPS)
139  data_[1] = 0.0;
140  if (data_[2] < HOMGLINE2D_EPS && data_[2] > -HOMGLINE2D_EPS)
141  data_[2] = 0.0;
142  }
143 
144  inline HOMGLINE2D_TYPE HomgLine2D::GetW()
145  {
146  return sqrt(data_[0] * data_[0] + data_[1] * data_[1]);
147  }
148 
149  inline void HomgLine2D::Homogenize()
150  {
151  register HOMGLINE2D_TYPE w = GetW();
152  BIASASSERT(w!=0.0);
153  data_[0] = data_[0] / -w;
154  data_[1] = data_[1] / -w;
155  data_[2] = data_[2] / -w;
156  }
157 
158 
159  inline HOMGLINE2D_TYPE
161  {
162  HOMGLINE2D_TYPE dist = data_[0] * point[0] + data_[1] * point[1] +
163  data_[2] * point[2];
164  dist = dist*dist;
165  dist/=((data_[0]*data_[0]+data_[1]*data_[1])* point[2]*point[2]);
166 
167  return (dist>HOMGLINE2D_EPS)? (dist):(0.0);
168  }
169 
170 
171  inline HomgPoint2D
173  {
174  HomgPoint2D point;
175  Intersection(line, point);
176  return point;
177  }
178 
179  inline void
181  HomgPoint2D& intersect) const
182  {
183  CrossProduct(line, intersect);
184  }
185 
186  inline void
188  {
189  point.Homogenize();
190  // change slope
191  perpline[0] = data_[1];
192  perpline[1] = -data_[0];
193  // point must lie on lie : perpline^T * point = 0
194  perpline[2] = - perpline[1]*point[1] - perpline[0]*point[0];
195  }
196 }// namespace BIAS
197 #endif // __HOMGLINE2D_HH__
198 
199 
200 
201 
202 
203 
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
HOMGLINE2D_TYPE data_[VECTOR3_SIZE]
Definition: Vector3.hh:514
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
HOMGLINE2D_TYPE GetW()
returns length of hypothenuse in gradient triangle
Definition: HomgLine2D.hh:144
~HomgLine2D()
destructor
Definition: HomgLine2D.hh:65
void Set(const HomgPoint2D &p1, const HomgPoint2D &p2)
constructing a line through two points
Definition: HomgLine2D.hh:131
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
void GetPerpendicularLine(HomgPoint2D &point, HomgLine2D &perpline)
return perpendicular line throug point
Definition: HomgLine2D.hh:187
void Intersection(const HomgLine2D &line, HomgPoint2D &intersect) const
calculates homogenized intersection of two lines
Definition: HomgLine2D.hh:180
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
void Homogenize()
aequivalent to homogenize for points the gradient triangle is normed to hypothenuse length of 1 ...
Definition: HomgLine2D.hh:149
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
HOMGLINE2D_TYPE Distance(const HomgPoint2D &point)
calculates distance of a point from line
Definition: HomgLine2D.hh:78
HOMGLINE2D_TYPE DistanceSquared(const HomgPoint2D &point)
calculates squared distance of a point from line
Definition: HomgLine2D.hh:160