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()
36 Sphere::Sphere(const Vec3 ¢, float radius)
40 this->radius = radius;
43 bool Sphere::valid() const
45 return radius >= 0.0f;
48 void Sphere::invalidate()
50 center = Vec3(0, 0, 0);
54 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
56 float a = dot(ray.dir, ray.dir);
57 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
58 2.0 * ray.dir.y * (ray.origin.y - center.y) +
59 2.0 * ray.dir.z * (ray.origin.z - center.z);
60 float c = dot(ray.origin, ray.origin) + dot(center, center) -
61 2.0 * dot(ray.origin, center) - radius * radius;
63 float discr = b * b - 4.0 * a * c;
68 float sqrt_discr = sqrt(discr);
69 float t0 = (-b + sqrt_discr) / (2.0 * a);
70 float t1 = (-b - sqrt_discr) / (2.0 * a);
77 float t = t0 < t1 ? t0 : t1;
82 // fill the HitPoint structure
86 hit->pos = ray.origin + ray.dir * t;
87 hit->normal = (hit->pos - center) / radius;
92 bool Sphere::contains(const Vec3 &pt) const
94 return length_sq(pt - center) <= radius * radius;
97 float Sphere::distance(const Vec3 &v) const
99 return std::max(length(v - center) - radius, 0.0f);
102 float Sphere::signed_distance(const Vec3 &v) const
104 return length(v - center) - radius;
112 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
113 : min(vmin), max(vmax)
118 bool AABox::valid() const
120 return min.x <= max.x && min.y <= max.y && min.z <= max.z;
123 void AABox::invalidate()
125 min = Vec3(FLT_MAX, FLT_MAX, FLT_MAX);
126 max = Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
129 Vec3 AABox::get_corner(int idx) const
131 Vec3 v[] = {min, max};
132 static const int xidx[] = {0, 1, 1, 0, 0, 1, 1, 0};
133 static const int yidx[] = {0, 0, 0, 0, 1, 1, 1, 1};
134 static const int zidx[] = {0, 0, 1, 1, 0, 0, 1, 1};
135 return Vec3(v[xidx[idx]].x, v[yidx[idx]].y, v[zidx[idx]].z);
138 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
140 Vec3 param[2] = {min, max};
143 if(fpexcept_enabled) {
144 inv_dir.x = ray.dir.x == 0.0f ? 1.0f : 1.0f / ray.dir.x;
145 inv_dir.y = ray.dir.y == 0.0f ? 1.0f : 1.0f / ray.dir.y;
146 inv_dir.z = ray.dir.z == 0.0f ? 1.0f : 1.0f / ray.dir.z;
148 inv_dir = Vec3(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
151 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
153 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
155 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
156 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
157 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
158 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
160 if(tmin > tymax || tymin > tmax) {
170 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
171 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
173 if(tmin > tzmax || tzmin > tmax) {
183 float t = tmin < 1e-4 ? tmax : tmin;
189 hit->pos = ray.origin + ray.dir * t;
191 float min_dist = FLT_MAX;
192 Vec3 offs = min + (max - min) / 2.0;
193 Vec3 local_hit = hit->pos - offs;
195 static const Vec3 axis[] = {
196 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
198 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
200 for(int i=0; i<3; i++) {
201 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
202 if(dist < min_dist) {
204 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
205 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
214 bool AABox::contains(const Vec3 &v) const
216 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
217 v.x <= max.x && v.y <= max.y && v.z <= max.z;
220 float AABox::distance(const Vec3 &v) const
225 float AABox::signed_distance(const Vec3 &v) const
235 Box::Box(const AABox &aabox, const Mat4 &xform)
243 void Box::invalidate()
246 xform = Mat4::identity;
249 Box::Box(const Vec3 &min, const Vec3 &max)
255 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
256 : AABox(min, max), xform(xform)
261 // XXX all this shit is completely untested
262 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
265 float ilen = length(vi);
266 float jlen = length(vj);
267 float klen = length(vk);
269 min = Vec3(-ilen, -jlen, -klen);
270 max = Vec3(ilen, jlen, klen);
272 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
273 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
274 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
276 xform = Mat4(vi * si, vj * sj, vk * sk);
277 xform.translate(pos);
280 Box::Box(const Vec3 *varr, int vcount)
283 calc_bounding_aabox(this, varr, vcount);
286 Vec3 Box::get_corner(int idx) const
288 return xform * AABox::get_corner(idx);
291 bool Box::intersect(const Ray &ray, HitPoint *hit) const
293 Mat4 inv_xform = inverse(xform);
294 Mat4 dir_inv_xform = inv_xform.upper3x3();
295 Mat4 dir_xform = transpose(dir_inv_xform);
296 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
298 bool res = AABox::intersect(local_ray, hit);
299 if(!res || !hit) return res;
301 hit->pos = xform * hit->pos;
302 hit->normal = dir_xform * hit->normal;
303 hit->local_ray = local_ray;
308 bool Box::contains(const Vec3 &pt) const
310 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
311 return AABox::contains(inverse(xform) * pt);
314 float Box::distance(const Vec3 &v) const
319 float Box::signed_distance(const Vec3 &v) const
325 : normal(0.0, 1.0, 0.0)
330 Plane::Plane(const Vec3 &p, const Vec3 &norm)
334 normal = normalize(norm);
337 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
341 normal = normalize(cross(p2 - p1, p3 - p1));
344 Plane::Plane(const Vec3 &normal, float dist)
347 this->normal = normalize(normal);
348 pt = this->normal * dist;
351 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
353 float ndotdir = dot(normal, ray.dir);
354 if(fabs(ndotdir) < 1e-4) {
359 Vec3 ptdir = pt - ray.origin;
360 float t = dot(normal, ptdir) / ndotdir;
363 hit->pos = ray.origin + ray.dir * t;
364 hit->normal = normal;
370 bool Plane::contains(const Vec3 &v) const
372 return dot(v, normal) <= 0.0;
375 float Plane::distance(const Vec3 &v) const
377 return std::max(dot(v - pt, normal), 0.0f);
380 float Plane::signed_distance(const Vec3 &v) const
382 return dot(v - pt, normal);
392 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
399 Disc::Disc(const Vec3 &normal, float dist, float rad)
400 : Plane(normal, dist)
406 bool Disc::valid() const
408 return radius >= 0.0f;
411 void Disc::invalidate()
416 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
419 if(Plane::intersect(ray, &phit)) {
420 if(length_sq(phit.pos - pt) <= radius * radius) {
428 bool Disc::contains(const Vec3 &pt) const
430 Vec3 pj = proj_point_plane(pt, *this);
431 return length_sq(pj - this->pt) <= radius * radius;
434 float Disc::distance(const Vec3 &v) const
439 float Disc::signed_distance(const Vec3 &v) const
445 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
447 float dist = plane.signed_distance(pt);
448 return pt - plane.normal * dist;
451 // ---- bounding sphere calculations ----
453 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
462 *sph = *(Sphere*)obj;
466 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
467 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
471 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
472 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
482 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
486 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
490 float dist = length(bsa.center - bsb.center);
491 float surf_dist = dist - (bsa.radius + bsb.radius);
492 float d1 = bsa.radius + surf_dist / 2.0;
493 float d2 = bsb.radius + surf_dist / 2.0;
494 float t = d1 / (d1 + d2);
499 sph->center = bsa.center * t + bsb.center * (1.0 - t);
500 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
504 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
506 if(num <= 0) return false;
508 if(!calc_bounding_sphere(sph, objv[0])) {
512 for(int i=1; i<num; i++) {
513 if(!calc_bounding_sphere(sph, sph, objv[i])) {
520 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
522 if(num <= 0) return false;
524 sph->center = Vec3(0.0, 0.0, 0.0);
525 for(int i=0; i<num; i++) {
526 sph->center += xform * v[i];
528 sph->center /= (float)num;
531 for(int i=0; i<num; i++) {
532 Vec3 dir = xform * v[i] - sph->center;
533 rad_sq = std::max(rad_sq, dot(dir, dir));
535 sph->radius = sqrt(rad_sq);
539 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
554 for(int i=0; i<8; i++) {
555 v[i] = BOX(obj)->get_corner(i);
557 calc_bounding_aabox(box, v, 8);
563 float r = SPHERE(obj)->radius;
564 box->min = SPHERE(obj)->center - Vec3(r, r, r);
565 box->max = SPHERE(obj)->center + Vec3(r, r, r);
576 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
580 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
584 for(int i=0; i<3; i++) {
585 box->min[i] = std::min(bba.min[i], bbb.min[i]);
586 box->max[i] = std::max(bba.max[i], bbb.max[i]);
591 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
593 if(num <= 0) return false;
595 if(!calc_bounding_aabox(box, objv[0])) {
599 for(int i=1; i<num; i++) {
600 if(!calc_bounding_aabox(box, box, objv[i])) {
607 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
609 if(num <= 0) return false;
611 box->min = box->max = xform * v[0];
612 for(int i=1; i<num; i++) {
613 Vec3 p = xform * v[i];
615 for(int j=0; j<3; j++) {
616 box->min[j] = std::min(box->min[j], p[j]);
617 box->max[j] = std::max(box->max[j], p[j]);
623 bool calc_bounding_box(Box *box, const GeomObject *obj)
636 box->min = BOX(obj)->min;
637 box->max = BOX(obj)->max;
638 box->xform = Mat4::identity;
643 float r = SPHERE(obj)->radius;
644 box->min = SPHERE(obj)->center - Vec3(r, r, r);
645 box->max = SPHERE(obj)->center + Vec3(r, r, r);
646 box->xform = Mat4::identity;
657 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
659 Vec3 dir = b.center - a.center;
661 float dist_sq = length_sq(dir);
662 if(dist_sq <= 1e-8) return false;
664 float rsum = a.radius + b.radius;
665 float rdif = fabs(a.radius - b.radius);
666 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
670 float dist = sqrt(dist_sq);
671 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
673 result->pt = a.center + dir * t;
674 result->normal = dir / dist;
675 result->radius = sin(acos(t)) * a.radius;
679 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
681 return false; // TODO
684 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
686 return false; // TODO
689 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
691 return false; // TODO
694 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
696 for(int i=0; i<3; i++) {
697 res->min[i] = std::max(a.min[i], b.min[i]);
698 res->max[i] = std::min(a.max[i], b.max[i]);
700 if(res->max[i] < res->min[i]) {
701 res->max[i] = res->min[i];
704 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;