Example usage for com.badlogic.gdx.utils ShortArray clear

List of usage examples for com.badlogic.gdx.utils ShortArray clear

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils ShortArray clear.

Prototype

public void clear() 

Source Link

Usage

From source file:com.esotericsoftware.spine.utils.SkeletonClipping.java

License:Open Source License

public void clipTriangles(float[] vertices, int verticesLength, short[] triangles, int trianglesLength,
        float[] uvs, float light, float dark, boolean twoColor) {

    FloatArray clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
    ShortArray clippedTriangles = this.clippedTriangles;
    Object[] polygons = clippingPolygons.items;
    int polygonsCount = clippingPolygons.size;
    int vertexSize = twoColor ? 6 : 5;

    short index = 0;
    clippedVertices.clear();// w ww  . jav a  2  s  .c o  m
    clippedTriangles.clear();
    outer: for (int i = 0; i < trianglesLength; i += 3) {
        int vertexOffset = triangles[i] << 1;
        float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
        float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];

        vertexOffset = triangles[i + 1] << 1;
        float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
        float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];

        vertexOffset = triangles[i + 2] << 1;
        float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
        float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];

        for (int p = 0; p < polygonsCount; p++) {
            int s = clippedVertices.size;
            if (clip(x1, y1, x2, y2, x3, y3, (FloatArray) polygons[p], clipOutput)) {
                int clipOutputLength = clipOutput.size;
                if (clipOutputLength == 0)
                    continue;
                float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
                float d = 1 / (d0 * d2 + d1 * (y1 - y3));

                int clipOutputCount = clipOutputLength >> 1;
                float[] clipOutputItems = clipOutput.items;
                float[] clippedVerticesItems = clippedVertices.setSize(s + clipOutputCount * vertexSize);
                for (int ii = 0; ii < clipOutputLength; ii += 2) {
                    float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
                    clippedVerticesItems[s] = x;
                    clippedVerticesItems[s + 1] = y;
                    clippedVerticesItems[s + 2] = light;
                    if (twoColor) {
                        clippedVerticesItems[s + 3] = dark;
                        s += 4;
                    } else
                        s += 3;
                    float c0 = x - x3, c1 = y - y3;
                    float a = (d0 * c0 + d1 * c1) * d;
                    float b = (d4 * c0 + d2 * c1) * d;
                    float c = 1 - a - b;
                    clippedVerticesItems[s] = u1 * a + u2 * b + u3 * c;
                    clippedVerticesItems[s + 1] = v1 * a + v2 * b + v3 * c;
                    s += 2;
                }

                s = clippedTriangles.size;
                short[] clippedTrianglesItems = clippedTriangles.setSize(s + 3 * (clipOutputCount - 2));
                clipOutputCount--;
                for (int ii = 1; ii < clipOutputCount; ii++) {
                    clippedTrianglesItems[s] = index;
                    clippedTrianglesItems[s + 1] = (short) (index + ii);
                    clippedTrianglesItems[s + 2] = (short) (index + ii + 1);
                    s += 3;
                }
                index += clipOutputCount + 1;

            } else {
                float[] clippedVerticesItems = clippedVertices.setSize(s + 3 * vertexSize);
                clippedVerticesItems[s] = x1;
                clippedVerticesItems[s + 1] = y1;
                clippedVerticesItems[s + 2] = light;
                if (!twoColor) {
                    clippedVerticesItems[s + 3] = u1;
                    clippedVerticesItems[s + 4] = v1;

                    clippedVerticesItems[s + 5] = x2;
                    clippedVerticesItems[s + 6] = y2;
                    clippedVerticesItems[s + 7] = light;
                    clippedVerticesItems[s + 8] = u2;
                    clippedVerticesItems[s + 9] = v2;

                    clippedVerticesItems[s + 10] = x3;
                    clippedVerticesItems[s + 11] = y3;
                    clippedVerticesItems[s + 12] = light;
                    clippedVerticesItems[s + 13] = u3;
                    clippedVerticesItems[s + 14] = v3;
                } else {
                    clippedVerticesItems[s + 3] = dark;
                    clippedVerticesItems[s + 4] = u1;
                    clippedVerticesItems[s + 5] = v1;

                    clippedVerticesItems[s + 6] = x2;
                    clippedVerticesItems[s + 7] = y2;
                    clippedVerticesItems[s + 8] = light;
                    clippedVerticesItems[s + 9] = dark;
                    clippedVerticesItems[s + 10] = u2;
                    clippedVerticesItems[s + 11] = v2;

                    clippedVerticesItems[s + 12] = x3;
                    clippedVerticesItems[s + 13] = y3;
                    clippedVerticesItems[s + 14] = light;
                    clippedVerticesItems[s + 15] = dark;
                    clippedVerticesItems[s + 16] = u3;
                    clippedVerticesItems[s + 17] = v3;
                }

                s = clippedTriangles.size;
                short[] clippedTrianglesItems = clippedTriangles.setSize(s + 3);
                clippedTrianglesItems[s] = index;
                clippedTrianglesItems[s + 1] = (short) (index + 1);
                clippedTrianglesItems[s + 2] = (short) (index + 2);
                index += 3;
                continue outer;
            }
        }
    }
}

From source file:com.esotericsoftware.spine.utils.Triangulator.java

