Search in sources :

Example 1 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class BVFeatureQueryTest method nearestVertexAndEdge.

private void nearestVertexAndEdge(MeshBase mesh, BVTree bvh, RigidTransform3d X, Point3d center, double diameter) {
    BVFeatureQuery query = new BVFeatureQuery();
    mesh.setMeshToWorld(X);
    bvh.setBvhToWorld(X);
    int testcnt = 1000;
    Point3d pnt = new Point3d();
    Vector3d dir = new Vector3d();
    NearestEdgeInfo einfo = new NearestEdgeInfo();
    double tol = EPS * (center.norm() + diameter);
    for (int i = 0; i < testcnt; i++) {
        pnt.setRandom();
        pnt.scale(2 * diameter);
        pnt.add(center);
        pnt.transform(X, pnt);
        dir.setRandom();
        dir.normalize();
        ArrayList<Vertex3d> nearestVertices = getNearestVerticesToPoint(mesh, pnt);
        Vertex3d vtx = query.nearestVertexToPoint(bvh, pnt);
        if (!nearestVertices.contains(vtx)) {
            System.out.println("Computed vertex:");
            System.out.println("Vertex " + vtx.getIndex());
            System.out.println("Possible vertices:");
            for (int k = 0; k < nearestVertices.size(); k++) {
                System.out.println("Vertex " + nearestVertices.get(i).getIndex());
            }
            throw new TestException("Vertex " + vtx.getIndex() + " computed as nearest does not match brute force calculation");
        }
        ArrayList<NearestEdgeInfo> nearestEdges = getNearestEdgesToPoint(mesh, pnt, tol);
        einfo.myEdge = query.nearestEdgeToPoint(einfo.myNear, einfo.myCoord, bvh, pnt);
        if (einfo.myEdge == null) {
            if (nearestEdges.size() > 0) {
                throw new TestException("No nearest edge found, but brute force discovered " + nearestEdges.size());
            }
        } else {
            if (!einfo.edgeFaceIsContained(nearestEdges, tol)) {
                System.out.println("Computed face:");
                System.out.println("Edge " + einfo.edgeString() + " near: " + einfo.myNear.toString("%10.6f") + ", coord " + einfo.myCoord.value + ", d=" + einfo.myDist);
                System.out.println("Possible edges:");
                for (int k = 0; k < nearestEdges.size(); k++) {
                    NearestEdgeInfo ei = nearestEdges.get(k);
                    System.out.println("Edge " + ei.edgeString() + " near: " + ei.myNear.toString("%10.6f") + ", coords " + ei.myCoord.value + ", d=" + ei.myDist);
                }
                throw new TestException("Edge " + einfo.edgeString() + " computed as nearest does not match brute force calculation");
            }
        }
    }
}
Also used : TestException(maspack.util.TestException) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d)

Example 2 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class BVFeatureQueryTest method pointInsideCheck.

private boolean pointInsideCheck(Point3d pnt, double tol, PolygonalMesh mesh, BVTree bvh, BVFeatureQuery query, TriangleIntersector intersector) {
    boolean isInside = query.isInsideOrientedMesh(bvh, pnt, tol);
    boolean isInsideCheck;
    // boolean isInsideCheck = obbTree.isInside (pnt, intersector, tol);
    // if (isInside != isInsideCheck) {
    // throw new TestException (
    // "query.isInsideMesh returned "+isInside+
    // ", OBBTree.isInside returned "+isInsideCheck);
    // }
    InsideQuery status = query.isInsideMesh(mesh, bvh, pnt, tol);
    // }
    if (status == InsideQuery.UNSURE) {
        System.out.println("isInsideMesh() did not converge");
        return false;
    } else {
        isInsideCheck = (status == InsideQuery.INSIDE);
        if (isInside != isInsideCheck) {
            System.out.println("pnt=" + pnt);
            System.out.println("last case: " + query.lastCase);
            throw new TestException("query.isInsideMesh returned  " + isInside + ", OBB.isInsideRayTest returned " + isInsideCheck);
        }
    }
    return isInside;
}
Also used : TestException(maspack.util.TestException) InsideQuery(maspack.geometry.BVFeatureQuery.InsideQuery)

