/** * Returns the angle in radians between this vector and the vector * parameter; the return value is constrained to the range [0,PI]. * @param v1 the other vector * @return the angle in radians in the range [0,PI] */ public final double angle(Vector3d v1) { double vDot = this.dot(v1) / ( this.length()*v1.length() ); if( vDot < -1.0) vDot = -1.0; if( vDot > 1.0) vDot = 1.0; return((double) (Math.acos( vDot ))); }
private boolean projectToPlane(Vector3d projVec, Vector3d planeVec) { double dis = planeVec.dot(projVec); projVec.x = projVec.x - planeVec.x*dis; projVec.y = projVec.y - planeVec.y*dis; projVec.z = projVec.z - planeVec.z*dis; double length = projVec.length(); if (length < EPSILON) { // projVec is parallel to planeVec return false; } projVec.scale(1 / length); return true; }
private boolean projectToPlane(Vector3d projVec, Vector3d planeVec) { double dis = planeVec.dot(projVec); projVec.x = projVec.x - planeVec.x*dis; projVec.y = projVec.y - planeVec.y*dis; projVec.z = projVec.z - planeVec.z*dis; double length = projVec.length(); if (length < EPSILON) { return false; } projVec.scale(1 / length); return true; }
static boolean edgeIntersectPlane(Vector3d normal, Point3d pnt, Point3d start, Point3d end, Point3d iPnt){ Vector3d tempV3d = new Vector3d(); Vector3d direction = new Vector3d(); double pD, pNrmDotrDir, tr; // Compute plane D. tempV3d.set((Tuple3d) pnt); pD = normal.dot(tempV3d); direction.x = end.x - start.x; direction.y = end.y - start.y; direction.z = end.z - start.z; pNrmDotrDir = normal.dot(direction); // edge is parallel to plane. if (pNrmDotrDir== 0.0) { // System.out.println("Edge is parallel to plane."); return false; } tempV3d.set((Tuple3d) start); tr = (pD - normal.dot(tempV3d))/ pNrmDotrDir; // Edge intersects the plane behind the edge's start. // or exceed the edge's length. if ((tr < 0.0 ) || (tr > 1.0 )) { // System.out.println("Edge intersects the plane behind the start or exceed end."); return false; } iPnt.x = start.x + tr * direction.x; iPnt.y = start.y + tr * direction.y; iPnt.z = start.z + tr * direction.z; return true; }
private void updateRotationAxisAndCenter() { v1.set(0, 1, 0); v2.cross(v1, rotationAxis); final double angle = Math.acos(v1.dot(rotationAxis)); aa.set(v2, angle); centerXform.set(aa); } }
static public final double distancePointToSegmentSq(final Vector3d p, final Vector3d v1, final Vector3d v2) { final Vector3d v = new Vector3d(); v.sub(v2, v1); final Vector3d w = new Vector3d(); w.sub(p, v1); final double c1 = w.dot(v); if (c1 <= 0) return distanceSq(p, v1); final double c2 = v.dot(v); if (c2 <= c1) return distanceSq(p, v2); final double b = c1 / c2; final Vector3d pb = new Vector3d(v); pb.scale(b); pb.add(v1); return distanceSq(p, pb); }
/** Minimum distance between point v0 and a line segment defined by points v1 and v2. */ static public final double distancePointToSegment(final Vector3d p, final Vector3d v1, final Vector3d v2) { final Vector3d v = new Vector3d(); v.sub(v2, v1); final Vector3d w = new Vector3d(); w.sub(p, v1); final double c1 = w.dot(v); if (c1 <= 0) return distance(p, v1); final double c2 = v.dot(v); if (c2 <= c1) return distance(p, v2); final double b = c1 / c2; final Vector3d pb = new Vector3d(v); pb.scale(b); pb.add(v1); return distance(p, pb); }
boolean edgeIntersectSphere(BoundingSphere sphere, Point3d start, Point3d end) { double abLenSq, acLenSq, apLenSq, abDotAp, radiusSq; Vector3d ab = new Vector3d(); Vector3d ap = new Vector3d(); ab.x = end.x - start.x; ab.y = end.y - start.y; ab.z = end.z - start.z; ap.x = sphere.center.x - start.x; ap.y = sphere.center.y - start.y; ap.z = sphere.center.z - start.z; abDotAp = ab.dot(ap); if(abDotAp < 0.0) { return false; // line segment points away from sphere. } abLenSq = ab.lengthSquared(); acLenSq = abDotAp * abDotAp / abLenSq; if(acLenSq < abLenSq) { return false; // C doesn't lies between end points of edge. } radiusSq = sphere.radius * sphere.radius; apLenSq = ap.lengthSquared(); if((apLenSq - acLenSq) <= radiusSq) { return true; } return false; }
static boolean edgeIntersectSphere (BoundingSphere sphere, Point3d start, Point3d end) { double abLenSq, acLenSq, apLenSq, abDotAp, radiusSq; Vector3d ab = new Vector3d(); Vector3d ap = new Vector3d(); Point3d center = new Point3d(); sphere.getCenter (center); double radius = sphere.getRadius (); ab.x = end.x - start.x; ab.y = end.y - start.y; ab.z = end.z - start.z; ap.x = center.x - start.x; ap.y = center.y - start.y; ap.z = center.z - start.z; abDotAp = ab.dot(ap); if (abDotAp < 0.0) return false; // line segment points away from sphere. abLenSq = ab.lengthSquared(); acLenSq = abDotAp * abDotAp / abLenSq; if (acLenSq < abLenSq) return false; // C doesn't lies between end points of edge. radiusSq = radius * radius; apLenSq = ap.lengthSquared(); if ((apLenSq - acLenSq) <= radiusSq) return true; return false; }
/** * Returns the square of the minimum distance from the given point to the ray * defined by start, dir. */ static final double ptToRaySquare(Point3d pt, Point3d start, Vector3d dir, Point3d closest) { Vector3d dt = new Vector3d(); dt.sub(pt, start); // Project the point onto the ray double proj = dir.dot(dt); // We projected 'before' the start point, just return the dSquared between // the point and the start if (proj <= 0.0d) { if (closest != null) closest.set(start); return dt.lengthSquared(); } // Project the ray onto itself double raySquared = dir.lengthSquared(); // We projected somewhere along the ray, calculate the closest point dt.scaleAdd(proj / raySquared, dir, start); if (closest != null) closest.set(dt); // return the distance from the point to the closest point on the ray dt.sub(pt, dt); return dt.lengthSquared(); }
getCrossValue(pnt1, pnt2, tmpvec); getCrossValue(pnt2, pnt0, tmpvec); area = normal.dot(tmpvec); totalarea += area; centroid.x += (pnt0.x+pnt1.x+pnt2.x) * area; getCrossValue(pnt2, pnt3, tmpvec); getCrossValue(pnt3, pnt0, tmpvec); area = normal.dot(tmpvec); totalarea += area; centroid.x += (pnt3.x+pnt0.x+pnt2.x) * area;
getCrossValue(pnt1, pnt2, tmpvec); getCrossValue(pnt2, pnt0, tmpvec); area = normal.dot(tmpvec); centroid.x += (pnt0.x + pnt1.x + pnt2.x)* area; centroid.y += (pnt0.y + pnt1.y + pnt2.y)* area;
double proj = dir.dot(dt);
getCrossValue(pnt1, pnt2, tmpvec); getCrossValue(pnt2, pnt0, tmpvec); area = normal.dot(tmpvec); totalarea += area; centroid.x += (pnt0.x + pnt1.x + pnt2.x) * area;