Code

Rename LPE: mirror reflect --> mirror symmetry
[inkscape.git] / src / live_effects / lpe-envelope.cpp
1 #define INKSCAPE_LPE_ENVELOPE_CPP
3 /*
4  * Copyright (C) Steren Giannini 2008 <steren.giannini@gmail.com>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "live_effects/lpe-envelope.h"
10 #include "sp-shape.h"
11 #include "sp-item.h"
12 #include "sp-path.h"
13 #include "sp-item-group.h"
14 #include "display/curve.h"
15 #include <libnr/n-art-bpath.h>
16 #include <libnr/nr-matrix-fns.h>
17 #include "libnr/n-art-bpath-2geom.h"
18 #include "svg/svg.h"
19 #include "ui/widget/scalar.h"
21 #include <2geom/sbasis.h>
22 #include <2geom/sbasis-geometric.h>
23 #include <2geom/bezier-to-sbasis.h>
24 #include <2geom/sbasis-to-bezier.h>
25 #include <2geom/d2.h>
26 #include <2geom/piecewise.h>
28 #include <algorithm>
29 using std::vector;
31 namespace Inkscape {
32 namespace LivePathEffect {
34 LPEEnvelope::LPEEnvelope(LivePathEffectObject *lpeobject) :
35     Effect(lpeobject),
36     bend_path1(_("Top bend path"), _("Top path along which to bend the original path"), "bendpath1", &wr, this, "M0,0 L1,0"),
37     bend_path2(_("Right bend path"), _("Right path along which to bend the original path"), "bendpath2", &wr, this, "M0,0 L1,0"),
38     bend_path3(_("Bottom bend path"), _("Bottom path along which to bend the original path"), "bendpath3", &wr, this, "M0,0 L1,0"),
39     bend_path4(_("Left bend path"), _("Left path along which to bend the original path"), "bendpath4", &wr, this, "M0,0 L1,0"),
40     xx(_("Enable left & right paths"), _("Enable the left and right deformation paths"), "xx", &wr, this, true),
41     yy(_("Enable top & bottom paths"), _("Enable the top and bottom deformation paths"), "yy", &wr, this, true)
42 {
43     registerParameter( dynamic_cast<Parameter *>(&yy) );
44     registerParameter( dynamic_cast<Parameter *>(&xx) );
45     registerParameter( dynamic_cast<Parameter *>(&bend_path1) );
46     registerParameter( dynamic_cast<Parameter *>(&bend_path2) );
47     registerParameter( dynamic_cast<Parameter *>(&bend_path3) );
48     registerParameter( dynamic_cast<Parameter *>(&bend_path4) );
49     concatenate_before_pwd2 = true;
50 }
52 LPEEnvelope::~LPEEnvelope()
53 {
55 }
57 void
58 LPEEnvelope::doBeforeEffect (SPLPEItem *lpeitem)
59 {
60     // get the item bounding box
61     original_bbox(lpeitem);
62 }
64 Geom::Piecewise<Geom::D2<Geom::SBasis> >
65 LPEEnvelope::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
66 {
68     if(xx.get_value() == false && yy.get_value() == false)
69     {
70         return pwd2_in;
71     }
73     using namespace Geom;
75     /*
76     The code below is inspired from the Bend Path code developed by jfb and mgsloan
77     Please, read it before tring to understand this one
78     */
80     Piecewise<D2<SBasis> > uskeleton1 = arc_length_parametrization(bend_path1.get_pwd2(),2,.1);
81     uskeleton1 = remove_short_cuts(uskeleton1,.01);
82     Piecewise<D2<SBasis> > n1 = rot90(derivative(uskeleton1));
83     n1 = force_continuity(remove_short_cuts(n1,.1));
85     Piecewise<D2<SBasis> > uskeleton2 = arc_length_parametrization(bend_path2.get_pwd2(),2,.1);
86     uskeleton2 = remove_short_cuts(uskeleton2,.01);
87     Piecewise<D2<SBasis> > n2 = rot90(derivative(uskeleton2));
88     n2 = force_continuity(remove_short_cuts(n2,.1));
90     Piecewise<D2<SBasis> > uskeleton3 = arc_length_parametrization(bend_path3.get_pwd2(),2,.1);
91     uskeleton3 = remove_short_cuts(uskeleton3,.01);
92     Piecewise<D2<SBasis> > n3 = rot90(derivative(uskeleton3));
93     n3 = force_continuity(remove_short_cuts(n3,.1));
95     Piecewise<D2<SBasis> > uskeleton4 = arc_length_parametrization(bend_path4.get_pwd2(),2,.1);
96     uskeleton4 = remove_short_cuts(uskeleton4,.01);
97     Piecewise<D2<SBasis> > n4 = rot90(derivative(uskeleton4));
98     n4 = force_continuity(remove_short_cuts(n4,.1));
101     D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(pwd2_in);
102     Piecewise<SBasis> x = Piecewise<SBasis>(patternd2[0]);
103     Piecewise<SBasis> y = Piecewise<SBasis>(patternd2[1]);
105     /*The *1.001 is a hack to avoid a small bug : path at x=0 and y=0 don't work well. */
106     x-= boundingbox_X.min()*1.001;
107     y-= boundingbox_Y.min()*1.001;
109     Piecewise<SBasis> x1 = x ;
110     Piecewise<SBasis> y1 = y ;
112     Piecewise<SBasis> x2 = x ;
113     Piecewise<SBasis> y2 = y ;
114     x2 -= boundingbox_X.extent();
116     Piecewise<SBasis> x3 = x ;
117     Piecewise<SBasis> y3 = y ;
118     y3 -= boundingbox_Y.extent();
120     Piecewise<SBasis> x4 = x ;
121     Piecewise<SBasis> y4 = y ;
124     /*Scaling to the Bend Path length*/
125     double scaling1 = uskeleton1.cuts.back()/boundingbox_X.extent();
126     if (scaling1 != 1.0) {
127         x1*=scaling1;
128     }
130     double scaling2 = uskeleton2.cuts.back()/boundingbox_Y.extent();
131     if (scaling2 != 1.0) {
132         y2*=scaling2;
133     }
135     double scaling3 = uskeleton3.cuts.back()/boundingbox_X.extent();
136     if (scaling3 != 1.0) {
137         x3*=scaling3;
138     }
140     double scaling4 = uskeleton4.cuts.back()/boundingbox_Y.extent();
141     if (scaling4 != 1.0) {
142         y4*=scaling4;
143     }
147     Piecewise<SBasis> xbis = x;
148     Piecewise<SBasis> ybis = y;
149     xbis *= -1.0;
150     xbis += boundingbox_X.extent();
151     ybis *= -1.0;
152     ybis += boundingbox_Y.extent();
153     /* This is important : y + ybis = constant  and x +xbis = constant */
155     Piecewise<D2<SBasis> > output;
156     Piecewise<D2<SBasis> > output1;
157     Piecewise<D2<SBasis> > output2;
158     Piecewise<D2<SBasis> > output_x;
159     Piecewise<D2<SBasis> > output_y;
161     /*
162     output_y : Deformation by Up and Down Bend Paths
163     We use weighting : The closer a point is to a Band Path, the more it will be affected by this Bend Path.
164     This is done by the line "ybis*Derformation1 + y*Deformation2"
165     The result is a mix between the 2 deformed paths
166     */
167     output_y =  ybis*(compose((uskeleton1),x1) + y1*compose(n1,x1) )
168             +    y*(compose((uskeleton3),x3) + y3*compose(n3,x3) );
169     output_y /= (boundingbox_Y.extent());
170     if(xx.get_value() == false && yy.get_value() == true)
171     {
172             return output_y;
173     }
175     /*output_x : Deformation by Left and Right Bend Paths*/
176     output_x =    x*(compose((uskeleton2),y2) + -x2*compose(n2,y2) )
177             + xbis*(compose((uskeleton4),y4) + -x4*compose(n4,y4) );
178     output_x /= (boundingbox_X.extent());
179     if(xx.get_value() == true && yy.get_value() == false)
180     {
181             return output_x;
182     }
184     /*output : Deformation by Up, Left, Right and Down Bend Paths*/
185     if(xx.get_value() == true && yy.get_value() == true)
186     {
187         Piecewise<SBasis> xsqr = x*xbis; /* xsqr = x * (BBox_X - x) */
188         Piecewise<SBasis> ysqr = y*ybis; /* xsqr = y * (BBox_Y - y) */
189         Piecewise<SBasis> xsqrbis = xsqr;
190         Piecewise<SBasis> ysqrbis = ysqr;
191         xsqrbis *= -1;
192         xsqrbis += boundingbox_X.extent()*boundingbox_X.extent()/4.;
193         ysqrbis *= -1;
194         ysqrbis += boundingbox_Y.extent()*boundingbox_Y.extent()/4.;
195         /*This is important : xsqr + xsqrbis = constant*/
198         /*
199         Here we mix the last two results : output_x and output_y
200         output1 : The more a point is close to Up and Down, the less it will be affected by output_x.
201         (This is done with the polynomial function)
202         output2 : The more a point is close to Left and Right, the less it will be affected by output_y.
203         output : we do the mean between output1 and output2 for all points.
204         */
205         output1 =  (ysqrbis*output_y) + (ysqr*output_x);
206         output1 /= (boundingbox_Y.extent()*boundingbox_Y.extent()/4.);
208         output2 =  (xsqrbis*output_x) + (xsqr*output_y);
209         output2 /= (boundingbox_X.extent()*boundingbox_X.extent()/4.);
211         output = output1 + output2;
212         output /= 2.;
214         return output;
215         /*Of course, the result is not perfect, but on a graphical point of view, this is sufficent.*/
217     }
219     // do nothing when xx and yy are both false
220     return pwd2_in;
223 void
224 LPEEnvelope::resetDefaults(SPItem * item)
226     original_bbox(SP_LPE_ITEM(item));
228     Geom::Point Up_Left(boundingbox_X.min(), boundingbox_Y.min());
229     Geom::Point Up_Right(boundingbox_X.max(), boundingbox_Y.min());
230     Geom::Point Down_Left(boundingbox_X.min(), boundingbox_Y.max());
231     Geom::Point Down_Right(boundingbox_X.max(), boundingbox_Y.max());
233     Geom::Path path1;
234     path1.start( Up_Left );
235     path1.appendNew<Geom::LineSegment>( Up_Right );
236     bend_path1.param_set_and_write_new_value( path1.toPwSb() );
238     Geom::Path path2;
239     path2.start( Up_Right );
240     path2.appendNew<Geom::LineSegment>( Down_Right );
241     bend_path2.param_set_and_write_new_value( path2.toPwSb() );
243     Geom::Path path3;
244     path3.start( Down_Left );
245     path3.appendNew<Geom::LineSegment>( Down_Right );
246     bend_path3.param_set_and_write_new_value( path3.toPwSb() );
248     Geom::Path path4;
249     path4.start( Up_Left );
250     path4.appendNew<Geom::LineSegment>( Down_Left );
251     bend_path4.param_set_and_write_new_value( path4.toPwSb() );
254 void
255 LPEEnvelope::transform_multiply(Geom::Matrix const& postmul, bool set)
257     // TODO: implement correct transformation instead of this default behavior
258     Effect::transform_multiply(postmul, set);
262 } // namespace LivePathEffect
263 } /* namespace Inkscape */