Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
affine.h
Go to the documentation of this file.
1/*
5 * Authors:
6 * Lauris Kaplinski <lauris@kaplinski.com> (Original NRAffine definition and related macros)
7 * Nathan Hurst <njh@mail.csse.monash.edu.au> (Geom::Affine class version of the above)
8 * Michael G. Sloan <mgsloan@gmail.com> (reorganization and additions)
9 * Krzysztof KosiƄski <tweenk.pl@gmail.com> (removal of boilerplate, docs)
10 *
11 * This code is in public domain.
12 */
13
14#ifndef LIB2GEOM_SEEN_AFFINE_H
15#define LIB2GEOM_SEEN_AFFINE_H
16
17#include <boost/operators.hpp>
18#include <2geom/forward.h>
19#include <2geom/point.h>
20#include <2geom/utils.h>
21
22namespace Geom {
23
60class Affine
61 : boost::equality_comparable< Affine // generates operator!= from operator==
62 , boost::multipliable1< Affine
63 , MultipliableNoncommutative< Affine, Translate
64 , MultipliableNoncommutative< Affine, Scale
65 , MultipliableNoncommutative< Affine, Rotate
66 , MultipliableNoncommutative< Affine, HShear
67 , MultipliableNoncommutative< Affine, VShear
68 , MultipliableNoncommutative< Affine, Zoom
69 > > > > > > > >
70{
72public:
74 _c[0] = _c[3] = 1;
75 _c[1] = _c[2] = _c[4] = _c[5] = 0;
76 }
77
87 Affine(Coord c0, Coord c1, Coord c2, Coord c3, Coord c4, Coord c5) {
88 _c[0] = c0; _c[1] = c1;
89 _c[2] = c2; _c[3] = c3;
90 _c[4] = c4; _c[5] = c5;
91 }
92
94 inline Coord operator[](unsigned i) const { return _c[i]; }
95 inline Coord &operator[](unsigned i) { return _c[i]; }
96
99 Affine &operator*=(Affine const &m);
100 // implemented in transforms.cpp
101 Affine &operator*=(Translate const &t);
102 Affine &operator*=(Scale const &s);
103 Affine &operator*=(Rotate const &r);
104 Affine &operator*=(HShear const &h);
105 Affine &operator*=(VShear const &v);
106 Affine &operator*=(Zoom const &);
108
109 bool operator==(Affine const &o) const {
110 for(unsigned i = 0; i < 6; ++i) {
111 if ( _c[i] != o._c[i] ) return false;
112 }
113 return true;
114 }
115
118 Point xAxis() const;
119 Point yAxis() const;
120 Point translation() const;
121 Coord expansionX() const;
122 Coord expansionY() const;
123 Point expansion() const { return Point(expansionX(), expansionY()); }
125
128 void setXAxis(Point const &vec);
129 void setYAxis(Point const &vec);
130
131 void setTranslation(Point const &loc);
132
133 void setExpansionX(Coord val);
134 void setExpansionY(Coord val);
135 void setIdentity();
137
140 bool isIdentity(Coord eps = EPSILON) const;
141
142 bool isTranslation(Coord eps = EPSILON) const;
143 bool isScale(Coord eps = EPSILON) const;
144 bool isUniformScale(Coord eps = EPSILON) const;
145 bool isRotation(Coord eps = EPSILON) const;
146 bool isHShear(Coord eps = EPSILON) const;
147 bool isVShear(Coord eps = EPSILON) const;
148
149 bool isNonzeroTranslation(Coord eps = EPSILON) const;
150 bool isNonzeroScale(Coord eps = EPSILON) const;
151 bool isNonzeroUniformScale(Coord eps = EPSILON) const;
152 bool isNonzeroRotation(Coord eps = EPSILON) const;
153 bool isNonzeroNonpureRotation(Coord eps = EPSILON) const;
154 Point rotationCenter() const;
155 bool isNonzeroHShear(Coord eps = EPSILON) const;
156 bool isNonzeroVShear(Coord eps = EPSILON) const;
157
158 bool isZoom(Coord eps = EPSILON) const;
159 bool preservesArea(Coord eps = EPSILON) const;
160 bool preservesAngles(Coord eps = EPSILON) const;
161 bool preservesDistances(Coord eps = EPSILON) const;
162 bool flips() const;
163
164 bool isSingular(Coord eps = EPSILON) const;
166
170 Affine ret(*this);
171 ret.setTranslation(Point(0,0));
172 return ret;
173 }
174 Affine inverse() const;
176
179 Coord det() const;
180 Coord descrim2() const;
181 Coord descrim() const;
183 inline static Affine identity();
184};
185
188inline std::ostream &operator<< (std::ostream &out_file, const Geom::Affine &m) {
189 out_file << "A: " << m[0] << " C: " << m[2] << " E: " << m[4] << "\n";
190 out_file << "B: " << m[1] << " D: " << m[3] << " F: " << m[5] << "\n";
191 return out_file;
192}
193
194// Affine factories
195Affine from_basis(const Point &x_basis, const Point &y_basis, const Point &offset=Point(0,0));
197
200class Eigen{
201public:
203 double values[2];
204 Eigen(Affine const &m);
205 Eigen(double M[2][2]);
206};
207
210inline Affine identity() {
212 return ret; // allow NRVO
213}
214
223 Affine ret(1.0, 0.0,
224 0.0, 1.0,
225 0.0, 0.0);
226 return ret; // allow NRVO
227}
228
229bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON);
230
231} // end namespace Geom
232
233#endif // LIB2GEOM_SEEN_AFFINE_H
234
235/*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243*/
244// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Various utility functions.
3x3 matrix representing an affine transformation.
Definition affine.h:70
void setYAxis(Point const &vec)
Definition affine.cpp:50
bool isHShear(Coord eps=EPSILON) const
Check whether this matrix represents pure horizontal shearing.
Definition affine.cpp:256
bool isNonzeroScale(Coord eps=EPSILON) const
Check whether this matrix represents pure, nonzero scaling.
Definition affine.cpp:160
Coord det() const
Calculate the determinant.
Definition affine.cpp:416
void setTranslation(Point const &loc)
Sets the translation imparted by the Affine.
Definition affine.cpp:56
bool flips() const
Check whether this transformation flips objects.
Definition affine.cpp:368
Coord _c[6]
Definition affine.h:71
Coord operator[](unsigned i) const
Access a coefficient by its index.
Definition affine.h:94
Point expansion() const
Definition affine.h:123
bool preservesDistances(Coord eps=EPSILON) const
Check whether the transformation preserves distances between points.
Definition affine.cpp:359
bool preservesAngles(Coord eps=EPSILON) const
Check whether the transformation preserves angles between lines.
Definition affine.cpp:339
bool preservesArea(Coord eps=EPSILON) const
Check whether the transformation preserves areas of polygons.
Definition affine.cpp:321
Point yAxis() const
Definition affine.cpp:36
Point translation() const
Gets the translation imparted by the Affine.
Definition affine.cpp:41
Coord descrim() const
Calculate the descriminant.
Definition affine.cpp:434
bool isNonzeroNonpureRotation(Coord eps=EPSILON) const
Check whether this matrix represents a non-zero rotation about any point.
Definition affine.cpp:233
bool isScale(Coord eps=EPSILON) const
Check whether this matrix represents pure scaling.
Definition affine.cpp:147
bool isUniformScale(Coord eps=EPSILON) const
Check whether this matrix represents pure uniform scaling.
Definition affine.cpp:174
void setXAxis(Point const &vec)
Definition affine.cpp:45
bool isNonzeroTranslation(Coord eps=EPSILON) const
Check whether this matrix represents a pure nonzero translation.
Definition affine.cpp:134
Coord expansionX() const
Calculates the amount of x-scaling imparted by the Affine.
Definition affine.cpp:64
bool isNonzeroHShear(Coord eps=EPSILON) const
Check whether this matrix represents pure, nonzero horizontal shearing.
Definition affine.cpp:268
void setExpansionX(Coord val)
Definition affine.cpp:75
Coord descrim2() const
Calculate the square of the descriminant.
Definition affine.cpp:424
bool isVShear(Coord eps=EPSILON) const
Check whether this matrix represents pure vertical shearing.
Definition affine.cpp:281
bool isSingular(Coord eps=EPSILON) const
Check whether this matrix is singular.
Definition affine.cpp:377
bool isZoom(Coord eps=EPSILON) const
Check whether this matrix represents zooming.
Definition affine.cpp:310
bool isIdentity(Coord eps=EPSILON) const
Check whether this matrix is an identity matrix.
Definition affine.cpp:109
Point xAxis() const
Definition affine.cpp:32
static Affine identity()
bool isNonzeroUniformScale(Coord eps=EPSILON) const
Check whether this matrix represents pure, nonzero uniform scaling.
Definition affine.cpp:189
Coord & operator[](unsigned i)
Definition affine.h:95
Point rotationCenter() const
For a (possibly non-pure) non-zero-rotation matrix, calculate the rotation center.
Definition affine.cpp:243
void setIdentity()
Sets this matrix to be the Identity Affine.
Definition affine.cpp:96
bool isNonzeroVShear(Coord eps=EPSILON) const
Check whether this matrix represents pure, nonzero vertical shearing.
Definition affine.cpp:294
bool isTranslation(Coord eps=EPSILON) const
Check whether this matrix represents a pure translation.
Definition affine.cpp:123
Affine inverse() const
Compute the inverse matrix.
Definition affine.cpp:388
Affine & operator*=(Affine const &m)
Combine this transformation with another one.
Definition affine.cpp:442
void setExpansionY(Coord val)
Definition affine.cpp:85
Affine withoutTranslation() const
Definition affine.h:169
bool isRotation(Coord eps=EPSILON) const
Check whether this matrix represents a pure rotation.
Definition affine.cpp:206
bool operator==(Affine const &o) const
Definition affine.h:109
bool isNonzeroRotation(Coord eps=EPSILON) const
Check whether this matrix represents a pure, nonzero rotation.
Definition affine.cpp:219
Coord expansionY() const
Calculates the amount of y-scaling imparted by the Affine.
Definition affine.cpp:71
Affine(Coord c0, Coord c1, Coord c2, Coord c3, Coord c4, Coord c5)
Create a matrix from its coefficient values.
Definition affine.h:87
Given a matrix (ignoring the translation) this returns the eigen values and vectors.
Definition affine.h:200
double values[2]
Definition affine.h:203
Point vectors[2]
Definition affine.h:202
Horizontal shearing.
Definition transforms.h:257
Two-dimensional point that doubles as a vector.
Definition point.h:66
Rotation around the origin.
Definition transforms.h:187
Scaling from the origin.
Definition transforms.h:150
Translation by a vector.
Definition transforms.h:115
Vertical shearing.
Definition transforms.h:274
Combination of a translation and uniform scale.
Definition transforms.h:292
Contains forward declarations of 2geom types.
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
double offset
Various utility functions.
Definition affine.h:22
Affine from_basis(const Point &x_basis, const Point &y_basis, const Point &offset=Point(0, 0))
Creates a Affine given an axis and origin point.
Definition affine.cpp:26
Affine identity()
Create an identity matrix.
Definition affine.h:210
Affine elliptic_quadratic_form(Affine const &m)
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)