Example 3 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class BVFeatureQueryTest method nearestFaceTest.

// private void getNearestFaceFromObbtree (
// NearestFaceInfo ninfo, OBBTree obbTree, Point3d pnt) {
// TriangleIntersector ti = new TriangleIntersector();
// ninfo.myFace = obbTree.nearestFace (
// pnt, null, ninfo.myNear, ninfo.myCoords, ti);
// }
// private void getNearestFaceIntersectedByRayFromObbtree (
// NearestFaceInfo ninfo, OBBTree obbTree, Point3d pnt, Vector3d dir) {
// //TriangleIntersector ti = new TriangleIntersector();
// BVFeatureQuery query = new BVFeatureQuery();
// Vector3d coords = new Vector3d();
// //ninfo.myFace = obbTree.intersect (pnt, dir, coords, ti);
// ninfo.myFace = query.nearestFaceAlongRay (
// null, coords, obbTree, pnt, dir);
// if (ninfo.myFace != null) {
// ninfo.myDist = coords.x;
// ninfo.myCoords.x = coords.y;
// ninfo.myCoords.y = coords.z;
// ninfo.myFace.computePoint (ninfo.myNear, ninfo.myCoords);
// }
// }
private void nearestFaceTest(PolygonalMesh mesh, BVTree bvh, RigidTransform3d X, Point3d center, double diameter) {
    BVFeatureQuery query = new BVFeatureQuery();
    mesh.setMeshToWorld(X);
    bvh.setBvhToWorld(X);
    int testcnt = 1000;
    Point3d pnt = new Point3d();
    Vector3d dir = new Vector3d();
    NearestFaceInfo ninfo = new NearestFaceInfo();
    NearestFaceInfo rinfo = new NearestFaceInfo();
    double tol = EPS * (center.norm() + diameter);
    for (int i = 0; i < testcnt; i++) {
        pnt.setRandom();
        pnt.scale(2 * diameter);
        pnt.add(center);
        pnt.transform(X, pnt);
        dir.setRandom();
        dir.normalize();
        ArrayList<NearestFaceInfo> nearestFaces = getNearestFacesToPoint(mesh, pnt, tol);
        ninfo.myFace = query.nearestFaceToPoint(ninfo.myNear, ninfo.myCoords, bvh, pnt);
        if (!ninfo.pointFaceIsContained(nearestFaces, tol)) {
            System.out.println("Computed face:");
            System.out.println("Face " + ninfo.myFace.getIndex() + " near: " + ninfo.myNear.toString("%10.6f") + ", coords " + ninfo.myCoords);
            System.out.println("Possible faces:");
            for (int k = 0; k < nearestFaces.size(); k++) {
                NearestFaceInfo ni = nearestFaces.get(k);
                System.out.println("Face " + ni.myFace.getIndex() + " near: " + ni.myNear.toString("%10.6f"));
            }
            throw new TestException("Face " + ninfo.myFace.getIndex() + " computed as nearest does not match brute force calculation");
        }
        ArrayList<NearestFaceInfo> nearestRayFaces = getNearestFacesToRay(mesh, pnt, dir, tol);
        rinfo.myFace = query.nearestFaceAlongRay(rinfo.myNear, rinfo.myCoords3, bvh, pnt, dir);
        if (rinfo.myFace == null) {
            if (nearestRayFaces.size() != 0) {
                System.out.println("Possible faces:");
                for (int k = 0; k < nearestRayFaces.size(); k++) {
                    NearestFaceInfo ni = nearestRayFaces.get(k);
                    System.out.printf(" Face %d dist=%g near=%s\n", ni.myFace.getIndex(), ni.myDist, ni.myNear.toString("%10.6f"));
                }
                throw new TestException("nearestFaceIntersectedByRay returned null; " + "brute force did not");
            }
        } else if (!rinfo.lineFaceIsContained(nearestRayFaces, tol)) {
            System.out.println("Computed face:");
            System.out.println("Face " + rinfo.myFace.getIndex() + " near: " + rinfo.myNear.toString("%10.6f") + ", coords " + rinfo.myCoords3);
            System.out.println("Possible faces:");
            for (int k = 0; k < nearestRayFaces.size(); k++) {
                NearestFaceInfo ni = nearestRayFaces.get(k);
                System.out.println("Face " + ni.myFace.getIndex() + " near: " + ni.myNear.toString("%10.6f") + ", coords " + ni.myCoords3);
            }
            throw new TestException("Face " + rinfo.myFace.getIndex() + " computed as nearest does not match brute force calculation");
        }
    // NearestFaceInfo oinfo = new NearestFaceInfo();
    // if (bvh instanceof OBBTree) {
    // OBBTree obbTree = (OBBTree)bvh;
    // getNearestFaceFromObbtree (oinfo, obbTree, pnt);
    // if (oinfo.myFace != ninfo.myFace) {
    // if (!ninfo.myNear.epsilonEquals (oinfo.myNear, tol)) {
    // System.out.println ("Query and OBBtree results differ:");
    // System.out.println (
    // "Face "+ninfo.myFace.getIndex()+
    // ", near="+ ninfo.myNear.toString ("%10.6f"));
    // System.out.println (
    // "Face "+oinfo.myFace.getIndex()+
    // ", near="+ oinfo.myNear.toString ("%10.6f"));
    // }
    // }
    // if (!oinfo.pointFaceIsContained (nearestFaces, tol)) {
    // System.out.println ("Computed face from Obbtree:");
    // System.out.println (
    // "Face "+ oinfo.myFace.getIndex() + " near: " +
    // oinfo.myNear.toString ("%10.6f"));
    // System.out.println ("Possible faces:");
    // for (int k=0; k<nearestFaces.size(); k++) {
    // NearestFaceInfo ni = nearestFaces.get(k);
    // System.out.println (
    // "Face "+ ni.myFace.getIndex() + " near: " +
    // ni.myNear.toString ("%10.6f"));
    // }
    // throw new TestException (
    // "Face "+oinfo.myFace.getIndex()+
    // " computed as nearest does not match brute force calculation");
    // }
    // getNearestFaceIntersectedByRayFromObbtree (oinfo, obbTree, pnt, dir);
    // if ((oinfo.myFace==null) != (rinfo.myFace==null)) {
    // if (oinfo.myFace!=null) {
    // System.out.println ("obbTree dist=" + oinfo.myDist);
    // }
    // else if (rinfo.myFace!=null) {
    // System.out.println ("query dist=" + rinfo.myDist);
    // }
    // throw new TestException (
    // "Different results computing nearest face to ray via query "+
    // "(face="+rinfo.myFace+") and OBBTree (face="+oinfo.myFace+")");
    // }
    // if (rinfo.myFace != oinfo.myFace) {
    // if (!rinfo.myNear.epsilonEquals (oinfo.myNear, tol)) {
    // System.out.println ("Query and OBBtree results differ:");
    // System.out.println (
    // "Face "+rinfo.myFace.getIndex()+
    // ", near="+ rinfo.myNear.toString ("%10.6f"));
    // System.out.println (
    // "Face "+oinfo.myFace.getIndex()+
    // ", near="+ oinfo.myNear.toString ("%10.6f"));
    // }
    // }
    // }
    }
}
Also used : TestException(maspack.util.TestException) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d)

