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
150 Vec3 c = (max - min) * 0.5f;
153 return Plane(Vec3(max.x, c.y, c.z), Vec3(1, 0, 0));
155 return Plane(Vec3(min.x, c.y, c.z), Vec3(-1, 0, 0));
157 return Plane(Vec3(c.x, max.x, c.z), Vec3(0, 1, 0));
159 return Plane(Vec3(c.x, min.x, c.z), Vec3(0, -1, 0));
161 return Plane(Vec3(c.x, c.y, max.z), Vec3(0, 0, 1));
163 return Plane(Vec3(c.x, c.y, min.z), Vec3(0, 0, -1));
171 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
173 Vec3 param[2] = {min, max};
176 if(fpexcept_enabled) {
177 inv_dir.x = ray.dir.x == 0.0f ? 1.0f : 1.0f / ray.dir.x;
178 inv_dir.y = ray.dir.y == 0.0f ? 1.0f : 1.0f / ray.dir.y;
179 inv_dir.z = ray.dir.z == 0.0f ? 1.0f : 1.0f / ray.dir.z;
181 inv_dir = Vec3(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
184 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
186 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
188 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
189 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
190 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
191 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
193 if(tmin > tymax || tymin > tmax) {
203 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
204 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
206 if(tmin > tzmax || tzmin > tmax) {
216 float t = tmin < 1e-4 ? tmax : tmin;
222 hit->pos = ray.origin + ray.dir * t;
224 float min_dist = FLT_MAX;
225 Vec3 offs = min + (max - min) / 2.0;
226 Vec3 local_hit = hit->pos - offs;
228 static const Vec3 axis[] = {
229 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
231 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
233 for(int i=0; i<3; i++) {
234 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
235 if(dist < min_dist) {
237 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
238 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
247 bool AABox::contains(const Vec3 &v) const
249 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
250 v.x <= max.x && v.y <= max.y && v.z <= max.z;
253 #define SQ(x) ((x) * (x))
254 float AABox::distance_sq(const Vec3 &v) const
258 for(int i=0; i<3; i++) {
259 if(v[i] < min[i]) dsq += SQ(min[i] - v[i]);
260 if(v[i] > max[i]) dsq += SQ(v[i] - max[i]);
265 float AABox::signed_distance_sq(const Vec3 &v) const
268 return distance_sq(v);
272 for(int i=0; i<3; i++) {
273 float dmin = v[i] - min[i];
274 float dmax = max[i] - v[i];
275 dsq -= dmin < dmax ? SQ(dmin) : SQ(dmax);
285 Box::Box(const AABox &aabox, const Mat4 &xform)
293 void Box::invalidate()
296 xform = Mat4::identity;
299 Box::Box(const Vec3 &min, const Vec3 &max)
305 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
306 : AABox(min, max), xform(xform)
311 // XXX all this shit is completely untested
312 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
315 float ilen = length(vi);
316 float jlen = length(vj);
317 float klen = length(vk);
319 min = Vec3(-ilen, -jlen, -klen);
320 max = Vec3(ilen, jlen, klen);
322 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
323 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
324 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
326 xform = Mat4(vi * si, vj * sj, vk * sk);
327 xform.translate(pos);
330 Box::Box(const Vec3 *varr, int vcount)
333 calc_bounding_aabox(this, varr, vcount);
336 Vec3 Box::get_corner(int idx) const
338 return xform * AABox::get_corner(idx);
341 bool Box::intersect(const Ray &ray, HitPoint *hit) const
343 Mat4 inv_xform = inverse(xform);
344 Mat4 dir_inv_xform = inv_xform.upper3x3();
345 Mat4 dir_xform = transpose(dir_inv_xform);
346 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
348 bool res = AABox::intersect(local_ray, hit);
349 if(!res || !hit) return res;
351 hit->pos = xform * hit->pos;
352 hit->normal = dir_xform * hit->normal;
353 hit->local_ray = local_ray;
358 bool Box::contains(const Vec3 &pt) const
360 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
361 return AABox::contains(inverse(xform) * pt);
364 float Box::distance_sq(const Vec3 &v) const
369 float Box::signed_distance_sq(const Vec3 &v) const
375 : normal(0.0, 1.0, 0.0)
380 Plane::Plane(const Vec3 &p, const Vec3 &norm)
384 normal = normalize(norm);
387 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
391 normal = normalize(cross(p2 - p1, p3 - p1));
394 Plane::Plane(const Vec3 &normal, float dist)
397 this->normal = normalize(normal);
398 pt = this->normal * dist;
401 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
403 float ndotdir = dot(normal, ray.dir);
404 if(fabs(ndotdir) < 1e-4) {
409 Vec3 ptdir = pt - ray.origin;
410 float t = dot(normal, ptdir) / ndotdir;
413 hit->pos = ray.origin + ray.dir * t;
414 hit->normal = normal;
420 bool Plane::contains(const Vec3 &v) const
422 return dot(v, normal) <= 0.0;
425 float Plane::distance(const Vec3 &v) const
427 return std::max(dot(v - pt, normal), 0.0f);
430 float Plane::signed_distance(const Vec3 &v) const
432 return dot(v - pt, normal);
435 float Plane::distance_sq(const Vec3 &v) const
437 float d = distance(v);
441 float Plane::signed_distance_sq(const Vec3 &v) const
443 float d = distance(v);
444 return dot(v, normal) >= 0.0f ? d * d : -(d * d);
454 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
461 Disc::Disc(const Vec3 &normal, float dist, float rad)
462 : Plane(normal, dist)
468 bool Disc::valid() const
470 return radius >= 0.0f;
473 void Disc::invalidate()
478 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
481 if(Plane::intersect(ray, &phit)) {
482 if(length_sq(phit.pos - pt) <= radius * radius) {
490 bool Disc::contains(const Vec3 &pt) const
492 Vec3 pj = proj_point_plane(pt, *this);
493 return length_sq(pj - this->pt) <= radius * radius;
496 float Disc::distance_sq(const Vec3 &v) const
501 float Disc::signed_distance_sq(const Vec3 &v) const
507 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
509 float dist = plane.signed_distance(pt);
510 return pt - plane.normal * dist;
513 // ---- bounding sphere calculations ----
515 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
524 *sph = *(Sphere*)obj;
528 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
529 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
533 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
534 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
544 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
548 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
552 float dist = length(bsa.center - bsb.center);
553 float surf_dist = dist - (bsa.radius + bsb.radius);
554 float d1 = bsa.radius + surf_dist / 2.0;
555 float d2 = bsb.radius + surf_dist / 2.0;
556 float t = d1 / (d1 + d2);
561 sph->center = bsa.center * t + bsb.center * (1.0 - t);
562 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
566 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
568 if(num <= 0) return false;
570 if(!calc_bounding_sphere(sph, objv[0])) {
574 for(int i=1; i<num; i++) {
575 if(!calc_bounding_sphere(sph, sph, objv[i])) {
582 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
584 if(num <= 0) return false;
586 sph->center = Vec3(0.0, 0.0, 0.0);
587 for(int i=0; i<num; i++) {
588 sph->center += xform * v[i];
590 sph->center /= (float)num;
593 for(int i=0; i<num; i++) {
594 Vec3 dir = xform * v[i] - sph->center;
595 rad_sq = std::max(rad_sq, dot(dir, dir));
597 sph->radius = sqrt(rad_sq);
601 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
616 for(int i=0; i<8; i++) {
617 v[i] = BOX(obj)->get_corner(i);
619 calc_bounding_aabox(box, v, 8);
625 float r = SPHERE(obj)->radius;
626 box->min = SPHERE(obj)->center - Vec3(r, r, r);
627 box->max = SPHERE(obj)->center + Vec3(r, r, r);
638 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
642 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
646 for(int i=0; i<3; i++) {
647 box->min[i] = std::min(bba.min[i], bbb.min[i]);
648 box->max[i] = std::max(bba.max[i], bbb.max[i]);
653 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
655 if(num <= 0) return false;
657 if(!calc_bounding_aabox(box, objv[0])) {
661 for(int i=1; i<num; i++) {
662 if(!calc_bounding_aabox(box, box, objv[i])) {
669 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
671 if(num <= 0) return false;
673 box->min = box->max = xform * v[0];
674 for(int i=1; i<num; i++) {
675 Vec3 p = xform * v[i];
677 for(int j=0; j<3; j++) {
678 box->min[j] = std::min(box->min[j], p[j]);
679 box->max[j] = std::max(box->max[j], p[j]);
685 bool calc_bounding_box(Box *box, const GeomObject *obj)
698 box->min = BOX(obj)->min;
699 box->max = BOX(obj)->max;
700 box->xform = Mat4::identity;
705 float r = SPHERE(obj)->radius;
706 box->min = SPHERE(obj)->center - Vec3(r, r, r);
707 box->max = SPHERE(obj)->center + Vec3(r, r, r);
708 box->xform = Mat4::identity;
719 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
721 Vec3 dir = b.center - a.center;
723 float dist_sq = length_sq(dir);
724 if(dist_sq <= 1e-8) return false;
726 float rsum = a.radius + b.radius;
727 float rdif = fabs(a.radius - b.radius);
728 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
732 float dist = sqrt(dist_sq);
733 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
735 result->pt = a.center + dir * t;
736 result->normal = dir / dist;
737 result->radius = sin(acos(t)) * a.radius;
741 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
743 return false; // TODO
746 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
748 return false; // TODO
751 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
753 return false; // TODO
756 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
758 for(int i=0; i<3; i++) {
759 res->min[i] = std::max(a.min[i], b.min[i]);
760 res->max[i] = std::min(a.max[i], b.max[i]);
762 if(res->max[i] < res->min[i]) {
763 res->max[i] = res->min[i];
766 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;
769 bool collision_sphere_aabox(const Sphere &s, const AABox &b)
771 return b.distance_sq(s.center) <= s.radius * s.radius;