-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.java
107 lines (80 loc) · 2.09 KB
/
Sphere.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
*
*/
package rayCast;
/**
* @author itay
*
*/
public class Sphere extends Shape {
/* (non-Javadoc)
* @see rayCast.Shape#Intersect(rayCast.Ray)
*/
private double r;
private Vector center;
public Sphere(){};
public void setRadius(double r){this.r=r;}
public void setCenter(Vector v){this.center = v;}
public Sphere(double r, Vector v){
this.center =v;
this.r =r;
}
//overloading
public Sphere(double r, double x,double y,double z){
Vector v = new BVector();
v.setXyz(new Point3D(x,y,z));
this.center =v;
this.r = r;
}
public double Intersect(Ray ray) {
// double a = *** Not required for 'pure' spheres (of uniform radius) ***
Vector tmp = ray.getDirection();
Vector tmp2 = ray.getStartPoint().sub(center);
double b = 2* tmp.dot(tmp2);
double c = Math.pow(tmp2.getSize(),2) - Math.pow(r, 2);
double d = b*b - 4*c;
if (d <= 0)
return 0;
d = Math.sqrt(d);
double t1 = (-b+d)/2.0;
double t2 = (-b-d)/2.0;
if (t1 <0 || t2 < 0)
return 0;
// Ray is inside if there is only 1 positive root
// Added for refractive transparency
if (t1 < 0 && t2 > 0)
{
return t2;
}
if (t2 < 0 && t1 > 0)
{
return t1;
}
return (t1 < t2) ? t1 : t2;
}
public void getIntersection(Ray ray, double t,Intersection inter){
inter.rayVector = ray.getStartPoint().add(ray.getDirection().MultScalar(t));
inter.norm = inter.rayVector.sub(center).norm();
}
public Point2D getUV(Vector p){
/* Vector rp = p.sub(center);
double v = Math.acos(rp.getXyz().zf/r);
double u = Math.acos(rp.getXyz().xr/(r * Math.sin(v)));
if (rp.getXyz().yt < 0)
u = -u;
if (rp.getXyz().zf < 0)
v = v + Math.PI;
System.out.println(u + " " + v);
return new Point2D(u / (2*Math.PI)+0.5, (v / (Math.PI)-0.5)/2);*/
Vector cent = center.sub(p);
double u = cent.getRtf().yt/((Math.PI));
double v = cent.getRtf().zf;
if (v < 0 )
v+= Math.PI;
v/=(Math.PI);
return new Point2D(u,v);
}
public Point2D getTextureSize(){
return new Point2D(1,1);
}
}