Code

remove many needless references to n-art-bpath.h
[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 "svg/svg.h"
16 #include "ui/widget/scalar.h"
18 #include <2geom/sbasis.h>
19 #include <2geom/sbasis-geometric.h>
20 #include <2geom/bezier-to-sbasis.h>
21 #include <2geom/sbasis-to-bezier.h>
22 #include <2geom/d2.h>
23 #include <2geom/piecewise.h>
25 #include <algorithm>
26 using std::vector;
28 namespace Inkscape {
29 namespace LivePathEffect {
31 LPEEnvelope::LPEEnvelope(LivePathEffectObject *lpeobject) :
32     Effect(lpeobject),
33     bend_path1(_("Top bend path"), _("Top path along which to bend the original path"), "bendpath1", &wr, this, "M0,0 L1,0"),
34     bend_path2(_("Right bend path"), _("Right path along which to bend the original path"), "bendpath2", &wr, this, "M0,0 L1,0"),
35     bend_path3(_("Bottom bend path"), _("Bottom path along which to bend the original path"), "bendpath3", &wr, this, "M0,0 L1,0"),
36     bend_path4(_("Left bend path"), _("Left path along which to bend the original path"), "bendpath4", &wr, this, "M0,0 L1,0"),
37     xx(_("Enable left & right paths"), _("Enable the left and right deformation paths"), "xx", &wr, this, true),
38     yy(_("Enable top & bottom paths"), _("Enable the top and bottom deformation paths"), "yy", &wr, this, true)
39 {
40     registerParameter( dynamic_cast<Parameter *>(&yy) );
41     registerParameter( dynamic_cast<Parameter *>(&xx) );
42     registerParameter( dynamic_cast<Parameter *>(&bend_path1) );
43     registerParameter( dynamic_cast<Parameter *>(&bend_path2) );
44     registerParameter( dynamic_cast<Parameter *>(&bend_path3) );
45     registerParameter( dynamic_cast<Parameter *>(&bend_path4) );
46     concatenate_before_pwd2 = true;
47 }
49 LPEEnvelope::~LPEEnvelope()
50 {
52 }
54 void
55 LPEEnvelope::doBeforeEffect (SPLPEItem *lpeitem)
56 {
57     // get the item bounding box
58     original_bbox(lpeitem);
59 }
61 Geom::Piecewise<Geom::D2<Geom::SBasis> >
62 LPEEnvelope::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
63 {
65     if(xx.get_value() == false && yy.get_value() == false)
66     {
67         return pwd2_in;
68     }
70     using namespace Geom;
72     /*
73     The code below is inspired from the Bend Path code developed by jfb and mgsloan
74     Please, read it before tring to understand this one
75     */
77     Piecewise<D2<SBasis> > uskeleton1 = arc_length_parametrization(bend_path1.get_pwd2(),2,.1);
78     uskeleton1 = remove_short_cuts(uskeleton1,.01);
79     Piecewise<D2<SBasis> > n1 = rot90(derivative(uskeleton1));
80     n1 = force_continuity(remove_short_cuts(n1,.1));
82     Piecewise<D2<SBasis> > uskeleton2 = arc_length_parametrization(bend_path2.get_pwd2(),2,.1);
83     uskeleton2 = remove_short_cuts(uskeleton2,.01);
84     Piecewise<D2<SBasis> > n2 = rot90(derivative(uskeleton2));
85     n2 = force_continuity(remove_short_cuts(n2,.1));
87     Piecewise<D2<SBasis> > uskeleton3 = arc_length_parametrization(bend_path3.get_pwd2(),2,.1);
88     uskeleton3 = remove_short_cuts(uskeleton3,.01);
89     Piecewise<D2<SBasis> > n3 = rot90(derivative(uskeleton3));
90     n3 = force_continuity(remove_short_cuts(n3,.1));
92     Piecewise<D2<SBasis> > uskeleton4 = arc_length_parametrization(bend_path4.get_pwd2(),2,.1);
93     uskeleton4 = remove_short_cuts(uskeleton4,.01);
94     Piecewise<D2<SBasis> > n4 = rot90(derivative(uskeleton4));
95     n4 = force_continuity(remove_short_cuts(n4,.1));
98     D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(pwd2_in);
99     Piecewise<SBasis> x = Piecewise<SBasis>(patternd2[0]);
100     Piecewise<SBasis> y = Piecewise<SBasis>(patternd2[1]);
102     /*The *1.001 is a hack to avoid a small bug : path at x=0 and y=0 don't work well. */
103     x-= boundingbox_X.min()*1.001;
104     y-= boundingbox_Y.min()*1.001;
106     Piecewise<SBasis> x1 = x ;
107     Piecewise<SBasis> y1 = y ;
109     Piecewise<SBasis> x2 = x ;
110     Piecewise<SBasis> y2 = y ;
111     x2 -= boundingbox_X.extent();
113     Piecewise<SBasis> x3 = x ;
114     Piecewise<SBasis> y3 = y ;
115     y3 -= boundingbox_Y.extent();
117     Piecewise<SBasis> x4 = x ;
118     Piecewise<SBasis> y4 = y ;
121     /*Scaling to the Bend Path length*/
122     double scaling1 = uskeleton1.cuts.back()/boundingbox_X.extent();
123     if (scaling1 != 1.0) {
124         x1*=scaling1;
125     }
127     double scaling2 = uskeleton2.cuts.back()/boundingbox_Y.extent();
128     if (scaling2 != 1.0) {
129         y2*=scaling2;
130     }
132     double scaling3 = uskeleton3.cuts.back()/boundingbox_X.extent();
133     if (scaling3 != 1.0) {
134         x3*=scaling3;
135     }
137     double scaling4 = uskeleton4.cuts.back()/boundingbox_Y.extent();
138     if (scaling4 != 1.0) {
139         y4*=scaling4;
140     }
144     Piecewise<SBasis> xbis = x;
145     Piecewise<SBasis> ybis = y;
146     xbis *= -1.0;
147     xbis += boundingbox_X.extent();
148     ybis *= -1.0;
149     ybis += boundingbox_Y.extent();
150     /* This is important : y + ybis = constant  and x +xbis = constant */
152     Piecewise<D2<SBasis> > output;
153     Piecewise<D2<SBasis> > output1;
154     Piecewise<D2<SBasis> > output2;
155     Piecewise<D2<SBasis> > output_x;
156     Piecewise<D2<SBasis> > output_y;
158     /*
159     output_y : Deformation by Up and Down Bend Paths
160     We use weighting : The closer a point is to a Band Path, the more it will be affected by this Bend Path.
161     This is done by the line "ybis*Derformation1 + y*Deformation2"
162     The result is a mix between the 2 deformed paths
163     */
164     output_y =  ybis*(compose((uskeleton1),x1) + y1*compose(n1,x1) )
165             +    y*(compose((uskeleton3),x3) + y3*compose(n3,x3) );
166     output_y /= (boundingbox_Y.extent());
167     if(xx.get_value() == false && yy.get_value() == true)
168     {
169             return output_y;
170     }
172     /*output_x : Deformation by Left and Right Bend Paths*/
173     output_x =    x*(compose((uskeleton2),y2) + -x2*compose(n2,y2) )
174             + xbis*(compose((uskeleton4),y4) + -x4*compose(n4,y4) );
175     output_x /= (boundingbox_X.extent());
176     if(xx.get_value() == true && yy.get_value() == false)
177     {
178             return output_x;
179     }
181     /*output : Deformation by Up, Left, Right and Down Bend Paths*/
182     if(xx.get_value() == true && yy.get_value() == true)
183     {
184         Piecewise<SBasis> xsqr = x*xbis; /* xsqr = x * (BBox_X - x) */
185         Piecewise<SBasis> ysqr = y*ybis; /* xsqr = y * (BBox_Y - y) */
186         Piecewise<SBasis> xsqrbis = xsqr;
187         Piecewise<SBasis> ysqrbis = ysqr;
188         xsqrbis *= -1;
189         xsqrbis += boundingbox_X.extent()*boundingbox_X.extent()/4.;
190         ysqrbis *= -1;
191         ysqrbis += boundingbox_Y.extent()*boundingbox_Y.extent()/4.;
192         /*This is important : xsqr + xsqrbis = constant*/
195         /*
196         Here we mix the last two results : output_x and output_y
197         output1 : The more a point is close to Up and Down, the less it will be affected by output_x.
198         (This is done with the polynomial function)
199         output2 : The more a point is close to Left and Right, the less it will be affected by output_y.
200         output : we do the mean between output1 and output2 for all points.
201         */
202         output1 =  (ysqrbis*output_y) + (ysqr*output_x);
203         output1 /= (boundingbox_Y.extent()*boundingbox_Y.extent()/4.);
205         output2 =  (xsqrbis*output_x) + (xsqr*output_y);
206         output2 /= (boundingbox_X.extent()*boundingbox_X.extent()/4.);
208         output = output1 + output2;
209         output /= 2.;
211         return output;
212         /*Of course, the result is not perfect, but on a graphical point of view, this is sufficent.*/
214     }
216     // do nothing when xx and yy are both false
217     return pwd2_in;
220 void
221 LPEEnvelope::resetDefaults(SPItem * item)
223     original_bbox(SP_LPE_ITEM(item));
225     Geom::Point Up_Left(boundingbox_X.min(), boundingbox_Y.min());
226     Geom::Point Up_Right(boundingbox_X.max(), boundingbox_Y.min());
227     Geom::Point Down_Left(boundingbox_X.min(), boundingbox_Y.max());
228     Geom::Point Down_Right(boundingbox_X.max(), boundingbox_Y.max());
230     Geom::Path path1;
231     path1.start( Up_Left );
232     path1.appendNew<Geom::LineSegment>( Up_Right );
233     bend_path1.set_new_value( path1.toPwSb(), true );
235     Geom::Path path2;
236     path2.start( Up_Right );
237     path2.appendNew<Geom::LineSegment>( Down_Right );
238     bend_path2.set_new_value( path2.toPwSb(), true );
240     Geom::Path path3;
241     path3.start( Down_Left );
242     path3.appendNew<Geom::LineSegment>( Down_Right );
243     bend_path3.set_new_value( path3.toPwSb(), true );
245     Geom::Path path4;
246     path4.start( Up_Left );
247     path4.appendNew<Geom::LineSegment>( Down_Left );
248     bend_path4.set_new_value( path4.toPwSb(), true );
251 void
252 LPEEnvelope::transform_multiply(Geom::Matrix const& postmul, bool set)
254     // TODO: implement correct transformation instead of this default behavior
255     Effect::transform_multiply(postmul, set);
259 } // namespace LivePathEffect
260 } /* namespace Inkscape */