Complete integration of ref_weights with spatial info. updating.
[Ale.git] / d3 / pt.h
blobe42d6cfb7a81ac93e00d59e8a75b0ff25939cc99
1 // Copyright 2005 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * d3/et.h: Represent 3D->2D projective transformations.
25 #ifndef __pt_h__
26 #define __pt_h__
28 #include "space.h"
29 #include "et.h"
32 * Structure to describe a 3D->2D projective transformation. 3D information is
33 * preserved by adding a depth element to the result.
35 * The following coordinate systems are considered:
37 * P: projective
38 * C: local cartesian
39 * W: world
42 struct pt {
43 private:
44 d2::transformation t;
45 et euclidean;
46 ale_real _view_angle; /* XXX: should be ale_pos */
47 ale_pos scale_factor;
48 ale_pos diag_per_depth;
50 public:
53 * Constructor
56 pt() {
57 _view_angle = M_PI / 4;
58 scale_factor = 1;
59 diag_per_depth = 0;
62 pt(d2::transformation t, et e, ale_real va, ale_pos sf = 1) {
63 this->t = t;
64 euclidean = e;
65 _view_angle = va;
66 scale_factor = sf;
67 diag_per_depth = 0;
71 * Get euclidean transformation reference.
74 et &e() {
75 return euclidean;
79 * Modify scale factor
81 void scale(ale_pos sf) {
82 scale_factor = sf;
86 * Modify or get view angle
88 void view_angle(ale_pos va) {
89 diag_per_depth = 0;
90 _view_angle = va;
93 ale_pos view_angle() {
94 return _view_angle;
98 * Get the 2D scale factor
100 ale_pos scale_2d() const {
101 return t.scale();
105 * Transform W to C.
107 point wc(point p) const {
108 return euclidean(p);
112 * Transform C to P for given width and height.
114 point cp_generic(point p, ale_pos w, ale_pos h) const {
116 * Divide x and y by negative z
119 p[0] /= -p[2];
120 p[1] /= -p[2];
123 * Scale x and y
126 ale_pos scaling_factor = sqrt(w*w + h*h) / (2 * tan(_view_angle / 2));
127 p[0] *= scaling_factor;
128 p[1] *= scaling_factor;
131 * Add an offset so that the upper-left corner is the origin.
134 p[0] += h / 2;
135 p[1] += w / 2;
137 return p;
141 * Transform point p.
143 struct point wp_generic(struct point p, ale_pos w, ale_pos h) const {
144 return cp_generic(wc(p), w, h);
148 * Width and height
151 ale_pos scaled_width() const {
152 return t.scaled_width() * scale_factor;
155 ale_pos scaled_height() const {
156 return t.scaled_height() * scale_factor;
159 int scaled_in_bounds(point p) const {
160 return (p[0] >= 0 && p[0] <= scaled_height() - 1
161 && p[1] >= 0 && p[1] <= scaled_width() - 1);
164 ale_pos unscaled_width() const {
165 return t.unscaled_width() * scale_factor;
168 ale_pos unscaled_height() const {
169 return t.unscaled_height() * scale_factor;
173 * Scaled transforms
176 point cp_scaled(point p) const {
177 return cp_generic(p, scaled_width(), scaled_height());
180 point wp_scaled(point p) const {
181 return wp_generic(p, scaled_width(), scaled_height());
185 * Unscaled transforms
188 point cp_unscaled(point p) const {
189 return cp_generic(p, unscaled_width(), unscaled_height());
192 point wp_unscaled(point p) const {
193 return wp_generic(p, unscaled_width(), unscaled_height());
197 * Transform P to C.
199 point pc_generic(point p, ale_pos w, ale_pos h) const {
201 * Subtract offset
204 p[0] -= h / 2;
205 p[1] -= w / 2;
208 * Scale x and y
211 ale_pos scaling_factor = sqrt(w*w + h*h) / (2 * tan(_view_angle / 2));
212 p[0] /= scaling_factor;
213 p[1] /= scaling_factor;
216 * Multiply x and y by negative z
219 p[0] *= -p[2];
220 p[1] *= -p[2];
222 return p;
226 * Transform C to W
228 point cw(point p) const {
229 return euclidean.inverse_transform(p);
233 * Transform P to W
235 point pw_generic(point p, ale_pos w, ale_pos h) const {
236 return cw(pc_generic(p, w, h));
240 * Inverse transforms for scaled points.
243 point pc_scaled(point p) const {
244 return pc_generic(p, scaled_width(), scaled_height());
247 point pw_scaled(point p) const {
248 return pw_generic(p, scaled_width(), scaled_height());
252 * Inverse transforms for unscaled points.
255 point pc_unscaled(point p) const {
256 return pc_generic(p, unscaled_width(), unscaled_height());
259 point pw_unscaled(point p) const {
260 return pw_generic(p, unscaled_width(), unscaled_height());
264 * Density calculation
267 ale_pos c_density_scaled(point p) const {
268 ale_pos one_density = 1 / (pc_scaled(point(0, 0, -1)).lengthto(pc_scaled(point(0, 1, -1)))
269 * pc_scaled(point(0, 0, -1)).lengthto(pc_scaled(point(1, 0, -1))));
271 return one_density / (p[2] * p[2]);
274 ale_pos w_density_scaled(point p) const {
275 return c_density_scaled(wc(p));
278 ale_pos w_density_scaled_max(point w0, point w1, point w2) {
279 point c0 = wc(w0);
280 point c1 = wc(w1);
281 point c2 = wc(w2);
284 * Select the point closest to the camera.
287 if (c0[2] > c1[2] && c0[2] > c2[2])
288 return c_density_scaled(c0);
289 else if (c1[2] > c2[2])
290 return c_density_scaled(c1);
291 else
292 return c_density_scaled(c2);
295 private:
296 void calculate_diag_per_depth() {
297 if (diag_per_depth != 0)
298 return;
299 ale_pos w = unscaled_width();
300 ale_pos h = unscaled_height();
302 diag_per_depth = sqrt(2) * (2 * tan(_view_angle / 2)) / sqrt(w*w + h*h);
305 public:
308 * Get a trilinear coordinate for a given position in the world and
309 * a given 2D diagonal distance.
311 ale_pos trilinear_coordinate(point w, ale_pos diagonal) {
312 calculate_diag_per_depth();
314 ale_pos depth = wc(w)[2];
316 ale_pos coord = log(diagonal / (depth * diag_per_depth)) / log(2);
318 return coord;
322 * Get a trilinear coordinate for a given subspace.
324 ale_pos trilinear_coordinate(const space::traverse &st) {
325 point min = st.get_min();
326 point max = st.get_max();
327 point avg = (min + max) / (ale_pos) 2;
329 ale_pos diagonal = min.lengthto(max) * sqrt(2) / sqrt(3);
331 return trilinear_coordinate(avg, diagonal);
335 * Get a diagonal distance for a given position in the world
336 * and a given trilinear coordinate.
338 ale_pos diagonal_distance(point w, ale_pos coordinate) {
339 calculate_diag_per_depth();
341 ale_pos depth = wc(w)[2];
342 ale_pos diagonal = pow(2, coordinate) * depth * diag_per_depth;
344 return diagonal;
348 * Get bounding box for projection of a subspace.
351 void get_view_local_bb(const space::traverse &st, point bb[2]) {
353 point min = point::posinf();
354 point max = point::neginf();
356 point wbb[2] = { st.get_min(), st.get_max() };
359 for (int x = 0; x < 2; x++)
360 for (int y = 0; y < 2; y++)
361 for (int z = 0; z < 2; z++) {
362 point p = wp_scaled(point(wbb[x][0], wbb[y][1], wbb[z][2]));
364 if (p[0] < min[0])
365 min[0] = p[0];
366 if (p[0] > max[0])
367 max[0] = p[0];
368 if (p[1] < min[1])
369 min[1] = p[1];
370 if (p[1] > max[1])
371 max[1] = p[1];
372 if (p[2] < min[2])
373 min[2] = p[2];
374 if (p[2] > max[2])
375 max[2] = p[2];
379 * Clip bounding box to image extents.
382 if (min[0] < 0)
383 min[0] = 0;
384 if (min[1] < 0)
385 min[1] = 0;
386 if (max[0] > scaled_height() - 1)
387 max[0] = scaled_height() - 1;
388 if (max[1] > scaled_width() - 1)
389 max[1] = scaled_width() - 1;
391 bb[0] = min;
392 bb[1] = max;
396 * Get the in-bounds centroid for a subspace, if one exists.
399 point centroid(const space::traverse &t) {
400 point bb[2];
402 get_view_local_bb(t, bb);
404 point min = bb[0];
405 point max = bb[1];
407 for (int d = 0; d < 2; d++)
408 if (min[d] > max[d])
409 return point::undefined();
411 if (min[2] > 0)
412 return point::undefined();
414 return (bb[0] + bb[1]) / 2;
418 * Get the local space origin in world space.
421 point origin() {
422 return cw(point(0, 0, 0));
427 #endif