00001
00002
00003
00004
00005
00006 #ifndef _GMTL_CONTAINMENT_H_
00007 #define _GMTL_CONTAINMENT_H_
00008
00009
00010 #include <vector>
00011 #include <gmtl/Sphere.h>
00012 #include <gmtl/AABox.h>
00013 #include <gmtl/Frustum.h>
00014 #include <gmtl/Tri.h>
00015 #include <gmtl/VecOps.h>
00016
00017
00018
00019
00020
00021
00022
00023
00024 namespace gmtl
00025 {
00026
00027
00028
00029
00030
00040 template< class DATA_TYPE >
00041 bool isInVolume( const Sphere<DATA_TYPE>& container,
00042 const Point<DATA_TYPE, 3>& pt )
00043 {
00044
00045
00046
00047
00048 return ( length(gmtl::Vec<DATA_TYPE,3>(pt - container.mCenter)) <= container.mRadius );
00049 }
00050
00060 template< class DATA_TYPE >
00061 bool isInVolume( const Sphere<DATA_TYPE>& container,
00062 const Sphere<DATA_TYPE>& sphere )
00063 {
00064
00065
00066
00067
00068 return ( length(gmtl::Vec<DATA_TYPE,3>(sphere.mCenter - container.mCenter)) + sphere.mRadius
00069 <= container.mRadius );
00070 }
00071
00078 template< class DATA_TYPE >
00079 void extendVolume( Sphere<DATA_TYPE>& container,
00080 const Point<DATA_TYPE, 3>& pt )
00081 {
00082
00083 if ( isInVolume( container, pt ) )
00084 {
00085 return;
00086 }
00087
00088
00089
00090 Vec<DATA_TYPE, 3> dir = pt - container.mCenter;
00091 DATA_TYPE len = normalize( dir );
00092
00093
00094 DATA_TYPE newRadius = (len + container.mRadius) * static_cast<DATA_TYPE>(0.5);
00095
00096
00097 Point<DATA_TYPE, 3> newCenter = container.mCenter +
00098 (dir * (newRadius - container.mRadius));
00099
00100
00101 container.mCenter = newCenter;
00102 container.mRadius = newRadius;
00103 }
00104
00111 template< class DATA_TYPE >
00112 void extendVolume( Sphere<DATA_TYPE>& container,
00113 const Sphere<DATA_TYPE>& sphere )
00114 {
00115
00116 if ( isInVolume( container, sphere ) )
00117 {
00118 return;
00119 }
00120
00121
00122
00123 Vec<DATA_TYPE, 3> dir = sphere.mCenter - container.mCenter;
00124 DATA_TYPE len = normalize( dir );
00125
00126
00127 DATA_TYPE newRadius = (len + sphere.mRadius + container.mRadius) *
00128 static_cast<DATA_TYPE>(0.5);
00129
00130
00131 Point<DATA_TYPE, 3> newCenter = container.mCenter +
00132 (dir * (newRadius - container.mRadius));
00133
00134
00135 container.mCenter = newCenter;
00136 container.mRadius = newRadius;
00137 }
00138
00149 template< class DATA_TYPE >
00150 void makeVolume( Sphere<DATA_TYPE>& container,
00151 const std::vector< Point<DATA_TYPE, 3> >& pts )
00152 {
00153 gmtlASSERT( pts.size() > 0 && "pts must contain at least 1 point" );
00154
00155
00156
00157 typename std::vector< Point<DATA_TYPE, 3> >::const_iterator itr = pts.begin();
00158
00159
00160 Point<DATA_TYPE, 3> sum = *itr;
00161 ++itr;
00162 while ( itr != pts.end() )
00163 {
00164 sum += *itr;
00165 ++itr;
00166 }
00167 container.mCenter = sum / static_cast<DATA_TYPE>(pts.size());
00168
00169
00170
00171 DATA_TYPE radiusSqr(0);
00172 for ( itr = pts.begin(); itr != pts.end(); ++itr )
00173 {
00174 DATA_TYPE len = lengthSquared( gmtl::Vec<DATA_TYPE,3>( (*itr) - container.mCenter) );
00175 if ( len > radiusSqr )
00176 radiusSqr = len;
00177 }
00178
00179 container.mRadius = Math::sqrt( radiusSqr );
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00261 template< class DATA_TYPE >
00262 bool isOnVolume( const Sphere<DATA_TYPE>& container,
00263 const Point<DATA_TYPE, 3>& pt )
00264 {
00265
00266 return ( length(gmtl::Vec<DATA_TYPE,3>(container.mCenter - pt)) - container.mRadius == 0 );
00267 }
00268
00279 template< class DATA_TYPE >
00280 bool isOnVolume( const Sphere<DATA_TYPE>& container,
00281 const Point<DATA_TYPE, 3>& pt,
00282 const DATA_TYPE& tol )
00283 {
00284 gmtlASSERT( tol >= 0 && "tolerance must be positive" );
00285
00286
00287 return ( Math::abs( length( gmtl::Vec<DATA_TYPE,3>(container.mCenter - pt)) - container.mRadius )
00288 <= tol );
00289 }
00290
00291
00292
00293
00294
00304 template< class DATA_TYPE>
00305 bool isInVolume(const AABox<DATA_TYPE>& container,
00306 const Point<DATA_TYPE, 3>& pt)
00307 {
00308 if (! container.isEmpty())
00309 {
00310 return ( pt[0] >= container.mMin[0] &&
00311 pt[1] >= container.mMin[1] &&
00312 pt[2] >= container.mMin[2] &&
00313 pt[0] <= container.mMax[0] &&
00314 pt[1] <= container.mMax[1] &&
00315 pt[2] <= container.mMax[2]);
00316 }
00317 else
00318 {
00319 return false;
00320 }
00321 }
00322
00333 template< class DATA_TYPE>
00334 bool isInVolumeExclusive(const AABox<DATA_TYPE>& container,
00335 const Point<DATA_TYPE, 3>& pt)
00336 {
00337 if (! container.isEmpty())
00338 {
00339 return ( pt[0] > container.mMin[0] &&
00340 pt[1] > container.mMin[1] &&
00341 pt[2] > container.mMin[2] &&
00342 pt[0] < container.mMax[0] &&
00343 pt[1] < container.mMax[1] &&
00344 pt[2] < container.mMax[2]);
00345 }
00346 else
00347 {
00348 return false;
00349 }
00350 }
00351
00352
00353
00354
00364 template< class DATA_TYPE >
00365 bool isInVolume(const AABox<DATA_TYPE>& container,
00366 const AABox<DATA_TYPE>& box)
00367 {
00368
00369 if (container.isEmpty() || box.isEmpty())
00370 {
00371 return false;
00372 }
00373
00374 if (container.mMin[0] <= box.mMin[0] && container.mMax[0] >= box.mMax[0] &&
00375 container.mMin[1] <= box.mMin[1] && container.mMax[1] >= box.mMax[1] &&
00376 container.mMin[2] <= box.mMin[2] && container.mMax[2] >= box.mMax[2])
00377 {
00378 return true;
00379 }
00380 else
00381 {
00382 return false;
00383 }
00384 }
00385
00392 template< class DATA_TYPE >
00393 void extendVolume(AABox<DATA_TYPE>& container,
00394 const Point<DATA_TYPE, 3>& pt)
00395 {
00396 if (! container.isEmpty())
00397 {
00398
00399 if (pt[0] > container.mMax[0])
00400 {
00401 container.mMax[0] = pt[0];
00402 }
00403 else if (pt[0] < container.mMin[0])
00404 {
00405 container.mMin[0] = pt[0];
00406 }
00407
00408
00409 if (pt[1] > container.mMax[1])
00410 {
00411 container.mMax[1] = pt[1];
00412 }
00413 else if (pt[1] < container.mMin[1])
00414 {
00415 container.mMin[1] = pt[1];
00416 }
00417
00418
00419 if (pt[2] > container.mMax[2])
00420 {
00421 container.mMax[2] = pt[2];
00422 }
00423 else if (pt[2] < container.mMin[2])
00424 {
00425 container.mMin[2] = pt[2];
00426 }
00427 }
00428 else
00429 {
00430
00431 container.setMin(pt);
00432 container.setMax(pt);
00433 container.setEmpty(false);
00434 }
00435 }
00436
00443 template< class DATA_TYPE >
00444 void extendVolume(AABox<DATA_TYPE>& container,
00445 const AABox<DATA_TYPE>& box)
00446 {
00447
00448 if (box.isEmpty())
00449 {
00450 return;
00451 }
00452
00453
00454 if (container.isEmpty())
00455 {
00456 container = box;
00457 }
00458
00459
00460 extendVolume(container, box.getMin());
00461 extendVolume(container, box.getMax());
00462 }
00463
00469 template< class DATA_TYPE >
00470 void makeVolume(AABox<DATA_TYPE>& box, const Sphere<DATA_TYPE>& sph)
00471 {
00472 const gmtl::Point<DATA_TYPE, 3>& center = sph.getCenter();
00473 const DATA_TYPE& radius = sph.getRadius();
00474
00475
00476 gmtl::Point<DATA_TYPE, 3> min_pt(center[0] - radius,
00477 center[1] - radius,
00478 center[2] - radius);
00479 gmtl::Point<DATA_TYPE, 3> max_pt(center[0] + radius,
00480 center[1] + radius,
00481 center[2] + radius);
00482
00483 box.setMin(min_pt);
00484 box.setMax(max_pt);
00485 box.setEmpty(radius == DATA_TYPE(0));
00486 }
00487
00488
00489
00490
00491
00492 const unsigned int IN_FRONT_OF_ALL_PLANES = 6;
00493
00494 template<typename T>
00495 inline bool isInVolume(const Frustum<T>& f, const Point<T, 3>& p,
00496 unsigned int& idx )
00497 {
00498 for ( unsigned int i = 0; i < 6; ++i )
00499 {
00500 T dist = dot(f.mPlanes[i].mNorm, static_cast< Vec<T, 3> >(p)) + f.mPlanes[i].mOffset;
00501 if (dist < T(0.0) )
00502 {
00503 idx = i;
00504 return false;
00505 }
00506 }
00507
00508 idx = IN_FRONT_OF_ALL_PLANES;
00509 return true;
00510 }
00511
00512 template<typename T>
00513 inline bool isInVolume(const Frustum<T>& f, const Sphere<T>& s)
00514 {
00515 for ( unsigned int i = 0; i < 6; ++i )
00516 {
00517 T dist = dot(f.mPlanes[i].mNorm, static_cast< Vec<T, 3> >(s.getCenter())) + f.mPlanes[i].mOffset;
00518 if ( dist <= -T(s.getRadius()) )
00519 {
00520 return false;
00521 }
00522 }
00523
00524 return true;
00525 }
00526
00527 template<typename T>
00528 inline bool isInVolume(const Frustum<T>& f, const AABox<T>& box)
00529 {
00530 const Point<T, 3>& min = box.getMin();
00531 const Point<T, 3>& max = box.getMax();
00532 Point<T, 3> p[8];
00533 p[0] = min;
00534 p[1] = max;
00535 p[2] = Point<T, 3>(max[0], min[1], min[2]);
00536 p[3] = Point<T, 3>(min[0], max[1], min[2]);
00537 p[4] = Point<T, 3>(min[0], min[1], max[2]);
00538 p[5] = Point<T, 3>(max[0], max[1], min[2]);
00539 p[6] = Point<T, 3>(min[0], max[1], max[2]);
00540 p[7] = Point<T, 3>(max[0], min[1], max[2]);
00541
00542 unsigned int idx = 6;
00543
00544 if ( isInVolume(f, p[0], idx) )
00545 {
00546 return true;
00547 }
00548
00549
00550
00551
00552 for ( unsigned int i = 1; i < 8; ++i )
00553 {
00554 T dist = dot(f.mPlanes[idx].mNorm, static_cast< Vec<T, 3> >(p[i])) + f.mPlanes[idx].mOffset;
00555 if ( dist > T(0.0) )
00556 {
00557 return true;
00558 }
00559 }
00560
00561 return false;
00562 }
00563
00564 template<typename T>
00565 inline bool isInVolume(const Frustum<T>& f, const Tri<T>& tri)
00566 {
00567 unsigned int junk;
00568
00569 if ( isInVolume(f, tri[0], junk) )
00570 {
00571 return true;
00572 }
00573
00574 if ( isInVolume(f, tri[1], junk) )
00575 {
00576 return true;
00577 }
00578
00579 if ( isInVolume(f, tri[2], junk) )
00580 {
00581 return true;
00582 }
00583
00584 return false;
00585 }
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777 }
00778
00779 #endif