diff options
Diffstat (limited to 'OpenSim/Region/Physics/OdePlugin/Meshing/Mesh.cs')
-rwxr-xr-x | OpenSim/Region/Physics/OdePlugin/Meshing/Mesh.cs | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/OdePlugin/Meshing/Mesh.cs b/OpenSim/Region/Physics/OdePlugin/Meshing/Mesh.cs new file mode 100755 index 0000000..5a51703 --- /dev/null +++ b/OpenSim/Region/Physics/OdePlugin/Meshing/Mesh.cs | |||
@@ -0,0 +1,197 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | |||
6 | using System.Runtime.InteropServices; | ||
7 | |||
8 | |||
9 | using OpenSim.Region.Physics.Manager; | ||
10 | |||
11 | namespace OpenSim.Region.Physics.OdePlugin.Meshing | ||
12 | { | ||
13 | public class Mesh | ||
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 float[] getVertexListAsFloat() | ||
124 | { | ||
125 | float[] result = new float[vertices.Count * 3]; | ||
126 | for (int i = 0; i < vertices.Count; i++) | ||
127 | { | ||
128 | Vertex v = vertices[i]; | ||
129 | if (v == null) | ||
130 | continue; | ||
131 | result[3 * i + 0] = v.X; | ||
132 | result[3 * i + 1] = v.Y; | ||
133 | result[3 * i + 2] = v.Z; | ||
134 | } | ||
135 | GCHandle.Alloc(result, GCHandleType.Pinned); | ||
136 | return result; | ||
137 | } | ||
138 | |||
139 | public int[] getIndexListAsInt() | ||
140 | { | ||
141 | int[] result = new int[triangles.Count * 3]; | ||
142 | for (int i = 0; i < triangles.Count; i++) | ||
143 | { | ||
144 | Triangle t = triangles[i]; | ||
145 | result[3 * i + 0] = vertices.IndexOf(t.v1); | ||
146 | result[3 * i + 1] = vertices.IndexOf(t.v2); | ||
147 | result[3 * i + 2] = vertices.IndexOf(t.v3); | ||
148 | } | ||
149 | GCHandle.Alloc(result, GCHandleType.Pinned); | ||
150 | return result; | ||
151 | } | ||
152 | |||
153 | |||
154 | public void Append(Mesh newMesh) | ||
155 | { | ||
156 | foreach (Vertex v in newMesh.vertices) | ||
157 | vertices.Add(v); | ||
158 | |||
159 | foreach (Triangle t in newMesh.triangles) | ||
160 | Add(t); | ||
161 | |||
162 | } | ||
163 | |||
164 | // Do a linear transformation of mesh. | ||
165 | public void TransformLinear(float[,] matrix, float[] offset) | ||
166 | { | ||
167 | foreach (Vertex v in vertices) | ||
168 | { | ||
169 | if (v == null) | ||
170 | continue; | ||
171 | float x, y, z; | ||
172 | x = v.X * matrix[0, 0] + v.Y * matrix[1, 0] + v.Z * matrix[2, 0]; | ||
173 | y = v.X * matrix[0, 1] + v.Y * matrix[1, 1] + v.Z * matrix[2, 1]; | ||
174 | z = v.X * matrix[0, 2] + v.Y * matrix[1, 2] + v.Z * matrix[2, 2]; | ||
175 | v.X = x + offset[0]; | ||
176 | v.Y = y + offset[1]; | ||
177 | v.Z = z + offset[2]; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | public void DumpRaw(String path, String name, String title) | ||
182 | { | ||
183 | if (path == null) | ||
184 | return; | ||
185 | String fileName = name + "_" + title + ".raw"; | ||
186 | String completePath = Path.Combine(path, fileName); | ||
187 | StreamWriter sw = new StreamWriter(completePath); | ||
188 | foreach (Triangle t in triangles) | ||
189 | { | ||
190 | String s = t.ToStringRaw(); | ||
191 | sw.WriteLine(s); | ||
192 | } | ||
193 | sw.Close(); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | } | ||