diff --git a/OpenSim/Region/Physics/Meshing/Mesh.cs b/OpenSim/Region/Physics/Meshing/Mesh.cs index 1134c63130..b19cf706c8 100644 --- a/OpenSim/Region/Physics/Meshing/Mesh.cs +++ b/OpenSim/Region/Physics/Meshing/Mesh.cs @@ -36,8 +36,8 @@ namespace OpenSim.Region.Physics.Meshing { public class Mesh : IMesh { - public List vertices; - public List triangles; + private Dictionary vertices; + private List triangles; GCHandle pinnedVirtexes; GCHandle pinnedIndex; public PrimMesh primMesh = null; @@ -45,7 +45,7 @@ namespace OpenSim.Region.Physics.Meshing public Mesh() { - vertices = new List(); + vertices = new Dictionary(); triangles = new List(); } @@ -53,23 +53,9 @@ namespace OpenSim.Region.Physics.Meshing { Mesh result = new Mesh(); - foreach (Vertex v in vertices) - { - if (v == null) - result.vertices.Add(null); - else - result.vertices.Add(v.Clone()); - } - foreach (Triangle t in triangles) { - int iV1, iV2, iV3; - iV1 = vertices.IndexOf(t.v1); - iV2 = vertices.IndexOf(t.v2); - iV3 = vertices.IndexOf(t.v3); - - Triangle newT = new Triangle(result.vertices[iV1], result.vertices[iV2], result.vertices[iV3]); - result.Add(newT); + result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone())); } return result; @@ -77,52 +63,17 @@ namespace OpenSim.Region.Physics.Meshing public void Add(Triangle triangle) { - int i; - i = vertices.IndexOf(triangle.v1); - if (i < 0) - throw new ArgumentException("Vertex v1 not known to mesh"); - i = vertices.IndexOf(triangle.v2); - if (i < 0) - throw new ArgumentException("Vertex v2 not known to mesh"); - i = vertices.IndexOf(triangle.v3); - if (i < 0) - throw new ArgumentException("Vertex v3 not known to mesh"); - + // If a vertex of the triangle is not yet in the vertices list, + // add it and set its index to the current index count + if( !vertices.ContainsKey(triangle.v1) ) + vertices[triangle.v1] = vertices.Count; + if (!vertices.ContainsKey(triangle.v2)) + vertices[triangle.v2] = vertices.Count; + if (!vertices.ContainsKey(triangle.v3)) + vertices[triangle.v3] = vertices.Count; triangles.Add(triangle); } - public void Add(Vertex v) - { - vertices.Add(v); - } - - public void Remove(Vertex v) - { - int i; - - // First, remove all triangles that are build on v - for (i = 0; i < triangles.Count; i++) - { - Triangle t = triangles[i]; - if (t.v1 == v || t.v2 == v || t.v3 == v) - { - triangles.RemoveAt(i); - i--; - } - } - - // Second remove v itself - vertices.Remove(v); - } - - public void Add(List lv) - { - foreach (Vertex v in lv) - { - vertices.Add(v); - } - } - public void CalcNormals() { int iTriangles = triangles.Count; @@ -188,7 +139,7 @@ namespace OpenSim.Region.Physics.Meshing public List getVertexList() { List result = new List(); - foreach (Vertex v in vertices) + foreach (Vertex v in vertices.Keys) { result.Add(v); } @@ -201,12 +152,13 @@ namespace OpenSim.Region.Physics.Meshing if (primMesh == null) { + //m_log.WarnFormat("vertices.Count = {0}", vertices.Count); result = new float[vertices.Count * 3]; - for (int i = 0; i < vertices.Count; i++) + foreach(KeyValuePair kvp in vertices) { - Vertex v = vertices[i]; - if (v == null) - continue; + Vertex v = kvp.Key; + int i = kvp.Value; + //m_log.WarnFormat("kvp.Value = {0}", i); result[3 * i + 0] = v.X; result[3 * i + 1] = v.Y; result[3 * i + 2] = v.Z; @@ -243,9 +195,9 @@ namespace OpenSim.Region.Physics.Meshing for (int i = 0; i < triangles.Count; i++) { Triangle t = triangles[i]; - result[3 * i + 0] = vertices.IndexOf(t.v1); - result[3 * i + 1] = vertices.IndexOf(t.v2); - result[3 * i + 2] = vertices.IndexOf(t.v3); + result[3 * i + 0] = vertices[t.v1]; + result[3 * i + 1] = vertices[t.v2]; + result[3 * i + 2] = vertices[t.v3]; } } else @@ -298,27 +250,17 @@ namespace OpenSim.Region.Physics.Meshing public void Append(IMesh newMesh) { - Mesh newMesh2; - if (newMesh is Mesh) - { - newMesh2 = (Mesh)newMesh; - } - else - { + if (!(newMesh is Mesh)) return; - } - foreach (Vertex v in newMesh2.vertices) - vertices.Add(v); - - foreach (Triangle t in newMesh2.triangles) + foreach (Triangle t in ((Mesh)newMesh).triangles) Add(t); } // Do a linear transformation of mesh. public void TransformLinear(float[,] matrix, float[] offset) { - foreach (Vertex v in vertices) + foreach (Vertex v in vertices.Keys) { if (v == null) continue; @@ -346,5 +288,10 @@ namespace OpenSim.Region.Physics.Meshing } sw.Close(); } + + public void TrimExcess() + { + triangles.TrimExcess(); + } } } diff --git a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs index 85c526b4ee..7b3f8f2e48 100644 --- a/OpenSim/Region/Physics/Meshing/Meshmerizer.cs +++ b/OpenSim/Region/Physics/Meshing/Meshmerizer.cs @@ -92,40 +92,40 @@ namespace OpenSim.Region.Physics.Meshing private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) { Mesh box = new Mesh(); - + List vertices = new List(); // bottom - box.Add(new Vertex(minX, maxY, minZ)); - box.Add(new Vertex(maxX, maxY, minZ)); - box.Add(new Vertex(maxX, minY, minZ)); - box.Add(new Vertex(minX, minY, minZ)); + vertices.Add(new Vertex(minX, maxY, minZ)); + vertices.Add(new Vertex(maxX, maxY, minZ)); + vertices.Add(new Vertex(maxX, minY, minZ)); + vertices.Add(new Vertex(minX, minY, minZ)); - box.Add(new Triangle(box.vertices[0], box.vertices[1], box.vertices[2])); - box.Add(new Triangle(box.vertices[0], box.vertices[2], box.vertices[3])); + box.Add(new Triangle(vertices[0], vertices[1], vertices[2])); + box.Add(new Triangle(vertices[0], vertices[2], vertices[3])); // top - box.Add(new Vertex(maxX, maxY, maxZ)); - box.Add(new Vertex(minX, maxY, maxZ)); - box.Add(new Vertex(minX, minY, maxZ)); - box.Add(new Vertex(maxX, minY, maxZ)); + vertices.Add(new Vertex(maxX, maxY, maxZ)); + vertices.Add(new Vertex(minX, maxY, maxZ)); + vertices.Add(new Vertex(minX, minY, maxZ)); + vertices.Add(new Vertex(maxX, minY, maxZ)); - box.Add(new Triangle(box.vertices[4], box.vertices[5], box.vertices[6])); - box.Add(new Triangle(box.vertices[4], box.vertices[6], box.vertices[7])); + box.Add(new Triangle(vertices[4], vertices[5], vertices[6])); + box.Add(new Triangle(vertices[4], vertices[6], vertices[7])); // sides - box.Add(new Triangle(box.vertices[5], box.vertices[0], box.vertices[3])); - box.Add(new Triangle(box.vertices[5], box.vertices[3], box.vertices[6])); + box.Add(new Triangle(vertices[5], vertices[0], vertices[3])); + box.Add(new Triangle(vertices[5], vertices[3], vertices[6])); - box.Add(new Triangle(box.vertices[1], box.vertices[0], box.vertices[5])); - box.Add(new Triangle(box.vertices[1], box.vertices[5], box.vertices[4])); + box.Add(new Triangle(vertices[1], vertices[0], vertices[5])); + box.Add(new Triangle(vertices[1], vertices[5], vertices[4])); - box.Add(new Triangle(box.vertices[7], box.vertices[1], box.vertices[4])); - box.Add(new Triangle(box.vertices[7], box.vertices[2], box.vertices[1])); + box.Add(new Triangle(vertices[7], vertices[1], vertices[4])); + box.Add(new Triangle(vertices[7], vertices[2], vertices[1])); - box.Add(new Triangle(box.vertices[3], box.vertices[2], box.vertices[7])); - box.Add(new Triangle(box.vertices[3], box.vertices[7], box.vertices[6])); + box.Add(new Triangle(vertices[3], vertices[2], vertices[7])); + box.Add(new Triangle(vertices[3], vertices[7], vertices[6])); return box; } @@ -145,7 +145,7 @@ namespace OpenSim.Region.Physics.Meshing float minZ = float.MaxValue; float maxZ = float.MinValue; - foreach (Vertex v in meshIn.vertices) + foreach (Vertex v in meshIn.getVertexList()) { if (v != null) { @@ -394,17 +394,19 @@ namespace OpenSim.Region.Physics.Meshing int numCoords = coords.Count; int numFaces = faces.Count; + // Create the list of vertices + List vertices = new List(); for (int i = 0; i < numCoords; i++) { Coord c = coords[i]; - mesh.vertices.Add(new Vertex(c.X, c.Y, c.Z)); + vertices.Add(new Vertex(c.X, c.Y, c.Z)); } - List vertices = mesh.vertices; + // Add the corresponding triangles to the mesh for (int i = 0; i < numFaces; i++) { Face f = faces[i]; - mesh.triangles.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3])); + mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3])); } return mesh; @@ -438,8 +440,7 @@ namespace OpenSim.Region.Physics.Meshing } // trim the vertex and triangle lists to free up memory - mesh.vertices.TrimExcess(); - mesh.triangles.TrimExcess(); + mesh.TrimExcess(); } return mesh;