minor changes to ubMeshmerizer memory use

melanie
UbitUmarov 2016-11-17 13:57:45 +00:00
parent 3d78388e55
commit 2db22bf064
2 changed files with 66 additions and 67 deletions

View File

@ -39,6 +39,24 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
{ {
public class MeshBuildingData public class MeshBuildingData
{ {
private class vertexcomp : IEqualityComparer<Vertex>
{
public bool Equals(Vertex v1, Vertex v2)
{
if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z)
return true;
else
return false;
}
public int GetHashCode(Vertex v)
{
int a = v.X.GetHashCode();
int b = v.Y.GetHashCode();
int c = v.Z.GetHashCode();
return (a << 16) ^ (b << 8) ^ c;
}
}
public Dictionary<Vertex, int> m_vertices; public Dictionary<Vertex, int> m_vertices;
public List<Triangle> m_triangles; public List<Triangle> m_triangles;
public float m_obbXmin; public float m_obbXmin;
@ -49,6 +67,21 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
public float m_obbZmax; public float m_obbZmax;
public Vector3 m_centroid; public Vector3 m_centroid;
public int m_centroidDiv; public int m_centroidDiv;
public MeshBuildingData()
{
vertexcomp vcomp = new vertexcomp();
m_vertices = new Dictionary<Vertex, int>(vcomp);
m_triangles = new List<Triangle>();
m_centroid = Vector3.Zero;
m_centroidDiv = 0;
m_obbXmin = float.MaxValue;
m_obbXmax = float.MinValue;
m_obbYmin = float.MaxValue;
m_obbYmax = float.MinValue;
m_obbZmin = float.MaxValue;
m_obbZmax = float.MinValue;
}
} }
[Serializable()] [Serializable()]
@ -76,50 +109,20 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
public int RefCount { get; set; } public int RefCount { get; set; }
public AMeshKey Key { get; set; } public AMeshKey Key { get; set; }
private class vertexcomp : IEqualityComparer<Vertex> public Mesh(bool forbuild)
{ {
public bool Equals(Vertex v1, Vertex v2) if(forbuild)
{
if (v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z)
return true;
else
return false;
}
public int GetHashCode(Vertex v)
{
int a = v.X.GetHashCode();
int b = v.Y.GetHashCode();
int c = v.Z.GetHashCode();
return (a << 16) ^ (b << 8) ^ c;
}
}
public Mesh()
{
vertexcomp vcomp = new vertexcomp();
m_bdata = new MeshBuildingData(); m_bdata = new MeshBuildingData();
m_bdata.m_vertices = new Dictionary<Vertex, int>(vcomp);
m_bdata.m_triangles = new List<Triangle>();
m_bdata.m_centroid = Vector3.Zero;
m_bdata.m_centroidDiv = 0;
m_bdata.m_obbXmin = float.MaxValue;
m_bdata.m_obbXmax = float.MinValue;
m_bdata.m_obbYmin = float.MaxValue;
m_bdata.m_obbYmax = float.MinValue;
m_bdata.m_obbZmin = float.MaxValue;
m_bdata.m_obbZmax = float.MinValue;
m_obb = new Vector3(0.5f, 0.5f, 0.5f); m_obb = new Vector3(0.5f, 0.5f, 0.5f);
m_obboffset = Vector3.Zero; m_obboffset = Vector3.Zero;
} }
public Mesh Scale(Vector3 scale) public Mesh Scale(Vector3 scale)
{ {
if (m_verticesPtr == null || m_indicesPtr == null) if (m_verticesPtr == null || m_indicesPtr == null)
return null; return null;
Mesh result = new Mesh(); Mesh result = new Mesh(false);
float x = scale.X; float x = scale.X;
float y = scale.Y; float y = scale.Y;
@ -167,7 +170,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
public Mesh Clone() public Mesh Clone()
{ {
Mesh result = new Mesh(); Mesh result = new Mesh(false);
if (m_bdata != null) if (m_bdata != null)
{ {
@ -514,8 +517,6 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
if (m_indicesPtr == IntPtr.Zero) if (m_indicesPtr == IntPtr.Zero)
indexes = getIndexListAsInt(); indexes = getIndexListAsInt();
pinMemory();
float x, y, z; float x, y, z;
if (m_bdata.m_centroidDiv > 0) if (m_bdata.m_centroidDiv > 0)
@ -543,18 +544,20 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
m_obb = new Vector3(x, y, z); m_obb = new Vector3(x, y, z);
releaseBuildingMeshData(); releaseBuildingMeshData();
pinMemory();
} }
public bool ToStream(Stream st) public bool ToStream(Stream st)
{ {
if (m_indicesPtr == IntPtr.Zero || m_verticesPtr == IntPtr.Zero) if (m_indicesPtr == IntPtr.Zero || m_verticesPtr == IntPtr.Zero)
return false; return false;
BinaryWriter bw = new BinaryWriter(st);
bool ok = true; bool ok = true;
try try
{ {
using(BinaryWriter bw = new BinaryWriter(st))
{
bw.Write(m_vertexCount); bw.Write(m_vertexCount);
bw.Write(m_indexCount); bw.Write(m_indexCount);
@ -568,30 +571,26 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
bw.Write(m_obboffset.X); bw.Write(m_obboffset.X);
bw.Write(m_obboffset.Y); bw.Write(m_obboffset.Y);
bw.Write(m_obboffset.Z); bw.Write(m_obboffset.Z);
bw.Flush();
bw.Close();
}
} }
catch catch
{ {
ok = false; ok = false;
} }
if (bw != null)
{
bw.Flush();
bw.Close();
}
return ok; return ok;
} }
public static Mesh FromStream(Stream st, AMeshKey key) public static Mesh FromStream(Stream st, AMeshKey key)
{ {
Mesh mesh = new Mesh(); Mesh mesh = new Mesh(false);
mesh.releaseBuildingMeshData();
bool ok = true; bool ok = true;
using(BinaryReader br = new BinaryReader(st))
{
try try
{
using(BinaryReader br = new BinaryReader(st))
{ {
mesh.m_vertexCount = br.ReadInt32(); mesh.m_vertexCount = br.ReadInt32();
mesh.m_indexCount = br.ReadInt32(); mesh.m_indexCount = br.ReadInt32();
@ -613,11 +612,11 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
mesh.m_obboffset.Y = br.ReadSingle(); mesh.m_obboffset.Y = br.ReadSingle();
mesh.m_obboffset.Z = br.ReadSingle(); mesh.m_obboffset.Z = br.ReadSingle();
} }
}
catch catch
{ {
ok = false; ok = false;
} }
}
if (ok) if (ok)
{ {

View File

@ -182,7 +182,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
/// <returns></returns> /// <returns></returns>
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(true);
List<Vertex> vertices = new List<Vertex>(); List<Vertex> vertices = new List<Vertex>();
// bottom // bottom
@ -357,7 +357,7 @@ namespace OpenSim.Region.PhysicsModule.ubODEMeshing
int numCoords = coords.Count; int numCoords = coords.Count;
int numFaces = faces.Count; int numFaces = faces.Count;
Mesh mesh = new Mesh(); Mesh mesh = new Mesh(true);
// Add the corresponding triangles to the mesh // Add the corresponding triangles to the mesh
for (int i = 0; i < numFaces; i++) for (int i = 0; i < numFaces; i++)
{ {