-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape_cylinder.c
71 lines (64 loc) · 2.54 KB
/
shape_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shape_cylinder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcosnefr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/07 19:04:15 by fcosnefr #+# #+# */
/* Updated: 2016/02/07 19:04:17 by fcosnefr ### ########.fr */
/* */
/* ************************************************************************** */
#include "RTv1.h"
static t_intersectInfo *end_int(t_cylinder *cylinder, t_ray r,
t_intersectInfo *i)
{
t_vector x;
double m;
i->color = cylinder->color;
i->point = (ADD(r.origin, mult(r.direction, i->t)));
x = SUB(r.origin, cylinder->origin);
m = DOT_PROD(r.direction, cylinder->direction) *
i->t + DOT_PROD(x, cylinder->direction);
i->normal = normalize(SUB(SUB(i->point, cylinder->origin),
mult(cylinder->direction, m)));
return (i);
}
t_intersectInfo *project_2d_cylinder(t_cylinder *cylinder, t_ray r,
t_intersectInfo *i)
{
double a;
double b;
double c;
a = SQR(r.direction);
b = 2 * (DOT_PROD(r.direction, r.origin) -
DOT_PROD(r.direction, cylinder->origin));
c = SQR(r.origin) + SQR(cylinder->origin) - 2 *
(DOT_PROD(r.origin, cylinder->origin)) -
(cylinder->radius * cylinder->radius);
if (!find_roots(a, b, c, &(i->t)))
return (NULL);
return (i);
}
t_intersectInfo *cylinder_intersection(t_cylinder *cylinder, t_ray r)
{
t_intersectInfo *i;
t_vector nray_origin;
t_vector nray_direction;
t_cylinder ncylinder;
t_ray nray;
if (!(i = (t_intersectInfo *)malloc(sizeof(t_intersectInfo))))
return (NULL);
nray_origin = SUB(r.origin,
mult(cylinder->direction, DOT_PROD(r.origin, cylinder->direction)));
nray_direction = SUB(r.direction, mult(cylinder->direction,
DOT_PROD(r.direction, cylinder->direction)));
ncylinder = CYLINDER(SUB(cylinder->origin,
mult(cylinder->direction,
DOT_PROD(cylinder->origin, cylinder->direction))),
cylinder->direction, cylinder->radius, cylinder->color);
nray = RAY(nray_origin, nray_direction);
if (!(project_2d_cylinder(&(ncylinder), nray, i)))
return (NULL);
return (end_int(cylinder, r, i));
}