Search in sources :

Example 16 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class Smasher method zjednotenie.

/**
 * Vezme polygony a vrati ich zjednotenie. Plygony su navzajom disjunknte
 * avsak dotykaju sa bodmi hranami, ktore maju referencnu zavislost.
 *
 * @param polygony
 * @return Vrati List zjednotenych polygonov.
 */
private static MyList<Polygon> zjednotenie(MyList<Fragment> polygony) {
    HashTabulka<GraphVertex> graf = new HashTabulka<>();
    for (Polygon p : polygony) {
        for (int i = 1; i <= p.size(); ++i) {
            Tuple2f v = p.cycleGet(i);
            GraphVertex vertex = graf.get(v);
            if (vertex == null) {
                vertex = new GraphVertex(v);
                graf.add(vertex);
                vertex.first = p;
            } else {
                vertex.polygonCount++;
                vertex.second = p;
            }
        }
    }
    for (Polygon p : polygony) {
        for (int i = 0; i < p.size(); ++i) {
            GraphVertex v1 = graf.get(p.get(i));
            GraphVertex v2 = graf.get(p.cycleGet(i + 1));
            if (v1.polygonCount == 1 || v2.polygonCount == 1 || (v1.polygonCount <= 2 && v2.polygonCount <= 2 && !((v1.first == v2.first && v1.second == v2.second) || (v1.first == v2.second && v1.second == v2.first)))) {
                v1.next = v2;
                v2.prev = v1;
            }
        }
    }
    MyList<Polygon> vysledok = new MyList<>();
    GraphVertex[] arr = graf.toArray(new GraphVertex[graf.size()]);
    for (GraphVertex v : arr) {
        if (v.next != null && !v.visited) {
            Polygon p = new Polygon();
            for (GraphVertex iterator = v; !iterator.visited; iterator = iterator.next) {
                if (PlatformMathUtils.siteDef(iterator.next.value, iterator.value, iterator.prev.value) != 0) {
                    p.add(iterator.value);
                }
                iterator.visited = true;
            }
            vysledok.add(p);
        }
    }
    return vysledok;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) MyList(spacegraph.space2d.phys.fracture.util.MyList) Polygon(spacegraph.space2d.phys.fracture.Polygon) HashTabulka(spacegraph.space2d.phys.fracture.util.HashTabulka)

Example 17 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class Smasher method calculate.

/**
 * Vrati prienik voronoi diagramu a polygonu.
 *
 * @param focee
 * @param p            Kopia polygonu, moze byt modifikovana
 * @param contactPoint Bod dotyku
 * @param ic           Funkcionalny interface, ktory definuje, ci fragment patri,
 *                     alebo nepatri do mnoziny ulomkov
 */
