rtv1/src/maths_cylinder.c

53 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* maths_cylinder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/08 18:31:29 by gbrochar #+# #+# */
/* Updated: 2019/02/22 17:26:27 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "rtv1.h"
double intersect_cylinder(void *obj, t_ray r)
{
t_cylinder *cy;
t_vec sub_ori;
double roots[2];
double params[3];
cy = (t_cylinder *)obj;
sub_ori = vec_sub(r.o, cy->o);
params[0] = dot_product(r.d, r.d) - pow(dot_product(r.d, cy->d), 2);
params[1] = (double)2 * (dot_product(r.d, sub_ori)
- (dot_product(r.d, cy->d) * dot_product(sub_ori, cy->d)));
params[2] = dot_product(sub_ori, sub_ori)
- pow(dot_product(sub_ori, cy->d), 2) - pow(cy->r, 2);
if (SUCCESS == solve_quadratic(params, roots))
{
if (roots[0] > 0)
return (roots[0] - E);
else if (roots[1] > 0)
return (roots[1] - E);
}
return (FAILURE);
}
t_vec normal_cylinder(void *obj, t_ray r, double t)
{
t_cylinder *cy;
t_vec p;
double m;
cy = (t_cylinder *)obj;
p = vec_add(r.o, vec_mul(r.d, t));
m = dot_product(r.d, cy->d) * t
+ dot_product(vec_sub(r.o, cy->o), cy->d);
p = vec_sub(vec_sub(p, cy->o), vec_mul(cy->d, m));
normalize(&p);
return (p);
}