cubemapper

view src/geom.cc @ 4:2bfafdced01a

added README, COPYING, and copyright headers
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Jul 2017 16:11:19 +0300
parents 8fc9e1d3aad2
children
line source
1 /*
2 Cubemapper - a program for converting panoramic images into cubemaps
3 Copyright (C) 2017 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <assert.h>
19 #include <float.h>
20 #include <algorithm>
21 #include "geom.h"
23 GeomObject::~GeomObject()
24 {
25 }
28 Sphere::Sphere()
29 {
30 radius = 1.0;
31 }
33 Sphere::Sphere(const Vec3 &cent, float radius)
34 : center(cent)
35 {
36 this->radius = radius;
37 }
39 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
40 {
41 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
42 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
44 if(!sph1 || !sph2) {
45 fprintf(stderr, "Sphere::set_union: arguments must be spheres");
46 return;
47 }
49 float dist = length(sph1->center - sph2->center);
50 float surf_dist = dist - (sph1->radius + sph2->radius);
51 float d1 = sph1->radius + surf_dist / 2.0;
52 float d2 = sph2->radius + surf_dist / 2.0;
53 float t = d1 / (d1 + d2);
55 if(t < 0.0) t = 0.0;
56 if(t > 1.0) t = 1.0;
58 center = sph1->center * t + sph2->center * (1.0 - t);
59 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
60 }
62 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
63 {
64 fprintf(stderr, "Sphere::intersection undefined\n");
65 }
67 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
68 {
69 float a = dot(ray.dir, ray.dir);
70 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
71 2.0 * ray.dir.y * (ray.origin.y - center.y) +
72 2.0 * ray.dir.z * (ray.origin.z - center.z);
73 float c = dot(ray.origin, ray.origin) + dot(center, center) -
74 2.0 * dot(ray.origin, center) - radius * radius;
76 float discr = b * b - 4.0 * a * c;
77 if(discr < 1e-4) {
78 return false;
79 }
81 float sqrt_discr = sqrt(discr);
82 float t0 = (-b + sqrt_discr) / (2.0 * a);
83 float t1 = (-b - sqrt_discr) / (2.0 * a);
85 if(t0 < 1e-4)
86 t0 = t1;
87 if(t1 < 1e-4)
88 t1 = t0;
90 float t = t0 < t1 ? t0 : t1;
91 if(t < 1e-4) {
92 return false;
93 }
95 // fill the HitPoint structure
96 if(hit) {
97 hit->obj = this;
98 hit->dist = t;
99 hit->pos = ray.origin + ray.dir * t;
100 hit->normal = (hit->pos - center) / radius;
101 }
102 return true;
103 }
106 AABox::AABox()
107 {
108 }
110 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
111 : min(vmin), max(vmax)
112 {
113 }
115 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
116 {
117 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
118 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
120 if(!box1 || !box2) {
121 fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
122 return;
123 }
125 min.x = std::min(box1->min.x, box2->min.x);
126 min.y = std::min(box1->min.y, box2->min.y);
127 min.z = std::min(box1->min.z, box2->min.z);
129 max.x = std::max(box1->max.x, box2->max.x);
130 max.y = std::max(box1->max.y, box2->max.y);
131 max.z = std::max(box1->max.z, box2->max.z);
132 }
134 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
135 {
136 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
137 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
139 if(!box1 || !box2) {
140 fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
141 return;
142 }
144 for(int i=0; i<3; i++) {
145 min[i] = std::max(box1->min[i], box2->min[i]);
146 max[i] = std::min(box1->max[i], box2->max[i]);
148 if(max[i] < min[i]) {
149 max[i] = min[i];
150 }
151 }
152 }
154 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
155 {
156 Vec3 param[2] = {min, max};
157 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
158 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
160 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
161 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
162 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
163 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
165 if(tmin > tymax || tymin > tmax) {
166 return false;
167 }
168 if(tymin > tmin) {
169 tmin = tymin;
170 }
171 if(tymax < tmax) {
172 tmax = tymax;
173 }
175 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
176 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
178 if(tmin > tzmax || tzmin > tmax) {
179 return false;
180 }
181 if(tzmin > tmin) {
182 tmin = tzmin;
183 }
184 if(tzmax < tmax) {
185 tmax = tzmax;
186 }
188 float t = tmin < 1e-4 ? tmax : tmin;
189 if(t >= 1e-4) {
191 if(hit) {
192 hit->obj = this;
193 hit->dist = t;
194 hit->pos = ray.origin + ray.dir * t;
196 float min_dist = FLT_MAX;
197 Vec3 offs = min + (max - min) / 2.0;
198 Vec3 local_hit = hit->pos - offs;
200 static const Vec3 axis[] = {
201 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
202 };
203 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
205 for(int i=0; i<3; i++) {
206 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
207 if(dist < min_dist) {
208 min_dist = dist;
209 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
210 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
211 }
212 }
213 }
214 return true;
215 }
216 return false;
218 }
220 Plane::Plane()
221 : normal(0.0, 1.0, 0.0)
222 {
223 }
225 Plane::Plane(const Vec3 &p, const Vec3 &norm)
226 : pt(p)
227 {
228 normal = normalize(norm);
229 }
231 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
232 : pt(p1)
233 {
234 normal = normalize(cross(p2 - p1, p3 - p1));
235 }
237 Plane::Plane(const Vec3 &normal, float dist)
238 {
239 this->normal = normalize(normal);
240 pt = this->normal * dist;
241 }
243 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
244 {
245 fprintf(stderr, "Plane::set_union undefined\n");
246 }
248 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
249 {
250 fprintf(stderr, "Plane::set_intersection undefined\n");
251 }
253 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
254 {
255 float ndotdir = dot(normal, ray.dir);
256 if(fabs(ndotdir) < 1e-4) {
257 return false;
258 }
260 if(hit) {
261 Vec3 ptdir = pt - ray.origin;
262 float t = dot(normal, ptdir) / ndotdir;
264 hit->pos = ray.origin + ray.dir * t;
265 hit->normal = normal;
266 hit->obj = this;
267 }
268 return true;
269 }
271 float sphere_distance(const Vec3 &cent, float rad, const Vec3 &pt)
272 {
273 return length(pt - cent) - rad;
274 }
276 // TODO version which takes both radii into account
277 float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt)
278 {
279 Vec3 ab_dir = b - a;
280 float ab_len_sq = length_sq(ab_dir);
282 if(fabs(ab_len_sq) < 1e-5) {
283 // if a == b, the capsule is a sphere with radius the maximum of the capsule radii
284 return sphere_distance(a, std::max(ra, rb), pt);
285 }
286 float ab_len = sqrt(ab_len_sq);
288 Vec3 ap_dir = pt - a;
290 float t = dot(ap_dir, ab_dir / ab_len) / ab_len;
291 if(t < 0.0) {
292 return sphere_distance(a, ra, pt);
293 }
294 if(t >= 1.0) {
295 return sphere_distance(b, rb, pt);
296 }
298 Vec3 pproj = a + ab_dir * t;
299 return length(pproj - pt) - ra;
300 }
302 #if 0
303 float capsule_distance(const Vec3 &a, float ra, const Vec3 &b, float rb, const Vec3 &pt)
304 {
305 Vec3 ab_dir = b - a;
307 if(fabs(length_sq(ab_dir)) < 1e-5) {
308 // if a == b, the capsule is a sphere with radius the maximum of the capsule radii
309 return sphere_distance(a, std::max(ra, rb), pt);
310 }
311 float ab_len = length(ab_dir);
313 Vec3 ap_dir = pt - a;
314 Vec3 rotaxis = normalize(cross(ab_dir, ap_dir));
316 Mat4 rmat;
317 rmat.set_rotation(rotaxis, M_PI / 2.0);
318 Vec3 right = rmat * ab_dir / ab_len;
320 // XXX I think this check is redundant, always false, due to the cross product order
321 //assert(dot(right, ab_dir) >= 0.0);
322 if(dot(right, ab_dir) < 0.0) {
323 right = -right;
324 }
325 Vec3 aa = a + right * ra;
326 Vec3 bb = b + right * rb;
328 // project pt to the line segment bb-aa, see if the projection lies within the interval [0, 1)
329 Vec3 aabb_dir = bb - aa;
330 float aabb_len = length(aabb_dir);
331 Vec3 aap_dir = pt - aa;
333 float t = dot(aap_dir, aabb_dir / aabb_len) / aabb_len;
334 if(t < 0.0) {
335 return sphere_distance(a, ra, pt);
336 }
337 if(t >= 1.0) {
338 return sphere_distance(b, rb, pt);
339 }
341 Vec3 ppt = aa + aabb_dir * t;
342 Vec3 norm = ppt - pt;
343 float dist = length(norm);
345 if(dot(norm, right) < 0.0) {
346 // inside the cone
347 dist = -dist;
348 }
349 return dist;
350 }
351 #endif