public void calculate(Polygon p, Tuple2f[] focee, Tuple2f contactPoint, IContains ic) {
    this.focee = focee;
    this.p = p;
    // Geometry geom = new Geometry(foceeAll, p);
    List<Fragment> list = getVoronoi();
    List<EdgePolygon> polygonEdgesList = new FasterList<>();
    HashTabulka<EdgeDiagram> diagramEdges = new HashTabulka<>();
    HashTabulka<EdgePolygon> polygonEdges = new HashTabulka<>();
    // vlozim hrany polygonu do hashovacej tabulky hran polygonu
    int count = p.size();
    for (int i = 1; i <= count; i++) {
        Tuple2f p1 = p.get(i - 1);
        Tuple2f p2 = p.get(i == count ? 0 : i);
        EdgePolygon e = new EdgePolygon(p1, p2);
        polygonEdges.add(e);
        polygonEdgesList.add(e);
    }
    // vlozim hrany diagramu do hashovacej tabulky hran diagramu
    for (Fragment pp : list) {
        count = pp.size();
        for (int i = 1; i <= count; i++) {
            Tuple2f p1 = pp.get(i - 1);
            Tuple2f p2 = pp.get(i == count ? 0 : i);
            EdgeDiagram e = new EdgeDiagram(p1, p2);
            EdgeDiagram alternative = diagramEdges.get(e);
            if (alternative == null) {
                diagramEdges.add(e);
                e.d1 = pp;
            } else {
                alternative.d2 = pp;
            }
        }
    }
    AEdge[][] allEdges = new AEdge[][] { diagramEdges.toArray(new AEdge[diagramEdges.size()]), polygonEdges.toArray(new AEdge[polygonEdges.size()]) };
    diagramEdges.clear();
    polygonEdges.clear();
    List<EVec2> vectorList = new FasterList<>();
    for (AEdge[] array : allEdges) {
        for (AEdge e : array) {
            EVec2 v1 = new EVec2(e.p1);
            EVec2 v2 = new EVec2(e.p2);
            v1.e = e;
            v2.e = e;
            if (v1.p.y < v2.p.y) {
                v1.start = true;
            } else {
                v2.start = true;
            }
            vectorList.add(v1);
            vectorList.add(v2);
        }
    }
    EVec2[] vectors = vectorList.toArray(new EVec2[vectorList.size()]);
    // zotriedim body
    Arrays.sort(vectors);
    for (EVec2 e : vectors) {
        if (e.e instanceof EdgeDiagram) {
            if (e.start) {
                EdgeDiagram ex = (EdgeDiagram) e.e;
                diagramEdges.add(ex);
                // for (EdgePolygon px : polygonEdges.toArray(new EdgePolygon[polygonEdges.size()])) {
                // process(px, ex);
                // }
                polygonEdges.forEach(px -> process(px, ex));
            } else {
                diagramEdges.remove(e.e);
            }
        } else {
            // je instanciou EdgePolygon
            if (e.start) {
                EdgePolygon px = (EdgePolygon) e.e;
                polygonEdges.add(px);
                diagramEdges.forEach(ex -> process(px, ex));
            // for (EdgeDiagram ex : diagramEdges.toArray(new EdgeDiagram[diagramEdges.size()]))
            // process(px, ex);
            } else {
                polygonEdges.remove(e.e);
            }
        }
    }
    for (Fragment pol : list) {
        pol.resort();
        int pn = pol.size();
        for (int i = 0; i < pn; i++) {
            Tuple2f v = pol.get(i);
            if (v instanceof Vec2Intersect) {
                Vec2Intersect vi = (Vec2Intersect) v;
                if (vi.p1 == pol) {
                    vi.i1 = i;
                } else {
                    vi.i2 = i;
                }
            }
        }
    }
    Polygon polygonAll = new Polygon();
    for (EdgePolygon ex : polygonEdgesList) {
        polygonAll.add(ex.p1);
        ex.list.sort(c);
        polygonAll.add(ex.list);
    }
    for (int i = 0; i < polygonAll.size(); i++) {
        Tuple2f v = polygonAll.get(i);
        if (v instanceof Vec2Intersect) {
            ((Vec2Intersect) v).index = i;
        }
    }
    MyList<Fragment> allIntersections = new MyList<>();
    // ostatne algoritmy generovali diery - tento je najlepsi - najdem najblizsi bod na hrane polygonu a zistim kolizny fargment - od neho prehladavam do sirky a kontrolujem vzdialenost a viditelnost (jednoduche, ciste)
    precalc_values();
    for (Fragment ppp : list) {
        List<Fragment> intsc = getIntersections(ppp, polygonAll);
        if (intsc == null) {
            // cely polygon sa nachadza vnutri fragmentu
            fragments = new Polygon[] { p };
            return;
        }
        allIntersections.addAll(intsc);
    }
    table.clear();
    // vytvorim hashovaciu tabulku hran
    for (Fragment f : allIntersections) {
        for (int i = 0; i < f.size(); ++i) {
            Tuple2f v1 = f.get(i);
            Tuple2f v2 = f.cycleGet(i + 1);
            EdgeDiagram e = new EdgeDiagram(v1, v2);
            EdgeDiagram e2 = table.get(e);
            if (e2 != null) {
                e = e2;
                e.d2 = f;
            } else {
                e.d1 = f;
                table.add(e);
            }
        }
    }
    // rozdelim polygony na 2 mnoziny - na tie, ktore budu ulomky a tie, ktore budu spojene a drzat spolu
    final double[] distance = { Double.MAX_VALUE };
    final Fragment[] startPolygon = { null };
    final Tuple2f[] kolmicovyBod = { null };
    MyList<EdgeDiagram> allEdgesPolygon = new MyList<>();
    // EdgeDiagram[] ee = table.toArray(new EdgeDiagram[table.size()]);
    table.forEach(ep -> {
        if (ep.d2 == null) {
            // toto sa nahradi vzorcom na vypocet vzdialenosti body od usecky
            Tuple2f vv = ep.kolmicovyBod(contactPoint);
            double newDistance = contactPoint.distanceSq(vv);
            if (newDistance <= distance[0]) {
                distance[0] = newDistance;
                kolmicovyBod[0] = vv;
                startPolygon[0] = ep.d1;
            }
            allEdgesPolygon.add(ep);
        }
    });
    MyList<Fragment> ppx = new MyList<>();
    ppx.add(startPolygon[0]);
    EdgeDiagram epx = new EdgeDiagram(null, null);
    HashTabulka<Fragment> vysledneFragmenty = new HashTabulka<>();
    startPolygon[0].visited = true;
    while (!ppx.isEmpty()) {
        Fragment px = ppx.get(0);
        vysledneFragmenty.add(px);
        for (int i = 0; i < px.size(); ++i) {
            Tuple2f v1 = px.get(i);
            Tuple2f v2 = px.cycleGet(i + 1);
            epx.p1 = v1;
            epx.p2 = v2;
            EdgeDiagram ep = table.get(epx);
            Fragment opposite = ep.d1 == px ? ep.d2 : ep.d1;
            if (opposite != null && !opposite.visited) {
                Tuple2f centroid = opposite.centroid();
                opposite.visited = true;
                if (ic.contains(centroid)) {
                    boolean intersection = false;
                    for (EdgeDiagram edge : allEdgesPolygon) {
                        // neberie do uvahy hrany polygonu
                        if (edge.d1 != startPolygon[0] && edge.d2 != startPolygon[0] && edge.intersectAre(centroid, kolmicovyBod[0])) {
                            intersection = true;
                            break;
                        }
                    }
                    // tu bude podmienka - ci ten polygon vezmem do uvahy, ak hej, priplnim ho do MyListu
                    if (!intersection) {
                        ppx.add(opposite);
                    }
                }
            }
        }
        ppx.removeAt(0);
    }
    Fragment[] fragmentsArray = vysledneFragmenty.toArray(new Fragment[vysledneFragmenty.size()]);
    MyList<Fragment> fragmentsBody = new MyList<>();
    for (Fragment fx : allIntersections) {
        if (!vysledneFragmenty.contains(fx)) {
            fragmentsBody.add(fx);
        }
    }
    MyList<Polygon> result = zjednotenie(fragmentsBody);
    result.add(fragmentsArray);
    fragments = new Polygon[result.size()];
    result.addToArray(fragments);
}
Also used : Fragment(spacegraph.space2d.phys.fracture.Fragment) Tuple2f(spacegraph.util.math.Tuple2f) Polygon(spacegraph.space2d.phys.fracture.Polygon) FasterList(jcog.list.FasterList) MyList(spacegraph.space2d.phys.fracture.util.MyList) HashTabulka(spacegraph.space2d.phys.fracture.util.HashTabulka)

Example 18 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class DynamicTree method moveProxy.

@Override
public final boolean moveProxy(int proxyId, final AABB aabb, Tuple2f displacement) {
    assert (aabb.isValid());
    assert (0 <= proxyId && proxyId < m_nodeCapacity);
    final DynamicTreeNode node = this.node[proxyId];
    assert (node.child1 == null);
    final AABB nodeAABB = node.aabb;
    // if (nodeAABB.contains(aabb)) {
    if (nodeAABB.lowerBound.x <= aabb.lowerBound.x && nodeAABB.lowerBound.y <= aabb.lowerBound.y && aabb.upperBound.x <= nodeAABB.upperBound.x && aabb.upperBound.y <= nodeAABB.upperBound.y) {
        return false;
    }
    removeLeaf(node);
    // Extend AABB
    final Tuple2f lowerBound = nodeAABB.lowerBound;
    final Tuple2f upperBound = nodeAABB.upperBound;
    lowerBound.x = aabb.lowerBound.x - Settings.aabbExtension;
    lowerBound.y = aabb.lowerBound.y - Settings.aabbExtension;
    upperBound.x = aabb.upperBound.x + Settings.aabbExtension;
    upperBound.y = aabb.upperBound.y + Settings.aabbExtension;
    // Predict AABB displacement.
    final float dx = displacement.x * Settings.aabbMultiplier;
    final float dy = displacement.y * Settings.aabbMultiplier;
    if (dx < 0.0f) {
        lowerBound.x += dx;
    } else {
        upperBound.x += dx;
    }
    if (dy < 0.0f) {
        lowerBound.y += dy;
    } else {
        upperBound.y += dy;
    }
    insertLeaf(proxyId);
    return true;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) AABB(spacegraph.space2d.phys.collision.AABB)

