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