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