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 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
149 Vec3 param[2] = {min, max};
152 if(fpexcept_enabled) {
153 inv_dir.x = ray.dir.x == 0.0f ? 1.0f : 1.0f / ray.dir.x;
154 inv_dir.y = ray.dir.y == 0.0f ? 1.0f : 1.0f / ray.dir.y;
155 inv_dir.z = ray.dir.z == 0.0f ? 1.0f : 1.0f / ray.dir.z;
157 inv_dir = Vec3(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
160 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
162 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
164 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
165 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
166 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
167 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
169 if(tmin > tymax || tymin > tmax) {
179 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
180 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
182 if(tmin > tzmax || tzmin > tmax) {
192 float t = tmin < 1e-4 ? tmax : tmin;
198 hit->pos = ray.origin + ray.dir * t;
200 float min_dist = FLT_MAX;
201 Vec3 offs = min + (max - min) / 2.0;
202 Vec3 local_hit = hit->pos - offs;
204 static const Vec3 axis[] = {
205 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
207 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
209 for(int i=0; i<3; i++) {
210 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
211 if(dist < min_dist) {
213 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
214 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
223 bool AABox::contains(const Vec3 &v) const
225 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
226 v.x <= max.x && v.y <= max.y && v.z <= max.z;
229 #define SQ(x) ((x) * (x))
230 float AABox::distance_sq(const Vec3 &v) const
234 for(int i=0; i<3; i++) {
235 if(v[i] < min[i]) dsq += SQ(min[i] - v[i]);
236 if(v[i] > max[i]) dsq += SQ(v[i] - max[i]);
241 float AABox::signed_distance_sq(const Vec3 &v) const
244 return distance_sq(v);
248 for(int i=0; i<3; i++) {
249 float dmin = v[i] - min[i];
250 float dmax = max[i] - v[i];
251 dsq -= dmin < dmax ? SQ(dmin) : SQ(dmax);
261 Box::Box(const AABox &aabox, const Mat4 &xform)
269 void Box::invalidate()
272 xform = Mat4::identity;
275 Box::Box(const Vec3 &min, const Vec3 &max)
281 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
282 : AABox(min, max), xform(xform)
287 // XXX all this shit is completely untested
288 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
291 float ilen = length(vi);
292 float jlen = length(vj);
293 float klen = length(vk);
295 min = Vec3(-ilen, -jlen, -klen);
296 max = Vec3(ilen, jlen, klen);
298 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
299 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
300 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
302 xform = Mat4(vi * si, vj * sj, vk * sk);
303 xform.translate(pos);
306 Box::Box(const Vec3 *varr, int vcount)
309 calc_bounding_aabox(this, varr, vcount);
312 Vec3 Box::get_corner(int idx) const
314 return xform * AABox::get_corner(idx);
317 bool Box::intersect(const Ray &ray, HitPoint *hit) const
319 Mat4 inv_xform = inverse(xform);
320 Mat4 dir_inv_xform = inv_xform.upper3x3();
321 Mat4 dir_xform = transpose(dir_inv_xform);
322 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
324 bool res = AABox::intersect(local_ray, hit);
325 if(!res || !hit) return res;
327 hit->pos = xform * hit->pos;
328 hit->normal = dir_xform * hit->normal;
329 hit->local_ray = local_ray;
334 bool Box::contains(const Vec3 &pt) const
336 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
337 return AABox::contains(inverse(xform) * pt);
340 float Box::distance_sq(const Vec3 &v) const
345 float Box::signed_distance_sq(const Vec3 &v) const
351 : normal(0.0, 1.0, 0.0)
356 Plane::Plane(const Vec3 &p, const Vec3 &norm)
360 normal = normalize(norm);
363 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
367 normal = normalize(cross(p2 - p1, p3 - p1));
370 Plane::Plane(const Vec3 &normal, float dist)
373 this->normal = normalize(normal);
374 pt = this->normal * dist;
377 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
379 float ndotdir = dot(normal, ray.dir);
380 if(fabs(ndotdir) < 1e-4) {
385 Vec3 ptdir = pt - ray.origin;
386 float t = dot(normal, ptdir) / ndotdir;
389 hit->pos = ray.origin + ray.dir * t;
390 hit->normal = normal;
396 bool Plane::contains(const Vec3 &v) const
398 return dot(v, normal) <= 0.0;
401 float Plane::distance(const Vec3 &v) const
403 return std::max(dot(v - pt, normal), 0.0f);
406 float Plane::signed_distance(const Vec3 &v) const
408 return dot(v - pt, normal);
411 float Plane::distance_sq(const Vec3 &v) const
413 float d = distance(v);
417 float Plane::signed_distance_sq(const Vec3 &v) const
419 float d = distance(v);
420 return dot(v, normal) >= 0.0f ? d * d : -(d * d);
430 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
437 Disc::Disc(const Vec3 &normal, float dist, float rad)
438 : Plane(normal, dist)
444 bool Disc::valid() const
446 return radius >= 0.0f;
449 void Disc::invalidate()
454 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
457 if(Plane::intersect(ray, &phit)) {
458 if(length_sq(phit.pos - pt) <= radius * radius) {
466 bool Disc::contains(const Vec3 &pt) const
468 Vec3 pj = proj_point_plane(pt, *this);
469 return length_sq(pj - this->pt) <= radius * radius;
472 float Disc::distance_sq(const Vec3 &v) const
477 float Disc::signed_distance_sq(const Vec3 &v) const
483 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
485 float dist = plane.signed_distance(pt);
486 return pt - plane.normal * dist;
489 // ---- bounding sphere calculations ----
491 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
500 *sph = *(Sphere*)obj;
504 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
505 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
509 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
510 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
520 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
524 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
528 float dist = length(bsa.center - bsb.center);
529 float surf_dist = dist - (bsa.radius + bsb.radius);
530 float d1 = bsa.radius + surf_dist / 2.0;
531 float d2 = bsb.radius + surf_dist / 2.0;
532 float t = d1 / (d1 + d2);
537 sph->center = bsa.center * t + bsb.center * (1.0 - t);
538 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
542 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
544 if(num <= 0) return false;
546 if(!calc_bounding_sphere(sph, objv[0])) {
550 for(int i=1; i<num; i++) {
551 if(!calc_bounding_sphere(sph, sph, objv[i])) {
558 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
560 if(num <= 0) return false;
562 sph->center = Vec3(0.0, 0.0, 0.0);
563 for(int i=0; i<num; i++) {
564 sph->center += xform * v[i];
566 sph->center /= (float)num;
569 for(int i=0; i<num; i++) {
570 Vec3 dir = xform * v[i] - sph->center;
571 rad_sq = std::max(rad_sq, dot(dir, dir));
573 sph->radius = sqrt(rad_sq);
577 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
592 for(int i=0; i<8; i++) {
593 v[i] = BOX(obj)->get_corner(i);
595 calc_bounding_aabox(box, v, 8);
601 float r = SPHERE(obj)->radius;
602 box->min = SPHERE(obj)->center - Vec3(r, r, r);
603 box->max = SPHERE(obj)->center + Vec3(r, r, r);
614 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
618 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
622 for(int i=0; i<3; i++) {
623 box->min[i] = std::min(bba.min[i], bbb.min[i]);
624 box->max[i] = std::max(bba.max[i], bbb.max[i]);
629 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
631 if(num <= 0) return false;
633 if(!calc_bounding_aabox(box, objv[0])) {
637 for(int i=1; i<num; i++) {
638 if(!calc_bounding_aabox(box, box, objv[i])) {
645 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
647 if(num <= 0) return false;
649 box->min = box->max = xform * v[0];
650 for(int i=1; i<num; i++) {
651 Vec3 p = xform * v[i];
653 for(int j=0; j<3; j++) {
654 box->min[j] = std::min(box->min[j], p[j]);
655 box->max[j] = std::max(box->max[j], p[j]);
661 bool calc_bounding_box(Box *box, const GeomObject *obj)
674 box->min = BOX(obj)->min;
675 box->max = BOX(obj)->max;
676 box->xform = Mat4::identity;
681 float r = SPHERE(obj)->radius;
682 box->min = SPHERE(obj)->center - Vec3(r, r, r);
683 box->max = SPHERE(obj)->center + Vec3(r, r, r);
684 box->xform = Mat4::identity;
695 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
697 Vec3 dir = b.center - a.center;
699 float dist_sq = length_sq(dir);
700 if(dist_sq <= 1e-8) return false;
702 float rsum = a.radius + b.radius;
703 float rdif = fabs(a.radius - b.radius);
704 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
708 float dist = sqrt(dist_sq);
709 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
711 result->pt = a.center + dir * t;
712 result->normal = dir / dist;
713 result->radius = sin(acos(t)) * a.radius;
717 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
719 return false; // TODO
722 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
724 return false; // TODO
727 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
729 return false; // TODO
732 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
734 for(int i=0; i<3; i++) {
735 res->min[i] = std::max(a.min[i], b.min[i]);
736 res->max[i] = std::min(a.max[i], b.max[i]);
738 if(res->max[i] < res->min[i]) {
739 res->max[i] = res->min[i];
742 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
745 bool collision_sphere_aabox(const Sphere &s, const AABox &b)
747 return b.distance_sq(s.center) <= s.radius * s.radius;