7 #define SPHERE(ptr) ((Sphere*)ptr)
8 #define AABOX(ptr) ((AABox*)ptr)
9 #define BOX(ptr) ((Box*)ptr)
10 #define PLANE(ptr) ((Plane*)ptr)
12 GeomObject::GeomObject()
17 GeomObject::~GeomObject()
21 bool GeomObject::valid() const
26 void GeomObject::invalidate()
30 float GeomObject::distance(const Vec3 &v) const
32 return sqrt(distance_sq(v));
35 float GeomObject::signed_distance(const Vec3 &v) const
37 return sqrt(signed_distance_sq(v));
46 Sphere::Sphere(const Vec3 ¢, float radius)
50 this->radius = radius;
53 bool Sphere::valid() const
55 return radius >= 0.0f;
58 void Sphere::invalidate()
60 center = Vec3(0, 0, 0);
64 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
66 float a = dot(ray.dir, ray.dir);
67 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
68 2.0 * ray.dir.y * (ray.origin.y - center.y) +
69 2.0 * ray.dir.z * (ray.origin.z - center.z);
70 float c = dot(ray.origin, ray.origin) + dot(center, center) -
71 2.0 * dot(ray.origin, center) - radius * radius;
73 float discr = b * b - 4.0 * a * c;
78 float sqrt_discr = sqrt(discr);
79 float t0 = (-b + sqrt_discr) / (2.0 * a);
80 float t1 = (-b - sqrt_discr) / (2.0 * a);
87 float t = t0 < t1 ? t0 : t1;
92 // fill the HitPoint structure
96 hit->pos = ray.origin + ray.dir * t;
97 hit->normal = (hit->pos - center) / radius;
102 bool Sphere::contains(const Vec3 &pt) const
104 return length_sq(pt - center) <= radius * radius;
107 float Sphere::distance_sq(const Vec3 &v) const
109 return std::max(length_sq(v - center) - radius * radius, 0.0f);
112 float Sphere::signed_distance_sq(const Vec3 &v) const
114 return length_sq(v - center) - radius * radius;
122 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
123 : min(vmin), max(vmax)
128 bool AABox::valid() const
130 return min.x <= max.x && min.y <= max.y && min.z <= max.z;
133 void AABox::invalidate()
135 min = Vec3(FLT_MAX, FLT_MAX, FLT_MAX);
136 max = Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
139 Vec3 AABox::get_corner(int idx) const
141 Vec3 v[] = {min, max};
142 static const int xidx[] = {0, 1, 1, 0, 0, 1, 1, 0};
143 static const int yidx[] = {0, 0, 0, 0, 1, 1, 1, 1};
144 static const int zidx[] = {0, 0, 1, 1, 0, 0, 1, 1};
145 return Vec3(v[xidx[idx]].x, v[yidx[idx]].y, v[zidx[idx]].z);
148 Plane AABox::get_plane(int pidx) const
152 return Plane(Vec3(max.x, 0, 0), Vec3(1, 0, 0));
154 return Plane(Vec3(min.x, 0, 0), Vec3(-1, 0, 0));
156 return Plane(Vec3(0, max.x, 0), Vec3(0, 1, 0));
158 return Plane(Vec3(0, min.x, 0), Vec3(0, -1, 0));
160 return Plane(Vec3(0, 0, max.z), Vec3(0, 0, 1));
162 return Plane(Vec3(0, 0, min.z), Vec3(0, 0, -1));
170 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
172 Vec3 param[2] = {min, max};
175 if(fpexcept_enabled) {
176 inv_dir.x = ray.dir.x == 0.0f ? 1.0f : 1.0f / ray.dir.x;
177 inv_dir.y = ray.dir.y == 0.0f ? 1.0f : 1.0f / ray.dir.y;
178 inv_dir.z = ray.dir.z == 0.0f ? 1.0f : 1.0f / ray.dir.z;
180 inv_dir = Vec3(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
183 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
185 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
187 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
188 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
189 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
190 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
192 if(tmin > tymax || tymin > tmax) {
202 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
203 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
205 if(tmin > tzmax || tzmin > tmax) {
215 float t = tmin < 1e-4 ? tmax : tmin;
221 hit->pos = ray.origin + ray.dir * t;
223 float min_dist = FLT_MAX;
224 Vec3 offs = min + (max - min) / 2.0;
225 Vec3 local_hit = hit->pos - offs;
227 static const Vec3 axis[] = {
228 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
230 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
232 for(int i=0; i<3; i++) {
233 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
234 if(dist < min_dist) {
236 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
237 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
246 bool AABox::contains(const Vec3 &v) const
248 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
249 v.x <= max.x && v.y <= max.y && v.z <= max.z;
252 #define SQ(x) ((x) * (x))
253 float AABox::distance_sq(const Vec3 &v) const
257 for(int i=0; i<3; i++) {
258 if(v[i] < min[i]) dsq += SQ(min[i] - v[i]);
259 if(v[i] > max[i]) dsq += SQ(v[i] - max[i]);
264 float AABox::signed_distance_sq(const Vec3 &v) const
267 return distance_sq(v);
271 for(int i=0; i<3; i++) {
272 float dmin = v[i] - min[i];
273 float dmax = max[i] - v[i];
274 dsq -= dmin < dmax ? SQ(dmin) : SQ(dmax);
284 Box::Box(const AABox &aabox, const Mat4 &xform)
292 void Box::invalidate()
295 xform = Mat4::identity;
298 Box::Box(const Vec3 &min, const Vec3 &max)
304 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
305 : AABox(min, max), xform(xform)
310 // XXX all this shit is completely untested
311 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
314 float ilen = length(vi);
315 float jlen = length(vj);
316 float klen = length(vk);
318 min = Vec3(-ilen, -jlen, -klen);
319 max = Vec3(ilen, jlen, klen);
321 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
322 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
323 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
325 xform = Mat4(vi * si, vj * sj, vk * sk);
326 xform.translate(pos);
329 Box::Box(const Vec3 *varr, int vcount)
332 calc_bounding_aabox(this, varr, vcount);
335 Vec3 Box::get_corner(int idx) const
337 return xform * AABox::get_corner(idx);
340 bool Box::intersect(const Ray &ray, HitPoint *hit) const
342 Mat4 inv_xform = inverse(xform);
343 Mat4 dir_inv_xform = inv_xform.upper3x3();
344 Mat4 dir_xform = transpose(dir_inv_xform);
345 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
347 bool res = AABox::intersect(local_ray, hit);
348 if(!res || !hit) return res;
350 hit->pos = xform * hit->pos;
351 hit->normal = dir_xform * hit->normal;
352 hit->local_ray = local_ray;
357 bool Box::contains(const Vec3 &pt) const
359 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
360 return AABox::contains(inverse(xform) * pt);
363 float Box::distance_sq(const Vec3 &v) const
368 float Box::signed_distance_sq(const Vec3 &v) const
374 : normal(0.0, 1.0, 0.0)
379 Plane::Plane(const Vec3 &p, const Vec3 &norm)
383 normal = normalize(norm);
386 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
390 normal = normalize(cross(p2 - p1, p3 - p1));
393 Plane::Plane(const Vec3 &normal, float dist)
396 this->normal = normalize(normal);
397 pt = this->normal * dist;
400 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
402 float ndotdir = dot(normal, ray.dir);
403 if(fabs(ndotdir) < 1e-4) {
408 Vec3 ptdir = pt - ray.origin;
409 float t = dot(normal, ptdir) / ndotdir;
412 hit->pos = ray.origin + ray.dir * t;
413 hit->normal = normal;
419 bool Plane::contains(const Vec3 &v) const
421 return dot(v, normal) <= 0.0;
424 float Plane::distance(const Vec3 &v) const
426 return std::max(dot(v - pt, normal), 0.0f);
429 float Plane::signed_distance(const Vec3 &v) const
431 return dot(v - pt, normal);
434 float Plane::distance_sq(const Vec3 &v) const
436 float d = distance(v);
440 float Plane::signed_distance_sq(const Vec3 &v) const
442 float d = distance(v);
443 return dot(v, normal) >= 0.0f ? d * d : -(d * d);
453 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
460 Disc::Disc(const Vec3 &normal, float dist, float rad)
461 : Plane(normal, dist)
467 bool Disc::valid() const
469 return radius >= 0.0f;
472 void Disc::invalidate()
477 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
480 if(Plane::intersect(ray, &phit)) {
481 if(length_sq(phit.pos - pt) <= radius * radius) {
489 bool Disc::contains(const Vec3 &pt) const
491 Vec3 pj = proj_point_plane(pt, *this);
492 return length_sq(pj - this->pt) <= radius * radius;
495 float Disc::distance_sq(const Vec3 &v) const
500 float Disc::signed_distance_sq(const Vec3 &v) const
506 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
508 float dist = plane.signed_distance(pt);
509 return pt - plane.normal * dist;
512 // ---- bounding sphere calculations ----
514 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
523 *sph = *(Sphere*)obj;
527 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
528 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
532 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
533 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
543 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
547 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
551 float dist = length(bsa.center - bsb.center);
552 float surf_dist = dist - (bsa.radius + bsb.radius);
553 float d1 = bsa.radius + surf_dist / 2.0;
554 float d2 = bsb.radius + surf_dist / 2.0;
555 float t = d1 / (d1 + d2);
560 sph->center = bsa.center * t + bsb.center * (1.0 - t);
561 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
565 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
567 if(num <= 0) return false;
569 if(!calc_bounding_sphere(sph, objv[0])) {
573 for(int i=1; i<num; i++) {
574 if(!calc_bounding_sphere(sph, sph, objv[i])) {
581 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
583 if(num <= 0) return false;
585 sph->center = Vec3(0.0, 0.0, 0.0);
586 for(int i=0; i<num; i++) {
587 sph->center += xform * v[i];
589 sph->center /= (float)num;
592 for(int i=0; i<num; i++) {
593 Vec3 dir = xform * v[i] - sph->center;
594 rad_sq = std::max(rad_sq, dot(dir, dir));
596 sph->radius = sqrt(rad_sq);
600 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
615 for(int i=0; i<8; i++) {
616 v[i] = BOX(obj)->get_corner(i);
618 calc_bounding_aabox(box, v, 8);
624 float r = SPHERE(obj)->radius;
625 box->min = SPHERE(obj)->center - Vec3(r, r, r);
626 box->max = SPHERE(obj)->center + Vec3(r, r, r);
637 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
641 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
645 for(int i=0; i<3; i++) {
646 box->min[i] = std::min(bba.min[i], bbb.min[i]);
647 box->max[i] = std::max(bba.max[i], bbb.max[i]);
652 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
654 if(num <= 0) return false;
656 if(!calc_bounding_aabox(box, objv[0])) {
660 for(int i=1; i<num; i++) {
661 if(!calc_bounding_aabox(box, box, objv[i])) {
668 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
670 if(num <= 0) return false;
672 box->min = box->max = xform * v[0];
673 for(int i=1; i<num; i++) {
674 Vec3 p = xform * v[i];
676 for(int j=0; j<3; j++) {
677 box->min[j] = std::min(box->min[j], p[j]);
678 box->max[j] = std::max(box->max[j], p[j]);
684 bool calc_bounding_box(Box *box, const GeomObject *obj)
697 box->min = BOX(obj)->min;
698 box->max = BOX(obj)->max;
699 box->xform = Mat4::identity;
704 float r = SPHERE(obj)->radius;
705 box->min = SPHERE(obj)->center - Vec3(r, r, r);
706 box->max = SPHERE(obj)->center + Vec3(r, r, r);
707 box->xform = Mat4::identity;
718 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
720 Vec3 dir = b.center - a.center;
722 float dist_sq = length_sq(dir);
723 if(dist_sq <= 1e-8) return false;
725 float rsum = a.radius + b.radius;
726 float rdif = fabs(a.radius - b.radius);
727 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
731 float dist = sqrt(dist_sq);
732 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
734 result->pt = a.center + dir * t;
735 result->normal = dir / dist;
736 result->radius = sin(acos(t)) * a.radius;
740 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
742 return false; // TODO
745 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
747 return false; // TODO
750 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
752 return false; // TODO
755 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
757 for(int i=0; i<3; i++) {
758 res->min[i] = std::max(a.min[i], b.min[i]);
759 res->max[i] = std::min(a.max[i], b.max[i]);
761 if(res->max[i] < res->min[i]) {
762 res->max[i] = res->min[i];
765 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
768 bool collision_sphere_aabox(const Sphere &s, const AABox &b)
770 return b.distance_sq(s.center) <= s.radius * s.radius;