Search in sources :

Example 6 with InternalErrorException

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

the class QuadraticUtils method ellipsoidSurfaceTangentInPlane.

/**
 * Find the point <code>pt</code> on an axis-aligned ellipsoid that
 * is tangent to the line <code>pa</code>-<code>pt</code>, lies
 * in the plane defined by <code>p0</code> and <code>nrm</code>,
 * and is nearest to <code>p0</code>.
 *
 * <p>The method works by projecting the ellipsoid into either YZ, ZX, or XY
 * planes, depending on which is closest to the plane in question, and then
 * finding the tangent points for the associated ellipse.
 */
public static boolean ellipsoidSurfaceTangentInPlane(Point3d pt, Point3d pa, Point3d p0, Vector3d nrm, double a, double b, double c) {
    // quadratic coefficents for ellispod
    double a0 = 1 / (a * a);
    double a1 = 1 / (b * b);
    double a2 = 1 / (c * c);
    double a9 = -1;
    // Start by seeing if pa is inside the ellipsoid. If it is, just set pa
    // to the surface projection of pa and return.
    double adist = a0 * pa.x * pa.x + a1 * pa.y * pa.y + a2 * pa.z * pa.z;
    if (adist <= 1) {
        Vector3d cnrm = new Vector3d();
        double d = ellipsoidPenetrationDistance(cnrm, pa, a, b, c, 1.0);
        pt.scaledAdd(-d, cnrm, pa);
        return true;
    }
    // compute offset for (p0, nrm) plane
    double off = nrm.dot(p0);
    double[] bc = new double[6];
    Vector2d[] pnts = new Vector2d[] { new Vector2d(), new Vector2d() };
    Point3d[] tans = new Point3d[] { new Point3d(), new Point3d() };
    int nr = 0;
    switch(nrm.maxAbsIndex()) {
        case 0:
            {
                // YZ plane
                double rx = nrm.y / nrm.x;
                double ry = nrm.z / nrm.x;
                double rz = off / nrm.x;
                bc[0] = a1 + a0 * rx * rx;
                bc[1] = a2 + a0 * ry * ry;
                bc[2] = 2 * a0 * rx * ry;
                bc[3] = -2 * a0 * rz * rx;
                bc[4] = -2 * a0 * rz * ry;
                bc[5] = a9 + a0 * rz * rz;
                nr = DistanceGridSurfCalc.findTangentPoints(pnts, bc, pa.y, pa.z);
                for (int i = 0; i < nr; i++) {
                    double y = pnts[i].x;
                    double z = pnts[i].y;
                    double x = rz - rx * y - ry * z;
                    tans[i].set(x, y, z);
                }
                break;
            }
        case 1:
            {
                // ZX plane
                double rx = nrm.z / nrm.y;
                double ry = nrm.x / nrm.y;
                double rz = off / nrm.y;
                bc[0] = a2 + a1 * rx * rx;
                bc[1] = a0 + a1 * ry * ry;
                bc[2] = 2 * a1 * rx * ry;
                bc[3] = -2 * a1 * rz * rx;
                bc[4] = -2 * a1 * rz * ry;
                bc[5] = a9 + a1 * rz * rz;
                nr = DistanceGridSurfCalc.findTangentPoints(pnts, bc, pa.z, pa.x);
                for (int i = 0; i < nr; i++) {
                    double z = pnts[i].x;
                    double x = pnts[i].y;
                    double y = rz - rx * z - ry * x;
                    tans[i].set(x, y, z);
                }
                break;
            }
        case 2:
            {
                // XY plane
                double rx = nrm.x / nrm.z;
                double ry = nrm.y / nrm.z;
                double rz = off / nrm.z;
                bc[0] = a0 + a2 * rx * rx;
                bc[1] = a1 + a2 * ry * ry;
                bc[2] = 2 * a2 * rx * ry;
                bc[3] = -2 * a2 * rz * rx;
                bc[4] = -2 * a2 * rz * ry;
                bc[5] = a9 + a2 * rz * rz;
                nr = DistanceGridSurfCalc.findTangentPoints(pnts, bc, pa.x, pa.y);
                for (int i = 0; i < nr; i++) {
                    double x = pnts[i].x;
                    double y = pnts[i].y;
                    double z = rz - rx * x - ry * y;
                    tans[i].set(x, y, z);
                }
                break;
            }
        default:
            {
                throw new InternalErrorException("Vector3d.maxAbsIndex() returned value " + nrm.maxAbsIndex());
            }
    }
    if (nr == 2) {
        // if there are two tangent points, take the nearest to p0
        if (tans[0].distance(p0) < tans[1].distance(p0)) {
            pt.set(tans[0]);
        } else {
            pt.set(tans[1]);
        }
        return true;
    } else if (nr == 1) {
        pt.set(tans[0]);
        return true;
    } else {
        // if no tangents computed, use p0 as a fall back and return false
        pt.set(p0);
        return false;
    }
}
Also used : InternalErrorException(maspack.util.InternalErrorException)

Example 7 with InternalErrorException

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

the class DicomElement method parseDate.

public static DicomDateTime parseDate(String in) {
    // YYYYMMDD or YYYY.MM.DD
    String dtStr = in;
    // remove periods
    dtStr = dtStr.replace(".", "");
    int year = 1970;
    int month = 1;
    int date = 1;
    String substr;
    switch(dtStr.length()) {
        case 8:
            {
                substr = dtStr.substring(6);
                date = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 6);
            }
        case 6:
            {
                substr = dtStr.substring(4);
                month = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 4);
            }
        case 4:
            {
                year = Integer.parseInt(dtStr);
                break;
            }
        default:
            {
                throw new InternalErrorException("Date/Time string in invalid format (" + dtStr + ")");
            }
    }
    DicomDateTime dt = new DicomDateTime(year, month, date, 0, 0, 0, 0);
    return dt;
}
Also used : InternalErrorException(maspack.util.InternalErrorException)

Example 8 with InternalErrorException

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

the class DicomElement method parseDateTime.

public static DicomDateTime parseDateTime(String in) {
    // YYYYMMDDHHMMSS.FFFFFF&ZZZZ
    String dtStr = in;
    // optional offset
    int offsetMinutes = 0;
    int idSign = dtStr.indexOf('+');
    if (idSign < 0) {
        idSign = dtStr.indexOf('-');
    }
    if (idSign >= 0) {
        String strOffset = dtStr.substring(idSign);
        dtStr = dtStr.substring(0, idSign);
        boolean minus = (dtStr.charAt(0) == '-');
        strOffset = strOffset.substring(1);
        if (strOffset.length() == 2) {
            offsetMinutes = 60 * Integer.parseInt(strOffset);
        } else if (strOffset.length() == 4) {
            offsetMinutes = 60 * Integer.parseInt(strOffset.substring(0, 2)) + Integer.parseInt(strOffset.substring(2, 4));
        } else {
            throw new InternalErrorException("Date offset does not adhere to proper format. (" + strOffset + ")");
        }
        if (minus) {
            offsetMinutes = -offsetMinutes;
        }
    }
    // parse actual Date/Time
    int micros = 0;
    int idPeriod = dtStr.indexOf('.');
    if (idPeriod >= 0) {
        String strDecimal = dtStr.substring(idPeriod);
        dtStr = dtStr.substring(0, idPeriod);
        micros = Math.round(Float.parseFloat(strDecimal) * 1000000);
    }
    // YYYYMMDDHHMMSS
    int year = 1970;
    int month = 1;
    int date = 1;
    int hour = 0;
    int min = 0;
    int sec = 0;
    String substr;
    switch(dtStr.length()) {
        case 14:
            {
                substr = dtStr.substring(12);
                sec = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 12);
            }
        case 12:
            {
                substr = dtStr.substring(10);
                min = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 10);
            }
        case 10:
            {
                substr = dtStr.substring(8);
                hour = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 8);
            }
        case 8:
            {
                substr = dtStr.substring(6);
                date = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 6);
            }
        case 6:
            {
                substr = dtStr.substring(4);
                month = Integer.parseInt(substr);
                dtStr = dtStr.substring(0, 4);
            }
        case 4:
            {
                year = Integer.parseInt(dtStr);
                break;
            }
        default:
            {
                throw new InternalErrorException("Date/Time string in invalid format (" + dtStr + ")");
            }
    }
    DicomDateTime dt = new DicomDateTime(year, month, date, hour, min, sec, micros);
    if (offsetMinutes != 0) {
        dt.addTimeMinutes(offsetMinutes);
    }
    return dt;
}
Also used : InternalErrorException(maspack.util.InternalErrorException)

