vgl_homg_plane_3d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_homg_plane_3d.hxx
2 #ifndef vgl_homg_plane_3d_hxx_
3 #define vgl_homg_plane_3d_hxx_
4 //:
5 // \file
6 
7 #include <cmath>
8 #include <iostream>
9 #include "vgl_homg_plane_3d.h"
10 #include <cassert>
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 #include <vgl/vgl_plane_3d.h>
15 #include <vgl/vgl_homg_point_3d.h>
17 
18 //: Construct from non-homogeneous plane
19 template <class Type>
21  : a_(pl.a()), b_(pl.b()), c_(pl.c()), d_(pl.d()) {}
22 
23 //: Construct from three points
24 template <class Type>
26  vgl_homg_point_3d<Type> const& p2,
27  vgl_homg_point_3d<Type> const& p3)
28 : a_(p1.w()*(p2.y()*p3.z()-p2.z()*p3.y())
29  +p2.w()*(p3.y()*p1.z()-p3.z()*p1.y())
30  +p3.w()*(p1.y()*p2.z()-p1.z()*p2.y()))
31 , b_(p1.w()*(p2.z()*p3.x()-p2.x()*p3.z())
32  +p2.w()*(p3.z()*p1.x()-p3.x()*p1.z())
33  +p3.w()*(p1.z()*p2.x()-p1.x()*p2.z()))
34 , c_(p1.w()*(p2.x()*p3.y()-p2.y()*p3.x())
35  +p2.w()*(p3.x()*p1.y()-p3.y()*p1.x())
36  +p3.w()*(p1.x()*p2.y()-p1.y()*p2.x()))
37 , d_(p1.x()*(p2.z()*p3.y()-p2.y()*p3.z())
38  +p2.x()*(p1.y()*p3.z()-p1.z()*p3.y())
39  +p3.x()*(p1.z()*p2.y()-p1.y()*p2.z()))
40 {
41  assert(a_||b_||c_||d_); // points should not be collinear or coinciding
42 }
43 
44 //: Construct from two concurrent lines
45 template <class Type>
48 {
49  assert(concurrent(l1,l2));
53  if (collinear(p1,p2,p3)) p3 = l2.point_infinite();
54  *this = vgl_homg_plane_3d<Type>(p1,p2,p3);
55 }
56 
57 //: Construct from normal and a point
58 // This will fail when the given point is at infinity!
59 template <class Type>
62  : a_(n.x()*p.w()), b_(n.y()*p.w()), c_(n.z()*p.w()),
63  d_(-(n.x()*p.x()+n.y()*p.y()+n.z()*p.z())) {}
64 
65 //: divide all coefficients by sqrt(a^2 + b^2 + c^2)
66 template <class Type>
68 {
69  double sum = a_*a_ + b_*b_ + c_*c_;
70  if (sum<1e-12) // don't normalize plane at infinity
71  return;
72  double den = std::sqrt(sum);
73  double an= (double)a()/den; a_ = (Type)an;
74  double bn= (double)b()/den; b_ = (Type)bn;
75  double cn= (double)c()/den; c_ = (Type)cn;
76  double dn= (double)d()/den; d_ = (Type)dn;
77  //standardize so that largest of (|a|,|b|,|c|) is positive
78  if ((std::fabs(an)>=std::fabs(bn) && std::fabs(an)>=std::fabs(cn) && an < 0) ||
79  (std::fabs(bn)>std::fabs(an) && std::fabs(bn)>=std::fabs(cn) && bn < 0) ||
80  (std::fabs(cn)>std::fabs(an) && std::fabs(cn)>std::fabs(bn) && cn < 0))
81  a_ = -a_, b_ = -b_, c_ = -c_, d_ = -d_;
82  return;
83 }
84 
85 template <class Type>
87 {
88  return (this==&p) ||
89  ( (a()*p.b()==p.a()*b())
90  && (a()*p.c()==p.a()*c())
91  && (a()*p.d()==p.a()*d())
92  && (b()*p.c()==p.b()*c())
93  && (b()*p.d()==p.b()*d())
94  && (c()*p.d()==p.c()*d()) );
95 }
96 
97 template <class Type>
98 std::ostream& operator<<(std::ostream& s, const vgl_homg_plane_3d<Type>& p)
99 {
100  return s << " <vgl_homg_plane_3d "
101  << p.a() << " x + "
102  << p.b() << " y + "
103  << p.c() << " z + "
104  << p.d() << " w = 0 >";
105 }
106 
107 template <class Type>
108 std::istream& operator>>(std::istream& s, vgl_homg_plane_3d<Type>& p)
109 {
110  Type a, b, c, d;
111  s >> a >> b >> c >> d;
112  p.set(a,b,c,d);
113  return s;
114 }
115 
116 #undef VGL_HOMG_PLANE_3D_INSTANTIATE
117 #define VGL_HOMG_PLANE_3D_INSTANTIATE(T) \
118 template class vgl_homg_plane_3d<T >; \
119 template std::ostream& operator<<(std::ostream&, vgl_homg_plane_3d<T >const&); \
120 template std::istream& operator>>(std::istream&, vgl_homg_plane_3d<T >&)
121 
122 #endif // vgl_homg_plane_3d_hxx_
homogeneous plane in 3D projective space
point in projective 3D space
void normalize()
divide all coefficients by sqrt(a^2 + b^2 + c^2).
Represents a homogeneous 3D plane.
Definition: vgl_fwd.h:22
Type b() const
Return y coefficient.
Type c() const
Return z coefficient.
bool concurrent(l const &l1, l const &l2, l const &l3)
Are three lines concurrent, i.e., do they pass through a common point?.
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?.
vgl_homg_plane_3d()=default
Represents a Euclidean 3D plane.
Definition: vgl_fwd.h:23
Type a() const
Return x coefficient.
bool operator==(vgl_homg_plane_3d< Type > const &pl) const
the comparison operator.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
void set(Type ta, Type tb, Type tc, Type td)
Set equation a*x+b*y+c*z+d*w=0.
a plane in 3D nonhomogeneous space
vgl_homg_point_3d< Type > point_finite() const
Finite point (Could be an ideal point, if the whole line is at infinity.).
vgl_homg_point_3d< Type > point_infinite() const
Infinite point: the intersection of the line with the plane at infinity.
Represents a homogeneous 3D line using two points.
Definition: vgl_fwd.h:15