-
Notifications
You must be signed in to change notification settings - Fork 4
/
Cylinder.c
169 lines (122 loc) · 5.07 KB
/
Cylinder.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "Cylinder.h"
#include "Plane.h"
#include "Ray.h"
#include "pi.h"
#include <math.h>
//#include "randf.h"
Cylinder makeCylinder(const Vector endA, const Vector endB, const float radius) {
return (Cylinder) {endA, endB, radius};
}
Intersection makeCapIntersection(const Cylinder cylinder, const Ray ray, const Plane cap, const Vector relevantEnd) {
Intersection capIntersection = pIntersect(cap, ray);
if (capIntersection.hitType == missed || vLengthSquared(vSub(capIntersection.position, relevantEnd)) < cylinder.radius * cylinder.radius) {
return capIntersection;
}
return makeIntersectionMiss();
}
Intersection makeCylinderIntersection(const float distance, const Cylinder cylinder, const Ray ray, const Vector endAInCylinderPlane, const Vector cylinderDirection, const float endAAlongDirection, const float endBAlongDirection) {
Vector position = vAdd(ray.origin, vsMul(ray.direction, distance));
// Clip to beginning and end.
float intersectionAlongDirection = vDot(position, cylinderDirection);
if (intersectionAlongDirection > endAAlongDirection && intersectionAlongDirection < endBAlongDirection) {
Intersection intersection;
intersection.hitType = surface;
intersection.distance = distance;
intersection.position = position;
Vector intersectionProjectedOntoCylinderDirection = vAdd(endAInCylinderPlane, vsMul(cylinderDirection, vDot(intersection.position, cylinderDirection)));
intersection.normal = vsDiv(vSub(intersection.position, intersectionProjectedOntoCylinderDirection), cylinder.radius);
return intersection;
}
return makeIntersectionMiss();
}
Intersection cIntersect(const Cylinder cylinder, const Ray ray) {
// TODO: Store endA, direction and length instead. Compute in the constructor.
// Normalize stuff.
Vector cylinderVector = vSub(cylinder.endB, cylinder.endA);
float cylinderLength = vLength(cylinderVector);
Vector cylinderDirection = vsDiv(cylinderVector, cylinderLength);
// Working in 2D is easier, so project the ray onto the plane with the cylinder as it's normal.
float rayDirectionAlongDirection = vDot(ray.direction, cylinderDirection);
Vector rayDirectionProjectedOntoCylinderVector = vsMul(cylinderDirection, rayDirectionAlongDirection);
Vector rayDirectionInCylinderPlane = vNormalized(vSub(ray.direction, rayDirectionProjectedOntoCylinderVector));
float endAAlongDirection = vDot(cylinder.endA, cylinderDirection);
float endBAlongDirection = vDot(cylinder.endB, cylinderDirection);
Vector endAProjectedOntoCylinderVector = vsMul(cylinderDirection, endAAlongDirection);
Vector endAInCylinderPlane = vSub(cylinder.endA, endAProjectedOntoCylinderVector);
Vector rayOriginProjectedOntoCylinderVector = vAdd(endAInCylinderPlane, vsMul(cylinderDirection, vDot(ray.origin, cylinderDirection)));
// Modified sphere intersetion test.
float b = vDot(vSub(ray.origin, rayOriginProjectedOntoCylinderVector), rayDirectionInCylinderPlane);
float c = vLengthSquared(vSub(ray.origin, rayOriginProjectedOntoCylinderVector)) - cylinder.radius*cylinder.radius;
float det = b*b - c;
Intersection cylinderIntersection = makeIntersectionMiss();
if (det>=0) {
det = sqrtf(det);
float t1 = -b - det;
if (t1>0.0001) {
// t1 is the distance *in the cylinder plane*.
cylinderIntersection = makeCylinderIntersection(
t1 / sqrtf(1-rayDirectionAlongDirection*rayDirectionAlongDirection),
cylinder,
ray,
endAInCylinderPlane,
cylinderDirection,
endAAlongDirection,
endBAlongDirection
);
} else {
float t2=-b+det;
if (t2>0.0001) {
cylinderIntersection = makeCylinderIntersection(
t2 / sqrtf(1-rayDirectionAlongDirection*rayDirectionAlongDirection),
cylinder,
ray,
endAInCylinderPlane,
cylinderDirection,
endAAlongDirection,
endBAlongDirection
);
}
}
}
// Intersect end caps.
Intersection capIntersection;
float rayDirectionAlongCylinderDirection = vDot(ray.direction, cylinderDirection);
if (rayDirectionAlongCylinderDirection > 0) {
capIntersection = makeCapIntersection(
cylinder,
ray,
makePlane(vNegated(cylinderDirection), -endAAlongDirection),
cylinder.endA
);
} else {
capIntersection = makeCapIntersection(
cylinder,
ray,
makePlane(cylinderDirection, endBAlongDirection),
cylinder.endB
);
}
// Return the closest intersection.
if (cylinderIntersection.hitType != missed && capIntersection.hitType != missed) {
return cylinderIntersection.distance < capIntersection.distance ? cylinderIntersection : capIntersection;
} else if (cylinderIntersection.hitType != missed) {
return cylinderIntersection;
} else {
return capIntersection;
}
}
//Vector cSampleSurface(const Cylinder s) {
//
// return vAdd(s.position, vRotated(
// vRotated(makeVector(s.radius, 0, 0), makeVector(0, 1, 0), acosf(randf()*2-1)),
// makeVector(1, 0, 0),
// randf() * 2*PI
// ));
//}
float cSurfaceArea(const Cylinder c) {
float capArea = c.radius * c.radius * PI;
float length = vLength(vSub(c.endA, c.endB));
float circumference = c.radius * PI * 2;
float sideArea = circumference * length;
return capArea * 2 + sideArea;
}