rev |
line source |
nuclear@13
|
1 #include <algorithm>
|
nuclear@13
|
2 #include <float.h>
|
nuclear@13
|
3 #include "geom.h"
|
nuclear@13
|
4 #include "logger.h"
|
nuclear@13
|
5
|
nuclear@13
|
6 GeomObject::~GeomObject()
|
nuclear@13
|
7 {
|
nuclear@13
|
8 }
|
nuclear@13
|
9
|
nuclear@13
|
10
|
nuclear@13
|
11 Sphere::Sphere()
|
nuclear@13
|
12 {
|
nuclear@13
|
13 radius = 1.0;
|
nuclear@13
|
14 }
|
nuclear@13
|
15
|
nuclear@13
|
16 Sphere::Sphere(const Vector3 ¢, float radius)
|
nuclear@13
|
17 : center(cent)
|
nuclear@13
|
18 {
|
nuclear@13
|
19 this->radius = radius;
|
nuclear@13
|
20 }
|
nuclear@13
|
21
|
nuclear@13
|
22 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
23 {
|
nuclear@13
|
24 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
|
nuclear@13
|
25 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
|
nuclear@13
|
26
|
nuclear@13
|
27 if(!sph1 || !sph2) {
|
nuclear@13
|
28 error_log("Sphere::set_union: arguments must be spheres");
|
nuclear@13
|
29 return;
|
nuclear@13
|
30 }
|
nuclear@13
|
31
|
nuclear@13
|
32 float dist = (sph1->center - sph2->center).length();
|
nuclear@13
|
33 float surf_dist = dist - (sph1->radius + sph2->radius);
|
nuclear@13
|
34 float d1 = sph1->radius + surf_dist / 2.0;
|
nuclear@13
|
35 float d2 = sph2->radius + surf_dist / 2.0;
|
nuclear@13
|
36 float t = d1 / (d1 + d2);
|
nuclear@13
|
37
|
nuclear@13
|
38 if(t < 0.0) t = 0.0;
|
nuclear@13
|
39 if(t > 1.0) t = 1.0;
|
nuclear@13
|
40
|
nuclear@13
|
41 center = sph1->center * t + sph2->center * (1.0 - t);
|
nuclear@13
|
42 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
|
nuclear@13
|
43 }
|
nuclear@13
|
44
|
nuclear@13
|
45 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
46 {
|
nuclear@13
|
47 error_log("Sphere::intersection undefined\n");
|
nuclear@13
|
48 }
|
nuclear@13
|
49
|
nuclear@13
|
50 bool Sphere::contains(const Vector3 &pt) const
|
nuclear@13
|
51 {
|
nuclear@13
|
52 float dist_sq = (pt - center).length_sq();
|
nuclear@13
|
53 return dist_sq <= radius * radius;
|
nuclear@13
|
54 }
|
nuclear@13
|
55
|
nuclear@13
|
56 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
|
nuclear@13
|
57 {
|
nuclear@13
|
58 float a = dot_product(ray.dir, ray.dir);
|
nuclear@13
|
59 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
|
nuclear@13
|
60 2.0 * ray.dir.y * (ray.origin.y - center.y) +
|
nuclear@13
|
61 2.0 * ray.dir.z * (ray.origin.z - center.z);
|
nuclear@13
|
62 float c = dot_product(ray.origin, ray.origin) + dot_product(center, center) -
|
nuclear@13
|
63 2.0 * dot_product(ray.origin, center) - radius * radius;
|
nuclear@13
|
64
|
nuclear@13
|
65 float discr = b * b - 4.0 * a * c;
|
nuclear@13
|
66 if(discr < 1e-4) {
|
nuclear@13
|
67 return false;
|
nuclear@13
|
68 }
|
nuclear@13
|
69
|
nuclear@13
|
70 float sqrt_discr = sqrt(discr);
|
nuclear@13
|
71 float t0 = (-b + sqrt_discr) / (2.0 * a);
|
nuclear@13
|
72 float t1 = (-b - sqrt_discr) / (2.0 * a);
|
nuclear@13
|
73
|
nuclear@13
|
74 if(t0 < 1e-4)
|
nuclear@13
|
75 t0 = t1;
|
nuclear@13
|
76 if(t1 < 1e-4)
|
nuclear@13
|
77 t1 = t0;
|
nuclear@13
|
78
|
nuclear@13
|
79 float t = t0 < t1 ? t0 : t1;
|
nuclear@13
|
80 if(t < 1e-4) {
|
nuclear@13
|
81 return false;
|
nuclear@13
|
82 }
|
nuclear@13
|
83
|
nuclear@13
|
84 // fill the HitPoint structure
|
nuclear@13
|
85 if(hit) {
|
nuclear@13
|
86 hit->obj = this;
|
nuclear@13
|
87 hit->dist = t;
|
nuclear@13
|
88 hit->pos = ray.origin + ray.dir * t;
|
nuclear@13
|
89 hit->normal = (hit->pos - center) / radius;
|
nuclear@13
|
90 }
|
nuclear@13
|
91 return true;
|
nuclear@13
|
92 }
|
nuclear@13
|
93
|
nuclear@13
|
94
|
nuclear@13
|
95 AABox::AABox()
|
nuclear@13
|
96 {
|
nuclear@13
|
97 }
|
nuclear@13
|
98
|
nuclear@13
|
99 AABox::AABox(const Vector3 &vmin, const Vector3 &vmax)
|
nuclear@13
|
100 : min(vmin), max(vmax)
|
nuclear@13
|
101 {
|
nuclear@13
|
102 }
|
nuclear@13
|
103
|
nuclear@13
|
104 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
105 {
|
nuclear@13
|
106 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
|
nuclear@13
|
107 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
|
nuclear@13
|
108
|
nuclear@13
|
109 if(!box1 || !box2) {
|
nuclear@13
|
110 error_log("AABox::set_union: arguments must be AABoxes too\n");
|
nuclear@13
|
111 return;
|
nuclear@13
|
112 }
|
nuclear@13
|
113
|
nuclear@13
|
114 min.x = std::min(box1->min.x, box2->min.x);
|
nuclear@13
|
115 min.y = std::min(box1->min.y, box2->min.y);
|
nuclear@13
|
116 min.z = std::min(box1->min.z, box2->min.z);
|
nuclear@13
|
117
|
nuclear@13
|
118 max.x = std::max(box1->max.x, box2->max.x);
|
nuclear@13
|
119 max.y = std::max(box1->max.y, box2->max.y);
|
nuclear@13
|
120 max.z = std::max(box1->max.z, box2->max.z);
|
nuclear@13
|
121 }
|
nuclear@13
|
122
|
nuclear@13
|
123 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
124 {
|
nuclear@13
|
125 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
|
nuclear@13
|
126 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
|
nuclear@13
|
127
|
nuclear@13
|
128 if(!box1 || !box2) {
|
nuclear@13
|
129 error_log("AABox::set_intersection: arguments must be AABoxes too\n");
|
nuclear@13
|
130 return;
|
nuclear@13
|
131 }
|
nuclear@13
|
132
|
nuclear@13
|
133 for(int i=0; i<3; i++) {
|
nuclear@13
|
134 min[i] = std::max(box1->min[i], box2->min[i]);
|
nuclear@13
|
135 max[i] = std::min(box1->max[i], box2->max[i]);
|
nuclear@13
|
136
|
nuclear@13
|
137 if(max[i] < min[i]) {
|
nuclear@13
|
138 max[i] = min[i];
|
nuclear@13
|
139 }
|
nuclear@13
|
140 }
|
nuclear@13
|
141 }
|
nuclear@13
|
142
|
nuclear@13
|
143 bool AABox::contains(const Vector3 &pt) const
|
nuclear@13
|
144 {
|
nuclear@13
|
145 return pt.x >= min.x && pt.x <= max.x &&
|
nuclear@13
|
146 pt.y >= min.y && pt.y <= max.y &&
|
nuclear@13
|
147 pt.z >= min.z && pt.z <= max.z;
|
nuclear@13
|
148 }
|
nuclear@13
|
149
|
nuclear@13
|
150 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
|
nuclear@13
|
151 {
|
nuclear@13
|
152 Vector3 param[2] = {min, max};
|
nuclear@13
|
153 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
|
nuclear@13
|
154 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
|
nuclear@13
|
155
|
nuclear@13
|
156 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
|
nuclear@13
|
157 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
|
nuclear@13
|
158 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
|
nuclear@13
|
159 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
|
nuclear@13
|
160
|
nuclear@13
|
161 if(tmin > tymax || tymin > tmax) {
|
nuclear@13
|
162 return false;
|
nuclear@13
|
163 }
|
nuclear@13
|
164 if(tymin > tmin) {
|
nuclear@13
|
165 tmin = tymin;
|
nuclear@13
|
166 }
|
nuclear@13
|
167 if(tymax < tmax) {
|
nuclear@13
|
168 tmax = tymax;
|
nuclear@13
|
169 }
|
nuclear@13
|
170
|
nuclear@13
|
171 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
|
nuclear@13
|
172 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
|
nuclear@13
|
173
|
nuclear@13
|
174 if(tmin > tzmax || tzmin > tmax) {
|
nuclear@13
|
175 return false;
|
nuclear@13
|
176 }
|
nuclear@13
|
177 if(tzmin > tmin) {
|
nuclear@13
|
178 tmin = tzmin;
|
nuclear@13
|
179 }
|
nuclear@13
|
180 if(tzmax < tmax) {
|
nuclear@13
|
181 tmax = tzmax;
|
nuclear@13
|
182 }
|
nuclear@13
|
183
|
nuclear@13
|
184 float t = tmin < 1e-4 ? tmax : tmin;
|
nuclear@13
|
185 if(t >= 1e-4) {
|
nuclear@13
|
186
|
nuclear@13
|
187 if(hit) {
|
nuclear@13
|
188 hit->obj = this;
|
nuclear@13
|
189 hit->dist = t;
|
nuclear@13
|
190 hit->pos = ray.origin + ray.dir * t;
|
nuclear@13
|
191
|
nuclear@13
|
192 float min_dist = FLT_MAX;
|
nuclear@13
|
193 Vector3 offs = min + (max - min) / 2.0;
|
nuclear@13
|
194 Vector3 local_hit = hit->pos - offs;
|
nuclear@13
|
195
|
nuclear@13
|
196 static const Vector3 axis[] = {
|
nuclear@13
|
197 Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)
|
nuclear@13
|
198 };
|
nuclear@13
|
199 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
|
nuclear@13
|
200
|
nuclear@13
|
201 for(int i=0; i<3; i++) {
|
nuclear@13
|
202 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
|
nuclear@13
|
203 if(dist < min_dist) {
|
nuclear@13
|
204 min_dist = dist;
|
nuclear@13
|
205 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
|
nuclear@13
|
206 //hit->texcoord = Vector2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
|
nuclear@13
|
207 }
|
nuclear@13
|
208 }
|
nuclear@13
|
209 }
|
nuclear@13
|
210 return true;
|
nuclear@13
|
211 }
|
nuclear@13
|
212 return false;
|
nuclear@13
|
213
|
nuclear@13
|
214 }
|
nuclear@13
|
215
|
nuclear@13
|
216 Plane::Plane()
|
nuclear@13
|
217 : normal(0.0, 1.0, 0.0)
|
nuclear@13
|
218 {
|
nuclear@13
|
219 }
|
nuclear@13
|
220
|
nuclear@13
|
221 Plane::Plane(const Vector3 &p, const Vector3 &norm)
|
nuclear@13
|
222 : pt(p)
|
nuclear@13
|
223 {
|
nuclear@13
|
224 normal = norm.normalized();
|
nuclear@13
|
225 }
|
nuclear@13
|
226
|
nuclear@13
|
227 Plane::Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
|
nuclear@13
|
228 : pt(p1)
|
nuclear@13
|
229 {
|
nuclear@13
|
230 normal = cross_product(p2 - p1, p3 - p1).normalized();
|
nuclear@13
|
231 }
|
nuclear@13
|
232
|
nuclear@13
|
233 Plane::Plane(const Vector3 &normal, float dist)
|
nuclear@13
|
234 {
|
nuclear@13
|
235 this->normal = normal.normalized();
|
nuclear@13
|
236 pt = this->normal * dist;
|
nuclear@13
|
237 }
|
nuclear@13
|
238
|
nuclear@13
|
239 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
240 {
|
nuclear@13
|
241 error_log("Plane::set_union undefined\n");
|
nuclear@13
|
242 }
|
nuclear@13
|
243
|
nuclear@13
|
244 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
|
nuclear@13
|
245 {
|
nuclear@13
|
246 error_log("Plane::set_intersection undefined\n");
|
nuclear@13
|
247 }
|
nuclear@13
|
248
|
nuclear@13
|
249 bool Plane::contains(const Vector3 &pt) const
|
nuclear@13
|
250 {
|
nuclear@13
|
251 return false; // TODO: maybe define containment as half-space containment?
|
nuclear@13
|
252 }
|
nuclear@13
|
253
|
nuclear@13
|
254 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
|
nuclear@13
|
255 {
|
nuclear@13
|
256 float ndotdir = dot_product(normal, ray.dir);
|
nuclear@13
|
257 if(fabs(ndotdir) < 1e-4) {
|
nuclear@13
|
258 return false;
|
nuclear@13
|
259 }
|
nuclear@13
|
260
|
nuclear@13
|
261 if(hit) {
|
nuclear@13
|
262 Vector3 ptdir = pt - ray.origin;
|
nuclear@13
|
263 float t = dot_product(normal, ptdir) / ndotdir;
|
nuclear@13
|
264
|
nuclear@13
|
265 hit->pos = ray.origin + ray.dir * t;
|
nuclear@13
|
266 hit->normal = normal;
|
nuclear@13
|
267 hit->obj = this;
|
nuclear@13
|
268 }
|
nuclear@13
|
269 return true;
|
nuclear@13
|
270 }
|