Polyhedron.cpp 9.4 KB

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