nuclear@4: /* nuclear@4: Cubemapper - a program for converting panoramic images into cubemaps nuclear@4: Copyright (C) 2017 John Tsiombikas nuclear@4: nuclear@4: This program is free software: you can redistribute it and/or modify nuclear@4: it under the terms of the GNU General Public License as published by nuclear@4: the Free Software Foundation, either version 3 of the License, or nuclear@4: (at your option) any later version. nuclear@4: nuclear@4: This program is distributed in the hope that it will be useful, nuclear@4: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@4: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@4: GNU General Public License for more details. nuclear@4: nuclear@4: You should have received a copy of the GNU General Public License nuclear@4: along with this program. If not, see . nuclear@4: */ nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "geom.h" nuclear@0: nuclear@0: GeomObject::~GeomObject() nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: nuclear@0: Sphere::Sphere() nuclear@0: { nuclear@0: radius = 1.0; nuclear@0: } nuclear@0: nuclear@0: Sphere::Sphere(const Vec3 ¢, float radius) nuclear@0: : center(cent) nuclear@0: { nuclear@0: this->radius = radius; nuclear@0: } nuclear@0: nuclear@0: void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: const Sphere *sph1 = dynamic_cast(obj1); nuclear@0: const Sphere *sph2 = dynamic_cast(obj2); nuclear@0: nuclear@0: if(!sph1 || !sph2) { nuclear@0: fprintf(stderr, "Sphere::set_union: arguments must be spheres"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: float dist = length(sph1->center - sph2->center); nuclear@0: float surf_dist = dist - (sph1->radius + sph2->radius); nuclear@0: float d1 = sph1->radius + surf_dist / 2.0; nuclear@0: float d2 = sph2->radius + surf_dist / 2.0; nuclear@0: float t = d1 / (d1 + d2); nuclear@0: nuclear@0: if(t < 0.0) t = 0.0; nuclear@0: if(t > 1.0) t = 1.0; nuclear@0: nuclear@0: center = sph1->center * t + sph2->center * (1.0 - t); nuclear@0: radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius); nuclear@0: } nuclear@0: nuclear@0: void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: fprintf(stderr, "Sphere::intersection undefined\n"); nuclear@0: } nuclear@0: nuclear@0: bool Sphere::intersect(const Ray &ray, HitPoint *hit) const nuclear@0: { nuclear@0: float a = dot(ray.dir, ray.dir); nuclear@0: float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) + nuclear@0: 2.0 * ray.dir.y * (ray.origin.y - center.y) + nuclear@0: 2.0 * ray.dir.z * (ray.origin.z - center.z); nuclear@0: float c = dot(ray.origin, ray.origin) + dot(center, center) - nuclear@0: 2.0 * dot(ray.origin, center) - radius * radius; nuclear@0: nuclear@0: float discr = b * b - 4.0 * a * c; nuclear@0: if(discr < 1e-4) { nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: float sqrt_discr = sqrt(discr); nuclear@0: float t0 = (-b + sqrt_discr) / (2.0 * a); nuclear@0: float t1 = (-b - sqrt_discr) / (2.0 * a); nuclear@0: nuclear@0: if(t0 < 1e-4) nuclear@0: t0 = t1; nuclear@0: if(t1 < 1e-4) nuclear@0: t1 = t0; nuclear@0: nuclear@0: float t = t0 < t1 ? t0 : t1; nuclear@0: if(t < 1e-4) { nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: // fill the HitPoint structure nuclear@0: if(hit) { nuclear@0: hit->obj = this; nuclear@0: hit->dist = t; nuclear@0: hit->pos = ray.origin + ray.dir * t; nuclear@0: hit->normal = (hit->pos - center) / radius; nuclear@0: } nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: AABox::AABox() nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: AABox::AABox(const Vec3 &vmin, const Vec3 &vmax) nuclear@0: : min(vmin), max(vmax) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: const AABox *box1 = dynamic_cast(obj1); nuclear@0: const AABox *box2 = dynamic_cast(obj2); nuclear@0: nuclear@0: if(!box1 || !box2) { nuclear@0: fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: min.x = std::min(box1->min.x, box2->min.x); nuclear@0: min.y = std::min(box1->min.y, box2->min.y); nuclear@0: min.z = std::min(box1->min.z, box2->min.z); nuclear@0: nuclear@0: max.x = std::max(box1->max.x, box2->max.x); nuclear@0: max.y = std::max(box1->max.y, box2->max.y); nuclear@0: max.z = std::max(box1->max.z, box2->max.z); nuclear@0: } nuclear@0: nuclear@0: void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: const AABox *box1 = dynamic_cast(obj1); nuclear@0: const AABox *box2 = dynamic_cast(obj2); nuclear@0: nuclear@0: if(!box1 || !box2) { nuclear@0: fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n"); nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: for(int i=0; i<3; i++) { nuclear@0: min[i] = std::max(box1->min[i], box2->min[i]); nuclear@0: max[i] = std::min(box1->max[i], box2->max[i]); nuclear@0: nuclear@0: if(max[i] < min[i]) { nuclear@0: max[i] = min[i]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: bool AABox::intersect(const Ray &ray, HitPoint *hit) const nuclear@0: { nuclear@0: Vec3 param[2] = {min, max}; nuclear@0: Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z); nuclear@0: int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0}; nuclear@0: nuclear@0: float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x; nuclear@0: float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x; nuclear@0: float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y; nuclear@0: float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y; nuclear@0: nuclear@0: if(tmin > tymax || tymin > tmax) { nuclear@0: return false; nuclear@0: } nuclear@0: if(tymin > tmin) { nuclear@0: tmin = tymin; nuclear@0: } nuclear@0: if(tymax < tmax) { nuclear@0: tmax = tymax; nuclear@0: } nuclear@0: nuclear@0: float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z; nuclear@0: float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z; nuclear@0: nuclear@0: if(tmin > tzmax || tzmin > tmax) { nuclear@0: return false; nuclear@0: } nuclear@0: if(tzmin > tmin) { nuclear@0: tmin = tzmin; nuclear@0: } nuclear@0: if(tzmax < tmax) { nuclear@0: tmax = tzmax; nuclear@0: } nuclear@0: nuclear@0: float t = tmin < 1e-4 ? tmax : tmin; nuclear@0: if(t >= 1e-4) { nuclear@0: nuclear@0: if(hit) { nuclear@0: hit->obj = this; nuclear@0: hit->dist = t; nuclear@0: hit->pos = ray.origin + ray.dir * t; nuclear@0: nuclear@0: float min_dist = FLT_MAX; nuclear@0: Vec3 offs = min + (max - min) / 2.0; nuclear@0: Vec3 local_hit = hit->pos - offs; nuclear@0: nuclear@0: static const Vec3 axis[] = { nuclear@0: Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1) nuclear@0: }; nuclear@0: //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}}; nuclear@0: nuclear@0: for(int i=0; i<3; i++) { nuclear@0: float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i])); nuclear@0: if(dist < min_dist) { nuclear@0: min_dist = dist; nuclear@0: hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0); nuclear@0: //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: return true; nuclear@0: } nuclear@0: return false; nuclear@0: nuclear@0: } nuclear@0: nuclear@0: Plane::Plane() nuclear@0: : normal(0.0, 1.0, 0.0) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: Plane::Plane(const Vec3 &p, const Vec3 &norm) nuclear@0: : pt(p) nuclear@0: { nuclear@0: normal = normalize(norm); nuclear@0: } nuclear@0: nuclear@0: Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3) nuclear@0: : pt(p1) nuclear@0: { nuclear@0: normal = normalize(cross(p2 - p1, p3 - p1)); nuclear@0: } nuclear@0: nuclear@0: Plane::Plane(const Vec3 &normal, float dist) nuclear@0: { nuclear@0: this->normal = normalize(normal); nuclear@0: pt = this->normal * dist; nuclear@0: } nuclear@0: nuclear@0: void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: fprintf(stderr, "Plane::set_union undefined\n"); nuclear@0: } nuclear@0: nuclear@0: void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2) nuclear@0: { nuclear@0: fprintf(stderr, "Plane::set_intersection undefined\n"); nuclear@0: } nuclear@0: nuclear@0: bool Plane::intersect(const Ray &ray, HitPoint *hit) const nuclear@0: { nuclear@0: float ndotdir = dot(normal, ray.dir); nuclear@0: if(fabs(ndotdir) < 1e-4) { nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: if(hit) { nuclear@0: Vec3 ptdir = pt - ray.origin; nuclear@0: float t = dot(normal, ptdir) / ndotdir; nuclear@0: nuclear@0: hit->pos = ray.origin + ray.dir * t; nuclear@0: hit->normal = normal; nuclear@0: hit->obj = this; nuclear@0: } nuclear@0: return true; nuclear@0: } nuclear@0: nuclear@0: float sphere_distance(const Vec3 ¢, float rad, const Vec3 &pt) nuclear@0: { nuclear@0: return length(pt - cent) - rad; nuclear@0: } nuclear@0: nuclear@0: // TODO version which takes both radii into account nuclear@0: float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt) nuclear@0: { nuclear@0: Vec3 ab_dir = b - a; nuclear@0: float ab_len_sq = length_sq(ab_dir); nuclear@0: nuclear@0: if(fabs(ab_len_sq) < 1e-5) { nuclear@0: // if a == b, the capsule is a sphere with radius the maximum of the capsule radii nuclear@0: return sphere_distance(a, std::max(ra, rb), pt); nuclear@0: } nuclear@0: float ab_len = sqrt(ab_len_sq); nuclear@0: nuclear@0: Vec3 ap_dir = pt - a; nuclear@0: nuclear@0: float t = dot(ap_dir, ab_dir / ab_len) / ab_len; nuclear@0: if(t < 0.0) { nuclear@0: return sphere_distance(a, ra, pt); nuclear@0: } nuclear@0: if(t >= 1.0) { nuclear@0: return sphere_distance(b, rb, pt); nuclear@0: } nuclear@0: nuclear@0: Vec3 pproj = a + ab_dir * t; nuclear@0: return length(pproj - pt) - ra; nuclear@0: } nuclear@0: nuclear@0: #if 0 nuclear@0: float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt) nuclear@0: { nuclear@0: Vec3 ab_dir = b - a; nuclear@0: nuclear@0: if(fabs(length_sq(ab_dir)) < 1e-5) { nuclear@0: // if a == b, the capsule is a sphere with radius the maximum of the capsule radii nuclear@0: return sphere_distance(a, std::max(ra, rb), pt); nuclear@0: } nuclear@0: float ab_len = length(ab_dir); nuclear@0: nuclear@0: Vec3 ap_dir = pt - a; nuclear@0: Vec3 rotaxis = normalize(cross(ab_dir, ap_dir)); nuclear@0: nuclear@0: Mat4 rmat; nuclear@0: rmat.set_rotation(rotaxis, M_PI / 2.0); nuclear@0: Vec3 right = rmat * ab_dir / ab_len; nuclear@0: nuclear@0: // XXX I think this check is redundant, always false, due to the cross product order nuclear@0: //assert(dot(right, ab_dir) >= 0.0); nuclear@0: if(dot(right, ab_dir) < 0.0) { nuclear@0: right = -right; nuclear@0: } nuclear@0: Vec3 aa = a + right * ra; nuclear@0: Vec3 bb = b + right * rb; nuclear@0: nuclear@0: // project pt to the line segment bb-aa, see if the projection lies within the interval [0, 1) nuclear@0: Vec3 aabb_dir = bb - aa; nuclear@0: float aabb_len = length(aabb_dir); nuclear@0: Vec3 aap_dir = pt - aa; nuclear@0: nuclear@0: float t = dot(aap_dir, aabb_dir / aabb_len) / aabb_len; nuclear@0: if(t < 0.0) { nuclear@0: return sphere_distance(a, ra, pt); nuclear@0: } nuclear@0: if(t >= 1.0) { nuclear@0: return sphere_distance(b, rb, pt); nuclear@0: } nuclear@0: nuclear@0: Vec3 ppt = aa + aabb_dir * t; nuclear@0: Vec3 norm = ppt - pt; nuclear@0: float dist = length(norm); nuclear@0: nuclear@0: if(dot(norm, right) < 0.0) { nuclear@0: // inside the cone nuclear@0: dist = -dist; nuclear@0: } nuclear@0: return dist; nuclear@0: } nuclear@0: #endif