Code

Pot and Dutch translation update
[inkscape.git] / src / live_effects / lpe-mirror_symmetry.cpp
1 #define INKSCAPE_LPE_MIRROR_SYMMETRY_CPP
2 /** \file
3  * LPE <mirror_symmetry> implementation: mirrors a path with respect to a given line.
4  */
5 /*
6  * Authors:
7  *   Maximilian Albert
8  *   Johan Engelen
9  *
10  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
11  * Copyright (C) Maximilin Albert 2008 <maximilian.albert@gmail.com>
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include "live_effects/lpe-mirror_symmetry.h"
17 #include <sp-path.h>
18 #include <display/curve.h>
19 #include <svg/path-string.h>
21 #include <2geom/path.h>
22 #include <2geom/transforms.h>
23 #include <2geom/matrix.h>
25 namespace Inkscape {
26 namespace LivePathEffect {
28 LPEMirrorSymmetry::LPEMirrorSymmetry(LivePathEffectObject *lpeobject) :
29     Effect(lpeobject),
30     discard_orig_path(_("Discard original path?"), _("Check this to only keep the mirrored part of the path"), "discard_orig_path", &wr, this, false),
31     reflection_line(_("Reflection line"), _("Line which serves as 'mirror' for the reflection"), "reflection_line", &wr, this, "M0,0 L100,100")
32 {
33     show_orig_path = true;
35     registerParameter( dynamic_cast<Parameter *>(&discard_orig_path) );
36     registerParameter( dynamic_cast<Parameter *>(&reflection_line) );
37 }
39 LPEMirrorSymmetry::~LPEMirrorSymmetry()
40 {
41 }
43 void
44 LPEMirrorSymmetry::doOnApply (SPLPEItem *lpeitem)
45 {
46     using namespace Geom;
48     SPItem *item = SP_ITEM(lpeitem);
49     Geom::Matrix t = sp_item_i2d_affine(item);
50     Geom::Rect bbox = *item->getBounds(t); // fixme: what happens if getBounds does not return a valid rect?
52     Point A(bbox.left(), bbox.bottom());
53     Point B(bbox.left(), bbox.top());
54     A *= t;
55     B *= t;
56     Piecewise<D2<SBasis> > rline = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(A[X], B[X]), Linear(A[Y], B[Y])));
57     reflection_line.set_new_value(rline, true);
58 }
60 std::vector<Geom::Path>
61 LPEMirrorSymmetry::doEffect_path (std::vector<Geom::Path> const & path_in)
62 {
63     // Don't allow empty path parameter:
64     if ( reflection_line.get_pathvector().empty() ) {
65         return path_in;
66     }
68     std::vector<Geom::Path> path_out;
69     if (!discard_orig_path) {
70         path_out = path_in;
71     }
73     std::vector<Geom::Path> mline(reflection_line.get_pathvector());
74     Geom::Point A(mline.front().initialPoint());
75     Geom::Point B(mline.back().finalPoint());
77     Geom::Matrix m1(1.0, 0.0, 0.0, 1.0, A[0], A[1]);
78     double hyp = Geom::distance(A, B);
79     double c = (B[0] - A[0]) / hyp; // cos(alpha)
80     double s = (B[1] - A[1]) / hyp; // sin(alpha)
82     Geom::Matrix m2(c, -s, s, c, 0.0, 0.0);
83     Geom::Matrix sca(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
85     Geom::Matrix m = m1.inverse() * m2;
86     m = m * sca;
87     m = m * m2.inverse();
88     m = m * m1;
90     for (int i = 0; i < static_cast<int>(path_in.size()); ++i) {
91         path_out.push_back(path_in[i] * m);
92     }
94     return path_out;
95 }
97 } //namespace LivePathEffect
98 } /* namespace Inkscape */
100 /*
101   Local Variables:
102   mode:c++
103   c-file-style:"stroustrup"
104   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
105   indent-tabs-mode:nil
106   fill-column:99
107   End:
108 */
109 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :