SimpleHull.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework.Console;
  31. using OpenSim.Region.Physics.Manager;
  32. namespace OpenSim.Region.Physics.Meshing
  33. {
  34. // A simple hull is a set of vertices building up to simplices that border a region
  35. // The word simple referes to the fact, that this class assumes, that all simplices
  36. // do not intersect
  37. // Simple hulls can be added and subtracted.
  38. // Vertices can be checked to lie inside a hull
  39. // Also note, that the sequence of the vertices is important and defines if the region that
  40. // is defined by the hull lies inside or outside the simplex chain
  41. public class SimpleHull
  42. {
  43. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  44. private List<Vertex> vertices = new List<Vertex>();
  45. private List<Vertex> holeVertices = new List<Vertex>(); // Only used, when the hull is hollow
  46. // Adds a vertex to the end of the list
  47. public void AddVertex(Vertex v)
  48. {
  49. vertices.Add(v);
  50. }
  51. public override String ToString()
  52. {
  53. String result = String.Empty;
  54. foreach (Vertex v in vertices)
  55. {
  56. result += "b:" + v.ToString() + "\n";
  57. }
  58. return result;
  59. }
  60. public List<Vertex> getVertices()
  61. {
  62. List<Vertex> newVertices = new List<Vertex>();
  63. newVertices.AddRange(vertices);
  64. newVertices.Add(null);
  65. newVertices.AddRange(holeVertices);
  66. return newVertices;
  67. }
  68. public SimpleHull Clone()
  69. {
  70. SimpleHull result = new SimpleHull();
  71. foreach (Vertex v in vertices)
  72. {
  73. result.AddVertex(v.Clone());
  74. }
  75. foreach (Vertex v in holeVertices)
  76. {
  77. result.holeVertices.Add(v.Clone());
  78. }
  79. return result;
  80. }
  81. public bool IsPointIn(Vertex v1)
  82. {
  83. int iCounter = 0;
  84. List<Simplex> simplices = buildSimplexList();
  85. foreach (Simplex s in simplices)
  86. {
  87. // Send a ray along the positive X-Direction
  88. // Note, that this direction must correlate with the "below" interpretation
  89. // of handling for the special cases below
  90. PhysicsVector intersection = s.RayIntersect(v1, new PhysicsVector(1.0f, 0.0f, 0.0f), true);
  91. if (intersection == null)
  92. continue; // No intersection. Done. More tests to follow otherwise
  93. // Did we hit the end of a simplex?
  94. // Then this can be one of two special cases:
  95. // 1. we go through a border exactly at a joint
  96. // 2. we have just marginally touched a corner
  97. // 3. we can slide along a border
  98. // Solution: If the other vertex is "below" the ray, we don't count it
  99. // Thus corners pointing down are counted twice, corners pointing up are not counted
  100. // borders are counted once
  101. if (intersection.IsIdentical(s.v1, 0.001f))
  102. {
  103. if (s.v2.Y < v1.Y)
  104. continue;
  105. }
  106. // Do this for the other vertex two
  107. if (intersection.IsIdentical(s.v2, 0.001f))
  108. {
  109. if (s.v1.Y < v1.Y)
  110. continue;
  111. }
  112. iCounter++;
  113. }
  114. return iCounter%2 == 1; // Point is inside if the number of intersections is odd
  115. }
  116. public bool containsPointsFrom(SimpleHull otherHull)
  117. {
  118. foreach (Vertex v in otherHull.vertices)
  119. {
  120. if (IsPointIn(v))
  121. return true;
  122. }
  123. return false;
  124. }
  125. private List<Simplex> buildSimplexList()
  126. {
  127. List<Simplex> result = new List<Simplex>();
  128. // Not asserted but assumed: at least three vertices
  129. for (int i = 0; i < vertices.Count - 1; i++)
  130. {
  131. Simplex s = new Simplex(vertices[i], vertices[i + 1]);
  132. result.Add(s);
  133. }
  134. Simplex s1 = new Simplex(vertices[vertices.Count - 1], vertices[0]);
  135. result.Add(s1);
  136. if (holeVertices.Count == 0)
  137. return result;
  138. // Same here. At least three vertices in hole assumed
  139. for (int i = 0; i < holeVertices.Count - 1; i++)
  140. {
  141. Simplex s = new Simplex(holeVertices[i], holeVertices[i + 1]);
  142. result.Add(s);
  143. }
  144. s1 = new Simplex(holeVertices[holeVertices.Count - 1], holeVertices[0]);
  145. result.Add(s1);
  146. return result;
  147. }
  148. private bool InsertVertex(Vertex v, int iAfter)
  149. {
  150. vertices.Insert(iAfter + 1, v);
  151. return true;
  152. }
  153. private Vertex getNextVertex(Vertex currentVertex)
  154. {
  155. int iCurrentIndex;
  156. iCurrentIndex = vertices.IndexOf(currentVertex);
  157. // Error handling for iCurrentIndex==-1 should go here (and probably never will)
  158. iCurrentIndex++;
  159. if (iCurrentIndex == vertices.Count)
  160. iCurrentIndex = 0;
  161. return vertices[iCurrentIndex];
  162. }
  163. public Vertex FindVertex(Vertex vBase, float tolerance)
  164. {
  165. foreach (Vertex v in vertices)
  166. {
  167. if (v.IsIdentical(vBase, tolerance))
  168. return v;
  169. }
  170. return null;
  171. }
  172. public void FindIntersection(Simplex s, ref Vertex Intersection, ref Vertex nextVertex)
  173. {
  174. Vertex bestIntersection = null;
  175. float distToV1 = Single.PositiveInfinity;
  176. Simplex bestIntersectingSimplex = null;
  177. List<Simplex> simple = buildSimplexList();
  178. foreach (Simplex sTest in simple)
  179. {
  180. PhysicsVector vvTemp = Simplex.Intersect(sTest, s, -.001f, -.001f, 0.999f, .999f);
  181. Vertex vTemp = null;
  182. if (vvTemp != null)
  183. vTemp = new Vertex(vvTemp);
  184. if (vTemp != null)
  185. {
  186. PhysicsVector diff = (s.v1 - vTemp);
  187. float distTemp = diff.length();
  188. if (bestIntersection == null || distTemp < distToV1)
  189. {
  190. bestIntersection = vTemp;
  191. distToV1 = distTemp;
  192. bestIntersectingSimplex = sTest;
  193. }
  194. } // end if vTemp
  195. } // end foreach
  196. Intersection = bestIntersection;
  197. if (bestIntersectingSimplex != null)
  198. nextVertex = bestIntersectingSimplex.v2;
  199. else
  200. nextVertex = null;
  201. }
  202. public static SimpleHull SubtractHull(SimpleHull baseHull, SimpleHull otherHull)
  203. {
  204. SimpleHull baseHullClone = baseHull.Clone();
  205. SimpleHull otherHullClone = otherHull.Clone();
  206. bool intersects = false;
  207. //m_log.Debug("State before intersection detection");
  208. //m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
  209. //m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
  210. {
  211. int iBase, iOther;
  212. // Insert into baseHull
  213. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  214. {
  215. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  216. Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
  217. for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
  218. {
  219. int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
  220. Simplex sOther =
  221. new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
  222. PhysicsVector intersect = Simplex.Intersect(sBase, sOther, 0.001f, -.001f, 0.999f, 1.001f);
  223. if (intersect != null)
  224. {
  225. Vertex vIntersect = new Vertex(intersect);
  226. baseHullClone.vertices.Insert(iBase + 1, vIntersect);
  227. sBase.v2 = vIntersect;
  228. intersects = true;
  229. }
  230. }
  231. }
  232. }
  233. //m_log.Debug("State after intersection detection for the base hull");
  234. //m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
  235. {
  236. int iOther, iBase;
  237. // Insert into otherHull
  238. for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
  239. {
  240. int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
  241. Simplex sOther = new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
  242. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  243. {
  244. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  245. Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
  246. PhysicsVector intersect = Simplex.Intersect(sBase, sOther, -.001f, 0.001f, 1.001f, 0.999f);
  247. if (intersect != null)
  248. {
  249. Vertex vIntersect = new Vertex(intersect);
  250. otherHullClone.vertices.Insert(iOther + 1, vIntersect);
  251. sOther.v2 = vIntersect;
  252. intersects = true;
  253. }
  254. }
  255. }
  256. }
  257. //m_log.Debug("State after intersection detection for the base hull");
  258. //m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
  259. bool otherIsInBase = baseHullClone.containsPointsFrom(otherHullClone);
  260. if (!intersects && otherIsInBase)
  261. {
  262. // We have a hole here
  263. baseHullClone.holeVertices = otherHullClone.vertices;
  264. return baseHullClone;
  265. }
  266. SimpleHull result = new SimpleHull();
  267. // Find a good starting Simplex from baseHull
  268. // A good starting simplex is one that is outside otherHull
  269. // Such a simplex must exist, otherwise the result will be empty
  270. Vertex baseStartVertex = null;
  271. {
  272. int iBase;
  273. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  274. {
  275. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  276. Vertex center = new Vertex((baseHullClone.vertices[iBase] + baseHullClone.vertices[iBaseNext])/2.0f);
  277. bool isOutside = !otherHullClone.IsPointIn(center);
  278. if (isOutside)
  279. {
  280. baseStartVertex = baseHullClone.vertices[iBaseNext];
  281. break;
  282. }
  283. }
  284. }
  285. if (baseStartVertex == null) // i.e. no simplex fulfilled the "outside" condition.
  286. // In otherwords, subtractHull completely embraces baseHull
  287. {
  288. return result;
  289. }
  290. // The simplex that *starts* with baseStartVertex is outside the cutting hull,
  291. // so we can start our walk with the next vertex without loosing a branch
  292. Vertex V1 = baseStartVertex;
  293. bool onBase = true;
  294. // And here is how we do the magic :-)
  295. // Start on the base hull.
  296. // Walk the vertices in the positive direction
  297. // For each vertex check, whether it is a vertex shared with the other hull
  298. // if this is the case, switch over to walking the other vertex list.
  299. // Note: The other hull *must* go backwards to our starting point (via several orther vertices)
  300. // Thus it is important that the cutting hull has the inverse directional sense than the
  301. // base hull!!!!!!!!! (means if base goes CW around it's center cutting hull must go CCW)
  302. bool done = false;
  303. while (!done)
  304. {
  305. result.AddVertex(V1);
  306. Vertex nextVertex = null;
  307. if (onBase)
  308. {
  309. nextVertex = otherHullClone.FindVertex(V1, 0.001f);
  310. }
  311. else
  312. {
  313. nextVertex = baseHullClone.FindVertex(V1, 0.001f);
  314. }
  315. if (nextVertex != null) // A node that represents an intersection
  316. {
  317. V1 = nextVertex; // Needed to find the next vertex on the other hull
  318. onBase = !onBase;
  319. }
  320. if (onBase)
  321. V1 = baseHullClone.getNextVertex(V1);
  322. else
  323. V1 = otherHullClone.getNextVertex(V1);
  324. if (V1 == baseStartVertex)
  325. done = true;
  326. }
  327. //m_log.DebugFormat("The resulting Hull is:\n{1}", 0, result.ToString());
  328. return result;
  329. }
  330. }
  331. }