-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCylinder.java
More file actions
152 lines (131 loc) · 4.94 KB
/
Copy pathCylinder.java
File metadata and controls
152 lines (131 loc) · 4.94 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* The MIT License
*
* Copyright 2023 akava.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ptjava;
class Cylinder extends TransformedShape {
double INF = 1e9;
double EPS = 1e-9;
double Radius;
double Z0, Z1;
Material CylinderMaterial;
Cylinder(double radius, double z0, double z1, Material material) {
this.Radius = radius;
this.Z0 = z0;
this.Z1 = z1;
this.CylinderMaterial = material;
}
static Cylinder NewCylinder(double radius, double z0, double z1, Material material) {
return new Cylinder(radius, z0, z1, material);
}
IShape NewTransformedCylinder(Vector v0, Vector v1, double radius, Material material) {
Vector up = new Vector(0,0,1);
Vector d = v1.Sub(v0);
double z = d.Length();
double a = Math.acos(d.Normalize().Dot(up));
Matrix m = new Matrix().Translate(v0);
if (a != 0) {
Vector u = d.Cross(up).Normalize();
m = m.Rotate(u, a).Translate(v0);
}
Cylinder c = NewCylinder(radius, 0, z, material);
return NewTransformedShape(c, m);
}
@Override
public void Compile() {
}
@Override
public Box BoundingBox() {
double radius = this.Radius;
return new Box(new Vector(-radius, -radius, this.Z0), new Vector(radius, radius, this.Z1));
}
@Override
public Vector UV(Vector p) {
return p;
}
@Override
public Material MaterialAt(Vector p) {
return this.CylinderMaterial;
}
@Override
public Vector NormalAt(Vector p) {
if (Math.abs(p.getZ() - Z0) < EPS) {
return new Vector(0, 0, -1);
} else if (Math.abs(p.getZ() - Z1) < EPS) {
return new Vector(0, 0, 1);
}
return new Vector(p.getX(), p.getY(), 0).Normalize();
}
@Override
public Hit Intersect(Ray ray) {
double r = Radius;
Vector o = ray.Origin;
Vector d = ray.Direction;
double bestT = INF;
// Check lateral surface
double a = d.getX() * d.getX() + d.getY() * d.getY();
double b = 2 * (o.getX() * d.getX() + o.getY() * d.getY());
double c = o.getX() * o.getX() + o.getY() * o.getY() - r * r;
double discriminant = b * b - 4 * a * c;
if (discriminant >= 0) {
double sqrtDisc = Math.sqrt(discriminant);
double t2 = (-b - sqrtDisc) / (2 * a);
double t1 = (-b + sqrtDisc) / (2 * a);
if (t2 > EPS) {
double z = o.getZ() + d.getZ() * t2;
if (z >= Z0 && z <= Z1 && t2 < bestT) {
bestT = t2;
}
}
if (t1 > EPS) {
double z = o.getZ() + d.getZ() * t1;
if (z >= Z0 && z <= Z1 && t1 < bestT) {
bestT = t1;
}
}
}
// Check bottom cap
if (Math.abs(d.getZ()) > EPS) {
double tBottom = (Z0 - o.getZ()) / d.getZ();
if (tBottom > EPS && tBottom < bestT) {
double px = o.getX() + d.getX() * tBottom;
double py = o.getY() + d.getY() * tBottom;
if (px * px + py * py <= r * r) {
bestT = tBottom;
}
}
// Check top cap
double tTop = (Z1 - o.getZ()) / d.getZ();
if (tTop > EPS && tTop < bestT) {
double px = o.getX() + d.getX() * tTop;
double py = o.getY() + d.getY() * tTop;
if (px * px + py * py <= r * r) {
bestT = tTop;
}
}
}
if (bestT < INF) {
return new Hit(this, bestT, null);
}
return Hit.NoHit;
}
}