Polyhedron.cpp 10 KB

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