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

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

Introduction

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

Prototype

public void clear() 

Source Link

Usage

From source file:CB_UI_Base.GL_UI.Controls.EditTextField.java

License:Open Source License

/**
 * Computes the glyph advances for the given character sequence and stores them in the provided {@link FloatArray}. The float
 * arrays are cleared. An additional element is added at the end.
 *
 * @param glyphAdvances  the glyph advances output array.
 * @param glyphPositions the glyph positions output array.
 *//*from   w  ww  .java  2s .  c  om*/
public void computeGlyphAdvancesAndPositions(CharSequence str, FloatArray glyphAdvances,
        FloatArray glyphPositions) {
    glyphAdvances.clear();
    glyphPositions.clear();
    int index = 0;
    int end = str.length();
    float width = 0;
    Glyph lastGlyph = null;
    BitmapFontData data = style.font.getData();
    if (data.scaleX == 1) {
        for (; index < end; index++) {
            char ch = str.charAt(index);
            Glyph g = data.getGlyph(ch);
            if (g != null) {
                if (lastGlyph != null)
                    width += lastGlyph.getKerning(ch);
                lastGlyph = g;
                glyphAdvances.add(g.xadvance);
                glyphPositions.add(width);
                width += g.xadvance;
            }
        }
        glyphAdvances.add(0);
        glyphPositions.add(width);
    } else {
        float scaleX = style.font.getData().scaleX;
        for (; index < end; index++) {
            char ch = str.charAt(index);
            Glyph g = data.getGlyph(ch);
            if (g != null) {
                if (lastGlyph != null)
                    width += lastGlyph.getKerning(ch) * scaleX;
                lastGlyph = g;
                float xadvance = g.xadvance * scaleX;
                glyphAdvances.add(xadvance);
                glyphPositions.add(width);
                width += xadvance;
            }
        }
        glyphAdvances.add(0);
        glyphPositions.add(width);
    }
}

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

License:Open Source License

/** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
 * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */
boolean clip(float x1, float y1, float x2, float y2, float x3, float y3, FloatArray clippingArea,
        FloatArray output) {/*  ww w.j  av  a2  s . co m*/
    FloatArray originalOutput = output;
    boolean clipped = false;

    // Avoid copy at the end.
    FloatArray input = null;
    if (clippingArea.size % 4 >= 2) {
        input = output;
        output = scratch;
    } else
        input = scratch;

    input.clear();
    input.add(x1);
    input.add(y1);
    input.add(x2);
    input.add(y2);
    input.add(x3);
    input.add(y3);
    input.add(x1);
    input.add(y1);
    output.clear();

    float[] clippingVertices = clippingArea.items;
    int clippingVerticesLast = clippingArea.size - 4;
    for (int i = 0;; i += 2) {
        float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
        float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3];
        float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2;

        float[] inputVertices = input.items;
        int inputVerticesLength = input.size - 2, outputStart = output.size;
        for (int ii = 0; ii < inputVerticesLength; ii += 2) {
            float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
            float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
            boolean side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
            if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
                if (side2) { // v1 inside, v2 inside
                    output.add(inputX2);
                    output.add(inputY2);
                    continue;
                }
                // v1 inside, v2 outside
                float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
                float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
                if (Math.abs(s) > 0.000001f) {
                    float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
                    output.add(edgeX + (edgeX2 - edgeX) * ua);
                    output.add(edgeY + (edgeY2 - edgeY) * ua);
                } else {
                    output.add(edgeX);
                    output.add(edgeY);
                }
            } else if (side2) { // v1 outside, v2 inside
                float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
                float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
                if (Math.abs(s) > 0.000001f) {
                    float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
                    output.add(edgeX + (edgeX2 - edgeX) * ua);
                    output.add(edgeY + (edgeY2 - edgeY) * ua);
                } else {
                    output.add(edgeX);
                    output.add(edgeY);
                }
                output.add(inputX2);
                output.add(inputY2);
            }
            clipped = true;
        }

        if (outputStart == output.size) { // All edges outside.
            originalOutput.clear();
            return true;
        }

        output.add(output.items[0]);
        output.add(output.items[1]);

        if (i == clippingVerticesLast)
            break;
        FloatArray temp = output;
        output = input;
        output.clear();
        input = temp;
    }

    if (originalOutput != output) {
        originalOutput.clear();
        originalOutput.addAll(output.items, 0, output.size - 2);
    } else
        originalOutput.setSize(originalOutput.size - 2);

    return clipped;
}

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 w  ww . ja v a  2  s  .co 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;
}

From source file:com.infunity.isometricgame.shared.utils.dermetfan.GeometryUtils.java

License:Apache License

/** @param segments the segments
 *  @param polygon if the segments represent a closed polygon
 *  @param intersections the array to store the intersections in */
public static void intersectSegments(float x1, float y1, float x2, float y2, float[] segments, boolean polygon,
        FloatArray intersections) {
    if (polygon && segments.length < 6)
        throw new IllegalArgumentException("a polygon consists of at least 3 points: " + segments.length);
    else if (segments.length < 4)
        throw new IllegalArgumentException(
                "segments does not contain enough vertices to represent at least one segment: "
                        + segments.length);
    if (segments.length % 2 != 0)
        throw new IllegalArgumentException(
                "malformed segments; the number of vertices is not dividable by 2: " + segments.length);
    intersections.clear();
    Vector2 tmp = Pools.obtain(Vector2.class);
    for (int i = 0, n = segments.length - (polygon ? 0 : 2); i < n; i += 2) {
        float x3 = segments[i], y3 = segments[i + 1], x4 = wrapIndex(i + 2, segments),
                y4 = wrapIndex(i + 3, segments);
        if (Intersector.intersectSegments(x1, y1, x2, y2, x3, y3, x4, y4, tmp)) {
            intersections.add(tmp.x);/*from w  w  w . j a  v  a2s .co  m*/
            intersections.add(tmp.y);
        }
    }
    Pools.free(tmp);
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link Fixture} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return the parsed {@link Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);/*w w  w  .  j a  va2s  .  com*/

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + ClassReflection.getSimpleName(mapObject.getClass())
                        + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + ClassReflection.getSimpleName(mapObject.getClass()) + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    MapProperties properties = mapObject.getProperties();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);
    listener.created(fixture, mapObject);

    return fixture;
}

From source file:de.fhkoeln.game.utils.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link com.badlogic.gdx.physics.box2d.Fixture} from a {@link com.badlogic.gdx.maps.MapObject}
 *  @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse
 *  @param body the {@link com.badlogic.gdx.physics.box2d.Body} to create the {@link com.badlogic.gdx.physics.box2d.Fixture Fixtures} on
 *  @return the parsed {@link com.badlogic.gdx.physics.box2d.Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);//from ww  w .  j  av a 2s .  c o  m

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + mapObject.getClass().getSimpleName() + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    MapProperties properties = mapObject.getProperties();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);
    listener.created(fixture, mapObject);

    return fixture;
}