Mesh.cs 7.3 KB

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