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