Example 9 with InternalErrorException

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

the class SparseBlockMatrix method numNonZeroVals.

public int numNonZeroVals(Partition part, int numRows, int numCols) {
    BlockSize bsize = getBlockSize(numRows, numCols);
    int num = 0;
    switch(part) {
        case None:
            {
                return 0;
            }
        case Full:
            {
                for (int bi = 0; bi < bsize.numBlkRows; bi++) {
                    for (MatrixBlock blk = myRows[bi].myHead; blk != null && blk.getBlockCol() < bsize.numBlkCols; blk = blk.next()) {
                        num += blk.numNonZeroVals();
                    }
                }
                break;
            }
        case UpperTriangular:
            {
                for (int bi = 0; bi < bsize.numBlkRows; bi++) {
                    for (MatrixBlock blk = myRows[bi].myHead; blk != null && blk.getBlockCol() < bsize.numBlkCols; blk = blk.next()) {
                        if (blk.getBlockCol() > bi) {
                            num += blk.numNonZeroVals();
                        } else if (blk.getBlockCol() == bi) {
                            num += blk.numNonZeroVals(Partition.UpperTriangular, blk.rowSize(), blk.colSize());
                        }
                    }
                }
                break;
            }
        case LowerTriangular:
            {
                for (int bi = 0; bi < bsize.numBlkRows; bi++) {
                    MatrixBlock blk;
                    for (blk = myRows[bi].myHead; blk != null; blk = blk.next()) {
                        int bj = blk.getBlockCol();
                        if (bj > bi || bj >= bsize.numBlkCols) {
                            break;
                        }
                        if (bj < bi) {
                            num += blk.numNonZeroVals();
                        } else {
                            // bj == bi
                            num += blk.numNonZeroVals(Partition.LowerTriangular, blk.rowSize(), blk.colSize());
                        }
                    }
                }
                break;
            }
        default:
            {
                throw new InternalErrorException("Unimplemented partition: " + part);
            }
    }
    return num;
}
Also used : InternalErrorException(maspack.util.InternalErrorException)

Example 10 with InternalErrorException

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

the class MatrixNd method clone.

public MatrixNd clone() throws CloneNotSupportedException {
    MatrixNd M = null;
    try {
        M = (MatrixNd) super.clone();
    } catch (CloneNotSupportedException e) {
        // shouldn't happen
        throw new InternalErrorException("clone failed for " + getClass());
    }
    M.tmp = new double[0];
    M.subMatrixRefCnt = 0;
    M.fixedSize = false;
    M.explicitBuffer = false;
    M.buf = new double[0];
    // nrows, ncols, and width will be reset by the set routine
    M.nrows = 0;
    M.ncols = 0;
    M.width = 0;
    M.base = 0;
    M.set(this);
    M.res = null;
    return M;
}
Also used : InternalErrorException(maspack.util.InternalErrorException)

Aggregations

InternalErrorException (maspack.util.InternalErrorException)92 Vector3d (maspack.matrix.Vector3d)9 CompositeProperty (maspack.properties.CompositeProperty)8 Point3d (maspack.matrix.Point3d)7 Property (maspack.properties.Property)7 FemModel3d (artisynth.core.femmodels.FemModel3d)6 Point (artisynth.core.mechmodels.Point)6 ModelComponent (artisynth.core.modelbase.ModelComponent)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 Line (maspack.matrix.Line)5 RigidTransform3d (maspack.matrix.RigidTransform3d)5 EditingProperty (maspack.properties.EditingProperty)5 BadLocationException (javax.swing.text.BadLocationException)4 RotationMatrix3d (maspack.matrix.RotationMatrix3d)4 RigidBody (artisynth.core.mechmodels.RigidBody)3 RootModel (artisynth.core.workspace.RootModel)3 File (java.io.File)3 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 SelectionManager (artisynth.core.gui.selectionManager.SelectionManager)2