29aab11aa20b28e0e12aa7ea3c8cbf55d157b532
1 /**
2 * \file
3 * \brief Transforms should be applied left to right. scale * translate means: first scale, then translate.
4 *
5 * Authors:
6 * ? <?@?.?>
7 *
8 * Copyright ?-? authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 *
33 */
35 #ifndef SEEN_Geom_TRANSFORMS_H
36 #define SEEN_Geom_TRANSFORMS_H
38 #include <2geom/matrix.h>
39 #include <cmath>
41 namespace Geom {
43 template <typename T>
44 struct TransformConcept {
45 T t;
46 Matrix m;
47 Point p;
48 void constraints() {
49 m = t; //implicit conversion
50 t = t.inverse();
51 p = p * t;
52 t = t * t;
53 }
54 };
57 class Rotate;
58 class Translate {
59 private:
60 Translate();
61 Point vec;
62 public:
63 explicit Translate(Point const &p) : vec(p) {}
64 explicit Translate(Coord const x, Coord const y) : vec(x, y) {}
65 inline operator Matrix() const { return Matrix(1, 0, 0, 1, vec[X], vec[Y]); }
67 inline Coord operator[](Dim2 const dim) const { return vec[dim]; }
68 inline Coord operator[](unsigned const dim) const { return vec[dim]; }
69 inline bool operator==(Translate const &o) const { return vec == o.vec; }
70 inline bool operator!=(Translate const &o) const { return vec != o.vec; }
72 inline Translate inverse() const { return Translate(-vec); }
74 friend Point operator*(Point const &v, Translate const &t);
75 inline Translate operator*(Translate const &b) const { return Translate(vec + b.vec); }
77 friend Matrix operator*(Translate const &t, Rotate const &r);
78 };
80 inline Point operator*(Point const &v, Translate const &t) { return v + t.vec; }
82 class Scale {
83 private:
84 Point vec;
85 Scale();
86 public:
87 explicit Scale(Point const &p) : vec(p) {}
88 Scale(Coord const x, Coord const y) : vec(x, y) {}
89 explicit Scale(Coord const s) : vec(s, s) {}
90 inline operator Matrix() const { return Matrix(vec[X], 0, 0, vec[Y], 0, 0); }
92 inline Coord operator[](Dim2 const d) const { return vec[d]; }
93 inline Coord operator[](unsigned const d) const { return vec[d]; }
94 //TODO: should we keep these mutators? add them to the other transforms?
95 inline Coord &operator[](Dim2 const d) { return vec[d]; }
96 inline Coord &operator[](unsigned const d) { return vec[d]; }
97 inline bool operator==(Scale const &o) const { return vec == o.vec; }
98 inline bool operator!=(Scale const &o) const { return vec != o.vec; }
100 inline Scale inverse() const { return Scale(1./vec[0], 1./vec[1]); }
102 friend Point operator*(Point const &v, Translate const &t);
103 inline Scale operator*(Scale const &b) const { return Scale(vec[X]*b[X], vec[Y]*b[Y]); }
104 };
106 inline Point operator*(Point const &p, Scale const &s) { return Point(p[X] * s[X], p[Y] * s[Y]); }
108 /** Notionally an Geom::Matrix corresponding to rotation about the origin.
109 Behaves like Geom::Matrix for multiplication.
110 **/
111 class Rotate {
112 private:
113 Rotate() {}
114 Point vec;
115 public:
116 explicit Rotate(Coord theta) : vec(std::cos(theta), std::sin(theta)) {}
117 Rotate(Point const &p) {Point v = p; v.normalize(); vec = v;} //TODO: UGLY!
118 explicit Rotate(Coord x, Coord y) { Rotate(Point(x, y)); }
119 inline operator Matrix() const { return Matrix(vec[X], vec[Y], -vec[Y], vec[X], 0, 0); }
121 inline Point vector() const { return vec; }
122 inline Coord operator[](Dim2 const dim) const { return vec[dim]; }
123 inline Coord operator[](unsigned const dim) const { return vec[dim]; }
124 inline bool operator==(Rotate const &o) const { return vec == o.vec; }
125 inline bool operator!=(Rotate const &o) const { return vec != o.vec; }
127 inline Rotate inverse() const {
128 Rotate r;
129 r.vec = Point(vec[X], -vec[Y]);
130 return r;
131 }
132 static Rotate from_degrees(Coord deg) {
133 Coord rad = (deg / 180.0) * M_PI;
134 return Rotate(rad);
135 }
137 friend Point operator*(Point const &v, Rotate const &r);
138 inline Rotate operator*(Rotate const &b) const { return Rotate(vec * b); }
139 };
141 inline Point operator*(Point const &v, Rotate const &r) { return v ^ r.vec; }
143 Matrix operator*(Translate const &t, Scale const &s);
144 Matrix operator*(Translate const &t, Rotate const &r);
146 Matrix operator*(Scale const &s, Translate const &t);
147 Matrix operator*(Scale const &s, Matrix const &m);
149 Matrix operator*(Matrix const &m, Translate const &t);
150 Matrix operator*(Matrix const &m, Scale const &s);
151 Matrix operator*(Matrix const &m, Rotate const &r);
152 Matrix operator*(Matrix const &m1, Matrix const &m2);
154 Translate pow(Translate const &t, int n);
155 Scale pow(Scale const &t, int n);
156 Rotate pow(Rotate t, int n);
157 Matrix pow(Matrix t, int n);
159 //TODO: matrix to trans/scale/rotate
161 } /* namespace Geom */
164 #endif /* !SEEN_Geom_TRANSFORMS_H */
166 /*
167 Local Variables:
168 mode:c++
169 c-file-style:"stroustrup"
170 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
171 indent-tabs-mode:nil
172 fill-column:99
173 End:
174 */
175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :