Polyhedron.h 3.7 KB

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