istereo

diff libs/vmath/geom.c @ 28:c0ae8e668447

added vmath library
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:42 +0300
parents
children ff055bff6a15
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/geom.c	Thu Sep 08 08:30:42 2011 +0300
     1.3 @@ -0,0 +1,131 @@
     1.4 +#include <math.h>
     1.5 +#include "geom.h"
     1.6 +#include "vector.h"
     1.7 +
     1.8 +plane_t plane_cons(scalar_t nx, scalar_t ny, scalar_t nz, scalar_t d)
     1.9 +{
    1.10 +	plane_t p;
    1.11 +	p.norm.x = nx;
    1.12 +	p.norm.y = ny;
    1.13 +	p.norm.z = nz;
    1.14 +	p.d = d;
    1.15 +	return p;
    1.16 +}
    1.17 +
    1.18 +plane_t plane_poly(vec3_t v0, vec3_t v1, vec3_t v2)
    1.19 +{
    1.20 +	vec3_t a, b, norm;
    1.21 +
    1.22 +	a = v3_sub(v1, v0);
    1.23 +	b = v3_sub(v2, v0);
    1.24 +	norm = v3_cross(a, b);
    1.25 +	norm = v3_normalize(norm);
    1.26 +
    1.27 +	return plane_ptnorm(v0, norm);
    1.28 +}
    1.29 +
    1.30 +plane_t plane_ptnorm(vec3_t pt, vec3_t normal)
    1.31 +{
    1.32 +	plane_t plane;
    1.33 +
    1.34 +	plane.norm = normal;
    1.35 +	plane.d = v3_dot(pt, normal);
    1.36 +
    1.37 +	return plane;
    1.38 +}
    1.39 +
    1.40 +plane_t plane_invert(plane_t p)
    1.41 +{
    1.42 +	p.norm = v3_neg(p.norm);
    1.43 +	p.d = -p.d;
    1.44 +	return p;
    1.45 +}
    1.46 +
    1.47 +scalar_t plane_signed_dist(plane_t plane, vec3_t pt)
    1.48 +{
    1.49 +	vec3_t pp = plane_point(plane);
    1.50 +	vec3_t pptopt = v3_sub(pt, pp);
    1.51 +	return v3_dot(pptopt, plane.norm);
    1.52 +}
    1.53 +
    1.54 +scalar_t plane_dist(plane_t plane, vec3_t pt)
    1.55 +{
    1.56 +	return fabs(plane_signed_dist(plane, pt));
    1.57 +}
    1.58 +
    1.59 +vec3_t plane_point(plane_t plane)
    1.60 +{
    1.61 +	return v3_scale(plane.norm, plane.d);
    1.62 +}
    1.63 +
    1.64 +int plane_ray_intersect(ray_t ray, plane_t plane, scalar_t *pos)
    1.65 +{
    1.66 +	vec3_t pt, orig_to_pt;
    1.67 +	scalar_t ndotdir;
    1.68 +
    1.69 +	pt = plane_point(plane);
    1.70 +	ndotdir = v3_dot(plane.norm, ray.dir);
    1.71 +
    1.72 +	if(fabs(ndotdir) < 1e-7) {
    1.73 +		return 0;
    1.74 +	}
    1.75 +
    1.76 +	if(pos) {
    1.77 +		orig_to_pt = v3_sub(pt, ray.origin);
    1.78 +		*pos = v3_dot(plane.norm, orig_to_pt) / ndotdir;
    1.79 +	}
    1.80 +	return 1;
    1.81 +}
    1.82 +
    1.83 +sphere_t sphere_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t rad)
    1.84 +{
    1.85 +	sphere_t sph;
    1.86 +	sph.pos.x = x;
    1.87 +	sph.pos.y = y;
    1.88 +	sph.pos.z = z;
    1.89 +	sph.rad = rad;
    1.90 +	return sph;
    1.91 +}
    1.92 +
    1.93 +int sphere_ray_intersect(ray_t ray, sphere_t sph, scalar_t *pos)
    1.94 +{
    1.95 +	scalar_t a, b, c, d, sqrt_d, t1, t2, t;
    1.96 +
    1.97 +	a = v3_dot(ray.dir, ray.dir);
    1.98 +	b = 2.0 * ray.dir.x * (ray.origin.x - sph.pos.x) +
    1.99 +		2.0 * ray.dir.y * (ray.origin.y - sph.pos.y) +
   1.100 +		2.0 * ray.dir.z * (ray.origin.z - sph.pos.z);
   1.101 +	c = v3_dot(sph.pos, sph.pos) + v3_dot(ray.origin, ray.origin) +
   1.102 +		2.0 * v3_dot(v3_neg(sph.pos), ray.origin) - sph.rad * sph.rad;
   1.103 +
   1.104 +	d = b * b - 4.0 * a * c;
   1.105 +	if(d < 0.0) {
   1.106 +		return 0;
   1.107 +	}
   1.108 +
   1.109 +	sqrt_d = sqrt(d);
   1.110 +	t1 = (-b + sqrt_d) / (2.0 * a);
   1.111 +	t2 = (-b - sqrt_d) / (2.0 * a);
   1.112 +
   1.113 +	if(t1 < 1e-7 || t1 > 1.0) {
   1.114 +		t1 = t2;
   1.115 +	}
   1.116 +	if(t2 < 1e-7 || t2 > 1.0) {
   1.117 +		t2 = t1;
   1.118 +	}
   1.119 +	t = t1 < t2 ? t1 : t2;
   1.120 +
   1.121 +	if(t < 1e-7 || t > 1.0) {
   1.122 +		return 0;
   1.123 +	}
   1.124 +
   1.125 +	if(pos) {
   1.126 +		*pos = t;
   1.127 +	}
   1.128 +	return 1;
   1.129 +}
   1.130 +
   1.131 +int sphere_sphere_intersect(sphere_t sph1, sphere_t sph2, scalar_t *pos, scalar_t *rad)
   1.132 +{
   1.133 +	return -1;
   1.134 +}