-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder.cpp
273 lines (246 loc) · 4.85 KB
/
cylinder.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "cylinder.h"
#include "ray.h"
#include <algorithm>
#include <cfloat>
// Determine if the ray intersects with the cylinder.
bool Cylinder::Intersection(const Ray& ray, std::vector<Hit>& hits) const
{
// TODO
/*
f(t) = u + tv <-- ray
u = ray.endpoint
v = ray.direction.normalized()
r = radius
p = A
q = B
y = u - p
w = q - p
w_hat = w / w.magnitude()
v_bar = v cross w_hat
y_bar = y cross w_hat
*/
float hitA1 = 0;
float hitA2 = 0;
float hitB1 = 0;
float hitB2 = 0;
int partA = 1;
int partB1 = 0;
int partB2 = 0;
vec3 u = ray.endpoint;
vec3 v = ray.direction.normalized();
double r = radius;
vec3 p = A;
vec3 q = B;
vec3 y = u - p;
vec3 w = q - p;
vec3 w_hat = w / w.magnitude();
vec3 v_bar = cross(v, w_hat);
vec3 y_bar = cross(y, w_hat);
vec3 n = p-q / (p-q).magnitude();
if(dot(v_bar,v_bar) == 0 )
{
//u get hit (0,inf)
if(r >= y_bar.magnitude())
{
hitA1 = 0;
hitA2 = DBL_MAX;
}
else
{
return false;
}
}
else if(r * v_bar.magnitude() >= cross(v_bar, y_bar).magnitude())
{
float temp = sqrt(r*r*dot(v_bar,v_bar) - cross(v_bar,y_bar).magnitude() * cross(v_bar,y_bar).magnitude());
float t1 = -dot(v_bar,y_bar) - temp;
float t2 = -dot(v_bar,y_bar) + temp;
//hit (0,t2)
if(t1 < 0 && t2 >= 0)
{
hitA2 = t2;
}
//hit (t1,t2)
else if(t1 >= 0 && t2 >= 0)
{
hitA1 = t1;
hitA2 = t2;
}
else
{
return false;
}
}
else
{
return false;
}
if(dot(u-p,n) <= 0)
{
if(dot(u-q,n) >= 0)
{
//get (0, inf)
if(dot(v,n) == 0)
{
hitB1 = 0;
hitB2 = DBL_MAX;
}
//get (0, tp)
else if(dot(v,n) > 0)
{
hitB1 = 0;
hitB2 = -dot((u-p),n) / dot(v,n);
partB1 = 0;
partB2 = 2;
}
//get (0, tq)
else
{
partB1 = 0;
partB2 = 3;
hitB1 = 0;
hitB2 = -dot((u-q),n) / dot(v,n);
}
}
}
else if(dot(u-q,n) < 0)
{
//get (tq, tp)
if(dot(v,n) > 0)
{
hitB1 = -dot((u-q),n) / dot(v,n);
hitB2 = -dot((u-p),n) / dot(v,n);
partB1 = 3;
partB2 = 2;
}
else
{
return false;
}
}
else
{
//get (tp, tq)
if(dot(v,n) < 0)
{
hitB1 = -dot((u-p),n) / dot(v,n);
hitB2 = -dot((u-q),n) / dot(v,n);
partB1 = 2;
partB2 = 3;
}
else
{
return false;
}
}
//so we have 4 pts
//look at all 4
//u know 1 < 2
//then the hits is A1 A2
Hit temp;
if( hitA2 < hitB1 )
{
return false;
}
else if( hitB2 < hitA1 )
{
return false;
}
else if( hitA1 <= hitB1 && hitB2 <= hitA2 )
{
std::cerr << "IN";
//hits = hits2;
temp.object = this;
temp.t = hitB1;
temp.part = partB1;
temp.ray_exiting = false;
hits.push_back(temp);
temp.object = this;
temp.t = hitB1;
temp.part = partB2;
temp.ray_exiting = true;
hits.push_back(temp);
return true;
}
else if( hitB1 <= hitA1 && hitA2 <= hitB2 )
{
//std::cerr << "IN";
//hits = hits1;
temp.object = this;
temp.t = hitA1;
temp.part = partA;
temp.ray_exiting = false;
hits.push_back(temp);
temp.object = this;
temp.t = hitA1;
temp.part = partA;
temp.ray_exiting = true;
hits.push_back(temp);
return true;
}
else if( hitA1 <= hitB1 && hitA2 <= hitB2 )
{
//hits.push_back(hits2[0]);
//hits.push_back(hits1[1]);
//std::cerr << "IN";
temp.object = this;
temp.t = hitB1;
temp.part = partB1;
temp.ray_exiting = false;
hits.push_back(temp);
temp.object = this;
temp.t = hitA2;
temp.part = partA;
temp.ray_exiting = true;
hits.push_back(temp);
return true;
}
else if( hitB1 <= hitA1 && hitB2 <= hitA2 )
{
//hits.push_back(hits1[0]);
//hits.push_back(hits2[1]);
//std::cerr << "IN";
temp.object = this;
temp.t = hitA1;
temp.part = partA;
temp.ray_exiting = false;
hits.push_back(temp);
temp.object = this;
temp.t = hitB1;
temp.part = partB2;
temp.ray_exiting = true;
hits.push_back(temp);
return true;
}
return false;
}
// part 1 = side, 2 = A side, 3 = B side
vec3 Cylinder::Normal(const vec3& point, int part) const
{
vec3 normal;
// TODO: set the normal
//find normal of side
if(part == 1)
{
//we know that mag point - X1 is r
// P - B --> BP , A - B --> BA
//
vec3 temp1 = point - this->B;
vec3 temp2 = this->B - this->A;
float i = temp1.magnitude_squared();
float j = temp2[0] * temp1[0] +temp2[1] * temp1[1] +temp2[2] * temp1[2];
float t = i/j;
normal = point - ((this->A - this->B).normalized() * t + this->B);
}
//find p normal
else if(part == 2)
{
normal = (this->A - this->B).normalized();
}
//find q normal
else if(part == 3)
{
normal = (this->B - this->A).normalized();
}
return normal;
}