1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3  * @file
4  * LPE sketch effect implementation.
5  */
6 /* Authors:
7  *   Jean-Francois Barraud <jf.barraud@gmail.com>
8  *   Johan Engelen <j.b.c.engelen@utwente.nl>
9  *
10  * Copyright (C) 2007 Authors
11  *
12  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13  */
14 
15 #include "live_effects/lpe-sketch.h"
16 
17 // You might need to include other 2geom files. You can add them here:
18 #include <2geom/sbasis-math.h>
19 #include <2geom/bezier-to-sbasis.h>
20 #include <2geom/path-intersection.h>
21 
22 // TODO due to internal breakage in glibmm headers, this must be last:
23 #include <glibmm/i18n.h>
24 
25 namespace Inkscape {
26 namespace LivePathEffect {
27 
LPESketch(LivePathEffectObject * lpeobject)28 LPESketch::LPESketch(LivePathEffectObject *lpeobject) :
29     Effect(lpeobject),
30     // initialise your parameters here:
31     //testpointA(_("Test Point A"), _("Test A"), "ptA", &wr, this, Geom::Point(100,100)),
32     nbiter_approxstrokes(_("Strokes:"), _("Draw that many approximating strokes"), "nbiter_approxstrokes", &wr, this, 5),
33     strokelength(_("Max stroke length:"),
34                  _("Maximum length of approximating strokes"), "strokelength", &wr, this, 100.),
35     strokelength_rdm(_("Stroke length variation:"),
36                      _("Random variation of stroke length (relative to maximum length)"), "strokelength_rdm", &wr, this, .3),
37     strokeoverlap(_("Max. overlap:"),
38                   _("How much successive strokes should overlap (relative to maximum length)"), "strokeoverlap", &wr, this, .3),
39     strokeoverlap_rdm(_("Overlap variation:"),
40                       _("Random variation of overlap (relative to maximum overlap)"), "strokeoverlap_rdm", &wr, this, .3),
41     ends_tolerance(_("Max. end tolerance:"),
42                    _("Maximum distance between ends of original and approximating paths (relative to maximum length)"), "ends_tolerance", &wr, this, .1),
43     parallel_offset(_("Average offset:"),
44                     _("Average distance each stroke is away from the original path"), "parallel_offset", &wr, this, 5.),
45     tremble_size(_("Max. tremble:"),
46                  _("Maximum tremble magnitude"), "tremble_size", &wr, this, 5.),
47     tremble_frequency(_("Tremble frequency:"),
48                       _("Average number of tremble periods in a stroke"), "tremble_frequency", &wr, this, 1.)
49 #ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES
50     ,nbtangents(_("Construction lines:"),
51                _("How many construction lines (tangents) to draw"), "nbtangents", &wr, this, 5),
52     tgtscale(_("Scale:"),
53              _("Scale factor relating curvature and length of construction lines (try 5*offset)"), "tgtscale", &wr, this, 10.0),
54     tgtlength(_("Max. length:"), _("Maximum length of construction lines"), "tgtlength", &wr, this, 100.0),
55     tgtlength_rdm(_("Length variation:"), _("Random variation of the length of construction lines"), "tgtlength_rdm", &wr, this, .3),
56     tgt_places_rdmness(_("Placement randomness:"), _("0: evenly distributed construction lines, 1: purely random placement"), "tgt_places_rdmness", &wr, this, 1.)
57 #ifdef LPE_SKETCH_USE_CURVATURE
58     ,min_curvature(_("k_min:"), _("min curvature"), "k_min", &wr, this, 4.0)
59     ,max_curvature(_("k_max:"), _("max curvature"), "k_max", &wr, this, 1000.0)
60 #endif
61 #endif
62 {
63     // register all your parameters here, so Inkscape knows which parameters this effect has:
64     //Add some comment in the UI:  *warning* the precise output of this effect might change in future releases!
65     //convert to path if you want to keep exact output unchanged in future releases...
66     //registerParameter(&testpointA) );
67     registerParameter(&nbiter_approxstrokes);
68     registerParameter(&strokelength);
69     registerParameter(&strokelength_rdm);
70     registerParameter(&strokeoverlap);
71     registerParameter(&strokeoverlap_rdm);
72     registerParameter(&ends_tolerance);
73     registerParameter(&parallel_offset);
74     registerParameter(&tremble_size);
75     registerParameter(&tremble_frequency);
76 #ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES
77     registerParameter(&nbtangents);
78     registerParameter(&tgt_places_rdmness);
79     registerParameter(&tgtscale);
80     registerParameter(&tgtlength);
81     registerParameter(&tgtlength_rdm);
82 #ifdef LPE_SKETCH_USE_CURVATURE
83     registerParameter(&min_curvature);
84     registerParameter(&max_curvature);
85 #endif
86 #endif
87 
88     nbiter_approxstrokes.param_make_integer();
89     nbiter_approxstrokes.param_set_range(0, std::numeric_limits<gint>::max());
90     strokelength.param_set_range(1, std::numeric_limits<double>::max());
91     strokelength.param_set_increments(1., 5.);
92     strokelength_rdm.param_set_range(0, 1.);
93     strokeoverlap.param_set_range(0, 1.);
94     strokeoverlap.param_set_increments(0.1, 0.30);
95     ends_tolerance.param_set_range(0., 1.);
96     parallel_offset.param_set_range(0, std::numeric_limits<double>::max());
97     tremble_frequency.param_set_range(0.01, 100.);
98     tremble_frequency.param_set_increments(.5, 1.5);
99     strokeoverlap_rdm.param_set_range(0, 1.);
100 
101 #ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES
102     nbtangents.param_make_integer();
103     nbtangents.param_set_range(0, std::numeric_limits<gint>::max());
104     tgtscale.param_set_range(0, std::numeric_limits<double>::max());
105     tgtscale.param_set_increments(.1, .5);
106     tgtlength.param_set_range(0, std::numeric_limits<double>::max());
107     tgtlength.param_set_increments(1., 5.);
108     tgtlength_rdm.param_set_range(0, 1.);
109     tgt_places_rdmness.param_set_range(0, 1.);
110     //this is not very smart, but required to avoid having lot of tangents stacked on short components.
111     //Note: we could specify a density instead of an absolute number, but this would be scale dependent.
112     concatenate_before_pwd2 = true;
113 #endif
114 }
115 
116 LPESketch::~LPESketch()
117 = default;
118 
119 /*
120 Geom::Piecewise<Geom::D2<Geom::SBasis> >
121 addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
122     using namespace Geom;
123     Piecewise<D2<SBasis> > output;
124     Piecewise<D2<SBasis> > start;
125     Piecewise<D2<SBasis> > end;
126     double x,y,vx,vy;
127 
128     x  = m.segs.front()[0].at0();
129     y  = m.segs.front()[1].at0();
130     vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
131     vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
132     start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
133     start.offsetDomain(m.cuts.front()-1.);
134 
135     x  = m.segs.back()[0].at1();
136     y  = m.segs.back()[1].at1();
137     vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
138     vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
139     end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
140     //end.offsetDomain(m.cuts.back());
141 
142     output = start;
143     output.concat(m);
144     output.concat(end);
145     return output;
146 }
147 */
148 
149 
150 
151 //This returns a random perturbation. Notice the domain is [s0,s0+first multiple of period>s1]...
152 Geom::Piecewise<Geom::D2<Geom::SBasis> >
computePerturbation(double s0,double s1)153 LPESketch::computePerturbation (double s0, double s1){
154     using namespace Geom;
155     Piecewise<D2<SBasis> >res;
156 
157     //global offset for this stroke.
158     double offsetX = 2*parallel_offset-parallel_offset.get_value();
159     double offsetY = 2*parallel_offset-parallel_offset.get_value();
160     Point A,dA,B,dB,offset = Point(offsetX,offsetY);
161     //start point A
162     for (unsigned dim=0; dim<2; dim++){
163         A[dim]  = offset[dim] + 2*tremble_size-tremble_size.get_value();
164         dA[dim] = 2*tremble_size-tremble_size.get_value();
165     }
166     //compute howmany deg 3 sbasis to concat according to frequency.
167 
168     unsigned count = unsigned((s1-s0)/strokelength*tremble_frequency)+1;
169     //unsigned count = unsigned((s1-s0)/tremble_frequency)+1;
170 
171     for (unsigned i=0; i<count; i++){
172         D2<SBasis> perturb = D2<SBasis>(SBasis(2, Linear()), SBasis(2, Linear()));
173         for (unsigned dim=0; dim<2; dim++){
174             B[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value();
175             perturb[dim][0] = Linear(A[dim],B[dim]);
176             dA[dim] = dA[dim]-B[dim]+A[dim];
177             //avoid dividing by 0. Very short strokes will have ends parallel to the curve...
178             if ( s1-s0 > 1e-2)
179                 dB[dim] = -(2*tremble_size-tremble_size.get_value())/(s0-s1)-B[dim]+A[dim];
180             else
181                 dB[dim] = -(2*tremble_size-tremble_size.get_value())-B[dim]+A[dim];
182             perturb[dim][1] = Linear(dA[dim],dB[dim]);
183         }
184         dA = B-A-dB;
185         A = B;
186         //dA = B-A-dB;
187         res.concat(Piecewise<D2<SBasis> >(perturb));
188     }
189     res.setDomain(Interval(s0,s0+count*strokelength/tremble_frequency));
190     //res.setDomain(Interval(s0,s0+count*tremble_frequency));
191     return res;
192 }
193 
194 
195 // Main effect body...
196 Geom::Piecewise<Geom::D2<Geom::SBasis> >
doEffect_pwd2(Geom::Piecewise<Geom::D2<Geom::SBasis>> const & pwd2_in)197 LPESketch::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
198 {
199     using namespace Geom;
200     //If the input path is empty, do nothing.
201     //Note: this happens when duplicating a 3d box... dunno why.
202     if (pwd2_in.size()==0) return pwd2_in;
203 
204     Piecewise<D2<SBasis> > output;
205 
206     // some variables for futur use (for construction lines; compute arclength only once...)
207     // notations will be : t = path time, s = distance from start along the path.
208     Piecewise<SBasis> pathlength;
209     double total_length = 0;
210 
211     //TODO: split Construction Lines/Approximated Strokes into two separate effects?
212 
213     //----- Approximated Strokes.
214     std::vector<Piecewise<D2<SBasis> > > pieces_in = split_at_discontinuities (pwd2_in);
215 
216     //work separately on each component.
217     for (auto piece : pieces_in){
218 
219         Piecewise<SBasis> piecelength = arcLengthSb(piece,.1);
220         double piece_total_length = piecelength.segs.back().at1()-piecelength.segs.front().at0();
221         pathlength.concat(piecelength + total_length);
222         total_length += piece_total_length;
223 
224 
225         //TODO: better check this on the Geom::Path.
226         bool closed = piece.segs.front().at0() == piece.segs.back().at1();
227         if (closed){
228             piece.concat(piece);
229             piecelength.concat(piecelength+piece_total_length);
230         }
231 
232         for (unsigned i = 0; i<nbiter_approxstrokes; i++){
233             //Basic steps:
234             //- Choose a rdm seg [s0,s1], find corresponding [t0,t1],
235             //- Pick a rdm perturbation delta(s), collect 'piece(t)+delta(s(t))' over [t0,t1] into output.
236 
237             // pick a point where to start the stroke (s0 = dist from start).
238             double s1=0.,s0 = ends_tolerance*strokelength+0.0001;//the root finder might miss 0.
239             double t1, t0;
240             double s0_initial = s0;
241             bool done = false;// was the end of the component reached?
242 
243             while (!done){
244                 // if the start point is already too far... do nothing. (this should not happen!)
245                 if (!closed && s1>piece_total_length - ends_tolerance.get_value()*strokelength) break;
246                 if ( closed && s0>piece_total_length + s0_initial) break;
247 
248                 std::vector<double> times;
249                 times = roots(piecelength-s0);
250                 t0 = times.at(0);//there should be one and only one solution!!
251 
252                 // pick a new end point (s1 = s0 + strokelength).
253                 s1 = s0 + strokelength*(1-strokelength_rdm);
254                 // don't let it go beyond the end of the original path.
255                 // TODO/FIXME: this might result in short strokes near the end...
256                 if (!closed && s1>piece_total_length-ends_tolerance.get_value()*strokelength){
257                     done = true;
258                     //!!the root solver might miss s1==piece_total_length...
259                     if (s1>piece_total_length){s1 = piece_total_length - ends_tolerance*strokelength-0.0001;}
260                 }
261                 if (closed && s1>piece_total_length + s0_initial){
262                     done = true;
263                     if (closed && s1>2*piece_total_length){
264                         s1 = 2*piece_total_length - strokeoverlap*(1-strokeoverlap_rdm)*strokelength-0.0001;
265                     }
266                 }
267                 times = roots(piecelength-s1);
268                 if (times.empty()) break;//we should not be there.
269                 t1 = times[0];
270 
271                 //pick a rdm perturbation, and collect the perturbed piece into output.
272                 Piecewise<D2<SBasis> > pwperturb = computePerturbation(s0-0.01,s1+0.01);
273                 pwperturb = compose(pwperturb,portion(piecelength,t0,t1));
274 
275                 output.concat(portion(piece,t0,t1)+pwperturb);
276 
277                 //step points: s0 = s1 - overlap.
278                 //TODO: make sure this has to end?
279                 s0 = s1 - strokeoverlap*(1-strokeoverlap_rdm)*(s1-s0);
280             }
281         }
282     }
283 
284 #ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES
285 
286     //----- Construction lines.
287     //TODO: choose places according to curvature?.
288 
289     //at this point we should have:
290     //pathlength = arcLengthSb(pwd2_in,.1);
291     //total_length = pathlength.segs.back().at1()-pathlength.segs.front().at0();
292     Piecewise<D2<SBasis> > m = pwd2_in;
293     Piecewise<D2<SBasis> > v = derivative(pwd2_in);
294     Piecewise<D2<SBasis> > a = derivative(v);
295 
296 #ifdef LPE_SKETCH_USE_CURVATURE
297     //---- curvature experiment...(enable
298     Piecewise<SBasis> k = curvature(pwd2_in);
299     OptInterval k_bnds = bounds_exact(abs(k));
300     double k_min = k_bnds->min() + k_bnds->extent() * min_curvature;
301     double k_max = k_bnds->min() + k_bnds->extent() * max_curvature;
302 
303     Piecewise<SBasis> bump;
304     //SBasis bump_seg = SBasis( 2, Linear(0) );
305     //bump_seg[1] = Linear( 4. );
306     SBasis bump_seg = SBasis( 1, Linear(1) );
307     bump.push_cut( k_bnds->min() - 1 );
308     bump.push( Linear(0), k_min );
309     bump.push(bump_seg,k_max);
310     bump.push( Linear(0), k_bnds->max()+1 );
311 
312     Piecewise<SBasis> repartition = compose( bump, k );
313     repartition = integral(repartition);
314     //-------------------------------
315 #endif
316 
317     for (unsigned i=0; i<nbtangents; i++){
318 
319         // pick a point where to draw a tangent (s = dist from start along path).
320 #ifdef LPE_SKETCH_USE_CURVATURE
321         double proba = repartition.firstValue()+ (rand()%100)/100.*(repartition.lastValue()-repartition.firstValue());
322         std::vector<double> times;
323         times = roots(repartition - proba);
324         double t = times.at(0);//there should be one and only one solution!
325 #else
326         //double s = total_length * ( i + tgtlength_rdm ) / (nbtangents+1.);
327         double reg_place = total_length * ( i + .5) / ( nbtangents );
328         double rdm_place = total_length * tgt_places_rdmness;
329         double s = ( 1.- tgt_places_rdmness.get_value() ) * reg_place  +  rdm_place ;
330         std::vector<double> times;
331         times = roots(pathlength-s);
332         double t = times.at(0);//there should be one and only one solution!
333 #endif
334         Point m_t = m(t), v_t = v(t), a_t = a(t);
335         //Compute tgt length according to curvature (not exceeding tgtlength) so that
336         //  dist to original curve ~ 4 * (parallel_offset+tremble_size).
337         //TODO: put this 4 as a parameter in the UI...
338         //TODO: what if with v=0?
339         double l = tgtlength*(1-tgtlength_rdm)/v_t.length();
340         double r = std::pow(v_t.length(), 3) / cross(v_t, a_t);
341         r = sqrt((2*fabs(r)-tgtscale)*tgtscale)/v_t.length();
342         l=(r<l)?r:l;
343         //collect the tgt segment into output.
344         D2<SBasis> tgt = D2<SBasis>();
345         for (unsigned dim=0; dim<2; dim++){
346             tgt[dim] = SBasis(Linear(m_t[dim]-v_t[dim]*l, m_t[dim]+v_t[dim]*l));
347         }
348         output.concat(Piecewise<D2<SBasis> >(tgt));
349     }
350 #endif
351 
352     return output;
353 }
354 
355 void
doBeforeEffect(SPLPEItem const *)356 LPESketch::doBeforeEffect (SPLPEItem const*/*lpeitem*/)
357 {
358     //init random parameters.
359     parallel_offset.resetRandomizer();
360     strokelength_rdm.resetRandomizer();
361     strokeoverlap_rdm.resetRandomizer();
362     ends_tolerance.resetRandomizer();
363     tremble_size.resetRandomizer();
364 #ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES
365     tgtlength_rdm.resetRandomizer();
366     tgt_places_rdmness.resetRandomizer();
367 #endif
368 }
369 
370 /* ######################## */
371 
372 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
373 } /* namespace Inkscape */
374 
375 /*
376   Local Variables:
377   mode:c++
378   c-file-style:"stroustrup"
379   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
380   indent-tabs-mode:nil
381   fill-column:99
382   End:
383 */
384 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
385