| Line | Branch | Exec | Source |
|---|---|---|---|
| 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. */ | ||
| 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. | ||
| 55 | 10000 | Point &Point::operator*=(Translate const &t) | |
| 56 | { | ||
| 57 | 10000 | _pt[X] += t.vec[X]; | |
| 58 | 10000 | _pt[Y] += t.vec[Y]; | |
| 59 | 10000 | return *this; | |
| 60 | } | ||
| 61 | 20018 | Point &Point::operator*=(Scale const &s) | |
| 62 | { | ||
| 63 | 20018 | _pt[X] *= s.vec[X]; | |
| 64 | 20018 | _pt[Y] *= s.vec[Y]; | |
| 65 | 20018 | return *this; | |
| 66 | } | ||
| 67 | 30433 | Point &Point::operator*=(Rotate const &r) | |
| 68 | { | ||
| 69 | 30433 | double x = _pt[X], y = _pt[Y]; | |
| 70 | 30433 | _pt[X] = x * r.vec[X] - y * r.vec[Y]; | |
| 71 | 30433 | _pt[Y] = y * r.vec[X] + x * r.vec[Y]; | |
| 72 | 30433 | return *this; | |
| 73 | } | ||
| 74 | ✗ | Point &Point::operator*=(HShear const &h) | |
| 75 | { | ||
| 76 | ✗ | _pt[X] += h.f * _pt[X]; | |
| 77 | ✗ | return *this; | |
| 78 | } | ||
| 79 | ✗ | Point &Point::operator*=(VShear const &v) | |
| 80 | { | ||
| 81 | ✗ | _pt[Y] += v.f * _pt[Y]; | |
| 82 | ✗ | return *this; | |
| 83 | } | ||
| 84 | 2 | Point &Point::operator*=(Zoom const &z) | |
| 85 | { | ||
| 86 | 2 | _pt[X] += z._trans[X]; | |
| 87 | 2 | _pt[Y] += z._trans[Y]; | |
| 88 | 2 | _pt[X] *= z._scale; | |
| 89 | 2 | _pt[Y] *= z._scale; | |
| 90 | 2 | return *this; | |
| 91 | } | ||
| 92 | |||
| 93 | // Affine multiplication methods. | ||
| 94 | |||
| 95 | /** @brief Combine this transformation with a translation. */ | ||
| 96 | 29 | Affine &Affine::operator*=(Translate const &t) { | |
| 97 | 29 | _c[4] += t[X]; | |
| 98 | 29 | _c[5] += t[Y]; | |
| 99 | 29 | return *this; | |
| 100 | } | ||
| 101 | |||
| 102 | /** @brief Combine this transformation with scaling. */ | ||
| 103 | 45009 | Affine &Affine::operator*=(Scale const &s) { | |
| 104 | 45009 | _c[0] *= s[X]; _c[1] *= s[Y]; | |
| 105 | 45009 | _c[2] *= s[X]; _c[3] *= s[Y]; | |
| 106 | 45009 | _c[4] *= s[X]; _c[5] *= s[Y]; | |
| 107 | 45009 | return *this; | |
| 108 | } | ||
| 109 | |||
| 110 | /** @brief Combine this transformation a rotation. */ | ||
| 111 | 428082 | 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 |
1/2✓ Branch 3 taken 428082 times.
✗ Branch 4 not taken.
|
428082 | *this *= (Affine) r; |
| 115 | 428082 | return *this; | |
| 116 | } | ||
| 117 | |||
| 118 | /** @brief Combine this transformation with horizontal shearing (skew). */ | ||
| 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). */ | ||
| 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 | |||
| 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 | |||
| 142 | 2 | Affine Rotate::around(Point const &p, Coord angle) | |
| 143 | { | ||
| 144 |
3/6✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 2 times.
✗ Branch 16 not taken.
|
2 | Affine result = Translate(-p) * Rotate(angle) * Translate(p); |
| 145 | 2 | return result; | |
| 146 | } | ||
| 147 | |||
| 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! | ||
| 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 |