Search in sources :

Example 26 with RigidTransform3d

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

the class NURBSCurveBase method render.

/**
 * {@inheritDoc}
 */
public void render(Renderer renderer, RenderProps props, int flags) {
    boolean selecting = renderer.isSelecting();
    int numc = numControlPoints();
    if (numc == 0) {
        return;
    }
    renderer.pushModelMatrix();
    if (myXObjToWorld != RigidTransform3d.IDENTITY) {
        RigidTransform3d XOW = new RigidTransform3d(myXObjToWorld);
        renderer.mulModelMatrix(XOW);
    }
    renderer.setShading(Shading.NONE);
    if (myDrawControlShapeP) {
        // draw the control polygon
        if (props.getDrawEdges()) {
            renderer.setLineWidth(props.getEdgeWidth());
            if (!selecting) {
                renderer.setColor(props.getEdgeOrLineColorF());
            }
            if (myClosedP) {
                renderer.beginDraw(DrawMode.LINE_LOOP);
            } else {
                renderer.beginDraw(DrawMode.LINE_STRIP);
            }
            for (int i = 0; i < numc; i++) {
                Vector4d cpnt = myCtrlPnts.get(i);
                renderer.addVertex(cpnt.x, cpnt.y, cpnt.z);
            }
            renderer.endDraw();
        }
        // draw the control points
        drawControlPoints(renderer, props, flags);
    }
    // draw the curve itself
    if (!selecting) {
        renderer.setColor(props.getLineColorF());
    }
    double len = computeControlPolygonLength();
    double res = myResolution * renderer.distancePerPixel(myXObjToWorld.p);
    int nsegs = (int) Math.max(10, len / res);
    Point3d pnt = new Point3d();
    renderer.setLineWidth(props.getLineWidth());
    renderer.beginDraw(DrawMode.LINE_LOOP);
    double[] urange = new double[2];
    getRange(urange);
    for (int i = 0; i < nsegs + 1; i++) {
        eval(pnt, urange[0] + (urange[1] - urange[0]) * i / nsegs);
        renderer.addVertex(pnt);
    }
    renderer.endDraw();
    renderer.setLineWidth(1);
    renderer.setShading(Shading.FLAT);
    renderer.popModelMatrix();
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) Vector4d(maspack.matrix.Vector4d) Point3d(maspack.matrix.Point3d)

Example 27 with RigidTransform3d

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

the class PolygonalMesh method computePrincipalAxes.

public static RigidTransform3d computePrincipalAxes(PolygonalMesh mesh) {
    Vector3d mov1 = new Vector3d();
    Vector3d mov2 = new Vector3d();
    Vector3d pov = new Vector3d();
    double vol = mesh.computeVolumeIntegrals(mov1, mov2, pov);
    double mass = vol;
    Point3d cov = new Point3d();
    // center of volume
    cov.scale(1.0 / vol, mov1);
    // [c], skew symmetric
    Matrix3d covMatrix = new Matrix3d(0, -cov.z, cov.y, cov.z, 0, -cov.x, -cov.y, cov.x, 0);
    // J
    Matrix3d J = new Matrix3d((mov2.y + mov2.z), -pov.z, -pov.y, -pov.z, (mov2.x + mov2.z), -pov.x, -pov.y, -pov.x, (mov2.x + mov2.y));
    // Jc = J + m[c][c]
    Matrix3d Jc = new Matrix3d();
    Jc.mul(covMatrix, covMatrix);
    Jc.scale(mass);
    Jc.add(J);
    // Compute eigenvectors and eigenvlaues of Jc
    SymmetricMatrix3d JcSymmetric = new SymmetricMatrix3d(Jc);
    Vector3d lambda = new Vector3d();
    Matrix3d U = new Matrix3d();
    JcSymmetric.getEigenValues(lambda, U);
    // Construct the rotation matrix
    RotationMatrix3d R = new RotationMatrix3d();
    R.set(U);
    lambda.absolute();
    if (lambda.x > lambda.y && lambda.z > lambda.y) {
        R.rotateZDirection(new Vector3d(R.m01, R.m11, R.m21));
    } else if (lambda.x > lambda.z && lambda.y > lambda.z) {
        R.rotateZDirection(new Vector3d(R.m00, R.m10, R.m20));
    }
    return (new RigidTransform3d(cov, R));
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) SymmetricMatrix3d(maspack.matrix.SymmetricMatrix3d) RotationMatrix3d(maspack.matrix.RotationMatrix3d) Matrix3d(maspack.matrix.Matrix3d) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) SymmetricMatrix3d(maspack.matrix.SymmetricMatrix3d) RotationMatrix3d(maspack.matrix.RotationMatrix3d)