Example 4 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class CholeskyDecompositionTest method testDecomposition.

public void testDecomposition(int nrows, int ncols) {
    MatrixNd M1 = new MatrixNd(nrows, ncols);
    M1.setRandom();
    if (nrows == ncols) {
        M1.mulTranspose(M1);
    }
    Exception eActual = null;
    Exception eExpected = null;
    if (nrows != ncols) {
        eExpected = new ImproperSizeException("Matrix not square");
    }
    try {
        chol.factor(M1);
    } catch (Exception e) {
        eActual = e;
    }
    MatrixTest.checkExceptions(eActual, eExpected);
    if (eActual == null) {
        int n = nrows;
        MatrixNd L = new MatrixNd(n, n);
        MatrixNd LLT = new MatrixNd(n, n);
        chol.get(L);
        LLT.mulTransposeRight(L, L);
        if (!LLT.epsilonEquals(M1, EPSILON)) {
            throw new TestException("LLT=\n" + LLT.toString("%9.4f") + "expected:\n" + M1.toString("%9.4f"));
        }
        double condEst = chol.conditionEstimate(M1);
        // check vector solver
        VectorNd b = new VectorNd(n);
        for (int i = 0; i < n; i++) {
            b.set(i, RandomGenerator.get().nextDouble() - 0.5);
        }
        VectorNd x = new VectorNd(n);
        VectorNd Mx = new VectorNd(n);
        chol.solve(x, b);
        Mx.mul(M1, x);
        if (!Mx.epsilonEquals(b, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "Mx=" + Mx.toString("%9.4f") + "b=" + b.toString("%9.4f"));
        }
        // check matrix solver
        MatrixNd B = new MatrixNd(n, 3);
        B.setRandom();
        MatrixNd X = new MatrixNd(n, 3);
        MatrixNd MX = new MatrixNd(n, 3);
        chol.solve(X, B);
        MX.mul(M1, X);
        if (!MX.epsilonEquals(B, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "MX=" + MX.toString("%9.4f") + "B=" + B.toString("%9.4f"));
        }
        // check L solve
        VectorNd Lx = new VectorNd(n);
        chol.solveL(x, b);
        Lx.mul(L, x);
        if (!Lx.epsilonEquals(b, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "Lx=" + Lx.toString("%9.4f") + "b=" + b.toString("%9.4f"));
        }
        // check L matrix solve
        MatrixNd LX = new MatrixNd(n, 3);
        chol.solveL(X, B);
        LX.mul(L, X);
        if (!LX.epsilonEquals(B, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "LX=" + LX.toString("%9.4f") + "B=" + B.toString("%9.4f"));
        }
        // check left L solve
        VectorNd xL = new VectorNd(n);
        chol.leftSolveL(x, b);
        xL.mulTranspose(L, x);
        if (!xL.epsilonEquals(b, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "xL=" + xL.toString("%9.4f") + "b=" + b.toString("%9.4f"));
        }
        // check L matrix solve
        MatrixNd XL = new MatrixNd(3, n);
        MatrixNd BT = new MatrixNd(3, n);
        BT.transpose(B);
        chol.leftSolveL(X, BT);
        XL.mul(X, L);
        if (!XL.epsilonEquals(BT, EPSILON * condEst)) {
            throw new TestException("solution failed:\n" + "XL=" + XL.toString("%9.4f") + "BT=" + BT.toString("%9.4f"));
        }
        // check determinant
        if (n <= 3) {
            double det;
            if (n == 1) {
                det = M1.get(0, 0);
            } else if (n == 2) {
                det = M1.get(0, 0) * M1.get(1, 1) - M1.get(0, 1) * M1.get(1, 0);
            } else // n == 3
            {
                det = M1.get(0, 0) * M1.get(1, 1) * M1.get(2, 2) + M1.get(0, 1) * M1.get(1, 2) * M1.get(2, 0) + M1.get(0, 2) * M1.get(1, 0) * M1.get(2, 1) - M1.get(0, 2) * M1.get(1, 1) * M1.get(2, 0) - M1.get(0, 0) * M1.get(1, 2) * M1.get(2, 1) - M1.get(0, 1) * M1.get(1, 0) * M1.get(2, 2);
            }
            if (Math.abs(det - chol.determinant()) > Math.abs(det * condEst * EPSILON)) {
                throw new TestException("determinant failed: got " + chol.determinant() + " expected " + det + "\nM=\n" + M1.toString("%9.4f"));
            }
        }
        // check inverse
        MatrixNd MI = new MatrixNd(n, n);
        MatrixNd IMI = new MatrixNd(n, n);
        chol.inverse(MI);
        IMI.mul(M1, MI);
        MatrixNd I = new MatrixNd(n, n);
        I.setIdentity();
        if (!IMI.epsilonEquals(I, EPSILON * condEst)) {
            throw new TestException("failed inverse:\n" + MI.toString("%9.4f") + "M1=\n" + M1.toString("%9.4f"));
        }
        // check column additional
        CholeskyDecomposition inc = new CholeskyDecomposition();
        MatrixNd Msub = new MatrixNd(0, 0);
        for (int j = 0; j < n; j++) {
            Msub.setSize(j + 1, j + 1);
            M1.getSubMatrix(0, 0, Msub);
            VectorNd col = new VectorNd(j + 1);
            Msub.getColumn(j, col);
            inc.addRowAndColumn(col);
            checkFactorization(inc, Msub);
        }
        if (n > 3) {
            inc.deleteRowAndColumn(0);
            Msub.setSize(n - 1, n - 1);
            M1.getSubMatrix(1, 1, Msub);
            checkFactorization(inc, Msub);
            Msub.setSize(n - 2, n - 2);
            M1.getSubMatrix(1, 1, Msub);
            inc.deleteRowAndColumn(n - 2);
            checkFactorization(inc, Msub);
            MatrixNd Msubsub = new MatrixNd(n - 3, n - 3);
            int delIdx = n / 2 - 1;
            int[] colIdxs = new int[n - 3];
            for (int i = 0; i < n - 3; i++) {
                colIdxs[i] = (i < delIdx ? i + 1 : i + 2);
            }
            M1.getSubMatrix(colIdxs, colIdxs, Msubsub);
            inc.deleteRowAndColumn(delIdx);
            checkFactorization(inc, Msubsub);
        }
    }
}
Also used : TestException(maspack.util.TestException) TestException(maspack.util.TestException)

