Polyhedron.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "Precompiled.h"
  24. #include "Frustum.h"
  25. #include "GeometryUtils.h"
  26. #include "Polyhedron.h"
  27. Polyhedron::~Polyhedron()
  28. {
  29. }
  30. void Polyhedron::Define(const BoundingBox& box)
  31. {
  32. Vector3 vertices[8];
  33. vertices[0] = box.min_;
  34. vertices[1] = Vector3(box.max_.x_, box.min_.y_, box.min_.z_);
  35. vertices[2] = Vector3(box.min_.x_, box.max_.y_, box.min_.z_);
  36. vertices[3] = Vector3(box.max_.x_, box.max_.y_, box.min_.z_);
  37. vertices[4] = Vector3(box.min_.x_, box.min_.y_, box.max_.z_);
  38. vertices[5] = Vector3(box.max_.x_, box.min_.y_, box.max_.z_);
  39. vertices[6] = Vector3(box.min_.x_, box.max_.y_, box.max_.z_);
  40. vertices[7] = box.max_;
  41. faces_.Resize(6);
  42. SetFace(0, vertices[3], vertices[7], vertices[5], vertices[1]);
  43. SetFace(1, vertices[6], vertices[2], vertices[0], vertices[4]);
  44. SetFace(2, vertices[6], vertices[7], vertices[3], vertices[2]);
  45. SetFace(3, vertices[1], vertices[5], vertices[4], vertices[0]);
  46. SetFace(4, vertices[7], vertices[6], vertices[4], vertices[5]);
  47. SetFace(5, vertices[2], vertices[3], vertices[1], vertices[0]);
  48. }
  49. void Polyhedron::Define(const Frustum& frustum)
  50. {
  51. const Vector3* vertices = frustum.vertices_;
  52. faces_.Resize(6);
  53. SetFace(0, vertices[0], vertices[4], vertices[5], vertices[1]);
  54. SetFace(1, vertices[7], vertices[3], vertices[2], vertices[6]);
  55. SetFace(2, vertices[7], vertices[4], vertices[0], vertices[3]);
  56. SetFace(3, vertices[1], vertices[5], vertices[6], vertices[2]);
  57. SetFace(4, vertices[4], vertices[7], vertices[6], vertices[5]);
  58. SetFace(5, vertices[3], vertices[0], vertices[1], vertices[2]);
  59. }
  60. void Polyhedron::AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  61. {
  62. faces_.Resize(faces_.Size() + 1);
  63. PODVector<Vector3>& face = faces_[faces_.Size() - 1];
  64. face.Resize(3);
  65. face[0] = v0;
  66. face[1] = v1;
  67. face[2] = v2;
  68. }
  69. void Polyhedron::AddFace(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3)
  70. {
  71. faces_.Resize(faces_.Size() + 1);
  72. PODVector<Vector3>& face = faces_[faces_.Size() - 1];
  73. face.Resize(4);
  74. face[0] = v0;
  75. face[1] = v1;
  76. face[2] = v2;
  77. face[3] = v3;
  78. }
  79. void Polyhedron::AddFace(const PODVector<Vector3>& face)
  80. {
  81. faces_.Push(face);
  82. }
  83. void Polyhedron::Clip(const Plane& plane)
  84. {
  85. unsigned totalVertices = 0;
  86. unsigned totalClippedVertices = 0;
  87. for (unsigned i = 0; i < faces_.Size(); ++i)
  88. totalVertices += faces_[i].Size();
  89. // Clipping may produce max. 1 vertex more for each face
  90. clippedVertices_.Resize(totalVertices + faces_.Size());
  91. for (unsigned i = 0; i < faces_.Size(); ++i)
  92. {
  93. PODVector<Vector3>& face = faces_[i];
  94. if (face.Empty())
  95. continue;
  96. outFace_.Resize(face.Size() + 2);
  97. unsigned outClippedVertices = 0;
  98. unsigned outVertices = ClipPolygon((float*)&face[0], (float*)&outFace_[0], face.Size(), sizeof(Vector3), plane,
  99. (float*)&clippedVertices_[totalClippedVertices], &outClippedVertices);
  100. totalClippedVertices += outClippedVertices;
  101. outFace_.Resize(outVertices);
  102. }
  103. clippedVertices_.Resize(totalClippedVertices);
  104. // Remove empty faces
  105. for (unsigned i = faces_.Size() - 1; i < faces_.Size(); --i)
  106. {
  107. if (faces_[i].Empty())
  108. faces_.Erase(i);
  109. }
  110. // Create a new face from the clipped vertices. First remove duplicates
  111. for (unsigned i = 0; i < clippedVertices_.Size(); ++i)
  112. {
  113. for (unsigned j = clippedVertices_.Size() - 1; j > i; --j)
  114. {
  115. if (clippedVertices_[j].Equals(clippedVertices_[i]))
  116. clippedVertices_.Erase(j);
  117. }
  118. }
  119. if (clippedVertices_.Size() > 3)
  120. {
  121. outFace_.Clear();
  122. // Start with the first vertex
  123. outFace_.Push(clippedVertices_.Front());
  124. clippedVertices_.Erase(0);
  125. while (!clippedVertices_.Empty())
  126. {
  127. // Then add the vertex which is closest to the last added
  128. const Vector3& lastAdded = outFace_.Back();
  129. float bestDistance = M_INFINITY;
  130. unsigned bestIndex = 0;
  131. for (unsigned i = 0; i < clippedVertices_.Size(); ++i)
  132. {
  133. float distance = (clippedVertices_[i] - lastAdded).LengthSquared();
  134. if (distance < bestDistance)
  135. {
  136. bestDistance = distance;
  137. bestIndex = i;
  138. }
  139. }
  140. outFace_.Push(clippedVertices_[bestIndex]);
  141. clippedVertices_.Erase(bestIndex);
  142. }
  143. faces_.Push(outFace_);
  144. }
  145. }
  146. void Polyhedron::Clip(const Frustum& frustum)
  147. {
  148. for (unsigned i = 0; i < NUM_FRUSTUM_PLANES; ++i)
  149. Clip(frustum.planes_[i]);
  150. }
  151. void Polyhedron::Clip(const BoundingBox& box)
  152. {
  153. Vector3 vertices[8];
  154. vertices[0] = box.min_;
  155. vertices[1] = Vector3(box.max_.x_, box.min_.y_, box.min_.z_);
  156. vertices[2] = Vector3(box.min_.x_, box.max_.y_, box.min_.z_);
  157. vertices[3] = Vector3(box.max_.x_, box.max_.y_, box.min_.z_);
  158. vertices[4] = Vector3(box.min_.x_, box.min_.y_, box.max_.z_);
  159. vertices[5] = Vector3(box.max_.x_, box.min_.y_, box.max_.z_);
  160. vertices[6] = Vector3(box.min_.x_, box.max_.y_, box.max_.z_);
  161. vertices[7] = box.max_;
  162. Clip(Plane(vertices[5], vertices[7], vertices[3]));
  163. Clip(Plane(vertices[0], vertices[2], vertices[6]));
  164. Clip(Plane(vertices[3], vertices[7], vertices[6]));
  165. Clip(Plane(vertices[4], vertices[5], vertices[1]));
  166. Clip(Plane(vertices[4], vertices[6], vertices[7]));
  167. Clip(Plane(vertices[1], vertices[3], vertices[2]));
  168. }
  169. void Polyhedron::Clear()
  170. {
  171. faces_.Clear();
  172. }
  173. void Polyhedron::Transform(const Matrix3& transform)
  174. {
  175. for (unsigned i = 0; i < faces_.Size(); ++i)
  176. {
  177. PODVector<Vector3>& face = faces_[i];
  178. for (unsigned j = 0; j < face.Size(); ++j)
  179. face[j] = transform * face[j];
  180. }
  181. }
  182. void Polyhedron::Transform(const Matrix3x4& transform)
  183. {
  184. for (unsigned i = 0; i < faces_.Size(); ++i)
  185. {
  186. PODVector<Vector3>& face = faces_[i];
  187. for (unsigned j = 0; j < face.Size(); ++j)
  188. face[j] = transform * face[j];
  189. }
  190. }
  191. Polyhedron Polyhedron::Transformed(const Matrix3& transform) const
  192. {
  193. Polyhedron ret;
  194. ret.faces_.Resize(faces_.Size());
  195. for (unsigned i = 0; i < faces_.Size(); ++i)
  196. {
  197. const PODVector<Vector3>& face = faces_[i];
  198. PODVector<Vector3>& newFace = ret.faces_[i];
  199. newFace.Resize(face.Size());
  200. for (unsigned j = 0; j < face.Size(); ++j)
  201. newFace[j] = transform * face[j];
  202. }
  203. return ret;
  204. }
  205. Polyhedron Polyhedron::Transformed(const Matrix3x4& transform) const
  206. {
  207. Polyhedron ret;
  208. ret.faces_.Resize(faces_.Size());
  209. for (unsigned i = 0; i < faces_.Size(); ++i)
  210. {
  211. const PODVector<Vector3>& face = faces_[i];
  212. PODVector<Vector3>& newFace = ret.faces_[i];
  213. newFace.Resize(face.Size());
  214. for (unsigned j = 0; j < face.Size(); ++j)
  215. newFace[j] = transform * face[j];
  216. }
  217. return ret;
  218. }
  219. void Polyhedron::SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2)
  220. {
  221. PODVector<Vector3>& face = faces_[index];
  222. face.Resize(3);
  223. face[0] = v0;
  224. face[1] = v1;
  225. face[2] = v2;
  226. }
  227. void Polyhedron::SetFace(unsigned index, const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3)
  228. {
  229. PODVector<Vector3>& face = faces_[index];
  230. face.Resize(4);
  231. face[0] = v0;
  232. face[1] = v1;
  233. face[2] = v2;
  234. face[3] = v3;
  235. }