vgl_closest_point.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_closest_point.h
2 #ifndef vgl_closest_point_h_
3 #define vgl_closest_point_h_
4 //:
5 // \file
6 // \brief Set of closest-point functions
7 // \author Peter Vanroose
8 //
9 // All these functions have two arguments which are geometry objects, and
10 // return either a pair of points (one from each object) which are the ones
11 // closest to each other, or just 1 point in case one of the objects is a point.
12 //
13 // See also vgl_distance if you only need the shortest distance between two
14 // geometric objects and not the actual closest points.
15 //
16 // \verbatim
17 // Modifications
18 // 5 June 2003 Peter Vanroose created from bits and pieces in vgl_distance
19 // 5 June 2003 Brendan McCane added closest-point algo for 3D lines
20 // 11 June 2003 Peter Vanroose added closest-point on 3D line from point
21 // 14 Nov. 2003 Peter Vanroose made all functions templated
22 // 25 Sept 2004 Peter Vanroose added full 3D interface
23 // \endverbatim
24 
25 #include <utility>
26 #include <limits>
27 #include <vgl/vgl_fwd.h> // forward declare various vgl classes
28 #ifdef _MSC_VER
29 # include <vcl_msvc_warnings.h>
30 #endif
32 //: Closest point to \a (x,y) on the line segment \a (x1,y1)-(x2,y2)
33 template <class T>
34 void vgl_closest_point_to_linesegment(T& ret_x, T& ret_y,
35  T x1, T y1,
36  T x2, T y2,
37  T x, T y);
38 
39 //: Closest point to \a (x,y,z) on the line segment \a (x1,y1,z1)-(x2,y2,z2)
40 template <class T>
41 void vgl_closest_point_to_linesegment(T& ret_x, T& ret_y, T& ret_z,
42  T x1, T y1, T z1,
43  T x2, T y2, T z2,
44  T x, T y, T z);
45 
46 //: Closest point to \a (x,y) on open polygon \a (px[i],py[i])
47 // Also returns the index of the polygon line segment where this point lies.
48 template <class T>
49 int vgl_closest_point_to_non_closed_polygon(T& ret_x, T& ret_y,
50  T const px[], T const py[], unsigned int n,
51  T x, T y);
52 
53 //: Closest point to \a (x,y,z) on open polygon \a (px[i],py[i],pz[i])
54 // Also returns the index of the polygon line segment where this point lies.
55 template <class T>
56 int vgl_closest_point_to_non_closed_polygon(T& ret_x, T& ret_y, T& ret_z,
57  T const px[], T const py[], T const pz[], unsigned int n,
58  T x, T y, T z);
59 
60 //: Closest point to \a (x,y) on closed polygon \a (px[i],py[i])
61 // Also returns the index of the polygon line segment where this point lies.
62 template <class T>
63 int vgl_closest_point_to_closed_polygon(T& ret_x, T& ret_y,
64  T const px[], T const py[], unsigned int n,
65  T x, T y);
66 
67 //: Closest point to \a (x,y,z) on closed polygon \a (px[i],py[i],pz[i])
68 // Also returns the index of the polygon line segment where this point lies.
69 template <class T>
70 int vgl_closest_point_to_closed_polygon(T& ret_x, T& ret_y, T& ret_z,
71  T const px[], T const py[], T const pz[], unsigned int n,
72  T x, T y, T z);
73 
74 //: Return the point on the given line closest to the origin
75 // \relatesalso vgl_line_2d
76 // \relatesalso vgl_point_2d
77 template <class T>
79 
80 //: Return the point on the given line closest to the origin
81 // \relatesalso vgl_homg_line_2d
82 // \relatesalso vgl_homg_point_2d
83 template <class T>
85 
86 //: Return the point on the given plane closest to the origin
87 // \relatesalso vgl_plane_3d
88 // \relatesalso vgl_point_3d
89 template <class T>
91 
92 //: Return the point on the given plane closest to the origin
93 // \relatesalso vgl_homg_plane_3d
94 // \relatesalso vgl_homg_point_3d
95 template <class T>
97 
98 //: Return the point on the given line closest to the origin
99 // \relatesalso vgl_line_3d_2_points
100 // \relatesalso vgl_point_3d
101 template <class T>
103 
104 //: Return the point on the given line closest to the origin
105 // \relatesalso vgl_homg_line_3d_2_points
106 // \relatesalso vgl_homg_point_3d
107 template <class T>
109 
110 //: Return the point on the given line closest to the given point
111 // \relatesalso vgl_point_2d
112 // \relatesalso vgl_line_2d
113 template <class T>
115  vgl_point_2d<T> const& p);
116 template <class T> inline
118  vgl_line_2d<T> const& l)
119 { return vgl_closest_point(l,p); }
120 
121 //: Return the point on the given line closest to the given point
122 // \relatesalso vgl_homg_point_2d
123 // \relatesalso vgl_homg_line_2d
124 template <class T>
126  vgl_homg_point_2d<T> const& p);
127 template <class T> inline
129  vgl_homg_line_2d<T> const& l)
130 { return vgl_closest_point(l,p); }
131 
132 //: Return the point on the given plane closest to the given point
133 // \relatesalso vgl_point_3d
134 // \relatesalso vgl_plane_3d
135 template <class T>
137  vgl_point_3d<T> const& p);
138 template <class T> inline
140  vgl_plane_3d<T> const& pl)
141 { return vgl_closest_point(pl,p); }
142 
143 //: Return the point on the given plane closest to the given point
144 // \relatesalso vgl_homg_point_3d
145 // \relatesalso vgl_homg_plane_3d
146 template <class T>
148  vgl_homg_point_3d<T> const& p);
149 template <class T> inline
151  vgl_homg_plane_3d<T> const& pl)
152 { return vgl_closest_point(pl,p); }
153 
154 //: Return the point on the given polygon closest to the given point
155 // If the third argument is "false", the edge from last to first point of
156 // each polygon sheet is not considered part of the polygon.
157 // \relatesalso vgl_point_2d
158 // \relatesalso vgl_polygon
159 template <class T>
161  vgl_point_2d<T> const& point,
162  bool closed=true);
163 
164 template <class T> inline
166  vgl_polygon<T> const& poly,
167  bool closed=true)
168 { return vgl_closest_point(poly, point, closed); }
169 
170 //: Return the two points of nearest approach of two 3D lines, one on each line.
171 //
172 // There are 3 cases: the lines intersect (hence these two points are equal);
173 // the lines are parallel (an infinite number of solutions viz all points);
174 // the lines are neither parallel nor do they intersect (the general case).
175 // This method handles all 3 cases. In all cases, a pair of points is returned;
176 // in case 1, the two returned points are equal;
177 // in case 2, both points are the common point at infinity of the two lines.
178 //
179 // Note that case 2 also comprises the case where the given lines are identical.
180 // Hence, when observing a point at infinity as a return value, one should
181 // interpret this as "all points are closest points".
182 //
183 // \param line1
184 // \param line2
185 //
186 // \return std::pair<vgl_homg_point_3d<T>,vgl_homg_point_3d<T> >
187 // \relatesalso vgl_homg_line_3d_2_points
188 //
189 // \author Paul Bourke, modified for use in VXL by Brendan McCane
190 //
191 // \note This routine is adapted from code written by Paul Bourke and
192 // available online at
193 // http://astronomy.swin.edu.au/~pbourke/geometry/lineline3d/
194 
195 template <class T>
196 std::pair<vgl_homg_point_3d<T>, vgl_homg_point_3d<T> >
198  vgl_homg_line_3d_2_points<T> const& line2);
199 
200 //: Return the point on the given line which is closest to the given point.
201 // If the given point is at infinity, the point at infinity of the line is returned.
202 // \relatesalso vgl_homg_line_3d_2_points
203 // \relatesalso vgl_homg_point_3d
204 template <class T>
206  vgl_homg_point_3d<T> const& p);
207 
208 template <class T> inline
211 { return vgl_closest_point(l,p); }
212 
213 //: Return the point on the given line which is closest to the given point.
214 // \relatesalso vgl_line_3d_2_points
215 // \relatesalso vgl_point_3d
216 template <class T>
218  vgl_point_3d<T> const& p);
219 
220 template <class T> inline
222  vgl_line_3d_2_points<T> const& l)
223 { return vgl_closest_point(l,p); }
224 
225 template <class T> inline
227  vgl_infinite_line_3d<T> const& l){
228  vgl_line_3d_2_points<T> l2(l.point(), l.point_t(T(1)));
229  return vgl_closest_point(p,l2);}
230 
231 template <class T> inline
233  vgl_point_3d<T> const& p){
234  return vgl_closest_point(p,l);}
235 
236 template <class T>
238  vgl_ray_3d<T> const& r);
239 
240 template <class T> inline
242  vgl_point_3d<T> const& p){
243  return vgl_closest_point(p,r);}
244 //: Return the point on the given line which is closest to the given point.
245 // The closest point is expressed in parametric form.
246 // \relatesalso vgl_line_3d_2_points
247 // \relatesalso vgl_point_3d
248 // \sa vgl_line_3d_2_points::point_t()
249 // \sa vgl_closest_point(vgl_line_3d_2_points<T> const&, vgl_point_3d<T> const&)
250 template <class T>
252  vgl_point_3d<T> const& p);
253 
254 template <class T> inline
256  vgl_line_3d_2_points<T> const& l)
257 { return vgl_closest_point_t(l,p); }
258 
259 
260 //: Return the points of closest approach on 2 3D lines.
261 // Uses non-homogeneous representations.
262 // \return The pair of closest points, the first on \a l1, the second on \a l2.
263 // \retval unique If provided, will be set to true if the returned points are unique,
264 // otherwise many solutions exist and the returned points are an arbitrary choice.
265 // The distance between the points is still valid, however.
266 // \relatesalso vgl_line_3d_2_points
267 template <class T>
268 std::pair<vgl_point_3d<T>, vgl_point_3d<T> >
270  const vgl_line_3d_2_points<T>& l2,
271  bool* unique=nullptr);
272 
273 
274 //: Return the points of closest approach on two infinite 3D lines.
275 // Uses non-homogeneous representations.
276 // \return The pair of closest points, the first on \a l1, the second on \a l2.
277 // \retval unique If provided, will be set to true if the returned points are unique,
278 // otherwise many solutions exist and the returned points are an arbitrary choice.
279 // The distance between the points is still valid, however.
280 // \relatesalso vgl_line_3d_2_points
281 template <class T>
282 std::pair<vgl_point_3d<T>, vgl_point_3d<T> >
284  const vgl_infinite_line_3d<T>& l2,
285  bool* unique=nullptr)
286 {
287  vgl_line_3d_2_points<T> l21(l1.point(), l1.point_t(T(1)));
288  vgl_line_3d_2_points<T> l22(l2.point(), l2.point_t(T(1)));
289  return vgl_closest_points(l21, l22, unique);
290 }
291 
292 //: Return the points of closest approach on 2 3D line segments.
293 // Uses non-homogeneous representations.
294 // \return The pair of closest points, the first on \a l1, the second on \a l2.
295 // \retval unique If provided, will be set to true if the returned points are unique,
296 // otherwise many solutions exist and the returned points are an arbitrary choice.
297 // The distance between the points is still valid, however.
298 // \relatesalso vgl_line_segment_3d
299 template <class T>
300 std::pair<vgl_point_3d<T>, vgl_point_3d<T> >
302  const vgl_line_segment_3d<T>& l2,
303  bool* unique=nullptr);
304 
305 //: Return the closest point on a line segment \a l to a point \a p in 2D
306 // \relatesalso vgl_point_2d
307 // \relatesalso vgl_line_segment_2d
308 // \sa vgl_distance_to_linesegment()
309 template <class T>
311  vgl_point_2d<T> const& p);
312 template <class T> inline
314  vgl_line_segment_2d<T> const& l) { return vgl_closest_point(l,p); }
315 
316 //: Return the closest point on a line segment \a l to a point \a p in 3D
317 // \relatesalso vgl_point_3d
318 // \relatesalso vgl_line_segment_3d
319 // \sa vgl_distance_to_linesegment()
320 template <class T>
322  vgl_point_3d<T> const& p);
323 template <class T> inline
325  vgl_line_segment_3d<T> const& l) { return vgl_closest_point(l,p); }
326 
327 //: Return the closest point on a sphere \a s to a point \a p in 3D
328 // \relatesalso vgl_point_3d
329 template <class T>
331  vgl_point_3d<T> const& p);
332 
333 //: Return the closest point on a pointset \a ptset to a point \a p in 3D
334 // \relatesalso vgl_point_3d. If ptset has normals, the closest point on the plane
335 // passing trough the closest point is returned. If that planar point is further than dist away from
336 // the closest point in ptset then the closest point in the ptset is returned.
337 //
338 template <class T>
340  T dist = std::numeric_limits<T>::max());
341 
342 //: Return the closest point on a cubic spline
343 template <class T>
345 
346 #endif // vgl_closest_point_h_
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
void vgl_closest_point_to_linesegment(T &ret_x, T &ret_y, T x1, T y1, T x2, T y2, T x, T y)
Closest point to (x,y) on the line segment (x1,y1)-(x2,y2).
Represents a homogeneous 3D point.
Definition: vgl_fwd.h:9
A class to hold a non-homogeneous representation of a 3D line.
Definition: vgl_fwd.h:17
int vgl_closest_point_to_closed_polygon(T &ret_x, T &ret_y, T const px[], T const py[], unsigned int n, T x, T y)
Closest point to (x,y) on closed polygon (px[i],py[i]).
vgl_point_2d< T > vgl_closest_point_origin(vgl_line_2d< T > const &l)
Return the point on the given line closest to the origin.
vgl_point_2d< T > vgl_closest_point(vgl_line_2d< T > const &l, vgl_point_2d< T > const &p)
Return the point on the given line closest to the given point.
vgl_point_3d< Type > point_t(const double t) const
Return a point on the line defined by a scalar parameter t.
Represents a Euclidean 3D plane.
Definition: vgl_fwd.h:23
Represents a 3D line segment using two points.
Definition: vgl_fwd.h:19
Represents a 3-d ray.
Definition: vgl_fwd.h:21
int vgl_closest_point_to_non_closed_polygon(T &ret_x, T &ret_y, T const px[], T const py[], unsigned int n, T x, T y)
Closest point to (x,y) on open polygon (px[i],py[i]).
Represents a 3-d line with position defined in the orthogonal plane passing through the origin.
Definition: vgl_fwd.h:20
#define l
A 3-d infinite line with position parameterized by orthogonal plane coordinates.
double vgl_closest_point_t(vgl_line_3d_2_points< T > const &l, vgl_point_3d< T > const &p)
Return the point on the given line which is closest to the given point.
Represents a homogeneous 2D point.
Definition: vgl_fwd.h:8
Store a polygon.
Definition: vgl_area.h:6
vgl_point_3d< Type > point() const
Return the point on the line closest to the origin.
std::pair< vgl_homg_point_3d< T >, vgl_homg_point_3d< T > > vgl_closest_points(vgl_homg_line_3d_2_points< T > const &line1, vgl_homg_line_3d_2_points< T > const &line2)
Return the two points of nearest approach of two 3D lines, one on each line.
Represents a homogeneous 3D line using two points.
Definition: vgl_fwd.h:15