30ec589afc2162bd5625efd3f424028d46262b2d
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 <libnr/n-art-bpath.h>
14 #include "libnr/n-art-bpath-2geom.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;
29 /* Theory in e-mail from J.F. Barraud
30 Let B be the skeleton path, and P the pattern (the path to be deformed).
32 P is a map t --> P(t) = ( x(t), y(t) ).
33 B is a map t --> B(t) = ( a(t), b(t) ).
35 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
36 U(s) is s itself.
38 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).
40 The basic deformation associated to B is then given by:
42 (x,y) --> U(x)+y*N(x)
44 (i.e. we go for distance x along the path, and then for distance y along the normal)
46 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
47 first) but I think we can first forget about them.
48 */
50 namespace Inkscape {
51 namespace LivePathEffect {
53 static const Util::EnumData<PAPCopyType> PAPCopyTypeData[PAPCT_END] = {
54 {PAPCT_SINGLE, N_("Single"), "single"},
55 {PAPCT_SINGLE_STRETCHED, N_("Single, stretched"), "single_stretched"},
56 {PAPCT_REPEATED, N_("Repeated"), "repeated"},
57 {PAPCT_REPEATED_STRETCHED, N_("Repeated, stretched"), "repeated_stretched"}
58 };
59 static const Util::EnumDataConverter<PAPCopyType> PAPCopyTypeConverter(PAPCopyTypeData, PAPCT_END);
61 LPEPatternAlongPath::LPEPatternAlongPath(LivePathEffectObject *lpeobject) :
62 Effect(lpeobject),
63 pattern(_("Pattern source"), _("Path to put along the skeleton path"), "pattern", &wr, this, "M0,0 L1,0"),
64 copytype(_("Pattern copies"), _("How many pattern copies to place along the skeleton path"), "copytype", PAPCopyTypeConverter, &wr, this, PAPCT_SINGLE_STRETCHED),
65 prop_scale(_("Width"), _("Width of the pattern"), "prop_scale", &wr, this, 1),
66 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),
67 spacing(_("Spacing"), _("Space between copies of the pattern. Negative values allowed, but are limited to -90% of pattern width."), "spacing", &wr, this, 0),
68 normal_offset(_("Normal offset"), "", "normal_offset", &wr, this, 0),
69 tang_offset(_("Tangential offset"), "", "tang_offset", &wr, this, 0),
70 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),
71 vertical_pattern(_("Pattern is vertical"), _("Rotate pattern 90 deg before applying"), "vertical_pattern", &wr, this, false)
72 {
73 registerParameter( dynamic_cast<Parameter *>(&pattern) );
74 registerParameter( dynamic_cast<Parameter *>(©type) );
75 registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
76 registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
77 registerParameter( dynamic_cast<Parameter *>(&spacing) );
78 registerParameter( dynamic_cast<Parameter *>(&normal_offset) );
79 registerParameter( dynamic_cast<Parameter *>(&tang_offset) );
80 registerParameter( dynamic_cast<Parameter *>(&prop_units) );
81 registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
83 prop_scale.param_set_digits(3);
84 prop_scale.param_set_increments(0.01, 0.10);
85 }
87 LPEPatternAlongPath::~LPEPatternAlongPath()
88 {
90 }
92 Geom::Piecewise<Geom::D2<Geom::SBasis> >
93 LPEPatternAlongPath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
94 {
95 using namespace Geom;
97 /* Much credit should go to jfb and mgsloan of lib2geom development for the code below! */
98 Piecewise<D2<SBasis> > output;
100 PAPCopyType type = copytype.get_value();
102 D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(pattern.get_pwd2());
103 Piecewise<SBasis> x0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[1]) : Piecewise<SBasis>(patternd2[0]);
104 Piecewise<SBasis> y0 = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[0]) : Piecewise<SBasis>(patternd2[1]);
105 Interval pattBndsX = bounds_exact(x0);
106 x0 -= pattBndsX.min();
107 Interval pattBndsY = bounds_exact(y0);
108 y0 -= pattBndsY.middle();
110 double xspace = spacing;
111 double noffset = normal_offset;
112 double toffset = tang_offset;
113 if (prop_units.get_value()){
114 xspace *= pattBndsX.extent();
115 noffset *= pattBndsY.extent();
116 toffset *= pattBndsX.extent();
117 }
119 //Prevent more than 90% overlap...
120 if (xspace < -pattBndsX.extent()*.9) {
121 xspace = -pattBndsX.extent()*.9;
122 }
123 //TODO: dynamical update of parameter ranges?
124 //if (prop_units.get_value()){
125 // spacing.param_set_range(-.9, NR_HUGE);
126 // }else{
127 // spacing.param_set_range(-pattBndsX.extent()*.9, NR_HUGE);
128 // }
130 y0+=noffset;
132 std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > > paths_in;
133 paths_in = split_at_discontinuities(pwd2_in);
135 for (unsigned idx = 0; idx < paths_in.size(); idx++){
136 Geom::Piecewise<Geom::D2<Geom::SBasis> > path_i = paths_in[idx];
137 Piecewise<SBasis> x = x0;
138 Piecewise<SBasis> y = y0;
139 Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(path_i,2,.1);
140 uskeleton = remove_short_cuts(uskeleton,.01);
141 Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));
142 n = force_continuity(remove_short_cuts(n,.1));
144 int nbCopies = 0;
145 double scaling = 1;
146 switch(type) {
147 case PAPCT_REPEATED:
148 nbCopies = static_cast<int>(floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX.extent()+xspace)));
149 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
150 break;
152 case PAPCT_SINGLE:
153 nbCopies = (toffset + pattBndsX.extent() < uskeleton.domain().extent()) ? 1 : 0;
154 break;
156 case PAPCT_SINGLE_STRETCHED:
157 nbCopies = 1;
158 scaling = (uskeleton.domain().extent() - toffset)/pattBndsX.extent();
159 break;
161 case PAPCT_REPEATED_STRETCHED:
162 // if uskeleton is closed:
163 if(path_i.segs.front().at0() == path_i.segs.back().at1()){
164 nbCopies = static_cast<int>(std::floor((uskeleton.domain().extent() - toffset)/(pattBndsX.extent()+xspace)));
165 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
166 scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX.extent());
167 // if not closed: no space at the end
168 }else{
169 nbCopies = static_cast<int>(std::floor((uskeleton.domain().extent() - toffset + xspace)/(pattBndsX.extent()+xspace)));
170 pattBndsX = Interval(pattBndsX.min(),pattBndsX.max()+xspace);
171 scaling = (uskeleton.domain().extent() - toffset)/(((double)nbCopies)*pattBndsX.extent() - xspace);
172 }
173 break;
175 default:
176 return pwd2_in;
177 };
179 double pattWidth = pattBndsX.extent() * scaling;
181 if (scaling != 1.0) {
182 x*=scaling;
183 }
184 if ( scale_y_rel.get_value() ) {
185 y*=(scaling*prop_scale);
186 } else {
187 if (prop_scale != 1.0) y *= prop_scale;
188 }
189 x += toffset;
191 double offs = 0;
192 for (int i=0; i<nbCopies; i++){
193 output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));
194 offs+=pattWidth;
195 }
196 }
197 return output;
198 }
200 void
201 LPEPatternAlongPath::transform_multiply(Geom::Matrix const& postmul, bool set)
202 {
203 // TODO: implement correct transformation instead of this default behavior
204 Effect::transform_multiply(postmul, set);
205 }
207 LPEFreehandShape::LPEFreehandShape(LivePathEffectObject *lpeobject) : LPEPatternAlongPath(lpeobject)
208 {
209 }
211 } // namespace LivePathEffect
212 } /* namespace Inkscape */
214 /*
215 Local Variables:
216 mode:c++
217 c-file-style:"stroustrup"
218 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
219 indent-tabs-mode:nil
220 fill-column:99
221 End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :