1 #ifndef __Geom_MATRIX_H__
2 #define __Geom_MATRIX_H__
4 /** \file
5 * \brief Definition of Geom::Matrix types.
6 *
7 * Main authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>:
9 * Original NRMatrix definition and related macros.
10 *
11 * Nathan Hurst <njh@mail.csse.monash.edu.au>:
12 * Geom::Matrix class version of the above.
13 *
14 * Michael G. Sloan <mgsloan@gmail.com>:
15 * Reorganization and additions.
16 *
17 * This code is in public domain.
18 */
20 //#include <glib/gmessages.h>
22 #include <2geom/point.h>
24 namespace Geom {
26 /**
27 * The Matrix class.
28 *
29 * For purposes of multiplication, points should be thought of as row vectors
30 *
31 * \f$(p_X p_Y 1)\f$
32 *
33 * to be right-multiplied by transformation matrices of the form
34 * \f[
35 \left[
36 \begin{array}{ccc}
37 c_0&c_1&0 \\
38 c_2&c_3&0 \\
39 c_4&c_5&1
40 \end{array}
41 \right]
42 \f]
43 * (so the columns of the matrix correspond to the columns (elements) of the result,
44 * and the rows of the matrix correspond to columns (elements) of the "input").
45 */
46 class Matrix {
47 private:
48 Coord _c[6];
49 public:
50 Matrix() {}
52 Matrix(Matrix const &m) {
53 for(int i = 0; i < 6; i++) {
54 _c[i] = m[i];
55 }
56 }
58 Matrix(Coord c0, Coord c1, Coord c2, Coord c3, Coord c4, Coord c5) {
59 _c[0] = c0; _c[1] = c1;
60 _c[2] = c2; _c[3] = c3;
61 _c[4] = c4; _c[5] = c5;
62 }
64 Matrix &operator=(Matrix const &m) {
65 for(int i = 0; i < 6; i++)
66 _c[i] = m._c[i];
67 return *this;
68 }
70 inline Coord operator[](unsigned const i) const { return _c[i]; }
71 inline Coord &operator[](unsigned const i) { return _c[i]; }
74 Point xAxis() const;
75 Point yAxis() const;
76 void setXAxis(Point const &vec);
77 void setYAxis(Point const &vec);
79 Point translation() const;
80 void setTranslation(Point const &loc);
82 double expansionX() const;
83 double expansionY() const;
84 inline Point expansion() const { return Point(expansionX(), expansionY()); }
85 void setExpansionX(double val);
86 void setExpansionY(double val);
88 void setIdentity();
90 bool isIdentity(Coord eps = EPSILON) const;
91 bool isTranslation(Coord eps = EPSILON) const;
92 bool isRotation(double eps = EPSILON) const;
93 bool isScale(double eps = EPSILON) const;
94 bool isUniformScale(double eps = EPSILON) const;
95 bool onlyScaleAndTranslation(double eps = EPSILON) const;
96 bool isSingular(double eps = EPSILON) const;
98 bool flips() const;
100 Matrix without_translation() const;
102 Matrix inverse() const;
104 Coord det() const;
105 Coord descrim2() const;
106 Coord descrim() const;
107 };
109 Matrix operator*(Matrix const &a, Matrix const &b);
110 inline Matrix &operator*=(Matrix &a, Matrix const &b) { a = a * b; return a; }
112 /** A function to print out the Matrix (for debugging) */
113 inline std::ostream &operator<< (std::ostream &out_file, const Geom::Matrix &m) {
114 out_file << "A: " << m[0] << " C: " << m[2] << " E: " << m[4] << "\n";
115 out_file << "B: " << m[1] << " D: " << m[3] << " F: " << m[5] << "\n";
116 return out_file;
117 }
119 /** Given a matrix m such that unit_circle = m*x, this returns the
120 * quadratic form x*A*x = 1. */
121 Matrix elliptic_quadratic_form(Matrix const &m);
123 /** Given a matrix (ignoring the translation) this returns the eigen
124 * values and vectors. */
125 class Eigen{
126 public:
127 Point vectors[2];
128 double values[2];
129 Eigen(Matrix const &m);
130 };
132 // Matrix factories
133 Matrix from_basis(const Point x_basis, const Point y_basis, const Point offset=Point(0,0));
135 /** Returns the Identity Matrix. */
136 inline Matrix identity() {
137 return Matrix(1.0, 0.0,
138 0.0, 1.0,
139 0.0, 0.0);
140 }
142 inline bool operator==(Matrix const &a, Matrix const &b) {
143 for(unsigned i = 0; i < 6; ++i) {
144 if ( a[i] != b[i] ) return false;
145 }
146 return true;
147 }
148 inline bool operator!=(Matrix const &a, Matrix const &b) { return !( a == b ); }
152 } /* namespace Geom */
154 #endif /* !__Geom_MATRIX_H__ */
156 /*
157 Local Variables:
158 mode:c++
159 c-file-style:"stroustrup"
160 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
161 indent-tabs-mode:nil
162 fill-column:99
163 End:
164 */
165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :