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