private static Intersection getIntersection(Ray ray, SphereObject obj, Model model) { RealMatrix transform = obj.getTransform(); final RealMatrix transformInverse = obj.getTransformInverse(); ray = ray.transform(transformInverse); Vector3D c = VectorUtils.toVector3D(obj.getCenter()); Vector3D p0 = VectorUtils.toVector3D(ray.getP0()); Vector3D p1 = VectorUtils.toVector3D(ray.getP1()); float a = (float) p1.dotProduct(p1); Vector3D p0c = p0.subtract(c); float b = (float) p1.dotProduct(p0c) * 2.0f; float cc = (float) (p0c.dotProduct(p0c)) - obj.getSize() * obj.getSize(); Double t = quadraticEquationRoot1(a, b, cc); if (t == null || t < EPSILON) { return new Intersection(false); } Intersection result = new Intersection(true); result.setObject(obj); final Vector3D p = p0.add(p1.scalarMultiply(t)); RealVector pv = VectorUtils.toRealVector(p); pv.setEntry(3, 1.0); RealVector pt = transform.preMultiply(pv); result.setP(VectorUtils.toVector3D(pt)); RealVector nv = pv.subtract(obj.getCenter()); final RealVector nvt = transformInverse.transpose().preMultiply(nv); result.setN(VectorUtils.toVector3D(nvt).normalize()); result.setDistance(t); return result; }
private static int toColour(RealVector ambient) { final double r = ambient.getEntry(0); final double g = ambient.getEntry(1); final double b = ambient.getEntry(2); int rc = 256 * 256 * (int) (255. * r); int rg = 256 * (int) (255. * g); int rb = (int) (255. * b); return rc + rg + rb; }
private static Intersection getIntersection( TriangleObject obj, DecompositionSolver solver, Vector3D p, Vector3D pc, double[] params) { RealVector constants = new ArrayRealVector(params, false); RealVector solution = solver.solve(constants); double alpfa = solution.getEntry(0); double beta = solution.getEntry(1); boolean match = false; if (alpfa >= 0 && beta >= 0 && alpfa + beta <= 1.0) { match = true; } else { match = false; } final Intersection intersection = new Intersection(match); intersection.setObject(obj); intersection.setP(p); return intersection; }