Polyhedron.cpp 9.9 KB

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