Polyhedron.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Vector3.h"
  24. namespace Urho3D
  25. {
  26. class BoundingBox;
  27. class Frustum;
  28. class Matrix3;
  29. class Matrix3x4;
  30. class Plane;
  31. /// A convex volume built from polygon faces.
  32. class URHO3D_API Polyhedron
  33. {
  34. public:
  35. /// Construct empty.
  36. Polyhedron() noexcept = default;
  37. /// Destruct.
  38. ~Polyhedron() noexcept = default;
  39. /// Copy-construct from another polyhedron.
  40. Polyhedron(const Polyhedron& polyhedron) :
  41. faces_(polyhedron.faces_)
  42. {
  43. }
  44. /// Construct from a list of faces.
  45. explicit Polyhedron(const Vector<PODVector<Vector3> >& faces) :
  46. faces_(faces)
  47. {
  48. }
  49. /// Construct from a bounding box.
  50. explicit Polyhedron(const BoundingBox& box)
  51. {
  52. Define(box);
  53. }
  54. /// Construct from a frustum.
  55. explicit Polyhedron(const Frustum& frustum)
  56. {
  57. Define(frustum);
  58. }
  59. /// Assign from another polyhedron.
  60. Polyhedron& operator =(const Polyhedron& rhs)
  61. {
  62. faces_ = rhs.faces_;
  63. return *this;
  64. }
  65. /// Define from a bounding box.
  66. void Define(const BoundingBox& box);
  67. /// Define from a frustum.
  68. void Define(const Frustum& frustum);
  69. /// Add a triangle face.
  70. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2);
  71. /// Add a quadrilateral face.
  72. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  73. /// Add an arbitrary face.
  74. void AddFace(const PODVector<Vector3>& face);
  75. /// Clip with a plane.
  76. void Clip(const Plane& plane);
  77. /// Clip with a bounding box.
  78. void Clip(const BoundingBox& box);
  79. /// Clip with a frustum.
  80. void Clip(const Frustum& frustum);
  81. /// Clear all faces.
  82. void Clear();
  83. /// Transform with a 3x3 matrix.
  84. void Transform(const Matrix3& transform);
  85. /// Transform with a 3x4 matrix.
  86. void Transform(const Matrix3x4& transform);
  87. /// Return transformed with a 3x3 matrix.
  88. Polyhedron Transformed(const Matrix3& transform) const;
  89. /// Return transformed with a 3x4 matrix.
  90. Polyhedron Transformed(const Matrix3x4& transform) const;
  91. /// Return whether is empty.
  92. bool Empty() const { return faces_.Empty(); }
  93. /// Polygon faces.
  94. Vector<PODVector<Vector3> > faces_;
  95. private:
  96. /// Set a triangle face by index.
  97. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2);
  98. /// Set a quadrilateral face by index.
  99. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  100. /// Internal vector for clipped vertices.
  101. PODVector<Vector3> clippedVertices_;
  102. /// Internal vector for the new face being constructed.
  103. PODVector<Vector3> outFace_;
  104. };
  105. }