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
|
//----------------------------------------------------------------------------
// 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 for time ranges.
//
//----------------------------------------------------------------------------
#ifndef AGG_TRANS_AFFINE_TIME_INCLUDED
#define AGG_TRANS_AFFINE_TIME_INCLUDED
#include <math.h>
#include "agg2/agg_basics.h"
#include <cfloat>
namespace agg
{
const double affine_time_epsilon = 1e-14;
//============================================================trans_affine_time
//
// 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 scaling and translation.
//
//----------------------------------------------------------------------
struct trans_affine_time
{
double st, tbegin, tend;
//------------------------------------------ Construction
// Identity matrix
trans_affine_time() :
st(1.0), tbegin(0.0), tend(0.0)
{}
// Custom matrix. Usually used in derived classes
trans_affine_time(double v0, double v1, double v2) :
st(v0), tbegin(v1), tend(v2)
{}
// Custom matrix from m[3]
explicit trans_affine_time(const double* m) :
st(m[0]), tbegin(m[1]), tend(m[2])
{}
//------------------------------------------ Operations
// Reset - load an identity matrix
const trans_affine_time& reset();
// Direct transformations operations
const trans_affine_time& translate(double x, double y);
const trans_affine_time& translate(double x);
const trans_affine_time& scale(double s);
// Multiply matrix to another one
const trans_affine_time& multiply(const trans_affine_time& m);
// Multiply "m" to "this" and assign the result to "this"
const trans_affine_time& premultiply(const trans_affine_time& m);
// Check if transform is valid
bool is_valid(double epsilon = affine_time_epsilon) const;
//------------------------------------------- Load/Store
// Store matrix to an array [3] of double
void store_to(double* m) const
{
*m++ = st; *m++ = tbegin; *m++ = tend;
}
// Load matrix from an array [3] of double
const trans_affine_time& load_from(const double* m)
{
st = *m++; tbegin = *m++; tend = *m++;
return *this;
}
// Load matrix from 3 doubles
const trans_affine_time& load_from(double v0, double v1, double v2)
{
st = v0; tbegin = v1; tend = v2;
return *this;
}
//------------------------------------------- Operators
// Multiply the matrix by another one
const trans_affine_time& operator *= (const trans_affine_time& m)
{
return multiply(m);
}
// Multiply the matrix by another one and return
// the result in a separete matrix.
trans_affine_time operator * (const trans_affine_time& m) const
{
return trans_affine_time(*this).multiply(m);
}
// Equal operator with default epsilon
bool operator == (const trans_affine_time& m) const
{
return is_equal(m, affine_time_epsilon);
}
// Not Equal operator with default epsilon
bool operator != (const trans_affine_time& m) const
{
return !is_equal(m, affine_time_epsilon);
}
// Check to see if it's an identity matrix
bool is_identity(double epsilon = affine_time_epsilon) const;
// Check to see if two matrices are equal
bool is_equal(const trans_affine_time& m, double epsilon = affine_time_epsilon) const;
// Check to see if two time ranges overlap
bool overlaps(const trans_affine_time& m) const;
// Determine the major parameters. Use with caution considering
// possible degenerate cases.
void translation(double* begin, double* end) const;
};
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::premultiply(const trans_affine_time& m)
{
trans_affine_time t = m;
return *this = t.multiply(*this);
}
//====================================================trans_affine_time_scaling
// Scaling matrix. x, y - scale coefficients by X and Y respectively
class trans_affine_time_scaling : public trans_affine_time
{
public:
trans_affine_time_scaling(double s) :
trans_affine_time(s, 0.0, 0.0)
{}
};
//================================================trans_affine_time_translation
// Translation matrix
class trans_affine_time_translation : public trans_affine_time
{
public:
trans_affine_time_translation(double begin, double end) :
trans_affine_time(1.0, begin, end)
{}
};
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::multiply(const trans_affine_time& m)
{
st = st * m.st;
tbegin = tbegin * m.st + m.tbegin;
tend = tend * m.st + m.tend;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::reset()
{
st = 1.0;
tbegin = 0.0;
tend = 0.0;
return *this;
}
static inline double sign(double a)
{
return (a < -affine_time_epsilon) ? -1.0 :
((a > affine_time_epsilon) ? 1.0 : 0.0);
}
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::scale(double s)
{
st *= s;
tbegin *= s;
tend *= s;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::translate(double x, double y)
{
tbegin += x;
tend += y;
return *this;
}
//------------------------------------------------------------------------
inline const trans_affine_time& trans_affine_time::translate(double x)
{
tbegin += x;
tend += x;
return *this;
}
//------------------------------------------------------------------------
inline bool trans_affine_time::is_identity(double epsilon) const
{
return is_equal_eps(st, 1.0, epsilon) &&
is_equal_eps(tbegin, 0.0, epsilon) &&
is_equal_eps(tend, 0.0, epsilon);
}
//------------------------------------------------------------------------
inline bool trans_affine_time::is_valid(double epsilon) const
{
return fabs(st) > epsilon && tbegin <= tend;
}
//------------------------------------------------------------------------
inline bool trans_affine_time::is_equal(const trans_affine_time& m, double epsilon) const
{
return is_equal_eps(st, m.st, epsilon) &&
is_equal_eps(tbegin, m.tbegin, epsilon) &&
is_equal_eps(tend, m.tend, epsilon);
}
//------------------------------------------------------------------------
inline bool trans_affine_time::overlaps(const trans_affine_time& m) const
{
return !(m.tbegin > tend || m.tend < tbegin);
}
//------------------------------------------------------------------------
inline void trans_affine_time::translation(double* begin, double* end) const
{
*begin = tbegin;
*end = tend;
}
}
#endif
|