1
//----------------------------------------------------------------------------
2
// Anti-Grain Geometry - Version 2.4
3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
10
//----------------------------------------------------------------------------
11
// Contact: mcseem@antigrain.com
12
// mcseemagg@yahoo.com
13
// http://www.antigrain.com
14
//----------------------------------------------------------------------------
16
// Affine transformations
18
//----------------------------------------------------------------------------
19
#include "agg_trans_affine.h"
26
//------------------------------------------------------------------------
27
const trans_affine& trans_affine::parl_to_parl(const double* src,
31
shy = src[3] - src[1];
32
shx = src[4] - src[0];
37
multiply(trans_affine(dst[2] - dst[0], dst[3] - dst[1],
38
dst[4] - dst[0], dst[5] - dst[1],
43
//------------------------------------------------------------------------
44
const trans_affine& trans_affine::rect_to_parl(double x1, double y1,
49
src[0] = x1; src[1] = y1;
50
src[2] = x2; src[3] = y1;
51
src[4] = x2; src[5] = y2;
52
parl_to_parl(src, parl);
56
//------------------------------------------------------------------------
57
const trans_affine& trans_affine::parl_to_rect(const double* parl,
62
dst[0] = x1; dst[1] = y1;
63
dst[2] = x2; dst[3] = y1;
64
dst[4] = x2; dst[5] = y2;
65
parl_to_parl(parl, dst);
69
//------------------------------------------------------------------------
70
const trans_affine& trans_affine::multiply(const trans_affine& m)
72
double t0 = sx * m.sx + shy * m.shx;
73
double t2 = shx * m.sx + sy * m.shx;
74
double t4 = tx * m.sx + ty * m.shx + m.tx;
75
shy = sx * m.shy + shy * m.sy;
76
sy = shx * m.shy + sy * m.sy;
77
ty = tx * m.shy + ty * m.sy + m.ty;
85
//------------------------------------------------------------------------
86
const trans_affine& trans_affine::invert()
88
double d = determinant_reciprocal();
95
double t4 = -tx * t0 - ty * shx;
96
ty = -tx * shy - ty * sy;
104
//------------------------------------------------------------------------
105
const trans_affine& trans_affine::flip_x()
113
//------------------------------------------------------------------------
114
const trans_affine& trans_affine::flip_y()
122
//------------------------------------------------------------------------
123
const trans_affine& trans_affine::reset()
126
shy = shx = tx = ty = 0.0;
130
//------------------------------------------------------------------------
131
bool trans_affine::is_identity(double epsilon) const
133
return is_equal_eps(sx, 1.0, epsilon) &&
134
is_equal_eps(shy, 0.0, epsilon) &&
135
is_equal_eps(shx, 0.0, epsilon) &&
136
is_equal_eps(sy, 1.0, epsilon) &&
137
is_equal_eps(tx, 0.0, epsilon) &&
138
is_equal_eps(ty, 0.0, epsilon);
141
//------------------------------------------------------------------------
142
bool trans_affine::is_valid(double epsilon) const
144
return fabs(sx) > epsilon && fabs(sy) > epsilon;
147
//------------------------------------------------------------------------
148
bool trans_affine::is_equal(const trans_affine& m, double epsilon) const
150
return is_equal_eps(sx, m.sx, epsilon) &&
151
is_equal_eps(shy, m.shy, epsilon) &&
152
is_equal_eps(shx, m.shx, epsilon) &&
153
is_equal_eps(sy, m.sy, epsilon) &&
154
is_equal_eps(tx, m.tx, epsilon) &&
155
is_equal_eps(ty, m.ty, epsilon);
158
//------------------------------------------------------------------------
159
double trans_affine::rotation() const
167
return atan2(y2-y1, x2-x1);
170
//------------------------------------------------------------------------
171
void trans_affine::translation(double* dx, double* dy) const
177
//------------------------------------------------------------------------
178
void trans_affine::scaling(double* x, double* y) const
184
trans_affine t(*this);
185
t *= trans_affine_rotation(-rotation());
186
t.transform(&x1, &y1);
187
t.transform(&x2, &y2);