Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RayTracing;
- public class Cylinder {
- private Vector center;
- private Float length;
- private Float rad;
- private Vector rotation;
- private Integer matIndex;
- private Vector noraml;
- public Cylinder() {
- this.normal = null;
- }
- public Vector getCenter() {
- return center;
- }
- public void setCenter(Vector center) {
- this.center = center;
- }
- public Float getLength() {
- return length;
- }
- public void setLength(Float length) {
- this.length = length;
- }
- public Float getRad() {
- return rad;
- }
- public void setRad(Float rad) {
- this.rad = rad;
- }
- public Vector getRotation() {
- return rotation;
- }
- public void setRotation(Vector rotation) {
- this.rotation = rotation;
- }
- public Integer getMatIndex() {
- return matIndex;
- }
- public void setMatIndex(Integer matIndex) {
- this.matIndex = matIndex;
- }
- public double getHitDistance(Ray ray) {
- //STEP 1
- //rotation
- Vector norm = new Vector(0,0,1);
- norm = norm.rotate(this.rotation);
- //STEP 2
- Vector s = norm.scale((this.length)/2);
- Vector cUp = this.center.add(s);
- Vector cDown = this.center.minus(s);
- //STEP 3
- Vector axis = (cUp.minus(cDown));
- Vector v1 = axis.multiply(ray.dir);
- Vector v2 = axis.multiply(ray.p0).add(cDown.multiply(cUp));
- double a = v1.normSquared();
- double b = 2 * ( v1.dot(v2));
- double c = v2.normSquared() - (Math.pow(this.rad * this.length, 2));
- double dis = (Math.pow(b, 2) - 4 * a * c);
- if (dis < 0)
- return -1;
- double n1 = -1;
- double n2 = -1;
- if (dis == 0)
- n1 = (-b/(2*a));
- else {
- n1 = (-b + dis) / (2 * a);
- n2 = (-b - dis) / (2 * a);
- }
- double minN = -1;
- if (n1 > 0 && n2 > 0)
- minN = Math.min(n1, n2);
- else if (n1 > 0 && n2 < 0)
- minN = n1;
- else if (n1 < 0 && n2 > 0)
- minN = n2;
- Vector x0 = new Vector(ray.p0.getX() + minN*ray.dir.getX(), ray.p0.getY() + minN*ray.dir.getY(), ray.p0.getZ() + minN*ray.dir.getZ());
- Vector movement = x0.minus(cDown).dot(norm);
- Vector x0OnAxis = new Vector(cDown.getX() + movement * norm.getX(), cDown.getY() + movement * norm.getY(), cDown.getZ() + movement * norm.getZ());
- normal = x0.minus(x0OnAxis);
- Plane upPlane = new Plane(axis, axis.dot(cUp));
- Plane downPlane = new Plane(axis, axis.dot(cDown));
- double planeHit = -1;
- double h1 = upPlane.getHitDistance(ray);
- double hittingPointUp = new Vector(ray.p0.getX() + h1*ray.dir.getX(), ray.p0.getY() + h1*ray.dir.getY(), ray.p0.getZ() + h1*ray.dir.getZ());
- if(cUp.minus(hittingPointUp).normSquared() <= r*r)
- planeHit = h1;
- double h2 = downPlane.getHitDistance(ray);
- double hittingPointDown = new Vector(ray.p0.getX() + h2*ray.dir.getX(), ray.p0.getY() + h2*ray.dir.getY(), ray.p0.getZ() + h2*ray.dir.getZ());
- if(cDown.minus(hittingPointDoown).normSquared() <= r*r && (planeHit < 0 || planeHit > h2))
- planeHit = h2;
- if(minN < 0 && planeHit < 0)
- return -1;
- if(minN > 0 && planeHit > 0) {
- if(minN < planeHit) {
- return minN;
- }
- this.normal = axis;
- return planeHit;
- }
- if(planeHit > 0) {
- this.normal = axis;
- return planeHit;
- }
- return minN;
- }
- public Vector getNormalAtSurfacePoint(Vector intersection) {
- return this.normal;
- }
- public Vector rotateX(Vector vector){
- Vector a = new Vector(1,0,0);
- Vector b = new Vector(0,Math.cos(Math.toRadians(rotation.getX())),-Math.sin(Math.toRadians(rotation.getX())));
- Vector c = new Vector (0,Math.sin(Math.toRadians(rotation.getX())),Math.cos(Math.toRadians(rotation.getX())));
- double x = a.dot(vector);
- double y = b.dot(vector);
- double z = c.dot(vector);
- return new Vector(x,y,z);
- }
- public Vector rotateY(Vector vector){
- Vector a = new Vector(Math.cos(Math.toRadians(rotation.getY())),0,Math.sin(Math.toRadians(rotation.getY())));
- Vector b = new Vector(0,1,0);
- Vector c = new Vector (-Math.sin(Math.toRadians(rotation.getY())),0,Math.cos(Math.toRadians(rotation.getY())));
- double x = a.dot(vector);
- double y = b.dot(vector);
- double z = c.dot(vector);
- return new Vector(x,y,z);
- }
- public Vector rotateZ(Vector vector){
- Vector a = new Vector(Math.cos(Math.toRadians(rotation.getZ())),-Math.sin(Math.toRadians(rotation.getZ())),0);
- Vector b = new Vector(Math.sin(Math.toRadians(rotation.getZ())),Math.cos(Math.toRadians(rotation.getZ())),0);
- Vector c = new Vector (0,0,1);
- double x = a.dot(vector);
- double y = b.dot(vector);
- double z = c.dot(vector);
- return new Vector(x,y,z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement