1 /**
2  * @file
3  * @brief Affine transformation classes
4  *//*
5  * Authors:
6  *   ? <?@?.?>
7  *   Krzysztof Kosiński <tweenk.pl@gmail.com>
8  *   Johan Engelen
9  *
10  * Copyright ?-2012 Authors
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it either under the terms of the GNU Lesser General Public
14  * License version 2.1 as published by the Free Software Foundation
15  * (the "LGPL") or, at your option, under the terms of the Mozilla
16  * Public License Version 1.1 (the "MPL"). If you do not alter this
17  * notice, a recipient may use your version of this file under either
18  * the MPL or the LGPL.
19  *
20  * You should have received a copy of the LGPL along with this library
21  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  * You should have received a copy of the MPL along with this library
24  * in the file COPYING-MPL-1.1
25  *
26  * The contents of this file are subject to the Mozilla Public License
27  * Version 1.1 (the "License"); you may not use this file except in
28  * compliance with the License. You may obtain a copy of the License at
29  * http://www.mozilla.org/MPL/
30  *
31  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
32  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
33  * the specific language governing rights and limitations.
34  */
35 
36 #include <boost/concept_check.hpp>
37 #include <2geom/point.h>
38 #include <2geom/transforms.h>
39 #include <2geom/rect.h>
40 
41 namespace Geom {
42 
43 /** @brief Zoom between rectangles.
44  * Given two rectangles, compute a zoom that maps one to the other.
45  * Rectangles are assumed to have the same aspect ratio. */
map_rect(Rect const & old_r,Rect const & new_r)46 Zoom Zoom::map_rect(Rect const &old_r, Rect const &new_r)
47 {
48     Zoom ret;
49     ret._scale = new_r.width() / old_r.width();
50     ret._trans = new_r.min() - old_r.min();
51     return ret;
52 }
53 
54 // Point transformation methods.
operator *=(Translate const & t)55 Point &Point::operator*=(Translate const &t)
56 {
57     _pt[X] += t.vec[X];
58     _pt[Y] += t.vec[Y];
59     return *this;
60 }
operator *=(Scale const & s)61 Point &Point::operator*=(Scale const &s)
62 {
63     _pt[X] *= s.vec[X];
64     _pt[Y] *= s.vec[Y];
65     return *this;
66 }
operator *=(Rotate const & r)67 Point &Point::operator*=(Rotate const &r)
68 {
69     double x = _pt[X], y = _pt[Y];
70     _pt[X] = x * r.vec[X] - y * r.vec[Y];
71     _pt[Y] = y * r.vec[X] + x * r.vec[Y];
72     return *this;
73 }
operator *=(HShear const & h)74 Point &Point::operator*=(HShear const &h)
75 {
76     _pt[X] += h.f * _pt[X];
77     return *this;
78 }
operator *=(VShear const & v)79 Point &Point::operator*=(VShear const &v)
80 {
81     _pt[Y] += v.f * _pt[Y];
82     return *this;
83 }
operator *=(Zoom const & z)84 Point &Point::operator*=(Zoom const &z)
85 {
86     _pt[X] += z._trans[X];
87     _pt[Y] += z._trans[Y];
88     _pt[X] *= z._scale;
89     _pt[Y] *= z._scale;
90     return *this;
91 }
92 
93 // Affine multiplication methods.
94 
95 /** @brief Combine this transformation with a translation. */
operator *=(Translate const & t)96 Affine &Affine::operator*=(Translate const &t) {
97     _c[4] += t[X];
98     _c[5] += t[Y];
99     return *this;
100 }
101 
102 /** @brief Combine this transformation with scaling. */
operator *=(Scale const & s)103 Affine &Affine::operator*=(Scale const &s) {
104     _c[0] *= s[X]; _c[1] *= s[Y];
105     _c[2] *= s[X]; _c[3] *= s[Y];
106     _c[4] *= s[X]; _c[5] *= s[Y];
107     return *this;
108 }
109 
110 /** @brief Combine this transformation a rotation. */
operator *=(Rotate const & r)111 Affine &Affine::operator*=(Rotate const &r) {
112     // TODO: we just convert the Rotate to an Affine and use the existing operator*=()
113     // is there a better way?
114     *this *= (Affine) r;
115     return *this;
116 }
117 
118 /** @brief Combine this transformation with horizontal shearing (skew). */
operator *=(HShear const & h)119 Affine &Affine::operator*=(HShear const &h) {
120     _c[0] += h.f * _c[1];
121     _c[2] += h.f * _c[3];
122     _c[4] += h.f * _c[5];
123     return *this;
124 }
125 
126 /** @brief Combine this transformation with vertical shearing (skew). */
operator *=(VShear const & v)127 Affine &Affine::operator*=(VShear const &v) {
128     _c[1] += v.f * _c[0];
129     _c[3] += v.f * _c[2];
130     _c[5] += v.f * _c[4];
131     return *this;
132 }
133 
operator *=(Zoom const & z)134 Affine &Affine::operator*=(Zoom const &z) {
135     _c[0] *= z._scale; _c[1] *= z._scale;
136     _c[2] *= z._scale; _c[3] *= z._scale;
137     _c[4] += z._trans[X]; _c[5] += z._trans[Y];
138     _c[4] *= z._scale; _c[5] *= z._scale;
139     return *this;
140 }
141 
around(Point const & p,Coord angle)142 Affine Rotate::around(Point const &p, Coord angle)
143 {
144     Affine result = Translate(-p) * Rotate(angle) * Translate(p);
145     return result;
146 }
147 
reflection(Point const & vector,Point const & origin)148 Affine reflection(Point const & vector, Point const & origin)
149 {
150     Geom::Point vn = unit_vector(vector);
151     Coord cx2 = vn[X] * vn[X];
152     Coord cy2 = vn[Y] * vn[Y];
153     Coord c2xy = 2 * vn[X] * vn[Y];
154     Affine mirror ( cx2 - cy2, c2xy,
155                     c2xy, cy2 - cx2,
156                     0, 0 );
157     return Translate(-origin) * mirror * Translate(origin);
158 }
159 
160 // this checks whether the requirements of TransformConcept are satisfied for all transforms.
161 // if you add a new transform type, include it here!
check_transforms()162 void check_transforms()
163 {
164 #ifdef BOOST_CONCEPT_ASSERT
165     BOOST_CONCEPT_ASSERT((TransformConcept<Translate>));
166     BOOST_CONCEPT_ASSERT((TransformConcept<Scale>));
167     BOOST_CONCEPT_ASSERT((TransformConcept<Rotate>));
168     BOOST_CONCEPT_ASSERT((TransformConcept<HShear>));
169     BOOST_CONCEPT_ASSERT((TransformConcept<VShear>));
170     BOOST_CONCEPT_ASSERT((TransformConcept<Zoom>));
171     BOOST_CONCEPT_ASSERT((TransformConcept<Affine>)); // Affine is also a transform
172 #endif
173 
174     // check inter-transform multiplication
175     Affine m;
176     Translate t(Translate::identity());
177     Scale s(Scale::identity());
178     Rotate r(Rotate::identity());
179     HShear h(HShear::identity());
180     VShear v(VShear::identity());
181     Zoom z(Zoom::identity());
182 
183     // notice that the first column is always the same and enumerates all transform types,
184     // while the second one changes to each transform type in turn.
185     // cppcheck-suppress redundantAssignment
186     m = t * t; m = t * s; m = t * r; m = t * h; m = t * v; m = t * z;
187     m = s * t; m = s * s; m = s * r; m = s * h; m = s * v; m = s * z;
188     m = r * t; m = r * s; m = r * r; m = r * h; m = r * v; m = r * z;
189     m = h * t; m = h * s; m = h * r; m = h * h; m = h * v; m = h * z;
190     m = v * t; m = v * s; m = v * r; m = v * h; m = v * v; m = v * z;
191     m = z * t; m = z * s; m = z * r; m = z * h; m = z * v; m = z * z;
192 }
193 
194 } // namespace Geom
195 
196 /*
197   Local Variables:
198   mode:c++
199   c-file-style:"stroustrup"
200   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201   indent-tabs-mode:nil
202   fill-column:99
203   End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
206