29 using System.Collections.Generic;
30 using System.Diagnostics;
32 namespace OpenSim.
Region.PhysicsModules.ConvexDecompositionDotNet
61 public float[] mMin =
new float[3];
62 public float[] mMax =
new float[3];
70 mVolume = Concavity.computeMeshVolume(result.HullVertices, result.HullIndices);
72 mDiagonal = getBoundingRegion(result.
HullVertices, mMin, mMax);
74 float dx = mMax[0] - mMin[0];
75 float dy = mMax[1] - mMin[1];
76 float dz = mMax[2] - mMin[2];
98 return overlapAABB(mMin, mMax, h.
mMin, h.
mMax);
102 private static float getBoundingRegion(List<float3> points,
float[] bmin,
float[] bmax)
114 for (
int i = 1; i < points.Count; i++)
118 if (p[0] < bmin[0]) bmin[0] = p[0];
119 if (p[1] < bmin[1]) bmin[1] = p[1];
120 if (p[2] < bmin[2]) bmin[2] = p[2];
122 if (p[0] > bmax[0]) bmax[0] = p[0];
123 if (p[1] > bmax[1]) bmax[1] = p[1];
124 if (p[2] > bmax[2]) bmax[2] = p[2];
127 float dx = bmax[0] - bmin[0];
128 float dy = bmax[1] - bmin[1];
129 float dz = bmax[2] - bmin[2];
131 return (
float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
135 private static bool overlapAABB(
float[] bmin1,
float[] bmax1,
float[] bmin2,
float[] bmax2)
137 if (bmax2[0] < bmin1[0])
return false;
138 if (bmax2[1] < bmin1[1])
return false;
139 if (bmax2[2] < bmin1[2])
return false;
141 if (bmin2[0] > bmax1[0])
return false;
142 if (bmin2[1] > bmax1[1])
return false;
143 if (bmin2[2] > bmax1[2])
return false;
151 public List<CHull> mChulls =
new List<CHull>();
154 private int MAXDEPTH = 8;
155 private float CONCAVE_PERCENT = 1f;
156 private float MERGE_PERCENT = 2f;
160 mCallback = callback;
166 for (i = 0; i < mChulls.Count; i++)
168 CHull cr = mChulls[i];
173 public bool isDuplicate(uint i1, uint i2, uint i3, uint ci1, uint ci2, uint ci3)
177 Debug.Assert(i1 != i2 && i1 != i3 && i2 != i3);
178 Debug.Assert(ci1 != ci2 && ci1 != ci3 && ci2 != ci3);
180 if (i1 == ci1 || i1 == ci2 || i1 == ci3)
182 if (i2 == ci1 || i2 == ci2 || i2 == ci3)
184 if (i3 == ci1 || i3 == ci2 || i3 == ci3)
192 List<int> src = cr.HullIndices;
194 for (
int i = 0; i < src.Count / 3; i++)
196 int i1 = src[i * 3 + 0];
197 int i2 = src[i * 3 + 1];
198 int i3 = src[i * 3 + 2];
200 float3 p1 = cr.HullVertices[i1];
201 float3 p2 = cr.HullVertices[i2];
202 float3 p3 = cr.HullVertices[i3];
204 i1 = vc.getIndex(p1);
205 i2 = vc.getIndex(p2);
206 i3 = vc.getIndex(p3);
222 List<int> indices =
new List<int>();
224 getMesh(a.
mResult, vc, indices);
225 getMesh(b.
mResult, vc, indices);
227 int vcount = vc.GetSize();
228 List<float3> vertices = vc.GetVertices();
229 int tcount = indices.Count / 3;
241 desc.SetHullFlag(HullFlag.QF_TRIANGLES);
242 desc.Vertices = vertices;
244 HullError hret = HullUtils.CreateConvexHull(desc, ref hresult);
248 float combineVolume = Concavity.computeMeshVolume(hresult.OutputVertices, hresult.Indices);
249 float sumVolume = a.mVolume + b.mVolume;
251 float percent = (sumVolume * 100) / combineVolume;
252 if (percent >= (100.0f - MERGE_PERCENT))
265 bool combine =
false;
269 List<CHull> output =
new List<CHull>();
272 for (i = 0; i < mChulls.Count && !combine; ++i)
274 CHull cr = mChulls[i];
277 for (j = 0; j < mChulls.Count; j++)
279 CHull match = mChulls[j];
284 CHull merge = canMerge(cr, match);
291 while (i != mChulls.Count)
293 CHull cr2 = mChulls[i];
333 MAXDEPTH = (int)desc.
mDepth;
337 ConvexDecomposition.calcConvexDecomposition(desc.
mVertices, desc.
mIndices, ConvexDecompResult, 0f, 0, MAXDEPTH, CONCAVE_PERCENT, MERGE_PERCENT);
339 while (combineHulls())
343 for (i = 0; i < mChulls.Count; i++)
345 CHull cr = mChulls[i];
355 hdesc.SetHullFlag(HullFlag.QF_TRIANGLES);
357 hdesc.Vertices = c.HullVertices;
358 hdesc.MaxVertices = desc.mMaxVertices;
362 hdesc.SkinWidth = desc.mSkinWidth;
363 hdesc.SetHullFlag(HullFlag.QF_SKIN_WIDTH);
366 HullError ret2 = HullUtils.CreateConvexHull(hdesc, ref result);
372 r.mHullVolume = Concavity.computeMeshVolume(result.OutputVertices, result.Indices);
408 hulls.Sort(delegate(
CHull a,
CHull b) {
return a.mVolume.CompareTo(b.mVolume); });
CHull canMerge(CHull a, CHull b)
void ConvexDecompResult(ConvexResult result)
int process(DecompDesc desc)
delegate void ConvexDecompositionCallback(ConvexResult result)
void sortChulls(List< CHull > hulls)
CHull(ConvexResult result)
List< float3 > OutputVertices
List< float3 > HullVertices
void getMesh(ConvexResult cr, VertexPool vc, List< int > indices)
bool isDuplicate(uint i1, uint i2, uint i3, uint ci1, uint ci2, uint ci3)
ConvexDecompositionCallback mCallback
ConvexBuilder(ConvexDecompositionCallback callback)