Applying Mantis #4079. Thank you, dslake

remotes/origin/0.6.7-post-fixes
Melanie 2009-09-01 02:17:08 +01:00
parent 29df190a3f
commit c89fc36f28
2 changed files with 57 additions and 109 deletions

View File

@ -36,8 +36,8 @@ namespace OpenSim.Region.Physics.Meshing
{ {
public class Mesh : IMesh public class Mesh : IMesh
{ {
public List<Vertex> vertices; private Dictionary<Vertex, int> vertices;
public List<Triangle> triangles; private List<Triangle> triangles;
GCHandle pinnedVirtexes; GCHandle pinnedVirtexes;
GCHandle pinnedIndex; GCHandle pinnedIndex;
public PrimMesh primMesh = null; public PrimMesh primMesh = null;
@ -45,7 +45,7 @@ namespace OpenSim.Region.Physics.Meshing
public Mesh() public Mesh()
{ {
vertices = new List<Vertex>(); vertices = new Dictionary<Vertex, int>();
triangles = new List<Triangle>(); triangles = new List<Triangle>();
} }
@ -53,23 +53,9 @@ namespace OpenSim.Region.Physics.Meshing
{ {
Mesh result = new Mesh(); 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) foreach (Triangle t in triangles)
{ {
int iV1, iV2, iV3; result.Add(new Triangle(t.v1.Clone(), t.v2.Clone(), t.v3.Clone()));
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);
} }
return result; return result;
@ -77,52 +63,17 @@ namespace OpenSim.Region.Physics.Meshing
public void Add(Triangle triangle) public void Add(Triangle triangle)
{ {
int i; // If a vertex of the triangle is not yet in the vertices list,
i = vertices.IndexOf(triangle.v1); // add it and set its index to the current index count
if (i < 0) if( !vertices.ContainsKey(triangle.v1) )
throw new ArgumentException("Vertex v1 not known to mesh"); vertices[triangle.v1] = vertices.Count;
i = vertices.IndexOf(triangle.v2); if (!vertices.ContainsKey(triangle.v2))
if (i < 0) vertices[triangle.v2] = vertices.Count;
throw new ArgumentException("Vertex v2 not known to mesh"); if (!vertices.ContainsKey(triangle.v3))
i = vertices.IndexOf(triangle.v3); vertices[triangle.v3] = vertices.Count;
if (i < 0)
throw new ArgumentException("Vertex v3 not known to mesh");
triangles.Add(triangle); 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<Vertex> lv)
{
foreach (Vertex v in lv)
{
vertices.Add(v);
}
}
public void CalcNormals() public void CalcNormals()
{ {
int iTriangles = triangles.Count; int iTriangles = triangles.Count;
@ -188,7 +139,7 @@ namespace OpenSim.Region.Physics.Meshing
public List<PhysicsVector> getVertexList() public List<PhysicsVector> getVertexList()
{ {
List<PhysicsVector> result = new List<PhysicsVector>(); List<PhysicsVector> result = new List<PhysicsVector>();
foreach (Vertex v in vertices) foreach (Vertex v in vertices.Keys)
{ {
result.Add(v); result.Add(v);
} }
@ -201,12 +152,13 @@ namespace OpenSim.Region.Physics.Meshing
if (primMesh == null) if (primMesh == null)
{ {
//m_log.WarnFormat("vertices.Count = {0}", vertices.Count);
result = new float[vertices.Count * 3]; result = new float[vertices.Count * 3];
for (int i = 0; i < vertices.Count; i++) foreach(KeyValuePair<Vertex, int> kvp in vertices)
{ {
Vertex v = vertices[i]; Vertex v = kvp.Key;
if (v == null) int i = kvp.Value;
continue; //m_log.WarnFormat("kvp.Value = {0}", i);
result[3 * i + 0] = v.X; result[3 * i + 0] = v.X;
result[3 * i + 1] = v.Y; result[3 * i + 1] = v.Y;
result[3 * i + 2] = v.Z; result[3 * i + 2] = v.Z;
@ -243,9 +195,9 @@ namespace OpenSim.Region.Physics.Meshing
for (int i = 0; i < triangles.Count; i++) for (int i = 0; i < triangles.Count; i++)
{ {
Triangle t = triangles[i]; Triangle t = triangles[i];
result[3 * i + 0] = vertices.IndexOf(t.v1); result[3 * i + 0] = vertices[t.v1];
result[3 * i + 1] = vertices.IndexOf(t.v2); result[3 * i + 1] = vertices[t.v2];
result[3 * i + 2] = vertices.IndexOf(t.v3); result[3 * i + 2] = vertices[t.v3];
} }
} }
else else
@ -298,27 +250,17 @@ namespace OpenSim.Region.Physics.Meshing
public void Append(IMesh newMesh) public void Append(IMesh newMesh)
{ {
Mesh newMesh2; if (!(newMesh is Mesh))
if (newMesh is Mesh)
{
newMesh2 = (Mesh)newMesh;
}
else
{
return; return;
}
foreach (Vertex v in newMesh2.vertices) foreach (Triangle t in ((Mesh)newMesh).triangles)
vertices.Add(v);
foreach (Triangle t in newMesh2.triangles)
Add(t); Add(t);
} }
// Do a linear transformation of mesh. // Do a linear transformation of mesh.
public void TransformLinear(float[,] matrix, float[] offset) public void TransformLinear(float[,] matrix, float[] offset)
{ {
foreach (Vertex v in vertices) foreach (Vertex v in vertices.Keys)
{ {
if (v == null) if (v == null)
continue; continue;
@ -346,5 +288,10 @@ namespace OpenSim.Region.Physics.Meshing
} }
sw.Close(); sw.Close();
} }
public void TrimExcess()
{
triangles.TrimExcess();
}
} }
} }

View File

@ -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) private static Mesh CreateSimpleBoxMesh(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
{ {
Mesh box = new Mesh(); Mesh box = new Mesh();
List<Vertex> vertices = new List<Vertex>();
// bottom // bottom
box.Add(new Vertex(minX, maxY, minZ)); vertices.Add(new Vertex(minX, maxY, minZ));
box.Add(new Vertex(maxX, maxY, minZ)); vertices.Add(new Vertex(maxX, maxY, minZ));
box.Add(new Vertex(maxX, minY, minZ)); vertices.Add(new Vertex(maxX, minY, minZ));
box.Add(new Vertex(minX, 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(vertices[0], vertices[1], vertices[2]));
box.Add(new Triangle(box.vertices[0], box.vertices[2], box.vertices[3])); box.Add(new Triangle(vertices[0], vertices[2], vertices[3]));
// top // top
box.Add(new Vertex(maxX, maxY, maxZ)); vertices.Add(new Vertex(maxX, maxY, maxZ));
box.Add(new Vertex(minX, maxY, maxZ)); vertices.Add(new Vertex(minX, maxY, maxZ));
box.Add(new Vertex(minX, minY, maxZ)); vertices.Add(new Vertex(minX, minY, maxZ));
box.Add(new Vertex(maxX, 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(vertices[4], vertices[5], vertices[6]));
box.Add(new Triangle(box.vertices[4], box.vertices[6], box.vertices[7])); box.Add(new Triangle(vertices[4], vertices[6], vertices[7]));
// sides // sides
box.Add(new Triangle(box.vertices[5], box.vertices[0], box.vertices[3])); box.Add(new Triangle(vertices[5], vertices[0], vertices[3]));
box.Add(new Triangle(box.vertices[5], box.vertices[3], box.vertices[6])); 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(vertices[1], vertices[0], vertices[5]));
box.Add(new Triangle(box.vertices[1], box.vertices[5], box.vertices[4])); 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(vertices[7], vertices[1], vertices[4]));
box.Add(new Triangle(box.vertices[7], box.vertices[2], box.vertices[1])); 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(vertices[3], vertices[2], vertices[7]));
box.Add(new Triangle(box.vertices[3], box.vertices[7], box.vertices[6])); box.Add(new Triangle(vertices[3], vertices[7], vertices[6]));
return box; return box;
} }
@ -145,7 +145,7 @@ namespace OpenSim.Region.Physics.Meshing
float minZ = float.MaxValue; float minZ = float.MaxValue;
float maxZ = float.MinValue; float maxZ = float.MinValue;
foreach (Vertex v in meshIn.vertices) foreach (Vertex v in meshIn.getVertexList())
{ {
if (v != null) if (v != null)
{ {
@ -394,17 +394,19 @@ namespace OpenSim.Region.Physics.Meshing
int numCoords = coords.Count; int numCoords = coords.Count;
int numFaces = faces.Count; int numFaces = faces.Count;
// Create the list of vertices
List<Vertex> vertices = new List<Vertex>();
for (int i = 0; i < numCoords; i++) for (int i = 0; i < numCoords; i++)
{ {
Coord c = coords[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<Vertex> vertices = mesh.vertices; // Add the corresponding triangles to the mesh
for (int i = 0; i < numFaces; i++) for (int i = 0; i < numFaces; i++)
{ {
Face f = faces[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; return mesh;
@ -438,8 +440,7 @@ namespace OpenSim.Region.Physics.Meshing
} }
// trim the vertex and triangle lists to free up memory // trim the vertex and triangle lists to free up memory
mesh.vertices.TrimExcess(); mesh.TrimExcess();
mesh.triangles.TrimExcess();
} }
return mesh; return mesh;