cleaned up and fleshed out geom.h/geom.cc and preparing to add bounding
[laserbrain_demo] / src / geom.cc
1 #include <algorithm>
2 #include <float.h>
3 #include "geom.h"
4
5 #define SPHERE(ptr)     ((Sphere*)ptr)
6 #define AABOX(ptr)      ((AABox*)ptr)
7 #define BOX(ptr)        ((Box*)ptr)
8 #define PLANE(ptr)      ((Plane*)ptr)
9
10 GeomObject::GeomObject()
11 {
12         type = GOBJ_UNKNOWN;
13 }
14
15 GeomObject::~GeomObject()
16 {
17 }
18
19
20 Sphere::Sphere()
21 {
22         type = GOBJ_SPHERE;
23         radius = 1.0;
24 }
25
26 Sphere::Sphere(const Vec3 &cent, float radius)
27         : center(cent)
28 {
29         type = GOBJ_SPHERE;
30         this->radius = radius;
31 }
32
33 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
34 {
35         float a = dot(ray.dir, ray.dir);
36         float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
37                 2.0 * ray.dir.y * (ray.origin.y - center.y) +
38                 2.0 * ray.dir.z * (ray.origin.z - center.z);
39         float c = dot(ray.origin, ray.origin) + dot(center, center) -
40                 2.0 * dot(ray.origin, center) - radius * radius;
41
42         float discr = b * b - 4.0 * a * c;
43         if(discr < 1e-4) {
44                 return false;
45         }
46
47         float sqrt_discr = sqrt(discr);
48         float t0 = (-b + sqrt_discr) / (2.0 * a);
49         float t1 = (-b - sqrt_discr) / (2.0 * a);
50
51         if(t0 < 1e-4)
52                 t0 = t1;
53         if(t1 < 1e-4)
54                 t1 = t0;
55
56         float t = t0 < t1 ? t0 : t1;
57         if(t < 1e-4) {
58                 return false;
59         }
60
61         // fill the HitPoint structure
62         if(hit) {
63                 hit->obj = this;
64                 hit->dist = t;
65                 hit->pos = ray.origin + ray.dir * t;
66                 hit->normal = (hit->pos - center) / radius;
67         }
68         return true;
69 }
70
71 bool Sphere::contains(const Vec3 &pt) const
72 {
73         return length_sq(pt - center) <= radius * radius;
74 }
75
76 float Sphere::distance(const Vec3 &v) const
77 {
78         return std::max(length(v - center) - radius, 0.0f);
79 }
80
81 float Sphere::signed_distance(const Vec3 &v) const
82 {
83         return length(v - center) - radius;
84 }
85
86 AABox::AABox()
87 {
88         type = GOBJ_AABOX;
89 }
90
91 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
92         : min(vmin), max(vmax)
93 {
94         type = GOBJ_AABOX;
95 }
96
97 Vec3 AABox::get_corner(int idx) const
98 {
99         static const Vec3 v[] = {
100                 Vec3(-0.5, -0.5, -0.5), Vec3(0.5, -0.5, -0.5), Vec3(0.5, -0.5, 0.5), Vec3(-0.5, -0.5, 0.5),
101                 Vec3(-0.5, 0.5, -0.5), Vec3(0.5, 0.5, -0.5), Vec3(0.5, 0.5, 0.5), Vec3(-0.5, 0.5, 0.5)
102         };
103         return v[idx] * Vec3(max - min);
104 }
105
106 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
107 {
108         Vec3 param[2] = {min, max};
109         Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
110         int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
111
112         float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
113         float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
114         float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
115         float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
116
117         if(tmin > tymax || tymin > tmax) {
118                 return false;
119         }
120         if(tymin > tmin) {
121                 tmin = tymin;
122         }
123         if(tymax < tmax) {
124                 tmax = tymax;
125         }
126
127         float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
128         float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
129
130         if(tmin > tzmax || tzmin > tmax) {
131                 return false;
132         }
133         if(tzmin > tmin) {
134                 tmin = tzmin;
135         }
136         if(tzmax < tmax) {
137                 tmax = tzmax;
138         }
139
140         float t = tmin < 1e-4 ? tmax : tmin;
141         if(t >= 1e-4) {
142
143                 if(hit) {
144                         hit->obj = this;
145                         hit->dist = t;
146                         hit->pos = ray.origin + ray.dir * t;
147
148                         float min_dist = FLT_MAX;
149                         Vec3 offs = min + (max - min) / 2.0;
150                         Vec3 local_hit = hit->pos - offs;
151
152                         static const Vec3 axis[] = {
153                                 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
154                         };
155                         //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
156
157                         for(int i=0; i<3; i++) {
158                                 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
159                                 if(dist < min_dist) {
160                                         min_dist = dist;
161                                         hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
162                                         //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
163                                 }
164                         }
165                 }
166                 return true;
167         }
168         return false;
169 }
170
171 bool AABox::contains(const Vec3 &v) const
172 {
173         return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
174                 v.x <= max.x && v.y <= max.y && v.z <= max.z;
175 }
176
177 float AABox::distance(const Vec3 &v) const
178 {
179         return 0.0;     // TODO
180 }
181
182 float AABox::signed_distance(const Vec3 &v) const
183 {
184         return 0.0;     // TODO
185 }
186
187 Box::Box()
188 {
189 }
190
191 Box::Box(const Vec3 &min, const Vec3 &max)
192         : AABox(min, max)
193 {
194 }
195
196 // XXX all this shit is completely untested
197 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
198 {
199         float ilen = length(vi);
200         float jlen = length(vj);
201         float klen = length(vk);
202
203         min = Vec3(-ilen, -jlen, -klen);
204         max = Vec3(ilen, jlen, klen);
205
206         float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
207         float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
208         float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
209
210         xform = Mat4(vi * si, vj * sj, vk * sk);
211         xform.translate(pos);
212 }
213
214 Box::Box(const Vec3 *varr, int vcount)
215 {
216         calc_bounding_aabox(this, varr, vcount);
217 }
218
219 Vec3 Box::get_corner(int idx) const
220 {
221         static const Vec3 v[] = {
222                 Vec3(-0.5, -0.5, -0.5), Vec3(0.5, -0.5, -0.5), Vec3(0.5, -0.5, 0.5), Vec3(-0.5, -0.5, 0.5),
223                 Vec3(-0.5, 0.5, -0.5), Vec3(0.5, 0.5, -0.5), Vec3(0.5, 0.5, 0.5), Vec3(-0.5, 0.5, 0.5)
224         };
225
226         return xform * (v[idx] * Vec3(max - min));
227 }
228
229 bool Box::intersect(const Ray &ray, HitPoint *hit) const
230 {
231         Mat4 inv_xform = inverse(xform);
232         Mat4 dir_inv_xform = inv_xform.upper3x3();
233         Mat4 dir_xform = transpose(dir_inv_xform);
234         Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
235
236         bool res = AABox::intersect(local_ray, hit);
237         if(!res || !hit) return res;
238
239         hit->pos = xform * hit->pos;
240         hit->normal = dir_xform * hit->normal;
241         hit->local_ray = local_ray;
242         hit->ray = ray;
243         return true;
244 }
245
246 bool Box::contains(const Vec3 &pt) const
247 {
248         // XXX is it faster to extract 6 planes and do dot products? sounds marginal
249         return AABox::contains(inverse(xform) * pt);
250 }
251
252 float Box::distance(const Vec3 &v) const
253 {
254         return 0.0f;    // TODO
255 }
256
257 float Box::signed_distance(const Vec3 &v) const
258 {
259         return 0.0f;    // TODO
260 }
261
262 Plane::Plane()
263         : normal(0.0, 1.0, 0.0)
264 {
265 }
266
267 Plane::Plane(const Vec3 &p, const Vec3 &norm)
268         : pt(p)
269 {
270         normal = normalize(norm);
271 }
272
273 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
274         : pt(p1)
275 {
276         normal = normalize(cross(p2 - p1, p3 - p1));
277 }
278
279 Plane::Plane(const Vec3 &normal, float dist)
280 {
281         this->normal = normalize(normal);
282         pt = this->normal * dist;
283 }
284
285 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
286 {
287         float ndotdir = dot(normal, ray.dir);
288         if(fabs(ndotdir) < 1e-4) {
289                 return false;
290         }
291
292         if(hit) {
293                 Vec3 ptdir = pt - ray.origin;
294                 float t = dot(normal, ptdir) / ndotdir;
295
296                 hit->dist = t;
297                 hit->pos = ray.origin + ray.dir * t;
298                 hit->normal = normal;
299                 hit->obj = this;
300         }
301         return true;
302 }
303
304 bool Plane::contains(const Vec3 &v) const
305 {
306         return dot(v, normal) <= 0.0;
307 }
308
309 float Plane::distance(const Vec3 &v) const
310 {
311         return std::max(dot(v - pt, normal), 0.0f);
312 }
313
314 float Plane::signed_distance(const Vec3 &v) const
315 {
316         return dot(v - pt, normal);
317 }
318
319
320 Disc::Disc()
321 {
322         radius = 1.0;
323 }
324
325 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
326         : Plane(pt, normal)
327 {
328         radius = rad;
329 }
330
331 Disc::Disc(const Vec3 &normal, float dist, float rad)
332         : Plane(normal, dist)
333 {
334         radius = rad;
335 }
336
337 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
338 {
339         HitPoint phit;
340         if(Plane::intersect(ray, &phit)) {
341                 if(length_sq(phit.pos - pt) <= radius * radius) {
342                         *hit = phit;
343                         return true;
344                 }
345         }
346         return false;
347 }
348
349 bool Disc::contains(const Vec3 &pt) const
350 {
351         Vec3 pj = proj_point_plane(pt, *this);
352         return length_sq(pj - this->pt) <= radius * radius;
353 }
354
355 float Disc::distance(const Vec3 &v) const
356 {
357         return 0.0;     // TODO
358 }
359
360 float Disc::signed_distance(const Vec3 &v) const
361 {
362         return 0.0;     // TODO
363 }
364
365
366 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
367 {
368         float dist = plane.signed_distance(pt);
369         return pt - plane.normal * dist;
370 }
371
372 // ---- bounding sphere calculations ----
373
374 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
375 {
376         switch(obj->type) {
377         case GOBJ_SPHERE:
378                 *sph = *(Sphere*)obj;
379                 break;
380
381         case GOBJ_AABOX:
382                 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
383                 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
384                 break;
385
386         case GOBJ_BOX:
387                 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
388                 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
389                 break;
390
391         case GOBJ_PLANE:
392         default:
393                 return false;
394         }
395         return true;
396 }
397
398 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
399 {
400         Sphere bsa, bsb;
401
402         if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
403                 return false;
404         }
405
406         float dist = length(bsa.center - bsb.center);
407         float surf_dist = dist - (bsa.radius + bsb.radius);
408         float d1 = bsa.radius + surf_dist / 2.0;
409         float d2 = bsb.radius + surf_dist / 2.0;
410         float t = d1 / (d1 + d2);
411
412         if(t < 0.0) t = 0.0;
413         if(t > 1.0) t = 1.0;
414
415         sph->center = bsa.center * t + bsb.center * (1.0 - t);
416         sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
417         return true;
418 }
419
420 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
421 {
422         if(num <= 0) return false;
423
424         if(!calc_bounding_sphere(sph, objv[0])) {
425                 return false;
426         }
427
428         for(int i=1; i<num; i++) {
429                 if(!calc_bounding_sphere(sph, sph, objv[i])) {
430                         return false;
431                 }
432         }
433         return true;
434 }
435
436 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
437 {
438         if(num <= 0) return false;
439
440         sph->center = Vec3(0.0, 0.0, 0.0);
441         for(int i=0; i<num; i++) {
442                 sph->center += xform * v[i];
443         }
444         sph->center /= (float)num;
445
446         float rad_sq = 0.0f;
447         for(int i=0; i<num; i++) {
448                 Vec3 dir = xform * v[i] - sph->center;
449                 rad_sq = std::max(rad_sq, dot(dir, dir));
450         }
451         sph->radius = sqrt(rad_sq);
452         return true;
453 }
454
455 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
456 {
457         switch(obj->type) {
458         case GOBJ_AABOX:
459                 *box = *(AABox*)obj;
460                 break;
461
462         case GOBJ_BOX:
463                 {
464                         Vec3 v[8];
465                         for(int i=0; i<8; i++) {
466                                 v[i] = BOX(obj)->get_corner(i);
467                         }
468                         calc_bounding_aabox(box, v, 8);
469                 }
470                 break;
471
472         case GOBJ_SPHERE:
473                 {
474                         float r = SPHERE(obj)->radius;
475                         box->min = SPHERE(obj)->center - Vec3(r, r, r);
476                         box->max = SPHERE(obj)->center + Vec3(r, r, r);
477                 }
478                 break;
479
480         case GOBJ_PLANE:
481         default:
482                 return false;
483         }
484         return true;
485 }
486
487 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
488 {
489         AABox bba, bbb;
490
491         if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
492                 return false;
493         }
494
495         for(int i=0; i<3; i++) {
496                 box->min[i] = std::min(bba.min[i], bbb.min[i]);
497                 box->max[i] = std::max(bba.max[i], bbb.max[i]);
498         }
499         return true;
500 }
501
502 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
503 {
504         if(num <= 0) return false;
505
506         if(!calc_bounding_aabox(box, objv[0])) {
507                 return false;
508         }
509
510         for(int i=1; i<num; i++) {
511                 if(!calc_bounding_aabox(box, box, objv[i])) {
512                         return false;
513                 }
514         }
515         return true;
516 }
517
518 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
519 {
520         if(num <= 0) return false;
521
522         box->min = box->max = xform * v[0];
523         for(int i=1; i<num; i++) {
524                 Vec3 p = xform * v[i];
525
526                 for(int j=0; j<3; j++) {
527                         box->min[j] = std::min(box->min[j], p[j]);
528                         box->max[j] = std::max(box->max[j], p[j]);
529                 }
530         }
531         return true;
532 }
533
534 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
535 {
536         Vec3 dir = b.center - a.center;
537
538         float dist_sq = length_sq(dir);
539         if(dist_sq <= 1e-8) return false;
540
541         float rsum = a.radius + b.radius;
542         float rdif = fabs(a.radius - b.radius);
543         if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
544                 return false;
545         }
546
547         float dist = sqrt(dist_sq);
548         float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
549
550         result->pt = a.center + dir * t;
551         result->normal = dir / dist;
552         result->radius = sin(acos(t)) * a.radius;
553         return true;
554 }
555
556 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
557 {
558         return false;   // TODO
559 }
560
561 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
562 {
563         return false;   // TODO
564 }
565
566 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
567 {
568         return false;   // TODO
569 }
570
571 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
572 {
573         for(int i=0; i<3; i++) {
574                 res->min[i] = std::max(a.min[i], b.min[i]);
575                 res->max[i] = std::min(a.max[i], b.max[i]);
576
577                 if(res->max[i] < res->min[i]) {
578                         res->max[i] = res->min[i];
579                 }
580         }
581         return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
582 }