tesselation
[ld42_outofspace] / src / geom.cc
1 #include <stdlib.h>
2 #include <float.h>
3 #include <algorithm>
4 #include "geom.h"
5
6 #define SPHERE(ptr)     ((Sphere*)ptr)
7 #define AABOX(ptr)      ((AABox*)ptr)
8 #define BOX(ptr)        ((Box*)ptr)
9 #define PLANE(ptr)      ((Plane*)ptr)
10
11 GeomObject::GeomObject()
12 {
13         type = GOBJ_UNKNOWN;
14 }
15
16 GeomObject::~GeomObject()
17 {
18 }
19
20 bool GeomObject::valid() const
21 {
22         return true;
23 }
24
25 void GeomObject::invalidate()
26 {
27 }
28
29 float GeomObject::distance(const Vec3 &v) const
30 {
31         return sqrt(distance_sq(v));
32 }
33
34 float GeomObject::signed_distance(const Vec3 &v) const
35 {
36         return sqrt(signed_distance_sq(v));
37 }
38
39 Sphere::Sphere()
40 {
41         type = GOBJ_SPHERE;
42         radius = 1.0;
43 }
44
45 Sphere::Sphere(const Vec3 &cent, float radius)
46         : center(cent)
47 {
48         type = GOBJ_SPHERE;
49         this->radius = radius;
50 }
51
52 bool Sphere::valid() const
53 {
54         return radius >= 0.0f;
55 }
56
57 void Sphere::invalidate()
58 {
59         center = Vec3(0, 0, 0);
60         radius = -1;
61 }
62
63 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
64 {
65         float a = dot(ray.dir, ray.dir);
66         float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
67                 2.0 * ray.dir.y * (ray.origin.y - center.y) +
68                 2.0 * ray.dir.z * (ray.origin.z - center.z);
69         float c = dot(ray.origin, ray.origin) + dot(center, center) -
70                 2.0 * dot(ray.origin, center) - radius * radius;
71
72         float discr = b * b - 4.0 * a * c;
73         if(discr < 1e-4) {
74                 return false;
75         }
76
77         float sqrt_discr = sqrt(discr);
78         float t0 = (-b + sqrt_discr) / (2.0 * a);
79         float t1 = (-b - sqrt_discr) / (2.0 * a);
80
81         if(t0 < 1e-4)
82                 t0 = t1;
83         if(t1 < 1e-4)
84                 t1 = t0;
85
86         float t = t0 < t1 ? t0 : t1;
87         if(t < 1e-4) {
88                 return false;
89         }
90
91         // fill the HitPoint structure
92         if(hit) {
93                 hit->obj = this;
94                 hit->dist = t;
95                 hit->pos = ray.origin + ray.dir * t;
96                 hit->normal = (hit->pos - center) / radius;
97         }
98         return true;
99 }
100
101 bool Sphere::contains(const Vec3 &pt) const
102 {
103         return length_sq(pt - center) <= radius * radius;
104 }
105
106 float Sphere::distance_sq(const Vec3 &v) const
107 {
108         return std::max(length_sq(v - center) - radius * radius, 0.0f);
109 }
110
111 float Sphere::signed_distance_sq(const Vec3 &v) const
112 {
113         return length_sq(v - center) - radius * radius;
114 }
115
116 AABox::AABox()
117 {
118         type = GOBJ_AABOX;
119 }
120
121 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
122         : min(vmin), max(vmax)
123 {
124         type = GOBJ_AABOX;
125 }
126
127 bool AABox::valid() const
128 {
129         return min.x <= max.x && min.y <= max.y && min.z <= max.z;
130 }
131
132 void AABox::invalidate()
133 {
134         min = Vec3(FLT_MAX, FLT_MAX, FLT_MAX);
135         max = Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
136 }
137
138 Vec3 AABox::get_corner(int idx) const
139 {
140         Vec3 v[] = {min, max};
141         static const int xidx[] = {0, 1, 1, 0, 0, 1, 1, 0};
142         static const int yidx[] = {0, 0, 0, 0, 1, 1, 1, 1};
143         static const int zidx[] = {0, 0, 1, 1, 0, 0, 1, 1};
144         return Vec3(v[xidx[idx]].x, v[yidx[idx]].y, v[zidx[idx]].z);
145 }
146
147 Plane AABox::get_plane(int pidx) const
148 {
149         Vec3 c = (max - min) * 0.5f;
150         switch(pidx) {
151         case AABOX_PLANE_PX:
152                 return Plane(Vec3(max.x, c.y, c.z), Vec3(1, 0, 0));
153         case AABOX_PLANE_NX:
154                 return Plane(Vec3(min.x, c.y, c.z), Vec3(-1, 0, 0));
155         case AABOX_PLANE_PY:
156                 return Plane(Vec3(c.x, max.x, c.z), Vec3(0, 1, 0));
157         case AABOX_PLANE_NY:
158                 return Plane(Vec3(c.x, min.x, c.z), Vec3(0, -1, 0));
159         case AABOX_PLANE_PZ:
160                 return Plane(Vec3(c.x, c.y, max.z), Vec3(0, 0, 1));
161         case AABOX_PLANE_NZ:
162                 return Plane(Vec3(c.x, c.y, min.z), Vec3(0, 0, -1));
163         default:
164                 break;
165         }
166         abort();
167         return Plane();
168 }
169
170 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
171 {
172         Vec3 param[2] = {min, max};
173         Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
174         int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
175
176         float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
177         float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
178         float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
179         float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
180
181         if(tmin > tymax || tymin > tmax) {
182                 return false;
183         }
184         if(tymin > tmin) {
185                 tmin = tymin;
186         }
187         if(tymax < tmax) {
188                 tmax = tymax;
189         }
190
191         float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
192         float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
193
194         if(tmin > tzmax || tzmin > tmax) {
195                 return false;
196         }
197         if(tzmin > tmin) {
198                 tmin = tzmin;
199         }
200         if(tzmax < tmax) {
201                 tmax = tzmax;
202         }
203
204         float t = tmin < 1e-4 ? tmax : tmin;
205         if(t >= 1e-4) {
206
207                 if(hit) {
208                         hit->obj = this;
209                         hit->dist = t;
210                         hit->pos = ray.origin + ray.dir * t;
211
212                         float min_dist = FLT_MAX;
213                         Vec3 offs = min + (max - min) / 2.0;
214                         Vec3 local_hit = hit->pos - offs;
215
216                         static const Vec3 axis[] = {
217                                 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
218                         };
219                         //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
220
221                         for(int i=0; i<3; i++) {
222                                 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
223                                 if(dist < min_dist) {
224                                         min_dist = dist;
225                                         hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
226                                         //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
227                                 }
228                         }
229                 }
230                 return true;
231         }
232         return false;
233 }
234
235 bool AABox::contains(const Vec3 &v) const
236 {
237         return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
238                 v.x <= max.x && v.y <= max.y && v.z <= max.z;
239 }
240
241 #define SQ(x)   ((x) * (x))
242 float AABox::distance_sq(const Vec3 &v) const
243 {
244         float dsq = 0.0f;
245
246         for(int i=0; i<3; i++) {
247                 if(v[i] < min[i]) dsq += SQ(min[i] - v[i]);
248                 if(v[i] > max[i]) dsq += SQ(v[i] - max[i]);
249         }
250         return dsq;
251 }
252
253 float AABox::signed_distance_sq(const Vec3 &v) const
254 {
255         if(!contains(v)) {
256                 return distance_sq(v);
257         }
258
259         float dsq = 0.0f;
260         for(int i=0; i<3; i++) {
261                 float dmin = v[i] - min[i];
262                 float dmax = max[i] - v[i];
263                 dsq -= dmin < dmax ? SQ(dmin) : SQ(dmax);
264         }
265         return dsq;
266 }
267
268 Box::Box()
269 {
270         type = GOBJ_BOX;
271 }
272
273 Box::Box(const AABox &aabox, const Mat4 &xform)
274         : xform(xform)
275 {
276         type = GOBJ_BOX;
277         min = aabox.min;
278         max = aabox.max;
279 }
280
281 void Box::invalidate()
282 {
283         AABox::invalidate();
284         xform = Mat4::identity;
285 }
286
287 Box::Box(const Vec3 &min, const Vec3 &max)
288         : AABox(min, max)
289 {
290         type = GOBJ_BOX;
291 }
292
293 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
294         : AABox(min, max), xform(xform)
295 {
296         type = GOBJ_BOX;
297 }
298
299 // XXX all this shit is completely untested
300 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
301 {
302         type = GOBJ_BOX;
303         float ilen = length(vi);
304         float jlen = length(vj);
305         float klen = length(vk);
306
307         min = Vec3(-ilen, -jlen, -klen);
308         max = Vec3(ilen, jlen, klen);
309
310         float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
311         float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
312         float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
313
314         xform = Mat4(vi * si, vj * sj, vk * sk);
315         xform.translate(pos);
316 }
317
318 Box::Box(const Vec3 *varr, int vcount)
319 {
320         type = GOBJ_BOX;
321         calc_bounding_aabox(this, varr, vcount);
322 }
323
324 Vec3 Box::get_corner(int idx) const
325 {
326         return xform * AABox::get_corner(idx);
327 }
328
329 bool Box::intersect(const Ray &ray, HitPoint *hit) const
330 {
331         Mat4 inv_xform = inverse(xform);
332         Mat4 dir_inv_xform = inv_xform.upper3x3();
333         Mat4 dir_xform = transpose(dir_inv_xform);
334         Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
335
336         bool res = AABox::intersect(local_ray, hit);
337         if(!res || !hit) return res;
338
339         hit->pos = xform * hit->pos;
340         hit->normal = dir_xform * hit->normal;
341         hit->local_ray = local_ray;
342         hit->ray = ray;
343         return true;
344 }
345
346 bool Box::contains(const Vec3 &pt) const
347 {
348         // XXX is it faster to extract 6 planes and do dot products? sounds marginal
349         return AABox::contains(inverse(xform) * pt);
350 }
351
352 float Box::distance_sq(const Vec3 &v) const
353 {
354         return 0.0f;    // TODO
355 }
356
357 float Box::signed_distance_sq(const Vec3 &v) const
358 {
359         return 0.0f;    // TODO
360 }
361
362 Plane::Plane()
363         : normal(0.0, 1.0, 0.0)
364 {
365         type = GOBJ_PLANE;
366 }
367
368 Plane::Plane(const Vec3 &p, const Vec3 &norm)
369         : pt(p)
370 {
371         type = GOBJ_PLANE;
372         normal = normalize(norm);
373 }
374
375 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
376         : pt(p1)
377 {
378         type = GOBJ_PLANE;
379         normal = normalize(cross(p2 - p1, p3 - p1));
380 }
381
382 Plane::Plane(const Vec3 &normal, float dist)
383 {
384         type = GOBJ_PLANE;
385         this->normal = normalize(normal);
386         pt = this->normal * dist;
387 }
388
389 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
390 {
391         float ndotdir = dot(normal, ray.dir);
392         if(fabs(ndotdir) < 1e-4) {
393                 return false;
394         }
395
396         if(hit) {
397                 Vec3 ptdir = pt - ray.origin;
398                 float t = dot(normal, ptdir) / ndotdir;
399
400                 hit->dist = t;
401                 hit->pos = ray.origin + ray.dir * t;
402                 hit->normal = normal;
403                 hit->obj = this;
404         }
405         return true;
406 }
407
408 bool Plane::contains(const Vec3 &v) const
409 {
410         return dot(v, normal) <= 0.0;
411 }
412
413 float Plane::distance(const Vec3 &v) const
414 {
415         return std::max(dot(v - pt, normal), 0.0f);
416 }
417
418 float Plane::signed_distance(const Vec3 &v) const
419 {
420         return dot(v - pt, normal);
421 }
422
423 float Plane::distance_sq(const Vec3 &v) const
424 {
425         float d = distance(v);
426         return d * d;
427 }
428
429 float Plane::signed_distance_sq(const Vec3 &v) const
430 {
431         float d = distance(v);
432         return dot(v, normal) >= 0.0f ? d * d : -(d * d);
433 }
434
435
436 Disc::Disc()
437 {
438         type = GOBJ_DISC;
439         radius = 1.0;
440 }
441
442 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
443         : Plane(pt, normal)
444 {
445         type = GOBJ_DISC;
446         radius = rad;
447 }
448
449 Disc::Disc(const Vec3 &normal, float dist, float rad)
450         : Plane(normal, dist)
451 {
452         type = GOBJ_DISC;
453         radius = rad;
454 }
455
456 bool Disc::valid() const
457 {
458         return radius >= 0.0f;
459 }
460
461 void Disc::invalidate()
462 {
463         radius = -1;
464 }
465
466 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
467 {
468         HitPoint phit;
469         if(Plane::intersect(ray, &phit)) {
470                 if(length_sq(phit.pos - pt) <= radius * radius) {
471                         *hit = phit;
472                         return true;
473                 }
474         }
475         return false;
476 }
477
478 bool Disc::contains(const Vec3 &pt) const
479 {
480         Vec3 pj = proj_point_plane(pt, *this);
481         return length_sq(pj - this->pt) <= radius * radius;
482 }
483
484 float Disc::distance_sq(const Vec3 &v) const
485 {
486         return 0.0;     // TODO
487 }
488
489 float Disc::signed_distance_sq(const Vec3 &v) const
490 {
491         return 0.0;     // TODO
492 }
493
494
495 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
496 {
497         float dist = plane.signed_distance(pt);
498         return pt - plane.normal * dist;
499 }
500
501 // ---- bounding sphere calculations ----
502
503 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
504 {
505         if(!obj->valid()) {
506                 sph->invalidate();
507                 return true;
508         }
509
510         switch(obj->type) {
511         case GOBJ_SPHERE:
512                 *sph = *(Sphere*)obj;
513                 break;
514
515         case GOBJ_AABOX:
516                 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
517                 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
518                 break;
519
520         case GOBJ_BOX:
521                 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
522                 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
523                 break;
524
525         case GOBJ_PLANE:
526         default:
527                 return false;
528         }
529         return true;
530 }
531
532 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
533 {
534         Sphere bsa, bsb;
535
536         if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
537                 return false;
538         }
539
540         float dist = length(bsa.center - bsb.center);
541         float surf_dist = dist - (bsa.radius + bsb.radius);
542         float d1 = bsa.radius + surf_dist / 2.0;
543         float d2 = bsb.radius + surf_dist / 2.0;
544         float t = d1 / (d1 + d2);
545
546         if(t < 0.0) t = 0.0;
547         if(t > 1.0) t = 1.0;
548
549         sph->center = bsa.center * t + bsb.center * (1.0 - t);
550         sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
551         return true;
552 }
553
554 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
555 {
556         if(num <= 0) return false;
557
558         if(!calc_bounding_sphere(sph, objv[0])) {
559                 return false;
560         }
561
562         for(int i=1; i<num; i++) {
563                 if(!calc_bounding_sphere(sph, sph, objv[i])) {
564                         return false;
565                 }
566         }
567         return true;
568 }
569
570 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
571 {
572         if(num <= 0) return false;
573
574         sph->center = Vec3(0.0, 0.0, 0.0);
575         for(int i=0; i<num; i++) {
576                 sph->center += xform * v[i];
577         }
578         sph->center /= (float)num;
579
580         float rad_sq = 0.0f;
581         for(int i=0; i<num; i++) {
582                 Vec3 dir = xform * v[i] - sph->center;
583                 rad_sq = std::max(rad_sq, dot(dir, dir));
584         }
585         sph->radius = sqrt(rad_sq);
586         return true;
587 }
588
589 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
590 {
591         if(!obj->valid()) {
592                 box->invalidate();
593                 return true;
594         }
595
596         switch(obj->type) {
597         case GOBJ_AABOX:
598                 *box = *(AABox*)obj;
599                 break;
600
601         case GOBJ_BOX:
602                 {
603                         Vec3 v[8];
604                         for(int i=0; i<8; i++) {
605                                 v[i] = BOX(obj)->get_corner(i);
606                         }
607                         calc_bounding_aabox(box, v, 8);
608                 }
609                 break;
610
611         case GOBJ_SPHERE:
612                 {
613                         float r = SPHERE(obj)->radius;
614                         box->min = SPHERE(obj)->center - Vec3(r, r, r);
615                         box->max = SPHERE(obj)->center + Vec3(r, r, r);
616                 }
617                 break;
618
619         case GOBJ_PLANE:
620         default:
621                 return false;
622         }
623         return true;
624 }
625
626 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
627 {
628         AABox bba, bbb;
629
630         if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
631                 return false;
632         }
633
634         for(int i=0; i<3; i++) {
635                 box->min[i] = std::min(bba.min[i], bbb.min[i]);
636                 box->max[i] = std::max(bba.max[i], bbb.max[i]);
637         }
638         return true;
639 }
640
641 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
642 {
643         if(num <= 0) return false;
644
645         if(!calc_bounding_aabox(box, objv[0])) {
646                 return false;
647         }
648
649         for(int i=1; i<num; i++) {
650                 if(!calc_bounding_aabox(box, box, objv[i])) {
651                         return false;
652                 }
653         }
654         return true;
655 }
656
657 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
658 {
659         if(num <= 0) return false;
660
661         box->min = box->max = xform * v[0];
662         for(int i=1; i<num; i++) {
663                 Vec3 p = xform * v[i];
664
665                 for(int j=0; j<3; j++) {
666                         box->min[j] = std::min(box->min[j], p[j]);
667                         box->max[j] = std::max(box->max[j], p[j]);
668                 }
669         }
670         return true;
671 }
672
673 bool calc_bounding_box(Box *box, const GeomObject *obj)
674 {
675         if(!obj->valid()) {
676                 box->invalidate();
677                 return true;
678         }
679
680         switch(obj->type) {
681         case GOBJ_BOX:
682                 *box = *(Box*)obj;
683                 break;
684
685         case GOBJ_AABOX:
686                 box->min = BOX(obj)->min;
687                 box->max = BOX(obj)->max;
688                 box->xform = Mat4::identity;
689                 break;
690
691         case GOBJ_SPHERE:
692                 {
693                         float r = SPHERE(obj)->radius;
694                         box->min = SPHERE(obj)->center - Vec3(r, r, r);
695                         box->max = SPHERE(obj)->center + Vec3(r, r, r);
696                         box->xform = Mat4::identity;
697                 }
698                 break;
699
700         case GOBJ_PLANE:
701         default:
702                 return false;
703         }
704         return true;
705 }
706
707 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
708 {
709         Vec3 dir = b.center - a.center;
710
711         float dist_sq = length_sq(dir);
712         if(dist_sq <= 1e-8) return false;
713
714         float rsum = a.radius + b.radius;
715         float rdif = fabs(a.radius - b.radius);
716         if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
717                 return false;
718         }
719
720         float dist = sqrt(dist_sq);
721         float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
722
723         result->pt = a.center + dir * t;
724         result->normal = dir / dist;
725         result->radius = sin(acos(t)) * a.radius;
726         return true;
727 }
728
729 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
730 {
731         return false;   // TODO
732 }
733
734 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
735 {
736         return false;   // TODO
737 }
738
739 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
740 {
741         return false;   // TODO
742 }
743
744 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
745 {
746         for(int i=0; i<3; i++) {
747                 res->min[i] = std::max(a.min[i], b.min[i]);
748                 res->max[i] = std::min(a.max[i], b.max[i]);
749
750                 if(res->max[i] < res->min[i]) {
751                         res->max[i] = res->min[i];
752                 }
753         }
754         return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
755 }
756
757 bool collision_sphere_aabox(const Sphere &s, const AABox &b)
758 {
759         return b.distance_sq(s.center) <= s.radius * s.radius;
760 }