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; }
public static Intersection intersect(Ray ray, Model model) { Intersection result = new Intersection(false); int matchCount = 0; for (DrawedObject obj : model.getObjects()) { if (obj instanceof TriangleObject) { Intersection intersection = getIntersection(ray, (TriangleObject) obj, model); final boolean match = intersection.isMatch(); if (match) { ++matchCount; } if (match && intersection.getDistance() < result.getDistance()) { result = intersection; } } else if (obj instanceof SphereObject) { Intersection intersection = getIntersection(ray, (SphereObject) obj, model); final boolean match = intersection.isMatch(); if (match) { ++matchCount; } if (match && intersection.getDistance() < result.getDistance()) { result = intersection; } } } result.setIntersectionCount(matchCount); return result; }
private static int findColor(Intersection hit, Camera cam, Model model) { if (hit.isMatch()) { RealVector light = LightCalculations.computeLight(cam.getFrom3(), model, hit, 0); return toColour(light); } return 0; }
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; }
private static Intersection getIntersection(Ray ray, TriangleObject obj, Model model) { Vector3D p0 = VectorUtils.toVector3D(ray.getP0()); Vector3D p1 = VectorUtils.toVector3D(ray.getP1()); Vector3D a = obj.getA(); Vector3D b = obj.getB(); Vector3D c = obj.getC(); Vector3D n = obj.getN(); double den = p1.dotProduct(n); if (den != 0.0) { double t = (a.dotProduct(n) - p0.dotProduct(n)) / den; if (t < EPSILON) { return new Intersection(false); } Vector3D p = p0.add(p1.scalarMultiply(t)); Vector3D pc = p.subtract(c); final Intersection intersection = getIntersection(obj, p, pc); if (intersection != null) { intersection.setDistance(t); return intersection; } } return new Intersection(false); }