-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.cpp
More file actions
341 lines (331 loc) · 12 KB
/
Copy pathShape.cpp
File metadata and controls
341 lines (331 loc) · 12 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
** EPITECH PROJECT, 2023
** B-OOP-400-MAR-4-1-raytracer-corentin.fortes
** File description:
** sphere
*/
#include "Shape.hpp"
Sphere::Sphere(Math::Point3D center, double radius) : center(center), radius(radius) {}
std::unique_ptr<Math::Point3D> Sphere::hits(RayTracer::Ray ray)
{
Math::Vector3D oc = ray.point - center;
double a = ray.vector.dot(ray.vector);
double b = 2 * oc.dot(ray.vector);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return nullptr;
}
if (discriminant == 0) {
double t = -b / (2.0 * a);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
}
double rassine = sqrt(discriminant);
double t1 = (-b - rassine) / (2.0 * a);
double t2 = (-b + rassine) / (2.0 * a);
if (t1 < 0.0 && t2 < 0.0) {
return nullptr;
}
else {
if (t1 < t2) {
double z = ray.point.z + t1 * ray.vector.z;
double x = ray.point.x + t1 * ray.vector.x;
double y = ray.point.y + t1 * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
} else if (t2 < t1) {
double z = ray.point.z + t2 * ray.vector.z;
double x = ray.point.x + t2 * ray.vector.x;
double y = ray.point.y + t2 * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
}
}
return nullptr;
}
Math::Vector3D Sphere::normal(RayTracer::Ray ray)
{
Math::Vector3D oc = ray.point - center;
double a = ray.vector.dot(ray.vector);
double b = 2 * oc.dot(ray.vector);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * a * c;
if (discriminant == 0) {
double t = -b / (2.0 * a);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
}
double rassine = sqrt(discriminant);
double t1 = (-b - rassine) / (2.0 * a);
double t2 = (-b + rassine) / (2.0 * a);
if (t1 < t2) {
double z = ray.point.z + t1 * ray.vector.z;
double x = ray.point.x + t1 * ray.vector.x;
double y = ray.point.y + t1 * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
} else if (t2 < t1) {
double z = ray.point.z + t2 * ray.vector.z;
double x = ray.point.x + t2 * ray.vector.x;
double y = ray.point.y + t2 * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
}
Math::Vector3D n(0, 0, 0);
return n;
}
Plane::Plane(double y, char a) : coo(y), axis(a) {}
std::unique_ptr<Math::Point3D> Plane::hits(RayTracer::Ray ray)
{
if (axis == 'x') {
double value = (coo - ray.point.x);
if (std::abs(value) < std::abs(value + ray.vector.x))
return nullptr;
else {
Math::Point3D p1(coo, 100, 200);
Math::Point3D p2(coo, 200, -100);
Math::Point3D p3(coo, -30, -50);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
double t = normal.dot(p1 - ray.point) / normal.dot(ray.vector);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
if (pi->z < 0)
pi->z = pi->z * -1;
pi->x = pi->x * -1;
pi->y = pi->y * -1;
// pi->z = pi->z * -1;
return pi;
}
}
if (axis == 'y') {
double value = (coo - ray.point.y);
if (std::abs(value) < std::abs(value + ray.vector.y))
return nullptr;
else {
Math::Point3D p1(100, coo, 200);
Math::Point3D p2(200, coo, -100);
Math::Point3D p3(-30, coo, -50);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
double t = normal.dot(p1 - ray.point) / normal.dot(ray.vector);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
if (pi->z < 0)
pi->z = pi->z * -1;
pi->x = pi->x * -1;
pi->y = pi->y * -1;
// pi->z = pi->z * -1;
return pi;;
}
}
if (axis == 'z') {
double value = (coo - ray.point.z);
if (std::abs(value) < std::abs(value + ray.vector.z))
return nullptr;
else {
Math::Point3D p1(100, 200, coo);
Math::Point3D p2(200, -100, coo);
Math::Point3D p3(-30, -50, coo);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
double t = normal.dot(p1 - ray.point) / normal.dot(ray.vector);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
if (pi->z < 0)
pi->z = pi->z * -1;
pi->x = pi->x * -1;
pi->y = pi->y * -1;
// pi->z = pi->z * -1;
return pi;
}
}
return NULL;
}
Math::Vector3D Plane::normal(RayTracer::Ray ray)
{
if (axis == 'x') {
Math::Point3D p1(coo, 100, 200);
Math::Point3D p2(coo, 200, -100);
Math::Point3D p3(coo, -30, -50);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
// normal.y = normal.y * -1;
return normal * -1;
}
if (axis == 'y') {
Math::Point3D p1(100, coo, 200);
Math::Point3D p2(200, coo, -100);
Math::Point3D p3(-30, coo, -50);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
// normal.y = normal.y * -1;
return normal * -1;
}
if (axis == 'z') {
Math::Point3D p1(100, 200, coo);
Math::Point3D p2(200, -100, coo);
Math::Point3D p3(-30, -50, coo);
Math::Vector3D v1 = p2 - p1;
Math::Vector3D v2 = p3 - p1;
Math::Vector3D normal = v1 * v2;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
// normal.y = normal.y * -1;
return normal * -1;
}
Math::Vector3D n(0, 0, 0);
return n;
}
Cylindre::Cylindre(Math::Point3D center, double radius, char ax) : center(center), radius(radius), axis(ax) {}
std::unique_ptr<Math::Point3D>Cylindre::hits(RayTracer::Ray ray)
{
if (axis == 'x') {
ray.vector.x = 0;
ray.point.x = 0;
center.x = 0;
}
if (axis == 'y') {
ray.vector.y = 0;
ray.point.y = 0;
center.y = 0;
}
if (axis == 'z') {
ray.vector.z = 0;
ray.point.z = 0;
center.z = 0;
}
Math::Vector3D oc = ray.point - center;
double a = ray.vector.dot(ray.vector);
double b = 2 * oc.dot(ray.vector);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
return nullptr;
}
if (discriminant == 0) {
double t = -b / (2.0 * a);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
}
double rassine = sqrt(discriminant);
double t1 = (-b - rassine) / (2.0 * a);
double t2 = (-b + rassine) / (2.0 * a);
if (t1 < 0.0 && t2 < 0.0) {
return nullptr;
}
else {
if (t1 < t2) {
double z = ray.point.z + t1 * ray.vector.z;
double x = ray.point.x + t1 * ray.vector.x;
double y = ray.point.y + t1 * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
} else if (t2 < t1) {
double z = ray.point.z + t2 * ray.vector.z;
double x = ray.point.x + t2 * ray.vector.x;
double y = ray.point.y + t2 * ray.vector.y;
std::unique_ptr<Math::Point3D> pi = std::make_unique<Math::Point3D>(x, y, z);
return pi;
}
}
return nullptr;
}
Math::Vector3D Cylindre::normal(RayTracer::Ray ray)
{
if (axis == 'x') {
ray.vector.x = 0;
ray.point.x = 0;
center.x = 0;
}
if (axis == 'y') {
ray.vector.y = 0;
ray.point.y = 0;
center.y = 0;
}
if (axis == 'z') {
ray.vector.z = 0;
ray.point.z = 0;
center.z = 0;
}
Math::Vector3D oc = ray.point - center;
double a = ray.vector.dot(ray.vector);
double b = 2 * oc.dot(ray.vector);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * a * c;
if (discriminant == 0) {
double t = -b / (2.0 * a);
double z = ray.point.z + t * ray.vector.z;
double x = ray.point.x + t * ray.vector.x;
double y = ray.point.y + t * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
}
double rassine = sqrt(discriminant);
double t1 = (-b - rassine) / (2.0 * a);
double t2 = (-b + rassine) / (2.0 * a);
if (t1 < t2) {
double z = ray.point.z + t1 * ray.vector.z;
double x = ray.point.x + t1 * ray.vector.x;
double y = ray.point.y + t1 * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
} else if (t2 < t1) {
double z = ray.point.z + t2 * ray.vector.z;
double x = ray.point.x + t2 * ray.vector.x;
double y = ray.point.y + t2 * ray.vector.y;
Math::Point3D pi(x, y, z);
Math::Vector3D normal = pi - center;
double n = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal = normal / n;
return normal;
}
Math::Vector3D n(0, 0, 0);
return n;
}