/** @see #computeTriangles(float[], int, int) */ public ShortArray computeTriangles (float[] vertices) { return computeTriangles(vertices, 0, vertices.length); }
private void triangulate () { int[] vertexTypes = this.vertexTypes.items; while (vertexCount > 3) { int earTipIndex = findEarTip(); cutEarTip(earTipIndex); // The type of the two vertices adjacent to the clipped vertex may have changed. int previousIndex = previousIndex(earTipIndex); int nextIndex = earTipIndex == vertexCount ? 0 : earTipIndex; vertexTypes[previousIndex] = classifyVertex(previousIndex); vertexTypes[nextIndex] = classifyVertex(nextIndex); } if (vertexCount == 3) { ShortArray triangles = this.triangles; short[] indices = this.indices; triangles.add(indices[0]); triangles.add(indices[1]); triangles.add(indices[2]); } }
/** @return {@link #CONCAVE} or {@link #CONVEX} */ private int classifyVertex (int index) { short[] indices = this.indices; int previous = indices[previousIndex(index)] * 2; int current = indices[index] * 2; int next = indices[nextIndex(index)] * 2; float[] vertices = this.vertices; return computeSpannedAreaSign(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next], vertices[next + 1]); }
Polygon tmpPoly = new Polygon(); Polygon intersectionPoly = new Polygon(); EarClippingTriangulator triangulator = new EarClippingTriangulator(); if(verts.length > 0) { parts.add(snapToGrid(verts)); ShortArray arr = triangulator.computeTriangles(verts); indices.add(arr.toArray()); } else {
indicesArray.size = vertexCount; short[] indices = this.indices = indicesArray.items; if (areVerticesClockwise(vertices, offset, count)) { for (short i = 0; i < vertexCount; i++) indices[i] = (short)(vertexOffset + i); vertexTypes.ensureCapacity(vertexCount); for (int i = 0, n = vertexCount; i < n; ++i) vertexTypes.add(classifyVertex(i)); triangles.clear(); triangles.ensureCapacity(Math.max(0, vertexCount - 2) * 3); triangulate(); return triangles;
// this will calculate the triangles given your vertices short triangles[] = new EarClippingTriangulator().computeTriangles(vertices1).toArray(); // use your texture region PolygonRegion polygonRegion = new PolygonRegion(textureRegion, vertices1, triangles);
private void cutEarTip (int earTipIndex) { short[] indices = this.indices; ShortArray triangles = this.triangles; triangles.add(indices[previousIndex(earTipIndex)]); triangles.add(indices[earTipIndex]); triangles.add(indices[nextIndex(earTipIndex)]); indicesArray.removeIndex(earTipIndex); vertexTypes.removeIndex(earTipIndex); vertexCount--; }
private int findEarTip () { int vertexCount = this.vertexCount; for (int i = 0; i < vertexCount; i++) if (isEarTip(i)) return i; // Desperate mode: if no vertex is an ear tip, we are dealing with a degenerate polygon (e.g. nearly collinear). // Note that the input was not necessarily degenerate, but we could have made it so by clipping some valid ears. // Idea taken from Martin Held, "FIST: Fast industrial-strength triangulation of polygons", Algorithmica (1998), // http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.115.291 // Return a convex or tangential vertex if one exists. int[] vertexTypes = this.vertexTypes.items; for (int i = 0; i < vertexCount; i++) if (vertexTypes[i] != CONCAVE) return i; return 0; // If all vertices are concave, just return the first one. }
Polygon tmpPoly = new Polygon(); Polygon intersectionPoly = new Polygon(); EarClippingTriangulator triangulator = new EarClippingTriangulator(); if(verts.length > 0) { parts.add(snapToGrid(verts)); ShortArray arr = triangulator.computeTriangles(verts); indices.add(arr.toArray()); } else {
indicesArray.size = vertexCount; short[] indices = this.indices = indicesArray.items; if (areVerticesClockwise(vertices, offset, count)) { for (short i = 0; i < vertexCount; i++) indices[i] = (short)(vertexOffset + i); vertexTypes.ensureCapacity(vertexCount); for (int i = 0, n = vertexCount; i < n; ++i) vertexTypes.add(classifyVertex(i)); triangles.clear(); triangles.ensureCapacity(Math.max(0, vertexCount - 2) * 3); triangulate(); return triangles;
/** * Constructor. Note that vertices must be in a clockwise order for * performance and accuracy. * * @param vertices * All points in x,y pairs. E.g. x1,y1,x2,y2,etc. */ public Polygon(float[] vertices) { super(); this.vertices = vertices; triangulator = new EarClippingTriangulator(); getNumberOfSides(); }
private void cutEarTip (int earTipIndex) { short[] indices = this.indices; ShortArray triangles = this.triangles; triangles.add(indices[previousIndex(earTipIndex)]); triangles.add(indices[earTipIndex]); triangles.add(indices[nextIndex(earTipIndex)]); indicesArray.removeIndex(earTipIndex); vertexTypes.removeIndex(earTipIndex); vertexCount--; }
private int findEarTip () { int vertexCount = this.vertexCount; for (int i = 0; i < vertexCount; i++) if (isEarTip(i)) return i; // Desperate mode: if no vertex is an ear tip, we are dealing with a degenerate polygon (e.g. nearly collinear). // Note that the input was not necessarily degenerate, but we could have made it so by clipping some valid ears. // Idea taken from Martin Held, "FIST: Fast industrial-strength triangulation of polygons", Algorithmica (1998), // http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.115.291 // Return a convex or tangential vertex if one exists. int[] vertexTypes = this.vertexTypes.items; for (int i = 0; i < vertexCount; i++) if (vertexTypes[i] != CONCAVE) return i; return 0; // If all vertices are concave, just return the first one. }
/** @see #computeTriangles(float[], int, int) */ public ShortArray computeTriangles (FloatArray vertices) { return computeTriangles(vertices.items, 0, vertices.size); }
EarClippingTriangulator ear = new EarClippingTriangulator(); ShortArray arrRes = ear.computeTriangles(vertices);
/** @return {@link #CONCAVE} or {@link #CONVEX} */ private int classifyVertex (int index) { short[] indices = this.indices; int previous = indices[previousIndex(index)] * 2; int current = indices[index] * 2; int next = indices[nextIndex(index)] * 2; float[] vertices = this.vertices; return computeSpannedAreaSign(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next], vertices[next + 1]); }
private void triangulate () { int[] vertexTypes = this.vertexTypes.items; while (vertexCount > 3) { int earTipIndex = findEarTip(); cutEarTip(earTipIndex); // The type of the two vertices adjacent to the clipped vertex may have changed. int previousIndex = previousIndex(earTipIndex); int nextIndex = earTipIndex == vertexCount ? 0 : earTipIndex; vertexTypes[previousIndex] = classifyVertex(previousIndex); vertexTypes[nextIndex] = classifyVertex(nextIndex); } if (vertexCount == 3) { ShortArray triangles = this.triangles; short[] indices = this.indices; triangles.add(indices[0]); triangles.add(indices[1]); triangles.add(indices[2]); } }
indicesArray.size = vertexCount; short[] indices = this.indices = indicesArray.items; if (areVerticesClockwise(vertices, offset, count)) { for (short i = 0; i < vertexCount; i++) indices[i] = (short)(vertexOffset + i); vertexTypes.ensureCapacity(vertexCount); for (int i = 0, n = vertexCount; i < n; ++i) vertexTypes.add(classifyVertex(i)); triangles.clear(); triangles.ensureCapacity(Math.max(0, vertexCount - 2) * 3); triangulate(); return triangles;
PolygonShape shape = (PolygonShape) fixture.getShape(); int vertexCount = shape.getVertexCount(); float[] vertices = new float[vertexCount * 2]; for (int k = 0; k < vertexCount; k++) { shape.getVertex(k, mTmp); mTmp.rotate(body.getAngle()* MathUtils.radiansToDegrees); mTmp.add(bodyPos); vertices[k * 2] = mTmp.x * PIXELS_PER_METER; vertices[k * 2 + 1] = mTmp.y * PIXELS_PER_METER; } short triangles[] = new EarClippingTriangulator() .computeTriangles(vertices) .toArray(); PolygonRegion region = new PolygonRegion( textureRegion, vertices, triangles);
private void cutEarTip (int earTipIndex) { short[] indices = this.indices; ShortArray triangles = this.triangles; triangles.add(indices[previousIndex(earTipIndex)]); triangles.add(indices[earTipIndex]); triangles.add(indices[nextIndex(earTipIndex)]); indicesArray.removeIndex(earTipIndex); vertexTypes.removeIndex(earTipIndex); vertexCount--; }