License:Open Source License

public ShortArray triangulate(FloatArray verticesArray) {
    float[] vertices = verticesArray.items;
    int vertexCount = verticesArray.size >> 1;

    ShortArray indicesArray = this.indicesArray;
    indicesArray.clear();
    short[] indices = indicesArray.setSize(vertexCount);
    for (short i = 0; i < vertexCount; i++)
        indices[i] = i;//  ww  w. j a v a  2  s.co  m

    BooleanArray isConcaveArray = this.isConcaveArray;
    boolean[] isConcave = isConcaveArray.setSize(vertexCount);
    for (int i = 0, n = vertexCount; i < n; ++i)
        isConcave[i] = isConcave(i, vertexCount, vertices, indices);

    ShortArray triangles = this.triangles;
    triangles.clear();
    triangles.ensureCapacity(Math.max(0, vertexCount - 2) << 2);

    while (vertexCount > 3) {
        // Find ear tip.
        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))
                                break outer;
                        }
                    }
                }
                break;
            }

            if (next == 0) {
                do {
                    if (!isConcave[i])
                        break;
                    i--;
                } while (i > 0);
                break;
            }

            previous = i;
            i = next;
            next = (next + 1) % vertexCount;
        }

        // Cut ear tip.
        triangles.add(indices[(vertexCount + i - 1) % vertexCount]);
        triangles.add(indices[i]);
        triangles.add(indices[(i + 1) % vertexCount]);
        indicesArray.removeIndex(i);
        isConcaveArray.removeIndex(i);
        vertexCount--;

        int previousIndex = (vertexCount + i - 1) % vertexCount;
        int nextIndex = i == vertexCount ? 0 : i;
        isConcave[previousIndex] = isConcave(previousIndex, vertexCount, vertices, indices);
        isConcave[nextIndex] = isConcave(nextIndex, vertexCount, vertices, indices);
    }

    if (vertexCount == 3) {
        triangles.add(indices[2]);
        triangles.add(indices[0]);
        triangles.add(indices[1]);
    }

    return triangles;
}

From source file:com.esotericsoftware.spine.utils.Triangulator.java

License:Open Source License

public Array<FloatArray> decompose(FloatArray verticesArray, ShortArray triangles) {
    float[] vertices = verticesArray.items;

    Array<FloatArray> convexPolygons = this.convexPolygons;
    polygonPool.freeAll(convexPolygons);
    convexPolygons.clear();/* ww w  .j a v a 2s . c  o  m*/

    Array<ShortArray> convexPolygonsIndices = this.convexPolygonsIndices;
    polygonIndicesPool.freeAll(convexPolygonsIndices);
    convexPolygonsIndices.clear();

    ShortArray polygonIndices = polygonIndicesPool.obtain();
    polygonIndices.clear();

    FloatArray polygon = polygonPool.obtain();
    polygon.clear();

    // Merge subsequent triangles if they form a triangle fan.
    int fanBaseIndex = -1, lastWinding = 0;
    short[] trianglesItems = triangles.items;
    for (int i = 0, n = triangles.size; 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];

        // 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;
        if (fanBaseIndex == t1) {
            int o = polygon.size - 4;
            float[] p = polygon.items;
            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]);
            if (winding1 == lastWinding && winding2 == lastWinding) {
                polygon.add(x3);
                polygon.add(y3);
                polygonIndices.add(t3);
                merged = true;
            }
        }

        // Otherwise make this triangle the new base.
        if (!merged) {
            if (polygon.size > 0) {
                convexPolygons.add(polygon);
                convexPolygonsIndices.add(polygonIndices);
            } else {
                polygonPool.free(polygon);
                polygonIndicesPool.free(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) {
        convexPolygons.add(polygon);
        convexPolygonsIndices.add(polygonIndices);
    }

    // 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++) {
        polygonIndices = convexPolygonsIndices.get(i);
        if (polygonIndices.size == 0)
            continue;
        int firstIndex = polygonIndices.get(0);
        int lastIndex = polygonIndices.get(polygonIndices.size - 1);

        polygon = convexPolygons.get(i);
        int o = polygon.size - 4;
        float[] p = polygon.items;
        float prevPrevX = p[o], prevPrevY = p[o + 1];
        float prevX = p[o + 2], prevY = p[o + 3];
        float firstX = p[0], firstY = p[1];
        float secondX = p[2], secondY = p[3];
        int winding = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);

        for (int ii = 0; ii < n; ii++) {
            if (ii == i)
                continue;
            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);
            float x3 = otherPoly.get(otherPoly.size - 2), y3 = otherPoly.get(otherPoly.size - 1);

            if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex)
                continue;
            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(otherLastIndex);
                prevPrevX = prevX;
                prevPrevY = prevY;
                prevX = x3;
                prevY = y3;
                ii = 0;
            }
        }
    }

    // Remove empty polygons that resulted from the merge step above.
    for (int i = convexPolygons.size - 1; i >= 0; i--) {
        polygon = convexPolygons.get(i);
        if (polygon.size == 0) {
            convexPolygons.removeIndex(i);
            polygonPool.free(polygon);
            polygonIndices = convexPolygonsIndices.removeIndex(i);
            polygonIndicesPool.free(polygonIndices);
        }
    }

    return convexPolygons;
}