1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #include "../MathGeoLibFwd.h"21 #include "../Math/float3.h"22 #include "../Math/SSEMath.h"23 24 #ifdef MATH_AUTOMATIC_SSE25 #include "../Math/float4.h"26 #endif27 28 #ifdef MATH_OGRE_INTEROP29 #include <OgreAxisAlignedBox.h>30 #endif31 #ifdef MATH_URHO3D_INTEROP32 #include <Urho3D/Math/BoundingBox.h>33 #endif34 35 [MATH_BEGIN_NAMESPACE]36 37 38 39 40 41 42 43 44 45 46 47 class [ALIGN16] [AABB]48 {49 public:50 51 52 vec [minPoint];53 54 vec [maxPoint];55 56 57 58 59 60 [AABB]() {}61 62 63 64 [AABB](const vec &minPoint, const vec &maxPoint);65 66 67 68 69 70 71 explicit [AABB](const [OBB] &obb);72 73 74 75 explicit [AABB](const [Sphere] &s);76 77 [FORCE_INLINE] static int [NumFaces]() { return 6; }78 [FORCE_INLINE] static int [NumEdges]() { return 12; }79 [FORCE_INLINE] static int [NumVertices]() { return 8; }80 81 82 float [MinX]() const { return minPoint.x; }83 float [MinY]() const { return minPoint.y; } 84 float [MinZ]() const { return minPoint.z; } 85 86 float [MaxX]() const { return maxPoint.x; }87 float [MaxY]() const { return maxPoint.y; } 88 float [MaxZ]() const { return maxPoint.z; } 89 90 91 92 93 94 void SetNegativeInfinity();95 96 97 98 99 100 void SetFromCenterAndSize(const vec ¢er, const vec &size);101 102 103 104 105 106 107 108 void SetFrom(const [OBB] &obb);109 110 111 112 113 114 115 116 117 118 119 120 121 void SetFrom(const [Sphere] &s);122 123 124 125 126 127 void SetFrom(const vec *pointArray, int numPoints);128 129 130 131 132 133 [Polyhedron] ToPolyhedron() const;134 135 136 137 138 139 [PBVolume<6>] ToPBVolume() const;140 141 142 143 144 145 [OBB] ToOBB() const;146 147 148 149 150 [Sphere] MinimalEnclosingSphere() const;151 152 153 154 155 156 [Sphere] MaximalContainedSphere() const;157 158 159 160 161 bool [IsFinite]() const;162 163 164 165 166 bool IsDegenerate() const;167 168 169 vec CenterPoint() const;170 171 vec [Centroid]() const { return CenterPoint(); }172 173 174 vec [AnyPointFast]() const { return minPoint; }175 176 177 178 179 180 181 182 vec PointInside(float x, float y, float z) const;183 184 185 186 187 188 [LineSegment] Edge(int edgeIndex) const;189 190 191 192 193 194 195 196 vec CornerPoint(int cornerIndex) const;197 198 199 200 201 202 203 204 205 206 vec ExtremePoint(const vec &direction) const;207 vec ExtremePoint(const vec &direction, float &projectionDistance) const;208 209 210 211 212 213 vec PointOnEdge(int edgeIndex, float u) const;214 215 216 217 218 219 vec FaceCenterPoint(int faceIndex) const;220 221 222 223 224 225 226 227 vec FacePoint(int faceIndex, float u, float v) const;228 229 230 231 232 233 vec FaceNormal(int faceIndex) const;234 235 236 237 238 239 240 [Plane] FacePlane(int faceIndex) const;241 242 243 244 245 void GetCornerPoints(vec *outPointArray) const;246 247 248 249 250 void GetFacePlanes([Plane] *outPlaneArray) const;251 252 253 254 255 256 257 static [AABB] MinimalEnclosingAABB(const vec *pointArray, int numPoints);258 259 260 261 262 263 264 265 266 267 268 static void ExtremePointsAlongAABB(const vec *pointArray, int numPoints, int &minx, int &maxx, int &miny, int &maxy, int &minz, int &maxz);269 270 271 272 static [AABB] FromCenterAndSize(const vec &aabbCenterPos, const vec &aabbSize);273 274 275 276 277 278 vec Size() const;279 280 281 282 283 vec HalfSize() const;284 285 286 287 288 289 vec [Diagonal]() const { return Size(); }290 291 292 293 294 vec [HalfDiagonal]() const { return HalfSize(); }295 296 297 298 float Volume() const;299 300 301 302 float SurfaceArea() const;303 304 305 306 307 vec RandomPointInside([LCG] &rng) const;308 309 310 311 312 vec RandomPointOnSurface([LCG] &rng) const;313 314 315 316 317 vec RandomPointOnEdge([LCG] &rng) const;318 319 320 321 322 vec RandomCornerPoint([LCG] &rng) const;323 324 325 326 327 void Translate(const vec &offset);328 329 330 331 332 333 334 335 void Scale(const vec ¢erPoint, float scaleFactor);336 337 338 339 340 341 342 343 void Scale(const vec ¢erPoint, const vec &scaleFactor);344 345 346 347 348 349 350 351 352 353 void TransformAsAABB(const [float3x3] &transform);354 void TransformAsAABB(const [float3x4] &transform);355 void TransformAsAABB(const [float4x4] &transform);356 void TransformAsAABB(const [Quat] &transform);357 358 359 360 361 362 363 364 365 [OBB] Transform(const [float3x3] &transform) const;366 [OBB] Transform(const [float3x4] &transform) const;367 [OBB] Transform(const [float4x4] &transform) const;368 [OBB] Transform(const [Quat] &transform) const;369 370 371 372 373 374 vec ClosestPoint(const vec &targetPoint) const;375 376 377 378 379 380 381 float [Distance](const vec &point) const;382 float [Distance](const [Sphere] &sphere) const;383 384 385 386 387 388 389 390 bool [Contains](const vec &point) const;391 bool [Contains](const [LineSegment] &lineSegment) const;392 bool [Contains](const vec &aabbMinPoint, const vec &aabbMaxPoint) const;393 bool [Contains](const [AABB] &aabb) const { return [Contains](aabb.[minPoint], aabb.[maxPoint]); }394 bool [Contains](const [OBB] &obb) const;395 bool [Contains](const [Sphere] &sphere) const;396 bool [Contains](const [Triangle] &triangle) const;397 bool [Contains](const [Polygon] &polygon) const;398 bool [Contains](const [Frustum] &frustum) const;399 bool [Contains](const [Polyhedron] &polyhedron) const;400 bool [Contains](const [Capsule] &capsule) const;401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 bool Intersects(const [Ray] &ray, float &dNear, float &dFar) const;416 bool Intersects(const [Ray] &ray) const;417 bool Intersects(const [Line] &line, float &dNear, float &dFar) const;418 bool Intersects(const [Line] &line) const;419 bool Intersects(const [LineSegment] &lineSegment, float &dNear, float &dFar) const;420 bool Intersects(const [LineSegment] &lineSegment) const;421 bool Intersects(const [Plane] &plane) const;422 bool Intersects(const [AABB] &aabb) const;423 bool Intersects(const [OBB] &obb) const;424 425 426 427 428 bool Intersects(const [Sphere] &sphere, vec *closestPointOnAABB = 0) const;429 bool Intersects(const [Capsule] &capsule) const;430 bool Intersects(const [Triangle] &triangle) const;431 bool Intersects(const [Polygon] &polygon) const;432 bool Intersects(const [Frustum] &frustum) const;433 bool Intersects(const [Polyhedron] &polyhedron) const;434 435 436 437 438 439 void ProjectToAxis(const vec &axis, float &dMin, float &dMax) const;440 441 int UniqueFaceNormals(vec *out) const;442 int UniqueEdgeDirections(vec *out) const;443 444 445 446 447 448 void Enclose(const vec &point);449 void Enclose(const vec &aabbMinPoint, const vec &aabbMaxPoint);450 void Enclose(const [LineSegment] &lineSegment);451 void [Enclose](const [AABB] &aabb) { [Enclose](aabb.[minPoint], aabb.[maxPoint]); }452 void Enclose(const [OBB] &obb);453 void Enclose(const [Sphere] &sphere);454 void Enclose(const [Triangle] &triangle);455 void Enclose(const [Capsule] &capsule);456 void Enclose(const [Frustum] &frustum);457 void Enclose(const [Polygon] &polygon);458 void Enclose(const [Polyhedron] &polyhedron);459 void Enclose(const vec *pointArray, int numPoints);460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 void Triangulate(int numFacesX, int numFacesY, int numFacesZ,478 vec *outPos, vec *outNormal, [float2] *outUV,479 bool ccwIsFrontFacing) const;480 481 482 483 static int NumVerticesInTriangulation(int numFacesX, int numFacesY, int numFacesZ)484 {485 return (numFacesX*numFacesY + numFacesX*numFacesZ + numFacesY*numFacesZ)*2*6;486 }487 488 489 490 491 void ToEdgeList(vec *outPos) const;492 493 494 495 static int NumVerticesInEdgeList()496 {497 return 4*3*2;498 }499 500 #ifdef MATH_ENABLE_STL_SUPPORT501 502 503 std::string ToString() const;504 std::string SerializeToString() const;505 506 507 std::string SerializeToCodeString() const;508 #endif509 510 static [AABB] FromString(const char *str, const char **outEndStr = 0);511 #ifdef MATH_ENABLE_STL_SUPPORT512 static [AABB] [FromString](const std::string &str) { return [FromString](str.c_str()); }513 #endif514 515 #ifdef MATH_QT_INTEROP516 operator QString() const { return toString(); }517 QString toString() const { return QString::fromStdString(ToString()); }518 #endif519 520 521 522 523 [AABB] Intersection(const [AABB] &aabb) const;524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 bool IntersectLineAABB(const vec &linePos, const vec &lineDir, float &tNear, float &tFar) const;553 554 bool IntersectLineAABB_CPP(const vec &linePos, const vec &lineDir, float &tNear, float &tFar) const;555 #ifdef MATH_SSE556 bool IntersectLineAABB_SSE(const [float4] &linePos, const [float4] &lineDir, float tNear, float tFar) const;557 #endif558 559 #ifdef MATH_OGRE_INTEROP560 [AABB](const Ogre::AxisAlignedBox &other):minPoint(other.getMinimum()), maxPoint(other.getMaximum()) {}561 operator Ogre::AxisAlignedBox() const { return Ogre::AxisAlignedBox(minPoint, maxPoint); }562 #endif563 #ifdef MATH_GRAPHICSENGINE_INTEROP564 void Triangulate(VertexBuffer &vb, int numFacesX, int numFacesY, int numFacesZ, bool ccwIsFrontFacing) const;565 void ToLineList(VertexBuffer &vb) const;566 #endif567 #ifdef MATH_URHO3D_INTEROP568 [AABB](const Urho3D::BoundingBox&other) : minPoint(other.min_), maxPoint(other.max_) {}569 operator Urho3D::BoundingBox() const { return Urho3D::BoundingBox(minPoint, maxPoint); }570 #endif571 572 bool [Equals](const [AABB] &rhs, float [epsilon] = 1[e]-3f) const { return minPoint.Equals(rhs.[minPoint], [epsilon]) && maxPoint.Equals(rhs.[maxPoint], [epsilon]); }573 574 575 576 bool [BitEquals](const [AABB] &other) const { return minPoint.BitEquals(other.[minPoint]) && maxPoint.BitEquals(other.[maxPoint]); }577 };578 579 [OBB] [operator *](const [float3x3] &transform, const [AABB] &aabb);580 [OBB] [operator *](const [float3x4] &transform, const [AABB] &aabb);581 [OBB] [operator *](const [float4x4] &transform, const [AABB] &aabb);582 [OBB] [operator *](const [Quat] &transform, const [AABB] &aabb);583 584 #ifdef MATH_QT_INTEROP585 Q_DECLARE_METATYPE([AABB])586 Q_DECLARE_METATYPE([AABB]*)587 #endif588 589 #ifdef MATH_ENABLE_STL_SUPPORT590 std::ostream &[operator <<](std::ostream &o, const [AABB] &aabb);591 #endif592 593 #ifdef MATH_SIMD594 void AABBTransformAsAABB_SIMD([AABB] &aabb, const [float4x4] &m);595 #endif596 597 [MATH_END_NAMESPACE] Go back to previous page