0a82566b756e8acbcd8356c2739bfa5692fe0ec5
[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 AABox &aabox, const Mat4 &xform)
192         : xform(xform)
193 {
194         min = aabox.min;
195         max = aabox.max;
196 }
197
198 Box::Box(const Vec3 &min, const Vec3 &max)
199         : AABox(min, max)
200 {
201 }
202
203 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
204         : AABox(min, max), xform(xform)
205 {
206 }
207
208 // XXX all this shit is completely untested
209 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
210 {
211         float ilen = length(vi);
212         float jlen = length(vj);
213         float klen = length(vk);
214
215         min = Vec3(-ilen, -jlen, -klen);
216         max = Vec3(ilen, jlen, klen);
217
218         float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
219         float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
220         float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
221
222         xform = Mat4(vi * si, vj * sj, vk * sk);
223         xform.translate(pos);
224 }
225
226 Box::Box(const Vec3 *varr, int vcount)
227 {
228         calc_bounding_aabox(this, varr, vcount);
229 }
230
231 Vec3 Box::get_corner(int idx) const
232 {
233         static const Vec3 v[] = {
234                 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),
235                 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)
236         };
237
238         return xform * (v[idx] * Vec3(max - min));
239 }
240
241 bool Box::intersect(const Ray &ray, HitPoint *hit) const
242 {
243         Mat4 inv_xform = inverse(xform);
244         Mat4 dir_inv_xform = inv_xform.upper3x3();
245         Mat4 dir_xform = transpose(dir_inv_xform);
246         Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
247
248         bool res = AABox::intersect(local_ray, hit);
249         if(!res || !hit) return res;
250
251         hit->pos = xform * hit->pos;
252         hit->normal = dir_xform * hit->normal;
253         hit->local_ray = local_ray;
254         hit->ray = ray;
255         return true;
256 }
257
258 bool Box::contains(const Vec3 &pt) const
259 {
260         // XXX is it faster to extract 6 planes and do dot products? sounds marginal
261         return AABox::contains(inverse(xform) * pt);
262 }
263
264 float Box::distance(const Vec3 &v) const
265 {
266         return 0.0f;    // TODO
267 }
268
269 float Box::signed_distance(const Vec3 &v) const
270 {
271         return 0.0f;    // TODO
272 }
273
274 Plane::Plane()
275         : normal(0.0, 1.0, 0.0)
276 {
277 }
278
279 Plane::Plane(const Vec3 &p, const Vec3 &norm)
280         : pt(p)
281 {
282         normal = normalize(norm);
283 }
284
285 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
286         : pt(p1)
287 {
288         normal = normalize(cross(p2 - p1, p3 - p1));
289 }
290
291 Plane::Plane(const Vec3 &normal, float dist)
292 {
293         this->normal = normalize(normal);
294         pt = this->normal * dist;
295 }
296
297 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
298 {
299         float ndotdir = dot(normal, ray.dir);
300         if(fabs(ndotdir) < 1e-4) {
301                 return false;
302         }
303
304         if(hit) {
305                 Vec3 ptdir = pt - ray.origin;
306                 float t = dot(normal, ptdir) / ndotdir;
307
308                 hit->dist = t;
309                 hit->pos = ray.origin + ray.dir * t;
310                 hit->normal = normal;
311                 hit->obj = this;
312         }
313         return true;
314 }
315
316 bool Plane::contains(const Vec3 &v) const
317 {
318         return dot(v, normal) <= 0.0;
319 }
320
321 float Plane::distance(const Vec3 &v) const
322 {
323         return std::max(dot(v - pt, normal), 0.0f);
324 }
325
326 float Plane::signed_distance(const Vec3 &v) const
327 {
328         return dot(v - pt, normal);
329 }
330
331
332 Disc::Disc()
333 {
334         radius = 1.0;
335 }
336
337 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
338         : Plane(pt, normal)
339 {
340         radius = rad;
341 }
342
343 Disc::Disc(const Vec3 &normal, float dist, float rad)
344         : Plane(normal, dist)
345 {
346         radius = rad;
347 }
348
349 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
350 {
351         HitPoint phit;
352         if(Plane::intersect(ray, &phit)) {
353                 if(length_sq(phit.pos - pt) <= radius * radius) {
354                         *hit = phit;
355                         return true;
356                 }
357         }
358         return false;
359 }
360
361 bool Disc::contains(const Vec3 &pt) const
362 {
363         Vec3 pj = proj_point_plane(pt, *this);
364         return length_sq(pj - this->pt) <= radius * radius;
365 }
366
367 float Disc::distance(const Vec3 &v) const
368 {
369         return 0.0;     // TODO
370 }
371
372 float Disc::signed_distance(const Vec3 &v) const
373 {
374         return 0.0;     // TODO
375 }
376
377
378 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
379 {
380         float dist = plane.signed_distance(pt);
381         return pt - plane.normal * dist;
382 }
383
384 // ---- bounding sphere calculations ----
385
386 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
387 {
388         switch(obj->type) {
389         case GOBJ_SPHERE:
390                 *sph = *(Sphere*)obj;
391                 break;
392
393         case GOBJ_AABOX:
394                 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
395                 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
396                 break;
397
398         case GOBJ_BOX:
399                 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
400                 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
401                 break;
402
403         case GOBJ_PLANE:
404         default:
405                 return false;
406         }
407         return true;
408 }
409
410 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
411 {
412         Sphere bsa, bsb;
413
414         if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
415                 return false;
416         }
417
418         float dist = length(bsa.center - bsb.center);
419         float surf_dist = dist - (bsa.radius + bsb.radius);
420         float d1 = bsa.radius + surf_dist / 2.0;
421         float d2 = bsb.radius + surf_dist / 2.0;
422         float t = d1 / (d1 + d2);
423
424         if(t < 0.0) t = 0.0;
425         if(t > 1.0) t = 1.0;
426
427         sph->center = bsa.center * t + bsb.center * (1.0 - t);
428         sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
429         return true;
430 }
431
432 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
433 {
434         if(num <= 0) return false;
435
436         if(!calc_bounding_sphere(sph, objv[0])) {
437                 return false;
438         }
439
440         for(int i=1; i<num; i++) {
441                 if(!calc_bounding_sphere(sph, sph, objv[i])) {
442                         return false;
443                 }
444         }
445         return true;
446 }
447
448 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
449 {
450         if(num <= 0) return false;
451
452         sph->center = Vec3(0.0, 0.0, 0.0);
453         for(int i=0; i<num; i++) {
454                 sph->center += xform * v[i];
455         }
456         sph->center /= (float)num;
457
458         float rad_sq = 0.0f;
459         for(int i=0; i<num; i++) {
460                 Vec3 dir = xform * v[i] - sph->center;
461                 rad_sq = std::max(rad_sq, dot(dir, dir));
462         }
463         sph->radius = sqrt(rad_sq);
464         return true;
465 }
466
467 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
468 {
469         switch(obj->type) {
470         case GOBJ_AABOX:
471                 *box = *(AABox*)obj;
472                 break;
473
474         case GOBJ_BOX:
475                 {
476                         Vec3 v[8];
477                         for(int i=0; i<8; i++) {
478                                 v[i] = BOX(obj)->get_corner(i);
479                         }
480                         calc_bounding_aabox(box, v, 8);
481                 }
482                 break;
483
484         case GOBJ_SPHERE:
485                 {
486                         float r = SPHERE(obj)->radius;
487                         box->min = SPHERE(obj)->center - Vec3(r, r, r);
488                         box->max = SPHERE(obj)->center + Vec3(r, r, r);
489                 }
490                 break;
491
492         case GOBJ_PLANE:
493         default:
494                 return false;
495         }
496         return true;
497 }
498
499 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
500 {
501         AABox bba, bbb;
502
503         if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
504                 return false;
505         }
506
507         for(int i=0; i<3; i++) {
508                 box->min[i] = std::min(bba.min[i], bbb.min[i]);
509                 box->max[i] = std::max(bba.max[i], bbb.max[i]);
510         }
511         return true;
512 }
513
514 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
515 {
516         if(num <= 0) return false;
517
518         if(!calc_bounding_aabox(box, objv[0])) {
519                 return false;
520         }
521
522         for(int i=1; i<num; i++) {
523                 if(!calc_bounding_aabox(box, box, objv[i])) {
524                         return false;
525                 }
526         }
527         return true;
528 }
529
530 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
531 {
532         if(num <= 0) return false;
533
534         box->min = box->max = xform * v[0];
535         for(int i=1; i<num; i++) {
536                 Vec3 p = xform * v[i];
537
538                 for(int j=0; j<3; j++) {
539                         box->min[j] = std::min(box->min[j], p[j]);
540                         box->max[j] = std::max(box->max[j], p[j]);
541                 }
542         }
543         return true;
544 }
545
546 bool calc_bounding_box(Box *box, const GeomObject *obj)
547 {
548         switch(obj->type) {
549         case GOBJ_BOX:
550                 *box = *(Box*)obj;
551                 break;
552
553         case GOBJ_AABOX:
554                 box->min = BOX(obj)->min;
555                 box->max = BOX(obj)->max;
556                 box->xform = Mat4::identity;
557                 break;
558
559         case GOBJ_SPHERE:
560                 {
561                         float r = SPHERE(obj)->radius;
562                         box->min = SPHERE(obj)->center - Vec3(r, r, r);
563                         box->max = SPHERE(obj)->center + Vec3(r, r, r);
564                         box->xform = Mat4::identity;
565                 }
566                 break;
567
568         case GOBJ_PLANE:
569         default:
570                 return false;
571         }
572         return true;
573 }
574
575 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
576 {
577         Vec3 dir = b.center - a.center;
578
579         float dist_sq = length_sq(dir);
580         if(dist_sq <= 1e-8) return false;
581
582         float rsum = a.radius + b.radius;
583         float rdif = fabs(a.radius - b.radius);
584         if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
585                 return false;
586         }
587
588         float dist = sqrt(dist_sq);
589         float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
590
591         result->pt = a.center + dir * t;
592         result->normal = dir / dist;
593         result->radius = sin(acos(t)) * a.radius;
594         return true;
595 }
596
597 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
598 {
599         return false;   // TODO
600 }
601
602 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
603 {
604         return false;   // TODO
605 }
606
607 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
608 {
609         return false;   // TODO
610 }
611
612 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
613 {
614         for(int i=0; i<3; i++) {
615                 res->min[i] = std::max(a.min[i], b.min[i]);
616                 res->max[i] = std::min(a.max[i], b.max[i]);
617
618                 if(res->max[i] < res->min[i]) {
619                         res->max[i] = res->min[i];
620                 }
621         }
622         return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
623 }