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 Vec3 &min, const Vec3 &max)
196 // XXX all this shit is completely untested
197 Box::Box(const Vec3 &pos, const Vec3 &vi, const Vec3 &vj, const Vec3 &vk)
199 float ilen = length(vi);
200 float jlen = length(vj);
201 float klen = length(vk);
203 min = Vec3(-ilen, -jlen, -klen);
204 max = Vec3(ilen, jlen, klen);
206 float si = ilen == 0.0 ? 1.0 : 1.0 / ilen;
207 float sj = jlen == 0.0 ? 1.0 : 1.0 / jlen;
208 float sk = klen == 0.0 ? 1.0 : 1.0 / klen;
210 xform = Mat4(vi * si, vj * sj, vk * sk);
211 xform.translate(pos);
214 Box::Box(const Vec3 *varr, int vcount)
216 calc_bounding_aabox(this, varr, vcount);
219 Vec3 Box::get_corner(int idx) const
221 static const Vec3 v[] = {
222 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),
223 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)
226 return xform * (v[idx] * Vec3(max - min));
229 bool Box::intersect(const Ray &ray, HitPoint *hit) const
231 Mat4 inv_xform = inverse(xform);
232 Mat4 dir_inv_xform = inv_xform.upper3x3();
233 Mat4 dir_xform = transpose(dir_inv_xform);
234 Ray local_ray = Ray(inv_xform * ray.origin, dir_inv_xform * ray.dir);
236 bool res = AABox::intersect(local_ray, hit);
237 if(!res || !hit) return res;
239 hit->pos = xform * hit->pos;
240 hit->normal = dir_xform * hit->normal;
241 hit->local_ray = local_ray;
246 bool Box::contains(const Vec3 &pt) const
248 // XXX is it faster to extract 6 planes and do dot products? sounds marginal
249 return AABox::contains(inverse(xform) * pt);
252 float Box::distance(const Vec3 &v) const
257 float Box::signed_distance(const Vec3 &v) const
263 : normal(0.0, 1.0, 0.0)
267 Plane::Plane(const Vec3 &p, const Vec3 &norm)
270 normal = normalize(norm);
273 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
276 normal = normalize(cross(p2 - p1, p3 - p1));
279 Plane::Plane(const Vec3 &normal, float dist)
281 this->normal = normalize(normal);
282 pt = this->normal * dist;
285 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
287 float ndotdir = dot(normal, ray.dir);
288 if(fabs(ndotdir) < 1e-4) {
293 Vec3 ptdir = pt - ray.origin;
294 float t = dot(normal, ptdir) / ndotdir;
297 hit->pos = ray.origin + ray.dir * t;
298 hit->normal = normal;
304 bool Plane::contains(const Vec3 &v) const
306 return dot(v, normal) <= 0.0;
309 float Plane::distance(const Vec3 &v) const
311 return std::max(dot(v - pt, normal), 0.0f);
314 float Plane::signed_distance(const Vec3 &v) const
316 return dot(v - pt, normal);
325 Disc::Disc(const Vec3 &pt, const Vec3 &normal, float rad)
331 Disc::Disc(const Vec3 &normal, float dist, float rad)
332 : Plane(normal, dist)
337 bool Disc::intersect(const Ray &ray, HitPoint *hit) const
340 if(Plane::intersect(ray, &phit)) {
341 if(length_sq(phit.pos - pt) <= radius * radius) {
349 bool Disc::contains(const Vec3 &pt) const
351 Vec3 pj = proj_point_plane(pt, *this);
352 return length_sq(pj - this->pt) <= radius * radius;
355 float Disc::distance(const Vec3 &v) const
360 float Disc::signed_distance(const Vec3 &v) const
366 Vec3 proj_point_plane(const Vec3 &pt, const Plane &plane)
368 float dist = plane.signed_distance(pt);
369 return pt - plane.normal * dist;
372 // ---- bounding sphere calculations ----
374 bool calc_bounding_sphere(Sphere *sph, const GeomObject *obj)
378 *sph = *(Sphere*)obj;
382 sph->center = (AABOX(obj)->min + AABOX(obj)->max) * 0.5;
383 sph->radius = length(AABOX(obj)->max - AABOX(obj)->min) * 0.5;
387 sph->center = (BOX(obj)->min + BOX(obj)->max) * 0.5 + BOX(obj)->xform.get_translation();
388 sph->radius = length(BOX(obj)->max - BOX(obj)->min) * 0.5;
398 bool calc_bounding_sphere(Sphere *sph, const GeomObject *a, const GeomObject *b)
402 if(!calc_bounding_sphere(&bsa, a) || !calc_bounding_sphere(&bsb, b)) {
406 float dist = length(bsa.center - bsb.center);
407 float surf_dist = dist - (bsa.radius + bsb.radius);
408 float d1 = bsa.radius + surf_dist / 2.0;
409 float d2 = bsb.radius + surf_dist / 2.0;
410 float t = d1 / (d1 + d2);
415 sph->center = bsa.center * t + bsb.center * (1.0 - t);
416 sph->radius = std::max(dist * t + bsb.radius, dist * (1.0f - t) + bsa.radius);
420 bool calc_bounding_sphere(Sphere *sph, const GeomObject **objv, int num)
422 if(num <= 0) return false;
424 if(!calc_bounding_sphere(sph, objv[0])) {
428 for(int i=1; i<num; i++) {
429 if(!calc_bounding_sphere(sph, sph, objv[i])) {
436 bool calc_bounding_sphere(Sphere *sph, const Vec3 *v, int num, const Mat4 &xform)
438 if(num <= 0) return false;
440 sph->center = Vec3(0.0, 0.0, 0.0);
441 for(int i=0; i<num; i++) {
442 sph->center += xform * v[i];
444 sph->center /= (float)num;
447 for(int i=0; i<num; i++) {
448 Vec3 dir = xform * v[i] - sph->center;
449 rad_sq = std::max(rad_sq, dot(dir, dir));
451 sph->radius = sqrt(rad_sq);
455 bool calc_bounding_aabox(AABox *box, const GeomObject *obj)
465 for(int i=0; i<8; i++) {
466 v[i] = BOX(obj)->get_corner(i);
468 calc_bounding_aabox(box, v, 8);
474 float r = SPHERE(obj)->radius;
475 box->min = SPHERE(obj)->center - Vec3(r, r, r);
476 box->max = SPHERE(obj)->center + Vec3(r, r, r);
487 bool calc_bounding_aabox(AABox *box, const GeomObject *a, const GeomObject *b)
491 if(!calc_bounding_aabox(&bba, a) || !calc_bounding_aabox(&bbb, b)) {
495 for(int i=0; i<3; i++) {
496 box->min[i] = std::min(bba.min[i], bbb.min[i]);
497 box->max[i] = std::max(bba.max[i], bbb.max[i]);
502 bool calc_bounding_aabox(AABox *box, const GeomObject **objv, int num)
504 if(num <= 0) return false;
506 if(!calc_bounding_aabox(box, objv[0])) {
510 for(int i=1; i<num; i++) {
511 if(!calc_bounding_aabox(box, box, objv[i])) {
518 bool calc_bounding_aabox(AABox *box, const Vec3 *v, int num, const Mat4 &xform)
520 if(num <= 0) return false;
522 box->min = box->max = xform * v[0];
523 for(int i=1; i<num; i++) {
524 Vec3 p = xform * v[i];
526 for(int j=0; j<3; j++) {
527 box->min[j] = std::min(box->min[j], p[j]);
528 box->max[j] = std::max(box->max[j], p[j]);
534 bool intersect_sphere_sphere(Disc *result, const Sphere &a, const Sphere &b)
536 Vec3 dir = b.center - a.center;
538 float dist_sq = length_sq(dir);
539 if(dist_sq <= 1e-8) return false;
541 float rsum = a.radius + b.radius;
542 float rdif = fabs(a.radius - b.radius);
543 if(dist_sq > rsum * rsum || dist_sq < rdif * rdif) {
547 float dist = sqrt(dist_sq);
548 float t = (dist_sq + a.radius * a.radius - b.radius * b.radius) / (2.0 * sqrt(dist_sq));
550 result->pt = a.center + dir * t;
551 result->normal = dir / dist;
552 result->radius = sin(acos(t)) * a.radius;
556 bool intersect_plane_plane(Ray *result, const Plane &a, const Plane &b)
558 return false; // TODO
561 bool intersect_sphere_plane(Sphere *result, const Sphere &s, const Plane &p)
563 return false; // TODO
566 bool intersect_plane_sphere(Sphere *result, const Plane &p, const Sphere &s)
568 return false; // TODO
571 bool intersect_aabox_aabox(AABox *res, const AABox &a, const AABox &b)
573 for(int i=0; i<3; i++) {
574 res->min[i] = std::max(a.min[i], b.min[i]);
575 res->max[i] = std::min(a.max[i], b.max[i]);
577 if(res->max[i] < res->min[i]) {
578 res->max[i] = res->min[i];
581 return res->min.x != res->max.x && res->min.y != res->max.y && res->min.z != res->max.z;