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