vgl_homg_point_3d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_homg_point_3d.hxx
2 #ifndef vgl_homg_point_3d_hxx_
3 #define vgl_homg_point_3d_hxx_
4 
5 #include <iostream>
6 #include "vgl_homg_point_3d.h"
8 #ifdef _MSC_VER
9 # include <vcl_msvc_warnings.h>
10 #endif
11 
12 // Note that the given planes must be distinct and not have a line in common!
13 template <class Type>
15  vgl_homg_plane_3d<Type> const& l2,
16  vgl_homg_plane_3d<Type> const& l3)
17 {
18  set(-l1.ny()*l2.nz()*l3.d()-l2.ny()*l3.nz()*l1.d()-l3.ny()*l1.nz()*l2.d()
19  +l1.ny()*l3.nz()*l2.d()+l2.ny()*l1.nz()*l3.d()+l3.ny()*l2.nz()*l1.d(),
20  l1.nz()*l2.d()*l3.nx()+l2.nz()*l3.d()*l1.nx()+l3.nz()*l1.d()*l2.nx()
21  -l1.nz()*l3.d()*l2.nx()-l2.nz()*l1.d()*l3.nx()-l3.nz()*l2.d()*l1.nx(),
22  -l1.d()*l2.nx()*l3.ny()-l2.d()*l3.nx()*l1.ny()-l3.d()*l1.nx()*l2.ny()
23  +l1.d()*l3.nx()*l2.ny()+l2.d()*l1.nx()*l3.ny()+l3.d()*l2.nx()*l1.ny(),
24  l1.nx()*l2.ny()*l3.nz()+l2.nx()*l3.ny()*l1.nz()+l3.nx()*l1.ny()*l2.nz()
25  -l1.nx()*l3.ny()*l2.nz()-l2.nx()*l1.ny()*l3.nz()-l3.nx()*l2.ny()*l1.nz());
26 }
27 
28 template <class Type>
30 {
31  return (this==&other) ||
32  (x()*other.y() == y()*other.x() &&
33  x()*other.z() == z()*other.x() &&
34  x()*other.w() == w()*other.x() &&
35  y()*other.z() == z()*other.y() &&
36  y()*other.w() == w()*other.y() &&
37  z()*other.w() == w()*other.z());
38 }
39 
40 template <class Type>
42  vgl_homg_point_3d<Type> const& p2,
43  vgl_homg_point_3d<Type> const& p3)
44 {
45  if (!p1.ideal() && !p2.ideal() && !p3.ideal())
46  return parallel(p1-p2, p1-p3);
47  if (!p1.ideal() && !p2.ideal() && p3.ideal())
48  return parallel(p1-p2, vgl_vector_3d<Type>(p3.x(),p3.y(),p3.z()));
49  if (!p1.ideal() && p2.ideal() && !p3.ideal())
50  return parallel(p1-p3, vgl_vector_3d<Type>(p2.x(),p2.y(),p2.z()));
51  if (p1.ideal() && !p2.ideal() && !p3.ideal())
52  return parallel(p2-p3, vgl_vector_3d<Type>(p1.x(),p1.y(),p1.z()));
53  if (p1.ideal() && p2.ideal() && !p3.ideal())
54  return false;
55  if (p1.ideal() && !p2.ideal() && p3.ideal())
56  return false;
57  if (!p1.ideal() && p2.ideal() && p3.ideal())
58  return false;
59  // all three are ideal:
60  return (p1.x()*p2.y()-p1.y()*p2.x())*p3.z()
61  +(p3.x()*p1.y()-p3.y()*p1.x())*p2.z()
62  +(p2.x()*p3.y()-p2.y()*p3.x())*p1.z()==0;
63 }
64 
65 template <class T>
67  vgl_homg_point_3d<T>const& p3, vgl_homg_point_3d<T>const& p4)
68 {
69  // least squares solution: (Num_x-CR*Den_x)^2 + (Num_y-CR*Den_y)^2 + (Num_z-CR*Den_z)^2 minimal.
70  double Num_x = (p1.x()*p3.w()-p3.x()*p1.w())*(p2.x()*p4.w()-p4.x()*p2.w());
71  double Num_y = (p1.y()*p3.w()-p3.y()*p1.w())*(p2.y()*p4.w()-p4.y()*p2.w());
72  double Num_z = (p1.z()*p3.w()-p3.z()*p1.w())*(p2.z()*p4.w()-p4.z()*p2.w());
73  double Den_x = (p1.x()*p4.w()-p4.x()*p1.w())*(p2.x()*p3.w()-p3.x()*p2.w());
74  double Den_y = (p1.y()*p4.w()-p4.y()*p1.w())*(p2.y()*p3.w()-p3.y()*p2.w());
75  double Den_z = (p1.z()*p4.w()-p4.z()*p1.w())*(p2.z()*p3.w()-p3.z()*p2.w());
76  if (Den_x == Den_y && Den_y == Den_z) return (Num_x+Num_y+Num_z)/3/Den_x;
77  else return (Den_x*Num_x+Den_y*Num_y+Den_z*Num_z)/(Den_x*Den_x+Den_y*Den_y+Den_z*Den_z);
78 }
79 
80 template <class Type>
81 std::ostream& operator<<(std::ostream& s, vgl_homg_point_3d<Type> const& p)
82 {
83  return s << " <vgl_homg_point_3d ("
84  << p.x() << ',' << p.y() << ','
85  << p.z() << ',' << p.w() << ") >";
86 }
87 
88 template <class Type>
89 std::istream& operator>>(std::istream& s, vgl_homg_point_3d<Type>& p)
90 {
91  Type x, y, z, w;
92  s >> x >> y >> z >> w;
93  p.set(x,y,z,w);
94  return s;
95 }
96 
97 #undef VGL_HOMG_POINT_3D_INSTANTIATE
98 #define VGL_HOMG_POINT_3D_INSTANTIATE(T) \
99 template class vgl_homg_point_3d<T >; \
100 template bool collinear(vgl_homg_point_3d<T >const&,vgl_homg_point_3d<T >const&,vgl_homg_point_3d<T >const&); \
101 template double cross_ratio(vgl_homg_point_3d<T >const&, vgl_homg_point_3d<T >const&, \
102  vgl_homg_point_3d<T >const&, vgl_homg_point_3d<T >const&); \
103 template std::ostream& operator<<(std::ostream&, vgl_homg_point_3d<T >const&); \
104 template std::istream& operator>>(std::istream&, vgl_homg_point_3d<T >&)
105 
106 #endif // vgl_homg_point_3d_hxx_
homogeneous plane in 3D projective space
point in projective 3D space
Represents a homogeneous 3D plane.
Definition: vgl_fwd.h:22
double cross_ratio(vgl_homg_point_1d< T >const &p1, vgl_homg_point_1d< T >const &p2, vgl_homg_point_1d< T >const &p3, vgl_homg_point_1d< T >const &p4)
cross ratio of four points.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
Type d() const
Return homogeneous scaling coefficient.
Represents a homogeneous 3D point.
Definition: vgl_fwd.h:9
bool collinear(l const &l1, vgl_homg_point_3d< Type > const &p)
Does a line pass through a point, i.e., are the point and the line collinear?.
bool operator==(vgl_homg_point_3d< Type > const &other) const
the comparison operator.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
void set(Type px, Type py, Type pz, Type pw=(Type) 1)
Set x,y,z,w.
vgl_homg_point_3d()
Default constructor with (0,0,0,1).
bool ideal(Type tol=(Type) 0) const
Return true iff the point is at infinity (an ideal point).
bool parallel(v const &a, v const &b, double eps=0.0)
are two vectors parallel, i.e., is one a scalar multiple of the other?.