[csharp] Ported ConvexDecomposer

This commit is contained in:
badlogic 2017-04-19 14:41:16 +02:00
parent 63c3a48014
commit 686b2b551d
2 changed files with 203 additions and 204 deletions

View File

@ -71,6 +71,7 @@
<Compile Include="src\BlendMode.cs" /> <Compile Include="src\BlendMode.cs" />
<Compile Include="src\Bone.cs" /> <Compile Include="src\Bone.cs" />
<Compile Include="src\BoneData.cs" /> <Compile Include="src\BoneData.cs" />
<Compile Include="src\ConvexDecomposer.cs" />
<Compile Include="src\Event.cs" /> <Compile Include="src\Event.cs" />
<Compile Include="src\EventData.cs" /> <Compile Include="src\EventData.cs" />
<Compile Include="src\ExposedList.cs" /> <Compile Include="src\ExposedList.cs" />
@ -85,6 +86,7 @@
<Compile Include="src\Skeleton.cs" /> <Compile Include="src\Skeleton.cs" />
<Compile Include="src\SkeletonBinary.cs" /> <Compile Include="src\SkeletonBinary.cs" />
<Compile Include="src\SkeletonBounds.cs" /> <Compile Include="src\SkeletonBounds.cs" />
<Compile Include="src\SkeletonClipping.cs" />
<Compile Include="src\SkeletonData.cs" /> <Compile Include="src\SkeletonData.cs" />
<Compile Include="src\SkeletonJson.cs" /> <Compile Include="src\SkeletonJson.cs" />
<Compile Include="src\Skin.cs" /> <Compile Include="src\Skin.cs" />

View File

