aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/Meshing/Mesh.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/Meshing/Mesh.cs')
-rw-r--r--OpenSim/Region/Physics/Meshing/Mesh.cs213
1 files changed, 213 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/Meshing/Mesh.cs b/OpenSim/Region/Physics/Meshing/Mesh.cs
new file mode 100644
index 0000000..fd4ce4e
--- /dev/null
+++ b/OpenSim/Region/Physics/Meshing/Mesh.cs
@@ -0,0 +1,213 @@
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Text;
5
6using System.Runtime.InteropServices;
7
8
9using OpenSim.Region.Physics.Manager;
10
11namespace OpenSim.Region.Physics.Meshing
12{
13 public class Mesh : IMesh
14 {
15 public List<Vertex> vertices;
16 public List<Triangle> triangles;
17
18 public float[] normals;
19
20 public Mesh()
21 {
22 vertices = new List<Vertex>();
23 triangles = new List<Triangle>();
24 }
25
26 public Mesh Clone()
27 {
28 Mesh result = new Mesh();
29
30 foreach (Vertex v in vertices)
31 {
32 if (v == null)
33 result.vertices.Add(null);
34 else
35 result.vertices.Add(v.Clone());
36 }
37
38 foreach (Triangle t in triangles)
39 {
40 int iV1, iV2, iV3;
41 iV1 = this.vertices.IndexOf(t.v1);
42 iV2 = this.vertices.IndexOf(t.v2);
43 iV3 = this.vertices.IndexOf(t.v3);
44
45 Triangle newT = new Triangle(result.vertices[iV1], result.vertices[iV2], result.vertices[iV3]);
46 result.Add(newT);
47 }
48
49 return result;
50 }
51
52
53
54 public void Add(Triangle triangle)
55 {
56 int i;
57 i = vertices.IndexOf(triangle.v1);
58 if (i < 0)
59 throw new ArgumentException("Vertex v1 not known to mesh");
60 i = vertices.IndexOf(triangle.v2);
61 if (i < 0)
62 throw new ArgumentException("Vertex v2 not known to mesh");
63 i = vertices.IndexOf(triangle.v3);
64 if (i < 0)
65 throw new ArgumentException("Vertex v3 not known to mesh");
66
67 triangles.Add(triangle);
68 }
69
70 public void Add(Vertex v)
71 {
72 vertices.Add(v);
73 }
74
75 public void Remove(Vertex v)
76 {
77 int i;
78
79 // First, remove all triangles that are build on v
80 for (i = 0; i < triangles.Count; i++)
81 {
82 Triangle t = triangles[i];
83 if (t.v1 == v || t.v2 == v || t.v3 == v)
84 {
85 triangles.RemoveAt(i);
86 i--;
87 }
88 }
89
90 // Second remove v itself
91 vertices.Remove(v);
92 }
93
94 public void RemoveTrianglesOutside(SimpleHull hull)
95 {
96 int i;
97
98 for (i = 0; i < triangles.Count; i++)
99 {
100 Triangle t = triangles[i];
101 Vertex v1 = t.v1;
102 Vertex v2 = t.v2;
103 Vertex v3 = t.v3;
104 PhysicsVector m = v1 + v2 + v3;
105 m /= 3.0f;
106 if (!hull.IsPointIn(new Vertex(m)))
107 {
108 triangles.RemoveAt(i);
109 i--;
110 }
111 }
112 }
113
114
115 public void Add(List<Vertex> lv)
116 {
117 foreach (Vertex v in lv)
118 {
119 vertices.Add(v);
120 }
121 }
122
123 public List<PhysicsVector> getVertexList()
124 {
125 List<PhysicsVector> result = new List<PhysicsVector>();
126 foreach (Vertex v in vertices)
127 {
128 result.Add(v);
129 }
130 return result;
131 }
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
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
162 public int[] getIndexListAsIntLocked()
163 {
164 int[] result = getIndexListAsInt();
165 GCHandle.Alloc(result, GCHandleType.Pinned);
166 return result;
167 }
168
169
170 public void Append(Mesh newMesh)
171 {
172 foreach (Vertex v in newMesh.vertices)
173 vertices.Add(v);
174
175 foreach (Triangle t in newMesh.triangles)
176 Add(t);
177
178 }
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
197 public void DumpRaw(String path, String name, String title)
198 {
199 if (path == null)
200 return;
201 String fileName = name + "_" + title + ".raw";
202 String completePath = Path.Combine(path, fileName);
203 StreamWriter sw = new StreamWriter(completePath);
204 foreach (Triangle t in triangles)
205 {
206 String s = t.ToStringRaw();
207 sw.WriteLine(s);
208 }
209 sw.Close();
210 }
211 }
212
213}