1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
// Copyright (C) 2009 John Horigan ( john@glyphic.com )
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: john@glyphic.com
// mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//
//----------------------------------------------------------------------------
//
// Affine transformation classes in one dimension.
//
//----------------------------------------------------------------------------
#ifndef AGG_TRANS_AFFINE_1D_INCLUDED
#define AGG_TRANS_AFFINE_1D_INCLUDED
#include <math.h>
#include "agg2/agg_basics.h"
namespace agg
{
const double affine_1D_epsilon = 1e-14;
//============================================================trans_affine_1D
//
// Affine transformation are linear transformations in Cartesian coordinates
// (strictly speaking not only in Cartesian, but for the beginning we will
// think so). In one dimension, they are rotation and translation.
//
//----------------------------------------------------------------------
struct trans_affine_1D
{
double sz, tz;
//------------------------------------------ Construction
// Identity matrix
trans_affine_1D() :
sz(1.0), tz(0.0)
{}
// Custom matrix. Usually used in derived classes
trans_affine_1D(double v0, double v1) :
sz(v0), tz(v1)
{}
// Custom matrix from m[2]
explicit trans_affine_1D(const double* m) :
sz(m[0]), tz(m[1])
{}
//------------------------------------------ Operations
// Reset - load an identity matrix
const trans_affine_1D& reset();
// Direct transformations operations
const trans_affine_1D& translate(double z);
const trans_affine_1D& scale(double s);
// Multiply matrix to another one
const trans_affine_1D& multiply(const trans_affine_1D& m);
// Multiply "m" to "this" and assign the result to "this"
const trans_affine_1D& premultiply(const trans_affine_1D& m);
// Multiply matrix to inverse of another one
const trans_affine_1D& multiply_inv(const trans_affine_1D& m);
// Multiply inverse of "m" to "this" and assign the result to "this"
const trans_affine_1D& premultiply_inv(const trans_affine_1D& m);
// Invert matrix. Do not try to invert degenerate matrices,
// there's no check for validity. If you set scale to 0 and
// then try to invert matrix, expect unpredictable result.
const trans_affine_1D& invert();
//------------------------------------------- Load/Store
// Store matrix to an array [6] of double
void store_to(double* m) const
{
*m++ = sz; *m++ = tz;
}
// Load matrix from an array [6] of double
const trans_affine_1D& load_from(const double* m)
{
sz = *m++; tz = *m++;
return *this;
}
//------------------------------------------- Operators
// Multiply the matrix by another one
const trans_affine_1D& operator *= (const trans_affine_1D& m)
{
return multiply(m);
}
// Multiply the matrix by inverse of another one
const trans_affine_1D& operator /= (const trans_affine_1D& m)
{
return multiply_inv(m);
}
// Multiply the matrix by another one and return
// the result in a separete matrix.
trans_affine_1D operator * (const trans_affine_1D& m) const
{
return trans_affine_1D(*this).multiply(m);
}
// Multiply the matrix by inverse of another one
// and return the result in a separete matrix.
trans_affine_1D operator / (const trans_affine_1D& m) const
{
return trans_affine_1D(*this).multiply_inv(m);
}
// Calculate and return the inverse matrix
trans_affine_1D operator ~ () const
{
trans_affine_1D ret = *this;
return ret.invert();
}
// Equal operator with default epsilon
bool operator == (const trans_affine_1D& m) const
{
return is_equal(m, affine_1D_epsilon);
}
// Not Equal operator with default epsilon
bool operator != (const trans_affine_1D& m) const
{
return !is_equal(m, affine_1D_epsilon);
}
//-------------------------------------------- Transformations
// Direct transformation of z
void transform(double* z) const;
// Inverse transformation of z. It works slower than the
// direct transformation. For massive operations it's better to
// invert() the matrix and then use direct transformations.
void inverse_transform(double* z) const;
//-------------------------------------------- Auxiliary
// Calculate the determinant of matrix
double determinant() const
{
return sz;
}
// Calculate the reciprocal of the determinant
double determinant_reciprocal() const
{
return 1.0 / (sz);
}
double scale() const;
// Check to see if the matrix is not degenerate
bool is_valid(double epsilon = affine_1D_epsilon) const;
// Check to see if it's an identity matrix
bool is_identity(double epsilon = affine_1D_epsilon) const;
// Check to see if two matrices are equal
bool is_equal(const trans_affine_1D& m, double epsilon = affine_1D_epsilon) const;
// Determine the major parameters. Use with caution considering
// possible degenerate cases.
void translation(double* dz) const;
};
//------------------------------------------------------------------------
inline void trans_affine_1D::transform(double* z) const
{
*z = *z * sz + tz;
}
//------------------------------------------------------------------------
inline void trans_affine_1D::inverse_transform(double* z) const
{
double d = determinant_reciprocal();
*z = (*z - tz) * d;
}
//------------------------------------------------------------------------
inline double trans_affine_1D::scale() const
{
return fabs(sz);
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::translate(double z)
{
tz += z;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::scale(double s)
{
double m = s; // Possible hint for the optimizer
sz *= m;
tz *= m;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::premultiply(const trans_affine_1D& m)
{
trans_affine_1D t = m;
return *this = t.multiply(*this);
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::multiply_inv(const trans_affine_1D& m)
{
trans_affine_1D t = m;
t.invert();
return multiply(t);
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::premultiply_inv(const trans_affine_1D& m)
{
trans_affine_1D t = m;
t.invert();
return *this = t.multiply(*this);
}
//====================================================trans_affine_1D_scaling
// Scaling matrix. x, y - scale coefficients by X and Y respectively
class trans_affine_1D_scaling : public trans_affine_1D
{
public:
trans_affine_1D_scaling(double s) :
trans_affine_1D(s, 0.0)
{}
};
//================================================trans_affine_1D_translation
// Translation matrix
class trans_affine_1D_translation : public trans_affine_1D
{
public:
trans_affine_1D_translation(double z) :
trans_affine_1D(1.0, z)
{}
};
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::invert()
{
double d = determinant_reciprocal();
sz = d;
tz = -tz * d;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::multiply(const trans_affine_1D& m)
{
sz = sz * m.sz;
tz = tz * m.sz + m.tz;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_1D& trans_affine_1D::reset()
{
sz = 1.0;
tz = 0.0;
return *this;
}
//------------------------------------------------------------------------
inline bool trans_affine_1D::is_identity(double epsilon) const
{
return is_equal_eps(sz, 1.0, epsilon) &&
is_equal_eps(tz, 0.0, epsilon);
}
//------------------------------------------------------------------------
inline bool trans_affine_1D::is_valid(double epsilon) const
{
return fabs(sz) > epsilon;
}
//------------------------------------------------------------------------
inline bool trans_affine_1D::is_equal(const trans_affine_1D& m, double epsilon) const
{
return is_equal_eps(sz, m.sz, epsilon) &&
is_equal_eps(tz, m.tz, epsilon);
}
//------------------------------------------------------------------------
inline void trans_affine_1D::translation(double* dz) const
{
*dz = tz;
}
}
#endif
|