6 GeomObject::~GeomObject()
16 Sphere::Sphere(const Vec3 ¢, float radius)
19 this->radius = radius;
22 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
24 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
25 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
28 fprintf(stderr, "Sphere::set_union: arguments must be spheres");
32 float dist = length(sph1->center - sph2->center);
33 float surf_dist = dist - (sph1->radius + sph2->radius);
34 float d1 = sph1->radius + surf_dist / 2.0;
35 float d2 = sph2->radius + surf_dist / 2.0;
36 float t = d1 / (d1 + d2);
41 center = sph1->center * t + sph2->center * (1.0 - t);
42 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
45 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
47 fprintf(stderr, "Sphere::intersection undefined\n");
50 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
52 float a = dot(ray.dir, ray.dir);
53 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
54 2.0 * ray.dir.y * (ray.origin.y - center.y) +
55 2.0 * ray.dir.z * (ray.origin.z - center.z);
56 float c = dot(ray.origin, ray.origin) + dot(center, center) -
57 2.0 * dot(ray.origin, center) - radius * radius;
59 float discr = b * b - 4.0 * a * c;
64 float sqrt_discr = sqrt(discr);
65 float t0 = (-b + sqrt_discr) / (2.0 * a);
66 float t1 = (-b - sqrt_discr) / (2.0 * a);
73 float t = t0 < t1 ? t0 : t1;
78 // fill the HitPoint structure
82 hit->pos = ray.origin + ray.dir * t;
83 hit->normal = (hit->pos - center) / radius;
93 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
94 : min(vmin), max(vmax)
98 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
100 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
101 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
104 fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
108 min.x = std::min(box1->min.x, box2->min.x);
109 min.y = std::min(box1->min.y, box2->min.y);
110 min.z = std::min(box1->min.z, box2->min.z);
112 max.x = std::max(box1->max.x, box2->max.x);
113 max.y = std::max(box1->max.y, box2->max.y);
114 max.z = std::max(box1->max.z, box2->max.z);
117 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
119 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
120 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
123 fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
127 for(int i=0; i<3; i++) {
128 min[i] = std::max(box1->min[i], box2->min[i]);
129 max[i] = std::min(box1->max[i], box2->max[i]);
131 if(max[i] < min[i]) {
137 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
139 Vec3 param[2] = {min, max};
140 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
141 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
143 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
144 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
145 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
146 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
148 if(tmin > tymax || tymin > tmax) {
158 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
159 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
161 if(tmin > tzmax || tzmin > tmax) {
171 float t = tmin < 1e-4 ? tmax : tmin;
177 hit->pos = ray.origin + ray.dir * t;
179 float min_dist = FLT_MAX;
180 Vec3 offs = min + (max - min) / 2.0;
181 Vec3 local_hit = hit->pos - offs;
183 static const Vec3 axis[] = {
184 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
186 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
188 for(int i=0; i<3; i++) {
189 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
190 if(dist < min_dist) {
192 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
193 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
204 : normal(0.0, 1.0, 0.0)
208 Plane::Plane(const Vec3 &p, const Vec3 &norm)
211 normal = normalize(norm);
214 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
217 normal = normalize(cross(p2 - p1, p3 - p1));
220 Plane::Plane(const Vec3 &normal, float dist)
222 this->normal = normalize(normal);
223 pt = this->normal * dist;
226 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
228 fprintf(stderr, "Plane::set_union undefined\n");
231 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
233 fprintf(stderr, "Plane::set_intersection undefined\n");
236 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
238 float ndotdir = dot(normal, ray.dir);
239 if(fabs(ndotdir) < 1e-4) {
244 Vec3 ptdir = pt - ray.origin;
245 float t = dot(normal, ptdir) / ndotdir;
247 hit->pos = ray.origin + ray.dir * t;
248 hit->normal = normal;
254 float sphere_distance(const Vec3 ¢, float rad, const Vec3 &pt)
256 return length(pt - cent) - rad;
259 // TODO version which takes both radii into account
260 float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt)
263 float ab_len_sq = length_sq(ab_dir);
265 if(fabs(ab_len_sq) < 1e-5) {
266 // if a == b, the capsule is a sphere with radius the maximum of the capsule radii
267 return sphere_distance(a, std::max(ra, rb), pt);
269 float ab_len = sqrt(ab_len_sq);
271 Vec3 ap_dir = pt - a;
273 float t = dot(ap_dir, ab_dir / ab_len) / ab_len;
275 return sphere_distance(a, ra, pt);
278 return sphere_distance(b, rb, pt);
281 Vec3 pproj = a + ab_dir * t;
282 return length(pproj - pt) - ra;
286 float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt)
290 if(fabs(length_sq(ab_dir)) < 1e-5) {
291 // if a == b, the capsule is a sphere with radius the maximum of the capsule radii
292 return sphere_distance(a, std::max(ra, rb), pt);
294 float ab_len = length(ab_dir);
296 Vec3 ap_dir = pt - a;
297 Vec3 rotaxis = normalize(cross(ab_dir, ap_dir));
300 rmat.set_rotation(rotaxis, M_PI / 2.0);
301 Vec3 right = rmat * ab_dir / ab_len;
303 // XXX I think this check is redundant, always false, due to the cross product order
304 //assert(dot(right, ab_dir) >= 0.0);
305 if(dot(right, ab_dir) < 0.0) {
308 Vec3 aa = a + right * ra;
309 Vec3 bb = b + right * rb;
311 // project pt to the line segment bb-aa, see if the projection lies within the interval [0, 1)
312 Vec3 aabb_dir = bb - aa;
313 float aabb_len = length(aabb_dir);
314 Vec3 aap_dir = pt - aa;
316 float t = dot(aap_dir, aabb_dir / aabb_len) / aabb_len;
318 return sphere_distance(a, ra, pt);
321 return sphere_distance(b, rb, pt);
324 Vec3 ppt = aa + aabb_dir * t;
325 Vec3 norm = ppt - pt;
326 float dist = length(norm);
328 if(dot(norm, right) < 0.0) {