Mesh.cs 7.3 KB

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