aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs')
-rw-r--r--OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs601
1 files changed, 601 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs b/OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs
new file mode 100644
index 0000000..0418893
--- /dev/null
+++ b/OpenSim/Region/PhysicsModules/UbitMeshing/Mesh.cs
@@ -0,0 +1,601 @@
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 OpenSimulator 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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Runtime.InteropServices;
32using OpenSim.Region.Physics.Manager;
33using PrimMesher;
34using OpenMetaverse;
35using System.Runtime.Serialization;
36using System.Runtime.Serialization.Formatters.Binary;
37
38namespace OpenSim.Region.Physics.Meshing
39{
40 public class MeshBuildingData
41 {
42 public Dictionary<Vertex, int> m_vertices;
43 public List<Triangle> m_triangles;
44 public float m_obbXmin;
45 public float m_obbXmax;
46 public float m_obbYmin;
47 public float m_obbYmax;
48 public float m_obbZmin;
49 public float m_obbZmax;
50 public Vector3 m_centroid;
51 public int m_centroidDiv;
52 }
53
54 [Serializable()]
55 public class Mesh : IMesh
56 {
57 float[] vertices;
58 int[] indexes;
59 Vector3 m_obb;
60 Vector3 m_obboffset;
61 [NonSerialized()]
62 MeshBuildingData m_bdata;
63 [NonSerialized()]
64 GCHandle vhandler;
65 [NonSerialized()]
66 GCHandle ihandler;
67 [NonSerialized()]
68 IntPtr m_verticesPtr = IntPtr.Zero;
69 [NonSerialized()]
70 IntPtr m_indicesPtr = IntPtr.Zero;
71 [NonSerialized()]
72 int m_vertexCount = 0;
73 [NonSerialized()]
74 int m_indexCount = 0;
75
76 public int RefCount { get; set; }
77 public AMeshKey Key { get; set; }
78
79 private class vertexcomp : IEqualityComparer<Vertex>
80 {
81 public bool Equals(Vertex v1, Vertex v2)
82 {
83 if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z)
84 return true;
85 else
86 return false;
87 }
88 public int GetHashCode(Vertex v)
89 {
90 int a = v.X.GetHashCode();
91 int b = v.Y.GetHashCode();
92 int c = v.Z.GetHashCode();
93 return (a << 16) ^ (b << 8) ^ c;
94 }
95 }
96
97 public Mesh()
98 {
99 vertexcomp vcomp = new vertexcomp();
100
101 m_bdata = new MeshBuildingData();
102 m_bdata.m_vertices = new Dictionary<Vertex, int>(vcomp);
103 m_bdata.m_triangles = new List<Triangle>();
104 m_bdata.m_centroid = Vector3.Zero;
105 m_bdata.m_centroidDiv = 0;
106 m_bdata.m_obbXmin = float.MaxValue;
107 m_bdata.m_obbXmax = float.MinValue;
108 m_bdata.m_obbYmin = float.MaxValue;
109 m_bdata.m_obbYmax = float.MinValue;
110 m_bdata.m_obbZmin = float.MaxValue;
111 m_bdata.m_obbZmax = float.MinValue;
112 m_obb = new Vector3(0.5f, 0.5f, 0.5f);
113 m_obboffset = Vector3.Zero;
114 }
115
116
117 public Mesh Scale(Vector3 scale)
118 {
119 if (m_verticesPtr == null || m_indicesPtr == null)
120 return null;
121
122 Mesh result = new Mesh();
123
124 float x = scale.X;
125 float y = scale.Y;
126 float z = scale.Z;
127
128 result.m_obb.X = m_obb.X * x;
129 result.m_obb.Y = m_obb.Y * y;
130 result.m_obb.Z = m_obb.Z * z;
131 result.m_obboffset.X = m_obboffset.X * x;
132 result.m_obboffset.Y = m_obboffset.Y * y;
133 result.m_obboffset.Z = m_obboffset.Z * z;
134
135 result.vertices = new float[vertices.Length];
136 int j = 0;
137 for (int i = 0; i < m_vertexCount; i++)
138 {
139 result.vertices[j] = vertices[j] * x;
140 j++;
141 result.vertices[j] = vertices[j] * y;
142 j++;
143 result.vertices[j] = vertices[j] * z;
144 j++;
145 }
146
147 result.indexes = new int[indexes.Length];
148 indexes.CopyTo(result.indexes,0);
149
150 result.pinMemory();
151
152 return result;
153 }
154
155 public Mesh Clone()
156 {
157 Mesh result = new Mesh();
158
159 if (m_bdata != null)
160 {
161 result.m_bdata = new MeshBuildingData();
162 foreach (Triangle t in m_bdata.m_triangles)
163 {
164 result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone()));
165 }
166 result.m_bdata.m_centroid = m_bdata.m_centroid;
167 result.m_bdata.m_centroidDiv = m_bdata.m_centroidDiv;
168 result.m_bdata.m_obbXmin = m_bdata.m_obbXmin;
169 result.m_bdata.m_obbXmax = m_bdata.m_obbXmax;
170 result.m_bdata.m_obbYmin = m_bdata.m_obbYmin;
171 result.m_bdata.m_obbYmax = m_bdata.m_obbYmax;
172 result.m_bdata.m_obbZmin = m_bdata.m_obbZmin;
173 result.m_bdata.m_obbZmax = m_bdata.m_obbZmax;
174 }
175 result.m_obb = m_obb;
176 result.m_obboffset = m_obboffset;
177 return result;
178 }
179
180 public void addVertexLStats(Vertex v)
181 {
182 float x = v.X;
183 float y = v.Y;
184 float z = v.Z;
185
186 m_bdata.m_centroid.X += x;
187 m_bdata.m_centroid.Y += y;
188 m_bdata.m_centroid.Z += z;
189 m_bdata.m_centroidDiv++;
190
191 if (x > m_bdata.m_obbXmax)
192 m_bdata.m_obbXmax = x;
193 else if (x < m_bdata.m_obbXmin)
194 m_bdata.m_obbXmin = x;
195
196 if (y > m_bdata.m_obbYmax)
197 m_bdata.m_obbYmax = y;
198 else if (y < m_bdata.m_obbYmin)
199 m_bdata.m_obbYmin = y;
200
201 if (z > m_bdata.m_obbZmax)
202 m_bdata.m_obbZmax = z;
203 else if (z < m_bdata.m_obbZmin)
204 m_bdata.m_obbZmin = z;
205
206 }
207
208 public void Add(Triangle triangle)
209 {
210 if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
211 throw new NotSupportedException("Attempt to Add to a pinned Mesh");
212
213
214 triangle.v1.X = (float)Math.Round(triangle.v1.X, 6);
215 triangle.v1.Y = (float)Math.Round(triangle.v1.Y, 6);
216 triangle.v1.Z = (float)Math.Round(triangle.v1.Z, 6);
217 triangle.v2.X = (float)Math.Round(triangle.v2.X, 6);
218 triangle.v2.Y = (float)Math.Round(triangle.v2.Y, 6);
219 triangle.v2.Z = (float)Math.Round(triangle.v2.Z, 6);
220 triangle.v3.X = (float)Math.Round(triangle.v3.X, 6);
221 triangle.v3.Y = (float)Math.Round(triangle.v3.Y, 6);
222 triangle.v3.Z = (float)Math.Round(triangle.v3.Z, 6);
223
224 if ((triangle.v1.X == triangle.v2.X && triangle.v1.Y == triangle.v2.Y && triangle.v1.Z == triangle.v2.Z)
225 || (triangle.v1.X == triangle.v3.X && triangle.v1.Y == triangle.v3.Y && triangle.v1.Z == triangle.v3.Z)
226 || (triangle.v2.X == triangle.v3.X && triangle.v2.Y == triangle.v3.Y && triangle.v2.Z == triangle.v3.Z)
227 )
228 {
229 return;
230 }
231
232 if (m_bdata.m_vertices.Count == 0)
233 {
234 m_bdata.m_centroidDiv = 0;
235 m_bdata.m_centroid = Vector3.Zero;
236 }
237
238 if (!m_bdata.m_vertices.ContainsKey(triangle.v1))
239 {
240 m_bdata.m_vertices[triangle.v1] = m_bdata.m_vertices.Count;
241 addVertexLStats(triangle.v1);
242 }
243 if (!m_bdata.m_vertices.ContainsKey(triangle.v2))
244 {
245 m_bdata.m_vertices[triangle.v2] = m_bdata.m_vertices.Count;
246 addVertexLStats(triangle.v2);
247 }
248 if (!m_bdata.m_vertices.ContainsKey(triangle.v3))
249 {
250 m_bdata.m_vertices[triangle.v3] = m_bdata.m_vertices.Count;
251 addVertexLStats(triangle.v3);
252 }
253 m_bdata.m_triangles.Add(triangle);
254 }
255
256 public Vector3 GetCentroid()
257 {
258 return m_obboffset;
259
260 }
261
262 public Vector3 GetOBB()
263 {
264 return m_obb;
265 float x, y, z;
266 if (m_bdata.m_centroidDiv > 0)
267 {
268 x = (m_bdata.m_obbXmax - m_bdata.m_obbXmin) * 0.5f;
269 y = (m_bdata.m_obbYmax - m_bdata.m_obbYmin) * 0.5f;
270 z = (m_bdata.m_obbZmax - m_bdata.m_obbZmin) * 0.5f;
271 }
272 else // ??
273 {
274 x = 0.5f;
275 y = 0.5f;
276 z = 0.5f;
277 }
278 return new Vector3(x, y, z);
279 }
280
281 public List<Vector3> getVertexList()
282 {
283 List<Vector3> result = new List<Vector3>();
284 foreach (Vertex v in m_bdata.m_vertices.Keys)
285 {
286 result.Add(new Vector3(v.X, v.Y, v.Z));
287 }
288 return result;
289 }
290
291 public float[] getVertexListAsFloat()
292 {
293 if (m_bdata.m_vertices == null)
294 throw new NotSupportedException();
295 float[] result = new float[m_bdata.m_vertices.Count * 3];
296 foreach (KeyValuePair<Vertex, int> kvp in m_bdata.m_vertices)
297 {
298 Vertex v = kvp.Key;
299 int i = kvp.Value;
300 result[3 * i + 0] = v.X;
301 result[3 * i + 1] = v.Y;
302 result[3 * i + 2] = v.Z;
303 }
304 return result;
305 }
306
307 public float[] getVertexListAsFloatLocked()
308 {
309 return null;
310 }
311
312 public void getVertexListAsPtrToFloatArray(out IntPtr _vertices, out int vertexStride, out int vertexCount)
313 {
314 // A vertex is 3 floats
315 vertexStride = 3 * sizeof(float);
316
317 // If there isn't an unmanaged array allocated yet, do it now
318 if (m_verticesPtr == IntPtr.Zero && m_bdata != null)
319 {
320 vertices = getVertexListAsFloat();
321 // Each vertex is 3 elements (floats)
322 m_vertexCount = vertices.Length / 3;
323 vhandler = GCHandle.Alloc(vertices, GCHandleType.Pinned);
324 m_verticesPtr = vhandler.AddrOfPinnedObject();
325 GC.AddMemoryPressure(Buffer.ByteLength(vertices));
326 }
327 _vertices = m_verticesPtr;
328 vertexCount = m_vertexCount;
329 }
330
331 public int[] getIndexListAsInt()
332 {
333 if (m_bdata.m_triangles == null)
334 throw new NotSupportedException();
335 int[] result = new int[m_bdata.m_triangles.Count * 3];
336 for (int i = 0; i < m_bdata.m_triangles.Count; i++)
337 {
338 Triangle t = m_bdata.m_triangles[i];
339 result[3 * i + 0] = m_bdata.m_vertices[t.v1];
340 result[3 * i + 1] = m_bdata.m_vertices[t.v2];
341 result[3 * i + 2] = m_bdata.m_vertices[t.v3];
342 }
343 return result;
344 }
345
346 /// <summary>
347 /// creates a list of index values that defines triangle faces. THIS METHOD FREES ALL NON-PINNED MESH DATA
348 /// </summary>
349 /// <returns></returns>
350 public int[] getIndexListAsIntLocked()
351 {
352 return null;
353 }
354
355 public void getIndexListAsPtrToIntArray(out IntPtr indices, out int triStride, out int indexCount)
356 {
357 // If there isn't an unmanaged array allocated yet, do it now
358 if (m_indicesPtr == IntPtr.Zero && m_bdata != null)
359 {
360 indexes = getIndexListAsInt();
361 m_indexCount = indexes.Length;
362 ihandler = GCHandle.Alloc(indexes, GCHandleType.Pinned);
363 m_indicesPtr = ihandler.AddrOfPinnedObject();
364 GC.AddMemoryPressure(Buffer.ByteLength(indexes));
365 }
366 // A triangle is 3 ints (indices)
367 triStride = 3 * sizeof(int);
368 indices = m_indicesPtr;
369 indexCount = m_indexCount;
370 }
371
372 public void releasePinned()
373 {
374 if (m_verticesPtr != IntPtr.Zero)
375 {
376 vhandler.Free();
377 vertices = null;
378 m_verticesPtr = IntPtr.Zero;
379 }
380 if (m_indicesPtr != IntPtr.Zero)
381 {
382 ihandler.Free();
383 indexes = null;
384 m_indicesPtr = IntPtr.Zero;
385 }
386 }
387
388 /// <summary>
389 /// frees up the source mesh data to minimize memory - call this method after calling get*Locked() functions
390 /// </summary>
391 public void releaseSourceMeshData()
392 {
393 if (m_bdata != null)
394 {
395 m_bdata.m_triangles = null;
396 m_bdata.m_vertices = null;
397 }
398 }
399
400 public void releaseBuildingMeshData()
401 {
402 if (m_bdata != null)
403 {
404 m_bdata.m_triangles = null;
405 m_bdata.m_vertices = null;
406 m_bdata = null;
407 }
408 }
409
410 public void Append(IMesh newMesh)
411 {
412 if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
413 throw new NotSupportedException("Attempt to Append to a pinned Mesh");
414
415 if (!(newMesh is Mesh))
416 return;
417
418 foreach (Triangle t in ((Mesh)newMesh).m_bdata.m_triangles)
419 Add(t);
420 }
421
422 // Do a linear transformation of mesh.
423 public void TransformLinear(float[,] matrix, float[] offset)
424 {
425 if (m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero)
426 throw new NotSupportedException("Attempt to TransformLinear a pinned Mesh");
427
428 foreach (Vertex v in m_bdata.m_vertices.Keys)
429 {
430 if (v == null)
431 continue;
432 float x, y, z;
433 x = v.X*matrix[0, 0] + v.Y*matrix[1, 0] + v.Z*matrix[2, 0];
434 y = v.X*matrix[0, 1] + v.Y*matrix[1, 1] + v.Z*matrix[2, 1];
435 z = v.X*matrix[0, 2] + v.Y*matrix[1, 2] + v.Z*matrix[2, 2];
436 v.X = x + offset[0];
437 v.Y = y + offset[1];
438 v.Z = z + offset[2];
439 }
440 }
441
442 public void DumpRaw(String path, String name, String title)
443 {
444 if (path == null)
445 return;
446 if (m_bdata == null)
447 return;
448 String fileName = name + "_" + title + ".raw";
449 String completePath = System.IO.Path.Combine(path, fileName);
450 StreamWriter sw = new StreamWriter(completePath);
451 foreach (Triangle t in m_bdata.m_triangles)
452 {
453 String s = t.ToStringRaw();
454 sw.WriteLine(s);
455 }
456 sw.Close();
457 }
458
459 public void TrimExcess()
460 {
461 m_bdata.m_triangles.TrimExcess();
462 }
463
464 public void pinMemory()
465 {
466 m_vertexCount = vertices.Length / 3;
467 vhandler = GCHandle.Alloc(vertices, GCHandleType.Pinned);
468 m_verticesPtr = vhandler.AddrOfPinnedObject();
469 GC.AddMemoryPressure(Buffer.ByteLength(vertices));
470
471 m_indexCount = indexes.Length;
472 ihandler = GCHandle.Alloc(indexes, GCHandleType.Pinned);
473 m_indicesPtr = ihandler.AddrOfPinnedObject();
474 GC.AddMemoryPressure(Buffer.ByteLength(indexes));
475 }
476
477 public void PrepForOde()
478 {
479 // If there isn't an unmanaged array allocated yet, do it now
480 if (m_verticesPtr == IntPtr.Zero)
481 vertices = getVertexListAsFloat();
482
483 // If there isn't an unmanaged array allocated yet, do it now
484 if (m_indicesPtr == IntPtr.Zero)
485 indexes = getIndexListAsInt();
486
487 pinMemory();
488
489 float x, y, z;
490
491 if (m_bdata.m_centroidDiv > 0)
492 {
493 m_obboffset = new Vector3(m_bdata.m_centroid.X / m_bdata.m_centroidDiv, m_bdata.m_centroid.Y / m_bdata.m_centroidDiv, m_bdata.m_centroid.Z / m_bdata.m_centroidDiv);
494 x = (m_bdata.m_obbXmax - m_bdata.m_obbXmin) * 0.5f;
495 y = (m_bdata.m_obbYmax - m_bdata.m_obbYmin) * 0.5f;
496 z = (m_bdata.m_obbZmax - m_bdata.m_obbZmin) * 0.5f;
497 }
498
499 else
500 {
501 m_obboffset = Vector3.Zero;
502 x = 0.5f;
503 y = 0.5f;
504 z = 0.5f;
505 }
506 m_obb = new Vector3(x, y, z);
507
508 releaseBuildingMeshData();
509 }
510 public bool ToStream(Stream st)
511 {
512 if (m_indicesPtr == IntPtr.Zero || m_verticesPtr == IntPtr.Zero)
513 return false;
514
515 BinaryWriter bw = new BinaryWriter(st);
516 bool ok = true;
517
518 try
519 {
520
521 bw.Write(m_vertexCount);
522 bw.Write(m_indexCount);
523
524 for (int i = 0; i < 3 * m_vertexCount; i++)
525 bw.Write(vertices[i]);
526 for (int i = 0; i < m_indexCount; i++)
527 bw.Write(indexes[i]);
528 bw.Write(m_obb.X);
529 bw.Write(m_obb.Y);
530 bw.Write(m_obb.Z);
531 bw.Write(m_obboffset.X);
532 bw.Write(m_obboffset.Y);
533 bw.Write(m_obboffset.Z);
534 }
535 catch
536 {
537 ok = false;
538 }
539
540 if (bw != null)
541 {
542 bw.Flush();
543 bw.Close();
544 }
545
546 return ok;
547 }
548
549 public static Mesh FromStream(Stream st, AMeshKey key)
550 {
551 Mesh mesh = new Mesh();
552 mesh.releaseBuildingMeshData();
553
554 BinaryReader br = new BinaryReader(st);
555
556 bool ok = true;
557 try
558 {
559 mesh.m_vertexCount = br.ReadInt32();
560 mesh.m_indexCount = br.ReadInt32();
561
562 int n = 3 * mesh.m_vertexCount;
563 mesh.vertices = new float[n];
564 for (int i = 0; i < n; i++)
565 mesh.vertices[i] = br.ReadSingle();
566
567 mesh.indexes = new int[mesh.m_indexCount];
568 for (int i = 0; i < mesh.m_indexCount; i++)
569 mesh.indexes[i] = br.ReadInt32();
570
571 mesh.m_obb.X = br.ReadSingle();
572 mesh.m_obb.Y = br.ReadSingle();
573 mesh.m_obb.Z = br.ReadSingle();
574
575 mesh.m_obboffset.X = br.ReadSingle();
576 mesh.m_obboffset.Y = br.ReadSingle();
577 mesh.m_obboffset.Z = br.ReadSingle();
578 }
579 catch
580 {
581 ok = false;
582 }
583
584 br.Close();
585
586 if (ok)
587 {
588 mesh.pinMemory();
589
590 mesh.Key = key;
591 mesh.RefCount = 1;
592
593 return mesh;
594 }
595
596 mesh.vertices = null;
597 mesh.indexes = null;
598 return null;
599 }
600 }
601}