private static int parity(Vector3f n1, Vector3f n) { if (n1.dot(n) < 0) { return -1; } else { return 1; } }
/** * Constructor instantiates a new <code>Plane</code> object. * * @param normal The normal of the plane. * @param displacement A vector representing a point on the plane. */ public Plane(Vector3f normal, Vector3f displacement) { this(normal, displacement.dot(normal)); }
/** * <code>pseudoDistance</code> calculates the distance from this plane to * a provided point. If the point is on the negative side of the plane the * distance returned is negative, otherwise it is positive. If the point is * on the plane, it is zero. * * @param point * the point to check. * @return the signed distance from the plane to a point. */ public float pseudoDistance(Vector3f point) { return normal.dot(point) - constant; }
/** * The method computes the distance between a point and a plane (described by point in space and normal vector). * @param p * the point in the space * @param c * mirror's plane center * @param n * mirror's plane normal (should be normalized) * @return the minimum distance from point to plane */ private float computeDistanceFromPlane(Vector3f p, Vector3f c, Vector3f n) { return Math.abs(n.dot(p) - c.dot(n)); } }
/** * <code>angleBetween</code> returns (in radians) the angle between two vectors. * It is assumed that both this vector and the given vector are unit vectors (iow, normalized). * * @param otherVector a unit vector to find the angle against * @return the angle in radians. */ public float angleBetween(Vector3f otherVector) { float dotProduct = dot(otherVector); float angle = FastMath.acos(dotProduct); return angle; }
private static void toTangentsWithParity(IrVertex vertex) { if (vertex.tang != null && vertex.bitang != null) { float wCoord = vertex.norm.cross(vertex.tang).dot(vertex.bitang) < 0f ? -1f : 1f; vertex.tang4d = new Vector4f(vertex.tang.x, vertex.tang.y, vertex.tang.z, wCoord); vertex.tang = null; vertex.bitang = null; } }
/** * Projects this vector onto another vector * * @param other The vector to project this vector onto * @return A new vector with the projection result */ public Vector3f project(Vector3f other){ float n = this.dot(other); // A . B float d = other.lengthSquared(); // |B|^2 return new Vector3f(other).multLocal(n/d); }
/** * Projects this vector onto another vector, stores the result in this * vector * * @param other The vector to project this vector onto * @return This Vector3f, set to the projection result */ public Vector3f projectLocal(Vector3f other){ float n = this.dot(other); // A . B float d = other.lengthSquared(); // |B|^2 return set(other).multLocal(n/d); }
private boolean insidePlane(Plane p, Vector3f axis1, Vector3f axis2, Vector3f axis3, Vector3f tn) { // transform the plane normal in the box local space. tn.set(axis1.dot(p.getNormal()), axis2.dot(p.getNormal()), axis3.dot(p.getNormal())); // distance check float radius = FastMath.abs(tn.x) + FastMath.abs(tn.y) + FastMath.abs(tn.z); float distance = p.pseudoDistance(transform.getTranslation()); if (distance < -radius) { return false; } return true; }
/** * * @param p * @param loc * @return true if the ray collides with the given Plane */ public boolean intersectsWherePlane(Plane p, Vector3f loc) { float denominator = p.getNormal().dot(direction); if (denominator > -FastMath.FLT_EPSILON && denominator < FastMath.FLT_EPSILON) { return false; // coplanar } float numerator = -(p.getNormal().dot(origin) - p.getConstant()); float ratio = numerator / denominator; if (ratio < FastMath.FLT_EPSILON) { return false; // intersects behind origin } loc.set(direction).multLocal(ratio).addLocal(origin); return true; }
/** * Initialize the Plane using the given 3 points as coplanar. * * @param v1 * the first point * @param v2 * the second point * @param v3 * the third point */ public void setPlanePoints(Vector3f v1, Vector3f v2, Vector3f v3) { normal.set(v2).subtractLocal(v1); normal.crossLocal(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z) .normalizeLocal(); constant = normal.dot(v1); }
public static boolean intersect(BoundingSphere sphere, Vector3f center, float radius) { assert Vector3f.isValidVector(center) && Vector3f.isValidVector(sphere.center); TempVars vars = TempVars.get(); try { Vector3f diff = center.subtract(sphere.center, vars.vect1); float rsum = sphere.getRadius() + radius; return (diff.dot(diff) <= rsum * rsum); } finally { vars.release(); } }
private float collideWithVertex(Vector3f sCenter, Vector3f sVelocity, float velocitySquared, Vector3f vertex, float t) { // A = velocity * velocity // B = 2 * (velocity . (center - vertex)); // C = ||vertex - center||^2 - 1f; temp1.set(sCenter).subtractLocal(vertex); float a = velocitySquared; float b = 2f * sVelocity.dot(temp1); float c = temp1.negateLocal().lengthSquared() - 1f; float newT = getLowestRoot(a, b, c, t); // float A = velocitySquared; // float B = sCenter.subtract(vertex).dot(sVelocity) * 2f; // float C = vertex.subtract(sCenter).lengthSquared() - 1f; // // float newT = getLowestRoot(A, B, C, Float.MAX_VALUE); // if (newT > 1.0f) // newT = Float.NaN; return newT; }
public boolean intersects(Ray ray) { assert Vector3f.isValidVector(center); TempVars vars = TempVars.get(); Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center); float radiusSquared = getRadius() * getRadius(); float a = diff.dot(diff) - radiusSquared; if (a <= 0.0) { vars.release(); // in sphere return true; } // outside sphere float b = ray.getDirection().dot(diff); vars.release(); if (b >= 0.0) { return false; } return b * b >= a; }
public float distanceSquared(Vector3f point) { TempVars vars = TempVars.get(); Vector3f compVec1 = vars.vect1; Vector3f compVec2 = vars.vect2; point.subtract(origin, compVec1); float lineParameter = direction.dot(compVec1); origin.add(direction.mult(lineParameter, compVec2), compVec2); compVec2.subtract(point, compVec1); float len = compVec1.lengthSquared(); vars.release(); return len; }
public float distanceToCam(Geometry spat){ if (spat == null) return Float.NEGATIVE_INFINITY; if (spat.queueDistance != Float.NEGATIVE_INFINITY) return spat.queueDistance; Vector3f camPosition = cam.getLocation(); Vector3f viewVector = cam.getDirection(tempVec2); Vector3f spatPosition = null; if (spat.getWorldBound() != null){ spatPosition = spat.getWorldBound().getCenter(); }else{ spatPosition = spat.getWorldTranslation(); } spatPosition.subtract(camPosition, tempVec); spat.queueDistance = tempVec.dot(viewVector); return spat.queueDistance; }
private boolean pointsOnSameSide(Vector3f p1, Vector3f p2, Vector3f line1, Vector3f line2) { // V1 = (line2 - line1) x (p1 - line1) // V2 = (p2 - line1) x (line2 - line1) temp1.set(line2).subtractLocal(line1); temp3.set(temp1); temp2.set(p1).subtractLocal(line1); temp1.crossLocal(temp2); temp2.set(p2).subtractLocal(line1); temp3.crossLocal(temp2); // V1 . V2 >= 0 return temp1.dot(temp3) >= 0; }
public float distanceSquared(Vector3f point) { TempVars vars = TempVars.get(); Vector3f tempVa = vars.vect1, tempVb = vars.vect2; point.subtract(origin, tempVa); float rayParam = direction.dot(tempVa); if (rayParam > 0) { origin.add(direction.mult(rayParam, tempVb), tempVb); } else { tempVb.set(origin); rayParam = 0.0f; } tempVb.subtract(point, tempVa); float len = tempVa.lengthSquared(); vars.release(); return len; }
public float distanceSquared(Vector3f point) { TempVars vars = TempVars.get(); Vector3f compVec1 = vars.vect1; point.subtract(origin, compVec1); float segmentParameter = direction.dot(compVec1); if (-extent < segmentParameter) { if (segmentParameter < extent) { origin.add(direction.mult(segmentParameter, compVec1), compVec1); } else { origin.add(direction.mult(extent, compVec1), compVec1); } } else { origin.subtract(direction.mult(extent, compVec1), compVec1); } compVec1.subtractLocal(point); float len = compVec1.lengthSquared(); vars.release(); return len; }