Code

First step towards making helper paths for LPE items work better
[inkscape.git] / src / live_effects / lpe-mirror_reflect.cpp
1 #define INKSCAPE_LPE_MIRROR_REFLECT_CPP
2 /** \file
3  * LPE <mirror_reflection> 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_reflect.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 LPEMirrorReflect::LPEMirrorReflect(LivePathEffectObject *lpeobject) :
29     Effect(lpeobject),
30     reflection_line(_("Reflection line"), _("Line which serves as 'mirror' for the reflection"), "reflection_line", &wr, this, "M0,0 L100,100")
31 {
32     show_orig_path = true;
34     registerParameter( dynamic_cast<Parameter *>(&reflection_line) );
35 }
37 LPEMirrorReflect::~LPEMirrorReflect()
38 {
40 }
42 void
43 LPEMirrorReflect::acceptParamPath (SPPath *param_path) {
44     using namespace Geom;
46     SPCurve* curve = sp_path_get_curve_for_edit (param_path);
47     Geom::Point A(curve->first_point().to_2geom());
48     Geom::Point B(curve->last_point().to_2geom());
49     
50     Piecewise<D2<SBasis> > rline = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(A[X], B[X]), Linear(A[Y], B[Y])));
51     reflection_line.param_set_and_write_new_value(rline);
53     SP_OBJECT(param_path)->deleteObject(true);
55     // don't remove this; needed for cleanup tasks
56     Effect::acceptParamPath(param_path);
57 }
59 std::vector<Geom::Path>
60 LPEMirrorReflect::doEffect_path (std::vector<Geom::Path> const & path_in)
61 {
62     std::vector<Geom::Path> path_out;
64     std::vector<Geom::Path> mline(reflection_line.get_pathvector());
65     Geom::Point A(mline.front().initialPoint());
66     Geom::Point B(mline.back().finalPoint());
68     Geom::Matrix m1(1.0, 0.0, 0.0, 1.0, A[0], A[1]);
69     double hyp = Geom::distance(A, B);
70     double c = (B[0] - A[0]) / hyp; // cos(alpha)
71     double s = (B[1] - A[1]) / hyp; // sin(alpha)
73     Geom::Matrix m2(c, -s, s, c, 0.0, 0.0);
74     Geom::Matrix sca(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
76     Geom::Matrix m = m1.inverse() * m2;
77     m = m * sca;
78     m = m * m2.inverse();
79     m = m * m1;
81     for (int i = 0; i < path_in.size(); ++i) {
82         path_out.push_back(path_in[i] * m);
83     }
85     return path_out;
86 }
88 /* ######################## */
90 } //namespace LivePathEffect
91 } /* namespace Inkscape */
93 /*
94   Local Variables:
95   mode:c++
96   c-file-style:"stroustrup"
97   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
98   indent-tabs-mode:nil
99   fill-column:99
100   End:
101 */
102 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :