dbf-halloween2015
view src/geom.cc @ 0:50683c78264e
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:09:12 +0200 |
parents | |
children |
line source
1 #include <algorithm>
2 #include <float.h>
3 #include "geom.h"
5 GeomObject::~GeomObject()
6 {
7 }
10 Sphere::Sphere()
11 {
12 radius = 1.0;
13 }
15 Sphere::Sphere(const Vector3 ¢, float radius)
16 : center(cent)
17 {
18 this->radius = radius;
19 }
21 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
22 {
23 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
24 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
26 if(!sph1 || !sph2) {
27 fprintf(stderr, "Sphere::set_union: arguments must be spheres");
28 return;
29 }
31 float dist = (sph1->center - sph2->center).length();
32 float surf_dist = dist - (sph1->radius + sph2->radius);
33 float d1 = sph1->radius + surf_dist / 2.0;
34 float d2 = sph2->radius + surf_dist / 2.0;
35 float t = d1 / (d1 + d2);
37 if(t < 0.0) t = 0.0;
38 if(t > 1.0) t = 1.0;
40 center = sph1->center * t + sph2->center * (1.0 - t);
41 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
42 }
44 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
45 {
46 fprintf(stderr, "Sphere::intersection undefined\n");
47 }
49 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
50 {
51 float a = dot_product(ray.dir, ray.dir);
52 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
53 2.0 * ray.dir.y * (ray.origin.y - center.y) +
54 2.0 * ray.dir.z * (ray.origin.z - center.z);
55 float c = dot_product(ray.origin, ray.origin) + dot_product(center, center) -
56 2.0 * dot_product(ray.origin, center) - radius * radius;
58 float discr = b * b - 4.0 * a * c;
59 if(discr < 1e-4) {
60 return false;
61 }
63 float sqrt_discr = sqrt(discr);
64 float t0 = (-b + sqrt_discr) / (2.0 * a);
65 float t1 = (-b - sqrt_discr) / (2.0 * a);
67 if(t0 < 1e-4)
68 t0 = t1;
69 if(t1 < 1e-4)
70 t1 = t0;
72 float t = t0 < t1 ? t0 : t1;
73 if(t < 1e-4) {
74 return false;
75 }
77 // fill the HitPoint structure
78 if(hit) {
79 hit->obj = this;
80 hit->dist = t;
81 hit->pos = ray.origin + ray.dir * t;
82 hit->normal = (hit->pos - center) / radius;
83 }
84 return true;
85 }
88 AABox::AABox()
89 {
90 }
92 AABox::AABox(const Vector3 &vmin, const Vector3 &vmax)
93 : min(vmin), max(vmax)
94 {
95 }
97 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
98 {
99 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
100 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
102 if(!box1 || !box2) {
103 fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
104 return;
105 }
107 min.x = std::min(box1->min.x, box2->min.x);
108 min.y = std::min(box1->min.y, box2->min.y);
109 min.z = std::min(box1->min.z, box2->min.z);
111 max.x = std::max(box1->max.x, box2->max.x);
112 max.y = std::max(box1->max.y, box2->max.y);
113 max.z = std::max(box1->max.z, box2->max.z);
114 }
116 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
117 {
118 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
119 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
121 if(!box1 || !box2) {
122 fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
123 return;
124 }
126 for(int i=0; i<3; i++) {
127 min[i] = std::max(box1->min[i], box2->min[i]);
128 max[i] = std::min(box1->max[i], box2->max[i]);
130 if(max[i] < min[i]) {
131 max[i] = min[i];
132 }
133 }
134 }
136 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
137 {
138 Vector3 param[2] = {min, max};
139 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
140 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
142 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
143 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
144 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
145 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
147 if(tmin > tymax || tymin > tmax) {
148 return false;
149 }
150 if(tymin > tmin) {
151 tmin = tymin;
152 }
153 if(tymax < tmax) {
154 tmax = tymax;
155 }
157 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
158 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
160 if(tmin > tzmax || tzmin > tmax) {
161 return false;
162 }
163 if(tzmin > tmin) {
164 tmin = tzmin;
165 }
166 if(tzmax < tmax) {
167 tmax = tzmax;
168 }
170 float t = tmin < 1e-4 ? tmax : tmin;
171 if(t >= 1e-4) {
173 if(hit) {
174 hit->obj = this;
175 hit->dist = t;
176 hit->pos = ray.origin + ray.dir * t;
178 float min_dist = FLT_MAX;
179 Vector3 offs = min + (max - min) / 2.0;
180 Vector3 local_hit = hit->pos - offs;
182 static const Vector3 axis[] = {
183 Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)
184 };
185 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
187 for(int i=0; i<3; i++) {
188 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
189 if(dist < min_dist) {
190 min_dist = dist;
191 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
192 //hit->texcoord = Vector2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
193 }
194 }
195 }
196 return true;
197 }
198 return false;
200 }
202 Plane::Plane()
203 : normal(0.0, 1.0, 0.0)
204 {
205 }
207 Plane::Plane(const Vector3 &p, const Vector3 &norm)
208 : pt(p)
209 {
210 normal = norm.normalized();
211 }
213 Plane::Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
214 : pt(p1)
215 {
216 normal = cross_product(p2 - p1, p3 - p1).normalized();
217 }
219 Plane::Plane(const Vector3 &normal, float dist)
220 {
221 this->normal = normal.normalized();
222 pt = this->normal * dist;
223 }
225 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
226 {
227 fprintf(stderr, "Plane::set_union undefined\n");
228 }
230 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
231 {
232 fprintf(stderr, "Plane::set_intersection undefined\n");
233 }
235 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
236 {
237 float ndotdir = dot_product(normal, ray.dir);
238 if(fabs(ndotdir) < 1e-4) {
239 return false;
240 }
242 if(hit) {
243 Vector3 ptdir = pt - ray.origin;
244 float t = dot_product(normal, ptdir) / ndotdir;
246 hit->pos = ray.origin + ray.dir * t;
247 hit->normal = normal;
248 hit->obj = this;
249 }
250 return true;
251 }