Search in sources :

Example 16 with Point2d

use of maspack.matrix.Point2d in project artisynth_core by artisynth.

the class Intersector2d method intersectLineSegmentLineSegment.

public int intersectLineSegmentLineSegment(Point2d p1a, Point2d p1b, Point2d p2a, Point2d p2b, ArrayList<Point2d> points) {
    Vector2d dir1 = new Vector2d(p1b);
    dir1.sub(p1a);
    double l1 = dir1.norm();
    // normalize
    dir1.scale(1.0 / l1);
    Vector2d dir2 = new Vector2d(p2b);
    dir2.sub(p2a);
    double l2 = dir2.norm();
    dir2.scale(1.0 / l2);
    // circle test
    Point2d a = new Point2d();
    a.combine(0.5, p1a, 0.5, p1b);
    Point2d b = new Point2d();
    b.combine(0.5, p2a, 0.5, p2b);
    // check if within each other's radii
    if (a.distance(b) > (l1 + l2) / 2 + epsilon) {
        return 0;
    }
    // within range, so intersect lines
    ArrayList<Point2d> tmpPnts = new ArrayList<Point2d>();
    int nAdded = intersectLineLine(p1a, dir1, p2a, dir2, tmpPnts);
    // we already know it lies on both lines, so just check distances
    if (nAdded == 1) {
        Point2d p = tmpPnts.get(0);
        if (p.distance(p1a) + p.distance(p1b) > l1 + epsilon) {
            return 0;
        }
        if (p.distance(p2a) + p.distance(p2b) > l2 + epsilon) {
            return 0;
        }
        points.add(p);
        return 1;
    }
    // parallel
    if (nAdded == 0) {
        return 0;
    }
    // colinear
    Vector2d v = dir1;
    // get direction vector
    double tmp;
    // end points of interval
    // segment 1: [t1s, t1e]
    double t1s = p1a.dot(v);
    double t1e = p1b.dot(v);
    if (t1s < t1e) {
        tmp = t1s;
        t1s = t1e;
        t1e = tmp;
    }
    // segment 2: [t2s, t2e]
    double t2s = p2a.dot(v);
    double t2e = p2b.dot(v);
    if (t2s < t2e) {
        tmp = t2s;
        t2s = t2e;
        t2e = tmp;
    }
    double ts = Math.max(t1s, t2s);
    double te = Math.min(t1e, t2e);
    if (te > ts - epsilon) {
        Vector2d n = new Vector2d();
        n.scaledAdd(-p1a.dot(v), v, p1a);
        a.scaledAdd(ts, v, n);
        b.scaledAdd(te, v, n);
        if (a.distance(b) < epsilon) {
            points.add(a);
            return 1;
        } else {
            points.add(a);
            points.add(b);
            return 2;
        }
    }
    return nAdded;
}
Also used : Vector2d(maspack.matrix.Vector2d) Point2d(maspack.matrix.Point2d) ArrayList(java.util.ArrayList)

Example 17 with Point2d

use of maspack.matrix.Point2d in project artisynth_core by artisynth.

the class Intersector2d method intersectLineLineSegment.

public int intersectLineLineSegment(Point2d p1, Vector2d v1, Point2d p2a, Point2d p2b, ArrayList<Point2d> points) {
    Vector2d v2 = new Vector2d(p2b.x - p2a.x, p2b.y - p2a.y);
    // intersect lines
    ArrayList<Point2d> tmpPnts = new ArrayList<Point2d>();
    int nAdded = intersectLineLine(p1, v1, p2a, v2, tmpPnts);
    if (nAdded == 1) {
        Point2d p = tmpPnts.get(0);
        if (p.distance(p2a) + p.distance(p2b) > p2a.distance(p2b) + epsilon) {
            return 0;
        }
        points.add(p);
        return 1;
    }
    if (nAdded == 2) {
        points.addAll(tmpPnts);
        return 2;
    }
    return 0;
}
Also used : Vector2d(maspack.matrix.Vector2d) Point2d(maspack.matrix.Point2d) ArrayList(java.util.ArrayList)

Example 18 with Point2d

use of maspack.matrix.Point2d in project artisynth_core by artisynth.

the class FreehandTool method mousePressed.

public boolean mousePressed(MouseRayEvent e) {
    if (isVisible()) {
        // DragMode mode = getDragMode (e);
        DragMode mode = getDragMode();
        if (mode != DragMode.OFF) {
            Vector3d isect = new Vector3d();
            int height = myViewer.getScreenHeight();
            myDragMode = mode;
            myPoints.clear();
            intersectRay(isect, e.getRay());
            myPoints.add(new Point2d(isect.x, isect.y));
            System.out.println("isect=" + isect);
            fireDrawToolBeginListeners(e.getModifiersEx());
            return true;
        }
    }
    return false;
}
Also used : Vector3d(maspack.matrix.Vector3d) Point2d(maspack.matrix.Point2d)

Example 19 with Point2d

use of maspack.matrix.Point2d in project artisynth_core by artisynth.

the class FreehandTool method render.

public void render(Renderer renderer, int flags) {
    if (!myVisibleP) {
        return;
    }
    renderer.pushModelMatrix();
    RigidTransform3d X = new RigidTransform3d();
    getToolToWorld(X);
    renderer.mulModelMatrix(X);
    Shading savedShading = renderer.setShading(Shading.NONE);
    float[] rgb = new float[4];
    myLineColor.getRGBColorComponents(rgb);
    renderer.setColor(rgb);
    renderer.setLineWidth(myLineWidth);
    if (myClosedP) {
        renderer.beginDraw(DrawMode.LINE_LOOP);
    } else {
        renderer.beginDraw(DrawMode.LINE_STRIP);
    }
    for (int i = 0; i < myPoints.size(); i++) {
        Point2d p = myPoints.get(i);
        renderer.addVertex(p.x, p.y, 0);
    }
    renderer.endDraw();
    renderer.setLineWidth(1);
    renderer.setShading(savedShading);
    renderer.popModelMatrix();
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) Point2d(maspack.matrix.Point2d) Shading(maspack.render.Renderer.Shading)

Example 20 with Point2d

use of maspack.matrix.Point2d in project artisynth_core by artisynth.

the class FreehandTool method mouseDragged.

public boolean mouseDragged(MouseRayEvent e) {
    if (myDragMode != DragMode.OFF) {
        Vector3d isect = new Vector3d();
        int height = myViewer.getScreenHeight();
        intersectRay(isect, e.getRay());
        myPoints.add(new Point2d(isect.x, isect.y));
        return true;
    }
    return false;
}
Also used : Vector3d(maspack.matrix.Vector3d) Point2d(maspack.matrix.Point2d)

Aggregations

Point2d (maspack.matrix.Point2d)20 ArrayList (java.util.ArrayList)6 Point3d (maspack.matrix.Point3d)5 Vector2d (maspack.matrix.Vector2d)5 Vector3d (maspack.matrix.Vector3d)4 Font (java.awt.Font)3 Face (maspack.geometry.Face)3 BVNode (maspack.geometry.BVNode)2 Boundable (maspack.geometry.Boundable)2 Vertex3d (maspack.geometry.Vertex3d)2 RenderObject (maspack.render.RenderObject)2 HalfEdge (maspack.geometry.HalfEdge)1 RigidTransform3d (maspack.matrix.RigidTransform3d)1 Shading (maspack.render.Renderer.Shading)1 Rectangle (maspack.util.Rectangle)1