@ -28,241 +28,238 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
using System;
namespace Spine { namespace Spine {
internal class ConvexDecomposer { internal class ConvexDecomposer {
// private final Array<FloatArray> convexPolygons = new Array(); private readonly ExposedList<ExposedList<float>> convexPolygons = new ExposedList<ExposedList<float>>();
// private final Array<ShortArray> convexPolygonsIndices = new Array(); private readonly ExposedList<ExposedList<short>> convexPolygonsIndices = new ExposedList<ExposedList<short>>();
// private final ShortArray indicesArray = new ShortArray(); private readonly ExposedList<short> indicesArray = new ExposedList<short>();
// private final BooleanArray isConcaveArray = new BooleanArray(); private readonly ExposedList<bool> isConcaveArray = new ExposedList<bool>();
// private final ShortArray triangles = new ShortArray(); private readonly ExposedList<short> triangles = new ExposedList<short>();
// private final Pool<FloatArray> polygonPool = new Pool() { private readonly Pool<ExposedList<float>> polygonPool = new Pool<ExposedList<float>>();
// protected FloatArray newObject() { private readonly Pool<ExposedList<short>> polygonIndicesPool = new Pool<ExposedList<short>>();
// return new FloatArray(16);
// }
//};
//private final Pool<ShortArray> polygonIndicesPool = new Pool() { public ExposedList<ExposedList<float>> Decompose(float[] input) {
// protected ShortArray newObject() { var vertices = input;
// return new ShortArray(16); int vertexCount = input.Length >> 1;
//}
// };
// public Array<FloatArray> decompose(FloatArray input) { var indicesArray = this.indicesArray;
// float[] vertices = input.items; indicesArray.Clear();
// int vertexCount = input.size >> 1; short[] indices = indicesArray.Resize(vertexCount).Items;
for (short i = 0; i < vertexCount; i++)
indices[i] = i;
// ShortArray indicesArray = this.indicesArray; var isConcaveArray = this.isConcaveArray;
// indicesArray.clear(); bool[] isConcave = isConcaveArray.Resize(vertexCount).Items;
// short[] indices = indicesArray.setSize(vertexCount); for (int i = 0, n = vertexCount; i < n; ++i)
// for (short i = 0; i < vertexCount; i++) isConcave[i] = IsConcave(i, vertexCount, vertices, indices);
// indices[i] = i;
// BooleanArray isConcaveArray = this.isConcaveArray; var triangles = this.triangles;
// boolean[] isConcave = isConcaveArray.setSize(vertexCount); triangles.Clear();
// for (int i = 0, n = vertexCount; i < n; ++i) triangles.EnsureCapacity(Math.Max(0, vertexCount - 2) << 2);
// isConcave[i] = isConcave(i, vertexCount, vertices, indices);
// ShortArray triangles = this.triangles; while (vertexCount > 3) {
// triangles.clear(); // Find ear tip.
// triangles.ensureCapacity(Math.max(0, vertexCount - 2) << 2); int previous = vertexCount - 1, i = 0, next = 1;
while (true) {
outer:
if (!isConcave[i]) {
int p1 = indices[previous] << 1, p2 = indices[i] << 1, p3 = indices[next] << 1;
float p1x = vertices[p1], p1y = vertices[p1 + 1];
float p2x = vertices[p2], p2y = vertices[p2 + 1];
float p3x = vertices[p3], p3y = vertices[p3 + 1];
for (int ii = (next + 1) % vertexCount; ii != previous; ii = (ii + 1) % vertexCount) {
if (!isConcave[ii]) continue;
int v = indices[ii] << 1;
float vx = vertices[v], vy = vertices[v + 1];
if (PositiveArea(p3x, p3y, p1x, p1y, vx, vy)) {
if (PositiveArea(p1x, p1y, p2x, p2y, vx, vy)) {
if (PositiveArea(p2x, p2y, p3x, p3y, vx, vy)) goto outer;
}
}
}
break;
}
// while (vertexCount > 3) { if (next == 0) {
// // Find ear tip. do {
// int previous = vertexCount - 1, i = 0, next = 1; if (!isConcave[i]) break;
// while (true) { i--;
// outer: } while (i > 0);
// if (!isConcave[i]) { break;
// int p1 = indices[previous] << 1, p2 = indices[i] << 1, p3 = indices[next] << 1; }
// float p1x = vertices[p1], p1y = vertices[p1 + 1];
// float p2x = vertices[p2], p2y = vertices[p2 + 1];
// float p3x = vertices[p3], p3y = vertices[p3 + 1];
// for (int ii = (next + 1) % vertexCount; ii != previous; ii = (ii + 1) % vertexCount) {
// if (!isConcave[ii]) continue;
// int v = indices[ii] << 1;
// float vx = vertices[v], vy = vertices[v + 1];
// if (positiveArea(p3x, p3y, p1x, p1y, vx, vy)) {
// if (positiveArea(p1x, p1y, p2x, p2y, vx, vy)) {
// if (positiveArea(p2x, p2y, p3x, p3y, vx, vy)) break outer;
// }
// }
// }
// break;
// }
// if (next == 0) { previous = i;
// do { i = next;
// if (!isConcave[i]) break; next = (next + 1) % vertexCount;
// i--; }
// } while (i > 0);
// break;
// }
// previous = i; // Cut ear tip.
// i = next; triangles.Add(indices[(vertexCount + i - 1) % vertexCount]);
// next = (next + 1) % vertexCount; triangles.Add(indices[i]);
// } triangles.Add(indices[(i + 1) % vertexCount]);
indicesArray.RemoveAt(i);
isConcaveArray.RemoveAt(i);
vertexCount--;
// // Cut ear tip. int previousIndex = (vertexCount + i - 1) % vertexCount;
// triangles.add(indices[(vertexCount + i - 1) % vertexCount]); int nextIndex = i == vertexCount ? 0 : i;
// triangles.add(indices[i]); isConcave[previousIndex] = IsConcave(previousIndex, vertexCount, vertices, indices);
// triangles.add(indices[(i + 1) % vertexCount]); isConcave[nextIndex] = IsConcave(nextIndex, vertexCount, vertices, indices);
// indicesArray.removeIndex(i); }
// isConcaveArray.removeIndex(i);
// vertexCount--;
// int previousIndex = (vertexCount + i - 1) % vertexCount; if (vertexCount == 3) {
// int nextIndex = i == vertexCount ? 0 : i; triangles.Add(indices[2]);
// isConcave[previousIndex] = isConcave(previousIndex, vertexCount, vertices, indices); triangles.Add(indices[0]);
// isConcave[nextIndex] = isConcave(nextIndex, vertexCount, vertices, indices); triangles.Add(indices[1]);
// } }
// if (vertexCount == 3) { var convexPolygons = this.convexPolygons;
// triangles.add(indices[2]); for (int i = 0, n = convexPolygons.Count; i < n; i++) {
// triangles.add(indices[0]); polygonPool.Free(convexPolygons.Items[i]);
// triangles.add(indices[1]); }
// } convexPolygons.Clear();
// Array<FloatArray> convexPolygons = this.convexPolygons; var convexPolygonsIndices = this.convexPolygonsIndices;
// polygonPool.freeAll(convexPolygons); for (int i = 0, n = convexPolygonsIndices.Count; i < n; i++) {
// convexPolygons.clear(); polygonIndicesPool.Free(convexPolygonsIndices.Items[i]);
}
convexPolygonsIndices.Clear();
// Array<ShortArray> convexPolygonsIndices = this.convexPolygonsIndices; var polygonIndices = polygonIndicesPool.Obtain();
// polygonIndicesPool.freeAll(convexPolygonsIndices); polygonIndices.Clear();
// convexPolygonsIndices.clear();
// ShortArray polygonIndices = polygonIndicesPool.obtain(); var polygon = polygonPool.Obtain();
// polygonIndices.clear(); polygon.Clear();
// FloatArray polygon = polygonPool.obtain(); // Merge subsequent triangles if they form a triangle fan.
// polygon.clear(); int fanBaseIndex = -1, lastWinding = 0;
short[] trianglesItems = triangles.Items;
for (int i = 0, n = triangles.Count; i < n; i += 3) {
int t1 = trianglesItems[i] << 1, t2 = trianglesItems[i + 1] << 1, t3 = trianglesItems[i + 2] << 1;
float x1 = vertices[t1], y1 = vertices[t1 + 1];
float x2 = vertices[t2], y2 = vertices[t2 + 1];
float x3 = vertices[t3], y3 = vertices[t3 + 1];
// // Merge subsequent triangles if they form a triangle fan. // If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan).
// int fanBaseIndex = -1, lastWinding = 0; var merged = false;
// short[] trianglesItems = triangles.items; if (fanBaseIndex == t1) {
// for (int i = 0, n = triangles.size; i < n; i += 3) { int o = polygon.Count - 4;
// int t1 = trianglesItems[i] << 1, t2 = trianglesItems[i + 1] << 1, t3 = trianglesItems[i + 2] << 1; float[] p = polygon.Items;
// float x1 = vertices[t1], y1 = vertices[t1 + 1]; int winding1 = Winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3);
// float x2 = vertices[t2], y2 = vertices[t2 + 1]; int winding2 = Winding(x3, y3, p[0], p[1], p[2], p[3]);
// float x3 = vertices[t3], y3 = vertices[t3 + 1]; if (winding1 == lastWinding && winding2 == lastWinding) {
polygon.Add(x3);
polygon.Add(y3);
polygonIndices.Add((short)t3);
merged = true;
}
}
// // If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan). // Otherwise make this triangle the new base.
// boolean merged = false; if (!merged) {
// if (fanBaseIndex == t1) { if (polygon.Count > 0) {
// int o = polygon.size - 4; convexPolygons.Add(polygon);
// float[] p = polygon.items; convexPolygonsIndices.Add(polygonIndices);
// int winding1 = winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3); }
// int winding2 = winding(x3, y3, p[0], p[1], p[2], p[3]); polygon = polygonPool.Obtain();
// if (winding1 == lastWinding && winding2 == lastWinding) { polygon.Clear();
// polygon.add(x3); polygon.Add(x1);
// polygon.add(y3); polygon.Add(y1);
// polygonIndices.add(t3); polygon.Add(x2);
// merged = true; polygon.Add(y2);
// } polygon.Add(x3);
// } polygon.Add(y3);
polygonIndices = polygonIndicesPool.Obtain();
polygonIndices.Clear();
polygonIndices.Add((short)t1);
polygonIndices.Add((short)t2);
polygonIndices.Add((short)t3);
lastWinding = Winding(x1, y1, x2, y2, x3, y3);
fanBaseIndex = t1;
}
}
// // Otherwise make this triangle the new base. if (polygon.Count > 0) {
// if (!merged) { convexPolygons.Add(polygon);
// if (polygon.size > 0) { convexPolygonsIndices.Add(polygonIndices);
// convexPolygons.add(polygon); }
// convexPolygonsIndices.add(polygonIndices);
// }
// polygon = polygonPool.obtain();
// polygon.clear();
// polygon.add(x1);
// polygon.add(y1);
// polygon.add(x2);
// polygon.add(y2);
// polygon.add(x3);
// polygon.add(y3);
// polygonIndices = polygonIndicesPool.obtain();
// polygonIndices.clear();
// polygonIndices.add(t1);
// polygonIndices.add(t2);
// polygonIndices.add(t3);
// lastWinding = winding(x1, y1, x2, y2, x3, y3);
// fanBaseIndex = t1;
// }
// }
// if (polygon.size > 0) { // Go through the list of polygons and try to merge the remaining triangles with the found triangle fans.
// convexPolygons.add(polygon); for (int i = 0, n = convexPolygons.Count; i < n; i++) {
// convexPolygonsIndices.add(polygonIndices); polygonIndices = convexPolygonsIndices.Items[i];
// } if (polygonIndices.Count == 0) continue;
int firstIndex = polygonIndices.Items[0];
int lastIndex = polygonIndices.Items[polygonIndices.Count - 1];
// // Go through the list of polygons and try to merge the remaining triangles with the found triangle fans. polygon = convexPolygons.Items[i];
// for (int i = 0, n = convexPolygons.size; i < n; i++) { int o = polygon.Count - 4;
// polygonIndices = convexPolygonsIndices.get(i); float[] p = polygon.Items;
// if (polygonIndices.size == 0) continue; float prevPrevX = p[o], prevPrevY = p[o + 1];
// int firstIndex = polygonIndices.get(0); float prevX = p[o + 2], prevY = p[o + 3];
// int lastIndex = polygonIndices.get(polygonIndices.size - 1); float firstX = p[0], firstY = p[1];
float secondX = p[2], secondY = p[3];
int winding = Winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);
// polygon = convexPolygons.get(i); for (int ii = 0; ii < n; ii++) {
// int o = polygon.size - 4; if (ii == i) continue;
// float[] p = polygon.items; var otherIndices = convexPolygonsIndices.Items[ii];
// float prevPrevX = p[o], prevPrevY = p[o + 1]; if (otherIndices.Count != 3) continue;
// float prevX = p[o + 2], prevY = p[o + 3]; int otherFirstIndex = otherIndices.Items[0];
// float firstX = p[0], firstY = p[1]; int otherSecondIndex = otherIndices.Items[1];
// float secondX = p[2], secondY = p[3]; int otherLastIndex = otherIndices.Items[2];
// int winding = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);
// for (int ii = 0; ii < n; ii++) { var otherPoly = convexPolygons.Items[ii];
// if (ii == i) continue; float x3 = otherPoly.Items[otherPoly.Count - 2], y3 = otherPoly.Items[otherPoly.Count - 1];
// ShortArray otherIndices = convexPolygonsIndices.get(ii);
// if (otherIndices.size != 3) continue;
// int otherFirstIndex = otherIndices.get(0);
// int otherSecondIndex = otherIndices.get(1);
// int otherLastIndex = otherIndices.get(2);
// FloatArray otherPoly = convexPolygons.get(ii); if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) continue;
// float x3 = otherPoly.get(otherPoly.size - 2), y3 = otherPoly.get(otherPoly.size - 1); int winding1 = Winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3);
int winding2 = Winding(x3, y3, firstX, firstY, secondX, secondY);
if (winding1 == winding && winding2 == winding) {
otherPoly.Clear();
otherIndices.Clear();
polygon.Add(x3);
polygon.Add(y3);
polygonIndices.Add((short)otherLastIndex);
prevPrevX = prevX;
prevPrevY = prevY;
prevX = x3;
prevY = y3;
ii = 0;
}
}
}
// if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) continue; // Remove empty polygons that resulted from the merge step above.
// int winding1 = winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3); for (int i = convexPolygons.Count - 1; i >= 0; i--) {
// int winding2 = winding(x3, y3, firstX, firstY, secondX, secondY); polygon = convexPolygons.Items[i];
// if (winding1 == winding && winding2 == winding) { if (polygon.Count == 0) {
// otherPoly.clear(); convexPolygons.RemoveAt(i);
// otherIndices.clear(); polygonPool.Free(polygon);
// polygon.add(x3); }
// polygon.add(y3); }
// polygonIndices.add(otherLastIndex);
// prevPrevX = prevX;
// prevPrevY = prevY;
// prevX = x3;
// prevY = y3;
// ii = 0;
// }
// }
// }
// // Remove empty polygons that resulted from the merge step above. return convexPolygons;
// for (int i = convexPolygons.size - 1; i >= 0; i--) { }
// polygon = convexPolygons.get(i);
// if (polygon.size == 0) {
// convexPolygons.removeIndex(i);
// polygonPool.free(polygon);
// }
// }
// return convexPolygons; static private bool IsConcave(int index, int vertexCount, float[] vertices, short[] indices) {
//} int previous = indices[(vertexCount + index - 1) % vertexCount] << 1;
int current = indices[index] << 1;
int next = indices[(index + 1) % vertexCount] << 1;
return !PositiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next],
vertices[next + 1]);
}
//static private boolean isConcave(int index, int vertexCount, float[] vertices, short[] indices) { static private bool PositiveArea(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) {
// int previous = indices[(vertexCount + index - 1) % vertexCount] << 1; return p1x * (p3y - p2y) + p2x * (p1y - p3y) + p3x * (p2y - p1y) >= 0;
// int current = indices[index] << 1; }
// int next = indices[(index + 1) % vertexCount] << 1;
// return !positiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next],
// vertices[next + 1]);
//}
//static private boolean positiveArea(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) { static private int Winding(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) {
// return p1x * (p3y - p2y) + p2x * (p1y - p3y) + p3x * (p2y - p1y) >= 0; float px = p2x - p1x, py = p2y - p1y;
//} return p3x * py - p3y * px + px * p1y - p1x * py >= 0 ? 1 : -1;
}
//static private int winding(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) {
// float px = p2x - p1x, py = p2y - p1y;
// return p3x * py - p3y * px + px * p1y - p1x * py >= 0 ? 1 : -1;
//}
} }
} }