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

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

Introduction

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

Prototype

public short get(int index) 

Source Link

Usage

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();/*from   www.ja  va2  s. c  om*/

    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;
}

From source file:com.flatfisk.gnomp.math.GeometryUtils.java

License:Apache License

/**
 * @param concave the concave polygon to triangulate
 * @return an array of triangles representing the given concave polygon
 * @see com.badlogic.gdx.math.EarClippingTriangulator#computeTriangles(float[])
 *//*from   w ww  . ja  v  a  2  s .  c o  m*/
public static Polygon[] triangulate(Polygon concave) {
    Vector2[] polygonVertices = toVector2Array(concave.getTransformedVertices());
    ShortArray indices = new EarClippingTriangulator().computeTriangles(toFloatArray(polygonVertices));
    Vector2[] vertices = new Vector2[indices.size];
    for (int i = 0; i < indices.size; i++)
        vertices[i] = polygonVertices[indices.get(i)];
    return toPolygonArray(vertices, 3);
}

From source file:io.piotrjastrzebski.dungen.DungeonGenerator.java

License:Apache License

private void triangulate() {
    DelaunayTriangulator triangulator = new DelaunayTriangulator();

    float[] points = new float[mainRooms.size * 2];
    for (int i = 0; i < points.length; i += 2) {
        Room room = mainRooms.get(i / 2);
        points[i] = room.cx();/*w w w.j a  v a2 s.  c  o m*/
        points[i + 1] = room.cy();
    }

    ShortArray indicies = triangulator.computeTriangles(points, 0, points.length, false);

    graph.clear();

    for (int i = 0; i < indicies.size; i += 3) {
        int p1 = indicies.get(i) * 2;
        int p2 = indicies.get(i + 1) * 2;
        int p3 = indicies.get(i + 2) * 2;
        // this is pretty dumb...
        Room roomA = getRoom(points[p1], points[p1 + 1]);
        Room roomB = getRoom(points[p2], points[p2 + 1]);
        Room roomC = getRoom(points[p3], points[p3 + 1]);
        graph.add(roomA, roomB);
        graph.add(roomA, roomC);
        graph.add(roomB, roomC);
    }

    createMST();
}