29 using System.Collections.Generic;
30 using System.Diagnostics;
32 namespace OpenSim.
Region.PhysicsModules.ConvexDecompositionDotNet
44 public FaceTri(List<float3> vertices,
int i1,
int i2,
int i3)
46 P1 =
new float3(vertices[i1]);
47 P2 =
new float3(vertices[i2]);
48 P3 =
new float3(vertices[i3]);
52 public static class ConvexDecomposition
54 private static void addTri(VertexPool vl, List<int> list, float3 p1, float3 p2, float3 p3)
56 int i1 = vl.getIndex(p1);
57 int i2 = vl.getIndex(p2);
58 int i3 = vl.getIndex(p3);
61 if ( i1 != i2 && i1 != i3 && i2 != i3 )
69 public static void calcConvexDecomposition(List<float3> vertices, List<int> indices,
ConvexDecompositionCallback callback,
float masterVolume,
int depth,
70 int maxDepth,
float concavePercent,
float mergePercent)
72 float4 plane =
new float4();
78 float c = Concavity.computeConcavity(vertices, indices, ref plane, ref volume);
82 masterVolume = volume;
85 float percent = (c * 100.0f) / masterVolume;
87 if (percent > concavePercent)
93 if (depth >= maxDepth || !split)
95 HullResult result =
new HullResult();
96 HullDesc desc =
new HullDesc();
98 desc.SetHullFlag(HullFlag.QF_TRIANGLES);
100 desc.Vertices = vertices;
102 HullError ret = HullUtils.CreateConvexHull(desc, ref result);
106 ConvexResult r =
new ConvexResult(result.OutputVertices, result.Indices);
113 List<int> ifront =
new List<int>();
114 List<int> iback =
new List<int>();
116 VertexPool vfront =
new VertexPool();
117 VertexPool vback =
new VertexPool();
120 for (
int i = 0; i < indices.Count / 3; i++)
122 int i1 = indices[i * 3 + 0];
123 int i2 = indices[i * 3 + 1];
124 int i3 = indices[i * 3 + 2];
126 FaceTri t =
new FaceTri(vertices, i1, i2, i3);
128 float3[] front =
new float3[4];
129 float3[] back =
new float3[4];
134 PlaneTriResult result = PlaneTri.planeTriIntersection(plane, t, 0.00001f, ref front, out fcount, ref back, out bcount);
136 if (fcount > 4 || bcount > 4)
138 result = PlaneTri.planeTriIntersection(plane, t, 0.00001f, ref front, out fcount, ref back, out bcount);
143 case PlaneTriResult.PTR_FRONT:
144 Debug.Assert(fcount == 3);
145 addTri(vfront, ifront, front[0], front[1], front[2]);
147 case PlaneTriResult.PTR_BACK:
148 Debug.Assert(bcount == 3);
149 addTri(vback, iback, back[0], back[1], back[2]);
151 case PlaneTriResult.PTR_SPLIT:
152 Debug.Assert(fcount >= 3 && fcount <= 4);
153 Debug.Assert(bcount >= 3 && bcount <= 4);
155 addTri(vfront, ifront, front[0], front[1], front[2]);
156 addTri(vback, iback, back[0], back[1], back[2]);
160 addTri(vfront, ifront, front[0], front[2], front[3]);
165 addTri(vback, iback, back[0], back[2], back[3]);
173 if (ifront.Count > 0)
175 int vcount = vfront.GetSize();
176 List<float3> vertices2 = vfront.GetVertices();
177 for (
int i = 0; i < vertices2.Count; i++)
178 vertices2[i] =
new float3(vertices2[i]);
179 int tcount = ifront.Count / 3;
181 calcConvexDecomposition(vertices2, ifront, callback, masterVolume, depth + 1, maxDepth, concavePercent, mergePercent);
189 int vcount = vback.GetSize();
190 List<float3> vertices2 = vback.GetVertices();
191 int tcount = iback.Count / 3;
193 calcConvexDecomposition(vertices2, iback, callback, masterVolume, depth + 1, maxDepth, concavePercent, mergePercent);
delegate void ConvexDecompositionCallback(ConvexResult result)
FaceTri(List< float3 > vertices, int i1, int i2, int i3)