Mesh.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 System.IO;
  30. using System.Runtime.InteropServices;
  31. using OpenSim.Region.Physics.Manager;
  32. namespace OpenSim.Region.Physics.Meshing
  33. {
  34. public class Mesh : IMesh
  35. {
  36. public List<Vertex> vertices;
  37. public List<Triangle> triangles;
  38. GCHandle pinnedVirtexes;
  39. GCHandle pinnedIndex;
  40. public float[] normals;
  41. public Mesh()
  42. {
  43. vertices = new List<Vertex>();
  44. triangles = new List<Triangle>();
  45. }
  46. public Mesh Clone()
  47. {
  48. Mesh result = new Mesh();
  49. foreach (Vertex v in vertices)
  50. {
  51. if (v == null)
  52. result.vertices.Add(null);
  53. else
  54. result.vertices.Add(v.Clone());
  55. }
  56. foreach (Triangle t in triangles)
  57. {
  58. int iV1, iV2, iV3;
  59. iV1 = vertices.IndexOf(t.v1);
  60. iV2 = vertices.IndexOf(t.v2);
  61. iV3 = vertices.IndexOf(t.v3);
  62. Triangle newT = new Triangle(result.vertices[iV1], result.vertices[iV2], result.vertices[iV3]);
  63. result.Add(newT);
  64. }
  65. return result;
  66. }
  67. public void Add(Triangle triangle)
  68. {
  69. int i;
  70. i = vertices.IndexOf(triangle.v1);
  71. if (i < 0)
  72. throw new ArgumentException("Vertex v1 not known to mesh");
  73. i = vertices.IndexOf(triangle.v2);
  74. if (i < 0)
  75. throw new ArgumentException("Vertex v2 not known to mesh");
  76. i = vertices.IndexOf(triangle.v3);
  77. if (i < 0)
  78. throw new ArgumentException("Vertex v3 not known to mesh");
  79. triangles.Add(triangle);
  80. }
  81. public void Add(Vertex v)
  82. {
  83. vertices.Add(v);
  84. }
  85. public void Remove(Vertex v)
  86. {
  87. int i;
  88. // First, remove all triangles that are build on v
  89. for (i = 0; i < triangles.Count; i++)
  90. {
  91. Triangle t = triangles[i];
  92. if (t.v1 == v || t.v2 == v || t.v3 == v)
  93. {
  94. triangles.RemoveAt(i);
  95. i--;
  96. }
  97. }
  98. // Second remove v itself
  99. vertices.Remove(v);
  100. }
  101. public void RemoveTrianglesOutside(SimpleHull hull)
  102. {
  103. int i;
  104. for (i = 0; i < triangles.Count; i++)
  105. {
  106. Triangle t = triangles[i];
  107. Vertex v1 = t.v1;
  108. Vertex v2 = t.v2;
  109. Vertex v3 = t.v3;
  110. PhysicsVector m = v1 + v2 + v3;
  111. m /= 3.0f;
  112. if (!hull.IsPointIn(new Vertex(m)))
  113. {
  114. triangles.RemoveAt(i);
  115. i--;
  116. }
  117. }
  118. }
  119. public void Add(List<Vertex> lv)
  120. {
  121. foreach (Vertex v in lv)
  122. {
  123. vertices.Add(v);
  124. }
  125. }
  126. public List<PhysicsVector> getVertexList()
  127. {
  128. List<PhysicsVector> result = new List<PhysicsVector>();
  129. foreach (Vertex v in vertices)
  130. {
  131. result.Add(v);
  132. }
  133. return result;
  134. }
  135. public float[] getVertexListAsFloatLocked()
  136. {
  137. float[] result = new float[vertices.Count*3];
  138. for (int i = 0; i < vertices.Count; i++)
  139. {
  140. Vertex v = vertices[i];
  141. if (v == null)
  142. continue;
  143. result[3*i + 0] = v.X;
  144. result[3*i + 1] = v.Y;
  145. result[3*i + 2] = v.Z;
  146. }
  147. pinnedVirtexes = GCHandle.Alloc(result, GCHandleType.Pinned);
  148. return result;
  149. }
  150. public int[] getIndexListAsInt()
  151. {
  152. int[] result = new int[triangles.Count*3];
  153. for (int i = 0; i < triangles.Count; i++)
  154. {
  155. Triangle t = triangles[i];
  156. result[3*i + 0] = vertices.IndexOf(t.v1);
  157. result[3*i + 1] = vertices.IndexOf(t.v2);
  158. result[3*i + 2] = vertices.IndexOf(t.v3);
  159. }
  160. return result;
  161. }
  162. public int[] getIndexListAsIntLocked()
  163. {
  164. int[] result = getIndexListAsInt();
  165. pinnedIndex = GCHandle.Alloc(result, GCHandleType.Pinned);
  166. return result;
  167. }
  168. public void releasePinned()
  169. {
  170. pinnedVirtexes.Free();
  171. pinnedIndex.Free();
  172. }
  173. public void Append(Mesh newMesh)
  174. {
  175. foreach (Vertex v in newMesh.vertices)
  176. vertices.Add(v);
  177. foreach (Triangle t in newMesh.triangles)
  178. Add(t);
  179. }
  180. // Do a linear transformation of mesh.
  181. public void TransformLinear(float[,] matrix, float[] offset)
  182. {
  183. foreach (Vertex v in vertices)
  184. {
  185. if (v == null)
  186. continue;
  187. float x, y, z;
  188. x = v.X*matrix[0, 0] + v.Y*matrix[1, 0] + v.Z*matrix[2, 0];
  189. y = v.X*matrix[0, 1] + v.Y*matrix[1, 1] + v.Z*matrix[2, 1];
  190. z = v.X*matrix[0, 2] + v.Y*matrix[1, 2] + v.Z*matrix[2, 2];
  191. v.X = x + offset[0];
  192. v.Y = y + offset[1];
  193. v.Z = z + offset[2];
  194. }
  195. }
  196. public void DumpRaw(String path, String name, String title)
  197. {
  198. if (path == null)
  199. return;
  200. String fileName = name + "_" + title + ".raw";
  201. String completePath = Path.Combine(path, fileName);
  202. StreamWriter sw = new StreamWriter(completePath);
  203. foreach (Triangle t in triangles)
  204. {
  205. String s = t.ToStringRaw();
  206. sw.WriteLine(s);
  207. }
  208. sw.Close();
  209. }
  210. }
  211. }