Search in sources :

Example 16 with Vector2d

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

the class GLMouseAdapter method mouseDragged.

public void mouseDragged(MouseEvent e) {
    currentCursor = new Point(e.getX(), e.getY());
    int xOff = e.getX() - lastX, yOff = e.getY() - lastY;
    switch(dragAction) {
        case ROTATE:
            {
                viewer.rotate(-xOff / 200.0, -yOff / 200.0);
                break;
            }
        case ZOOM:
            {
                double r = yOff < 0 ? 0.99 : 1.01;
                double s = Math.pow(r, Math.abs(yOff));
                viewer.zoom(s);
                // viewer.repaint();
                break;
            }
        case TRANSLATE:
            {
                double dpp = viewer.centerDistancePerPixel();
                viewer.translate(dpp * xOff, -dpp * yOff);
                break;
            }
        case DRAGGER_ACTION:
            {
                Dragger3d drawTool = viewer.myDrawTool;
                boolean grabbed = false;
                int mods = e.getModifiersEx() & ALL_MODIFIERS;
                if (viewer.myDraggers.size() > 0 || drawTool != null) {
                    MouseRayEvent rayEvt = MouseRayEvent.create(e, viewer);
                    for (Dragger3d d : viewer.myDraggers) {
                        // pass to any visible Dragger3dBase, or any other Dragger3d
                        if (d.isVisible()) {
                            updateDraggerFlags(mods, d);
                            if (d.mouseDragged(rayEvt)) {
                                grabbed = true;
                                break;
                            }
                        }
                    }
                    if (drawTool != null && drawTool.isVisible()) {
                        updateDraggerFlags(mods, drawTool);
                        if (drawTool.mouseDragged(rayEvt)) {
                            grabbed = true;
                        }
                    }
                }
                if (grabbed) {
                    viewer.repaint();
                }
                break;
            }
        case DRAG_SELECT:
            {
                if (viewer.getEllipticSelection()) {
                    Vector2d csize = viewer.getEllipticCursorSize();
                    float x = e.getX();
                    float y = e.getY();
                    // Increase to make the program check for selection more often when dragging a circle.
                    int sensitivity = 4;
                    if (Math.abs(x - dragStartX) > csize.x / sensitivity || Math.abs(y - dragStartY) > csize.y / sensitivity) {
                        checkForSelection(e);
                        // viewer.setSelectionCircle(null);
                        dragStartX = (int) x;
                        dragStartY = (int) y;
                    }
                } else {
                    int x = dragStartX;
                    int y = dragStartY;
                    int w = e.getX() - x;
                    int h = e.getY() - y;
                    if (w == 0) {
                        w = 1;
                    } else if (w < 0) {
                        x = e.getX();
                        w = -w;
                    }
                    if (h == 0) {
                        h = 1;
                    } else if (h < 0) {
                        y = e.getY();
                        h = -h;
                    }
                    viewer.setDragBox(new Rectangle(x, y, w, h));
                    viewer.repaint();
                }
                break;
            }
        case ELLIPTIC_CURSOR_RESIZE:
            {
                Vector2d csize = new Vector2d(ellipticCursorStartSize);
                csize.x = Math.max(1, csize.x + e.getX() - dragStartX);
                csize.y = Math.max(1, csize.y + e.getY() - dragStartY);
                viewer.setEllipticCursorSize(csize);
                break;
            }
    }
    if (viewer.getEllipticCursorActive()) {
        viewer.repaint();
    }
    lastX = e.getX();
    lastY = e.getY();
}
Also used : Vector2d(maspack.matrix.Vector2d) MouseRayEvent(maspack.render.MouseRayEvent) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) Dragger3d(maspack.render.Dragger3d)

Example 17 with Vector2d

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

the class GLColorSelector method flushQueries.

