Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomgPlane3D.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __HOMGPLANE3D_HH__
27 #define __HOMGPLANE3D_HH__
28 #include "bias_config.h"
29 
30 #include <Base/Debug/Error.hh>
31 #include <Base/Debug/Debug.hh>
32 #include <Base/Math/Vector4.hh>
33 #include <Base/Math/Matrix3x3.hh>
34 
35 #include "HomgPoint3D.hh"
36 
37 #define HOMGPLANE3D_TYPE double
38 // values whose abs is < epsilon are handled as zero
39 #define HOMGPLANE3D_EPS 1E-12
40 
41 namespace BIAS {
42  /** @class HomgPlane3D
43  @ingroup g_geometry
44  @brief A homogeneous plane (in P^3)
45  All points X on the plane p fulfill p ' * X = 0 */
46  class BIASGeometryBase_EXPORT HomgPlane3D
47  : public Vector4<HOMGPLANE3D_TYPE>
48  {
49  public:
50  inline HomgPlane3D()
51  { data_[0] = data_[1] = data_[3] = 0.0 ; data_[2] = 1.0 ; };
52 
53  /** @brief set four values of the plane vector */
54  inline HomgPlane3D(HOMGPLANE3D_TYPE a, HOMGPLANE3D_TYPE b,
55  HOMGPLANE3D_TYPE c, HOMGPLANE3D_TYPE d)
56  { data_[0]=a; data_[1]=b; data_[2]=c; data_[3]=d; };
57 
58  /** @brief constructs a homogeneous plane from three points
59  @author koeser 07/2004 */
60  inline HomgPlane3D(const HomgPoint3D& p1,
61  const HomgPoint3D& p2,
62  const HomgPoint3D& p3) {
63  SetFromPoints(p1, p2, p3);
64  }
65 
66  /** @brief construct plane, given arbitrary point and normal
67  @param NormalVector unit length normal vector
68  @param PointOnPlane arbitrary point on the plane
69  @author koeser 02/2005 */
70  explicit inline HomgPlane3D(const Vector3<double>& PointOnPlane,
71  const Vector3<double>& NormalVector) {
72  Set(PointOnPlane, NormalVector); }
73 
74  explicit inline HomgPlane3D(const HomgPoint3D& PointOnPlane,
75  const Vector3<double>& NormalVector) {
76  Set(PointOnPlane, NormalVector); }
77 
78  explicit inline HomgPlane3D(const Vector4<HOMGPLANE3D_TYPE>& theplane)
80 
81  explicit inline HomgPlane3D(const Vector<HOMGPLANE3D_TYPE>& theplane)
82  { SetFromVector(theplane); };
83 
84  /** @brief constructs a homogeneous plane from three points
85  @author koeser 07/2004 */
86  inline void SetFromPoints(const HomgPoint3D& p1,
87  const HomgPoint3D& p2,
88  const HomgPoint3D& p3);
89 
90  inline void SetFromVector(const Vector<HOMGPLANE3D_TYPE>& theplane) {
91  BIASASSERT(theplane.size()==4);
92  data_[0]=theplane[0]; data_[1]=theplane[1];
93  data_[2]=theplane[2]; data_[3]=theplane[3];
94  }
95 
96  /** @brief empty destructor */
97  inline ~HomgPlane3D() {};
98 
99  /** @brief construct plane, given arbitrary point and normal
100  @param NormalVector unit length normal vector
101  @param PointOnPlane arbitrary point on the plane
102  @author koeser 02/2005 */
103  void Set(const Vector3<double>& PointOnPlane,
104  const Vector3<double>& NormalVector) {
105  data_[0] = NormalVector[0];
106  data_[1] = NormalVector[1];
107  data_[2] = NormalVector[2];
108  data_[3] = -1.0 * NormalVector.ScalarProduct(PointOnPlane);
109  }
110 
111  /** @brief construct plane, given arbitrary point and normal
112  @param NormalVector normal vector with length != 0
113  @param PointOnPlane arbitrary point on the plane (also at infinity)
114  @author woelk 06/2008 (c) www.vision-n.de */
115  void Set(const HomgPoint3D& PointOnPlane,
116  const Vector3<double>& NormalVector);
117 
118  /** @brief set first three components to vector of norm 1 and scales
119  w accordingly => encodes Hesse normal form
120 
121  afterwards w contains the distance of the plane
122  from the origin, while the first three coordinates are the normal
123  vector in euclidean 3-space. This is called the Hesse normal form. */
124  inline void Normalize();
125 
126  /** @brief returns normal vector */
127  inline void GetNormalVector(Vector3<HOMGPLANE3D_TYPE>& normalv) const;
128 
129  /** @brief returns normal vector, only wrapper */
130  inline Vector3<HOMGPLANE3D_TYPE> GetNormalVector() const;
131 
132  /** @brief calculates squared distance of a point from plane */
133  inline HOMGPLANE3D_TYPE DistanceSquared(const HomgPoint3D& point) const;
134 
135  /** @brief compute the intersection point of the plane with a line (defined
136  by point and direction)
137 
138  @param PointOnLine one arbitrary point on the line
139  @param LineDir The direction of the line, LineDir[3]==0 !
140  @param Intersection (output) A point lying on the line and on the plane
141  @return - 0 on success
142  - +1 point is at infinity (line and plane parallel)
143  - -1 line is in the plane, whole line is solution
144  @author koeser 01/2005 */
145  inline int GetLineIntersection(const HomgPoint3D& PointOnLine,
146  const HomgPoint3D& LineDir,
147  HomgPoint3D& Intersection) const;
148 
149  /** @brief finds the closest point which is on the plane
150  @author koeser */
151  inline HomgPoint3D ProjectPoint(const HomgPoint3D& origPoint) const;
152 
153  }; // class
154 
155 
156  // ----------------------- inline implementation ---------------------------
157 
158  inline void HomgPlane3D::Normalize() {
159  (*this) /= sqrt(data_[0]*data_[0]+data_[1]*data_[1]+data_[2]*data_[2]);
160  }
161 
164  {
165  HOMGPLANE3D_TYPE norm =
166  sqrt(data_[0]*data_[0]+data_[1]*data_[1]+data_[2]*data_[2]);
167  Vector3<HOMGPLANE3D_TYPE> normal(data_[0] / norm, data_[1] / norm,
168  data_[2] / norm);
169  return normal;
170  }
171 
172  inline void
174  {
175  HOMGPLANE3D_TYPE norm =
176  sqrt(data_[0]*data_[0]+data_[1]*data_[1]+data_[2]*data_[2]);
177  normalv[0] = data_[0] / norm;
178  normalv[1] = data_[1] / norm;
179  normalv[2] = data_[2] / norm;
180  }
181 
182 
183  inline HOMGPLANE3D_TYPE
185  {
186  HOMGPLANE3D_TYPE dist = data_[0] * point[0] + data_[1] * point[1] +
187  data_[2] * point[2] + data_[3] * point[3];
188  dist = dist*dist;
189  dist/=((data_[0]*data_[0]+data_[1]*data_[1]+data_[2]*data_[2])*
190  point[3]*point[3]);
191 
192  return dist;
193  }
194 
196  const HomgPoint3D& p2,
197  const HomgPoint3D& p3) {
198  // set up four submatrices as in "Multiple View Geometry", p.47
199  Matrix3x3<HOMGPLANE3D_TYPE> M234, M134, M124, M123;
200  for (unsigned int row=0; row<3; row++) {
201  M234[row][0] = p1[row+1];
202  M234[row][1] = p2[row+1];
203  M234[row][2] = p3[row+1];
204 
205  M123[row][0] = p1[row];
206  M123[row][1] = p2[row];
207  M123[row][2] = p3[row];
208  }
209 
210  M124[0][0] = p1[0];
211  M124[0][1] = p2[0];
212  M124[0][2] = p3[0];
213  M124[1][0] = p1[1];
214  M124[1][1] = p2[1];
215  M124[1][2] = p3[1];
216  M124[2][0] = p1[3];
217  M124[2][1] = p2[3];
218  M124[2][2] = p3[3];
219 
220  M134[0][0] = p1[0];
221  M134[0][1] = p2[0];
222  M134[0][2] = p3[0];
223  M134[1][0] = p1[2];
224  M134[1][1] = p2[2];
225  M134[1][2] = p3[2];
226  M134[2][0] = p1[3];
227  M134[2][1] = p2[3];
228  M134[2][2] = p3[3];
229 
230  // plane is constructed by determinants of submatrices
231  (*this)[0] = M234.GetDeterminant();
232  (*this)[1] = -M134.GetDeterminant();
233  (*this)[2] = M124.GetDeterminant();
234  (*this)[3] = -M123.GetDeterminant();
235  }
236 
237  inline int
239  const HomgPoint3D& LineDir,
240  HomgPoint3D& Intersection) const {
241 #ifdef BIAS_DEBUG
242  if (LineDir[3]!=0.0) {
243  BIASERR("Incorrect data for line direction (w==0 required for dir)! "
244  <<PointOnLine<<" "<<LineDir);
245  BIASABORT;
246  }
247 #endif
248  // solve equation for lamda:
249  // (PointOnLine + lamda*LineDir) * (this) = 0
250  // Intersection = PointOnLine + lamda*LineDir
251 
252  // assume Linedir[3]==0 (only direction)
253  const double dirproduct = LineDir[0]*data_[0]+LineDir[1]*data_[1]+
254  LineDir[2]*data_[2];
255  const double pointproduct = PointOnLine[0]*data_[0] +
256  PointOnLine[1]*data_[1] + PointOnLine[2]*data_[2] +
257  PointOnLine[3]*data_[3];
258  // catch degenerate cases
259  if (fabs(dirproduct)<HOMGPLANE3D_EPS) {
260  if (fabs(pointproduct)<HOMGPLANE3D_EPS) {
261  // lamda = 0/0 -> arbitrary solution
262  Intersection = PointOnLine;
263  return -1;
264  } else {
265  // lamda = x/0 -> line parallel to plane
266  Intersection = PointOnLine;
267  Intersection[3] = 0.0;
268  return 1;
269  }
270  }
271  const double lamda = pointproduct / dirproduct;
272  Intersection = PointOnLine - (LineDir * lamda);
273  return 0;
274  }
275 
277  origPoint) const {
278  // search for plane in normal direction
279  HomgPoint3D LineDir(data_[0], data_[1], data_[2], 0.0);
280  HomgPoint3D projection(0,0,0,0);
281  GetLineIntersection(origPoint, LineDir, projection);
282  return projection;
283  }
284 
285 
286 
287 
288 } //namespace BIAS
289 #endif // __HOMGPLANE3D_HH__
void SetFromPoints(const HomgPoint3D &p1, const HomgPoint3D &p2, const HomgPoint3D &p3)
constructs a homogeneous plane from three points
Definition: HomgPlane3D.hh:195
int GetLineIntersection(const HomgPoint3D &PointOnLine, const HomgPoint3D &LineDir, HomgPoint3D &Intersection) const
compute the intersection point of the plane with a line (defined by point and direction) ...
Definition: HomgPlane3D.hh:238
HOMGPLANE3D_TYPE DistanceSquared(const HomgPoint3D &point) const
calculates squared distance of a point from plane
Definition: HomgPlane3D.hh:184
class for column vectors with arbitrary size
HomgPlane3D(const Vector< HOMGPLANE3D_TYPE > &theplane)
Definition: HomgPlane3D.hh:81
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector3.hh:603
Vector3< HOMGPLANE3D_TYPE > GetNormalVector() const
returns normal vector, only wrapper
Definition: HomgPlane3D.hh:163
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
HomgPlane3D(const HomgPoint3D &p1, const HomgPoint3D &p2, const HomgPoint3D &p3)
constructs a homogeneous plane from three points
Definition: HomgPlane3D.hh:60
HOMGPLANE3D_TYPE data_[VECTOR4SIZE]
Definition: Vector4.hh:375
HomgPlane3D(const HomgPoint3D &PointOnPlane, const Vector3< double > &NormalVector)
Definition: HomgPlane3D.hh:74
HomgPlane3D(const Vector4< HOMGPLANE3D_TYPE > &theplane)
Definition: HomgPlane3D.hh:78
HomgPlane3D(const Vector3< double > &PointOnPlane, const Vector3< double > &NormalVector)
construct plane, given arbitrary point and normal
Definition: HomgPlane3D.hh:70
HomgPoint3D ProjectPoint(const HomgPoint3D &origPoint) const
finds the closest point which is on the plane
Definition: HomgPlane3D.hh:276
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
Vector4< T > & operator=(const T &scalar)
assignment operator set the vector elementwise to scalar value
Definition: Vector4.hh:449
void Set(const Vector3< double > &PointOnPlane, const Vector3< double > &NormalVector)
construct plane, given arbitrary point and normal
Definition: HomgPlane3D.hh:103
T GetDeterminant() const
returns the Determinant |A| of this
Definition: Matrix3x3.cpp:347
void Normalize()
set first three components to vector of norm 1 and scales w accordingly =&gt; encodes Hesse normal form ...
Definition: HomgPlane3D.hh:158
void SetFromVector(const Vector< HOMGPLANE3D_TYPE > &theplane)
Definition: HomgPlane3D.hh:90
~HomgPlane3D()
empty destructor
Definition: HomgPlane3D.hh:97
Subscript size() const
Definition: vec.h:262
HomgPlane3D(HOMGPLANE3D_TYPE a, HOMGPLANE3D_TYPE b, HOMGPLANE3D_TYPE c, HOMGPLANE3D_TYPE d)
set four values of the plane vector
Definition: HomgPlane3D.hh:54
A homogeneous plane (in P^3) All points X on the plane p fulfill p &#39; * X = 0.
Definition: HomgPlane3D.hh:46