Example 5 with TestException

use of maspack.util.TestException in project artisynth_core by artisynth.

the class VectorTest method checkResult.

void checkResult(Vector vr, Vector vc, Exception eactual, Exception eexpected, double epsilon) {
    MatrixTest.checkExceptions(eactual, eexpected);
    double tol = 0;
    if (epsilon != 0) {
        tol = vr.infinityNorm() * epsilon;
    }
    if (!vr.epsilonEquals(vc, tol)) {
        VectorNd ME = new VectorNd(vr);
        VectorNd v1 = new VectorNd(vc);
        ME.sub(v1);
        ME.absolute();
        throw new TestException("Expected result:\n" + vc.toString(new NumberFormat("%9.4f")) + "\n" + "Actual result:\n" + vr.toString(new NumberFormat("%9.4f")) + "\n" + "max err: " + ME.maxElement() + ", tol=" + tol);
    }
}
Also used : TestException(maspack.util.TestException) NumberFormat(maspack.util.NumberFormat)

Aggregations

TestException (maspack.util.TestException)17 NumberFormat (maspack.util.NumberFormat)4 Point3d (maspack.matrix.Point3d)3 Random (java.util.Random)2 Vector3d (maspack.matrix.Vector3d)2 ReaderTokenizer (maspack.util.ReaderTokenizer)2 StringReader (java.io.StringReader)1 InsideQuery (maspack.geometry.BVFeatureQuery.InsideQuery)1 Vertex3d (maspack.geometry.Vertex3d)1 MatrixNd (maspack.matrix.MatrixNd)1 SymmetricMatrix3d (maspack.matrix.SymmetricMatrix3d)1 VectorNd (maspack.matrix.VectorNd)1