private void flushQueries(GL gl) {
    if (myQueryCount == 0) {
        return;
    }
    // check if any valid ids
    gl.glFlush();
    ByteBuffer pixels = fbo.getPixels(myGl, GL.GL_RGBA);
    int w = fbo.getWidth();
    int h = fbo.getHeight();
    double centerX = (w - 1.0) / 2.0;
    double centerY = (h - 1.0) / 2.0;
    boolean badIdWarningIssued = false;
    int idx = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int r = 0xff & pixels.get();
            int g = 0xff & pixels.get();
            int b = 0xff & pixels.get();
            int a = 0xff & pixels.get();
            int lcolorId = a;
            lcolorId <<= 8;
            lcolorId += b;
            lcolorId <<= 8;
            lcolorId += g;
            lcolorId <<= 8;
            lcolorId += r;
            int colorId = (int) (lcolorId & (0xFFFFFFFF));
            Vector2d ellipticSize = myViewer.getEllipticCursorSize();
            if (myViewer.getEllipticSelection()) {
                double x = (h - i - 1) - centerX;
                double y = j - centerY;
                if (sqr(x / ellipticSize.x) + sqr(y / ellipticSize.y) > 1) {
                    colorId = 0;
                }
            }
            if (colorId != 0) {
                // color id are incremented by 1
                int id = colorId / ID_STEP - ID_OFFSET + myQueryBase;
                if (id < 0 || id > myTotalMaxQ) {
                    if (!badIdWarningIssued) {
                        System.out.printf("Warning: Color selection id 0x%x out of range; " + "was GL_LIGHTING enabled or glColor() called during selection?\n", id);
                        badIdWarningIssued = true;
                    }
                } else {
                    myQueryBuffer[idx] = id;
                }
            }
            ++idx;
        }
    }
    BufferUtilities.freeDirectBuffer(pixels);
    // clear color FBO without writing depth
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    myQueryBase += myQueryCount;
    myQueryTotal += myQueryCount;
    myQueryCount = 0;
}
Also used : Vector2d(maspack.matrix.Vector2d) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Vector2d

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

the class GLSelector method setupSelection.

public void setupSelection(GL gl) {
    myGl = gl;
    myTotalMaxQ = myViewer.numSelectionQueriesNeeded();
    // nothing to select
    if (myTotalMaxQ == 0) {
        return;
    }
    myCurrentMaxQ = myTotalMaxQ;
    myCurrentIdx = -1;
    myIdxBase = 0;
    // paranoid ...
    myMaxQStack.clear();
    // paranoid ...
    myIdxBaseStack.clear();
    // restrict the viewport to the specified selection region
    mySavedViewport = myViewer.getViewport(gl);
    // apply pick
    int myViewW = mySavedViewport[2];
    int myViewH = mySavedViewport[3];
    myViewW = (int) Math.ceil(myRectW);
    myViewH = (int) Math.ceil(myRectH);
    float x = (float) myRectX;
    float y = (float) (mySavedViewport[3] - myRectY);
    myViewer.setPickMatrix(x, y, (float) myRectW, (float) myRectH, mySavedViewport);
    Vector2d zRange = new Vector2d();
    myViewer.getZRange(zRange);
    myViewer.pushProjectionMatrix();
    myViewer.setViewVolume(/*near=*/
    zRange.x, /*far=*/
    zRange.y);
    savedResize = myViewer.isAutoResizeEnabled();
    myViewer.setAutoResizeEnabled(false);
    savedViewport = myViewer.isAutoViewportEnabled();
    myViewer.setAutoViewportEnabled(false);
    myViewer.getBackgroundColor(savedBackgroundColor);
    myViewer.setBackgroundColor(0, 0, 0, 0);
    savedBlend = myViewer.isBlendingEnabled();
    if (savedBlend) {
        myViewer.setBlendingEnabled(false);
    }
    savedMultisampled = myViewer.isMultiSampleEnabled();
    if (savedMultisampled) {
        myViewer.setMultiSampleEnabled(false);
    }
    // set the initial color to be 0
    myViewer.getFrontColor(savedColor);
    activateFBO(gl, myViewW, myViewH);
    myViewer.setViewport(gl, 0, 0, myViewW, myViewH);
    // disable lighting
    savedLighting = myViewer.setLightingEnabled(false);
}
Also used : Vector2d(maspack.matrix.Vector2d)

Example 19 with Vector2d

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

the class MatricesUBO method putMatrix.

public static void putMatrix(ByteBuffer buff, AffineTransform2dBase T) {
    Matrix2dBase M = T.getMatrix();
    Vector2d b = T.getOffset();
    buff.putFloat((float) M.m00);
    buff.putFloat((float) M.m10);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat((float) M.m01);
    buff.putFloat((float) M.m11);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat(0);
    buff.putFloat((float) b.x);
    buff.putFloat((float) b.y);
    buff.putFloat(0);
    buff.putFloat((float) 1);
}
Also used : Vector2d(maspack.matrix.Vector2d) Matrix2dBase(maspack.matrix.Matrix2dBase)

Example 20 with Vector2d

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

the class MeshICP method align.

public static AffineTransform3d align(PolygonalMesh mesh1, PolygonalMesh mesh2, AlignmentType alignType, double eps, int maxIters, ArrayList<Point3d> out) {
    // align mesh1 to mesh2, then return inverse transform
    // OBBTree obbt = mesh2.getObbtree(); // for projecting points onto mesh1
    // TriangleIntersector ti = new TriangleIntersector(); // stores information in nearest face
    // algorithm
    // barycentric coordinates of closest
    Vector2d coords = new Vector2d();
    // point
    // mean distance error
    double err = Double.POSITIVE_INFINITY;
    // previous mean error
    double prevErr = Double.POSITIVE_INFINITY;
    // number of iterations
    int iters = 0;
    // get points
    ArrayList<Point3d> pnts = new ArrayList<Point3d>();
    ArrayList<Point3d> projected = new ArrayList<Point3d>();
    // incremental transformation
    AffineTransform3d transInc = new AffineTransform3d();
    // total transform
    AffineTransform3d transOut = new AffineTransform3d();
    // clear point sets
    pnts.clear();
    for (Vertex3d v : mesh1.getVertices()) {
        pnts.add(new Point3d(v.getWorldPoint()));
    }
    do {
        projected.clear();
        for (int i = 0; i < pnts.size(); i++) {
            Point3d p = pnts.get(i);
            Point3d q = new Point3d();
            BVFeatureQuery.getNearestFaceToPoint(q, coords, mesh2, p);
            // q is the closest point on mesh2 to p
            projected.add(q);
        }
        switch(alignType) {
            case AFFINE:
                // affine
                transInc.fit(projected, pnts);
                break;
            case ORTHOGONAL:
                // allow orthogonal scaling
                transInc.fitOrthogonal(projected, pnts);
                break;
            case RIGID:
                // rigid no scaling
                transInc.fitRigid(projected, pnts, false);
                break;
            case RIGID_WITH_SCALING:
                // rigid with scaling
                transInc.fitRigid(projected, pnts, true);
                break;
        }
        // concatenate transforms through pre-multiplication
        transOut.mul(transInc, transOut);
        // compute error
        prevErr = err;
        err = 0;
        for (int i = 0; i < pnts.size(); i++) {
            pnts.get(i).transform(transInc);
            err += pnts.get(i).distance(projected.get(i));
        }
        // mean error
        err = err / pnts.size();
        iters++;
    // stop when we mean error has converged or maxIters is reached.
    } while (Math.abs(err - prevErr) > eps && iters < maxIters);
    // copy projected points to out (so in same reference coordinate as mesh2)
    if (out != null) {
        out.clear();
        for (int i = 0; i < projected.size(); i++) {
            Point3d p = projected.get(i);
            // p.transform(transOut);
            out.add(p);
        }
    }
    // transOut now holds mesh1 -> mesh2 transform, we want opposite
    transOut.invert();
    return transOut;
}
Also used : Vector2d(maspack.matrix.Vector2d) Point3d(maspack.matrix.Point3d) ArrayList(java.util.ArrayList) AffineTransform3d(maspack.matrix.AffineTransform3d)

Aggregations

Vector2d (maspack.matrix.Vector2d)35 Point3d (maspack.matrix.Point3d)16 Vector3d (maspack.matrix.Vector3d)11 Face (maspack.geometry.Face)9 Vertex3d (maspack.geometry.Vertex3d)8 ArrayList (java.util.ArrayList)7 BVFeatureQuery (maspack.geometry.BVFeatureQuery)7 RigidTransform3d (maspack.matrix.RigidTransform3d)6 Point2d (maspack.matrix.Point2d)5 PolygonalMesh (maspack.geometry.PolygonalMesh)4 ContactPoint (artisynth.core.mechmodels.ContactPoint)3 Point (artisynth.core.mechmodels.Point)3 Point (java.awt.Point)3 VectorNd (maspack.matrix.VectorNd)3 PointAttachment (artisynth.core.mechmodels.PointAttachment)2 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)2 Rectangle (java.awt.Rectangle)2 HashMap (java.util.HashMap)2 BVTree (maspack.geometry.BVTree)2 Matrix2dBase (maspack.matrix.Matrix2dBase)2