use of maspack.matrix.Point3d in project artisynth_core by artisynth.
the class OBB method intersectsLineSegment.
public boolean intersectsLineSegment(Point3d p1, Point3d p2) {
Vector3d hw = myHalfWidths;
Point3d p1t = new Point3d(p1);
Point3d p2t = new Point3d(p2);
p1t.inverseTransform(myX);
p2t.inverseTransform(myX);
// if either point is inside, return true
if (Math.abs(p1t.x) <= hw.x && Math.abs(p1t.y) <= hw.y && Math.abs(p1t.z) <= hw.z) {
return true;
}
if (Math.abs(p2t.x) <= hw.x && Math.abs(p2t.y) <= hw.y && Math.abs(p2t.z) <= hw.z) {
return true;
}
// otherwise, intersect with edges
double[] dir = { p2t.x - p1t.x, p2t.y - p1t.y, p2t.z - p1t.z };
double[] min = { -hw.x, -hw.y, -hw.z };
double[] max = { hw.x, hw.y, hw.z };
double[] p1v = { p1t.x, p1t.y, p1t.z };
double[] p2v = { p2t.x, p2t.y, p2t.z };
double near = -INF;
double far = INF;
double t1, t2, tMin, tMax;
// check line/plane intersections
for (int i = 0; i < 3; i++) {
if (dir[i] == 0) {
if ((min[i] - p1v[i] > 0) || (max[i] - p1v[i] < 0)) {
return false;
}
} else {
t1 = (min[i] - p1v[i]) / dir[i];
t2 = (min[i] - p2v[i]) / dir[i];
tMin = Math.min(t1, t2);
tMax = Math.max(t1, t2);
if (tMin > near) {
near = tMin;
}
if (tMax < far) {
far = tMax;
}
if (near > far)
return false;
}
}
if ((near >= 0 && near <= 1) || (far >= 0 && far <= 1)) {
return true;
}
return false;
}
use of maspack.matrix.Point3d in project artisynth_core by artisynth.
the class OBB method computeCovarianceFromElements.
private void computeCovarianceFromElements(Matrix3d C, Point3d cent, Boundable[] elems, int num) {
Matrix3d Celem = new Matrix3d();
Point3d centElem = new Point3d();
double size = 0;
for (int i = 0; i < num; i++) {
Boundable elem = elems[i];
double s = elem.computeCovariance(Celem);
if (s < 0) {
throw new IllegalArgumentException("Cannot create OBB: boundable type " + elem.getClass() + " does not implement computeCovariance()");
}
elem.computeCentroid(centElem);
C.add(Celem);
size += s;
cent.scaledAdd(s, centElem);
}
cent.scale(1.0 / size);
C.addScaledOuterProduct(-size, cent, cent);
}
use of maspack.matrix.Point3d in project artisynth_core by artisynth.
the class OBB method containsPoint.
public boolean containsPoint(Point3d pnt) {
Point3d p = new Point3d(pnt);
p.inverseTransform(myX);
Vector3d hw = myHalfWidths;
boolean status = (-hw.x <= p.x && p.x <= hw.x && -hw.y <= p.y && p.y <= hw.y && -hw.z <= p.z && p.z <= hw.z);
return status;
}
use of maspack.matrix.Point3d in project artisynth_core by artisynth.
the class OBB method computeBoundsFromPoints.
private void computeBoundsFromPoints(Point3d min, Point3d max, Point3d[] pnts) {
Vector3d xpnt = new Point3d();
for (Point3d pnt : pnts) {
xpnt.inverseTransform(myX, pnt);
xpnt.updateBounds(min, max);
}
}
use of maspack.matrix.Point3d in project artisynth_core by artisynth.
the class PointMesh method write.
/**
* Writes this mesh to a PrintWriter, using an Alias Wavefront "obj" file
* format. Vertices are printed first, each starting with the letter "v" and
* followed by x, y, and z coordinates. Normals, if present, are printed
* next, starting with the letter "vn" and followed by x, y, and z
* coordinates.
*
* <p>
* The format used to print vertex coordinates is specified by a
* {@link maspack.util.NumberFormat NumberFormat}.
*
* @param pw
* PrintWriter to write this mesh to
* @param fmt
* (optional) format for writing the vertex and normals coordinates. If <code>null</code>,
* a format of <code>"%.8g"</code> is assumed.
* @param zeroIndexed
* if true, index numbering for mesh vertices starts at 0. Otherwise,
* numbering starts at 1.
*/
public void write(PrintWriter pw, NumberFormat fmt, boolean zeroIndexed) throws IOException {
if (fmt == null) {
fmt = new NumberFormat("%.8g");
}
for (Vertex3d vertex : myVertices) {
Point3d pnt = vertex.pnt;
pw.println("v " + fmt.format(pnt.x) + " " + fmt.format(pnt.y) + " " + fmt.format(pnt.z));
}
if (myNormals != null) {
for (Vector3d nrm : myNormals) {
pw.println("vn " + fmt.format(nrm.x) + " " + fmt.format(nrm.y) + " " + fmt.format(nrm.z));
}
}
pw.flush();
}
Aggregations