Advertisement
Guest User

Untitled

a guest
May 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package RayTracing;
  2.  
  3. public class Cylinder {
  4.  
  5.     private Vector center;
  6.     private Float length;
  7.     private Float rad;
  8.     private Vector rotation;
  9.     private Integer matIndex;
  10.    
  11.     private Vector noraml;
  12.    
  13.    
  14.     public Cylinder() {
  15.         this.normal = null;
  16.     }
  17.    
  18.     public Vector getCenter() {
  19.         return center;
  20.     }
  21.     public void setCenter(Vector center) {
  22.         this.center = center;
  23.     }
  24.     public Float getLength() {
  25.         return length;
  26.     }
  27.     public void setLength(Float length) {
  28.         this.length = length;
  29.     }
  30.     public Float getRad() {
  31.         return rad;
  32.     }
  33.     public void setRad(Float rad) {
  34.         this.rad = rad;
  35.     }
  36.     public Vector getRotation() {
  37.         return rotation;
  38.     }
  39.     public void setRotation(Vector rotation) {
  40.         this.rotation = rotation;
  41.     }
  42.     public Integer getMatIndex() {
  43.         return matIndex;
  44.     }
  45.     public void setMatIndex(Integer matIndex) {
  46.         this.matIndex = matIndex;
  47.     }
  48.  
  49.     public double getHitDistance(Ray ray) {
  50.        
  51.         //STEP 1
  52.         //rotation
  53.        
  54.         Vector norm = new Vector(0,0,1);
  55.         norm = norm.rotate(this.rotation);
  56.        
  57.        
  58.         //STEP 2
  59.         Vector s = norm.scale((this.length)/2);
  60.         Vector cUp = this.center.add(s);
  61.         Vector cDown = this.center.minus(s);
  62.        
  63.         //STEP 3
  64.         Vector axis = (cUp.minus(cDown));
  65.         Vector v1 = axis.multiply(ray.dir);
  66.         Vector v2 = axis.multiply(ray.p0).add(cDown.multiply(cUp));
  67.        
  68.         double a = v1.normSquared();
  69.         double b = 2 * ( v1.dot(v2));
  70.         double c = v2.normSquared()  - (Math.pow(this.rad * this.length, 2));
  71.  
  72.         double dis = (Math.pow(b, 2) - 4 * a * c);
  73.        
  74.         if (dis < 0)
  75.             return -1;
  76.        
  77.         double n1 = -1;
  78.         double n2 = -1;
  79.        
  80.         if (dis == 0)
  81.             n1 = (-b/(2*a));
  82.        
  83.         else {
  84.             n1 = (-b + dis) / (2 * a);
  85.             n2 = (-b - dis) / (2 * a);
  86.         }
  87.        
  88.         double minN = -1;
  89.         if (n1 > 0 && n2 > 0)
  90.             minN = Math.min(n1, n2);
  91.         else if (n1 > 0 && n2 < 0)
  92.                 minN = n1;
  93.         else if (n1 < 0 && n2 > 0)
  94.                 minN = n2;
  95.        
  96.        
  97.         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());
  98.         Vector movement = x0.minus(cDown).dot(norm);
  99.         Vector x0OnAxis = new Vector(cDown.getX() + movement * norm.getX(), cDown.getY() + movement * norm.getY(), cDown.getZ() + movement * norm.getZ());
  100.         normal = x0.minus(x0OnAxis);
  101.        
  102.         Plane upPlane = new Plane(axis, axis.dot(cUp));
  103.         Plane downPlane = new Plane(axis, axis.dot(cDown));
  104.        
  105.         double planeHit = -1;
  106.  
  107.         double h1 = upPlane.getHitDistance(ray);
  108.         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());
  109.        
  110.         if(cUp.minus(hittingPointUp).normSquared() <= r*r)
  111.             planeHit = h1;
  112.  
  113.         double h2 = downPlane.getHitDistance(ray);
  114.         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());
  115.         if(cDown.minus(hittingPointDoown).normSquared() <= r*r && (planeHit < 0 || planeHit > h2))
  116.             planeHit = h2;
  117.  
  118.  
  119.         if(minN < 0 && planeHit < 0)
  120.             return -1;
  121.  
  122.         if(minN > 0 && planeHit > 0) {
  123.             if(minN < planeHit) {
  124.                 return minN;
  125.             }
  126.             this.normal = axis;
  127.             return planeHit;
  128.         }
  129.  
  130.         if(planeHit > 0) {
  131.             this.normal = axis;
  132.             return planeHit;
  133.         }
  134.         return minN;       
  135.     }
  136.  
  137.     public Vector getNormalAtSurfacePoint(Vector intersection) {
  138.         return this.normal;
  139.     }
  140.    
  141.     public Vector rotateX(Vector vector){
  142.         Vector a = new Vector(1,0,0);
  143.         Vector b = new Vector(0,Math.cos(Math.toRadians(rotation.getX())),-Math.sin(Math.toRadians(rotation.getX())));
  144.         Vector c = new Vector (0,Math.sin(Math.toRadians(rotation.getX())),Math.cos(Math.toRadians(rotation.getX())));
  145.        
  146.         double x = a.dot(vector);
  147.         double y = b.dot(vector);
  148.         double z = c.dot(vector);
  149.        
  150.         return new Vector(x,y,z);
  151.     }
  152.    
  153.    
  154.     public Vector rotateY(Vector vector){
  155.         Vector a = new Vector(Math.cos(Math.toRadians(rotation.getY())),0,Math.sin(Math.toRadians(rotation.getY())));
  156.         Vector b = new Vector(0,1,0);
  157.         Vector c = new Vector (-Math.sin(Math.toRadians(rotation.getY())),0,Math.cos(Math.toRadians(rotation.getY())));
  158.        
  159.         double x = a.dot(vector);
  160.         double y = b.dot(vector);
  161.         double z = c.dot(vector);
  162.        
  163.         return new Vector(x,y,z);
  164.     }
  165.    
  166.     public Vector rotateZ(Vector vector){
  167.         Vector a = new Vector(Math.cos(Math.toRadians(rotation.getZ())),-Math.sin(Math.toRadians(rotation.getZ())),0);
  168.         Vector b = new Vector(Math.sin(Math.toRadians(rotation.getZ())),Math.cos(Math.toRadians(rotation.getZ())),0);
  169.         Vector c = new Vector (0,0,1);
  170.        
  171.         double x = a.dot(vector);
  172.         double y = b.dot(vector);
  173.         double z = c.dot(vector);
  174.        
  175.         return new Vector(x,y,z);
  176.     }
  177.    
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement