| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2005 Imetric 3D GmbH * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef BASE_BOUNDBOX_H |
| 27 |
#define BASE_BOUNDBOX_H |
| 28 |
|
| 29 |
#include <array> |
| 30 |
#include <limits> |
| 31 |
#include "Matrix.h" |
| 32 |
#include "Tools2D.h" |
| 33 |
#include "ViewProj.h" |
| 34 |
|
| 35 |
|
| 36 |
namespace Base |
| 37 |
{ |
| 38 |
|
| 39 |
class ViewProjMethod; |
| 40 |
|
| 41 |
/** The 3D bounding box class. */ |
| 42 |
template<class Precision> |
| 43 |
class BoundBox3 |
| 44 |
{ |
| 45 |
// helper function |
| 46 |
static bool isOnRayW(Precision min, Precision max, Precision val); |
| 47 |
static bool isOnRayS(Precision min, Precision max, Precision val); |
| 48 |
|
| 49 |
public: |
| 50 |
using num_type = Precision; |
| 51 |
using traits_type = float_traits<num_type>; |
| 52 |
|
| 53 |
/** Public attributes */ |
| 54 |
//@{ |
| 55 |
Precision MinX; |
| 56 |
Precision MinY; |
| 57 |
Precision MinZ; |
| 58 |
Precision MaxX; |
| 59 |
Precision MaxY; |
| 60 |
Precision MaxZ; |
| 61 |
//@} |
| 62 |
|
| 63 |
/** Builds box from pairs of x,y,z values. */ |
| 64 |
inline explicit BoundBox3( |
| 65 |
Precision fMinX = std::numeric_limits<Precision>::max(), |
| 66 |
Precision fMinY = std::numeric_limits<Precision>::max(), |
| 67 |
Precision fMinZ = std::numeric_limits<Precision>::max(), |
| 68 |
Precision fMaxX = -std::numeric_limits<Precision>::max(), |
| 69 |
Precision fMaxY = -std::numeric_limits<Precision>::max(), |
| 70 |
Precision fMaxZ = -std::numeric_limits<Precision>::max() |
| 71 |
); |
| 72 |
BoundBox3(const BoundBox3<Precision>& rcBB) = default; |
| 73 |
BoundBox3(BoundBox3<Precision>&& rcBB) noexcept = default; |
| 74 |
/** Builds box from an array of points. */ |
| 75 |
inline BoundBox3(const Vector3<Precision>* pclVect, std::size_t ulCt); |
| 76 |
|
| 77 |
/** Defines a bounding box around the center \a rcCnt with the |
| 78 |
* distances \a fDistance in each coordinate. |
| 79 |
*/ |
| 80 |
BoundBox3(const Vector3<Precision>& point, Precision distance); |
| 81 |
~BoundBox3(); |
| 82 |
|
| 83 |
/// Assignment operator |
| 84 |
inline BoundBox3<Precision>& operator=(const BoundBox3<Precision>& rcBound) = default; |
| 85 |
inline BoundBox3<Precision>& operator=(BoundBox3<Precision>&& rcBound) noexcept = default; |
| 86 |
|
| 87 |
/** Methods for intersection, cuttíng and union of bounding boxes */ |
| 88 |
//@{ |
| 89 |
/** Checks for intersection. */ |
| 90 |
inline bool Intersect(const BoundBox3<Precision>& rcBB) const; |
| 91 |
/** Checks for intersection. */ |
| 92 |
inline bool operator&&(const BoundBox3<Precision>& rcBB) const; |
| 93 |
/** Checks for intersection. */ |
| 94 |
inline bool Intersect(const BoundBox2d& rcBB) const; |
| 95 |
/** Checks for intersection. */ |
| 96 |
inline bool operator&&(const BoundBox2d& rcBB) const; |
| 97 |
/** Computes the intersection between two bounding boxes. |
| 98 |
* The result is also a bounding box. |
| 99 |
*/ |
| 100 |
BoundBox3<Precision> Intersected(const BoundBox3<Precision>& rcBB) const; |
| 101 |
/** The union of two bounding boxes. */ |
| 102 |
BoundBox3<Precision> United(const BoundBox3<Precision>& rcBB) const; |
| 103 |
/** Appends the point to the box. The box can grow but not shrink. */ |
| 104 |
inline void Add(const Vector3<Precision>& rclVect); |
| 105 |
/** Appends the bounding box to this box. The box can grow but not shrink. */ |
| 106 |
inline void Add(const BoundBox3<Precision>& rcBB); |
| 107 |
//@} |
| 108 |
|
| 109 |
/** Test methods */ |
| 110 |
//@{ |
| 111 |
/** Checks if this point lies inside the box. |
| 112 |
* @note It's up to the client programmer to make sure that this bounding box is valid. |
| 113 |
*/ |
| 114 |
inline bool IsInBox(const Vector3<Precision>& rcVct) const; |
| 115 |
/** Checks if this 3D box lies inside the box. |
| 116 |
* @note It's up to the client programmer to make sure that both bounding boxes are valid. |
| 117 |
*/ |
| 118 |
inline bool IsInBox(const BoundBox3<Precision>& rcBB) const; |
| 119 |
/** Checks if this 2D box lies inside the box. |
| 120 |
* @note It's up to the client programmer to make sure that both bounding boxes are valid. |
| 121 |
*/ |
| 122 |
inline bool IsInBox(const BoundBox2d& rcbb) const; |
| 123 |
/** Checks whether the bounding box is valid. */ |
| 124 |
bool IsValid() const; |
| 125 |
//@} |
| 126 |
|
| 127 |
enum OCTANT |
| 128 |
{ |
| 129 |
OCT_LDB = 0, |
| 130 |
OCT_RDB, |
| 131 |
OCT_LUB, |
| 132 |
OCT_RUB, |
| 133 |
OCT_LDF, |
| 134 |
OCT_RDF, |
| 135 |
OCT_LUF, |
| 136 |
OCT_RUF |
| 137 |
}; |
| 138 |
bool GetOctantFromVector(const Vector3<Precision>& rclVct, OCTANT& rclOctant) const; |
| 139 |
BoundBox3<Precision> CalcOctant(typename BoundBox3<Precision>::OCTANT Octant) const; |
| 140 |
|
| 141 |
enum SIDE |
| 142 |
{ |
| 143 |
LEFT = 0, |
| 144 |
RIGHT = 1, |
| 145 |
TOP = 2, |
| 146 |
BOTTOM = 3, |
| 147 |
FRONT = 4, |
| 148 |
BACK = 5, |
| 149 |
INVALID = 255 |
| 150 |
}; |
| 151 |
enum CORNER |
| 152 |
{ |
| 153 |
TLB = 0, // top-left-back |
| 154 |
TLF = 1, // top-left-front |
| 155 |
TRF = 2, // top-right-front |
| 156 |
TRB = 3, // top-right-back |
| 157 |
BLB = 4, // bottom-left-back |
| 158 |
BLF = 5, // bottom-left-front |
| 159 |
BRF = 6, // bottom-right-front |
| 160 |
BRB = 7, // bottom-right-back |
| 161 |
}; |
| 162 |
enum EDGE |
| 163 |
{ |
| 164 |
TLB_TLF = 0, |
| 165 |
TLF_TRF = 1, |
| 166 |
TRF_TRB = 2, |
| 167 |
TRB_TLB = 3, |
| 168 |
BLB_BLF = 4, |
| 169 |
BLF_BRF = 5, |
| 170 |
BRF_BRB = 6, |
| 171 |
BRB_BLB = 7, |
| 172 |
TLB_BLB = 8, |
| 173 |
TLF_BLF = 9, |
| 174 |
TRF_BRF = 10, |
| 175 |
TRB_BRB = 11 |
| 176 |
}; |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the corner point \a usPoint. |
| 180 |
*/ |
| 181 |
inline Vector3<Precision> CalcPoint(unsigned short usPoint) const; |
| 182 |
/** Returns the plane of the given side. */ |
| 183 |
void CalcPlane(unsigned short usPlane, Vector3<Precision>& rBase, Vector3<Precision>& rNormal) const; |
| 184 |
/** Calculates the two points of an edge. |
| 185 |
*/ |
| 186 |
bool CalcEdge(unsigned short usEdge, Vector3<Precision>& rcP0, Vector3<Precision>& rcP1) const; |
| 187 |
/** Intersection point of an inner search ray with the bounding box, built of |
| 188 |
* the base \a rcVct and the direction \a rcVctDir. \a rcVct must lie inside the |
| 189 |
* bounding box. |
| 190 |
*/ |
| 191 |
bool IntersectionPoint( |
| 192 |
const Vector3<Precision>& rcVct, |
| 193 |
const Vector3<Precision>& rcVctDir, |
| 194 |
Vector3<Precision>& cVctRes, |
| 195 |
Precision epsilon |
| 196 |
) const; |
| 197 |
/** Checks for intersection with line incl. search tolerance. */ |
| 198 |
bool IsCutLine( |
| 199 |
const Vector3<Precision>& rcBase, |
| 200 |
const Vector3<Precision>& rcDir, |
| 201 |
Precision fTolerance = 0.0F |
| 202 |
) const; |
| 203 |
/** Checks if this plane specified by (point,normal) cuts this box. |
| 204 |
* @note It's up to the client programmer to make sure that this bounding box is valid. |
| 205 |
*/ |
| 206 |
inline bool IsCutPlane(const Vector3<Precision>& rclBase, const Vector3<Precision>& rclNormal) const; |
| 207 |
/** Computes the intersection points of line and bounding box. */ |
| 208 |
bool IntersectWithLine( |
| 209 |
const Vector3<Precision>& rcBase, |
| 210 |
const Vector3<Precision>& rcDir, |
| 211 |
Vector3<Precision>& rcP0, |
| 212 |
Vector3<Precision>& rcP1 |
| 213 |
) const; |
| 214 |
/** Computes the intersection point of line and a plane of the bounding box. */ |
| 215 |
bool IntersectPlaneWithLine( |
| 216 |
unsigned short usSide, |
| 217 |
const Vector3<Precision>& rcBase, |
| 218 |
const Vector3<Precision>& rcDir, |
| 219 |
Vector3<Precision>& rcP0 |
| 220 |
) const; |
| 221 |
/** Returns the side of the bounding box the ray exits. */ |
| 222 |
typename BoundBox3<Precision>::SIDE GetSideFromRay( |
| 223 |
const Vector3<Precision>& rclPt, |
| 224 |
const Vector3<Precision>& rclDir |
| 225 |
) const; |
| 226 |
/** Returns the side of the bounding box the ray exits. */ |
| 227 |
typename BoundBox3<Precision>::SIDE GetSideFromRay( |
| 228 |
const Vector3<Precision>& rclPt, |
| 229 |
const Vector3<Precision>& rclDir, |
| 230 |
Vector3<Precision>& rcInt |
| 231 |
) const; |
| 232 |
|
| 233 |
/** |
| 234 |
* Searches for the closest point of the bounding box. |
| 235 |
*/ |
| 236 |
Vector3<Precision> ClosestPoint(const Vector3<Precision>& rclPt) const; |
| 237 |
/** Projects the box onto a plane and returns a 2D box. */ |
| 238 |
BoundBox2d ProjectBox(const ViewProjMethod* proj) const; |
| 239 |
/** Transform the corners of this box with the given matrix and create a new bounding box. |
| 240 |
* @note It's up to the client programmer to make sure that this bounding box is valid. |
| 241 |
*/ |
| 242 |
BoundBox3<Precision> Transformed(const Matrix4D& mat) const; |
| 243 |
|
| 244 |
/** Returns the center of the box. */ |
| 245 |
inline Vector3<Precision> GetCenter() const; |
| 246 |
/** Returns the minimum point of the box. */ |
| 247 |
inline Vector3<Precision> GetMinimum() const; |
| 248 |
/** Returns the maximum point of the box. */ |
| 249 |
inline Vector3<Precision> GetMaximum() const; |
| 250 |
/** Compute the diagonal length of this bounding box. |
| 251 |
* @note It's up to the client programmer to make sure that this bounding box is valid. |
| 252 |
*/ |
| 253 |
inline Precision CalcDiagonalLength() const; |
| 254 |
void SetVoid(); |
| 255 |
|
| 256 |
/** Enlarges the box with \a fLen. */ |
| 257 |
inline void Enlarge(Precision fLen); |
| 258 |
/** Shrinks the box with \a fLen. */ |
| 259 |
inline void Shrink(Precision fLen); |
| 260 |
|
| 261 |
/** Calculates expansion in x-direction. */ |
| 262 |
inline Precision LengthX() const; |
| 263 |
/** Calculates expansion in y-direction. */ |
| 264 |
inline Precision LengthY() const; |
| 265 |
/** Calculates expansion in z-direction. */ |
| 266 |
inline Precision LengthZ() const; |
| 267 |
/** Calculates the volume. If the box is invalid a negative value is returned */ |
| 268 |
inline Precision Volume() const; |
| 269 |
/** Moves in x-direction. */ |
| 270 |
inline void MoveX(Precision value); |
| 271 |
/** Moves in y-direction. */ |
| 272 |
inline void MoveY(Precision value); |
| 273 |
/** Moves in z-direction. */ |
| 274 |
inline void MoveZ(Precision value); |
| 275 |
/** Scales in x-direction. */ |
| 276 |
inline void ScaleX(Precision value); |
| 277 |
/** Scales in y-direction. */ |
| 278 |
inline void ScaleY(Precision value); |
| 279 |
/** Scales in z-direction. */ |
| 280 |
inline void ScaleZ(Precision value); |
| 281 |
|
| 282 |
/** Prints the values to stream. */ |
| 283 |
void Print(std::ostream&) const; |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
template<class Precision> |
| 288 |
bool BoundBox3<Precision>::isOnRayW(Precision min, Precision max, Precision val) |
| 289 |
{ |
| 290 |
// Checks if point val lies on the ray [min,max] |
| 291 |
return ((min <= val) && (val <= max)); |
| 292 |
} |
| 293 |
|
| 294 |
template<class Precision> |
| 295 |
bool BoundBox3<Precision>::isOnRayS(Precision min, Precision max, Precision val) |
| 296 |
{ |
| 297 |
// Checks if point val lies on the ray [min,max[ |
| 298 |
return ((min <= val) && (val < max)); |
| 299 |
} |
| 300 |
|
| 301 |
// NOLINTBEGIN(bugprone-easily-swappable-parameters) |
| 302 |
template<class Precision> |
| 303 |
inline BoundBox3<Precision>::BoundBox3( |
| 304 |
Precision fMinX, |
| 305 |
Precision fMinY, |
| 306 |
Precision fMinZ, |
| 307 |
Precision fMaxX, |
| 308 |
Precision fMaxY, |
| 309 |
Precision fMaxZ |
| 310 |
) |
| 311 |
: MinX(fMinX) |
| 312 |
, MinY(fMinY) |
| 313 |
, MinZ(fMinZ) |
| 314 |
, MaxX(fMaxX) |
| 315 |
, MaxY(fMaxY) |
| 316 |
, MaxZ(fMaxZ) |
| 317 |
{} |
| 318 |
// NOLINTEND(bugprone-easily-swappable-parameters) |
| 319 |
|
| 320 |
template<class Precision> |
| 321 |
inline BoundBox3<Precision>::BoundBox3(const Vector3<Precision>* pclVect, std::size_t ulCt) |
| 322 |
: MinX(std::numeric_limits<Precision>::max()) |
| 323 |
, MinY(std::numeric_limits<Precision>::max()) |
| 324 |
, MinZ(std::numeric_limits<Precision>::max()) |
| 325 |
, MaxX(-std::numeric_limits<Precision>::max()) |
| 326 |
, MaxY(-std::numeric_limits<Precision>::max()) |
| 327 |
, MaxZ(-std::numeric_limits<Precision>::max()) |
| 328 |
{ |
| 329 |
const Vector3<Precision>* pI = nullptr; |
| 330 |
const Vector3<Precision>* pEnd = pclVect + ulCt; |
| 331 |
for (pI = pclVect; pI < pEnd; ++pI) { |
| 332 |
MinX = std::min<Precision>(MinX, pI->x); |
| 333 |
MinY = std::min<Precision>(MinY, pI->y); |
| 334 |
MinZ = std::min<Precision>(MinZ, pI->z); |
| 335 |
MaxX = std::max<Precision>(MaxX, pI->x); |
| 336 |
MaxY = std::max<Precision>(MaxY, pI->y); |
| 337 |
MaxZ = std::max<Precision>(MaxZ, pI->z); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
template<class Precision> |
| 342 |
inline BoundBox3<Precision>::BoundBox3(const Vector3<Precision>& point, Precision distance) |
| 343 |
: MinX(point.x - distance) |
| 344 |
, MinY(point.y - distance) |
| 345 |
, MinZ(point.z - distance) |
| 346 |
, MaxX(point.x + distance) |
| 347 |
, MaxY(point.y + distance) |
| 348 |
, MaxZ(point.z + distance) |
| 349 |
{} |
| 350 |
|
| 351 |
template<class Precision> |
| 352 |
inline BoundBox3<Precision>::~BoundBox3() = default; |
| 353 |
|
| 354 |
template<class Precision> |
| 355 |
inline bool BoundBox3<Precision>::Intersect(const BoundBox3<Precision>& rcBB) const |
| 356 |
{ |
| 357 |
if (rcBB.MaxX < this->MinX || rcBB.MinX > this->MaxX) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
if (rcBB.MaxY < this->MinY || rcBB.MinY > this->MaxY) { |
| 361 |
return false; |
| 362 |
} |
| 363 |
if (rcBB.MaxZ < this->MinZ || rcBB.MinZ > this->MaxZ) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
template<class Precision> |
| 370 |
bool BoundBox3<Precision>::operator&&(const BoundBox3<Precision>& rcBB) const |
| 371 |
{ |
| 372 |
return Intersect(rcBB); |
| 373 |
} |
| 374 |
|
| 375 |
template<class Precision> |
| 376 |
inline bool BoundBox3<Precision>::Intersect(const BoundBox2d& rcBB) const |
| 377 |
{ |
| 378 |
if (rcBB.MaxX < this->MinX || rcBB.MinX > this->MaxX) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
if (rcBB.MaxY < this->MinY || rcBB.MinY > this->MaxY) { |
| 382 |
return false; |
| 383 |
} |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
template<class Precision> |
| 388 |
inline bool BoundBox3<Precision>::operator&&(const BoundBox2d& rcBB) const |
| 389 |
{ |
| 390 |
return Intersect(rcBB); |
| 391 |
} |
| 392 |
|
| 393 |
template<class Precision> |
| 394 |
inline BoundBox3<Precision> BoundBox3<Precision>::Intersected(const BoundBox3<Precision>& rcBB) const |
| 395 |
{ |
| 396 |
BoundBox3<Precision> cBBRes; |
| 397 |
|
| 398 |
cBBRes.MinX = std::max<Precision>(MinX, rcBB.MinX); |
| 399 |
cBBRes.MaxX = std::min<Precision>(MaxX, rcBB.MaxX); |
| 400 |
cBBRes.MinY = std::max<Precision>(MinY, rcBB.MinY); |
| 401 |
cBBRes.MaxY = std::min<Precision>(MaxY, rcBB.MaxY); |
| 402 |
cBBRes.MinZ = std::max<Precision>(MinZ, rcBB.MinZ); |
| 403 |
cBBRes.MaxZ = std::min<Precision>(MaxZ, rcBB.MaxZ); |
| 404 |
|
| 405 |
return cBBRes; |
| 406 |
} |
| 407 |
|
| 408 |
template<class Precision> |
| 409 |
inline BoundBox3<Precision> BoundBox3<Precision>::United(const BoundBox3<Precision>& rcBB) const |
| 410 |
{ |
| 411 |
BoundBox3<Precision> cBBRes; |
| 412 |
|
| 413 |
cBBRes.MinX = std::min<Precision>(MinX, rcBB.MinX); |
| 414 |
cBBRes.MaxX = std::max<Precision>(MaxX, rcBB.MaxX); |
| 415 |
cBBRes.MinY = std::min<Precision>(MinY, rcBB.MinY); |
| 416 |
cBBRes.MaxY = std::max<Precision>(MaxY, rcBB.MaxY); |
| 417 |
cBBRes.MinZ = std::min<Precision>(MinZ, rcBB.MinZ); |
| 418 |
cBBRes.MaxZ = std::max<Precision>(MaxZ, rcBB.MaxZ); |
| 419 |
|
| 420 |
return cBBRes; |
| 421 |
} |
| 422 |
|
| 423 |
template<class Precision> |
| 424 |
inline void BoundBox3<Precision>::Add(const Vector3<Precision>& rclVect) |
| 425 |
{ |
| 426 |
this->MinX = std::min<Precision>(this->MinX, rclVect.x); |
| 427 |
this->MinY = std::min<Precision>(this->MinY, rclVect.y); |
| 428 |
this->MinZ = std::min<Precision>(this->MinZ, rclVect.z); |
| 429 |
this->MaxX = std::max<Precision>(this->MaxX, rclVect.x); |
| 430 |
this->MaxY = std::max<Precision>(this->MaxY, rclVect.y); |
| 431 |
this->MaxZ = std::max<Precision>(this->MaxZ, rclVect.z); |
| 432 |
} |
| 433 |
|
| 434 |
template<class Precision> |
| 435 |
inline void BoundBox3<Precision>::Add(const BoundBox3<Precision>& rcBB) |
| 436 |
{ |
| 437 |
this->MinX = std::min<Precision>(this->MinX, rcBB.MinX); |
| 438 |
this->MaxX = std::max<Precision>(this->MaxX, rcBB.MaxX); |
| 439 |
this->MinY = std::min<Precision>(this->MinY, rcBB.MinY); |
| 440 |
this->MaxY = std::max<Precision>(this->MaxY, rcBB.MaxY); |
| 441 |
this->MinZ = std::min<Precision>(this->MinZ, rcBB.MinZ); |
| 442 |
this->MaxZ = std::max<Precision>(this->MaxZ, rcBB.MaxZ); |
| 443 |
} |
| 444 |
|
| 445 |
template<class Precision> |
| 446 |
inline bool BoundBox3<Precision>::IsInBox(const Vector3<Precision>& rcVct) const |
| 447 |
{ |
| 448 |
if (rcVct.x < this->MinX || rcVct.x > this->MaxX) { |
| 449 |
return false; |
| 450 |
} |
| 451 |
if (rcVct.y < this->MinY || rcVct.y > this->MaxY) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
if (rcVct.z < this->MinZ || rcVct.z > this->MaxZ) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
return true; |
| 458 |
} |
| 459 |
|
| 460 |
template<class Precision> |
| 461 |
inline bool BoundBox3<Precision>::IsInBox(const BoundBox3<Precision>& rcBB) const |
| 462 |
{ |
| 463 |
if (rcBB.MinX < this->MinX || rcBB.MaxX > this->MaxX) { |
| 464 |
return false; |
| 465 |
} |
| 466 |
if (rcBB.MinY < this->MinY || rcBB.MaxY > this->MaxY) { |
| 467 |
return false; |
| 468 |
} |
| 469 |
if (rcBB.MinZ < this->MinZ || rcBB.MaxZ > this->MaxZ) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
return true; |
| 473 |
} |
| 474 |
|
| 475 |
template<class Precision> |
| 476 |
inline bool BoundBox3<Precision>::IsInBox(const BoundBox2d& rcBB) const |
| 477 |
{ |
| 478 |
if (rcBB.MinX < this->MinX || rcBB.MaxX > this->MaxX) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
if (rcBB.MinY < this->MinY || rcBB.MaxY > this->MaxY) { |
| 482 |
return false; |
| 483 |
} |
| 484 |
return true; |
| 485 |
} |
| 486 |
|
| 487 |
template<class Precision> |
| 488 |
inline bool BoundBox3<Precision>::IsValid() const |
| 489 |
{ |
| 490 |
return ((MinX <= MaxX) && (MinY <= MaxY) && (MinZ <= MaxZ)); |
| 491 |
} |
| 492 |
|
| 493 |
template<class Precision> |
| 494 |
inline bool BoundBox3<Precision>::GetOctantFromVector( |
| 495 |
const Vector3<Precision>& rclVct, |
| 496 |
OCTANT& rclOctant |
| 497 |
) const |
| 498 |
{ |
| 499 |
if (!IsInBox(rclVct)) { |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
unsigned short usNdx = 0; |
| 504 |
if (isOnRayS((MinX + MaxX) / 2, MaxX, rclVct.x)) { // left/RIGHT |
| 505 |
usNdx |= 1; |
| 506 |
} |
| 507 |
if (isOnRayS((MinY + MaxY) / 2, MaxY, rclVct.y)) { // down/UP |
| 508 |
usNdx |= 2; |
| 509 |
} |
| 510 |
if (isOnRayS((MinZ + MaxZ) / 2, MaxZ, rclVct.z)) { // back/FRONT |
| 511 |
usNdx |= 4; |
| 512 |
} |
| 513 |
rclOctant = static_cast<OCTANT>(usNdx); |
| 514 |
return true; |
| 515 |
} |
| 516 |
|
| 517 |
template<class Precision> |
| 518 |
inline BoundBox3<Precision> BoundBox3<Precision>::CalcOctant( |
| 519 |
typename BoundBox3<Precision>::OCTANT Octant |
| 520 |
) const |
| 521 |
{ |
| 522 |
BoundBox3<Precision> cOct(*this); |
| 523 |
|
| 524 |
switch (Octant) { |
| 525 |
case OCT_LDB: |
| 526 |
cOct.MaxX = (cOct.MinX + cOct.MaxX) / 2; |
| 527 |
cOct.MaxY = (cOct.MinY + cOct.MaxY) / 2; |
| 528 |
cOct.MaxZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 529 |
break; |
| 530 |
|
| 531 |
case OCT_RDB: |
| 532 |
cOct.MinX = (cOct.MinX + cOct.MaxX) / 2; |
| 533 |
cOct.MaxY = (cOct.MinY + cOct.MaxY) / 2; |
| 534 |
cOct.MaxZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 535 |
break; |
| 536 |
|
| 537 |
case OCT_LUB: |
| 538 |
cOct.MaxX = (cOct.MinX + cOct.MaxX) / 2; |
| 539 |
cOct.MinY = (cOct.MinY + cOct.MaxY) / 2; |
| 540 |
cOct.MaxZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 541 |
break; |
| 542 |
|
| 543 |
case OCT_RUB: |
| 544 |
cOct.MinX = (cOct.MinX + cOct.MaxX) / 2; |
| 545 |
cOct.MinY = (cOct.MinY + cOct.MaxY) / 2; |
| 546 |
cOct.MaxZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 547 |
break; |
| 548 |
|
| 549 |
case OCT_LDF: |
| 550 |
cOct.MaxX = (cOct.MinX + cOct.MaxX) / 2; |
| 551 |
cOct.MaxY = (cOct.MinY + cOct.MaxY) / 2; |
| 552 |
cOct.MinZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 553 |
break; |
| 554 |
|
| 555 |
case OCT_RDF: |
| 556 |
cOct.MinX = (cOct.MinX + cOct.MaxX) / 2; |
| 557 |
cOct.MaxY = (cOct.MinY + cOct.MaxY) / 2; |
| 558 |
cOct.MinZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 559 |
break; |
| 560 |
|
| 561 |
case OCT_LUF: |
| 562 |
cOct.MaxX = (cOct.MinX + cOct.MaxX) / 2; |
| 563 |
cOct.MinY = (cOct.MinY + cOct.MaxY) / 2; |
| 564 |
cOct.MinZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 565 |
break; |
| 566 |
|
| 567 |
case OCT_RUF: |
| 568 |
cOct.MinX = (cOct.MinX + cOct.MaxX) / 2; |
| 569 |
cOct.MinY = (cOct.MinY + cOct.MaxY) / 2; |
| 570 |
cOct.MinZ = (cOct.MinZ + cOct.MaxZ) / 2; |
| 571 |
break; |
| 572 |
} |
| 573 |
return cOct; |
| 574 |
} |
| 575 |
|
| 576 |
template<class Precision> |
| 577 |
inline Vector3<Precision> BoundBox3<Precision>::CalcPoint(unsigned short usPoint) const |
| 578 |
{ |
| 579 |
switch (usPoint) { |
| 580 |
case TLB: |
| 581 |
return Vector3<Precision>(MinX, MinY, MaxZ); |
| 582 |
case TLF: |
| 583 |
return Vector3<Precision>(MaxX, MinY, MaxZ); |
| 584 |
case TRF: |
| 585 |
return Vector3<Precision>(MaxX, MaxY, MaxZ); |
| 586 |
case TRB: |
| 587 |
return Vector3<Precision>(MinX, MaxY, MaxZ); |
| 588 |
case BLB: |
| 589 |
return Vector3<Precision>(MinX, MinY, MinZ); |
| 590 |
case BLF: |
| 591 |
return Vector3<Precision>(MaxX, MinY, MinZ); |
| 592 |
case BRF: |
| 593 |
return Vector3<Precision>(MaxX, MaxY, MinZ); |
| 594 |
case BRB: |
| 595 |
return Vector3<Precision>(MinX, MaxY, MinZ); |
| 596 |
} |
| 597 |
|
| 598 |
return Vector3<Precision>(); |
| 599 |
} |
| 600 |
|
| 601 |
// NOLINTBEGIN(bugprone-easily-swappable-parameters) |
| 602 |
template<class Precision> |
| 603 |
inline void BoundBox3<Precision>::CalcPlane( |
| 604 |
unsigned short usPlane, |
| 605 |
Vector3<Precision>& rBase, |
| 606 |
Vector3<Precision>& rNormal |
| 607 |
) const |
| 608 |
// NOLINTEND(bugprone-easily-swappable-parameters) |
| 609 |
{ |
| 610 |
switch (usPlane) { |
| 611 |
case LEFT: |
| 612 |
rBase.Set(MinX, MinY, MaxZ); |
| 613 |
rNormal.Set(1.0F, 0.0F, 0.0F); |
| 614 |
break; |
| 615 |
|
| 616 |
case RIGHT: |
| 617 |
rBase.Set(MaxX, MinY, MaxZ); |
| 618 |
rNormal.Set(1.0F, 0.0F, 0.0F); |
| 619 |
break; |
| 620 |
|
| 621 |
case TOP: |
| 622 |
rBase.Set(MinX, MaxY, MaxZ); |
| 623 |
rNormal.Set(0.0F, 1.0F, 0.0F); |
| 624 |
break; |
| 625 |
|
| 626 |
case BOTTOM: |
| 627 |
rBase.Set(MinX, MinY, MaxZ); |
| 628 |
rNormal.Set(0.0F, 1.0F, 0.0F); |
| 629 |
break; |
| 630 |
|
| 631 |
case FRONT: |
| 632 |
rBase.Set(MinX, MinY, MaxZ); |
| 633 |
rNormal.Set(0.0F, 0.0F, 1.0F); |
| 634 |
break; |
| 635 |
|
| 636 |
case BACK: |
| 637 |
rBase.Set(MinX, MinY, MinZ); |
| 638 |
rNormal.Set(0.0F, 0.0F, 1.0F); |
| 639 |
break; |
| 640 |
default: |
| 641 |
break; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
template<class Precision> |
| 646 |
inline bool BoundBox3<Precision>::CalcEdge( |
| 647 |
unsigned short usEdge, |
| 648 |
Vector3<Precision>& rcP0, |
| 649 |
Vector3<Precision>& rcP1 |
| 650 |
) const |
| 651 |
{ |
| 652 |
switch (usEdge) { |
| 653 |
case TLB_TLF: |
| 654 |
rcP0 = CalcPoint(TLB); |
| 655 |
rcP1 = CalcPoint(TLF); |
| 656 |
break; |
| 657 |
case TLF_TRF: |
| 658 |
rcP0 = CalcPoint(TLF); |
| 659 |
rcP1 = CalcPoint(TRF); |
| 660 |
break; |
| 661 |
case TRF_TRB: |
| 662 |
rcP0 = CalcPoint(TRF); |
| 663 |
rcP1 = CalcPoint(TRB); |
| 664 |
break; |
| 665 |
case TRB_TLB: |
| 666 |
rcP0 = CalcPoint(TRB); |
| 667 |
rcP1 = CalcPoint(TLB); |
| 668 |
break; |
| 669 |
case BLB_BLF: |
| 670 |
rcP0 = CalcPoint(BLB); |
| 671 |
rcP1 = CalcPoint(BLF); |
| 672 |
break; |
| 673 |
case BLF_BRF: |
| 674 |
rcP0 = CalcPoint(BLF); |
| 675 |
rcP1 = CalcPoint(BRF); |
| 676 |
break; |
| 677 |
case BRF_BRB: |
| 678 |
rcP0 = CalcPoint(BRF); |
| 679 |
rcP1 = CalcPoint(BRB); |
| 680 |
break; |
| 681 |
case BRB_BLB: |
| 682 |
rcP0 = CalcPoint(BRB); |
| 683 |
rcP1 = CalcPoint(BLB); |
| 684 |
break; |
| 685 |
case TLB_BLB: |
| 686 |
rcP0 = CalcPoint(TLB); |
| 687 |
rcP1 = CalcPoint(BLB); |
| 688 |
break; |
| 689 |
case TLF_BLF: |
| 690 |
rcP0 = CalcPoint(TLF); |
| 691 |
rcP1 = CalcPoint(BLF); |
| 692 |
break; |
| 693 |
case TRF_BRF: |
| 694 |
rcP0 = CalcPoint(TRF); |
| 695 |
rcP1 = CalcPoint(BRF); |
| 696 |
break; |
| 697 |
case TRB_BRB: |
| 698 |
rcP0 = CalcPoint(TRB); |
| 699 |
rcP1 = CalcPoint(BRB); |
| 700 |
break; |
| 701 |
default: |
| 702 |
return false; // undefined |
| 703 |
} |
| 704 |
|
| 705 |
return true; |
| 706 |
} |
| 707 |
|
| 708 |
template<class Precision> |
| 709 |
inline bool BoundBox3<Precision>::IntersectionPoint( |
| 710 |
const Vector3<Precision>& rcVct, |
| 711 |
const Vector3<Precision>& rcVctDir, |
| 712 |
Vector3<Precision>& cVctRes, |
| 713 |
Precision epsilon |
| 714 |
) const |
| 715 |
{ |
| 716 |
const unsigned short num = 6; |
| 717 |
bool rc = false; |
| 718 |
BoundBox3<Precision> cCmpBound(*this); |
| 719 |
|
| 720 |
// enlarge bounding box by epsilon |
| 721 |
cCmpBound.Enlarge(epsilon); |
| 722 |
|
| 723 |
// Is point inside? |
| 724 |
if (cCmpBound.IsInBox(rcVct)) { |
| 725 |
// test sides |
| 726 |
for (unsigned short i = 0; (i < num) && (!rc); i++) { |
| 727 |
rc = IntersectPlaneWithLine(i, rcVct, rcVctDir, cVctRes); |
| 728 |
|
| 729 |
if (!cCmpBound.IsInBox(cVctRes)) { |
| 730 |
rc = false; |
| 731 |
} |
| 732 |
|
| 733 |
if (rc) { |
| 734 |
// does intersection point lie in desired direction |
| 735 |
// or was found the opposing side? |
| 736 |
// -> scalar product of both direction vectors > 0 (angle < 90) |
| 737 |
rc = ((cVctRes - rcVct) * rcVctDir) >= 0.0; |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
return rc; |
| 743 |
} |
| 744 |
|
| 745 |
template<class Precision> |
| 746 |
inline bool BoundBox3<Precision>::IsCutLine( |
| 747 |
const Vector3<Precision>& rcBase, |
| 748 |
const Vector3<Precision>& rcDir, |
| 749 |
Precision fTolerance |
| 750 |
) const |
| 751 |
{ |
| 752 |
const unsigned short num = 6; |
| 753 |
Precision fDist; |
| 754 |
|
| 755 |
// at first only a rough and quick test by the |
| 756 |
// Distance of the line to the center of the BB is calculated |
| 757 |
// and with the maximum diagonal length + fTolerance |
| 758 |
// will be compared. |
| 759 |
|
| 760 |
// Distance between center point and line |
| 761 |
fDist = (rcDir % (GetCenter() - rcBase)).Length() / rcDir.Length(); |
| 762 |
|
| 763 |
if (fDist > (CalcDiagonalLength() + fTolerance)) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
// more detailed test here |
| 768 |
Vector3<Precision> clVectRes; |
| 769 |
|
| 770 |
// intersect each face with the line |
| 771 |
for (unsigned short i = 0; i < num; i++) { |
| 772 |
if (IntersectPlaneWithLine(i, rcBase, rcDir, clVectRes)) { |
| 773 |
// Check whether the intersection point is within BB limits + Tolerance |
| 774 |
switch (i) { |
| 775 |
case LEFT: // left and right plane |
| 776 |
case RIGHT: |
| 777 |
if ((isOnRayW(MinY - fTolerance, MaxY + fTolerance, clVectRes.y) |
| 778 |
&& isOnRayW(MinZ - fTolerance, MaxZ + fTolerance, clVectRes.z))) { |
| 779 |
return true; |
| 780 |
} |
| 781 |
break; |
| 782 |
case TOP: // top and bottom plane |
| 783 |
case BOTTOM: |
| 784 |
if ((isOnRayW(MinX - fTolerance, MaxX + fTolerance, clVectRes.x) |
| 785 |
&& isOnRayW(MinZ - fTolerance, MaxZ + fTolerance, clVectRes.z))) { |
| 786 |
return true; |
| 787 |
} |
| 788 |
break; |
| 789 |
case FRONT: // front and back plane |
| 790 |
case BACK: |
| 791 |
if ((isOnRayW(MinX - fTolerance, MaxX + fTolerance, clVectRes.x) |
| 792 |
&& isOnRayW(MinY - fTolerance, MaxY + fTolerance, clVectRes.y))) { |
| 793 |
return true; |
| 794 |
} |
| 795 |
break; |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
return false; |
| 801 |
} |
| 802 |
|
| 803 |
template<class Precision> |
| 804 |
inline bool BoundBox3<Precision>::IsCutPlane( |
| 805 |
const Vector3<Precision>& rclBase, |
| 806 |
const Vector3<Precision>& rclNormal |
| 807 |
) const |
| 808 |
{ |
| 809 |
const unsigned short num = 8; |
| 810 |
if (fabs(GetCenter().DistanceToPlane(rclBase, rclNormal)) < CalcDiagonalLength()) { |
| 811 |
Precision fD = CalcPoint(CORNER::TLB).DistanceToPlane(rclBase, rclNormal); |
| 812 |
for (unsigned short i = 1; i < num; i++) { |
| 813 |
if ((CalcPoint(i).DistanceToPlane(rclBase, rclNormal) * fD) <= 0.0F) { |
| 814 |
return true; |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
return false; |
| 819 |
} |
| 820 |
|
| 821 |
// NOLINTBEGIN(bugprone-easily-swappable-parameters) |
| 822 |
template<class Precision> |
| 823 |
inline bool BoundBox3<Precision>::IntersectWithLine( |
| 824 |
const Vector3<Precision>& rcBase, |
| 825 |
const Vector3<Precision>& rcDir, |
| 826 |
Vector3<Precision>& rcP0, |
| 827 |
Vector3<Precision>& rcP1 |
| 828 |
) const |
| 829 |
// NOLINTEND(bugprone-easily-swappable-parameters) |
| 830 |
{ |
| 831 |
const unsigned short num = 6; |
| 832 |
Vector3<Precision> clVectRes; |
| 833 |
std::array<Vector3<Precision>, num> clVect; |
| 834 |
unsigned short numIntersect = 0; |
| 835 |
|
| 836 |
auto checkIntersect = [&](Base::Vector3<Precision> p1, Base::Vector3<Precision> p2) { |
| 837 |
if (isOnRayS(p1.x, p1.y, p1.z) && isOnRayS(p2.x, p2.y, p2.z)) { |
| 838 |
clVect[numIntersect] = clVectRes; |
| 839 |
numIntersect++; |
| 840 |
} |
| 841 |
}; |
| 842 |
|
| 843 |
// cut each face with the line |
| 844 |
for (unsigned short i = 0; i < num; i++) { |
| 845 |
if (IntersectPlaneWithLine(i, rcBase, rcDir, clVectRes)) { |
| 846 |
// check if intersection point is inside |
| 847 |
switch (i) { |
| 848 |
case LEFT: // left and right plane |
| 849 |
case RIGHT: |
| 850 |
checkIntersect( |
| 851 |
Vector3<Precision> {MinY, MaxY, clVectRes.y}, |
| 852 |
Vector3<Precision> {MinZ, MaxZ, clVectRes.z} |
| 853 |
); |
| 854 |
break; |
| 855 |
case TOP: // top and bottom plane |
| 856 |
case BOTTOM: |
| 857 |
checkIntersect( |
| 858 |
Vector3<Precision> {MinX, MaxX, clVectRes.x}, |
| 859 |
Vector3<Precision> {MinZ, MaxZ, clVectRes.z} |
| 860 |
); |
| 861 |
break; |
| 862 |
case FRONT: // front and back plane |
| 863 |
case BACK: |
| 864 |
checkIntersect( |
| 865 |
Vector3<Precision> {MinX, MaxX, clVectRes.x}, |
| 866 |
Vector3<Precision> {MinY, MaxY, clVectRes.y} |
| 867 |
); |
| 868 |
break; |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
if (numIntersect == 2) { |
| 874 |
rcP0 = clVect[0]; |
| 875 |
rcP1 = clVect[1]; |
| 876 |
return true; |
| 877 |
} |
| 878 |
|
| 879 |
if (numIntersect > 2) { // search two different intersection points |
| 880 |
for (unsigned short i = 1; i < numIntersect; i++) { |
| 881 |
if (clVect[i] != clVect[0]) { |
| 882 |
rcP0 = clVect[0]; |
| 883 |
rcP1 = clVect[i]; |
| 884 |
return true; |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
return false; |
| 890 |
} |
| 891 |
|
| 892 |
// NOLINTBEGIN(bugprone-easily-swappable-parameters) |
| 893 |
template<class Precision> |
| 894 |
inline bool BoundBox3<Precision>::IntersectPlaneWithLine( |
| 895 |
unsigned short usSide, |
| 896 |
const Vector3<Precision>& rcBase, |
| 897 |
const Vector3<Precision>& rcDir, |
| 898 |
Vector3<Precision>& rcP0 |
| 899 |
) const |
| 900 |
// NOLINTEND(bugprone-easily-swappable-parameters) |
| 901 |
{ |
| 902 |
Precision value; |
| 903 |
Vector3<Precision> cBase; |
| 904 |
Vector3<Precision> cNormal; |
| 905 |
Vector3<Precision> cDir(rcDir); |
| 906 |
CalcPlane(usSide, cBase, cNormal); |
| 907 |
|
| 908 |
if ((cNormal * cDir) == 0.0F) { |
| 909 |
return false; // no point of intersection |
| 910 |
} |
| 911 |
|
| 912 |
value = (cNormal * (cBase - rcBase)) / (cNormal * cDir); |
| 913 |
cDir.Scale(value, value, value); |
| 914 |
rcP0 = rcBase + cDir; |
| 915 |
return true; |
| 916 |
} |
| 917 |
|
| 918 |
template<class Precision> |
| 919 |
inline typename BoundBox3<Precision>::SIDE BoundBox3<Precision>::GetSideFromRay( |
| 920 |
const Vector3<Precision>& rclPt, |
| 921 |
const Vector3<Precision>& rclDir |
| 922 |
) const |
| 923 |
{ |
| 924 |
Vector3<Precision> cIntersection; |
| 925 |
return GetSideFromRay(rclPt, rclDir, cIntersection); |
| 926 |
} |
| 927 |
|
| 928 |
template<class Precision> |
| 929 |
inline typename BoundBox3<Precision>::SIDE BoundBox3<Precision>::GetSideFromRay( |
| 930 |
const Vector3<Precision>& rclPt, |
| 931 |
const Vector3<Precision>& rclDir, |
| 932 |
Vector3<Precision>& rcInt |
| 933 |
) const |
| 934 |
{ |
| 935 |
Vector3<Precision> cP0; |
| 936 |
Vector3<Precision> cP1; |
| 937 |
if (!IntersectWithLine(rclPt, rclDir, cP0, cP1)) { |
| 938 |
return INVALID; |
| 939 |
} |
| 940 |
|
| 941 |
Vector3<Precision> cOut; |
| 942 |
// same orientation |
| 943 |
if ((cP1 - cP0) * rclDir > 0) { |
| 944 |
cOut = cP1; |
| 945 |
} |
| 946 |
else { |
| 947 |
cOut = cP0; |
| 948 |
} |
| 949 |
|
| 950 |
rcInt = cOut; |
| 951 |
|
| 952 |
Precision fMax = 1.0e-3F; // NOLINT |
| 953 |
SIDE tSide = INVALID; |
| 954 |
|
| 955 |
if (fabs(cOut.x - MinX) < fMax) { // left plane |
| 956 |
fMax = Precision(fabs(cOut.x - MinX)); |
| 957 |
tSide = LEFT; |
| 958 |
} |
| 959 |
|
| 960 |
if (fabs(cOut.x - MaxX) < fMax) { // right plane |
| 961 |
fMax = Precision(fabs(cOut.x - MaxX)); |
| 962 |
tSide = RIGHT; |
| 963 |
} |
| 964 |
|
| 965 |
if (fabs(cOut.y - MinY) < fMax) { // bottom plane |
| 966 |
fMax = Precision(fabs(cOut.y - MinY)); |
| 967 |
tSide = BOTTOM; |
| 968 |
} |
| 969 |
|
| 970 |
if (fabs(cOut.y - MaxY) < fMax) { // top plane |
| 971 |
fMax = Precision(fabs(cOut.y - MaxY)); |
| 972 |
tSide = TOP; |
| 973 |
} |
| 974 |
|
| 975 |
if (fabs(cOut.z - MinZ) < fMax) { // front plane |
| 976 |
fMax = Precision(fabs(cOut.z - MinZ)); |
| 977 |
tSide = FRONT; |
| 978 |
} |
| 979 |
|
| 980 |
if (fabs(cOut.z - MaxZ) < fMax) { // back plane |
| 981 |
fMax = Precision(fabs(cOut.z - MaxZ)); |
| 982 |
tSide = BACK; |
| 983 |
} |
| 984 |
|
| 985 |
return tSide; |
| 986 |
} |
| 987 |
|
| 988 |
template<class Precision> |
| 989 |
inline Vector3<Precision> BoundBox3<Precision>::ClosestPoint(const Vector3<Precision>& rclPt) const |
| 990 |
{ |
| 991 |
Vector3<Precision> closest = rclPt; |
| 992 |
|
| 993 |
Vector3<Precision> center = GetCenter(); |
| 994 |
Precision devx = closest.x - center.x; |
| 995 |
Precision devy = closest.y - center.y; |
| 996 |
Precision devz = closest.z - center.z; |
| 997 |
|
| 998 |
Precision halfwidth = (MaxX - MinX) / 2; |
| 999 |
Precision halfheight = (MaxY - MinY) / 2; |
| 1000 |
Precision halfdepth = (MaxZ - MinZ) / 2; |
| 1001 |
|
| 1002 |
// Move point to be on the nearest plane of the box. |
| 1003 |
if ((fabs(devx) > fabs(devy)) && (fabs(devx) > fabs(devz))) { |
| 1004 |
closest.x = center.x + halfwidth * ((devx < 0.0) ? -1.0 : 1.0); |
| 1005 |
} |
| 1006 |
else if (fabs(devy) > fabs(devz)) { |
| 1007 |
closest.y = center.y + halfheight * ((devy < 0.0) ? -1.0 : 1.0); |
| 1008 |
} |
| 1009 |
else { |
| 1010 |
closest.z = center.z + halfdepth * ((devz < 0.0) ? -1.0 : 1.0); |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Clamp to be inside box. |
| 1014 |
closest.x = std::min<Precision>(std::max<Precision>(closest.x, MinX), MaxX); |
| 1015 |
closest.y = std::min<Precision>(std::max<Precision>(closest.y, MinY), MaxY); |
| 1016 |
closest.z = std::min<Precision>(std::max<Precision>(closest.z, MinZ), MaxZ); |
| 1017 |
|
| 1018 |
return closest; |
| 1019 |
} |
| 1020 |
|
| 1021 |
template<class Precision> |
| 1022 |
inline BoundBox2d BoundBox3<Precision>::ProjectBox(const ViewProjMethod* proj) const |
| 1023 |
{ |
| 1024 |
const int num = 8; |
| 1025 |
BoundBox2d clBB2D; |
| 1026 |
clBB2D.SetVoid(); |
| 1027 |
|
| 1028 |
for (int i = 0; i < num; i++) { |
| 1029 |
Vector3<Precision> clTrsPt = (*proj)(CalcPoint(i)); |
| 1030 |
clBB2D.Add(Vector2d(clTrsPt.x, clTrsPt.y)); |
| 1031 |
} |
| 1032 |
|
| 1033 |
return clBB2D; |
| 1034 |
} |
| 1035 |
|
| 1036 |
template<class Precision> |
| 1037 |
inline BoundBox3<Precision> BoundBox3<Precision>::Transformed(const Matrix4D& mat) const |
| 1038 |
{ |
| 1039 |
const int num = 8; |
| 1040 |
BoundBox3<Precision> bbox; |
| 1041 |
for (int i = 0; i < num; i++) { |
| 1042 |
bbox.Add(mat * CalcPoint(i)); |
| 1043 |
} |
| 1044 |
return bbox; |
| 1045 |
} |
| 1046 |
|
| 1047 |
template<class Precision> |
| 1048 |
inline Vector3<Precision> BoundBox3<Precision>::GetCenter() const |
| 1049 |
{ |
| 1050 |
return Vector3<Precision>((MaxX + MinX) / 2, (MaxY + MinY) / 2, (MaxZ + MinZ) / 2); |
| 1051 |
} |
| 1052 |
|
| 1053 |
template<class Precision> |
| 1054 |
inline Vector3<Precision> BoundBox3<Precision>::GetMinimum() const |
| 1055 |
{ |
| 1056 |
return Vector3<Precision>(MinX, MinY, MinZ); |
| 1057 |
} |
| 1058 |
|
| 1059 |
template<class Precision> |
| 1060 |
inline Vector3<Precision> BoundBox3<Precision>::GetMaximum() const |
| 1061 |
{ |
| 1062 |
return Vector3<Precision>(MaxX, MaxY, MaxZ); |
| 1063 |
} |
| 1064 |
|
| 1065 |
template<class Precision> |
| 1066 |
inline Precision BoundBox3<Precision>::CalcDiagonalLength() const |
| 1067 |
{ |
| 1068 |
return static_cast<Precision>(sqrt( |
| 1069 |
((MaxX - MinX) * (MaxX - MinX)) + ((MaxY - MinY) * (MaxY - MinY)) |
| 1070 |
+ ((MaxZ - MinZ) * (MaxZ - MinZ)) |
| 1071 |
)); |
| 1072 |
} |
| 1073 |
|
| 1074 |
template<class Precision> |
| 1075 |
inline void BoundBox3<Precision>::SetVoid() |
| 1076 |
{ |
| 1077 |
MinX = MinY = MinZ = std::numeric_limits<Precision>::max(); |
| 1078 |
MaxX = MaxY = MaxZ = -std::numeric_limits<Precision>::max(); |
| 1079 |
} |
| 1080 |
|
| 1081 |
template<class Precision> |
| 1082 |
inline void BoundBox3<Precision>::Enlarge(Precision fLen) |
| 1083 |
{ |
| 1084 |
MinX -= fLen; |
| 1085 |
MinY -= fLen; |
| 1086 |
MinZ -= fLen; |
| 1087 |
MaxX += fLen; |
| 1088 |
MaxY += fLen; |
| 1089 |
MaxZ += fLen; |
| 1090 |
} |
| 1091 |
|
| 1092 |
template<class Precision> |
| 1093 |
inline void BoundBox3<Precision>::Shrink(Precision fLen) |
| 1094 |
{ |
| 1095 |
MinX += fLen; |
| 1096 |
MinY += fLen; |
| 1097 |
MinZ += fLen; |
| 1098 |
MaxX -= fLen; |
| 1099 |
MaxY -= fLen; |
| 1100 |
MaxZ -= fLen; |
| 1101 |
} |
| 1102 |
|
| 1103 |
template<class Precision> |
| 1104 |
inline Precision BoundBox3<Precision>::LengthX() const |
| 1105 |
{ |
| 1106 |
return MaxX - MinX; |
| 1107 |
} |
| 1108 |
|
| 1109 |
template<class Precision> |
| 1110 |
inline Precision BoundBox3<Precision>::LengthY() const |
| 1111 |
{ |
| 1112 |
return MaxY - MinY; |
| 1113 |
} |
| 1114 |
|
| 1115 |
template<class Precision> |
| 1116 |
inline Precision BoundBox3<Precision>::LengthZ() const |
| 1117 |
{ |
| 1118 |
return MaxZ - MinZ; |
| 1119 |
} |
| 1120 |
|
| 1121 |
template<class Precision> |
| 1122 |
inline Precision BoundBox3<Precision>::Volume() const |
| 1123 |
{ |
| 1124 |
if (!IsValid()) { |
| 1125 |
return -1.0; |
| 1126 |
} |
| 1127 |
return LengthX() * LengthY() * LengthZ(); |
| 1128 |
} |
| 1129 |
|
| 1130 |
template<class Precision> |
| 1131 |
inline void BoundBox3<Precision>::MoveX(Precision value) |
| 1132 |
{ |
| 1133 |
MinX += value; |
| 1134 |
MaxX += value; |
| 1135 |
} |
| 1136 |
|
| 1137 |
template<class Precision> |
| 1138 |
inline void BoundBox3<Precision>::MoveY(Precision value) |
| 1139 |
{ |
| 1140 |
MinY += value; |
| 1141 |
MaxY += value; |
| 1142 |
} |
| 1143 |
|
| 1144 |
template<class Precision> |
| 1145 |
inline void BoundBox3<Precision>::MoveZ(Precision value) |
| 1146 |
{ |
| 1147 |
MinZ += value; |
| 1148 |
MaxZ += value; |
| 1149 |
} |
| 1150 |
|
| 1151 |
template<class Precision> |
| 1152 |
inline void BoundBox3<Precision>::ScaleX(Precision value) |
| 1153 |
{ |
| 1154 |
MinX *= value; |
| 1155 |
MaxX *= value; |
| 1156 |
} |
| 1157 |
|
| 1158 |
template<class Precision> |
| 1159 |
inline void BoundBox3<Precision>::ScaleY(Precision value) |
| 1160 |
{ |
| 1161 |
MinY *= value; |
| 1162 |
MaxY *= value; |
| 1163 |
} |
| 1164 |
|
| 1165 |
template<class Precision> |
| 1166 |
inline void BoundBox3<Precision>::ScaleZ(Precision value) |
| 1167 |
{ |
| 1168 |
MinZ *= value; |
| 1169 |
MaxZ *= value; |
| 1170 |
} |
| 1171 |
|
| 1172 |
using BoundBox3f = BoundBox3<float>; |
| 1173 |
using BoundBox3d = BoundBox3<double>; |
| 1174 |
|
| 1175 |
} // namespace Base |
| 1176 |
|
| 1177 |
#endif // BASE_BOUNDBOX_H |