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 "sp-shape.h"
11 #include "display/curve.h"
12 #include <libnr/n-art-bpath.h>
13 #include "live_effects/n-art-bpath-2geom.h"
14 #include "svg/svg.h"
15 #include "ui/widget/scalar.h"
17 #include <2geom/sbasis.h>
18 #include <2geom/sbasis-geometric.h>
19 #include <2geom/bezier-to-sbasis.h>
20 #include <2geom/sbasis-to-bezier.h>
21 #include <2geom/d2.h>
22 #include <2geom/piecewise.h>
24 #include <algorithm>
25 using std::vector;
28 /* Theory in e-mail from J.F. Barraud
29 Let B be the skeleton path, and P the pattern (the path to be deformed).
31 P is a map t --> P(t) = ( x(t), y(t) ).
32 B is a map t --> B(t) = ( a(t), b(t) ).
34 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
35 U(s) is s itself.
37 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).
39 The basic deformation associated to B is then given by:
41 (x,y) --> U(x)+y*N(x)
43 (i.e. we go for distance x along the path, and then for distance y along the normal)
45 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
46 first) but I think we can first forget about them.
47 */
49 namespace Inkscape {
50 namespace LivePathEffect {
52 static const Util::EnumData<PAPCopyType> PAPCopyTypeData[PAPCT_END] = {
53 {PAPCT_SINGLE, N_("Single"), "single"},
54 {PAPCT_SINGLE_STRETCHED, N_("Single, stretched"), "single_stretched"},
55 {PAPCT_REPEATED, N_("Repeated"), "repeated"},
56 {PAPCT_REPEATED_STRETCHED, N_("Repeated, stretched"), "repeated_stretched"}
57 };
58 static const Util::EnumDataConverter<PAPCopyType> PAPCopyTypeConverter(PAPCopyTypeData, PAPCT_END);
60 LPEPatternAlongPath::LPEPatternAlongPath(LivePathEffectObject *lpeobject) :
61 Effect(lpeobject),
62 pattern(_("Pattern source"), _("Path to put along the skeleton path"), "pattern", &wr, this, "M0,0 L1,0"),
63 copytype(_("Pattern copies"), _("How many pattern copies to place along the skeleton path"), "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"), _("Scale the width of the pattern in units of its length"), "scale_y_rel", &wr, this, false),
66 spacing(_("Spacing"), _("Space between copies of the pattern. Negative values allowed, but are limited to -90% of pattern width."), "spacing", &wr, this, 0),
67 normal_offset(_("Normal offset"), "", "normal_offset", &wr, this, 0),
68 tang_offset(_("Tangential offset"), "", "tang_offset", &wr, this, 0),
69 prop_units(_("Offsets in unit of pattern size"), _("Spacing, tangential and normal offset are expressed as a ratio of width/height"), "prop_units", &wr, this, false),
70 vertical_pattern(_("Pattern is vertical"), _("Rotate pattern 90 deg before applying"), "vertical_pattern", &wr, this, false)
71 {
72 registerParameter( dynamic_cast<Parameter *>(&pattern) );
73 registerParameter( dynamic_cast<Parameter *>(©type) );
74 registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
75 registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
76 registerParameter( dynamic_cast<Parameter *>(&spacing) );
77 registerParameter( dynamic_cast<Parameter *>(&normal_offset) );
78 registerParameter( dynamic_cast<Parameter *>(&tang_offset) );
79 registerParameter( dynamic_cast<Parameter *>(&prop_units) );
80 registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
82 prop_scale.param_set_digits(3);
83 prop_scale.param_set_increments(0.01, 0.10);
84 }
86 LPEPatternAlongPath::~LPEPatternAlongPath()
87 {
89 }
92 //TODO: does this already exist in 2Geom? if not, move this there...
93 static
94 std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > >
95 split_at_discontinuities (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwsbin, double tol = .0001)
96 {
97 using namespace Geom;
98 std::vector<Piecewise<D2<SBasis> > > ret;
99 unsigned piece_start = 0;
100 for (unsigned i=0; i<pwsbin.segs.size(); i++){
101 if (i==(pwsbin.segs.size()-1) || L2(pwsbin.segs[i].at1()- pwsbin.segs[i+1].at0()) > tol){
102 Piecewise<D2<SBasis> > piece;
103 piece.cuts.push_back(pwsbin.cuts[piece_start]);
104 for (unsigned j = piece_start; j<i+1; j++){
105 piece.segs.push_back(pwsbin.segs[j]);
106 piece.cuts.push_back(pwsbin.cuts[j+1]);
107 }
108 ret.push_back(piece);
109 piece_start = i+1;
110 }
111 }
112 return ret;
113 }
116 Geom::Piecewise<Geom::D2<Geom::SBasis> >
117 LPEPatternAlongPath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
118 {
119 using namespace Geom;
121 /* Much credit should go to jfb and mgsloan of lib2geom development for the code below! */
122 Piecewise<D2<SBasis> > output;
124 PAPCopyType type = copytype.get_value();
126 D2<Piecewise<SBasis> > patternd2 = make_cuts_independant(pattern.get_pwd2());
127 Piecewise<SBasis> x0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[1]) : Piecewise<SBasis>(patternd2[0]);
128 Piecewise<SBasis> y0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[0]) : Piecewise<SBasis>(patternd2[1]);
129 Interval pattBndsX = bounds_exact(x0);
130 x0 -= pattBndsX.min();
131 Interval pattBndsY = bounds_exact(y0);
132 y0 -= pattBndsY.middle();
134 double xspace = spacing;
135 double noffset = normal_offset;
136 double toffset = tang_offset;
137 if (prop_units.get_value()){
138 xspace *= pattBndsX.extent();
139 noffset *= pattBndsY.extent();
140 toffset *= pattBndsX.extent();
141 }
143 //Prevent more than 90% overlap...
144 if (xspace < -pattBndsX.extent()*.9) {
145 xspace = -pattBndsX.extent()*.9;
146 }
147 //TODO: dynamical update of parameter ranges?
148 //if (prop_units.get_value()){
149 // spacing.param_set_range(-.9, NR_HUGE);
150 // }else{
151 // spacing.param_set_range(-pattBndsX.extent()*.9, NR_HUGE);
152 // }
154 y0+=noffset;
156 std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > > paths_in;
157 paths_in = split_at_discontinuities(pwd2_in);
159 for (unsigned idx = 0; idx < paths_in.size(); idx++){
160 Geom::Piecewise<Geom::D2<Geom::SBasis> > path_i = paths_in[idx];
161 Piecewise<SBasis> x = x0;
162 Piecewise<SBasis> y = y0;
163 Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(path_i,2,.1);
164 uskeleton = remove_short_cuts(uskeleton,.01);
165 Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));
166 n = force_continuity(remove_short_cuts(n,.1));
168 int nbCopies = 0;
169 double scaling = 1;
170 switch(type) {
171 case PAPCT_REPEATED:
172 nbCopies = floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX.extent()+xspace));
173 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
174 break;
176 case PAPCT_SINGLE:
177 nbCopies = (toffset + pattBndsX.extent() < uskeleton.domain().extent()) ? 1 : 0;
178 break;
180 case PAPCT_SINGLE_STRETCHED:
181 nbCopies = 1;
182 scaling = (uskeleton.domain().extent() - toffset)/pattBndsX.extent();
183 break;
185 case PAPCT_REPEATED_STRETCHED:
186 // if uskeleton is closed:
187 if(path_i.segs.front().at0() == path_i.segs.back().at1()){
188 nbCopies = std::floor((uskeleton.domain().extent() - toffset)/(pattBndsX.extent()+xspace));
189 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
190 scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX.extent());
191 // if not closed: no space at the end
192 }else{
193 nbCopies = std::floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX.extent()+xspace));
194 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
195 scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX.extent() - xspace);
196 }
197 break;
199 default:
200 return pwd2_in;
201 };
203 double pattWidth = pattBndsX.extent() * scaling;
205 if (scaling != 1.0) {
206 x*=scaling;
207 }
208 if ( scale_y_rel.get_value() ) {
209 y*=(scaling*prop_scale);
210 } else {
211 if (prop_scale != 1.0) y *= prop_scale;
212 }
213 x += toffset;
215 double offs = 0;
216 for (int i=0; i<nbCopies; i++){
217 output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));
218 offs+=pattWidth;
219 }
220 }
221 return output;
222 }
224 void
225 LPEPatternAlongPath::transform_multiply(Geom::Matrix const& postmul, bool set)
226 {
227 // TODO: implement correct transformation instead of this default behavior
228 Effect::transform_multiply(postmul, set);
229 }
232 } // namespace LivePathEffect
233 } /* namespace Inkscape */
235 /*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :