Code

copyediting
[inkscape.git] / src / live_effects / lpe-sketch.cpp
1 #define INKSCAPE_LPE_SKETCH_CPP
2 /** \file
3  * LPE <sketch> implementation
4  */
5 /*
6  * Authors:
7  *   Johan Engelen
8 *
9 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "live_effects/lpe-sketch.h"
15 #include "display/curve.h"
16 #include <libnr/n-art-bpath.h>
18 // You might need to include other 2geom files. You can add them here:
19 #include <2geom/path.h>
20 #include <2geom/sbasis.h>
21 #include <2geom/sbasis-geometric.h>
22 #include <2geom/bezier-to-sbasis.h>
23 #include <2geom/sbasis-to-bezier.h>
24 #include <2geom/d2.h>
25 #include <2geom/sbasis-math.h>
26 #include <2geom/piecewise.h>
27 #include <2geom/crossing.h>
28 #include <2geom/path-intersection.h>
30 namespace Inkscape {
31 namespace LivePathEffect {
33 LPESketch::LPESketch(LivePathEffectObject *lpeobject) :
34     Effect(lpeobject),
35     // initialise your parameters here:
36     //testpointA(_("Test Point A"), _("Test A"), "ptA", &wr, this, Geom::Point(100,100)),
37     nbiter_approxstrokes(_("Strokes"), _("Draw that many approximating strokes"), "nbiter_approxstrokes", &wr, this, 5),
38     strokelength(_("Max stroke length"), 
39                  _("Maximum length of approximating strokes"), "strokelength", &wr, this, 100.),
40     strokelength_rdm(_("Stroke length variation"), 
41                      _("Random variation of stroke length (relative to maximum length)"), "strokelength_rdm", &wr, this, .3),
42     strokeoverlap(_("Max. overlap"), 
43                   _("How much successive strokes should overlap (relative to maximum length)."), "strokeoverlap", &wr, this, .3),
44     strokeoverlap_rdm(_("Overlap variation"), 
45                       _("Random variation of overlap (relative to maximum overlap)"), "strokeoverlap_rdm", &wr, this, .3),
46     ends_tolerance(_("Max. end tolerance"), 
47                    _("Maximum distance between ends of original and approximating paths (relative to maximum length)"), "ends_tolerance", &wr, this, .1),
48     parallel_offset(_("Parallel offset"), 
49                     _("Average distance from approximating path to original path"), "parallel_offset", &wr, this, 5.),
50     tremble_size(_("Max. tremble"), 
51                  _("Maximum tremble magnitude"), "tremble_size", &wr, this, 5.),
52     tremble_frequency(_("Tremble frequency"), 
53                       _("Avreage number of tremble periods in an approximating stroke"), "tremble_frequency", &wr, this, 1.),
54     nbtangents(_("Construction lines"), 
55                _("How many construction lines (tangents) to draw"), "nbtangents", &wr, this, 5),
56     tgtscale(_("Scale"), 
57              _("Scale factor relating curvature and length of construction lines (try 5*offset)"), "tgtscale", &wr, this, 10.0),
58     tgtlength(_("Max. length"), _("Maximum length of construction lines"), "tgtlength", &wr, this, 100.0),
59     tgtlength_rdm(_("Length variation"), _("Random variation of the length of construction lines"), "tgtlength_rdm", &wr, this, .3)
60 {
61     // register all your parameters here, so Inkscape knows which parameters this effect has:
62     //Add some comment in the UI:  *warning* the precise output of this effect might change in future releases!
63     //convert to path if you want to keep exact output unchanged in future releases...
64     //registerParameter( dynamic_cast<Parameter *>(&testpointA) );
65     registerParameter( dynamic_cast<Parameter *>(&nbiter_approxstrokes) );
66     registerParameter( dynamic_cast<Parameter *>(&strokelength) );
67     registerParameter( dynamic_cast<Parameter *>(&strokelength_rdm) );
68     registerParameter( dynamic_cast<Parameter *>(&strokeoverlap) );
69     registerParameter( dynamic_cast<Parameter *>(&strokeoverlap_rdm) );
70     registerParameter( dynamic_cast<Parameter *>(&ends_tolerance) );
71     registerParameter( dynamic_cast<Parameter *>(&parallel_offset) );
72     registerParameter( dynamic_cast<Parameter *>(&tremble_size) );
73     registerParameter( dynamic_cast<Parameter *>(&tremble_frequency) );
74     registerParameter( dynamic_cast<Parameter *>(&nbtangents) );
75     registerParameter( dynamic_cast<Parameter *>(&tgtscale) );
76     registerParameter( dynamic_cast<Parameter *>(&tgtlength) );
77     registerParameter( dynamic_cast<Parameter *>(&tgtlength_rdm) );
80     nbiter_approxstrokes.param_make_integer();
81     nbiter_approxstrokes.param_set_range(0, NR_HUGE);
82     strokelength.param_set_range(1, NR_HUGE);
83     strokelength.param_set_increments(1., 5.);
84     strokelength_rdm.param_set_range(0, 1.);
85     strokeoverlap.param_set_range(0, 1.);
86     strokeoverlap.param_set_increments(0.1, 0.30);
87     ends_tolerance.param_set_range(0., 1.);
88     parallel_offset.param_set_range(0, NR_HUGE);
89     tremble_frequency.param_set_range(0.01, 100.);
90     tremble_frequency.param_set_increments(.5, 1.5);
91     strokeoverlap_rdm.param_set_range(0, 1.);
93     nbtangents.param_make_integer();
94     nbtangents.param_set_range(0, NR_HUGE);
95     tgtscale.param_set_range(0, NR_HUGE);
96     tgtscale.param_set_increments(.1, .5);
97     tgtlength.param_set_range(0, NR_HUGE);
98     tgtlength.param_set_increments(1., 5.);
99     tgtlength_rdm.param_set_range(0, 1.);
102 LPESketch::~LPESketch()
107 /*
108 Geom::Piecewise<Geom::D2<Geom::SBasis> >
109 addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
110     using namespace Geom;
111     Piecewise<D2<SBasis> > output;
112     Piecewise<D2<SBasis> > start;
113     Piecewise<D2<SBasis> > end;
114     double x,y,vx,vy;
116     x  = m.segs.front()[0].at0();
117     y  = m.segs.front()[1].at0();
118     vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
119     vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
120     start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
121     start.offsetDomain(m.cuts.front()-1.);
123     x  = m.segs.back()[0].at1();
124     y  = m.segs.back()[1].at1();
125     vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
126     vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
127     end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
128     //end.offsetDomain(m.cuts.back());
130     output = start;
131     output.concat(m);
132     output.concat(end);
133     return output;
135 */
139 //This returns a random perturbation. Notice the domain is [s0,s0+first multiple of period>s1]...
140 Geom::Piecewise<Geom::D2<Geom::SBasis> > 
141 LPESketch::computePerturbation (double s0, double s1){
142     using namespace Geom;
143     Piecewise<D2<SBasis> >res;
144     
145     //global offset for this stroke.
146     double offsetX = parallel_offset-parallel_offset.get_value();
147     double offsetY = parallel_offset-parallel_offset.get_value();
148     Point A,dA,B,dB,offset = Point(offsetX,offsetY);
149     //start point A
150     for (unsigned dim=0; dim<2; dim++){
151         A[dim]  = offset[dim] + 2*tremble_size-tremble_size.get_value();
152         dA[dim] = 2*tremble_size-tremble_size.get_value();
153     }
154     //compute howmany deg 3 sbasis to concat according to frequency.
155     unsigned count = unsigned((s1-s0)/strokelength*tremble_frequency)+1; 
156     for (unsigned i=0; i<count; i++){
157         D2<SBasis> perturb = D2<SBasis>();
158         for (unsigned dim=0; dim<2; dim++){
159             B[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value();
160             perturb[dim].push_back(Linear(A[dim],B[dim]));
161             dA[dim] = dA[dim]-B[dim]+A[dim];
162             dB[dim] = 2*tremble_size-tremble_size.get_value();
163             perturb[dim].push_back(Linear(dA[dim],dB[dim]));
164         }
165         A = B;
166         dA = B-A-dB;
167         res.concat(Piecewise<D2<SBasis> >(perturb));
168     }
169     res.setDomain(Interval(s0,s0+count*strokelength/tremble_frequency));
170     return res;
174 // Main effect body...
175 Geom::Piecewise<Geom::D2<Geom::SBasis> >
176 LPESketch::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
178     using namespace Geom;
179     //If the input path is empty, do nothing.
180     //Note: this happens when duplicating a 3d box... dunno why.
181     if (pwd2_in.size()==0) return pwd2_in;
183     Piecewise<D2<SBasis> > output;
186     //init random parameters.
187     parallel_offset.resetRandomizer();
188     strokelength_rdm.resetRandomizer();
189     strokeoverlap_rdm.resetRandomizer();
190     tgtlength_rdm.resetRandomizer();
192     // some variables for futur use (for construction lines; compute arclength only once...)
193     // notations will be : t = path time, s = distance from start along the path.
194     Piecewise<SBasis> pathlength;
195     double total_length = 0;
197     //TODO: split Construction Lines/Approximated Strokes into two separate effects?
199     //----- Approximated Strokes.
200     std::vector<Piecewise<D2<SBasis> > > pieces_in = split_at_discontinuities (pwd2_in);
202     //work separately on each component.
203     for (unsigned pieceidx = 0; pieceidx < pieces_in.size(); pieceidx++){
205         Piecewise<D2<SBasis> > piece = pieces_in[pieceidx];
206         Piecewise<SBasis> piecelength = arcLengthSb(piece,.1);
207         double piece_total_length = piecelength.segs.back().at1()-piecelength.segs.front().at0();
208         pathlength.concat(piecelength + total_length);
209         total_length += piece_total_length;
210         
212         //TODO: better check this on the Geom::Path.
213         bool closed = piece.segs.front().at0() == piece.segs.back().at1(); 
214         if (closed){ 
215             piece.concat(piece);
216             piecelength.concat(piecelength+piece_total_length);
217         }
219         for (unsigned i = 0; i<nbiter_approxstrokes; i++){
220             //Basic steps: 
221             //- Choose a rdm seg [s0,s1], find coresponding [t0,t1], 
222             //- Pick a rdm perturbation delta(s), collect 'piece(t)+delta(s(t))' over [t0,t1] into output.
224             // pick a point where to start the stroke (s0 = dist from start).
225             double s1=0.,s0 = ends_tolerance*strokelength+0.0001;//the root finder might miss 0.  
226             double t1, t0;
227             double s0_initial = s0;
228             bool done = false;// was the end of the component reached?
230             while (!done){
231                 // if the start point is already too far... do nothing. (this should not happen!)
232                 if (!closed && s1>piece_total_length - ends_tolerance.get_value()*strokelength) break;
233                 if ( closed && s0>piece_total_length + s0_initial) break;
235                 std::vector<double> times;  
236                 times = roots(piecelength-s0);  
237                 t0 = times.at(0);//there should be one and only one solution!!
238                 
239                 // pick a new end point (s1 = s0 + strokelength).
240                 s1 = s0 + strokelength*(1-strokelength_rdm);
241                 // don't let it go beyond the end of the orgiginal path.
242                 // TODO/FIXME: this might result in short strokes near the end...
243                 if (!closed && s1>piece_total_length-ends_tolerance.get_value()*strokelength){
244                     done = true;
245                     //!!the root solver might miss s1==piece_total_length...
246                     if (s1>piece_total_length){s1 = piece_total_length - ends_tolerance*strokelength-0.0001;}
247                 }
248                 if (closed && s1>piece_total_length + s0_initial){
249                     done = true;
250                     if (closed && s1>2*piece_total_length){
251                         s1 = 2*piece_total_length - strokeoverlap*(1-strokeoverlap_rdm)*strokelength-0.0001;
252                     }
253                 }
254                 times = roots(piecelength-s1);  
255                 if (times.size()==0) break;//we should not be there.
256                 t1 = times[0];
257                 
258                 //pick a rdm perturbation, and collect the perturbed piece into output.
259                 Piecewise<D2<SBasis> > pwperturb = computePerturbation(s0,s1);
260                 pwperturb = compose(pwperturb,portion(piecelength,t0,t1));
261                 output.concat(portion(piece,t0,t1)+pwperturb);
262                 
263                 //step points: s0 = s1 - overlap.
264                 //TODO: make sure this has to end?
265                 s0 = s1 - strokeoverlap*(1-strokeoverlap_rdm)*(s1-s0);
266             }
267         }
268     }
271     //----- Construction lines.
272     //TODO: choose places according to curvature?.
274     //at this point we should have:
275     //pathlength = arcLengthSb(pwd2_in,.1);
276     //total_length = pathlength.segs.back().at1()-pathlength.segs.front().at0();
277     Piecewise<D2<SBasis> > m = pwd2_in;
278     Piecewise<D2<SBasis> > v = derivative(pwd2_in);
279     Piecewise<D2<SBasis> > a = derivative(v);
280     for (unsigned i=0; i<nbtangents; i++){
281         // pick a point where to draw a tangent (s = dist from start along path).
282         double s = total_length * ( i + tgtlength_rdm ) / (nbtangents+1.);
283         std::vector<double> times;  
284         times = roots(pathlength-s);
285         double t = times.at(0);//there should be one and only one solution!
286         Point m_t = m(t), v_t = v(t), a_t = a(t);
287         //Compute tgt length according to curvature (not exceeding tgtlength) so that  
288         //  dist to origninal curve ~ 4 * (parallel_offset+tremble_size).
289         //TODO: put this 4 as a parameter in the UI...
290         //TODO: what if with v=0?
291         double l = tgtlength*(1-tgtlength_rdm)/v_t.length();
292         double r = pow(v_t.length(),3)/cross(a_t,v_t);
293         r = sqrt((2*fabs(r)-tgtscale)*tgtscale)/v_t.length();
294         l=(r<l)?r:l;
295         //collect the tgt segment into output.
296         D2<SBasis> tgt = D2<SBasis>();
297         for (unsigned dim=0; dim<2; dim++){
298             tgt[dim] = SBasis(Linear(m_t[dim]-v_t[dim]*l, m_t[dim]+v_t[dim]*l));
299         }
300         output.concat(Piecewise<D2<SBasis> >(tgt));
301     }
302     return output;
305 /* ######################## */
307 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
308 } /* namespace Inkscape */
310 /*
311   Local Variables:
312   mode:c++
313   c-file-style:"stroustrup"
314   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
315   indent-tabs-mode:nil
316   fill-column:99
317   End:
318 */
319 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :