diff options
Diffstat (limited to 'OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs')
-rw-r--r-- | OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs | 87 |
1 files changed, 81 insertions, 6 deletions
diff --git a/OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs b/OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs index bf397ee..42ba37e 100644 --- a/OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs +++ b/OpenSim/Region/PhysicsModules/Meshing/Meshmerizer/Mesh.cs | |||
@@ -33,7 +33,7 @@ using OpenSim.Region.PhysicsModules.SharedBase; | |||
33 | using PrimMesher; | 33 | using PrimMesher; |
34 | using OpenMetaverse; | 34 | using OpenMetaverse; |
35 | 35 | ||
36 | namespace OpenSim.Region.PhysicsModules.Meshing | 36 | namespace OpenSim.Region.PhysicsModule.Meshing |
37 | { | 37 | { |
38 | public class Mesh : IMesh | 38 | public class Mesh : IMesh |
39 | { | 39 | { |
@@ -46,11 +46,36 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
46 | IntPtr m_indicesPtr = IntPtr.Zero; | 46 | IntPtr m_indicesPtr = IntPtr.Zero; |
47 | int m_indexCount = 0; | 47 | int m_indexCount = 0; |
48 | public float[] m_normals; | 48 | public float[] m_normals; |
49 | Vector3 _centroid; | ||
50 | int _centroidDiv; | ||
51 | |||
52 | private class vertexcomp : IEqualityComparer<Vertex> | ||
53 | { | ||
54 | public bool Equals(Vertex v1, Vertex v2) | ||
55 | { | ||
56 | if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z) | ||
57 | return true; | ||
58 | else | ||
59 | return false; | ||
60 | } | ||
61 | public int GetHashCode(Vertex v) | ||
62 | { | ||
63 | int a = v.X.GetHashCode(); | ||
64 | int b = v.Y.GetHashCode(); | ||
65 | int c = v.Z.GetHashCode(); | ||
66 | return (a << 16) ^ (b << 8) ^ c; | ||
67 | } | ||
68 | |||
69 | } | ||
49 | 70 | ||
50 | public Mesh() | 71 | public Mesh() |
51 | { | 72 | { |
52 | m_vertices = new Dictionary<Vertex, int>(); | 73 | vertexcomp vcomp = new vertexcomp(); |
74 | |||
75 | m_vertices = new Dictionary<Vertex, int>(vcomp); | ||
53 | m_triangles = new List<Triangle>(); | 76 | m_triangles = new List<Triangle>(); |
77 | _centroid = Vector3.Zero; | ||
78 | _centroidDiv = 0; | ||
54 | } | 79 | } |
55 | 80 | ||
56 | public Mesh Clone() | 81 | public Mesh Clone() |
@@ -61,7 +86,8 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
61 | { | 86 | { |
62 | result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone())); | 87 | result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone())); |
63 | } | 88 | } |
64 | 89 | result._centroid = _centroid; | |
90 | result._centroidDiv = _centroidDiv; | ||
65 | return result; | 91 | return result; |
66 | } | 92 | } |
67 | 93 | ||
@@ -71,15 +97,63 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
71 | throw new NotSupportedException("Attempt to Add to a pinned Mesh"); | 97 | throw new NotSupportedException("Attempt to Add to a pinned Mesh"); |
72 | // If a vertex of the triangle is not yet in the vertices list, | 98 | // If a vertex of the triangle is not yet in the vertices list, |
73 | // add it and set its index to the current index count | 99 | // add it and set its index to the current index count |
100 | // vertex == seems broken | ||
101 | // skip colapsed triangles | ||
102 | if ((triangle.v1.X == triangle.v2.X && triangle.v1.Y == triangle.v2.Y && triangle.v1.Z == triangle.v2.Z) | ||
103 | || (triangle.v1.X == triangle.v3.X && triangle.v1.Y == triangle.v3.Y && triangle.v1.Z == triangle.v3.Z) | ||
104 | || (triangle.v2.X == triangle.v3.X && triangle.v2.Y == triangle.v3.Y && triangle.v2.Z == triangle.v3.Z) | ||
105 | ) | ||
106 | { | ||
107 | return; | ||
108 | } | ||
109 | |||
110 | if (m_vertices.Count == 0) | ||
111 | { | ||
112 | _centroidDiv = 0; | ||
113 | _centroid = Vector3.Zero; | ||
114 | } | ||
115 | |||
74 | if (!m_vertices.ContainsKey(triangle.v1)) | 116 | if (!m_vertices.ContainsKey(triangle.v1)) |
117 | { | ||
75 | m_vertices[triangle.v1] = m_vertices.Count; | 118 | m_vertices[triangle.v1] = m_vertices.Count; |
119 | _centroid.X += triangle.v1.X; | ||
120 | _centroid.Y += triangle.v1.Y; | ||
121 | _centroid.Z += triangle.v1.Z; | ||
122 | _centroidDiv++; | ||
123 | } | ||
76 | if (!m_vertices.ContainsKey(triangle.v2)) | 124 | if (!m_vertices.ContainsKey(triangle.v2)) |
125 | { | ||
77 | m_vertices[triangle.v2] = m_vertices.Count; | 126 | m_vertices[triangle.v2] = m_vertices.Count; |
127 | _centroid.X += triangle.v2.X; | ||
128 | _centroid.Y += triangle.v2.Y; | ||
129 | _centroid.Z += triangle.v2.Z; | ||
130 | _centroidDiv++; | ||
131 | } | ||
78 | if (!m_vertices.ContainsKey(triangle.v3)) | 132 | if (!m_vertices.ContainsKey(triangle.v3)) |
133 | { | ||
79 | m_vertices[triangle.v3] = m_vertices.Count; | 134 | m_vertices[triangle.v3] = m_vertices.Count; |
135 | _centroid.X += triangle.v3.X; | ||
136 | _centroid.Y += triangle.v3.Y; | ||
137 | _centroid.Z += triangle.v3.Z; | ||
138 | _centroidDiv++; | ||
139 | } | ||
80 | m_triangles.Add(triangle); | 140 | m_triangles.Add(triangle); |
81 | } | 141 | } |
82 | 142 | ||
143 | public Vector3 GetCentroid() | ||
144 | { | ||
145 | if (_centroidDiv > 0) | ||
146 | return new Vector3(_centroid.X / _centroidDiv, _centroid.Y / _centroidDiv, _centroid.Z / _centroidDiv); | ||
147 | else | ||
148 | return Vector3.Zero; | ||
149 | } | ||
150 | |||
151 | // not functional | ||
152 | public Vector3 GetOBB() | ||
153 | { | ||
154 | return new Vector3(0.5f, 0.5f, 0.5f); | ||
155 | } | ||
156 | |||
83 | public void CalcNormals() | 157 | public void CalcNormals() |
84 | { | 158 | { |
85 | int iTriangles = m_triangles.Count; | 159 | int iTriangles = m_triangles.Count; |
@@ -185,6 +259,7 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
185 | public void getVertexListAsPtrToFloatArray(out IntPtr vertices, out int vertexStride, out int vertexCount) | 259 | public void getVertexListAsPtrToFloatArray(out IntPtr vertices, out int vertexStride, out int vertexCount) |
186 | { | 260 | { |
187 | // A vertex is 3 floats | 261 | // A vertex is 3 floats |
262 | |||
188 | vertexStride = 3 * sizeof(float); | 263 | vertexStride = 3 * sizeof(float); |
189 | 264 | ||
190 | // If there isn't an unmanaged array allocated yet, do it now | 265 | // If there isn't an unmanaged array allocated yet, do it now |
@@ -224,7 +299,7 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
224 | { | 299 | { |
225 | if (m_pinnedIndex.IsAllocated) | 300 | if (m_pinnedIndex.IsAllocated) |
226 | return (int[])(m_pinnedIndex.Target); | 301 | return (int[])(m_pinnedIndex.Target); |
227 | 302 | ||
228 | int[] result = getIndexListAsInt(); | 303 | int[] result = getIndexListAsInt(); |
229 | m_pinnedIndex = GCHandle.Alloc(result, GCHandleType.Pinned); | 304 | m_pinnedIndex = GCHandle.Alloc(result, GCHandleType.Pinned); |
230 | // Inform the garbage collector of this unmanaged allocation so it can schedule | 305 | // Inform the garbage collector of this unmanaged allocation so it can schedule |
@@ -282,7 +357,7 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
282 | { | 357 | { |
283 | if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) | 358 | if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) |
284 | throw new NotSupportedException("Attempt to Append to a pinned Mesh"); | 359 | throw new NotSupportedException("Attempt to Append to a pinned Mesh"); |
285 | 360 | ||
286 | if (!(newMesh is Mesh)) | 361 | if (!(newMesh is Mesh)) |
287 | return; | 362 | return; |
288 | 363 | ||
@@ -295,7 +370,7 @@ namespace OpenSim.Region.PhysicsModules.Meshing | |||
295 | { | 370 | { |
296 | if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) | 371 | if (m_pinnedIndex.IsAllocated || m_pinnedVertexes.IsAllocated || m_indicesPtr != IntPtr.Zero || m_verticesPtr != IntPtr.Zero) |
297 | throw new NotSupportedException("Attempt to TransformLinear a pinned Mesh"); | 372 | throw new NotSupportedException("Attempt to TransformLinear a pinned Mesh"); |
298 | 373 | ||
299 | foreach (Vertex v in m_vertices.Keys) | 374 | foreach (Vertex v in m_vertices.Keys) |
300 | { | 375 | { |
301 | if (v == null) | 376 | if (v == null) |