5 #define SPHERE(ptr) ((Sphere*)ptr)
6 #define AABOX(ptr) ((AABox*)ptr)
7 #define BOX(ptr) ((Box*)ptr)
8 #define PLANE(ptr) ((Plane*)ptr)
10 GeomObject::GeomObject()
15 GeomObject::~GeomObject()
26 Sphere::Sphere(const Vec3 ¢, float radius)
30 this->radius = radius;
33 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
35 float a = dot(ray.dir, ray.dir);
36 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
37 2.0 * ray.dir.y * (ray.origin.y - center.y) +
38 2.0 * ray.dir.z * (ray.origin.z - center.z);
39 float c = dot(ray.origin, ray.origin) + dot(center, center) -
40 2.0 * dot(ray.origin, center) - radius * radius;
42 float discr = b * b - 4.0 * a * c;
47 float sqrt_discr = sqrt(discr);
48 float t0 = (-b + sqrt_discr) / (2.0 * a);
49 float t1 = (-b - sqrt_discr) / (2.0 * a);
56 float t = t0 < t1 ? t0 : t1;
61 // fill the HitPoint structure
65 hit->pos = ray.origin + ray.dir * t;
66 hit->normal = (hit->pos - center) / radius;
71 bool Sphere::contains(const Vec3 &pt) const
73 return length_sq(pt - center) <= radius * radius;
76 float Sphere::distance(const Vec3 &v) const
78 return std::max(length(v - center) - radius, 0.0f);
81 float Sphere::signed_distance(const Vec3 &v) const
83 return length(v - center) - radius;
91 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
92 : min(vmin), max(vmax)
97 Vec3 AABox::get_corner(int idx) const
99 static const Vec3 v[] = {
100 Vec3(-0.5, -0.5, -0.5), Vec3(0.5, -0.5, -0.5), Vec3(0.5, -0.5, 0.5), Vec3(-0.5, -0.5, 0.5),
101 Vec3(-0.5, 0.5, -0.5), Vec3(0.5, 0.5, -0.5), Vec3(0.5, 0.5, 0.5), Vec3(-0.5, 0.5, 0.5)
103 return v[idx] * Vec3(max - min);
106 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
108 Vec3 param[2] = {min, max};
109 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
110 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
112 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
113 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
114 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
115 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
117 if(tmin > tymax || tymin > tmax) {
127 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
128 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
130 if(tmin > tzmax || tzmin > tmax) {
140 float t = tmin < 1e-4 ? tmax : tmin;
146 hit->pos = ray.origin + ray.dir * t;
148 float min_dist = FLT_MAX;
149 Vec3 offs = min + (max - min) / 2.0;
150 Vec3 local_hit = hit->pos - offs;
152 static const Vec3 axis[] = {
153 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
155 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
157 for(int i=0; i<3; i++) {
158 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
159 if(dist < min_dist) {
161 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
162 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
171 bool AABox::contains(const Vec3 &v) const
173 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
174 v.x <= max.x && v.y <= max.y && v.z <= max.z;
177 float AABox::distance(const Vec3 &v) const
182 float AABox::signed_distance(const Vec3 &v) const
191 Box::Box(const AABox &aabox, const Mat4 &xform)
198 Box::Box(const Vec3 &min, const Vec3 &max)
203 Box::Box(const Vec3 &min, const Vec3 &max, const Mat4 &xform)
204 : AABox(min, max), xform(xform)
208 // XXX all this shit is completely untested
209 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
211 float ilen = length(vi);
212 float jlen = length(vj);
213 float klen = length(vk);
215 min = Vec3(-ilen, -jlen, -klen);
216 max = Vec3(ilen, jlen, klen);
218 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
219 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
220 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
222 xform = Mat4(vi * si, vj * sj, vk * sk);
223 xform.translate(pos);
226 Box::Box(const Vec3 *varr, int vcount)
228 calc_bounding_aabox(this, varr, vcount);
231 Vec3 Box::get_corner(int idx) const
233 static const Vec3 v[] = {
234 Vec3(-0.5, -0.5, -0.5), Vec3(0.5, -0.5, -0.5), Vec3(0.5, -0.5, 0.5), Vec3(-0.5, -0.5, 0.5),
235 Vec3(-0.5, 0.5, -0.5), Vec3(0.5, 0.5, -0.5), Vec3(0.5, 0.5, 0.5), Vec3(-0.5, 0.5, 0.5)
238 return xform * (v[idx] * Vec3(max - min));
241 bool Box::intersect(const Ray &ray, HitPoint *hit) const
243 Mat4 inv_xform = inverse(xform);
244 Mat4 dir_inv_xform = inv_xform.upper3x3();
245 Mat4 dir_xform = transpose(dir_inv_xform);
246 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
248 bool res = AABox::intersect(local_ray, hit);
249 if(!res || !hit) return res;
251 hit->pos = xform * hit->pos;
252 hit->normal = dir_xform * hit->normal;
253 hit->local_ray = local_ray;
258 bool Box::contains(const Vec3 &pt) const
260 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
261 return AABox::contains(inverse(xform) * pt);
264 float Box::distance(const Vec3 &v) const
269 float Box::signed_distance(const Vec3 &v) const
275 : normal(0.0, 1.0, 0.0)
279 Plane::Plane(const Vec3 &p, const Vec3 &norm)
282 normal = normalize(norm);
285 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
288 normal = normalize(cross(p2 - p1, p3 - p1));
291 Plane::Plane(const Vec3 &normal, float dist)
293 this->normal = normalize(normal);
294 pt = this->normal * dist;
297 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
299 float ndotdir = dot(normal, ray.dir);
300 if(fabs(ndotdir) < 1e-4) {
305 Vec3 ptdir = pt - ray.origin;
306 float t = dot(normal, ptdir) / ndotdir;
309 hit->pos = ray.origin + ray.dir * t;
310 hit->normal = normal;
316 bool Plane::contains(const Vec3 &v) const
318 return dot(v, normal) <= 0.0;
321 float Plane::distance(const Vec3 &v) const
323 return std::max(dot(v - pt, normal), 0.0f);
326 float Plane::signed_distance(const Vec3 &v) const
328 return dot(v - pt, normal);
337 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
343 Disc::Disc(const Vec3 &normal, float dist, float rad)
344 : Plane(normal, dist)
349 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
352 if(Plane::intersect(ray, &phit)) {
353 if(length_sq(phit.pos - pt) <= radius * radius) {
361 bool Disc::contains(const Vec3 &pt) const
363 Vec3 pj = proj_point_plane(pt, *this);
364 return length_sq(pj - this->pt) <= radius * radius;
367 float Disc::distance(const Vec3 &v) const
372 float Disc::signed_distance(const Vec3 &v) const
378 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
380 float dist = plane.signed_distance(pt);
381 return pt - plane.normal * dist;
384 // ---- bounding sphere calculations ----
386 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
390 *sph = *(Sphere*)obj;
394 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
395 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
399 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
400 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
410 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
414 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
418 float dist = length(bsa.center - bsb.center);
419 float surf_dist = dist - (bsa.radius + bsb.radius);
420 float d1 = bsa.radius + surf_dist / 2.0;
421 float d2 = bsb.radius + surf_dist / 2.0;
422 float t = d1 / (d1 + d2);
427 sph->center = bsa.center * t + bsb.center * (1.0 - t);
428 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
432 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
434 if(num <= 0) return false;
436 if(!calc_bounding_sphere(sph, objv[0])) {
440 for(int i=1; i<num; i++) {
441 if(!calc_bounding_sphere(sph, sph, objv[i])) {
448 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
450 if(num <= 0) return false;
452 sph->center = Vec3(0.0, 0.0, 0.0);
453 for(int i=0; i<num; i++) {
454 sph->center += xform * v[i];
456 sph->center /= (float)num;
459 for(int i=0; i<num; i++) {
460 Vec3 dir = xform * v[i] - sph->center;
461 rad_sq = std::max(rad_sq, dot(dir, dir));
463 sph->radius = sqrt(rad_sq);
467 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
477 for(int i=0; i<8; i++) {
478 v[i] = BOX(obj)->get_corner(i);
480 calc_bounding_aabox(box, v, 8);
486 float r = SPHERE(obj)->radius;
487 box->min = SPHERE(obj)->center - Vec3(r, r, r);
488 box->max = SPHERE(obj)->center + Vec3(r, r, r);
499 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
503 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
507 for(int i=0; i<3; i++) {
508 box->min[i] = std::min(bba.min[i], bbb.min[i]);
509 box->max[i] = std::max(bba.max[i], bbb.max[i]);
514 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
516 if(num <= 0) return false;
518 if(!calc_bounding_aabox(box, objv[0])) {
522 for(int i=1; i<num; i++) {
523 if(!calc_bounding_aabox(box, box, objv[i])) {
530 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
532 if(num <= 0) return false;
534 box->min = box->max = xform * v[0];
535 for(int i=1; i<num; i++) {
536 Vec3 p = xform * v[i];
538 for(int j=0; j<3; j++) {
539 box->min[j] = std::min(box->min[j], p[j]);
540 box->max[j] = std::max(box->max[j], p[j]);
546 bool calc_bounding_box(Box *box, const GeomObject *obj)
554 box->min = BOX(obj)->min;
555 box->max = BOX(obj)->max;
556 box->xform = Mat4::identity;
561 float r = SPHERE(obj)->radius;
562 box->min = SPHERE(obj)->center - Vec3(r, r, r);
563 box->max = SPHERE(obj)->center + Vec3(r, r, r);
564 box->xform = Mat4::identity;
575 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
577 Vec3 dir = b.center - a.center;
579 float dist_sq = length_sq(dir);
580 if(dist_sq <= 1e-8) return false;
582 float rsum = a.radius + b.radius;
583 float rdif = fabs(a.radius - b.radius);
584 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
588 float dist = sqrt(dist_sq);
589 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
591 result->pt = a.center + dir * t;
592 result->normal = dir / dist;
593 result->radius = sin(acos(t)) * a.radius;
597 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
599 return false; // TODO
602 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
604 return false; // TODO
607 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
609 return false; // TODO
612 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
614 for(int i=0; i<3; i++) {
615 res->min[i] = std::max(a.min[i], b.min[i]);
616 res->max[i] = std::min(a.max[i], b.max[i]);
618 if(res->max[i] < res->min[i]) {
619 res->max[i] = res->min[i];
622 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;