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