Code

45b2b67b4505630f1cec7ac4cf49c3d8a9018f0c
[inkscape.git] / src / live_effects / lpe-patternalongpath.cpp
1 #define INKSCAPE_LPE_PATTERN_ALONG_PATH_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "live_effects/lpe-patternalongpath.h"
10 #include "live_effects/lpeobject.h"
11 #include "sp-shape.h"
12 #include "display/curve.h"
13 #include "svg/svg.h"
14 #include "ui/widget/scalar.h"
16 #include <2geom/sbasis.h>
17 #include <2geom/sbasis-geometric.h>
18 #include <2geom/bezier-to-sbasis.h>
19 #include <2geom/sbasis-to-bezier.h>
20 #include <2geom/d2.h>
21 #include <2geom/piecewise.h>
23 #include <algorithm>
24 using std::vector;
27 /* Theory in e-mail from J.F. Barraud
28 Let B be the skeleton path, and P the pattern (the path to be deformed).
30 P is a map t --> P(t) = ( x(t), y(t) ).
31 B is a map t --> B(t) = ( a(t), b(t) ).
33 The first step is to re-parametrize B by its arc length: this is the parametrization in which a point p on B is located by its distance s from start. One obtains a new map s --> U(s) = (a'(s),b'(s)), that still describes the same path B, but where the distance along B from start to
34 U(s) is s itself.
36 We also need a unit normal to the path. This can be obtained by computing a unit tangent vector, and rotate it by 90�. Call this normal vector N(s).
38 The basic deformation associated to B is then given by:
40    (x,y) --> U(x)+y*N(x)
42 (i.e. we go for distance x along the path, and then for distance y along the normal)
44 Of course this formula needs some minor adaptations (as is it depends on the absolute position of P for instance, so a little translation is needed
45 first) but I think we can first forget about them.
46 */
48 namespace Inkscape {
49 namespace LivePathEffect {
51 static const Util::EnumData<PAPCopyType> PAPCopyTypeData[PAPCT_END] = {
52     {PAPCT_SINGLE,               N_("Single"),               "single"},
53     {PAPCT_SINGLE_STRETCHED,     N_("Single, stretched"),    "single_stretched"},
54     {PAPCT_REPEATED,             N_("Repeated"),             "repeated"},
55     {PAPCT_REPEATED_STRETCHED,   N_("Repeated, stretched"),  "repeated_stretched"}
56 };
57 static const Util::EnumDataConverter<PAPCopyType> PAPCopyTypeConverter(PAPCopyTypeData, PAPCT_END);
59 LPEPatternAlongPath::LPEPatternAlongPath(LivePathEffectObject *lpeobject) :
60     Effect(lpeobject),
61     pattern(_("Pattern source"), _("Path to put along the skeleton path"), "pattern", &wr, this, "M0,0 L1,0"),
62     copytype(_("Pattern copies"), _("How many pattern copies to place along the skeleton path"),
63         "copytype", PAPCopyTypeConverter, &wr, this, PAPCT_SINGLE_STRETCHED),
64     prop_scale(_("Width"), _("Width of the pattern"), "prop_scale", &wr, this, 1),
65     scale_y_rel(_("Width in units of length"),
66         _("Scale the width of the pattern in units of its length"),
67         "scale_y_rel", &wr, this, false),
68     spacing(_("Spacing"),
69         // xgettext:no-c-format
70         _("Space between copies of the pattern. Negative values allowed, but are limited to -90% of pattern width."),
71         "spacing", &wr, this, 0),
72     normal_offset(_("Normal offset"), "", "normal_offset", &wr, this, 0),
73     tang_offset(_("Tangential offset"), "", "tang_offset", &wr, this, 0),
74     prop_units(_("Offsets in unit of pattern size"),
75         _("Spacing, tangential and normal offset are expressed as a ratio of width/height"),
76         "prop_units", &wr, this, false),
77     vertical_pattern(_("Pattern is vertical"), _("Rotate pattern 90 deg before applying"),
78         "vertical_pattern", &wr, this, false),
79     fuse_tolerance(_("Fuse nearby ends"), _("Fuse ends closer than this number. 0 means don't fuse."),
80         "fuse_tolerance", &wr, this, 0)
81 {
82     registerParameter( dynamic_cast<Parameter *>(&pattern) );
83     registerParameter( dynamic_cast<Parameter *>(&copytype) );
84     registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
85     registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
86     registerParameter( dynamic_cast<Parameter *>(&spacing) );
87     registerParameter( dynamic_cast<Parameter *>(&normal_offset) );
88     registerParameter( dynamic_cast<Parameter *>(&tang_offset) );
89     registerParameter( dynamic_cast<Parameter *>(&prop_units) );
90     registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
91     registerParameter( dynamic_cast<Parameter *>(&fuse_tolerance) );
93     prop_scale.param_set_digits(3);
94     prop_scale.param_set_increments(0.01, 0.10);
95 }
97 LPEPatternAlongPath::~LPEPatternAlongPath()
98 {
102 Geom::Piecewise<Geom::D2<Geom::SBasis> >
103 LPEPatternAlongPath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
105     using namespace Geom;
107     // Don't allow empty path parameter:
108     if ( pattern.get_pathvector().empty() ) {
109         return pwd2_in;
110     }
112 /* Much credit should go to jfb and mgsloan of lib2geom development for the code below! */
113     Piecewise<D2<SBasis> > output;
114     std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > > pre_output;
116     PAPCopyType type = copytype.get_value();
118     D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(pattern.get_pwd2());
119     Piecewise<SBasis> x0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[1]) : Piecewise<SBasis>(patternd2[0]);
120     Piecewise<SBasis> y0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[0]) : Piecewise<SBasis>(patternd2[1]);
121     OptInterval pattBndsX = bounds_exact(x0);
122     OptInterval pattBndsY = bounds_exact(y0);
123     if (pattBndsX && pattBndsY) {
124         x0 -= pattBndsX->min();
125         y0 -= pattBndsY->middle();
127         double xspace  = spacing;
128         double noffset = normal_offset;
129         double toffset = tang_offset;
130         if (prop_units.get_value() && pattBndsY){
131             xspace  *= pattBndsX->extent();
132             noffset *= pattBndsY->extent();
133             toffset *= pattBndsX->extent();
134         }
136         //Prevent more than 90% overlap...
137         if (xspace < -pattBndsX->extent()*.9) {
138             xspace = -pattBndsX->extent()*.9;
139         }
140         //TODO: dynamical update of parameter ranges?
141         //if (prop_units.get_value()){
142         //        spacing.param_set_range(-.9, NR_HUGE);
143         //    }else{
144         //        spacing.param_set_range(-pattBndsX.extent()*.9, NR_HUGE);
145         //    }
147         y0+=noffset;
149         std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > > paths_in;
150         paths_in = split_at_discontinuities(pwd2_in);
152         for (unsigned idx = 0; idx < paths_in.size(); idx++){
153             Geom::Piecewise<Geom::D2<Geom::SBasis> > path_i = paths_in[idx];
154             Piecewise<SBasis> x = x0;
155             Piecewise<SBasis> y = y0;
156             Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(path_i,2,.1);
157             uskeleton = remove_short_cuts(uskeleton,.01);
158             Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));
159             n = force_continuity(remove_short_cuts(n,.1));
160             
161             int nbCopies = 0;
162             double scaling = 1;
163             switch(type) {
164                 case PAPCT_REPEATED:
165                     nbCopies = static_cast<int>(floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX->extent()+xspace)));
166                     pattBndsX = Interval(pattBndsX->min(),pattBndsX->max()+xspace);
167                     break;
168                     
169                 case PAPCT_SINGLE:
170                     nbCopies = (toffset + pattBndsX->extent() < uskeleton.domain().extent()) ? 1 : 0;
171                     break;
172                     
173                 case PAPCT_SINGLE_STRETCHED:
174                     nbCopies = 1;
175                     scaling = (uskeleton.domain().extent() - toffset)/pattBndsX->extent();
176                     break;
177                     
178                 case PAPCT_REPEATED_STRETCHED:
179                     // if uskeleton is closed:
180                     if(path_i.segs.front().at0() == path_i.segs.back().at1()){
181                         nbCopies = static_cast<int>(std::floor((uskeleton.domain().extent() - toffset)/(pattBndsX->extent()+xspace)));
182                         pattBndsX = Interval(pattBndsX->min(),pattBndsX->max()+xspace);
183                         scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX->extent());
184                         // if not closed: no space at the end
185                     }else{
186                         nbCopies = static_cast<int>(std::floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX->extent()+xspace)));
187                         pattBndsX = Interval(pattBndsX->min(),pattBndsX->max()+xspace);
188                         scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX->extent() - xspace);
189                     }
190                     break;
191                     
192                 default:
193                     return pwd2_in;
194             };
195             
196             double pattWidth = pattBndsX->extent() * scaling;
197             
198             if (scaling != 1.0) {
199                 x*=scaling;
200             }
201             if ( scale_y_rel.get_value() ) {
202                 y*=(scaling*prop_scale);
203             } else {
204                 if (prop_scale != 1.0) y *= prop_scale;
205             }
206             x += toffset;
207             
208             double offs = 0;
209             for (int i=0; i<nbCopies; i++){
210                 if (fuse_tolerance > 0){        
211                     Geom::Piecewise<Geom::D2<Geom::SBasis> > output_piece = compose(uskeleton,x+offs)+y*compose(n,x+offs);
212                     std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > > splited_output_piece = split_at_discontinuities(output_piece);
213                     pre_output.insert(pre_output.end(), splited_output_piece.begin(), splited_output_piece.end() );
214                 }else{
215                     output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));
216                 }
217                 offs+=pattWidth;
218             }
219         }
220         if (fuse_tolerance > 0){        
221             pre_output = fuse_nearby_ends(pre_output, fuse_tolerance);
222             for (unsigned i=0; i<pre_output.size(); i++){
223                 output.concat(pre_output[i]);
224             }
225         }
226         return output;
227     } else {
228         return pwd2_in;
229     }
232 void
233 LPEPatternAlongPath::transform_multiply(Geom::Matrix const& postmul, bool set)
235     // overriding the Effect class default method, disabling transform forwarding to the parameters.
237     // only take translations into account
238     if (postmul.isTranslation()) {
239         pattern.param_transform_multiply(postmul, set);
240     }
243 } // namespace LivePathEffect
244 } /* namespace Inkscape */
246 /*
247   Local Variables:
248   mode:c++
249   c-file-style:"stroustrup"
250   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
251   indent-tabs-mode:nil
252   fill-column:99
253   End:
254 */
255 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :