1 //---------------------------------------------------------------------------- 2 // Anti-Grain Geometry - Version 2.2 3 // Copyright (C) 2002-2004 Maxim Shemanarev (http://www.antigrain.com) 4 // 5 // Permission to copy, use, modify, sell and distribute this software 6 // is granted provided this copyright notice appears in all copies. 7 // This software is provided "as is" without express or implied 8 // warranty, and with no claim as to its suitability for any purpose. 9 // 10 //---------------------------------------------------------------------------- 11 // Contact: mcseem@antigrain.com 12 // mcseemagg@yahoo.com 13 // http://www.antigrain.com 14 //---------------------------------------------------------------------------- 15 // 16 // Affine transformation classes. 17 // 18 //---------------------------------------------------------------------------- 19 #ifndef AGG_TRANS_AFFINE_INCLUDED 20 #define AGG_TRANS_AFFINE_INCLUDED 21 22 #include <math.h> 23 #include "agg_basics.h" 24 25 namespace agg 26 { 27 const double affine_epsilon = 1e-14; // About of precision of doubles 28 29 //============================================================trans_affine 30 // 31 // See Implementation agg_trans_affine.cpp 32 // 33 // Affine transformation are linear transformations in Cartesian coordinates 34 // (strictly speaking not only in Cartesian, but for the beginning we will 35 // think so). They are rotation, scaling, translation and skewing. 36 // After any affine transformation a line segment remains a line segment 37 // and it will never become a curve. 38 // 39 // There will be no math about matrix calculations, since it has been 40 // described many times. Ask yourself a very simple question: 41 // "why do we need to understand and use some matrix stuff instead of just 42 // rotating, scaling and so on". The answers are: 43 // 44 // 1. Any combination of transformations can be done by only 4 multiplications 45 // and 4 additions in floating point. 46 // 2. One matrix transformation is equivalent to the number of consecutive 47 // discrete transformations, i.e. the matrix "accumulates" all transformations 48 // in the order of their settings. Suppose we have 4 transformations: 49 // * rotate by 30 degrees, 50 // * scale X to 2.0, 51 // * scale Y to 1.5, 52 // * move to (100, 100). 53 // The result will depend on the order of these transformations, 54 // and the advantage of matrix is that the sequence of discret calls: 55 // rotate(30), scaleX(2.0), scaleY(1.5), move(100,100) 56 // will have exactly the same result as the following matrix transformations: 57 // 58 // affine_matrix m; 59 // m *= rotate_matrix(30); 60 // m *= scaleX_matrix(2.0); 61 // m *= scaleY_matrix(1.5); 62 // m *= move_matrix(100,100); 63 // 64 // m.transform_my_point_at_last(x, y); 65 // 66 // What is the good of it? In real life we will set-up the matrix only once 67 // and then transform many points, let alone the convenience to set any 68 // combination of transformations. 69 // 70 // So, how to use it? Very easy - literally as it's shown above. Not quite, 71 // let us write a correct example: 72 // 73 // agg::trans_affine m; 74 // m *= agg::trans_affine_rotation(30.0 * 3.1415926 / 180.0); 75 // m *= agg::trans_affine_scaling(2.0, 1.5); 76 // m *= agg::trans_affine_translation(100.0, 100.0); 77 // m.transform(&x, &y); 78 // 79 // The affine matrix is all you need to perform any linear transformation, 80 // but all transformations have origin point (0,0). It means that we need to 81 // use 2 translations if we want to rotate someting around (100,100): 82 // 83 // m *= agg::trans_affine_translation(-100.0, -100.0); // move to (0,0) 84 // m *= agg::trans_affine_rotation(30.0 * 3.1415926 / 180.0); // rotate 85 // m *= agg::trans_affine_translation(100.0, 100.0); // move back to (100,100) 86 //---------------------------------------------------------------------- 87 class trans_affine 88 { 89 public: 90 //------------------------------------------ Construction 91 // Construct an identity matrix - it does not transform anything 92 trans_affine() : 93 m0(1.0), m1(0.0), m2(0.0), m3(1.0), m4(0.0), m5(0.0) 94 {} 95 96 // Construct a custom matrix. Usually used in derived classes 97 trans_affine(double v0, double v1, double v2, double v3, double v4, double v5) : 98 m0(v0), m1(v1), m2(v2), m3(v3), m4(v4), m5(v5) 99 {} 100 101 // Construct a matrix to transform a parallelogram to another one. 102 trans_affine(const double* rect, const double* parl) 103 { 104 parl_to_parl(rect, parl); 105 } 106 107 // Construct a matrix to transform a rectangle to a parallelogram. 108 trans_affine(double x1, double y1, double x2, double y2, 109 const double* parl) 110 { 111 rect_to_parl(x1, y1, x2, y2, parl); 112 } 113 114 // Construct a matrix to transform a parallelogram to a rectangle. 115 trans_affine(const double* parl, 116 double x1, double y1, double x2, double y2) 117 { 118 parl_to_rect(parl, x1, y1, x2, y2); 119 } 120 121 122 //---------------------------------- Parellelogram transformations 123 // Calculate a matrix to transform a parallelogram to another one. 124 // src and dst are pointers to arrays of three points 125 // (double[6], x,y,...) that identify three corners of the 126 // parallelograms assuming implicit fourth points. 127 // There are also transformations rectangtle to parallelogram and 128 // parellelogram to rectangle 129 const trans_affine& parl_to_parl(const double* src, 130 const double* dst); 131 132 const trans_affine& rect_to_parl(double x1, double y1, 133 double x2, double y2, 134 const double* parl); 135 136 const trans_affine& parl_to_rect(const double* parl, 137 double x1, double y1, 138 double x2, double y2); 139 140 141 //------------------------------------------ Operations 142 // Reset - actually load an identity matrix 143 const trans_affine& reset(); 144 145 // Multiply matrix to another one 146 const trans_affine& multiply(const trans_affine& m); 147 148 // Multiply "m" to "this" and assign the result to "this" 149 const trans_affine& premultiply(const trans_affine& m); 150 151 // Invert matrix. Do not try to invert degenerate matrices, 152 // there's no check for validity. If you set scale to 0 and 153 // then try to invert matrix, expect unpredictable result. 154 const trans_affine& invert(); 155 156 // Mirroring around X 157 const trans_affine& flip_x(); 158 159 // Mirroring around Y 160 const trans_affine& flip_y(); 161 162 //------------------------------------------- Load/Store 163 // Store matrix to an array [6] of double 164 void store_to(double* m) const 165 { 166 *m++ = m0; *m++ = m1; *m++ = m2; *m++ = m3; *m++ = m4; *m++ = m5; 167 } 168 169 // Load matrix from an array [6] of double 170 const trans_affine& load_from(const double* m) 171 { 172 m0 = *m++; m1 = *m++; m2 = *m++; m3 = *m++; m4 = *m++; m5 = *m++; 173 return *this; 174 } 175 176 //------------------------------------------- Operators 177 178 // Multiply current matrix to another one 179 const trans_affine& operator *= (const trans_affine& m) 180 { 181 return multiply(m); 182 } 183 184 // Multiply current matrix to another one and return 185 // the result in a separete matrix. 186 trans_affine operator * (const trans_affine& m) 187 { 188 return trans_affine(*this).multiply(m); 189 } 190 191 // Calculate and return the inverse matrix 192 trans_affine operator ~ () const 193 { 194 trans_affine ret = *this; 195 return ret.invert(); 196 } 197 198 // Equal operator with default epsilon 199 bool operator == (const trans_affine& m) const 200 { 201 return is_equal(m, affine_epsilon); 202 } 203 204 // Not Equal operator with default epsilon 205 bool operator != (const trans_affine& m) const 206 { 207 return !is_equal(m, affine_epsilon); 208 } 209 210 //-------------------------------------------- Transformations 211 // Direct transformation x and y 212 void transform(double* x, double* y) const; 213 214 // Inverse transformation x and y. It works slower than the 215 // direct transformation, so if the performance is critical 216 // it's better to invert() the matrix and then use transform() 217 void inverse_transform(double* x, double* y) const; 218 219 //-------------------------------------------- Auxiliary 220 // Calculate the determinant of matrix 221 double determinant() const 222 { 223 return 1.0 / (m0 * m3 - m1 * m2); 224 } 225 226 // Get the average scale (by X and Y). 227 // Basically used to calculate the approximation_scale when 228 // decomposinting curves into line segments. 229 double scale() const; 230 231 // Check to see if it's an identity matrix 232 bool is_identity(double epsilon = affine_epsilon) const; 233 234 // Check to see if two matrices are equal 235 bool is_equal(const trans_affine& m, double epsilon = affine_epsilon) const; 236 237 // Determine the major parameters. Use carefully considering degenerate matrices 238 double rotation() const; 239 void translation(double* dx, double* dy) const; 240 void scaling(double* sx, double* sy) const; 241 242 private: 243 double m0; 244 double m1; 245 double m2; 246 double m3; 247 double m4; 248 double m5; 249 }; 250 251 //------------------------------------------------------------------------ 252 inline void trans_affine::transform(double* x, double* y) const 253 { 254 register double tx = *x; 255 *x = tx * m0 + *y * m2 + m4; 256 *y = tx * m1 + *y * m3 + m5; 257 } 258 259 //------------------------------------------------------------------------ 260 inline void trans_affine::inverse_transform(double* x, double* y) const 261 { 262 register double d = determinant(); 263 register double a = (*x - m4) * d; 264 register double b = (*y - m5) * d; 265 *x = a * m3 - b * m2; 266 *y = b * m0 - a * m1; 267 } 268 269 //------------------------------------------------------------------------ 270 inline double trans_affine::scale() const 271 { 272 double x = 0.707106781 * m0 + 0.707106781 * m2; 273 double y = 0.707106781 * m1 + 0.707106781 * m3; 274 return sqrt(x*x + y*y); 275 } 276 277 278 //------------------------------------------------------------------------ 279 inline const trans_affine& trans_affine::premultiply(const trans_affine& m) 280 { 281 trans_affine t = m; 282 return *this = t.multiply(*this); 283 } 284 285 286 //====================================================trans_affine_rotation 287 // Rotation matrix. sin() and cos() are calculated twice for the same angle. 288 // There's no harm because the performance of sin()/cos() is very good on all 289 // modern processors. Besides, this operation is not going to be invoked too 290 // often. 291 class trans_affine_rotation : public trans_affine 292 { 293 public: 294 trans_affine_rotation(double a) : 295 trans_affine(cos(a), sin(a), -sin(a), cos(a), 0.0, 0.0) 296 {} 297 }; 298 299 //====================================================trans_affine_scaling 300 // Scaling matrix. sx, sy - scale coefficients by X and Y respectively 301 class trans_affine_scaling : public trans_affine 302 { 303 public: 304 trans_affine_scaling(double sx, double sy) : 305 trans_affine(sx, 0.0, 0.0, sy, 0.0, 0.0) 306 {} 307 308 trans_affine_scaling(double s) : 309 trans_affine(s, 0.0, 0.0, s, 0.0, 0.0) 310 {} 311 }; 312 313 //================================================trans_affine_translation 314 // Translation matrix 315 class trans_affine_translation : public trans_affine 316 { 317 public: 318 trans_affine_translation(double tx, double ty) : 319 trans_affine(1.0, 0.0, 0.0, 1.0, tx, ty) 320 {} 321 }; 322 323 //====================================================trans_affine_skewing 324 // Sckewing (shear) matrix 325 class trans_affine_skewing : public trans_affine 326 { 327 public: 328 trans_affine_skewing(double sx, double sy) : 329 trans_affine(1.0, tan(sy), tan(sx), 1.0, 0.0, 0.0) 330 {} 331 }; 332 333 334 335 } 336 337 338 #endif 339 340