Polyhedron.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Vector3.h"
  5. namespace Urho3D
  6. {
  7. class BoundingBox;
  8. class Frustum;
  9. class Matrix3;
  10. class Matrix3x4;
  11. class Plane;
  12. /// A convex volume built from polygon faces.
  13. class URHO3D_API Polyhedron
  14. {
  15. public:
  16. /// Construct empty.
  17. Polyhedron() noexcept = default;
  18. /// Destruct.
  19. ~Polyhedron() noexcept = default;
  20. /// Copy-construct from another polyhedron.
  21. Polyhedron(const Polyhedron& polyhedron) :
  22. faces_(polyhedron.faces_)
  23. {
  24. }
  25. /// Construct from a list of faces.
  26. explicit Polyhedron(const Vector<Vector<Vector3>>& faces) :
  27. faces_(faces)
  28. {
  29. }
  30. /// Construct from a bounding box.
  31. explicit Polyhedron(const BoundingBox& box)
  32. {
  33. Define(box);
  34. }
  35. /// Construct from a frustum.
  36. explicit Polyhedron(const Frustum& frustum)
  37. {
  38. Define(frustum);
  39. }
  40. /// Assign from another polyhedron.
  41. Polyhedron& operator =(const Polyhedron& rhs)
  42. {
  43. faces_ = rhs.faces_;
  44. return *this;
  45. }
  46. /// Define from a bounding box.
  47. void Define(const BoundingBox& box);
  48. /// Define from a frustum.
  49. void Define(const Frustum& frustum);
  50. /// Add a triangle face.
  51. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2);
  52. /// Add a quadrilateral face.
  53. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  54. /// Add an arbitrary face.
  55. void AddFace(const Vector<Vector3>& face);
  56. /// Clip with a plane.
  57. void Clip(const Plane& plane);
  58. /// Clip with a bounding box.
  59. void Clip(const BoundingBox& box);
  60. /// Clip with a frustum.
  61. void Clip(const Frustum& frustum);
  62. /// Clear all faces.
  63. void Clear();
  64. /// Transform with a 3x3 matrix.
  65. void Transform(const Matrix3& transform);
  66. /// Transform with a 3x4 matrix.
  67. void Transform(const Matrix3x4& transform);
  68. /// Return transformed with a 3x3 matrix.
  69. Polyhedron Transformed(const Matrix3& transform) const;
  70. /// Return transformed with a 3x4 matrix.
  71. Polyhedron Transformed(const Matrix3x4& transform) const;
  72. /// Return whether is empty.
  73. bool Empty() const { return faces_.Empty(); }
  74. /// Polygon faces.
  75. Vector<Vector<Vector3>> faces_;
  76. private:
  77. /// Set a triangle face by index.
  78. void SetFace(i32 index, const Vector3& v0, const Vector3& v1, const Vector3& v2);
  79. /// Set a quadrilateral face by index.
  80. void SetFace(i32 index, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  81. /// Internal vector for clipped vertices.
  82. Vector<Vector3> clippedVertices_;
  83. /// Internal vector for the new face being constructed.
  84. Vector<Vector3> outFace_;
  85. };
  86. }