Example 19 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class ChainShape method raycast.

@Override
public boolean raycast(RayCastOutput output, RayCastInput input, Transform xf, int childIndex) {
    assert (childIndex < m_count);
    final EdgeShape edgeShape = pool0;
    int i1 = childIndex;
    int i2 = childIndex + 1;
    if (i2 == m_count) {
        i2 = 0;
    }
    Tuple2f v = m_vertices[i1];
    edgeShape.m_vertex1.x = v.x;
    edgeShape.m_vertex1.y = v.y;
    Tuple2f v1 = m_vertices[i2];
    edgeShape.m_vertex2.x = v1.x;
    edgeShape.m_vertex2.y = v1.y;
    return edgeShape.raycast(output, input, xf, 0);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f)

Example 20 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class ChainShape method computeAABB.

@Override
public void computeAABB(AABB aabb, Transform xf, int childIndex) {
    assert (childIndex < m_count);
    final Tuple2f lower = aabb.lowerBound;
    final Tuple2f upper = aabb.upperBound;
    int i1 = childIndex;
    int i2 = childIndex + 1;
    if (i2 == m_count) {
        i2 = 0;
    }
    final Tuple2f vi1 = m_vertices[i1];
    final Tuple2f vi2 = m_vertices[i2];
    final Rot xfq = xf;
    final Tuple2f xfp = xf.pos;
    float v1x = (xfq.c * vi1.x - xfq.s * vi1.y) + xfp.x;
    float v1y = (xfq.s * vi1.x + xfq.c * vi1.y) + xfp.y;
    float v2x = (xfq.c * vi2.x - xfq.s * vi2.y) + xfp.x;
    float v2y = (xfq.s * vi2.x + xfq.c * vi2.y) + xfp.y;
    lower.x = v1x < v2x ? v1x : v2x;
    lower.y = v1y < v2y ? v1y : v2y;
    upper.x = v1x > v2x ? v1x : v2x;
    upper.y = v1y > v2y ? v1y : v2y;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f)

Aggregations

Tuple2f (spacegraph.util.math.Tuple2f)154 spacegraph.util.math.v2 (spacegraph.util.math.v2)32 Rot (spacegraph.space2d.phys.common.Rot)23 AABB (spacegraph.space2d.phys.collision.AABB)7 Vec2 (spacegraph.space2d.phys.common.Vec2)6 Body2D (spacegraph.space2d.phys.dynamics.Body2D)6 ManifoldPoint (spacegraph.space2d.phys.collision.ManifoldPoint)5 VelocityConstraintPoint (spacegraph.space2d.phys.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)4 Joint (spacegraph.space2d.phys.dynamics.joints.Joint)4 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)4 MyList (spacegraph.space2d.phys.fracture.util.MyList)4 FasterList (jcog.list.FasterList)3 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)3 Shape (spacegraph.space2d.phys.collision.shapes.Shape)3 Transform (spacegraph.space2d.phys.common.Transform)3 DistanceJoint (spacegraph.space2d.phys.dynamics.joints.DistanceJoint)3 MouseJoint (spacegraph.space2d.phys.dynamics.joints.MouseJoint)3 Fragment (spacegraph.space2d.phys.fracture.Fragment)3 Polygon (spacegraph.space2d.phys.fracture.Polygon)3