xform_2d.h

Go to the documentation of this file.
00001 /*
00002  * xform_2d.h
00003  *
00004  * Copyright (C) 2007,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * 2D transformations (rotations, translations, scaling, skew, etc...)
00032  */
00033 
00034 #ifndef WAVEPACKET_2D_XFORM_H__
00035 #define WAVEPACKET_2D_XFORM_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "geometry_2d.h"
00039 
00040 #include <math.h>
00041 
00042 
00043 /// \ingroup geometry
00044 /*@{*/
00045 
00046 /// 2D transform object - basically a 2x2 rotation matrix + translation
00047 struct xform_2d_t {
00048         typedef point2d_t<float> point_t;
00049         typedef rect2d_t<float> rect_t;
00050 
00051         // constructor + functions
00052         xform_2d_t(void) throw() { this->setIdentity(); }
00053         void clear(void) throw() { this->setIdentity(); }
00054         void setIdentity(void) throw() {
00055                         // set xform to identity matrix
00056                         t00 = t11 = 1.0;
00057                         t01 = t10 = 0.0;
00058                         dx = dy = 0.0;
00059                 }
00060         void setZRotate(IN float phi) throw() {
00061                         // set xform to rotation by angle phi around z axis
00062                         // phi is in RADIANS, not degrees
00063                         t00 = t11 = cos(phi);
00064                         t01 = -sin(phi);
00065                         t10 = -t01;
00066                         dx = dy = 0.0;
00067                 }
00068         void setTranslate(IN float x, IN float y) throw() {
00069                         // set xform to translation by dx,dy
00070                         t00 = t11 = 1.0;
00071                         t01 = t10 = 0.0;
00072                         dx = x;
00073                         dy = y;
00074                 }
00075 
00076         void scale(IN float r) throw() {
00077                         // multiply xform by scalar r
00078                         scaleX(r);
00079                         scaleY(r);
00080                 }
00081 
00082         void scaleX(IN float rx) throw() {
00083                         // scale up x-direction
00084                         t00 *= rx;
00085                         t01 *= rx;
00086                         dx *= rx;
00087                 }
00088 
00089         void scaleY(IN float ry) throw() {
00090                         // scale up y-direction
00091                         t10 *= ry;
00092                         t11 *= ry;
00093                         dy *= ry;
00094                 }
00095 
00096         void setToProductOf(IN const xform_2d_t& left,
00097                                 IN const xform_2d_t& right) throw() {
00098                         // this xform = left * right
00099                         t00 = left.t00 * right.t00 + left.t01 * right.t10;
00100                         t01 = left.t00 * right.t01 + left.t01 * right.t11;
00101                         t10 = left.t10 * right.t00 + left.t11 * right.t10;
00102                         t11 = left.t10 * right.t01 + left.t11 * right.t11;
00103                         dx = left.t00 * right.dx + left.t01 * right.dy + left.dx;
00104                         dy = left.t10 * right.dx + left.t11 * right.dy + left.dy;
00105                 }
00106 
00107         void transformPoint(IN const point_t& in,
00108                                 OUT point_t& out) const throw() {
00109                         out.x = t00 * in.x + t01 * in.y + dx;
00110                         out.y = t10 * in.x + t11 * in.y + dy;
00111                 }
00112 
00113         void transformRect(IN const rect_t& in,
00114                                 OUT rect_t& out) const throw();
00115 
00116         void setToInverseOf(IN const xform_2d_t& T) throw();
00117 
00118         void dump(IN const char * title) const throw() {
00119                         DPRINTF("  %s (2d transform):", title);
00120                         DPRINTF("    [ %5.2f  %5.2f  %5.2f ]", t00, t01, dx);
00121                         DPRINTF("    [ %5.2f  %5.2f  %5.2f ]", t10, t11, dy);
00122                 }
00123 
00124         // data fields
00125         float   t00;
00126         float   t01;
00127         float   t10;
00128         float   t11;
00129         float   dx;
00130         float   dy;
00131 };
00132 
00133 
00134 
00135 inline xform_2d_t::point_t operator *
00136 (IN const xform_2d_t& T, IN const xform_2d_t::point_t& p)
00137 throw()
00138 {
00139         xform_2d_t::point_t q;
00140         T.transformPoint(p, q);
00141         return q;
00142 }
00143 
00144 
00145 
00146 #endif  // WAVEPACKET_2D_XFORM_H__
00147