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 Ray rayThruPixel(Camera cam, int xi, int yi) { Ray result = new Ray(); float x = xi + .5f; float y = yi + .5f; final int halfW = cam.getHalfW(); float alpha = (float) (cam.getTanHalfFovX()) * ((x - halfW) / halfW); final int halfH = cam.getHalfH(); float beta = (float) (cam.getTanHalfFovY() * (halfH - y) / halfH); Vector3D p13 = cam.getU().scalarMultiply(alpha).add(cam.getV().scalarMultiply(beta)).subtract(cam.getW()); result.setP0(cam.getFrom()); result.setP1(VectorUtils.toRealVector(p13)); return result; }
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); }