conworlds

view src/geom.cc @ 13:283cdfa7dda2

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