Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HomgPoint3D.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 _HomgPoint3D_hh_
27 #define _HomgPoint3D_hh_
28 #include "bias_config.h"
29 
30 #include <Base/Debug/Error.hh>
31 #include <Base/Math/Vector4.hh>
32 #include <Base/Math/Vector3.hh>
33 #include <limits>
34 
35 #define HOMGPOINT3D_TYPE double
36 #define HOMGPOINT3D_EPS 1E-12
37 #define HOMGPOINT3D_TYPE_INF std::numeric_limits<double>::infinity()
38 
39 namespace BIAS {
40 
41 /** @class HomgPoint3D
42  @ingroup g_geometry
43  @brief class HomgPoint3D describes a point with 3 degrees of
44  freedom in projective coordinates.
45 
46  manual loop unrolling is used if possible.
47  The Vector is in row-major order (4 rows, 1 column)
48  the indizes begin with zero (to size-1)
49 
50  constructors and assignment operators are never inherited,
51  so they have be wrapped (or reimplemented) here.
52  @author Jan Woetzel
53  @status tested in part (03/04/2002)
54 
55  To DO:
56  - CrossProduvt von zwei HomgPOint3D
57  (Aunterscheidung beide w==0 / beide w!=0, sonst Meldung.
58 **/
59 
60  // all inline - nothing to compile JW
61  class BIASGeometryBase_EXPORT HomgPoint3D : public Vector4<HOMGPOINT3D_TYPE>
62  {
63  public:
64 
65  inline HomgPoint3D():Vector4 <HOMGPOINT3D_TYPE> () {};
66 
67  inline HomgPoint3D(const HomgPoint3D & p)
68  : Vector4<HOMGPOINT3D_TYPE>(p) {};
69 
71  : Vector4<HOMGPOINT3D_TYPE>(p) {};
72 
73  explicit inline HomgPoint3D(const Vector3<HOMGPOINT3D_TYPE>& vec);
74 
75  inline ~HomgPoint3D() {};
76 
77  /** assignment with a constant value for all elements
78  @author Jan Woetzel
79  @status alpha (03/01/2002) **/
80  explicit inline HomgPoint3D(const HOMGPOINT3D_TYPE & scalar)
81  : Vector4<HOMGPOINT3D_TYPE>(scalar) {};
82 
83  /** assignment with an array of values
84  which is copied into this ones class members
85  @author Jan Woetzel
86  @status untested (02/28/2002) **/
87  explicit inline HomgPoint3D(const HOMGPOINT3D_TYPE * pv)
88  : Vector4<HOMGPOINT3D_TYPE>(pv) {};
89 
90  /** constructor with element assignment
91  @author Jan Woetzel
92  @status alpha (03/01/2002) **/
93  explicit inline HomgPoint3D(char *s)
94  : Vector4<HOMGPOINT3D_TYPE>(s) {};
95 
96  /** constructor using euclidic coordinates, setting the 4th
97  HomgPoint3D component to 1
98  @author Ingo Thomsen
99  @status untested */
100  inline HomgPoint3D(const HOMGPOINT3D_TYPE & x,
101  const HOMGPOINT3D_TYPE & y,
102  const HOMGPOINT3D_TYPE & z);
103 
104  /** constructor using projective coordinates and initializes *this
105  from Vector<double> of length 4 or length 3 in this case the
106  fourth entry is 1.0.
107  @author JMF
108  @status tested */
109  inline HomgPoint3D(const Vector<double>& vec);
110 
111  /** @author woelk 11/2003 */
112  inline HomgPoint3D(const HOMGPOINT3D_TYPE & x,
113  const HOMGPOINT3D_TYPE & y,
114  const HOMGPOINT3D_TYPE & z,
115  const HOMGPOINT3D_TYPE & w)
116  : Vector4<HOMGPOINT3D_TYPE>(x, y, z, w) {};
117 
118  inline bool IsAtInfinity() const;
119 
120  inline HOMGPOINT3D_TYPE GetX() const;
121  inline HOMGPOINT3D_TYPE GetY() const;
122  inline HOMGPOINT3D_TYPE GetZ() const;
123  inline HOMGPOINT3D_TYPE GetW() const;
124  inline HOMGPOINT3D_TYPE GetW();
125 
126  /** assignment operator
127  set the elements of this HomgPoint to the element of vec
128  @author Jan Woetzel
129  @status alpha (02/25/2002)
130  **/
131  inline HomgPoint3D& operator=(const Vector4<HOMGPOINT3D_TYPE>& vec);
132 
133  /** assignment operator
134  set the vector elementwise to scalar value
135  @author Jan Woetzel
136  @status alpha (02/28/2002)
137  **/
138  inline HomgPoint3D& operator=(const HOMGPOINT3D_TYPE& scalar);
139  inline HomgPoint3D& operator=(const Vector3<HOMGPOINT3D_TYPE> &vec);
140 
141  /** homogenize class data member elements to W==1 by divison by W
142  @author Jan Woetzel
143  @status alpha (02/28/2002)**/
144  inline void Homogenize();
145 
146  /** @author woelk 10 2003 */
147  inline bool IsHomogenized() const {
148  return data_[3]<1.0+HOMGPOINT3D_EPS &&
149  data_[3]>1.0-HOMGPOINT3D_EPS;
150  }
151 
152  /** set elementwise with given 3 euclidean scalar values.
153  the additional W-coordinate is set to 1
154  @author Jan Woetzel
155  @status alpha (03/05/2002) **/
156  inline void Set(const HOMGPOINT3D_TYPE& x, const HOMGPOINT3D_TYPE& y,
157  const HOMGPOINT3D_TYPE& z);
158 
159  /** set elementwise
160  @author woelk 11/2003 */
161  inline void Set(const HOMGPOINT3D_TYPE& x, const HOMGPOINT3D_TYPE& y,
162  const HOMGPOINT3D_TYPE& z, const HOMGPOINT3D_TYPE& w)
163  { Vector4<HOMGPOINT3D_TYPE>::Set(x, y, z, w); }
164 
165  /** set from Vector3
166  @author woelk 03/2004 */
167  inline void Set(const Vector3<HOMGPOINT3D_TYPE> &vec);
168 
169  /** calculate affine coordinates of this and write them to dest
170  affine coordinates are projective coordinates with last element w == 1;
171  For w==0 the first three elements are copied describing a direction,
172  not a point.**/
173  inline void GetEuclidean(Vector3<HOMGPOINT3D_TYPE> &dest) const;
174  inline Vector3<HOMGPOINT3D_TYPE> GetEuclidean() const;
175  /// deprecated versions
176  inline void GetEuclidian(Vector3<HOMGPOINT3D_TYPE> &dest) const {
177  this->GetEuclidean(dest);
178  };
180  return this->GetEuclidean();
181  };
182 
183  /** return the euclidean coordinates
184  Only possible for w !=0 because w==0 describes points of infinity
185  which are not in euclidean space (but may be interpreted as
186  directions)
187  @author Jan Woetzel
188  @status untested (03/04/2002)**/
189  inline HOMGPOINT3D_TYPE GetEuclideanX() const;
190  inline HOMGPOINT3D_TYPE GetEuclideanY() const;
191  inline HOMGPOINT3D_TYPE GetEuclideanZ() const;
192  /// deprecated versions
193  inline HOMGPOINT3D_TYPE GetEuclidianX() const {
194  return this->GetEuclideanX();
195  };
196  inline HOMGPOINT3D_TYPE GetEuclidianY() const {
197  return this->GetEuclideanY();
198  };
199  inline HOMGPOINT3D_TYPE GetEuclidianZ() const {
200  return this->GetEuclideanZ();
201  };
202 
203  /** Comparison operator 'equal'
204  @author Arne Petersen
205  @status untested **/
206  inline bool operator==(const HomgPoint3D& arg) const;
207 
208  /** Comparison operator 'not equal'
209  @author Ingo Thomsen
210  @status tested **/
211  inline bool operator!=(const HomgPoint3D& arg) const;
212 
213  inline HOMGPOINT3D_TYPE Distance(const HomgPoint3D& point) const;
214 
215  }; // class
216 
217 
218 
219 
221  {
222  Set(vec);
223  }
224 
225  inline HomgPoint3D::HomgPoint3D(const HOMGPOINT3D_TYPE & x,
226  const HOMGPOINT3D_TYPE & y,
227  const HOMGPOINT3D_TYPE & z)
228  {
229  data_[0] = x;
230  data_[1] = y;
231  data_[2] = z;
232  data_[3] = 1;
233  }
234 
235  inline HOMGPOINT3D_TYPE HomgPoint3D::GetX() const
236  {
237  return data_[0];
238  }
239 
240  inline HOMGPOINT3D_TYPE HomgPoint3D::GetY() const
241  {
242  return data_[1];
243  }
244 
245  inline HOMGPOINT3D_TYPE HomgPoint3D::GetZ() const
246  {
247  return data_[2];
248  }
249 
250  inline HOMGPOINT3D_TYPE HomgPoint3D::GetW()
251  {
252  if (data_[3] > -HOMGPOINT3D_EPS && data_[3] < HOMGPOINT3D_EPS)
253  data_[3] = 0.0;
254  return data_[3];
255  }
256 
257  inline HOMGPOINT3D_TYPE HomgPoint3D::GetW() const
258  {
259  if (data_[3] > -HOMGPOINT3D_EPS && data_[3] < HOMGPOINT3D_EPS)
260  return 0.0;
261  else
262  return data_[3];
263  }
264 
265  inline HomgPoint3D&
267  {
268  data_[0] = vec[0];
269  data_[1] = vec[1];
270  data_[2] = vec[2];
271  data_[3] = 1.0;
272  return *this;
273  }
274 
275  inline HomgPoint3D&
277  {
278  Copy(vec.GetData());
279  return *this;
280  }
281 
282  inline HomgPoint3D&
283  HomgPoint3D::operator=(const HOMGPOINT3D_TYPE& scalar)
284  {
285  data_[0] = scalar;
286  data_[1] = scalar;
287  data_[2] = scalar;
288  data_[3] = scalar;
289  return *this;
290  }
291 
293 
294  switch (vec.size()){
295  case 3:
296  data_[3]=1.0;
297  case 4:
298  for (int i=0;i<vec.size();i++)
299  data_[i]=vec[i];
300  break;
301  default:
302  BIASERR("Initialization of HomgPoint3D from Vector with length"
303  << "!= 3 or !=4,length is " << vec.size());
304  }
305 
306  }
307 
308  inline void HomgPoint3D::Homogenize()
309  {
310  HOMGPOINT3D_TYPE w = GetW();
311  if (fabs(w) > HOMGPOINT3D_EPS){
312  data_[0] = data_[0] / w;
313  data_[1] = data_[1] / w;
314  data_[2] = data_[2] / w;
315  data_[3] = data_[3] / w;
316  } else {
317  BEXCEPTION("cannot homogenize, w is zero: "<<w);
318  }
319  }
320 
321  inline void HomgPoint3D::Set(const HOMGPOINT3D_TYPE & x,
322  const HOMGPOINT3D_TYPE & y,
323  const HOMGPOINT3D_TYPE & z)
324  {
325  Vector4<HOMGPOINT3D_TYPE>::Set(x, y, z, 1);
326  }
327 
329  {
330  data_[0] = vec[0];
331  data_[1] = vec[1];
332  data_[2] = vec[2];
333  data_[3] = 1.0;
334  }
335 
337  {
338  HOMGPOINT3D_TYPE w = GetW();
339  if (w != 0.0){
340  dest[0] = data_[0] / w;
341  dest[1] = data_[1] / w;
342  dest[2] = data_[2] / w;
343  } else {
344  dest[0] = data_[0];
345  dest[1] = data_[1];
346  dest[2] = data_[2];
347  }
348  }
349 
351  {
353  this->GetEuclidean(dest);
354  return dest;
355  }
356 
357  inline HOMGPOINT3D_TYPE HomgPoint3D::GetEuclideanX() const
358  {
359  HOMGPOINT3D_TYPE w = GetW();
360  BIASASSERT(w != 0.0);
361  return (data_[0] / w);
362  }
363 
364  inline HOMGPOINT3D_TYPE HomgPoint3D::GetEuclideanY() const
365  {
366  HOMGPOINT3D_TYPE w = GetW();
367  BIASASSERT(w != 0.0);
368  return (data_[1] / w);
369  }
370 
371  inline HOMGPOINT3D_TYPE HomgPoint3D::GetEuclideanZ() const
372  {
373  HOMGPOINT3D_TYPE w = GetW();
374  BIASASSERT(w != 0.0);
375  return (data_[2] / w);
376  }
377 
378  inline bool HomgPoint3D::operator==(const HomgPoint3D& arg) const
379  {
380  //check 'homogenized' equality
381  bool res = (data_[0]*arg[3] == data_[3]*arg[0])
382  &&(data_[1]*arg[3] == data_[3]*arg[1])
383  &&(data_[2]*arg[3] == data_[3]*arg[2]);
384  //return 'homogenized' equality if at least one point is not at infinity
385  if ((data_[3]!=0.0)||(arg[3]!=0.0)) return res;
386  //if both points are at infinity, check if they're parallel
387  res = (data_[2]*arg[1] == data_[1]*arg[2])
388  && (data_[2]*arg[0] == data_[0]*arg[2])
389  && (data_[1]*arg[0] == data_[0]*arg[1]);
390  return res;
391  }
392 
393  inline bool HomgPoint3D::operator!=(const HomgPoint3D& arg) const
394  {
395  return !(*this == arg);
396  }
397 
398  inline bool HomgPoint3D::IsAtInfinity() const
399  {
400  return (data_[3] > -HOMGPOINT3D_EPS && data_[3] < HOMGPOINT3D_EPS);
401  }
402 
403  inline HOMGPOINT3D_TYPE
404  HomgPoint3D::Distance(const HomgPoint3D& point) const
405  {
406  if ((data_[3] > -HOMGPOINT3D_EPS && data_[3] < HOMGPOINT3D_EPS) ||
407  (point[3] > -HOMGPOINT3D_EPS && point[3] < HOMGPOINT3D_EPS)) {
408  return HOMGPOINT3D_TYPE_INF;
409  }
410  HOMGPOINT3D_TYPE dx=data_[0]/data_[3] - point[0]/point[3];
411  HOMGPOINT3D_TYPE dy=data_[1]/data_[3] - point[1]/point[3];
412  HOMGPOINT3D_TYPE dz=data_[2]/data_[3] - point[2]/point[3];
413  return sqrt(dx*dx + dy*dy + dz*dz);
414  }
415 
416 } // namespace BIAS
417 #endif
418 
HOMGPOINT3D_TYPE GetX() const
Definition: HomgPoint3D.hh:235
HOMGPOINT3D_TYPE GetEuclidianY() const
Definition: HomgPoint3D.hh:196
bool operator==(const HomgPoint3D &arg) const
Comparison operator &#39;equal&#39;.
Definition: HomgPoint3D.hh:378
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
Definition: HomgPoint3D.hh:321
void Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint3D.hh:308
void Set(const T &scalar)
set all elements to a scalat value
Definition: Vector4.hh:421
HOMGPOINT3D_TYPE GetEuclideanX() const
return the euclidean coordinates Only possible for w !=0 because w==0 describes points of infinity wh...
Definition: HomgPoint3D.hh:357
HOMGPOINT3D_TYPE GetW() const
Definition: HomgPoint3D.hh:257
HomgPoint3D(const HOMGPOINT3D_TYPE &scalar)
assignment with a constant value for all elements
Definition: HomgPoint3D.hh:80
HomgPoint3D(const HOMGPOINT3D_TYPE *pv)
assignment with an array of values which is copied into this ones class members
Definition: HomgPoint3D.hh:87
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
Vector3< HOMGPOINT3D_TYPE > GetEuclidian() const
Definition: HomgPoint3D.hh:179
HOMGPOINT3D_TYPE data_[VECTOR4SIZE]
Definition: Vector4.hh:375
void Copy(const HOMGPOINT3D_TYPE *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
bool operator!=(const HomgPoint3D &arg) const
Comparison operator &#39;not equal&#39;.
Definition: HomgPoint3D.hh:393
HomgPoint3D(const HomgPoint3D &p)
Definition: HomgPoint3D.hh:67
HOMGPOINT3D_TYPE Distance(const HomgPoint3D &point) const
Definition: HomgPoint3D.hh:404
bool IsAtInfinity() const
Definition: HomgPoint3D.hh:398
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z, const HOMGPOINT3D_TYPE &w)
set elementwise
Definition: HomgPoint3D.hh:161
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
HOMGPOINT3D_TYPE GetEuclideanZ() const
Definition: HomgPoint3D.hh:371
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the...
Definition: Vector4.hh:177
HomgPoint3D & operator=(const Vector4< HOMGPOINT3D_TYPE > &vec)
assignment operator set the elements of this HomgPoint to the element of vec
Definition: HomgPoint3D.hh:276
Vector3< HOMGPOINT3D_TYPE > GetEuclidean() const
Definition: HomgPoint3D.hh:350
HOMGPOINT3D_TYPE GetEuclidianZ() const
Definition: HomgPoint3D.hh:199
HOMGPOINT3D_TYPE GetEuclidianX() const
deprecated versions
Definition: HomgPoint3D.hh:193
HOMGPOINT3D_TYPE GetY() const
Definition: HomgPoint3D.hh:240
void GetEuclidian(Vector3< HOMGPOINT3D_TYPE > &dest) const
deprecated versions
Definition: HomgPoint3D.hh:176
bool IsHomogenized() const
Definition: HomgPoint3D.hh:147
HomgPoint3D(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z, const HOMGPOINT3D_TYPE &w)
Definition: HomgPoint3D.hh:112
HomgPoint3D(const Vector4< HOMGPOINT3D_TYPE > &p)
Definition: HomgPoint3D.hh:70
HOMGPOINT3D_TYPE GetEuclideanY() const
Definition: HomgPoint3D.hh:364
Subscript size() const
Definition: vec.h:262
HOMGPOINT3D_TYPE GetZ() const
Definition: HomgPoint3D.hh:245
class BIASGeometryBase_EXPORT HomgPoint3D
HomgPoint3D(char *s)
constructor with element assignment
Definition: HomgPoint3D.hh:93