QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_bounding_box.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2007 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(ON_BOUNDING_BOX_INC_)
17#define ON_BOUNDING_BOX_INC_
18
20//
21// ON_BoundingBox - axis aligned bounding box
22//
23
25{
26public:
27 static const ON_BoundingBox EmptyBoundingBox; // ((1.0,0,0,0,0),(-1.0,0.0,0.0))
28
29 ON_BoundingBox(); // creates EmptyBox
30
32 const ON_3dPoint&, // min corner of axis aligned bounding box
33 const ON_3dPoint& // max corner of axis aligned bounding box
34 );
36
37
38 // temporary - use ON_ClippingRegion - this function will be removed soon.
39 int IsVisible(
40 const ON_Xform& bbox2c
41 ) const;
42
43 void Destroy(); // invalidates bounding box
44
45 // operator[] returns min if index <= 0 and max if indes >= 1
46 ON_3dPoint& operator[](int);
47 const ON_3dPoint& operator[](int) const;
48
49 ON_3dPoint Min() const;
50 ON_3dPoint Max() const;
51 ON_3dVector Diagonal() const; // max corner - min corner
52 ON_3dPoint Center() const;
53 ON_3dPoint Corner( // 8 corners of box
54 int, // x_index 0 = Min().x, 1 = Max().x
55 int, // y_index 0 = Min().y, 1 = Max().y
56 int // z_index 0 = Min().z, 1 = Max().z
57 ) const;
58 bool GetCorners(
59 ON_3dPointArray& box_corners // returns list of 8 corner points
60 ) const;
61 bool GetCorners(
62 ON_3dPoint box_corners[8] // returns list of 8 corner points
63 ) const;
64
65 bool IsValid() const; // empty boxes are not valid
66
67 /*
68 Description:
69 Test a bounding box to see if it is degenerate (flat)
70 in one or more directions.
71 Parameters:
72 tolerance - [in] Distances <= tolerance will be considered
73 to be zero. If tolerance is negative (default), then
74 a scale invarient tolerance is used.
75 Returns:
76 @untitled table
77 0 box is not degenerate
78 1 box is a rectangle (degenerate in one direction)
79 2 box is a line (degenerate in two directions)
80 3 box is a point (degenerate in three directions)
81 4 box is not valid
82 */
83 int IsDegenerate(
84 double tolerance = ON_UNSET_VALUE
85 ) const;
86
87
89 // ON_BoundingBox::Transform() updates the bounding box
90 // to be the smallest axis aligned bounding box that contains
91 // the transform of the eight corner points of the input
92 // bounding box.
93 bool Transform( const ON_Xform& );
94
95 double Tolerance() const; // rough guess at a tolerance to use for comparing
96 // objects in this bounding box
97
98
99 // All of these Set() functions set or expand a box to enclose the points in the arguments
100 // If bGrowBox is true, the existing box is expanded, otherwise it is only set to the current point list
101 bool Set(
102 int dim,
103 int is_rat,
104 int count,
105 int stride,
106 const double* point_array,
107 int bGrowBox = false
108 );
109
110 bool Set(
111 const ON_3dPoint& point,
112 int bGrowBox = false
113 );
114
115 bool Set(
116 const ON_SimpleArray<ON_4dPoint>& point_array,
117 int bGrowBox = false
118 );
119
120 bool Set(
121 const ON_SimpleArray<ON_3dPoint>& point_array,
122 int bGrowBox = false
123 );
124
125 bool Set(
126 const ON_SimpleArray<ON_2dPoint>& point_array,
127 int bGrowBox = false
128 );
129
130 bool IsPointIn(
131 const ON_3dPoint& test_point, // point to test
132 int bStrictlyIn = false
133 // true to test for strict ( min < point < max )
134 // false to test for (min <= point <= max)
135 //
136 ) const;
137
139 // Point on or in the box that is closest to test_point.
140 // If test_point is in or on the box, the test_point is returned.
141 ON_3dPoint ClosestPoint(
142 const ON_3dPoint& test_point
143 ) const;
144
145
146 /*
147 Description:
148 Quickly find a lower bound on the distance
149 between the point and this bounding box.
150 Parameters:
151 P - [in]
152 Returns:
153 A distance that is less than or equal to the shortest
154 distance from the line to this bounding box.
155 Put another way, if Q is any point in this bounding box,
156 then P.DistanceTo(Q) >= MinimumDistanceTo(bbox).
157 */
158 double MinimumDistanceTo( const ON_3dPoint& P ) const;
159
160 /*
161 Description:
162 Quickly find an upper bound on the distance
163 between the point and this bounding box.
164 Parameters:
165 P - [in]
166 Returns:
167 A distance that is greater than or equal to the
168 longest distance from the point P to this bounding box.
169 Put another way, if Q is any point in this bounding box,
170 then P.DistanceTo(Q) <= MaximumDistanceTo(bbox).
171 */
172 double MaximumDistanceTo( const ON_3dPoint& P ) const;
173
174
175 /*
176 Description:
177 Quickly find a lower bound on the distance
178 between this and the other bounding box.
179 Parameters:
180 other - [in]
181 Returns:
182 A distance that is less than or equal to the shortest
183 distance between the bounding boxes.
184 Put another way, if Q is any point in this bounding box
185 and P is any point in the other bounding box,
186 then P.DistanceTo(Q) >= MinimumDistanceTo(bbox).
187 */
188 double MinimumDistanceTo( const ON_BoundingBox& other ) const;
189
190 /*
191 Description:
192 Quickly find an upper bound on the distance
193 between this and the other bounding box.
194 Parameters:
195 other - [in]
196 Returns:
197 A distance that is greater than or equal to the longest
198 distance between the bounding boxes.
199 Put another way, if Q is any point in this bounding box
200 and P is any point in the other bounding box,
201 then P.DistanceTo(Q) <= MaximumDistanceTo(bbox).
202 */
203 double MaximumDistanceTo( const ON_BoundingBox& other ) const;
204
205 /*
206 Description:
207 Quickly find a lower bound on the distance
208 between the line segment and this bounding box.
209 Parameters:
210 line - [in]
211 Returns:
212 A distance that is less than or equal to the shortest
213 distance from the line to this bounding box.
214 Put another way, if Q is any point on line
215 and P is any point in this bounding box, then
216 P.DistanceTo(Q) >= MinimumDistanceTo(bbox).
217 */
218 double MinimumDistanceTo( const ON_Line& line ) const;
219
220 /*
221 Description:
222 Quickly find a tight lower bound on the distance
223 between the plane and this bounding box.
224 Parameters:
225 plane - [in]
226 Returns:
227 The minimum distance between a point on the plane
228 and a point on the bounding box.
229 See Also:
230 ON_PlaneEquation::MimimumValueAt
231 ON_PlaneEquation::MaximumValueAt
232 */
233 double MinimumDistanceTo( const ON_Plane& plane ) const;
234 double MinimumDistanceTo( const ON_PlaneEquation& plane_equation ) const;
235
236 /*
237 Description:
238 Quickly find an upper bound on the distance
239 between the line segment and this bounding box.
240 Parameters:
241 line - [in]
242 Returns:
243 A distance that is greater than or equal to the
244 longest distance from the line to this bounding box.
245 Put another way, if Q is any point on the line
246 and P is any point in this bounding box, then
247 P.DistanceTo(Q) <= MaximumDistanceTo(bbox).
248 */
249 double MaximumDistanceTo( const ON_Line& line ) const;
250
251 /*
252 Description:
253 Quickly find a tight upper bound on the distance
254 between the plane and this bounding box.
255 Parameters:
256 plane - [in]
257 Returns:
258 A distance that is equal to the longest distance from
259 the plane to this bounding box. Put another way,
260 if Q is any point on the plane and P is any point
261 in this bounding box, then
262 P.DistanceTo(Q) <= MaximumDistanceTo(bbox) and there
263 is at least one point on the bounding box where the
264 distance is equal to the returned value.
265 See Also:
266 ON_PlaneEquation::MaximumValueAt
267 */
268 double MaximumDistanceTo( const ON_Plane& plane ) const;
269 double MaximumDistanceTo( const ON_PlaneEquation& plane_equation ) const;
270
271
272 /*
273 Description:
274 Quickly determine if the shortest distance from
275 the point P to the bounding box is greater than d.
276 Parameters:
277 d - [in] distance (> 0.0)
278 P - [in]
279 Returns:
280 True if if the shortest distance from the point P
281 to the bounding box is greater than d.
282 */
283 bool IsFartherThan( double d, const ON_3dPoint& P ) const;
284
285 /*
286 Description:
287 Quickly determine if the shortest distance from the line
288 to the bounding box is greater than d.
289 Parameters:
290 d - [in] distance (> 0.0)
291 line - [in]
292 Returns:
293 True if the shortest distance from the line
294 to the bounding box is greater than d. It is not the
295 case that false means that the shortest distance
296 is less than or equal to d.
297 */
298 bool IsFartherThan( double d, const ON_Line& line ) const;
299
300 /*
301 Description:
302 Quickly determine if the shortest distance from the plane
303 to the bounding box is greater than d.
304 Parameters:
305 d - [in] distance (> 0.0)
306 plane - [in]
307 Returns:
308 True if the shortest distance from the plane
309 to the bounding box is greater than d, and false
310 if the shortest distance is less than or equal to d.
311 */
312 bool IsFartherThan( double d, const ON_Plane& plane ) const;
313
314 /*
315 Description:
316 Quickly determine if the shortest distance from the plane
317 to the bounding box is greater than d.
318 Parameters:
319 d - [in] distance (> 0.0)
320 plane_equation - [in] (the first three coefficients
321 are assumed to be a unit vector.
322 If not, adjust your d accordingly.)
323 Returns:
324 True if the shortest distance from the plane
325 to the bounding box is greater than d, and false
326 if the shortest distance is less than or equal to d.
327 */
328 bool IsFartherThan( double d, const ON_PlaneEquation& plane_equation ) const;
329
330 /*
331 Description:
332 Quickly determine if the shortest distance this bounding
333 box to another bounding box is greater than d.
334 Parameters:
335 d - [in] distance (> 0.0)
336 other - [in] other bounding box
337 Returns:
338 True if if the shortest distance from this bounding
339 box to the other bounding box is greater than d.
340 */
341 bool IsFartherThan( double d, const ON_BoundingBox& other ) const;
342
343
344 // Description:
345 // Get point in a bounding box that is closest to a line
346 // segment.
347 // Parameters:
348 // line - [in] line segment
349 // box_point - [out] point in box that is closest to line
350 // segment point at t0.
351 // t0 - [out] parameter of point on line that is closest to
352 // the box.
353 // t1 - [out] parameter of point on line that is closest to
354 // the box.
355 // Returns:
356 // 3 success - line segments intersects box in a segment
357 // from line(t0) to line(t1) (t0 < t1)
358 // 2 success - line segments intersects box in a single point
359 // at line(t0) (t0==t1)
360 // 1 success - line segment does not intersect box. Closest
361 // point on the line is at line(t0) (t0==t1)
362 // 0 failure - box is invalid.
363 // Remarks:
364 // The box is treated as a solid box. If the intersection
365 // of the line segment, then 3 is returned.
366 int GetClosestPoint(
367 const ON_Line&, // line
368 ON_3dPoint&, // box_point
369 double*, // t0
370 double* // t1
371 ) const;
372
374 // Get points on bounding boxes that are closest to each other.
375 // If the boxes intersect, then the point at the centroid of the
376 // intersection is returned for both points.
377 bool GetClosestPoint(
378 const ON_BoundingBox&, // "other" bounding box
379 ON_3dPoint&, // point on "this" box that is closest to "other" box
380 ON_3dPoint& // point on "other" box that is closest to "this" box
381 ) const;
382
384 // Point on the box that is farthest from the test_point.
385 ON_3dPoint FarPoint(
386 const ON_3dPoint& // test_point
387 ) const;
388
390 // Get points on bounding boxes that are farthest from each other.
391 bool GetFarPoint(
392 const ON_BoundingBox&, // "other" bounding box
393 ON_3dPoint&, // point on "this" box that is farthest from "other" box
394 ON_3dPoint& // point on "other" box that is farthest from "this" box
395 ) const;
396
397 /*
398 Description:
399 Intersect this with other_bbox and save intersection in this.
400 Parameters:
401 other_bbox - [in]
402 Returns:
403 True if this-intesect-other_bbox is a non-empty valid bounding box
404 and this is set. False if the intersection is empty, in which case
405 "this" is set to an invalid bounding box.
406 Remarks:
407 If "this" or other_bbox is invalid, they are treated as
408 the empty set, and false is returned.
409 */
410 bool Intersection(
411 const ON_BoundingBox& other_bbox
412 );
413
414 /*
415 Description:
416 Set "this" to the intersection of bbox_A and bbox_B.
417 Parameters:
418 bbox_A - [in]
419 bbox_B - [in]
420 Returns:
421 True if the "this" is a non-empty valid bounding box.
422 False if the intersection is empty, in which case
423 "this" is set to an invalid bounding box.
424 Remarks:
425 If bbox_A or bbox_B is invalid, they are treated as
426 the empty set, and false is returned.
427 */
428 bool Intersection( // this = intersection of two args
429 const ON_BoundingBox& bbox_A,
430 const ON_BoundingBox& bbox_B
431 );
432
433 bool Intersection( //Returns true when intersect is non-empty.
434 const ON_Line&, //Infinite Line segment to intersect with
435 double* =NULL , // t0 parameter of first intersection point
436 double* =NULL // t1 parameter of last intersection point (t0<=t1)
437 ) const;
438
439 /*
440 Description:
441 Test a box to see if it is contained in this box.
442 Parameters:
443 other - [in] box to test
444 bProperSubSet - [in] if true, then the test is for a proper inclusion.
445 Returns:
446 If bProperSubSet is false, then the result is true when
447 this->m_min[i] <= other.m_min[i] and other.m_max[i] <= this->m_max[i].
448 for i=0,1 and 2.
449 If bProperSubSet is true, then the result is true when
450 the above condition is true and at least one of the inequalities is strict.
451 */
452 bool Includes(
453 const ON_BoundingBox& other,
454 bool bProperSubSet = false
455 ) const;
456
457 double Volume() const;
458
459 double Area() const;
460
461 // Union() returns true if union is not empty.
462 // Invalid boxes are treated as the empty set.
463 bool Union( // this = this union arg
464 const ON_BoundingBox&
465 );
466
467 bool Union( // this = union of two args
468 const ON_BoundingBox&,
469 const ON_BoundingBox&
470 );
471
472 /*
473 Description:
474 Test to see if "this" and other_bbox are disjoint (do not intersect).
475 Parameters:
476 other_bbox - [in]
477 Returns:
478 True if "this" and other_bbox are disjoint.
479 Remarks:
480 If "this" or other_bbox is invalid, then true is returned.
481 */
482 bool IsDisjoint(
483 const ON_BoundingBox& other_bbox
484 ) const;
485
486 bool SwapCoordinates( int, int );
487
490};
491
492#if defined(ON_DLL_TEMPLATE)
493
494// This stuff is here because of a limitation in the way Microsoft
495// handles templates and DLLs. See Microsoft's knowledge base
496// article ID Q168958 for details.
497#pragma warning( push )
498#pragma warning( disable : 4231 )
499ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_BoundingBox>;
500#pragma warning( pop )
501
502#endif
503
504/*
505Description:
506 Get a tight bounding box that contains the points.
507Parameters:
508 dim - [in] (>=1)
509 is_rat - [in] true if points are rational
510 count - [in] number of points
511 stride - [in] stride between points
512 point_list - [in]
513 bbox - [in/out]
514 bGrowBox - [in] (default = false)
515 If the input bbox is valid and bGrowBox is true,
516 then the output bbox is the union of the input
517 bbox and the bounding box of the point list.
518 xform - [in] (default = NULL)
519 If not null, the bounding box of the transformed
520 points is calculated. The points are not modified.
521Returns:
522 True if the output bbox is valid.
523*/
526 int dim,
527 int is_rat,
528 int count,
529 int stride,
530 const double* point_list,
531 ON_BoundingBox& bbox,
532 int bGrowBox = false,
533 const ON_Xform* xform = 0
534 );
535
538 int dim,
539 int is_rat,
540 int count,
541 int stride,
542 const float* point_list,
543 ON_BoundingBox& bbox,
544 int bGrowBox = false,
545 const ON_Xform* xform = 0
546 );
547
550 int dim,
551 int is_rat,
552 int count,
553 int stride,
554 const double* point_list,
555 double* boxmin, // min[dim]
556 double* boxmax, // max[dim]
557 int bGrowBox
558 );
559
562 int dim,
563 int is_rat,
564 int count,
565 int stride,
566 const double* point_list
567 );
568
571 int dim,
572 int is_rat,
573 int count,
574 int stride,
575 const float* point_list,
576 float* boxmin, // min[dim]
577 float* boxmax, // max[dim]
578 int bGrowBox
579 );
580
582ON_BoundingBox ON_PointListBoundingBox( // low level workhorse function
583 int dim,
584 int is_rat,
585 int count,
586 int stride,
587 const float* point_list
588 );
589
592 int dim,
593 int is_rat,
594 int point_count0, int point_count1,
595 int point_stride0, int point_stride1,
596 const double* point_grid,
597 double* boxmin, // min[dim]
598 double* boxmax, // max[dim]
599 int bGrowBox
600 );
601
604 int dim,
605 int is_rat,
606 int point_count0, int point_count1,
607 int point_stride0, int point_stride1,
608 const double* point_grid
609 );
610
613 int dim,
614 const double* bboxmin,
615 const double* bboxmax
616 );
617
620 const ON_BoundingBox& tight_bbox,
621 const ON_BoundingBox& world_bbox,
622 const ON_Xform* xform
623 );
624
625#endif
@ Transform
Definition RSMetaType.h:67
Definition opennurbs_array.h:398
Definition opennurbs_point.h:403
Definition opennurbs_point.h:931
Definition opennurbs_bounding_box.h:25
ON_3dPoint m_min
Definition opennurbs_bounding_box.h:488
ON_3dPoint m_max
Definition opennurbs_bounding_box.h:489
static const ON_BoundingBox EmptyBoundingBox
Definition opennurbs_bounding_box.h:27
Definition opennurbs_line.h:20
Definition opennurbs_point.h:1169
Definition opennurbs_plane.h:20
Definition opennurbs_array.h:46
Definition opennurbs_xform.h:28
ON_DECL double ON_BoundingBoxTolerance(int dim, const double *bboxmin, const double *bboxmax)
Definition opennurbs_bounding_box.cpp:2241
ON_DECL bool ON_WorldBBoxIsInTightBBox(const ON_BoundingBox &tight_bbox, const ON_BoundingBox &world_bbox, const ON_Xform *xform)
Definition opennurbs_bezier.cpp:679
ON_DECL ON_BoundingBox ON_PointGridBoundingBox(int dim, int is_rat, int point_count0, int point_count1, int point_stride0, int point_stride1, const double *point_grid)
Definition opennurbs_bounding_box.cpp:2186
ON_DECL bool ON_GetPointGridBoundingBox(int dim, int is_rat, int point_count0, int point_count1, int point_stride0, int point_stride1, const double *point_grid, double *boxmin, double *boxmax, int bGrowBox)
Definition opennurbs_bounding_box.cpp:2208
ON_DECL ON_BoundingBox ON_PointListBoundingBox(int dim, int is_rat, int count, int stride, const double *point_list)
Definition opennurbs_bounding_box.cpp:1609
ON_DECL bool ON_GetPointListBoundingBox(int dim, int is_rat, int count, int stride, const double *point_list, ON_BoundingBox &bbox, int bGrowBox=false, const ON_Xform *xform=0)
Definition opennurbs_bounding_box.cpp:1618
#define ON_UNSET_VALUE
Definition opennurbs_defines.h:172
#define ON_DECL
Definition opennurbs_defines.h:92
#define ON_CLASS
Definition opennurbs_defines.h:91
#define NULL
Definition opennurbs_system.h:256