6 #define SPHERE(ptr) ((Sphere*)ptr)
7 #define AABOX(ptr) ((AABox*)ptr)
8 #define BOX(ptr) ((Box*)ptr)
9 #define PLANE(ptr) ((Plane*)ptr)
11 GeomObject::GeomObject()
16 GeomObject::~GeomObject()
20 bool GeomObject::valid() const
25 void GeomObject::invalidate()
29 float GeomObject::distance(const Vec3 &v) const
31 return sqrt(distance_sq(v));
34 float GeomObject::signed_distance(const Vec3 &v) const
36 return sqrt(signed_distance_sq(v));
45 Sphere::Sphere(const Vec3 ¢, float radius)
49 this->radius = radius;
52 bool Sphere::valid() const
54 return radius >= 0.0f;
57 void Sphere::invalidate()
59 center = Vec3(0, 0, 0);
63 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
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;
72 float discr = b * b - 4.0 * a * c;
77 float sqrt_discr = sqrt(discr);
78 float t0 = (-b + sqrt_discr) / (2.0 * a);
79 float t1 = (-b - sqrt_discr) / (2.0 * a);
86 float t = t0 < t1 ? t0 : t1;
91 // fill the HitPoint structure
95 hit->pos = ray.origin + ray.dir * t;
96 hit->normal = (hit->pos - center) / radius;
101 bool Sphere::contains(const Vec3 &pt) const
103 return length_sq(pt - center) <= radius * radius;
106 float Sphere::distance_sq(const Vec3 &v) const
108 return std::max(length_sq(v - center) - radius * radius, 0.0f);
111 float Sphere::signed_distance_sq(const Vec3 &v) const
113 return length_sq(v - center) - radius * radius;
121 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
122 : min(vmin), max(vmax)
127 bool AABox::valid() const
129 return min.x <= max.x && min.y <= max.y && min.z <= max.z;
132 void AABox::invalidate()
134 min = Vec3(FLT_MAX, FLT_MAX, FLT_MAX);
135 max = Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
138 Vec3 AABox::get_corner(int idx) const
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);
147 Plane AABox::get_plane(int pidx) const
149 Vec3 c = (max - min) * 0.5f;
152 return Plane(Vec3(max.x, c.y, c.z), Vec3(1, 0, 0));
154 return Plane(Vec3(min.x, c.y, c.z), Vec3(-1, 0, 0));
156 return Plane(Vec3(c.x, max.x, c.z), Vec3(0, 1, 0));
158 return Plane(Vec3(c.x, min.x, c.z), Vec3(0, -1, 0));
160 return Plane(Vec3(c.x, c.y, max.z), Vec3(0, 0, 1));
162 return Plane(Vec3(c.x, c.y, min.z), Vec3(0, 0, -1));
170 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
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};
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;
181 if(tmin > tymax || tymin > tmax) {
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;
194 if(tmin > tzmax || tzmin > tmax) {
204 float t = tmin < 1e-4 ? tmax : tmin;
210 hit->pos = ray.origin + ray.dir * t;
212 float min_dist = FLT_MAX;
213 Vec3 offs = min + (max - min) / 2.0;
214 Vec3 local_hit = hit->pos - offs;
216 static const Vec3 axis[] = {
217 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
219 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
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) {
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]]);
235 bool AABox::contains(const Vec3 &v) const
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;
241 #define SQ(x) ((x) * (x))
242 float AABox::distance_sq(const Vec3 &v) const
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]);
253 float AABox::signed_distance_sq(const Vec3 &v) const
256 return distance_sq(v);
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);
273 Box::Box(const AABox &aabox, const Mat4 &xform)
281 void Box::invalidate()
284 xform = Mat4::identity;
287 Box::Box(const Vec3 &min, const Vec3 &max)
293 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
294 : AABox(min, max), xform(xform)
299 // XXX all this shit is completely untested
300 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
303 float ilen = length(vi);
304 float jlen = length(vj);
305 float klen = length(vk);
307 min = Vec3(-ilen, -jlen, -klen);
308 max = Vec3(ilen, jlen, klen);
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;
314 xform = Mat4(vi * si, vj * sj, vk * sk);
315 xform.translate(pos);
318 Box::Box(const Vec3 *varr, int vcount)
321 calc_bounding_aabox(this, varr, vcount);
324 Vec3 Box::get_corner(int idx) const
326 return xform * AABox::get_corner(idx);
329 bool Box::intersect(const Ray &ray, HitPoint *hit) const
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);
336 bool res = AABox::intersect(local_ray, hit);
337 if(!res || !hit) return res;
339 hit->pos = xform * hit->pos;
340 hit->normal = dir_xform * hit->normal;
341 hit->local_ray = local_ray;
346 bool Box::contains(const Vec3 &pt) const
348 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
349 return AABox::contains(inverse(xform) * pt);
352 float Box::distance_sq(const Vec3 &v) const
357 float Box::signed_distance_sq(const Vec3 &v) const
363 : normal(0.0, 1.0, 0.0)
368 Plane::Plane(const Vec3 &p, const Vec3 &norm)
372 normal = normalize(norm);
375 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
379 normal = normalize(cross(p2 - p1, p3 - p1));
382 Plane::Plane(const Vec3 &normal, float dist)
385 this->normal = normalize(normal);
386 pt = this->normal * dist;
389 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
391 float ndotdir = dot(normal, ray.dir);
392 if(fabs(ndotdir) < 1e-4) {
397 Vec3 ptdir = pt - ray.origin;
398 float t = dot(normal, ptdir) / ndotdir;
401 hit->pos = ray.origin + ray.dir * t;
402 hit->normal = normal;
408 bool Plane::contains(const Vec3 &v) const
410 return dot(v, normal) <= 0.0;
413 float Plane::distance(const Vec3 &v) const
415 return std::max(dot(v - pt, normal), 0.0f);
418 float Plane::signed_distance(const Vec3 &v) const
420 return dot(v - pt, normal);
423 float Plane::distance_sq(const Vec3 &v) const
425 float d = distance(v);
429 float Plane::signed_distance_sq(const Vec3 &v) const
431 float d = distance(v);
432 return dot(v, normal) >= 0.0f ? d * d : -(d * d);
442 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
449 Disc::Disc(const Vec3 &normal, float dist, float rad)
450 : Plane(normal, dist)
456 bool Disc::valid() const
458 return radius >= 0.0f;
461 void Disc::invalidate()
466 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
469 if(Plane::intersect(ray, &phit)) {
470 if(length_sq(phit.pos - pt) <= radius * radius) {
478 bool Disc::contains(const Vec3 &pt) const
480 Vec3 pj = proj_point_plane(pt, *this);
481 return length_sq(pj - this->pt) <= radius * radius;
484 float Disc::distance_sq(const Vec3 &v) const
489 float Disc::signed_distance_sq(const Vec3 &v) const
495 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
497 float dist = plane.signed_distance(pt);
498 return pt - plane.normal * dist;
501 // ---- bounding sphere calculations ----
503 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
512 *sph = *(Sphere*)obj;
516 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
517 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
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;
532 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
536 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
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);
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);
554 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
556 if(num <= 0) return false;
558 if(!calc_bounding_sphere(sph, objv[0])) {
562 for(int i=1; i<num; i++) {
563 if(!calc_bounding_sphere(sph, sph, objv[i])) {
570 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
572 if(num <= 0) return false;
574 sph->center = Vec3(0.0, 0.0, 0.0);
575 for(int i=0; i<num; i++) {
576 sph->center += xform * v[i];
578 sph->center /= (float)num;
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));
585 sph->radius = sqrt(rad_sq);
589 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
604 for(int i=0; i<8; i++) {
605 v[i] = BOX(obj)->get_corner(i);
607 calc_bounding_aabox(box, v, 8);
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);
626 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
630 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
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]);
641 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
643 if(num <= 0) return false;
645 if(!calc_bounding_aabox(box, objv[0])) {
649 for(int i=1; i<num; i++) {
650 if(!calc_bounding_aabox(box, box, objv[i])) {
657 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
659 if(num <= 0) return false;
661 box->min = box->max = xform * v[0];
662 for(int i=1; i<num; i++) {
663 Vec3 p = xform * v[i];
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]);
673 bool calc_bounding_box(Box *box, const GeomObject *obj)
686 box->min = BOX(obj)->min;
687 box->max = BOX(obj)->max;
688 box->xform = Mat4::identity;
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;
707 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
709 Vec3 dir = b.center - a.center;
711 float dist_sq = length_sq(dir);
712 if(dist_sq <= 1e-8) return false;
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) {
720 float dist = sqrt(dist_sq);
721 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
723 result->pt = a.center + dir * t;
724 result->normal = dir / dist;
725 result->radius = sin(acos(t)) * a.radius;
729 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
731 return false; // TODO
734 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
736 return false; // TODO
739 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
741 return false; // TODO
744 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
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]);
750 if(res->max[i] < res->min[i]) {
751 res->max[i] = res->min[i];
754 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
757 bool collision_sphere_aabox(const Sphere &s, const AABox &b)
759 return b.distance_sq(s.center) <= s.radius * s.radius;