Mesh.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. using PrimMesher;
  33. namespace OpenSim.Region.Physics.Meshing
  34. {
  35. public class Mesh : IMesh
  36. {
  37. public List<Vertex> vertices;
  38. public List<Triangle> triangles;
  39. GCHandle pinnedVirtexes;
  40. GCHandle pinnedIndex;
  41. public PrimMesh primMesh = null;
  42. public float[] normals;
  43. public Mesh()
  44. {
  45. vertices = new List<Vertex>();
  46. triangles = new List<Triangle>();
  47. }
  48. public Mesh Clone()
  49. {
  50. Mesh result = new Mesh();
  51. foreach (Vertex v in vertices)
  52. {
  53. if (v == null)
  54. result.vertices.Add(null);
  55. else
  56. result.vertices.Add(v.Clone());
  57. }
  58. foreach (Triangle t in triangles)
  59. {
  60. int iV1, iV2, iV3;
  61. iV1 = vertices.IndexOf(t.v1);
  62. iV2 = vertices.IndexOf(t.v2);
  63. iV3 = vertices.IndexOf(t.v3);
  64. Triangle newT = new Triangle(result.vertices[iV1], result.vertices[iV2], result.vertices[iV3]);
  65. result.Add(newT);
  66. }
  67. return result;
  68. }
  69. public void Add(Triangle triangle)
  70. {
  71. int i;
  72. i = vertices.IndexOf(triangle.v1);
  73. if (i < 0)
  74. throw new ArgumentException("Vertex v1 not known to mesh");
  75. i = vertices.IndexOf(triangle.v2);
  76. if (i < 0)
  77. throw new ArgumentException("Vertex v2 not known to mesh");
  78. i = vertices.IndexOf(triangle.v3);
  79. if (i < 0)
  80. throw new ArgumentException("Vertex v3 not known to mesh");
  81. triangles.Add(triangle);
  82. }
  83. public void Add(Vertex v)
  84. {
  85. vertices.Add(v);
  86. }
  87. public void Remove(Vertex v)
  88. {
  89. int i;
  90. // First, remove all triangles that are build on v
  91. for (i = 0; i < triangles.Count; i++)
  92. {
  93. Triangle t = triangles[i];
  94. if (t.v1 == v || t.v2 == v || t.v3 == v)
  95. {
  96. triangles.RemoveAt(i);
  97. i--;
  98. }
  99. }
  100. // Second remove v itself
  101. vertices.Remove(v);
  102. }
  103. public void Add(List<Vertex> lv)
  104. {
  105. foreach (Vertex v in lv)
  106. {
  107. vertices.Add(v);
  108. }
  109. }
  110. public void CalcNormals()
  111. {
  112. int iTriangles = triangles.Count;
  113. this.normals = new float[iTriangles * 3];
  114. int i = 0;
  115. foreach (Triangle t in triangles)
  116. {
  117. float ux, uy, uz;
  118. float vx, vy, vz;
  119. float wx, wy, wz;
  120. ux = t.v1.X;
  121. uy = t.v1.Y;
  122. uz = t.v1.Z;
  123. vx = t.v2.X;
  124. vy = t.v2.Y;
  125. vz = t.v2.Z;
  126. wx = t.v3.X;
  127. wy = t.v3.Y;
  128. wz = t.v3.Z;
  129. // Vectors for edges
  130. float e1x, e1y, e1z;
  131. float e2x, e2y, e2z;
  132. e1x = ux - vx;
  133. e1y = uy - vy;
  134. e1z = uz - vz;
  135. e2x = ux - wx;
  136. e2y = uy - wy;
  137. e2z = uz - wz;
  138. // Cross product for normal
  139. float nx, ny, nz;
  140. nx = e1y * e2z - e1z * e2y;
  141. ny = e1z * e2x - e1x * e2z;
  142. nz = e1x * e2y - e1y * e2x;
  143. // Length
  144. float l = (float)Math.Sqrt(nx * nx + ny * ny + nz * nz);
  145. float lReciprocal = 1.0f / l;
  146. // Normalized "normal"
  147. //nx /= l;
  148. //ny /= l;
  149. //nz /= l;
  150. normals[i] = nx * lReciprocal;
  151. normals[i + 1] = ny * lReciprocal;
  152. normals[i + 2] = nz * lReciprocal;
  153. i += 3;
  154. }
  155. }
  156. public List<PhysicsVector> getVertexList()
  157. {
  158. List<PhysicsVector> result = new List<PhysicsVector>();
  159. foreach (Vertex v in vertices)
  160. {
  161. result.Add(v);
  162. }
  163. return result;
  164. }
  165. public float[] getVertexListAsFloatLocked()
  166. {
  167. float[] result;
  168. if (primMesh == null)
  169. {
  170. result = new float[vertices.Count * 3];
  171. for (int i = 0; i < vertices.Count; i++)
  172. {
  173. Vertex v = vertices[i];
  174. if (v == null)
  175. continue;
  176. result[3 * i + 0] = v.X;
  177. result[3 * i + 1] = v.Y;
  178. result[3 * i + 2] = v.Z;
  179. }
  180. pinnedVirtexes = GCHandle.Alloc(result, GCHandleType.Pinned);
  181. }
  182. else
  183. {
  184. int count = primMesh.coords.Count;
  185. result = new float[count * 3];
  186. for (int i = 0; i < count; i++)
  187. {
  188. Coord c = primMesh.coords[i];
  189. {
  190. int resultIndex = 3 * i;
  191. result[resultIndex] = c.X;
  192. result[resultIndex + 1] = c.Y;
  193. result[resultIndex + 2] = c.Z;
  194. }
  195. }
  196. pinnedVirtexes = GCHandle.Alloc(result, GCHandleType.Pinned);
  197. }
  198. return result;
  199. }
  200. public int[] getIndexListAsInt()
  201. {
  202. int[] result;
  203. if (primMesh == null)
  204. {
  205. result = new int[triangles.Count * 3];
  206. for (int i = 0; i < triangles.Count; i++)
  207. {
  208. Triangle t = triangles[i];
  209. result[3 * i + 0] = vertices.IndexOf(t.v1);
  210. result[3 * i + 1] = vertices.IndexOf(t.v2);
  211. result[3 * i + 2] = vertices.IndexOf(t.v3);
  212. }
  213. }
  214. else
  215. {
  216. int numFaces = primMesh.faces.Count;
  217. result = new int[numFaces * 3];
  218. for (int i = 0; i < numFaces; i++)
  219. {
  220. Face f = primMesh.faces[i];
  221. // Coord c1 = primMesh.coords[f.v1];
  222. // Coord c2 = primMesh.coords[f.v2];
  223. // Coord c3 = primMesh.coords[f.v3];
  224. int resultIndex = i * 3;
  225. result[resultIndex] = f.v1;
  226. result[resultIndex + 1] = f.v2;
  227. result[resultIndex + 2] = f.v3;
  228. }
  229. }
  230. return result;
  231. }
  232. /// <summary>
  233. /// creates a list of index values that defines triangle faces. THIS METHOD FREES ALL NON-PINNED MESH DATA
  234. /// </summary>
  235. /// <returns></returns>
  236. public int[] getIndexListAsIntLocked()
  237. {
  238. int[] result = getIndexListAsInt();
  239. pinnedIndex = GCHandle.Alloc(result, GCHandleType.Pinned);
  240. return result;
  241. }
  242. public void releasePinned()
  243. {
  244. pinnedVirtexes.Free();
  245. pinnedIndex.Free();
  246. }
  247. /// <summary>
  248. /// frees up the source mesh data to minimize memory - call this method after calling get*Locked() functions
  249. /// </summary>
  250. public void releaseSourceMeshData()
  251. {
  252. triangles = null;
  253. vertices = null;
  254. primMesh = null;
  255. }
  256. public void Append(Mesh newMesh)
  257. {
  258. foreach (Vertex v in newMesh.vertices)
  259. vertices.Add(v);
  260. foreach (Triangle t in newMesh.triangles)
  261. Add(t);
  262. }
  263. // Do a linear transformation of mesh.
  264. public void TransformLinear(float[,] matrix, float[] offset)
  265. {
  266. foreach (Vertex v in vertices)
  267. {
  268. if (v == null)
  269. continue;
  270. float x, y, z;
  271. x = v.X*matrix[0, 0] + v.Y*matrix[1, 0] + v.Z*matrix[2, 0];
  272. y = v.X*matrix[0, 1] + v.Y*matrix[1, 1] + v.Z*matrix[2, 1];
  273. z = v.X*matrix[0, 2] + v.Y*matrix[1, 2] + v.Z*matrix[2, 2];
  274. v.X = x + offset[0];
  275. v.Y = y + offset[1];
  276. v.Z = z + offset[2];
  277. }
  278. }
  279. public void DumpRaw(String path, String name, String title)
  280. {
  281. if (path == null)
  282. return;
  283. String fileName = name + "_" + title + ".raw";
  284. String completePath = Path.Combine(path, fileName);
  285. StreamWriter sw = new StreamWriter(completePath);
  286. foreach (Triangle t in triangles)
  287. {
  288. String s = t.ToStringRaw();
  289. sw.WriteLine(s);
  290. }
  291. sw.Close();
  292. }
  293. }
  294. }