Polyhedron.cpp 10.0 KB

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