istereo

annotate 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
rev   line source
nuclear@28 1 #include <math.h>
nuclear@28 2 #include "geom.h"
nuclear@28 3 #include "vector.h"
nuclear@28 4
nuclear@28 5 plane_t plane_cons(scalar_t nx, scalar_t ny, scalar_t nz, scalar_t d)
nuclear@28 6 {
nuclear@28 7 plane_t p;
nuclear@28 8 p.norm.x = nx;
nuclear@28 9 p.norm.y = ny;
nuclear@28 10 p.norm.z = nz;
nuclear@28 11 p.d = d;
nuclear@28 12 return p;
nuclear@28 13 }
nuclear@28 14
nuclear@28 15 plane_t plane_poly(vec3_t v0, vec3_t v1, vec3_t v2)
nuclear@28 16 {
nuclear@28 17 vec3_t a, b, norm;
nuclear@28 18
nuclear@28 19 a = v3_sub(v1, v0);
nuclear@28 20 b = v3_sub(v2, v0);
nuclear@28 21 norm = v3_cross(a, b);
nuclear@28 22 norm = v3_normalize(norm);
nuclear@28 23
nuclear@28 24 return plane_ptnorm(v0, norm);
nuclear@28 25 }
nuclear@28 26
nuclear@28 27 plane_t plane_ptnorm(vec3_t pt, vec3_t normal)
nuclear@28 28 {
nuclear@28 29 plane_t plane;
nuclear@28 30
nuclear@28 31 plane.norm = normal;
nuclear@28 32 plane.d = v3_dot(pt, normal);
nuclear@28 33
nuclear@28 34 return plane;
nuclear@28 35 }
nuclear@28 36
nuclear@28 37 plane_t plane_invert(plane_t p)
nuclear@28 38 {
nuclear@28 39 p.norm = v3_neg(p.norm);
nuclear@28 40 p.d = -p.d;
nuclear@28 41 return p;
nuclear@28 42 }
nuclear@28 43
nuclear@28 44 scalar_t plane_signed_dist(plane_t plane, vec3_t pt)
nuclear@28 45 {
nuclear@28 46 vec3_t pp = plane_point(plane);
nuclear@28 47 vec3_t pptopt = v3_sub(pt, pp);
nuclear@28 48 return v3_dot(pptopt, plane.norm);
nuclear@28 49 }
nuclear@28 50
nuclear@28 51 scalar_t plane_dist(plane_t plane, vec3_t pt)
nuclear@28 52 {
nuclear@28 53 return fabs(plane_signed_dist(plane, pt));
nuclear@28 54 }
nuclear@28 55
nuclear@28 56 vec3_t plane_point(plane_t plane)
nuclear@28 57 {
nuclear@28 58 return v3_scale(plane.norm, plane.d);
nuclear@28 59 }
nuclear@28 60
nuclear@28 61 int plane_ray_intersect(ray_t ray, plane_t plane, scalar_t *pos)
nuclear@28 62 {
nuclear@28 63 vec3_t pt, orig_to_pt;
nuclear@28 64 scalar_t ndotdir;
nuclear@28 65
nuclear@28 66 pt = plane_point(plane);
nuclear@28 67 ndotdir = v3_dot(plane.norm, ray.dir);
nuclear@28 68
nuclear@28 69 if(fabs(ndotdir) < 1e-7) {
nuclear@28 70 return 0;
nuclear@28 71 }
nuclear@28 72
nuclear@28 73 if(pos) {
nuclear@28 74 orig_to_pt = v3_sub(pt, ray.origin);
nuclear@28 75 *pos = v3_dot(plane.norm, orig_to_pt) / ndotdir;
nuclear@28 76 }
nuclear@28 77 return 1;
nuclear@28 78 }
nuclear@28 79
nuclear@28 80 sphere_t sphere_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t rad)
nuclear@28 81 {
nuclear@28 82 sphere_t sph;
nuclear@28 83 sph.pos.x = x;
nuclear@28 84 sph.pos.y = y;
nuclear@28 85 sph.pos.z = z;
nuclear@28 86 sph.rad = rad;
nuclear@28 87 return sph;
nuclear@28 88 }
nuclear@28 89
nuclear@28 90 int sphere_ray_intersect(ray_t ray, sphere_t sph, scalar_t *pos)
nuclear@28 91 {
nuclear@28 92 scalar_t a, b, c, d, sqrt_d, t1, t2, t;
nuclear@28 93
nuclear@28 94 a = v3_dot(ray.dir, ray.dir);
nuclear@28 95 b = 2.0 * ray.dir.x * (ray.origin.x - sph.pos.x) +
nuclear@28 96 2.0 * ray.dir.y * (ray.origin.y - sph.pos.y) +
nuclear@28 97 2.0 * ray.dir.z * (ray.origin.z - sph.pos.z);
nuclear@28 98 c = v3_dot(sph.pos, sph.pos) + v3_dot(ray.origin, ray.origin) +
nuclear@28 99 2.0 * v3_dot(v3_neg(sph.pos), ray.origin) - sph.rad * sph.rad;
nuclear@28 100
nuclear@28 101 d = b * b - 4.0 * a * c;
nuclear@28 102 if(d < 0.0) {
nuclear@28 103 return 0;
nuclear@28 104 }
nuclear@28 105
nuclear@28 106 sqrt_d = sqrt(d);
nuclear@28 107 t1 = (-b + sqrt_d) / (2.0 * a);
nuclear@28 108 t2 = (-b - sqrt_d) / (2.0 * a);
nuclear@28 109
nuclear@28 110 if(t1 < 1e-7 || t1 > 1.0) {
nuclear@28 111 t1 = t2;
nuclear@28 112 }
nuclear@28 113 if(t2 < 1e-7 || t2 > 1.0) {
nuclear@28 114 t2 = t1;
nuclear@28 115 }
nuclear@28 116 t = t1 < t2 ? t1 : t2;
nuclear@28 117
nuclear@28 118 if(t < 1e-7 || t > 1.0) {
nuclear@28 119 return 0;
nuclear@28 120 }
nuclear@28 121
nuclear@28 122 if(pos) {
nuclear@28 123 *pos = t;
nuclear@28 124 }
nuclear@28 125 return 1;
nuclear@28 126 }
nuclear@28 127
nuclear@28 128 int sphere_sphere_intersect(sphere_t sph1, sphere_t sph2, scalar_t *pos, scalar_t *rad)
nuclear@28 129 {
nuclear@28 130 return -1;
nuclear@28 131 }