Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
transforms.cpp
Go to the documentation of this file.
1/*
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
41namespace Geom {
42
46Zoom 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.
55Point &Point::operator*=(Translate const &t)
56{
57 _pt[X] += t.vec[X];
58 _pt[Y] += t.vec[Y];
59 return *this;
60}
61Point &Point::operator*=(Scale const &s)
62{
63 _pt[X] *= s.vec[X];
64 _pt[Y] *= s.vec[Y];
65 return *this;
66}
67Point &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}
74Point &Point::operator*=(HShear const &h)
75{
76 _pt[X] += h.f * _pt[X];
77 return *this;
78}
79Point &Point::operator*=(VShear const &v)
80{
81 _pt[Y] += v.f * _pt[Y];
82 return *this;
83}
84Point &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
97 _c[4] += t[X];
98 _c[5] += t[Y];
99 return *this;
100}
101
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
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
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
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
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
143{
145 return result;
146}
147
148Affine 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!
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;
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 :
Cartesian point / 2D vector and related operations.
Point origin
Definition aa.cpp:227
3x3 matrix representing an affine transformation.
Definition affine.h:70
Coord _c[6]
Definition affine.h:71
Affine & operator*=(Affine const &m)
Combine this transformation with another one.
Definition affine.cpp:442
C width() const
Get the horizontal extent of the rectangle.
CPoint min() const
Get the corner of the rectangle with smallest coordinate values.
Horizontal shearing.
Definition transforms.h:257
Two-dimensional point that doubles as a vector.
Definition point.h:66
Coord _pt[2]
Definition point.h:67
constexpr Coord y() const noexcept
Definition point.h:106
constexpr Coord x() const noexcept
Definition point.h:104
Axis aligned, non-empty rectangle.
Definition rect.h:92
Rotation around the origin.
Definition transforms.h:187
static Affine around(Point const &p, Coord angle)
static Rotate identity()
Get a zero-degree rotation.
Definition transforms.h:215
Coord angle() const
Definition transforms.h:204
Rotate()=default
Construct a zero-degree rotation.
Scaling from the origin.
Definition transforms.h:150
static Scale identity()
Definition transforms.h:173
static HShear identity()
Definition transforms.h:245
Translation by a vector.
Definition transforms.h:115
static Translate identity()
Get a translation that doesn't do anything.
Definition transforms.h:135
Vertical shearing.
Definition transforms.h:274
Combination of a translation and uniform scale.
Definition transforms.h:292
static Zoom identity()
Definition transforms.h:321
static Zoom map_rect(Rect const &old_r, Rect const &new_r)
Zoom between rectangles.
Point _trans
Definition transforms.h:294
Coord _scale
Definition transforms.h:293
Css & result
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Affine reflection(Point const &vector, Point const &origin)
Reflects objects about line.
Various utility functions.
Definition affine.h:22
void check_transforms()
Point unit_vector(Point const &a)
Axis-aligned rectangle.
Type requirements for transforms.
Definition transforms.h:50
Affine transformation classes.