Example 28 with RigidTransform3d

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

the class NURBSObject method set.

protected void set(NURBSObject nobj) {
    myCtrlPnts.clear();
    for (Vector4d cpnt : nobj.myCtrlPnts) {
        myCtrlPnts.add(new Vector4d(cpnt));
    }
    myCtrlPntSelected = (ArrayList<Boolean>) nobj.myCtrlPntSelected.clone();
    if (nobj.myXObjToWorld.isIdentity()) {
        myXObjToWorld = RigidTransform3d.IDENTITY;
    } else {
        myXObjToWorld = new RigidTransform3d(nobj.myXObjToWorld);
    }
    myRenderProps = nobj.myRenderProps.clone();
}
Also used : Vector4d(maspack.matrix.Vector4d) RigidTransform3d(maspack.matrix.RigidTransform3d)

Example 29 with RigidTransform3d

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

the class SphereIntersector method bound.

/**
 * Bounds the intersection of two spheres
 * @param c1 center of sphere 1
 * @param r1 radius of sphere 1
 * @param c2 center of sphere 2
 * @param r2 radius of sphere 2
 * @return oriented bounding box
 */
public static OBB bound(Point3d c1, double r1, Point3d c2, double r2) {
    Vector3d axis = new Vector3d();
    Vector3d xdv = new Vector3d();
    double v = intersect(c1, r1, c2, r2, axis, xdv);
    if (v <= 0) {
        return null;
    }
    RigidTransform3d trans = new RigidTransform3d();
    Vector3d widths = new Vector3d();
    if (xdv.y <= r2 - r1) {
        // inside r2
        trans.setTranslation(c2);
        widths.set(r2, r2, r2);
    } else if (xdv.y <= r1 - r2) {
        // inside r1
        trans.setTranslation(c1);
        widths.set(r1, r1, r1);
    } else {
        trans.R.rotateZDirection(axis);
        double x = xdv.x;
        double d = xdv.y;
        double d1 = x;
        double d2 = d - x;
        // h1 is height of cap 1
        // h2 is height of cap 2
        double h1 = r1 - d1;
        double h2 = r2 - d2;
        if (h1 > r1) {
            // r1 sticks out
            widths.x = r1;
            widths.y = r1;
        } else if (h2 > r2) {
            // r2 sticks out
            widths.x = r2;
            widths.y = r2;
        } else {
            // bounded by intersection circle
            double r = Math.sqrt(r1 * r1 - x * x);
            widths.x = r;
            widths.y = r;
        }
        widths.z = h1 + h2;
        // center-z is half-way between (x+h1) and (x-h2)
        Vector3d c = new Vector3d(0, 0, x + (h1 - h2) / 2);
        c.transform(trans.R);
        c.add(c1);
        trans.setTranslation(c);
    }
    return new OBB(widths, trans);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) Vector3d(maspack.matrix.Vector3d)

Example 30 with RigidTransform3d

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

the class GL2Viewer method drawPoints.

@Override
public void drawPoints(RenderObject robj, int gidx, RenderInstances rinst) {
    boolean selecting = isSelecting();
    boolean hasColors = ((robj.hasColors() || rinst.hasColors()) && hasVertexColoring());
    boolean useColors = hasColors && !selecting && (myActiveColor == ActiveColor.DEFAULT);
    boolean useHSV = isHSVColorInterpolationEnabled();
    // update state
    maybeUpdateState(gl);
    // if use vertex colors, get them to track glColor
    int savedShading = 0;
    if (useColors) {
        savedShading = enableVertexColoring(useHSV);
    }
    boolean useDisplayList = (!selecting || !hasColors) && (!robj.isTransient());
    GL2VersionedObject gvo = null;
    boolean compile = true;
    if (useDisplayList) {
        RenderInstancesKey key = new RenderInstancesKey(rinst.getIdentifier(), robj.getIdentifier(), DrawType.POINTS, gidx);
        // get snapshot of version information
        RenderInstancesFingerprint fingerprint = new RenderInstancesFingerprint(rinst.getVersionInfo(), robj.getVersionInfo());
        gvo = myGLResources.getVersionedObject(key);
        if (gvo == null || gvo.disposeInvalid(gl)) {
            gvo = myGLResources.allocateVersionedObject(gl, key, fingerprint);
            compile = true;
        } else {
            compile = !(gvo.compareExchangeFingerPrint(fingerprint));
        }
    }
    if (compile) {
        if (gvo != null) {
            gvo.beginCompile(gl);
        }
        // prevent writes to robj and rinst
        robj.readLock();
        rinst.readLock();
        int ninstances = rinst.numInstances();
        int[] instances = rinst.getInstances();
        int ipos = rinst.getInstanceTypeOffset();
        int tpos = rinst.getInstanceTransformOffset();
        int cpos = rinst.getInstanceColorOffset();
        int spos = rinst.getInstanceScaleOffset();
        int stride = rinst.getInstanceStride();
        InstanceTransformType[] type = RenderInstances.getTransformTypes();
        boolean hasInstanceScales = rinst.hasScales();
        boolean hasInstanceColors = useColors && rinst.hasColors();
        for (int i = 0; i < ninstances; ++i) {
            int iidx = instances[ipos];
            int tidx = instances[tpos];
            int cidx = instances[cpos];
            int sidx = instances[spos];
            gl.glPushMatrix();
            // transform
            switch(type[iidx]) {
                case AFFINE:
                    {
                        AffineTransform3d aff = rinst.getAffine(tidx);
                        mulTransform(gl, aff);
                        break;
                    }
                case FRAME:
                    {
                        RigidTransform3d frame = rinst.getFrame(tidx);
                        mulTransform(gl, frame);
                        break;
                    }
                case POINT:
                    {
                        float[] trans = rinst.getPoint(tidx);
                        gl.glTranslatef(trans[0], trans[1], trans[2]);
                        break;
                    }
            }
            if (hasInstanceScales && (sidx >= 0)) {
                Double s = rinst.getScale(sidx);
                gl.glScaled(s, s, s);
            }
            if (hasInstanceColors && (cidx >= 0)) {
                byte[] c = rinst.getColor(cidx);
                gl.glColor4ub(c[0], c[1], c[2], c[3]);
            }
            // draw raw object
            drawRawPoints(gl, robj, gidx, 0, robj.numPoints(gidx), robj.hasNormals(), !hasInstanceColors && useColors, !selecting & robj.hasTextureCoords(), useHSV);
            gl.glPopMatrix();
            ipos += stride;
            tpos += stride;
            cpos += stride;
            spos += stride;
        }
        robj.readUnlock();
        rinst.readUnlock();
        if (gvo != null) {
            gvo.endCompile(gl);
            gvo.draw(gl);
        }
    } else {
        gvo.draw(gl);
    }
    // disable color tracking
    if (useColors) {
        disableVertexColoring(useHSV, savedShading);
    }
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) InstanceTransformType(maspack.render.RenderInstances.InstanceTransformType) Point(java.awt.Point) AffineTransform3d(maspack.matrix.AffineTransform3d)

Aggregations

RigidTransform3d (maspack.matrix.RigidTransform3d)206 Vector3d (maspack.matrix.Vector3d)56 Point3d (maspack.matrix.Point3d)48 PolygonalMesh (maspack.geometry.PolygonalMesh)21 MechModel (artisynth.core.mechmodels.MechModel)19 RigidBody (artisynth.core.mechmodels.RigidBody)18 AxisAngle (maspack.matrix.AxisAngle)18 RotationMatrix3d (maspack.matrix.RotationMatrix3d)17 AffineTransform3d (maspack.matrix.AffineTransform3d)13 FemModel3d (artisynth.core.femmodels.FemModel3d)11 RenderProps (maspack.render.RenderProps)11 FemNode3d (artisynth.core.femmodels.FemNode3d)9 Color (java.awt.Color)7 Point (java.awt.Point)7 Matrix3d (maspack.matrix.Matrix3d)7 Shading (maspack.render.Renderer.Shading)7 LinearMaterial (artisynth.core.materials.LinearMaterial)6 Renderable (maspack.render.Renderable)6 ArrayList (java.util.ArrayList)5 Vector2d (maspack.matrix.Vector2d)5