SimpleHull.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. using System;
  28. using System.Collections.Generic;
  29. using OpenSim.Framework.Console;
  30. using OpenSim.Region.Physics.Manager;
  31. namespace OpenSim.Region.Physics.Meshing
  32. {
  33. // A simple hull is a set of vertices building up to simplices that border a region
  34. // The word simple referes to the fact, that this class assumes, that all simplices
  35. // do not intersect
  36. // Simple hulls can be added and subtracted.
  37. // Vertices can be checked to lie inside a hull
  38. // Also note, that the sequence of the vertices is important and defines if the region that
  39. // is defined by the hull lies inside or outside the simplex chain
  40. public class SimpleHull
  41. {
  42. //private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. private List<Vertex> vertices = new List<Vertex>();
  44. private List<Vertex> holeVertices = new List<Vertex>(); // Only used, when the hull is hollow
  45. // Adds a vertex to the end of the list
  46. public void AddVertex(Vertex v)
  47. {
  48. vertices.Add(v);
  49. }
  50. public override String ToString()
  51. {
  52. String result = String.Empty;
  53. foreach (Vertex v in vertices)
  54. {
  55. result += "b:" + v.ToString() + "\n";
  56. }
  57. return result;
  58. }
  59. public List<Vertex> getVertices()
  60. {
  61. List<Vertex> newVertices = new List<Vertex>();
  62. newVertices.AddRange(vertices);
  63. newVertices.Add(null);
  64. newVertices.AddRange(holeVertices);
  65. return newVertices;
  66. }
  67. public SimpleHull Clone()
  68. {
  69. SimpleHull result = new SimpleHull();
  70. foreach (Vertex v in vertices)
  71. {
  72. result.AddVertex(v.Clone());
  73. }
  74. foreach (Vertex v in holeVertices)
  75. {
  76. result.holeVertices.Add(v.Clone());
  77. }
  78. return result;
  79. }
  80. public bool IsPointIn(Vertex v1)
  81. {
  82. int iCounter = 0;
  83. List<Simplex> simplices = buildSimplexList();
  84. foreach (Simplex s in simplices)
  85. {
  86. // Send a ray along the positive X-Direction
  87. // Note, that this direction must correlate with the "below" interpretation
  88. // of handling for the special cases below
  89. PhysicsVector intersection = s.RayIntersect(v1, new PhysicsVector(1.0f, 0.0f, 0.0f), true);
  90. if (intersection == null)
  91. continue; // No intersection. Done. More tests to follow otherwise
  92. // Did we hit the end of a simplex?
  93. // Then this can be one of two special cases:
  94. // 1. we go through a border exactly at a joint
  95. // 2. we have just marginally touched a corner
  96. // 3. we can slide along a border
  97. // Solution: If the other vertex is "below" the ray, we don't count it
  98. // Thus corners pointing down are counted twice, corners pointing up are not counted
  99. // borders are counted once
  100. if (intersection.IsIdentical(s.v1, 0.001f))
  101. {
  102. if (s.v2.Y < v1.Y)
  103. continue;
  104. }
  105. // Do this for the other vertex two
  106. if (intersection.IsIdentical(s.v2, 0.001f))
  107. {
  108. if (s.v1.Y < v1.Y)
  109. continue;
  110. }
  111. iCounter++;
  112. }
  113. return iCounter%2 == 1; // Point is inside if the number of intersections is odd
  114. }
  115. public bool containsPointsFrom(SimpleHull otherHull)
  116. {
  117. foreach (Vertex v in otherHull.vertices)
  118. {
  119. if (IsPointIn(v))
  120. return true;
  121. }
  122. return false;
  123. }
  124. private List<Simplex> buildSimplexList()
  125. {
  126. List<Simplex> result = new List<Simplex>();
  127. // Not asserted but assumed: at least three vertices
  128. for (int i = 0; i < vertices.Count - 1; i++)
  129. {
  130. Simplex s = new Simplex(vertices[i], vertices[i + 1]);
  131. result.Add(s);
  132. }
  133. Simplex s1 = new Simplex(vertices[vertices.Count - 1], vertices[0]);
  134. result.Add(s1);
  135. if (holeVertices.Count == 0)
  136. return result;
  137. // Same here. At least three vertices in hole assumed
  138. for (int i = 0; i < holeVertices.Count - 1; i++)
  139. {
  140. Simplex s = new Simplex(holeVertices[i], holeVertices[i + 1]);
  141. result.Add(s);
  142. }
  143. s1 = new Simplex(holeVertices[holeVertices.Count - 1], holeVertices[0]);
  144. result.Add(s1);
  145. return result;
  146. }
  147. // TODO: unused
  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. }
  195. }
  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. }