vgl_homg_operators_3d.h
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_homg_operators_3d.h
2 #ifndef vgl_homg_operators_3d_h_
3 #define vgl_homg_operators_3d_h_
4 //:
5 // \file
6 // \brief 3D homogeneous functions
7 // \author Don Hamilton, Peter Tu
8 // \date Feb 16 2000
9 //
10 // \verbatim
11 // Modifications
12 // 31-oct-00 Peter Vanroose - implementations fixed, and vgl_homg_line_3d typedef'd
13 // 16-Mar-01 Tim Cootes - Tidied up documentation
14 // 14-Jun-04 Peter Vanroose - implemented conjugate(), unitize(), perp_dist_squared(), midpoint(), planes_to_point()
15 // 3-Feb-07 Peter Vanroose - changed vnl_vector to vnl_vector_fixed
16 // \endverbatim
17 
18 #include <vector>
19 #ifdef _MSC_VER
20 # include <vcl_msvc_warnings.h>
21 #endif
22 #include <vnl/vnl_fwd.h>
23 #include <vgl/vgl_fwd.h>
25 
26 //: 3D homogeneous operations
27 template <class Type>
29 {
31 
32  public:
33  //: Get a vnl_vector_fixed representation of a homogeneous object
34  static vnl_vector_fixed<Type,4> get_vector(vgl_homg_point_3d<Type> const& p);
35  //: Get a vnl_vector_fixed representation of a homogeneous object
36  static vnl_vector_fixed<Type,4> get_vector(vgl_homg_plane_3d<Type> const& p);
37 
38  //: Normalize vgl_homg_point_3d<Type> to unit magnitude
39  static void unitize(vgl_homg_point_3d<Type>& a);
40 
41  static double angle_between_oriented_lines(const vgl_homg_line_3d& line1,
42  const vgl_homg_line_3d& line2);
43 
44  //: Return the Euclidean distance between the points
45  static Type distance(const vgl_homg_point_3d<Type>& point1,
46  const vgl_homg_point_3d<Type>& point2);
47  //: Return the squared distance between the points
48  static Type distance_squared(const vgl_homg_point_3d<Type>& point1,
49  const vgl_homg_point_3d<Type>& point2);
50 
51  //: True if the points are closer than Euclidean distance d.
53  const vgl_homg_point_3d<Type>& p2, double d)
54  {
55  if (d <= 0) return false;
56  return distance_squared(p1, p2) < d*d;
57  }
58 
59  //: Get the square of the perpendicular distance to a plane.
60  // This is just the homogeneous form of the familiar
61  // $ \frac{a x + b y + c z + d}{\sqrt{a^2+b^2+c^2}} $ :
62  // \[ d = \frac{(l^\top p)}{p_z\sqrt{l_x^2 + l_y^2 l_z^2}} \]
63  // If either the point or the plane are at infinity an error message is
64  // printed and Homg::infinity is returned.
65  static double perp_dist_squared(const vgl_homg_point_3d<Type>& point,
66  const vgl_homg_plane_3d<Type>& plane);
67  static double perp_dist_squared(const vgl_homg_plane_3d<Type>& plane,
68  const vgl_homg_point_3d<Type>& point)
69  { return perp_dist_squared(point, plane); }
70 
73 #if 0 // not yet implemented
74  static vgl_homg_point_3d<Type> lines_to_point(const vgl_homg_line_3d& line1,
75  const vgl_homg_line_3d& line2);
76  static vgl_homg_point_3d<Type> lines_to_point(const std::vector<vgl_homg_line_3d>& line_list);
77 #endif // 0
78  static double perp_dist_squared(const vgl_homg_line_3d& line,
79  const vgl_homg_point_3d<Type>& point);
80  static double perp_dist_squared(const vgl_homg_point_3d<Type>& point,
81  const vgl_homg_line_3d& line)
82  { return perp_dist_squared(line,point); }
84  const vgl_homg_point_3d<Type>& point);
86  const vgl_homg_point_3d<Type>& point);
88  const vgl_homg_plane_3d<Type>& plane2);
89  static Type plane_plane_angle(const vgl_homg_plane_3d<Type>& plane1,
90  const vgl_homg_plane_3d<Type>& plane2);
91 #if 0 // not yet implemented
92  static vgl_homg_line_3d planes_to_line(const std::vector<vgl_homg_plane_3d<Type> >& plane_list);
93  static vgl_homg_line_3d points_to_line(const vgl_homg_point_3d<Type>& point1,
94  const vgl_homg_point_3d<Type>& point2);
95  static vgl_homg_line_3d points_to_line(const std::vector<vgl_homg_point_3d<Type> >& point_list);
96 
97  static vgl_homg_plane_3d<Type> points_to_plane(const vgl_homg_point_3d<Type>&,
99  const vgl_homg_point_3d<Type>& );
100  static vgl_homg_plane_3d<Type> points_to_plane(const std::vector<vgl_homg_point_3d<Type> >& point_list);
101 #endif // 0
104  const vgl_homg_plane_3d<Type>&);
105  static vgl_homg_point_3d<Type> intersection(const std::vector<vgl_homg_plane_3d<Type> >&);
106 
107  //: Return the midpoint of the line joining two homogeneous points
109  const vgl_homg_point_3d<Type>& p2);
110 
111  //: Intersect a set of 3D planes to find the least-square point of intersection.
112  static vgl_homg_point_3d<Type> planes_to_point(const std::vector<vgl_homg_plane_3d<Type> >& planes);
113 
114  //-----------------------------------------------------------------------------
115  //: Calculates the cross ratio of four collinear points p1, p2, p3 and p4.
116  // This number is projectively invariant, and it is the coordinate of p4
117  // in the reference frame where p2 is the origin (coordinate 0), p3 is
118  // the unity (coordinate 1) and p1 is the point at infinity.
119  // This cross ratio is often denoted as ((p1, p2; p3, p4)) (which also
120  // equals ((p3, p4; p1, p2)) or ((p2, p1; p4, p3)) or ((p4, p3; p2, p1)) )
121  // and is calculated as
122  // \verbatim
123  // p1 - p3 p2 - p3 (p1-p3)(p2-p4)
124  // ------- : -------- = --------------
125  // p1 - p4 p2 - p4 (p1-p4)(p2-p3)
126  // \endverbatim
127  //
128  // In principle, any single nonhomogeneous coordinate from the four points
129  // can be used as parameters for cross_ratio (but of course the same for all
130  // points). The most reliable answer will be obtained when the coordinate with
131  // the largest spacing is used, i.e., the one with smallest slope.
132  //
133  // In this implementation, a least-squares result is calculated when the
134  // points are not exactly collinear.
135 
136  static double cross_ratio(const vgl_homg_point_3d<Type>& p1,
137  const vgl_homg_point_3d<Type>& p2,
138  const vgl_homg_point_3d<Type>& p3,
139  const vgl_homg_point_3d<Type>& p4);
140  static double cross_ratio(const vgl_homg_plane_3d<Type>& p1,
141  const vgl_homg_plane_3d<Type>& p2,
142  const vgl_homg_plane_3d<Type>& p3,
143  const vgl_homg_plane_3d<Type>& p4);
144 
145  //: Conjugate point of three given collinear points.
146  // If cross ratio cr is given (default: -1), the generalized conjugate point
147  // returned is such that the cross ratio ((x1,x2;x3,answer)) = cr.
149  const vgl_homg_point_3d<Type>& b,
150  const vgl_homg_point_3d<Type>& c,
151  double cr = -1.0);
152 
153  //: compute most orthogonal vector with SVD
154  static vnl_vector_fixed<Type,4> most_orthogonal_vector_svd(const std::vector<vgl_homg_plane_3d<Type> >& planes);
155 };
156 
157 //: Homographic transformation of a 3D point through a 4x4 projective transformation matrix
158 template <class Type>
159 vgl_homg_point_3d<Type> operator*(vnl_matrix_fixed<Type,4,4> const& m,
160  vgl_homg_point_3d<Type> const& p);
161 
162 //: Project a 3D point to 2D through a 3x4 projective transformation matrix
163 template <class Type>
164 vgl_homg_point_2d<Type> operator*(vnl_matrix_fixed<Type,3,4> const& m,
165  vgl_homg_point_3d<Type> const& p);
166 
167 //: Homographic transformation of a 3D plane through a 4x4 projective transformation matrix
168 template <class Type>
169 vgl_homg_plane_3d<Type> operator*(vnl_matrix_fixed<Type,4,4> const& m,
170  vgl_homg_plane_3d<Type> const& p);
171 
172 //: Backproject a 2D line through a 4x3 projective transformation matrix
173 template <class Type>
174 vgl_homg_plane_3d<Type> operator*(vnl_matrix_fixed<Type,4,3> const& m,
175  vgl_homg_line_2d<Type> const& l);
176 
177 #define VGL_HOMG_OPERATORS_3D_INSTANTIATE(T) \
178  "Please #include <vgl/algo/vgl_homg_operators_3d.hxx>"
179 
180 #endif // vgl_homg_operators_3d_h_
3D homogeneous operations.
Definition: vgl_algo_fwd.h:29
static double perp_dist_squared(const vgl_homg_point_3d< Type > &point, const vgl_homg_plane_3d< Type > &plane)
Get the square of the perpendicular distance to a plane.
static vgl_homg_point_3d< Type > intersect_line_and_plane(const vgl_homg_line_3d &, const vgl_homg_plane_3d< Type > &)
Return the intersection point of the line and plane.
static vgl_homg_point_3d< Type > intersection(const vgl_homg_plane_3d< Type > &, const vgl_homg_plane_3d< Type > &, const vgl_homg_plane_3d< Type > &)
Compute best-fit intersection of planes in a point.
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
static vgl_homg_point_3d< Type > planes_to_point(const std::vector< vgl_homg_plane_3d< Type > > &planes)
Intersect a set of 3D planes to find the least-square point of intersection.
Represents a homogeneous 3D plane.
Definition: vgl_fwd.h:22
static bool is_within_distance(const vgl_homg_point_3d< Type > &p1, const vgl_homg_point_3d< Type > &p2, double d)
True if the points are closer than Euclidean distance d.
static double cross_ratio(const vgl_homg_point_3d< Type > &p1, const vgl_homg_point_3d< Type > &p2, const vgl_homg_point_3d< Type > &p3, const vgl_homg_point_3d< Type > &p4)
Calculates the cross ratio of four collinear points p1, p2, p3 and p4.
static Type plane_plane_angle(const vgl_homg_plane_3d< Type > &plane1, const vgl_homg_plane_3d< Type > &plane2)
Dihedral angle (of intersection) of 2 planes.
Represents a homogeneous 3D point.
Definition: vgl_fwd.h:9
static vnl_vector_fixed< Type, 4 > most_orthogonal_vector_svd(const std::vector< vgl_homg_plane_3d< Type > > &planes)
compute most orthogonal vector with SVD.
static vgl_homg_point_3d< Type > midpoint(const vgl_homg_point_3d< Type > &p1, const vgl_homg_point_3d< Type > &p2)
Return the midpoint of the line joining two homogeneous points.
static double perp_dist_squared(const vgl_homg_point_3d< Type > &point, const vgl_homg_line_3d &line)
static Type distance_squared(const vgl_homg_point_3d< Type > &point1, const vgl_homg_point_3d< Type > &point2)
Return the squared distance between the points.
static vgl_homg_point_3d< Type > conjugate(const vgl_homg_point_3d< Type > &a, const vgl_homg_point_3d< Type > &b, const vgl_homg_point_3d< Type > &c, double cr=-1.0)
Conjugate point of three given collinear points.
static vgl_homg_line_3d planes_to_line(const vgl_homg_plane_3d< Type > &plane1, const vgl_homg_plane_3d< Type > &plane2)
Return the intersection line of the planes.
static double angle_between_oriented_lines(const vgl_homg_line_3d &line1, const vgl_homg_line_3d &line2)
Return the angle between the (oriented) lines (in radians).
vgl_homg_line_3d_2_points< Type > vgl_homg_line_3d
#define l
vgl_homg_point_1d< T > operator *(vnl_matrix_fixed< T, 2, 2 > const &m, vgl_homg_point_1d< T > const &p)
Transform a point through a 2x2 projective transformation matrix.
static void unitize(vgl_homg_point_3d< Type > &a)
Normalize vgl_homg_point_3d<Type> to unit magnitude.
static vgl_homg_point_3d< Type > perp_projection(const vgl_homg_line_3d &line, const vgl_homg_point_3d< Type > &point)
Compute the perpendicular projection point of p onto l.
static vgl_homg_line_3d perp_line_through_point(const vgl_homg_line_3d &line, const vgl_homg_point_3d< Type > &point)
Return the line which is perpendicular to l and passes through p.
static Type distance(const vgl_homg_point_3d< Type > &point1, const vgl_homg_point_3d< Type > &point2)
Return the Euclidean distance between the points.
static vnl_vector_fixed< Type, 4 > get_vector(vgl_homg_point_3d< Type > const &p)
Get a vnl_vector_fixed representation of a homogeneous object.
static double perp_dist_squared(const vgl_homg_plane_3d< Type > &plane, const vgl_homg_point_3d< Type > &point)
Represents a homogeneous 3D line using two points.
Definition: vgl_fwd.h:15