24 #ifndef BASE_BOUNDBOX_H 25 #define BASE_BOUNDBOX_H 38 template <
class _Precision>
42 static bool isOnRayW(_Precision, _Precision, _Precision);
43 static bool isOnRayS(_Precision, _Precision, _Precision);
60 inline explicit BoundBox3 (_Precision fMinX = std::numeric_limits<_Precision>::max(),
61 _Precision fMinY = std::numeric_limits<_Precision>::max(),
62 _Precision fMinZ = std::numeric_limits<_Precision>::max(),
63 _Precision fMaxX = -std::numeric_limits<_Precision>::max(),
64 _Precision fMaxY = -std::numeric_limits<_Precision>::max(),
65 _Precision fMaxZ = -std::numeric_limits<_Precision>::max());
184 inline void Enlarge (_Precision fLen);
186 inline void Shrink (_Precision fLen);
189 inline _Precision
LengthX (
void)
const;
191 inline _Precision
LengthY (
void)
const;
193 inline _Precision
LengthZ (
void)
const;
195 inline void MoveX (_Precision f);
197 inline void MoveY (_Precision f);
199 inline void MoveZ (_Precision f);
201 inline void ScaleX (_Precision f);
203 inline void ScaleY (_Precision f);
205 inline void ScaleZ (_Precision f);
208 void Print (std::ostream&)
const;
212 template <
class _Precision>
213 bool BoundBox3<_Precision>::isOnRayW(_Precision A, _Precision B, _Precision K)
216 return ((A <= K) && (K <= B));
219 template <
class _Precision>
220 bool BoundBox3<_Precision>::isOnRayS(_Precision A, _Precision B, _Precision K)
223 return ((A <= K) && (K < B));
226 template <
class _Precision>
228 _Precision fMaxX, _Precision fMaxY, _Precision fMaxZ)
229 : MinX(fMinX), MinY(fMinY), MinZ(fMinZ),
230 MaxX(fMaxX), MaxY(fMaxY), MaxZ(fMaxZ)
234 template <
class _Precision>
236 : MinX(
std::numeric_limits<_Precision>::max())
237 , MinY(
std::numeric_limits<_Precision>::max())
238 , MinZ(
std::numeric_limits<_Precision>::max())
239 , MaxX(-
std::numeric_limits<_Precision>::max())
240 , MaxY(-
std::numeric_limits<_Precision>::max())
241 , MaxZ(-
std::numeric_limits<_Precision>::max())
244 for (pI = pclVect; pI < pEnd; ++pI) {
245 MinX = std::min<_Precision>(
MinX, pI->
x);
246 MinY = std::min<_Precision>(
MinY, pI->
y);
247 MinZ = std::min<_Precision>(
MinZ, pI->
z);
248 MaxX = std::max<_Precision>(
MaxX, pI->
x);
249 MaxY = std::max<_Precision>(
MaxY, pI->
y);
250 MaxZ = std::max<_Precision>(
MaxZ, pI->
z);
254 template <
class _Precision>
257 MinX = rcVector.
x - fDistance;
258 MaxX = rcVector.
x + fDistance;
259 MinY = rcVector.
y - fDistance;
260 MaxY = rcVector.
y + fDistance;
261 MinZ = rcVector.
z - fDistance;
262 MaxZ = rcVector.
z + fDistance;
265 template <
class _Precision>
270 template <
class _Precision>
282 template <
class _Precision>
285 if (rcBB.
MaxX < this->MinX || rcBB.
MinX > this->MaxX)
287 if (rcBB.
MaxY < this->MinY || rcBB.
MinY > this->MaxY)
289 if (rcBB.
MaxZ < this->MinZ || rcBB.
MinZ > this->MaxZ)
294 template <
class _Precision>
297 return Intersect(rcBB);
300 template <
class _Precision>
303 if (rcBB.
MaxX < this->MinX || rcBB.
MinX > this->MaxX)
305 if (rcBB.
MaxY < this->MinY || rcBB.
MinY > this->MaxY)
310 template <
class _Precision>
313 return Intersect(rcBB);
316 template <
class _Precision>
321 cBBRes.
MinX = std::max<_Precision> (MinX, rcBB.
MinX);
322 cBBRes.
MaxX = std::min<_Precision> (MaxX, rcBB.
MaxX);
323 cBBRes.
MinY = std::max<_Precision> (MinY, rcBB.
MinY);
324 cBBRes.
MaxY = std::min<_Precision> (MaxY, rcBB.
MaxY);
325 cBBRes.
MinZ = std::max<_Precision> (MinZ, rcBB.
MinZ);
326 cBBRes.
MaxZ = std::min<_Precision> (MaxZ, rcBB.
MaxZ);
331 template <
class _Precision>
336 cBBRes.
MinX = std::min<_Precision> (MinX, rcBB.
MinX);
337 cBBRes.
MaxX = std::max<_Precision> (MaxX, rcBB.
MaxX);
338 cBBRes.
MinY = std::min<_Precision> (MinY, rcBB.
MinY);
339 cBBRes.
MaxY = std::max<_Precision> (MaxY, rcBB.
MaxY);
340 cBBRes.
MinZ = std::min<_Precision> (MinZ, rcBB.
MinZ);
341 cBBRes.
MaxZ = std::max<_Precision> (MaxZ, rcBB.
MaxZ);
346 template <
class _Precision>
349 this->MinX = std::min<_Precision>(this->MinX, rclVect.
x);
350 this->MinY = std::min<_Precision>(this->MinY, rclVect.
y);
351 this->MinZ = std::min<_Precision>(this->MinZ, rclVect.
z);
352 this->MaxX = std::max<_Precision>(this->MaxX, rclVect.
x);
353 this->MaxY = std::max<_Precision>(this->MaxY, rclVect.
y);
354 this->MaxZ = std::max<_Precision>(this->MaxZ, rclVect.
z);
357 template <
class _Precision>
360 this->MinX = std::min<_Precision> (this->MinX, rcBB.
MinX);
361 this->MaxX = std::max<_Precision> (this->MaxX, rcBB.
MaxX);
362 this->MinY = std::min<_Precision> (this->MinY, rcBB.
MinY);
363 this->MaxY = std::max<_Precision> (this->MaxY, rcBB.
MaxY);
364 this->MinZ = std::min<_Precision> (this->MinZ, rcBB.
MinZ);
365 this->MaxZ = std::max<_Precision> (this->MaxZ, rcBB.
MaxZ);
368 template <
class _Precision>
371 if (rcVct.
x < this->MinX || rcVct.
x > this->MaxX)
373 if (rcVct.
y < this->MinY || rcVct.
y > this->MaxY)
375 if (rcVct.
z < this->MinZ || rcVct.
z > this->MaxZ)
380 template <
class _Precision>
383 if (rcBB.
MinX < this->MinX || rcBB.
MaxX > this->MaxX)
385 if (rcBB.
MinY < this->MinY || rcBB.
MaxY > this->MaxY)
387 if (rcBB.
MinZ < this->MinZ || rcBB.
MaxZ > this->MaxZ)
392 template <
class _Precision>
395 if (rcBB.
MinX < this->MinX || rcBB.
MaxX > this->MaxX)
397 if (rcBB.
MinY < this->MinY || rcBB.
MaxY > this->MaxY)
402 template <
class _Precision>
405 return ((MinX <= MaxX) && (MinY <= MaxY) && (MinZ <= MaxZ));
408 template <
class _Precision>
411 if (!IsInBox (rclVct))
414 unsigned short usNdx = 0;
415 if (isOnRayS ((MinX + MaxX)/2, MaxX, rclVct.
x))
417 if (isOnRayS ((MinY + MaxY)/2, MaxY, rclVct.
y))
419 if (isOnRayS ((MinZ + MaxZ)/2, MaxZ, rclVct.
z))
421 rclOctant = static_cast<OCTANT>(usNdx);
425 template <
class _Precision>
482 template <
class _Precision>
499 template <
class _Precision>
504 rBase.
Set(MinX, MinY, MaxZ);
505 rNormal.
Set(1.0f, 0.0f, 0.0f);
509 rBase.
Set(MaxX, MinY, MaxZ);
510 rNormal.
Set(1.0f, 0.0f, 0.0f);
514 rBase.
Set(MinX, MaxY, MaxZ);
515 rNormal.
Set(0.0f, 1.0f, 0.0f);
519 rBase.
Set(MinX, MinY, MaxZ);
520 rNormal.
Set(0.0f, 1.0f, 0.0f);
524 rBase.
Set(MinX, MinY, MaxZ);
525 rNormal.
Set(0.0f, 0.0f, 1.0f);
529 rBase.
Set(MinX, MinY, MinZ);
530 rNormal.
Set(0.0f, 0.0f, 1.0f);
537 template <
class _Precision>
596 template <
class _Precision>
607 if (cCmpBound.
IsInBox (rcVct)) {
609 for (i = 0; (i < 6) && (!rc); i++) {
610 rc = IntersectPlaneWithLine(i, rcVct, rcVctDir, cVctRes);
611 if (!cCmpBound.
IsInBox(cVctRes))
617 rc = ((cVctRes - rcVct) * rcVctDir) >= 0.0;
625 template <
class _Precision>
636 fDist = (rcDir % (GetCenter() - rcBase)).Length() / rcDir.
Length();
638 if (fDist > (CalcDiagonalLength() + fTolerance)) {
646 for (i = 0; i < 6; i++) {
647 if (IntersectPlaneWithLine(i, rcBase, rcDir, clVectRes)) {
652 if ((isOnRayW (MinY - fTolerance, MaxY + fTolerance, clVectRes.
y) &&
653 isOnRayW (MinZ - fTolerance, MaxZ + fTolerance, clVectRes.
z)))
658 if ((isOnRayW (MinX - fTolerance, MaxX + fTolerance, clVectRes.
x) &&
659 isOnRayW (MinZ - fTolerance, MaxZ + fTolerance, clVectRes.
z)))
664 if ((isOnRayW (MinX - fTolerance, MaxX + fTolerance, clVectRes.
x) &&
665 isOnRayW (MinY - fTolerance, MaxY + fTolerance, clVectRes.
y)))
676 template <
class _Precision>
679 if (fabs(GetCenter().DistanceToPlane(rclBase, rclNormal)) < CalcDiagonalLength()) {
680 _Precision fD = CalcPoint(0).DistanceToPlane(rclBase, rclNormal);
681 for (
unsigned short i = 1; i < 8; i++) {
682 if ((CalcPoint(i).DistanceToPlane(rclBase, rclNormal) * fD) <= 0.0f)
689 template <
class _Precision>
695 unsigned short numIntersect = 0;
697 for (
unsigned short i = 0; i < 6; i++) {
698 if (IntersectPlaneWithLine(i, rcBase, rcDir, clVectRes)) {
703 if ((isOnRayS(MinY, MaxY, clVectRes.
y) &&
704 isOnRayS(MinZ, MaxZ, clVectRes.
z))) {
705 clVect[numIntersect] = clVectRes;
710 if ((isOnRayS(MinX, MaxX, clVectRes.
x) &&
711 isOnRayS(MinZ, MaxZ, clVectRes.
z))) {
712 clVect[numIntersect] = clVectRes;
717 if ((isOnRayS(MinX, MaxX, clVectRes.
x) &&
718 isOnRayS(MinY, MaxY, clVectRes.
y))) {
719 clVect[numIntersect] = clVectRes;
726 if (numIntersect == 2) {
731 else if (numIntersect > 2) {
732 for (
unsigned short i = 1; i < numIntersect; i++) {
733 if (clVect[i] != clVect[0]) {
744 template <
class _Precision>
751 CalcPlane(usSide, cBase, cNormal);
753 if ((cNormal * cDir) == 0.0f) {
757 k = (cNormal * (cBase - rcBase)) / (cNormal * cDir);
759 rcP0 = rcBase + cDir;
764 template <
class _Precision>
768 return GetSideFromRay( rclPt, rclDir, cIntersection);
771 template <
class _Precision>
776 if (IntersectWithLine(rclPt, rclDir, cP0, cP1) ==
false)
781 if ((cP1-cP0)*rclDir > 0)
788 _Precision fMax = 1.0e-3f;
789 SIDE tSide = INVALID;
791 if (fabs(cOut.
x - MinX) < fMax) {
792 fMax = _Precision(fabs(cOut.
x - MinX));
796 if (fabs(cOut.
x - MaxX) < fMax) {
797 fMax = _Precision(fabs(cOut.
x - MaxX));
801 if (fabs(cOut.
y - MinY) < fMax) {
802 fMax = _Precision(fabs(cOut.
y - MinY));
806 if (fabs(cOut.
y - MaxY) < fMax) {
807 fMax = _Precision(fabs(cOut.
y - MaxY));
811 if (fabs(cOut.
z - MinZ) < fMax) {
812 fMax = _Precision(fabs(cOut.
z - MinZ));
816 if (fabs(cOut.
z - MaxZ) < fMax) {
817 fMax = _Precision(fabs(cOut.
z - MaxZ));
824 template <
class _Precision>
829 _Precision fMinDist = std::numeric_limits<_Precision>::max();
832 for (
int i = 0; i < 6; i++) {
834 CalcPlane(i, cBase, cNormal);
836 _Precision fDist = (clTemp - rclPt).Length();
837 if (fDist < fMinDist) {
848 _Precision devx = closest.
x - center.
x;
849 _Precision devy = closest.
y - center.
y;
850 _Precision devz = closest.
z - center.
z;
852 _Precision halfwidth = (MaxX - MinX) / 2;
853 _Precision halfheight = (MaxY - MinY) / 2;
854 _Precision halfdepth = (MaxZ - MinZ) / 2;
857 if ((fabs(devx) > fabs(devy)) && (fabs(devx) > fabs(devz)))
858 closest.
x = center.
x + halfwidth * ((devx < 0.0) ? -1.0 : 1.0);
859 else if (fabs(devy) > fabs(devz))
860 closest.
y = center.
y + halfheight * ((devy < 0.0) ? -1.0 : 1.0);
862 closest.
z = center.
z + halfdepth * ((devz < 0.0) ? -1.0 : 1.0);
865 closest.
x = std::min<_Precision>(std::max<_Precision>(closest.
x, MinX), MaxX);
866 closest.
y = std::min<_Precision>(std::max<_Precision>(closest.
y, MinY), MaxY);
867 closest.
z = std::min<_Precision>(std::max<_Precision>(closest.
z, MinZ), MaxZ);
873 template <
class _Precision>
879 for (
int i = 0; i < 8; i++) {
887 template <
class _Precision>
891 for (
int i=0; i<8; i++)
892 bbox.
Add(mat * CalcPoint(i));
896 template <
class _Precision>
904 template <
class _Precision>
907 return static_cast<_Precision>(sqrt (((MaxX - MinX) * (MaxX - MinX)) +
908 ((MaxY - MinY) * (MaxY - MinY)) +
909 ((MaxZ - MinZ) * (MaxZ - MinZ))));
912 template <
class _Precision>
915 MinX = MinY = MinZ = std::numeric_limits<_Precision>::max();
916 MaxX = MaxY = MaxZ = -std::numeric_limits<_Precision>::max();
919 template <
class _Precision>
922 MinX -= fLen; MinY -= fLen; MinZ -= fLen;
923 MaxX += fLen; MaxY += fLen; MaxZ += fLen;
926 template <
class _Precision>
929 MinX += fLen; MinY += fLen; MinZ += fLen;
930 MaxX -= fLen; MaxY -= fLen; MaxZ -= fLen;
933 template <
class _Precision>
939 template <
class _Precision>
945 template <
class _Precision>
951 template <
class _Precision>
954 MinX += f; MaxX += f;
957 template <
class _Precision>
960 MinY += f; MaxY += f;
963 template <
class _Precision>
966 MinZ += f; MaxZ += f;
969 template <
class _Precision>
972 MinX *= f; MaxX *= f;
975 template <
class _Precision>
978 MinY *= f; MaxY *= f;
981 template <
class _Precision>
984 MinZ *= f; MaxZ *= f;
992 #endif // BASE_BOUNDBOX_H _Precision z
Definition: Vector3D.h:89
Definition: ViewProj.h:36
Definition: BoundBox.h:120
_Precision CalcDiagonalLength(void) const
Definition: BoundBox.h:905
bool IsCutPlane(const Vector3< _Precision > &rclBase, const Vector3< _Precision > &rclNormal) const
Definition: BoundBox.h:677
Definition: BoundBox.h:119
Definition: BoundBox.h:119
BoundBox3< float > BoundBox3f
Definition: BoundBox.h:987
Definition: BoundBox.h:124
_Precision y
Definition: Vector3D.h:88
bool IntersectionPoint(const Vector3< _Precision > &rcVct, const Vector3< _Precision > &rcVctDir, Vector3< _Precision > &cVctRes, _Precision epsilon) const
Definition: BoundBox.h:597
bool operator &&(const BoundBox3< _Precision > &rcBB) const
Definition: BoundBox.h:295
Definition: BoundBox.h:120
Definition: BoundBox.h:119
_Precision MinZ
Definition: BoundBox.h:53
Definition: BoundBox.h:124
Definition: GlobalId.h:61
_Precision LengthY(void) const
Definition: BoundBox.h:940
bool GetOctantFromVector(const Vector3< _Precision > &rclVct, OCTANT &rclOctant) const
Definition: BoundBox.h:409
BoundBox3< _Precision > Intersected(const BoundBox3< _Precision > &rcBB) const
Definition: BoundBox.h:317
bool IsValid(void) const
Definition: BoundBox.h:403
void SetVoid(void)
Definition: Tools2D.h:536
BoundBox3(_Precision fMinX=std::numeric_limits< _Precision >::max(), _Precision fMinY=std::numeric_limits< _Precision >::max(), _Precision fMinZ=std::numeric_limits< _Precision >::max(), _Precision fMaxX=-std::numeric_limits< _Precision >::max(), _Precision fMaxY=-std::numeric_limits< _Precision >::max(), _Precision fMaxZ=-std::numeric_limits< _Precision >::max())
Definition: BoundBox.h:227
_Precision MaxY
Definition: BoundBox.h:55
_Precision x
Definition: Vector3D.h:87
Definition: BoundBox.h:124
_Precision MaxZ
Definition: BoundBox.h:56
bool IsInBox(const Vector3< _Precision > &rcVct) const
Definition: BoundBox.h:369
_Precision LengthX(void) const
Definition: BoundBox.h:934
void ScaleZ(_Precision f)
Definition: BoundBox.h:982
Vector3 & ProjectToPlane(const Vector3 &rclBase, const Vector3 &rclNorm)
Projects this point onto the plane given by the base rclBase and the normal rclNorm.
void ScaleY(_Precision f)
Definition: BoundBox.h:976
Definition: BoundBox.h:120
double MaxY
Definition: Tools2D.h:100
bool CalcEdge(unsigned short usEdge, Vector3< _Precision > &rcP0, Vector3< _Precision > &rcP1) const
Definition: BoundBox.h:538
_Precision MaxX
Definition: BoundBox.h:54
bool IntersectPlaneWithLine(unsigned short usSide, const Vector3< _Precision > &rcBase, const Vector3< _Precision > &rcDir, Vector3< _Precision > &rcP0) const
Definition: BoundBox.h:745
SIDE
Definition: BoundBox.h:124
BoundBox3< _Precision >::SIDE GetSideFromRay(const Vector3< _Precision > &rclPt, const Vector3< _Precision > &rclDir) const
Definition: BoundBox.h:765
bool IsCutLine(const Vector3< _Precision > &rcBase, const Vector3< _Precision > &rcDir, _Precision fTolerance=0.0f) const
Definition: BoundBox.h:626
BoundBox3(const BoundBox3< _Precision > &rcBB)
Definition: BoundBox.h:66
BoundBox3< _Precision > & operator=(const BoundBox3< _Precision > &rcBound)
Assignment operator.
Definition: BoundBox.h:271
double MinX
Definition: Tools2D.h:100
OCTANT
Definition: BoundBox.h:119
void ScaleX(_Precision f)
Definition: BoundBox.h:970
BoundBox3< _Precision > CalcOctant(typename BoundBox3< _Precision >::OCTANT Octant) const
Definition: BoundBox.h:426
Vector3< _Precision > GetCenter(void) const
Definition: BoundBox.h:897
Vector3< _Precision > ClosestPoint(const Vector3< _Precision > &rclPt) const
Definition: BoundBox.h:825
void MoveZ(_Precision f)
Definition: BoundBox.h:964
BoundBox3< _Precision > Transformed(const Matrix4D &mat) const
Definition: BoundBox.h:888
Definition: BoundBox.h:124
Vector3< _Precision > CalcPoint(unsigned short usPoint) const
Definition: BoundBox.h:483
void MoveX(_Precision f)
Definition: BoundBox.h:952
void Add(const Vector3< _Precision > &rclVect)
Definition: BoundBox.h:347
BoundBox3< _Precision > United(const BoundBox3< _Precision > &rcBB) const
Definition: BoundBox.h:332
BoundBox2d ProjectBox(const ViewProjMethod *rclP) const
Definition: BoundBox.h:874
Definition: BoundBox.h:124
BoundBox3< double > BoundBox3d
Definition: BoundBox.h:988
Definition: BoundBox.h:124
~BoundBox3()
Definition: BoundBox.h:266
Definition: BoundBox.h:119
_Precision MinY
Definition: BoundBox.h:52
_Precision MinX
Definition: BoundBox.h:51
void Enlarge(_Precision fLen)
Definition: BoundBox.h:920
void Print(std::ostream &) const
Definition: BoundBox.h:120
Definition: BoundBox.h:124
Definition: AbstractXMLReader.h:5
Definition: Vector3D.h:78
void Set(_Precision fX, _Precision fY, _Precision fZ)
double MaxX
Definition: Tools2D.h:100
_Precision LengthZ(void) const
Definition: BoundBox.h:946
void Shrink(_Precision fLen)
Definition: BoundBox.h:927
void CalcPlane(unsigned short usPlane, Vector3< _Precision > &rBase, Vector3< _Precision > &rNormal) const
Definition: BoundBox.h:500
void SetVoid(void)
Definition: BoundBox.h:913
_Precision num_type
Definition: BoundBox.h:46
bool IntersectWithLine(const Vector3< _Precision > &rcBase, const Vector3< _Precision > &rcDir, Vector3< _Precision > &rcP0, Vector3< _Precision > &rcP1) const
Definition: BoundBox.h:690
_Precision Length(void) const
Length of the vector.
float_traits< num_type > traits_type
Definition: BoundBox.h:47
bool Intersect(const BoundBox3< _Precision > &rcBB) const
Definition: BoundBox.h:283
Definition: Vector3D.h:58
Definition: BoundBox.h:39
double MinY
Definition: Tools2D.h:100
void MoveY(_Precision f)
Definition: BoundBox.h:958
void Add(const Vector2d &v)
Definition: Tools2D.h:542