transforms.cpp revision 4115d2a7efa05a72856652c38e52105903318912
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/**
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * @file
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * @brief Affine transformation classes
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *//*
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * Authors:
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * ? <?@?.?>
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * Krzysztof Kosiński <tweenk.pl@gmail.com>
4115d2a7efa05a72856652c38e52105903318912Johan Engelen * Johan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *
4115d2a7efa05a72856652c38e52105903318912Johan Engelen * Copyright ?-2012 Authors
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * This library is free software; you can redistribute it and/or
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * modify it either under the terms of the GNU Lesser General Public
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * License version 2.1 as published by the Free Software Foundation
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * (the "LGPL") or, at your option, under the terms of the Mozilla
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * Public License Version 1.1 (the "MPL"). If you do not alter this
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * notice, a recipient may use your version of this file under either
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * the MPL or the LGPL.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * You should have received a copy of the LGPL along with this library
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * in the file COPYING-LGPL-2.1; if not, write to the Free Software
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * You should have received a copy of the MPL along with this library
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * in the file COPYING-MPL-1.1
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * The contents of this file are subject to the Mozilla Public License
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * Version 1.1 (the "License"); you may not use this file except in
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * compliance with the License. You may obtain a copy of the License at
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * http://www.mozilla.org/MPL/
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * OF ANY KIND, either express or implied. See the LGPL or the MPL for
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen * the specific language governing rights and limitations.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen */
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen#include <boost/concept_check.hpp>
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen#include <2geom/point.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/transforms.h>
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński#include <2geom/rect.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Geom {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński/** @brief Zoom between rectangles.
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński * Given two rectangles, compute a zoom that maps one to the other.
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński * Rectangles are assumed to have the same aspect ratio. */
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof KosińskiZoom Zoom::map_rect(Rect const &old_r, Rect const &new_r)
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński{
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński Zoom ret;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński ret._scale = new_r.width() / old_r.width();
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński ret._trans = new_r.min() - old_r.min();
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński return ret;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński}
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen// Point transformation methods.
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenPoint &Point::operator*=(Translate const &t)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen{
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[X] += t.vec[X];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[Y] += t.vec[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenPoint &Point::operator*=(Scale const &s)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen{
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[X] *= s.vec[X];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[Y] *= s.vec[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenPoint &Point::operator*=(Rotate const &r)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen{
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen double x = _pt[X], y = _pt[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[X] = x * r.vec[X] - y * r.vec[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _pt[Y] = y * r.vec[X] + x * r.vec[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenPoint &Point::operator*=(HShear const &h)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[X] += h.f * _pt[X];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenPoint &Point::operator*=(VShear const &v)
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[Y] += v.f * _pt[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof KosińskiPoint &Point::operator*=(Zoom const &z)
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński{
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _pt[X] += z._trans[X];
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _pt[Y] += z._trans[Y];
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _pt[X] *= z._scale;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _pt[Y] *= z._scale;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński return *this;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen// Affine multiplication methods.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/** @brief Combine this transformation with a translation. */
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenAffine &Affine::operator*=(Translate const &t) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[4] += t[X];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[5] += t[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
c325659c5498684f90d96fc36b16ed0d3a906a87cilix}
c325659c5498684f90d96fc36b16ed0d3a906a87cilix
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/** @brief Combine this transformation with scaling. */
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenAffine &Affine::operator*=(Scale const &s) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[0] *= s[X]; _c[1] *= s[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[2] *= s[X]; _c[3] *= s[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[4] *= s[X]; _c[5] *= s[Y];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen}
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/** @brief Combine this transformation a rotation. */
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenAffine &Affine::operator*=(Rotate const &r) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen // TODO: we just convert the Rotate to an Affine and use the existing operator*=()
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen // is there a better way?
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen *this *= (Affine) r;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen}
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/** @brief Combine this transformation with horizontal shearing (skew). */
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenAffine &Affine::operator*=(HShear const &h) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[0] += h.f * _c[1];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[2] += h.f * _c[3];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[4] += h.f * _c[5];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen}
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen/** @brief Combine this transformation with vertical shearing (skew). */
d37634d73670180f99a3e0ea583621373d90ec4fJohan EngelenAffine &Affine::operator*=(VShear const &v) {
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[1] += v.f * _c[0];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[3] += v.f * _c[2];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen _c[5] += v.f * _c[4];
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen return *this;
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen}
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof KosińskiAffine &Affine::operator*=(Zoom const &z) {
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _c[0] *= z._scale; _c[1] *= z._scale;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _c[2] *= z._scale; _c[3] *= z._scale;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _c[4] += z._trans[X]; _c[5] += z._trans[Y];
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński _c[4] *= z._scale; _c[5] *= z._scale;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński return *this;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński}
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen// this checks whether the requirements of TransformConcept are satisfied for all transforms.
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen// if you add a new transform type, include it here!
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelenvoid check_transforms()
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen{
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen#ifdef BOOST_CONCEPT_ASSERT
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<Translate>));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<Scale>));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<Rotate>));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<HShear>));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<VShear>));
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński BOOST_CONCEPT_ASSERT((TransformConcept<Zoom>));
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen BOOST_CONCEPT_ASSERT((TransformConcept<Affine>)); // Affine is also a transform
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen#endif
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen // check inter-transform multiplication
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Affine m;
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Translate t(Translate::identity());
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Scale s(Scale::identity());
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen Rotate r(Rotate::identity());
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen HShear h(HShear::identity());
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen VShear v(VShear::identity());
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński Zoom z(Zoom::identity());
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen // notice that the first column is always the same and enumerates all transform types,
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen // while the second one changes to each transform type in turn.
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = t * t; m = t * s; m = t * r; m = t * h; m = t * v; m = t * z;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = s * t; m = s * s; m = s * r; m = s * h; m = s * v; m = s * z;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = r * t; m = r * s; m = r * r; m = r * h; m = r * v; m = r * z;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = h * t; m = h * s; m = h * r; m = h * h; m = h * v; m = h * z;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = v * t; m = v * s; m = v * r; m = v * h; m = v * v; m = v * z;
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński m = z * t; m = z * s; m = z * r; m = z * h; m = z * v; m = z * z;
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen}
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen
4115d2a7efa05a72856652c38e52105903318912Johan EngelenAffine reflection(Point const & vector, Point const & origin) {
4115d2a7efa05a72856652c38e52105903318912Johan Engelen Geom::Point vec_norm = unit_vector(vector);
4115d2a7efa05a72856652c38e52105903318912Johan Engelen Affine mirror ( vec_norm[X]*vec_norm[X] - vec_norm[Y]*vec_norm[Y], 2 * vec_norm[X] * vec_norm[Y] ,
4115d2a7efa05a72856652c38e52105903318912Johan Engelen 2 * vec_norm[X] * vec_norm[Y], vec_norm[Y]*vec_norm[Y] - vec_norm[X]*vec_norm[X] ,
4115d2a7efa05a72856652c38e52105903318912Johan Engelen 0 ,0 );
4115d2a7efa05a72856652c38e52105903318912Johan Engelen return Translate(-origin) * mirror * Translate(origin);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
4115d2a7efa05a72856652c38e52105903318912Johan Engelen} // namespace Geom
4115d2a7efa05a72856652c38e52105903318912Johan Engelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen/*
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen Local Variables:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen mode:c++
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-style:"stroustrup"
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen indent-tabs-mode:nil
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen fill-column:99
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen End:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen*/
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :