Polyhedron.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2014 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 "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()
  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. /// Define from a bounding box.
  62. void Define(const BoundingBox& box);
  63. /// Define from a frustum.
  64. void Define(const Frustum& frustum);
  65. /// Add a triangle face.
  66. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2);
  67. /// Add a quadrilateral face.
  68. void AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  69. /// Add an arbitrary face.
  70. void AddFace(const PODVector<Vector3>& face);
  71. /// Clip with a plane.
  72. void Clip(const Plane& plane);
  73. /// Clip with a bounding box.
  74. void Clip(const BoundingBox& box);
  75. /// Clip with a frustum.
  76. void Clip(const Frustum& box);
  77. /// Clear all faces.
  78. void Clear();
  79. /// Transform with a 3x3 matrix.
  80. void Transform(const Matrix3& transform);
  81. /// Transform with a 3x4 matrix.
  82. void Transform(const Matrix3x4& transform);
  83. /// Return transformed with a 3x3 matrix.
  84. Polyhedron Transformed(const Matrix3& transform) const;
  85. /// Return transformed with a 3x4 matrix.
  86. Polyhedron Transformed(const Matrix3x4& transform) const;
  87. /// Return whether is empty.
  88. bool Empty() const { return faces_.Empty(); }
  89. /// Polygon faces.
  90. Vector<PODVector<Vector3> > faces_;
  91. private:
  92. /// Set a triangle face by index.
  93. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2);
  94. /// Set a quadrilateral face by index.
  95. void SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
  96. /// Internal vector for clipped vertices.
  97. PODVector<Vector3> clippedVertices_;
  98. /// Internal vector for the new face being constructed.
  99. PODVector<Vector3> outFace_;
  100. };
  101. }