doc/web: Place examples section more visibly.
[Ale.git] / d2 / trans_single.h
blob093718ca6e74035ba4c49027a5d7c9aad5b45af2
1 // Copyright 2002, 2004 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 and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (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 * trans_single.h: Represent transformations of the kind q = c(b^-1(p)),
23 * where p is a point in the source coordinate system, q is a point in the
24 * target coordinate system, b^-1 is a transformation correcting barrel
25 * distortion, and c is a transformation of projective or Euclidean type.
26 * (Note that ^-1 in this context indicates the function inverse rather than
27 * the exponential.)
30 #ifndef __trans_single_h__
31 #define __trans_single_h__
33 #include "trans_abstract.h"
36 * transformation: a structure to describe a transformation of kind q =
37 * c(b^-1(p)), where p is a point in the source coordinate system, q is a point
38 * in the target coordinate system, b^-1 is a transformation correcting barrel
39 * distortion, and c is a projective or Euclidean transformation. (Note that
40 * ^-1 in this case indicates a function inverse, not exponentiation.) Data
41 * elements are divided into those describing barrel distortion correction and
42 * those describing projective/Euclidean transformations.
44 * Barrel distortion correction estimates barrel distortion using polynomial
45 * functions of distance from the center of an image, following (roughly) the
46 * example set by Helmut Dersch in his PanoTools software:
48 * http://www.path.unimelb.edu.au/~dersch/barrel/barrel.html
50 * Projective transformation data member names roughly correspond to a typical
51 * treatment of projective transformations from:
53 * Heckbert, Paul. "Projective Mappings for Image Warping." Excerpted
54 * from his Master's Thesis (UC Berkeley, 1989). 1995.
56 * http://www.cs.cmu.edu/afs/cs/project/classes-ph/862.95/www/notes/proj.ps
58 * For convenience, Heckbert's 'x' and 'y' are noted here numerically by '0'
59 * and '1', respectively. 'x0' is denoted 'x[0][0]'; 'y0' is 'x[0][1]'.
61 * eu[i] are the parameters for euclidean transformations.
63 * We consider points to be transformed as homogeneous coordinate vectors
64 * multiplied on the right of the transformation matrix, and so we consider the
65 * transformation matrix as
67 * - -
68 * | a b c |
69 * | d e f |
70 * | g h i |
71 * - -
73 * where element i is always equal to 1.
76 struct trans_single : public trans_abstract {
77 private:
78 point x[4];
79 ale_pos eu[3];
80 mutable ale_pos a, b, c, d, e, f, g, h; // matrix
81 mutable ale_pos _a, _b, _c, _d, _e, _f, _g, _h; // matrix inverse
82 int _is_projective;
83 mutable int resultant_memo;
84 mutable int resultant_inverse_memo;
87 * Calculate resultant matrix values.
89 void resultant() const {
92 * If we already know the answers, don't bother calculating
93 * them again.
96 if (resultant_memo)
97 return;
99 int ale_pos_casting = ale_pos_casting_status();
100 ale_pos_enable_casting();
102 if (_is_projective) {
105 * Calculate resultant matrix values for a general
106 * projective transformation given that we are mapping
107 * from the source domain of dimension input_height *
108 * input_width to a specified arbitrary quadrilateral.
109 * Follow the calculations outlined in the document by
110 * Paul Heckbert cited above for the case in which the
111 * source domain is a unit square and then divide to
112 * correct for the scale factor in each dimension.
116 * First, perform calculations as outlined in Heckbert.
119 ale_pos delta_01 = x[1][0] - x[2][0];
120 ale_pos delta_02 = x[3][0] - x[2][0];
121 ale_pos sigma_0 = x[0][0] - x[1][0] + x[2][0] - x[3][0];
122 ale_pos delta_11 = x[1][1] - x[2][1];
123 ale_pos delta_12 = x[3][1] - x[2][1];
124 ale_pos sigma_1 = x[0][1] - x[1][1] + x[2][1] - x[3][1];
126 g = (sigma_0 * delta_12 - sigma_1 * delta_02)
127 / (delta_01 * delta_12 - delta_11 * delta_02);
129 h = (delta_01 * sigma_1 - delta_11 * sigma_0 )
130 / (delta_01 * delta_12 - delta_11 * delta_02);
132 a = (x[1][0] - x[0][0] + g * x[1][0]);
133 b = (x[3][0] - x[0][0] + h * x[3][0]);
134 c = x[0][0];
136 d = (x[1][1] - x[0][1] + g * x[1][1]);
137 e = (x[3][1] - x[0][1] + h * x[3][1]);
138 f = x[0][1];
141 * Finish by scaling so that our transformation maps
142 * from a rectangle of width and height matching the
143 * width and height of the input image.
146 a /= input_height;
147 b /= input_width;
148 d /= input_height;
149 e /= input_width;
150 g /= input_height;
151 h /= input_width;
153 } else {
156 * Calculate matrix values for a euclidean
157 * transformation.
159 * We want to translate the image center by (eu[0],
160 * eu[1]) and rotate the image about the center by
161 * eu[2] degrees. This is equivalent to the following
162 * sequence of affine transformations applied to the
163 * point to be transformed:
165 * translate by (-h/2, -w/2)
166 * rotate by eu[2] degrees about the origin
167 * translate by (h/2, w/2)
168 * translate by (eu[0], eu[1])
170 * The matrix assigned below represents the result of
171 * combining all of these transformations. Matrix
172 * elements g and h are always zero in an affine
173 * transformation.
176 ale_pos theta = (double) eu[2] * M_PI / 180;
178 a = (double) cos(theta) * (double) scale_factor;
179 b = (double) sin(theta) * (double) scale_factor;
180 c = 0.5 * ((double) input_height * ((double) scale_factor - (double) a)
181 - (double) input_width * (double) b) + (double) eu[0]
182 * (double) scale_factor;
183 d = -b;
184 e = a;
185 f = 0.5 * ((double) input_height * (double) b
186 + (double) input_width * ((double) scale_factor
187 - (double) a))
188 + (double) eu[1] * (double) scale_factor;
189 g = 0;
190 h = 0;
193 resultant_memo = 1;
195 if (!ale_pos_casting)
196 ale_pos_disable_casting();
200 * Calculate the inverse transform matrix values.
202 void resultant_inverse () const {
205 * If we already know the answers, don't bother calculating
206 * them again.
209 if (resultant_inverse_memo)
210 return;
212 resultant();
214 int ale_pos_casting = ale_pos_casting_status();
215 ale_pos_enable_casting();
218 * For projective transformations, we calculate
219 * the inverse of the forward transformation
220 * matrix.
223 double scale = (double) a * (double) e - (double) b * (double) d;
225 _a = ((double) e * 1 - (double) f * (double) h) / scale;
226 _b = ((double) h * (double) c - 1 * (double) b) / scale;
227 _c = ((double) b * (double) f - (double) c * (double) e) / scale;
228 _d = ((double) f * (double) g - (double) d * 1) / scale;
229 _e = (1 * (double) a - (double) g * (double) c) / scale;
230 _f = ((double) c * (double) d - (double) a * (double) f) / scale;
231 _g = ((double) d * (double) h - (double) e * (double) g) / scale;
232 _h = ((double) g * (double) b - (double) h * (double) a) / scale;
234 resultant_inverse_memo = 1;
236 if (!ale_pos_casting)
237 ale_pos_disable_casting();
240 public:
242 trans_single &operator=(const trans_single &ta) {
244 this->trans_abstract::operator=(*((trans_abstract *) &ta));
246 for (int i = 0; i < 4; i++) {
247 x[i] = ta.x[i];
250 for (int i = 0; i < 3; i++) {
251 eu[i] = ta.eu[i];
254 _is_projective = ta._is_projective;
256 resultant_memo = 0;
257 resultant_inverse_memo = 0;
259 return *this;
262 trans_single(const trans_single &ta) {
263 operator=(ta);
266 trans_single() {
271 * Returns non-zero if the transformation might be non-Euclidean.
273 int is_projective() const {
274 return _is_projective;
278 * Projective/Euclidean transformation
280 struct point pe(struct point p) const {
281 struct point result;
283 resultant();
285 result[0] = (a * p[0] + b * p[1] + c)
286 / (g * p[0] + h * p[1] + 1);
287 result[1] = (d * p[0] + e * p[1] + f)
288 / (g * p[0] + h * p[1] + 1);
290 return result;
294 * Projective/Euclidean inverse
296 struct point pei(struct point p) const {
297 struct point result;
299 resultant_inverse();
301 result[0] = (_a * p[0] + _b * p[1] + _c)
302 / (_g * p[0] + _h * p[1] + 1);
303 result[1] = (_d * p[0] + _e * p[1] + _f)
304 / (_g * p[0] + _h * p[1] + 1);
306 return result;
310 #if 0
312 * operator() is the transformation operator.
314 struct point operator()(struct point p) {
315 return transform(p);
317 #endif
320 * Calculate projective transformation parameters from a euclidean
321 * transformation.
323 void eu_to_gpt() {
325 assert(!_is_projective);
327 x[0] = transform_unscaled(point( 0 , 0 ) );
328 x[1] = transform_unscaled(point( input_height, 0 ) );
329 x[2] = transform_unscaled(point( input_height, input_width ) );
330 x[3] = transform_unscaled(point( 0 , input_width ) );
332 resultant_memo = 0;
333 resultant_inverse_memo = 0;
335 _is_projective = 1;
339 * Calculate euclidean identity transform for a given image.
341 static struct trans_single eu_identity(const image *i = NULL, ale_pos scale_factor = 1) {
342 struct trans_single r;
344 r.resultant_memo = 0;
345 r.resultant_inverse_memo = 0;
347 r.eu[0] = 0;
348 r.eu[1] = 0;
349 r.eu[2] = 0;
350 r.input_width = i ? i->width() : 2;
351 r.input_height = i ? i->height() : 2;
352 r.scale_factor = scale_factor;
354 r._is_projective = 0;
356 r.bd_set(0, (ale_pos *) NULL);
358 return r;
362 * Calculate projective identity transform for a given image.
364 static trans_single gpt_identity(const image *i, ale_pos scale_factor) {
365 struct trans_single r = eu_identity(i, scale_factor);
367 r.eu_to_gpt();
369 return r;
373 * Modify a euclidean transform in the indicated manner.
375 void eu_modify(int i1, ale_pos diff) {
377 assert(!_is_projective);
379 resultant_memo = 0;
380 resultant_inverse_memo = 0;
382 if (i1 < 2)
383 eu[i1] += diff / scale_factor;
384 else
385 eu[i1] += diff;
390 * Rotate about a given point in the original reference frame.
392 void eu_rotate_about_scaled(point center, ale_pos diff) {
393 assert(center.defined());
394 point fixpoint = scaled_inverse_transform(center);
395 eu_modify(2, diff);
396 point offset = center - transform_scaled(fixpoint);
398 eu_modify(0, offset[0]);
399 eu_modify(1, offset[1]);
403 * Modify all euclidean parameters at once.
405 void eu_set(ale_pos eu[3]) {
407 resultant_memo = 0;
408 resultant_inverse_memo = 0;
410 this->eu[0] = eu[0] / scale_factor;
411 this->eu[1] = eu[1] / scale_factor;
412 this->eu[2] = eu[2];
414 if (_is_projective) {
415 _is_projective = 0;
416 eu_to_gpt();
422 * Get the specified euclidean parameter
424 ale_pos eu_get(int param) const {
425 assert (!_is_projective);
426 assert (param >= 0);
427 assert (param < 3);
429 if (param < 2)
430 return eu[param] * scale_factor;
431 else
432 return eu[param];
436 * Modify a projective transform in the indicated manner.
438 void gpt_modify(int i1, int i2, ale_pos diff) {
440 assert (_is_projective);
442 resultant_memo = 0;
443 resultant_inverse_memo = 0;
445 x[i2][i1] += diff;
450 * Modify a projective transform according to the group operation.
452 void gr_modify(int i1, int i2, ale_pos diff) {
453 assert (_is_projective);
454 assert (i1 == 0 || i1 == 1);
456 point diff_vector = (i1 == 0)
457 ? point(diff, 0)
458 : point(0, diff);
460 trans_single t = *this;
462 t.resultant_memo = 0;
463 t.resultant_inverse_memo = 0;
464 t.input_height = (unsigned int) scaled_height();
465 t.input_width = (unsigned int) scaled_width();
466 t.scale_factor = 1;
467 t.bd_set(0, (ale_pos *) NULL);
469 resultant_memo = 0;
470 resultant_inverse_memo = 0;
472 x[i2] = t.transform_scaled(t.scaled_inverse_transform(x[i2]) + diff_vector);
476 * Modify all projective parameters at once.
478 void gpt_set(point x[4]) {
480 resultant_memo = 0;
481 resultant_inverse_memo = 0;
483 _is_projective = 1;
485 for (int i = 0; i < 4; i++)
486 this->x[i] = x[i];
490 void gpt_set(point x1, point x2, point x3, point x4) {
491 point x[4] = {x1, x2, x3, x4};
492 gpt_set(x);
495 void snap(ale_pos interval) {
496 for (int i = 0; i < 4; i++)
497 for (int j = 0; j < 2; j++)
498 x[i][j] = round(x[i][j] / interval) * interval;
500 interval /= scale();
502 for (int i = 0; i < 2; i++)
503 eu[i] = round(eu[i] / interval) * interval;
505 interval *= 2 * 180 / M_PI
506 / sqrt(pow(unscaled_height(), 2)
507 + pow(unscaled_width(), 2));
509 eu[2] = round(eu[2] / interval) * interval;
511 resultant_memo = 0;
512 resultant_inverse_memo = 0;
516 * Get the specified projective parameter
518 point gpt_get(int point) const {
519 assert (_is_projective);
520 assert (point >= 0);
521 assert (point < 4);
523 return x[point];
527 * Get the specified projective parameter
529 ale_pos gpt_get(int point, int dim) {
530 assert (_is_projective);
531 assert (dim >= 0);
532 assert (dim < 2);
534 return gpt_get(point)[dim];
538 * Translate by a given amount
540 void translate(point p) {
542 resultant_memo = 0;
543 resultant_inverse_memo = 0;
545 if (_is_projective)
546 for (int i = 0; i < 4; i++)
547 x[i] += p;
548 else {
549 eu[0] += p[0] / scale_factor;
550 eu[1] += p[1] / scale_factor;
555 * Rotate by a given amount about a given point.
557 void rotate(point center, ale_pos degrees) {
558 if (_is_projective)
559 for (int i = 0; i <= 4; i++) {
560 ale_pos radians = (double) degrees * M_PI / (double) 180;
562 x[i] -= center;
563 x[i] = point(
564 (double) x[i][0] * cos(radians) + (double) x[i][1] * sin(radians),
565 (double) x[i][1] * cos(radians) - (double) x[i][0] * sin(radians));
566 x[i] += center;
568 resultant_memo = 0;
569 resultant_inverse_memo = 0;
570 } else {
571 assert(center.defined());
572 point fixpoint = scaled_inverse_transform(center);
573 eu_modify(2, degrees);
574 point offset = center - transform_scaled(fixpoint);
576 eu_modify(0, offset[0]);
577 eu_modify(1, offset[1]);
581 void reset_memos() {
582 resultant_memo = 0;
583 resultant_inverse_memo = 0;
588 * Rescale a transform with a given factor.
590 void specific_rescale(ale_pos factor) {
592 resultant_memo = 0;
593 resultant_inverse_memo = 0;
595 if (_is_projective) {
597 for (int i = 0; i < 4; i++)
598 for (int j = 0; j < 2; j++)
599 x[i][j] *= factor;
601 } else {
602 #if 0
604 * Euclidean scaling is handled in resultant().
606 for (int i = 0; i < 2; i++)
607 eu[i] *= factor;
608 #endif
613 * Set the dimensions of the image.
615 void specific_set_dimensions(const image *im) {
617 int new_height = (int) im->height();
618 int new_width = (int) im->width();
620 if (_is_projective) {
623 * If P(w, x, y, z) is a projective transform mapping
624 * the corners of the unit square to points w, x, y, z,
625 * and Q(w, x, y, z)(i, j) == P(w, x, y, z)(ai, bj),
626 * then we have:
628 * Q(w, x, y, z) == P( P(w, x, y, z)(0, 0),
629 * P(w, x, y, z)(a, 0),
630 * P(w, x, y, z)(a, b),
631 * P(w, x, y, z)(0, b) )
633 * If we note that P(w, x, y, z)(0, 0) == w, we can
634 * omit a calculation.
636 * We take 'a' as the ratio (new_height /
637 * old_height) and 'b' as the ratio (new_width /
638 * old_width) if we want the common upper left-hand
639 * region of both new and old images to map to the same
640 * area.
642 * Since we're not mapping from the unit square, we
643 * take 'a' as new_height and 'b' as new_width to
644 * accommodate the existing scale factor.
647 point _x, _y, _z;
649 _x = transform_unscaled(point(new_height, 0 ));
650 _y = transform_unscaled(point(new_height, new_width));
651 _z = transform_unscaled(point( 0 , new_width));
653 x[1] = _x;
654 x[2] = _y;
655 x[3] = _z;
661 * Modify all projective parameters at once. Accommodate bugs in the
662 * version 0 transformation file handler (ALE versions 0.4.0p1 and
663 * earlier). This code is only called when using a transformation data
664 * file created with an old version of ALE.
666 void gpt_v0_set(point x[4]) {
668 _is_projective = 1;
671 * This is slightly modified code from version
672 * 0.4.0p1.
675 ale_pos delta_01 = x[1][0] - x[2][0];
676 ale_pos delta_02 = x[3][0] - x[2][0];
677 ale_pos sigma_0 = x[0][0] - x[1][0] + x[2][0] - x[3][0];
678 ale_pos delta_11 = x[1][1] - x[2][1];
679 ale_pos delta_12 = x[3][1] - x[2][1];
680 ale_pos sigma_1 = x[0][1] - x[1][1] + x[2][1] - x[3][1];
682 g = (sigma_0 * delta_12 - sigma_1 * delta_02)
683 / (delta_01 * delta_12 - delta_11 * delta_02)
684 / (input_width * scale_factor);
686 h = (delta_01 * sigma_1 - delta_11 * sigma_0 )
687 / (delta_01 * delta_12 - delta_11 * delta_02)
688 / (input_height * scale_factor);
690 a = (x[1][0] - x[0][0] + g * x[1][0])
691 / (input_width * scale_factor);
692 b = (x[3][0] - x[0][0] + h * x[3][0])
693 / (input_height * scale_factor);
694 c = x[0][0];
696 d = (x[1][1] - x[0][1] + g * x[1][1])
697 / (input_width * scale_factor);
698 e = (x[3][1] - x[0][1] + h * x[3][1])
699 / (input_height * scale_factor);
700 f = x[0][1];
702 resultant_memo = 1;
703 resultant_inverse_memo = 0;
705 this->x[0] = scaled_inverse_transform( point( 0 , 0 ) );
706 this->x[1] = scaled_inverse_transform( point( (input_height * scale_factor), 0 ) );
707 this->x[2] = scaled_inverse_transform( point( (input_height * scale_factor), (input_width * scale_factor) ) );
708 this->x[3] = scaled_inverse_transform( point( 0 , (input_width * scale_factor) ) );
710 resultant_memo = 0;
711 resultant_inverse_memo = 0;
715 * Modify all euclidean parameters at once. Accommodate bugs in the
716 * version 0 transformation file handler (ALE versions 0.4.0p1 and
717 * earlier). This code is only called when using a transformation data
718 * file created with an old version of ALE.
720 void eu_v0_set(ale_pos eu[3]) {
723 * This is slightly modified code from version
724 * 0.4.0p1.
727 int i;
729 x[0][0] = 0; x[0][1] = 0;
730 x[1][0] = (input_width * scale_factor); x[1][1] = 0;
731 x[2][0] = (input_width * scale_factor); x[2][1] = (input_height * scale_factor);
732 x[3][0] = 0; x[3][1] = (input_height * scale_factor);
735 * Rotate
738 ale_pos theta = (double) eu[2] * M_PI / 180;
740 for (i = 0; i < 4; i++) {
741 ale_pos _x[2];
743 _x[0] = ((double) x[i][0] - (double) (input_width * scale_factor)/2) * cos(theta)
744 + ((double) x[i][1] - (double) (input_height * scale_factor)/2) * sin(theta)
745 + (input_width * scale_factor)/2;
746 _x[1] = ((double) x[i][1] - (double) (input_height * scale_factor)/2) * cos(theta)
747 - ((double) x[i][0] - (double) (input_width * scale_factor)/2) * sin(theta)
748 + (input_height * scale_factor)/2;
750 x[i][0] = _x[0];
751 x[i][1] = _x[1];
755 * Translate
758 for (i = 0; i < 4; i++) {
759 x[i][0] += eu[0];
760 x[i][1] += eu[1];
763 if (_is_projective) {
764 gpt_v0_set(x);
765 return;
769 * Reconstruct euclidean parameters
772 gpt_v0_set(x);
774 point center((input_height * scale_factor) / 2, (input_width * scale_factor) / 2);
775 point center_image = transform_scaled(center);
777 this->eu[0] = (center_image[0] - center[0]) / scale_factor;
778 this->eu[1] = (center_image[1] - center[1]) / scale_factor;
780 point center_left((input_height * scale_factor) / 2, 0);
781 point center_left_image = transform_scaled(center_left);
783 ale_pos displacement = center_image[0] - center_left_image[0];
785 this->eu[2] = asin(2 * displacement / (input_width * scale_factor)) / M_PI * 180;
787 if (center_left_image[1] > center_image[1])
788 this->eu[2] = this->eu[2] + 180;
790 resultant_memo = 0;
791 resultant_inverse_memo = 0;
793 _is_projective = 0;
797 void debug_output() {
798 fprintf(stderr, "[t.do ih=%u, iw=%d x=[[%f %f] [%f %f] [%f %f] [%f %f]] eu=[%f %f %f]\n"
799 " a-f=[%f %f %f %f %f %f %f %f] _a-_f=[%f %f %f %f %f %f %f %f]\n"
800 " bdcnm=%d ip=%d rm=%d rim=%d sf=%f]\n",
801 input_height, input_width,
802 (double) x[0][0], (double) x[0][1],
803 (double) x[1][0], (double) x[1][1],
804 (double) x[2][0], (double) x[2][1],
805 (double) x[3][0], (double) x[3][1],
806 (double) eu[0], (double) eu[1], (double) eu[2],
807 (double) a, (double) b, (double) c, (double) d,
808 (double) e, (double) f, (double) g, (double) h,
809 (double) _a, (double) _b, (double) _c, (double) _d,
810 (double) _e, (double) _f, (double) _g, (double) _h,
811 bd_count(),
812 _is_projective,
813 resultant_memo,
814 resultant_inverse_memo,
815 (double) scale_factor);
820 #endif