vgl_intersection.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_intersection.h
2 #ifndef vgl_intersection_h_
3 #define vgl_intersection_h_
4 //:
5 // \file
6 // \brief Set of intersection functions
7 // \author Jan 25, 2007 Gamze Tunali
8 //
9 // For intersections of "homogeneous coordinates" objects like vgl_homg_line_2d<T>,
10 // see the static methods of vgl/algo/vgl_homg_operators_2d<T> and _3d.
11 //
12 // \verbatim
13 // Modifications
14 // 01 Mar 2007 - Gamze Tunali - split up into vgl/algo and vgl parts
15 // 21 Jul 2009 - Peter Vanroose - added box intersection (2d and 3d)
16 // 21 Jul 2009 - Peter Vanroose - added inlined point intersection functions
17 // \endverbatim
18 
19 #include <vector>
20 #include <vgl/vgl_fwd.h> // forward declare various vgl classes
21 #include <vgl/vgl_box_2d.h> // method "contains()"
22 #include <vgl/vgl_box_3d.h> // method "contains()"
23 #include <vgl/vgl_point_2d.h> // method "operator==()"
24 #include <vgl/vgl_point_3d.h> // method "operator==()"
28 #include <vgl/vgl_pointset_3d.h>
29 #ifdef _MSC_VER
30 # include <vcl_msvc_warnings.h>
31 #endif
32 
33 //: Return true if the two points intersect, i.e., coincide
34 // \relatesalso vgl_point_2d
35 template <class T>
36 inline bool vgl_intersection(vgl_point_2d<T> const& p0,
37  vgl_point_2d<T> const& p1)
38 { return p0 == p1; }
39 
40 //: Return true if the two points intersect, i.e., coincide
41 // \relatesalso vgl_point_2d
42 template <class T>
43 inline bool vgl_intersection(vgl_point_3d<T> const& p0,
44  vgl_point_3d<T> const& p1)
45 { return p0 == p1; }
46 
47 //: Return true if line intersects box. If so, compute intersection points.
48 // \relatesalso vgl_line_2d
49 template <class T>
50 bool vgl_intersection(vgl_box_2d<T> const& box,
51  vgl_line_2d<T> const& line,
52  vgl_point_2d<T>& p0,
53  vgl_point_2d<T>& p1);
54 
55 //: Return true if line intersects box.If so,return the line segment inside box.
56 // \relatesalso vgl_line_2d
57 template <class T>
58 bool vgl_intersection(vgl_box_2d<T> const& box,
59  vgl_line_segment_2d<T> const& line,
60  vgl_line_segment_2d<T>& int_line);
61 
62 //: Returns the number of intersections of a line segment with a box, up to two are returned in p0 and p1.(warning! one intersection could be either p0 or p1)
63 // \relatesalso vgl_line_segment_2d
64 template <class T>
65 unsigned vgl_intersection(vgl_box_2d<T> const& box,
66  vgl_line_segment_2d<T> const& line,
67  vgl_point_2d<T>& p0,
68  vgl_point_2d<T>& p1);
69 
70 //: Return the intersection point of two concurrent lines
71 // \relatesalso vgl_line_3d_2_points
72 //Allows intersection points outside the line segments
73 //Throws an assertion if lines not concurrent
74 template <class T>
76  vgl_line_3d_2_points<T> const& l2);
77 
78 //: Return the intersection point of segments of two concurrent lines. Returns false if the intersection point is not inside both line segments
79 // \relatesalso vgl_line_segment_3d.
80 //
81 template <class T>
83  vgl_line_segment_3d<T> const& l2,
84  vgl_point_3d<T>& i_pnt);
85 
86 //: Return the intersection point of segments of a concurrent line and line segment pair. Returns false if the intersection point is not inside both line segments
87 // \relatesalso vgl_line_segment_3d
88 // \relatesalso vgl_line_3d_2_points
89 template <class T>
91  vgl_line_segment_3d<T> const& l2,
92  vgl_point_3d<T>& i_pnt);
93 
94 template <class T> inline
96  vgl_line_3d_2_points<T> const& l2,
97  vgl_point_3d<T>& i_pnt)
98 {
99  return vgl_intersection(l2, l1, i_pnt);
100 }
101 
102 //: Return the intersection point of infinite lines, if concurrent.
103 // \relatesalso vgl_infinite_line_3d
104 template <class T>
106  vgl_infinite_line_3d<T> const& l2,
107  vgl_point_3d<T>& i_pnt);
108 
109 //: Return the intersection point of rays. Returns false if rays are parallel or intersect outside of positive ray domain
110 // \relatesalso vgl_ray_3d
111 template <class T>
112 bool vgl_intersection(vgl_ray_3d<T> const& r1,
113  vgl_ray_3d<T> const& r2,
114  vgl_point_3d<T>& i_pnt);
115 
116 //: Return the intersection point of two lines. Return false if lines are parallel
117 // \relatesalso vgl_line_2d
118 template <class T>
119 bool vgl_intersection(vgl_line_2d<T> const& line0,
120  vgl_line_2d<T> const& line1,
121  vgl_point_2d<T> &intersection_point );
122 
123 
124 //: Return the intersection point of a line and a plane.
125 // \relatesalso vgl_line_3d_2_points
126 // \relatesalso vgl_plane_3d
127 template <class T>
129  vgl_plane_3d<T> const& plane);
130 
131 //: Return the intersection point of a line and a plane.
132 // \relatesalso vgl_line_segment_3d
133 // \relatesalso vgl_plane_3d
134 template <class T>
136  vgl_plane_3d<T> const& plane,
137  vgl_point_3d<T> & i_pt);
138 
139 
140 //: Return the intersection point of a line and a plane.
141 // \relatesalso vgl_line_segment_3d
142 // \relatesalso vgl_plane_3d
143 template <class T>
145  vgl_plane_3d<T> const& plane,
146  vgl_point_3d<T> & i_pt);
147 
148 //: Return the intersection point of a ray and a plane.
149 // \relatesalso vgl_line_segment_3d
150 // \relatesalso vgl_plane_3d
151 template <class T>
152 bool vgl_intersection(vgl_ray_3d<T> const& ray,
153  vgl_plane_3d<T> const& plane,
154  vgl_point_3d<T> & i_pt);
155 
156 //: Return the intersection line of two planes.
157 // Returns false if planes are effectively parallel
158 // \relatesalso vgl_line_segment_3d
159 // \relatesalso vgl_plane_3d
160 template <class T>
162  vgl_plane_3d<T> const& plane1,
163  vgl_line_segment_3d<T> & line){
165  bool status = vgl_intersection(plane0, plane1, inf_l);
166  if (status)
167  line.set(inf_l.point_t(T(0)), inf_l.point_t(T(1)));
168  return status;
169 }
170 
171 template <class T>
173  vgl_plane_3d<T> const& plane1,
174  vgl_line_3d_2_points<T> & line){
176  bool status = vgl_intersection(plane0, plane1, inf_l);
177  if (status)
178  line.set(inf_l.point_t(T(0)), inf_l.point_t(T(1)));
179  return status;
180 }
181 
182 template <class T>
183 bool vgl_intersection(vgl_plane_3d<T> const& plane0,
184  vgl_plane_3d<T> const& plane1,
185  vgl_infinite_line_3d<T> & line);
186 
187 //: Return the intersection point of three planes.
188 // \relatesalso vgl_plane_3d
189 template <class T>
191  vgl_plane_3d<T> const& p2,
192  vgl_plane_3d<T> const& p3);
193 
194 //: Return true if any point on [p1,p2] is within tol of [q1,q2]
195 // Tests two line segments for intersection or near intersection
196 // (within given tolerance).
197 // \author Dan jackson
198 // \relatesalso vgl_point_2d
199 template <class T>
200 bool vgl_intersection(vgl_point_2d<T> const& p1,
201  vgl_point_2d<T> const& p2,
202  vgl_point_2d<T> const& q1,
203  vgl_point_2d<T> const& q2,
204  double tol = 1e-6);
205 
206 //: Return true if the point lies inside the box
207 // \relatesalso vgl_point_2d
208 // \relatesalso vgl_box_2d
209 template <class T>
210 inline bool vgl_intersection(vgl_box_2d<T> const& b, vgl_point_2d<T> const& p)
211 { return b.contains(p); }
212 
213 //: Return true if the point lies inside the box
214 // \relatesalso vgl_point_2d
215 // \relatesalso vgl_box_2d
216 template <class T>
217 inline bool vgl_intersection(vgl_point_2d<T> const& p, vgl_box_2d<T> const& b)
218 { return b.contains(p); }
219 
220 //: Return true if the point lies inside the box
221 // \relatesalso vgl_point_3d
222 // \relatesalso vgl_box_3d
223 template <class T>
224 inline bool vgl_intersection(vgl_box_3d<T> const& b, vgl_point_3d<T> const& p)
225 { return b.contains(p); }
226 
227 //: Return true if the point lies inside the box
228 // \relatesalso vgl_point_3d
229 // \relatesalso vgl_box_3d
230 template <class T>
231 inline bool vgl_intersection(vgl_point_3d<T> const& p, vgl_box_3d<T> const& b)
232 { return b.contains(p); }
233 
234 //: Return true if line intersects box. If so, compute intersection points.
235 // \relatesalso vgl_infinite_line_3d
236 template <class T>
237 bool vgl_intersection(vgl_box_3d<T> const& box,
238  vgl_infinite_line_3d<T> const& line,
239  vgl_point_3d<T>& p0,
240  vgl_point_3d<T>& p1);
241 //: Return true if ray intersects box. If so, compute intersection points.
242 // If ray origin is inside box then p0==p1
243 // \relatesalso vgl_ray_3d
244 template <class T>
245 bool vgl_intersection(vgl_box_3d<T> const& box,
246  vgl_ray_3d<T> const& ray,
247  vgl_point_3d<T>& p0,
248  vgl_point_3d<T>& p1);
249 //: Return true if a box and plane intersect in 3D
250 // \relatesalso vgl_plane_3d
251 // \relatesalso vgl_box_3d
252 template <class T>
253 bool vgl_intersection(vgl_box_3d<T> const& b, vgl_plane_3d<T> const& plane);
254 
255 
256 //: Return the intersection of two boxes (which is itself either a box, or empty)
257 // \relatesalso vgl_box_2d
258 template <class T>
260 
261 //: Return the intersection of two boxes (which is itself either a box, or empty)
262 // \relatesalso vgl_box_3d
263 template <class T>
265 
266 //: Return true if the box and polygon regions intersect, regions include boundaries
267 // \relatesalso vgl_polygon
268 // \relatesalso vgl_box_2d
269 template <class T>
270 bool vgl_intersection(vgl_box_2d<T> const& b, vgl_polygon<T> const& poly);
271 
272 //: Return the points from the list that lie inside the box
273 // \relatesalso vgl_point_2d
274 // \relatesalso vgl_box_2d
275 template <class T>
276 std::vector<vgl_point_2d<T> > vgl_intersection(vgl_box_2d<T> const& b, std::vector<vgl_point_2d<T> > const& p);
277 
278 //: Return the points from the list that lie inside the box
279 // \relatesalso vgl_point_2d
280 // \relatesalso vgl_box_2d
281 template <class T>
282 std::vector<vgl_point_2d<T> > vgl_intersection(std::vector<vgl_point_2d<T> > const& p, vgl_box_2d<T> const& b);
283 
284 //: Return the points from the list that lie inside the box
285 // \relatesalso vgl_point_3d
286 // \relatesalso vgl_box_3d
287 template <class T>
288 std::vector<vgl_point_3d<T> > vgl_intersection(vgl_box_3d<T> const& b, std::vector<vgl_point_3d<T> > const& p);
289 
290 //: Return the points from the list that lie inside the box
291 // \relatesalso vgl_point_3d
292 // \relatesalso vgl_box_3d
293 template <class T>
294 std::vector<vgl_point_3d<T> > vgl_intersection(std::vector<vgl_point_3d<T> > const& p, vgl_box_3d<T> const& b);
295 
296 //: Find the intersections of a line with a polygon( can have multiple sheets)
297 // \relatesalso vgl_line_2d
298 // \relatesalso vgl_point_2d
299 template <class T>
300 std::vector<vgl_point_2d<T> > vgl_intersection(vgl_polygon<T> const& poly,
301  vgl_line_2d<T> const& line);
302 template <class T>
303 std::vector<vgl_point_2d<T> > vgl_intersection(vgl_line_2d<T> const& line,
304  vgl_polygon<T> const& poly){
305  return vgl_intersection(poly, line);
306 }
307 // SEE vgl_clip.h to compute the intersection (as well as other boolean operations) of two vgl_polygons
308 
309 //: return the intersection of a pointset with a plane, given a tolerance tol
310 // the normal distance from the plane to the point is compared to the tolerance
311 // the points within tolerance are projected along the normal direction onto the plane
312 template <class T>
313 vgl_pointset_3d<T> vgl_intersection(vgl_plane_3d<T> const& plane, vgl_pointset_3d<T> const& ptset, T tol);
314 
315 template <class T>
317  return vgl_intersection(plane, ptset, tol);}
318 
319 //: intersection of a box with the pointset
320 template <class T>
322 
323 template <class T>
325  return vgl_intersection(box, ptset);}
326 
327 
328 template <class T>
329 vgl_pointset_3d<T> vgl_intersection(vgl_plane_3d<T> const& plane, vgl_pointset_3d<T> const& ptset, T tol);
330 
331 #define VGL_INTERSECTION_INSTANTIATE(T) extern "please include vgl/vgl_intersection.hxx first"
332 #define VGL_INTERSECTION_BOX_INSTANTIATE(T) extern "please include vgl/vgl_intersection.hxx first"
333 
334 #endif // vgl_intersection_h_
a point in 2D nonhomogeneous space
void set(vgl_point_3d< Type > const &p1, vgl_point_3d< Type > const &p2)
Assignment.
void set(vgl_point_3d< Type > const &p1, vgl_point_3d< Type > const &p2)
assignment.
Contains class to represent a cartesian 3D bounding box.
bool contains(vgl_point_2d< Type > const &p) const
Return true iff the point p is inside this box.
Definition: vgl_box_2d.hxx:347
vgl_point_3d< T > vgl_intersection(const std::vector< vgl_plane_3d< T > > &p)
Return the intersection point of vector of planes.
A class to hold a non-homogeneous representation of a 3D line.
Definition: vgl_fwd.h:17
a point in 3D nonhomogeneous space
line segment in 3D nonhomogeneous space
A 3-d pointset.
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
Contains class to represent a cartesian 2D bounding box.
Represents a 3-d line with position defined in the orthogonal plane passing through the origin.
Definition: vgl_fwd.h:20
bool contains(vgl_point_3d< Type > const &p) const
Return true iff the point p is inside this box.
Definition: vgl_box_3d.hxx:406
A 3-d infinite line with position parameterized by orthogonal plane coordinates.
non-homogeneous 3D line, represented by 2 points.
Represents a cartesian 3D box.
Definition: vgl_box_3d.h:65
Store a polygon.
Definition: vgl_area.h:6