Polyhedron.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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 ATOMIC_API Polyhedron
  33. {
  34. public:
  35. /// Construct empty.
  36. Polyhedron()
  37. {
  38. }
  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. Polyhedron(const Vector<PODVector<Vector3> >& faces) :
  46. faces_(faces)
  47. {
  48. }
  49. /// Construct from a bounding box.
  50. Polyhedron(const BoundingBox& box)
  51. {
  52. Define(box);
  53. }
  54. /// Construct from a frustum.
  55. Polyhedron(const Frustum& frustum)
  56. {
  57. Define(frustum);
  58. }
  59. /// Destruct.
  60. ~Polyhedron();
  61. /// Assign from another polyhedron.
  62. Polyhedron& operator =(const Polyhedron& rhs)
  63. {
  64. faces_ = rhs.faces_;
  65. return *this;
  66. }
  67. /// Define from a bounding box.
  68. void Define(const BoundingBox& box);
  69. /// Define from a frustum.
  70. void Define(const Frustum& frustum);
  71. /// Add a triangle face.
  72. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2);
  73. /// Add a quadrilateral face.
  74. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  75. /// Add an arbitrary face.
  76. void AddFace(const PODVector<Vector3>& face);
  77. /// Clip with a plane.
  78. void Clip(const Plane& plane);
  79. /// Clip with a bounding box.
  80. void Clip(const BoundingBox& box);
  81. /// Clip with a frustum.
  82. void Clip(const Frustum& box);
  83. /// Clear all faces.
  84. void Clear();
  85. /// Transform with a 3x3 matrix.
  86. void Transform(const Matrix3& transform);
  87. /// Transform with a 3x4 matrix.
  88. void Transform(const Matrix3x4& transform);
  89. /// Return transformed with a 3x3 matrix.
  90. Polyhedron Transformed(const Matrix3& transform) const;
  91. /// Return transformed with a 3x4 matrix.
  92. Polyhedron Transformed(const Matrix3x4& transform) const;
  93. /// Return whether is empty.
  94. bool Empty() const { return faces_.Empty(); }
  95. /// Polygon faces.
  96. Vector<PODVector<Vector3> > faces_;
  97. private:
  98. /// Set a triangle face by index.
  99. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2);
  100. /// Set a quadrilateral face by index.
  101. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  102. /// Internal vector for clipped vertices.
  103. PODVector<Vector3> clippedVertices_;
  104. /// Internal vector for the new face being constructed.
  105. PODVector<Vector3> outFace_;
  106. };
  107. }