Usages of

.getDirection()
@Override public double isVisible(Point3d globalPoint, List<Body> bodies) { Point3d localPoint = toLocal(globalPoint); Vector3d direction = Vectors.normalize(Vectors.sub(position, localPoint)); Ray ray = new Ray(direction, localPoint); double maxT = position.distance(localPoint); Ray globalRay = new Ray(toGlobal(ray.getDirection()), toGlobal(ray.getOrigin())); IntersectionContext ic = globalRay.trace(bodies); if (ic.getHit() && maxT - ic.getT() > Shape.EPS) return 0; return 1; }
@Override public Hit getHit(Ray ray) { float d = normal.dot(position); float ndotd = normal.dot(ray.getDirection()); if (ndotd == 0) { return null; } float t = (d - normal.dot(ray.getOrigin())) / ndotd; if (t < Constants.EPSILON) { return null; } return new Hit(t, material, normal, this); }
public double calculateReflectionCoefficient(Ray refracted, Ray reflected, IntersectionContext ic) { double n1,n2; double costt = Math.abs(refracted.getDirection().dot(ic.getNormal())); double costi = Math.abs(ic.getRay().getDirection().dot(ic.getNormal())); if (ic.getRay().getDirection().dot(ic.getNormal()) < 0) { n1 = 1; n2 = refractionIndex; } else { n1 = refractionIndex; n2 = 1; } double r0 = (n1*costi-n2*costt)/(n1*costi+n2*costt); return r0*r0; }
public Color3f indirectShade(IntersectionContext ic, List<Light> lights, List<Body> bodies, int refractionDepth, int reflectionDepth) { Ray reflected = reflect(ic); Color3f bodyColor = getColor(ic.getU(), ic.getV()); Vector3d direction = reflected.getDirection(); Vector3d l = reflected.getDirection(); Vector3d n = ic.getNormal(); Vector3d v = ic.getRay().getDirection(); Vector3d fixedNormal = new Vector3d(n); if (direction.dot(fixedNormal) < 0) //la normal siempre se toma en la direccion del rayo fixedNormal.negate(); Vector3d sample = sampler.sampleHemisphere(getRoughness()); Vector3d x = Vectors.cross(new Vector3d(0.00123, 1, 0.00321), direction); x.normalize(); Vector3d sampledDirection = new Vector3d(); sampledDirection.add(Vectors.scale(direction, sample.z)); sampledDirection.add(Vectors.scale(x, sample.x)); sampledDirection.add(Vectors.scale(Vectors.cross(x, direction), sample.y)); if (sampledDirection.dot(fixedNormal) < 0) { //Reflejar cuando esta debajo del plano normal sampledDirection.add(Vectors.scale(x, -2*sample.x)); sampledDirection.add(Vectors.scale(Vectors.cross(x, direction), -2*sample.y)); } sampledDirection.normalize(); Color3f sampleColor; sampleColor = new Ray(sampledDirection, reflected.getOrigin()).trace(bodies).directShade(lights, bodies, refractionDepth+1, reflectionDepth+1); float pdf = (float) Math.pow(sampledDirection.dot(direction), getRoughness()); if (Double.isInfinite(pdf)) return sampleColor; sampleColor.scale(pdf); Color3f diffuseColor = orenNayar.apply(sampledDirection, n, v); Color3f lightColor = new Color3f(sampleColor); //Diffuse if (diffuseColor.x < 0) { // System.out.println("Diffuse color out of range. Value: " + diffuseColor.x+ "\n" + ic); diffuseColor.absolute(); } diffuseColor.x *= lightColor.x * bodyColor.x; diffuseColor.y *= lightColor.y * bodyColor.y; diffuseColor.z *= lightColor.z * bodyColor.z; diffuseColor.scale((float) (1-reflectionCoefficient-refractionCoefficient)); // Specular Color3f brdfC = orenNayar.apply(sampledDirection, n, v); brdfC.x *= lightColor.x * bodyColor.x; brdfC.y *= lightColor.y * bodyColor.y; brdfC.z *= lightColor.z * bodyColor.z; brdfC.scale((float)reflectionCoefficient); Color3f color = new Color3f(); color.add(brdfC); color.add(diffuseColor); color.absolute(); return color; }
@Override public IntersectionContext trace(Ray ray) { Vector3d normal = new Vector3d(this.normal); Vector3d P = Vectors.cross(ray.getDirection(), d2); double det = P.dot(d1); if (det > -EPS && det < EPS) return IntersectionContext.noHit(); double invDet = 1/det; Vector3d T = Vectors.sub(ray.getOrigin(), p1); double u = T.dot(P)*invDet; if (u < 0 || u > 1) return IntersectionContext.noHit(); Vector3d Q = Vectors.cross(T,d1); double v = ray.getDirection().dot(Q)*invDet; if (v < 0 || v > 1 || u + v > 1) return IntersectionContext.noHit(); double t = d2.dot(Q)*invDet; if (t > EPS) { if (normal.dot(ray.getDirection()) > EPS) normal.negate(); Point2d uv = getGlobalUV(new Point2d(u,v)); return new IntersectionContext(t, getNormal(new Point2d(u,v)), ray, true, uv.x, uv.y); } return IntersectionContext.noHit(); }
@Override public IntersectionContext trace(Ray ray) { Vector3d rayDirection = toLocal(ray.getDirection()); Point3d rayOrigin = toLocal(ray.getOrigin()); double a = rayDirection.x*rayDirection.x + rayDirection.z*rayDirection.z; double b = 2*(rayOrigin.x*rayDirection.x + rayOrigin.z*rayDirection.z); double c = rayOrigin.x*rayOrigin.x + rayOrigin.z*rayOrigin.z - 1; double disc = b * b - 4 * a * c; if (disc < 0) return IntersectionContext.noHit(); double e = Math.sqrt(disc); double t; t = (-b - e)/(2*a); if (t > EPS) { Point3d localHitPoint = Vectors.add(rayOrigin, Vectors.scale(rayDirection, t)); Vector3d normal = new Vector3d(localHitPoint.x, 0, localHitPoint.z); if (localHitPoint.y*heightFactor < 1 && localHitPoint.y > 0) { Point2d uv = getUVCoordinates(localHitPoint); return new IntersectionContext(t, normalToGlobal(normal), ray, true, uv.x, uv.y); } } t = (-b + e)/(2*a); if (t > EPS) { Point3d localHitPoint = Vectors.add(rayOrigin, Vectors.scale(rayDirection, t)); Vector3d normal = new Vector3d(-localHitPoint.x, 0, -localHitPoint.z); if (localHitPoint.y*heightFactor < 1 && localHitPoint.y > 0) { Point2d uv = getUVCoordinates(localHitPoint); return new IntersectionContext(t, normalToGlobal(normal), ray, true, uv.x, uv.y); } } return IntersectionContext.noHit(); }
public IntersectionContext trace(Ray ray) { Vector3d rayDirection = toLocal(ray.getDirection()); Point3d rayOrigin = toLocal(ray.getOrigin()); double t; Vector3d temp = new Vector3d(rayOrigin); double a = rayDirection.dot(rayDirection); double b = rayDirection.dot(Vectors.scale(temp,2)); double c = temp.dot(temp) - 1; double disc = (b * b) - (4 * a * c); if (disc < 0) return IntersectionContext.noHit(); else { double e = Math.sqrt(disc); double denom = a*2; t = (-b-e)/denom; if (t > EPS) { Vector3d rayDisplacement = Vectors.scale(rayDirection, t); Point3d localHitPoint = new Point3d(Vectors.add(rayOrigin,rayDisplacement)); if (checkBounds(localHitPoint)) { Point2d uv = getUVCoordinates(localHitPoint); Vector3d normal = normalToGlobal(getNormalAt(localHitPoint, true)); normal.normalize(); return new IntersectionContext(t, normal, ray, true, uv.x, uv.y); } } t = (-b+e)/denom; if (t > EPS) { Vector3d rayDisplacement = Vectors.scale(rayDirection, t); Point3d localHitPoint = new Point3d(Vectors.add(rayOrigin,rayDisplacement)); if (checkBounds(localHitPoint)) { Point2d uv = getUVCoordinates(localHitPoint); Vector3d normal = normalToGlobal(getNormalAt(localHitPoint, false)); normal.normalize(); return new IntersectionContext(t, normal, ray, true, uv.x, uv.y); } } } return IntersectionContext.noHit(); }
@Override public IntersectionContext trace(Ray ray) { double ox = ray.getOrigin().x; double oy = ray.getOrigin().y; double oz = ray.getOrigin().z; double dx = ray.getDirection().x; double dy = ray.getDirection().y; double dz = ray.getDirection().z; double tx_min, ty_min, tz_min; double tx_max, ty_max, tz_max; double a = 1.0f / dx; if(a >= 0){ tx_min = (x0 - ox) * a; tx_max = (x1 - ox) * a; }else{ tx_min = (x1 - ox) * a; tx_max = (x0 - ox) * a; } double b = 1.0f / dy; if(b >= 0){ ty_min = (y0 - oy) * b; ty_max = (y1 - oy) * b; }else{ ty_min = (y1 - oy) * b; ty_max = (y0 - oy) * b; } double c = 1.0f / dz; if(c >= 0){ tz_min = (z0 - oz) * c; tz_max = (z1 - oz) * c; }else{ tz_min = (z1 - oz) * c; tz_max = (z0 - oz) * c; } double t0, t1; int face_in, face_out; if(tx_min > ty_min){ t0 = tx_min; face_in = (a >= 0.0d ) ? 0 : 3 ; }else{ t0 = ty_min; face_in = (b >= 0.0d ) ? 1 : 4 ; } if( tz_min > t0){ t0 = tz_min; face_in = (c >= 0.0d ) ? 2 : 5 ; } if(tx_max < ty_max){ t1 = tx_max; face_out = (a >= 0.0d ) ? 3 : 0 ; }else{ t1 = ty_max; face_out = (b >= 0.0d ) ? 4 : 1 ; } if( tz_max < t1){ t1 = tz_max; face_out = (c >= 0.0d ) ? 5 : 2 ; } double tmin = 0.0d; Vector3d normal = null; if(t0 < t1 && t1 > EPS){ if(t0 > EPS){ tmin = t0; normal = getNormal(face_in); }else{ tmin = t1; normal = getNormal(face_out); } Vector3d dir = new Vector3d(ray.getDirection()); dir.scale(tmin); Point3d ori = new Point3d(ray.getOrigin()); ori.add(dir); Point3d localHitPoint = ori; Point2d uv = getUVCoordinates(localHitPoint); return new IntersectionContext(tmin,normal,ray,true,uv.x,uv.y); } return IntersectionContext.noHit(); }
public IntersectionContext trace (Ray ray) { Point3d rayOrigin = toLocal(ray.getOrigin()); Vector3d rayDirection = toLocal(ray.getDirection()); Vector3d normal = new Vector3d(this.normal); double differenceY = -rayOrigin.y; double normalY = 1; double t = differenceY/rayDirection.y; if (normalY*rayDirection.y > 0) { normal.negate(); } if (t > EPS) { Vector3d displacement = new Vector3d(rayDirection); displacement.scale(t); Point3d hitPoint = new Point3d(rayOrigin); hitPoint.add(displacement); Point2d uv = getUVCoordinates(hitPoint); return new IntersectionContext(t, normal, ray, true, uv.x, uv.y); } else return IntersectionContext.noHit(); }
@Override public OptionalDouble intersectionDistance(Ray ray) { double A, B, C, dirc, dircSqrt, t0 = Double.MAX_VALUE, t1; B = 2*ray.getDirection().dot(ray.getOrigin().sub(position)); A = ray.getDirection().dot(ray.getDirection()); C = ray.getOrigin().sub(position).dot(ray.getOrigin().sub(position)) - Math.pow(radius, 2); dirc = Math.pow(B, 2) - 4 * A * C; if(dirc >= 0) { dircSqrt = Math.sqrt(dirc); t0 = (-B + dircSqrt) / 2 * A; t1 = (-B - dircSqrt) / 2 * A; t0 = t0 < t1 ? t0 : t1; } if(t0 == Double.MAX_VALUE) return OptionalDouble.empty(); else return OptionalDouble.of(t0); }
Usage snippet has been bookmarked! Review your bookmarks
Thank you! Review your likes