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(_("Nb of iterations"), _("Draw that many approximating strokes sequences."), "nbiter_approxstrokes", &wr, this, 5),
38 strokelength(_("Max stroke length"),
39 _("Maximal length of approximated strokes."), "strokelength", &wr, this, 100.),
40 strokelength_rdm(_("Stroke length variation"),
41 _("Random variation of stroke length (relative to max. length)."), "strokelength_rdm", &wr, this, .3),
42 strokeoverlap(_("Max. overlap"),
43 _("How much successive strokes should overlap (relative to max. length)."), "strokeoverlap", &wr, this, .3),
44 strokeoverlap_rdm(_("Overlap variation"),
45 _("Random variation of overlap (relative to max. overlap)"), "strokeoverlap_rdm", &wr, this, .3),
46 ends_tolerance(_("Max. ends tolerance"),
47 _("Max. distance between original and approximated paths ends (relative to max. length)."), "ends_tolerance", &wr, this, .1),
48 parallel_offset(_("Parallel offset"),
49 _("Average distance to original stroke(try 0.)."), "parallel_offset", &wr, this, 5.),
50 tremble_size(_("Max. tremble"),
51 _("Maximal tremble magnitude."), "tremble_size", &wr, this, 5.),
52 tremble_frequency(_("Tremble frequency"),
53 _("Typical nb of tremble 'period' in a stroke."), "tremble_frequency", &wr, this, 1.),
54 nbtangents(_("Nb of 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*avarage offset) )"), "tgtscale", &wr, this, 10.0),
58 tgtlength(_("Max. length"), _("Max. length of construction lines."), "tgtlength", &wr, this, 100.0),
59 tgtlength_rdm(_("Length variation"), _("Random variation of construction lines length."), "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 *>(¶llel_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.);
100 }
102 LPESketch::~LPESketch()
103 {
105 }
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;
134 }
135 */
137 //TODO: does this already exist in 2Geom? if not, move this there...
138 std::vector<Geom::Piecewise<Geom::D2<Geom::SBasis> > >
139 split_at_discontinuities (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwsbin, double tol = .0001)
140 {
141 using namespace Geom;
142 std::vector<Piecewise<D2<SBasis> > > ret;
143 unsigned piece_start = 0;
144 for (unsigned i=0; i<pwsbin.segs.size(); i++){
145 if (i==(pwsbin.segs.size()-1) || L2(pwsbin.segs[i].at1()- pwsbin.segs[i+1].at0()) > tol){
146 Piecewise<D2<SBasis> > piece;
147 piece.cuts.push_back(pwsbin.cuts[piece_start]);
148 for (unsigned j = piece_start; j<i+1; j++){
149 piece.segs.push_back(pwsbin.segs[j]);
150 piece.cuts.push_back(pwsbin.cuts[j+1]);
151 }
152 ret.push_back(piece);
153 piece_start = i+1;
154 }
155 }
156 return ret;
157 }
159 //This returns a random perturbation. Notice the domain is [s0,s0+first multiple of period>s1]...
160 Geom::Piecewise<Geom::D2<Geom::SBasis> >
161 LPESketch::computePerturbation (double s0, double s1){
162 using namespace Geom;
163 Piecewise<D2<SBasis> >res;
165 //global offset for this stroke.
166 double offsetX = parallel_offset-parallel_offset.get_value();
167 double offsetY = parallel_offset-parallel_offset.get_value();
168 Point A,dA,B,dB,offset = Point(offsetX,offsetY);
169 //start point A
170 for (unsigned dim=0; dim<2; dim++){
171 A[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value();
172 dA[dim] = 2*tremble_size-tremble_size.get_value();
173 }
174 //compute howmany deg 3 sbasis to concat according to frequency.
175 unsigned count = unsigned((s1-s0)/strokelength*tremble_frequency)+1;
176 for (unsigned i=0; i<count; i++){
177 D2<SBasis> perturb = D2<SBasis>();
178 for (unsigned dim=0; dim<2; dim++){
179 B[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value();
180 perturb[dim].push_back(Linear(A[dim],B[dim]));
181 dA[dim] = dA[dim]-B[dim]+A[dim];
182 dB[dim] = 2*tremble_size-tremble_size.get_value();
183 perturb[dim].push_back(Linear(dA[dim],dB[dim]));
184 }
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 return res;
191 }
194 // Main effect body...
195 Geom::Piecewise<Geom::D2<Geom::SBasis> >
196 LPESketch::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
197 {
198 using namespace Geom;
199 //If the input path is empty, do nothing.
200 //Note: this happens when duplicating a 3d box... dunno why.
201 if (pwd2_in.size()==0) return pwd2_in;
203 Piecewise<D2<SBasis> > output;
206 //init random parameters.
207 parallel_offset.resetRandomizer();
208 strokelength_rdm.resetRandomizer();
209 strokeoverlap_rdm.resetRandomizer();
210 tgtlength_rdm.resetRandomizer();
212 // some variables for futur use (for construction lines; compute arclength only once...)
213 // notations will be : t = path time, s = distance from start along the path.
214 Piecewise<SBasis> pathlength;
215 double total_length = 0;
217 //TODO: split Construction Lines/Approximated Strokes into two separate effects?
219 //----- Approximated Strokes.
220 std::vector<Piecewise<D2<SBasis> > > pieces_in = split_at_discontinuities (pwd2_in);
222 //work separately on each component.
223 for (unsigned pieceidx = 0; pieceidx < pieces_in.size(); pieceidx++){
225 Piecewise<D2<SBasis> > piece = pieces_in[pieceidx];
226 Piecewise<SBasis> piecelength = arcLengthSb(piece,.1);
227 double piece_total_length = piecelength.segs.back().at1()-piecelength.segs.front().at0();
228 pathlength.concat(piecelength + total_length);
229 total_length += piece_total_length;
232 //TODO: better check this on the Geom::Path.
233 bool closed = piece.segs.front().at0() == piece.segs.back().at1();
234 if (closed){
235 piece.concat(piece);
236 piecelength.concat(piecelength+piece_total_length);
237 }
239 for (unsigned i = 0; i<nbiter_approxstrokes; i++){
240 //Basic steps:
241 //- Choose a rdm seg [s0,s1], find coresponding [t0,t1],
242 //- Pick a rdm perturbation delta(s), collect 'piece(t)+delta(s(t))' over [t0,t1] into output.
244 // pick a point where to start the stroke (s0 = dist from start).
245 double s1=0.,s0 = ends_tolerance*strokelength+0.0001;//the root finder might miss 0.
246 double t1, t0;
247 double s0_initial = s0;
248 bool done = false;// was the end of the component reached?
250 while (!done){
251 // if the start point is already too far... do nothing. (this should not happen!)
252 if (!closed && s1>piece_total_length - ends_tolerance.get_value()*strokelength) break;
253 if ( closed && s0>piece_total_length + s0_initial) break;
255 std::vector<double> times;
256 times = roots(piecelength-s0);
257 t0 = times.at(0);//there should be one and only one solution!!
259 // pick a new end point (s1 = s0 + strokelength).
260 s1 = s0 + strokelength*(1-strokelength_rdm);
261 // don't let it go beyond the end of the orgiginal path.
262 // TODO/FIXME: this might result in short strokes near the end...
263 if (!closed && s1>piece_total_length-ends_tolerance.get_value()*strokelength){
264 done = true;
265 //!!the root solver might miss s1==piece_total_length...
266 if (s1>piece_total_length){s1 = piece_total_length - ends_tolerance*strokelength-0.0001;}
267 }
268 if (closed && s1>piece_total_length + s0_initial){
269 done = true;
270 if (closed && s1>2*piece_total_length){
271 s1 = 2*piece_total_length - strokeoverlap*(1-strokeoverlap_rdm)*strokelength-0.0001;
272 }
273 }
274 times = roots(piecelength-s1);
275 if (times.size()==0) break;//we should not be there.
276 t1 = times[0];
278 //pick a rdm perturbation, and collect the perturbed piece into output.
279 Piecewise<D2<SBasis> > pwperturb = computePerturbation(s0,s1);
280 pwperturb = compose(pwperturb,portion(piecelength,t0,t1));
281 output.concat(portion(piece,t0,t1)+pwperturb);
283 //step points: s0 = s1 - overlap.
284 //TODO: make sure this has to end?
285 s0 = s1 - strokeoverlap*(1-strokeoverlap_rdm)*(s1-s0);
286 }
287 }
288 }
291 //----- Construction lines.
292 //TODO: choose places according to curvature?.
294 //at this point we should have:
295 //pathlength = arcLengthSb(pwd2_in,.1);
296 //total_length = pathlength.segs.back().at1()-pathlength.segs.front().at0();
297 Piecewise<D2<SBasis> > m = pwd2_in;
298 Piecewise<D2<SBasis> > v = derivative(pwd2_in);
299 Piecewise<D2<SBasis> > a = derivative(v);
300 for (unsigned i=0; i<nbtangents; i++){
301 // pick a point where to draw a tangent (s = dist from start along path).
302 double s = total_length * ( i + tgtlength_rdm ) / (nbtangents+1.);
303 std::vector<double> times;
304 times = roots(pathlength-s);
305 double t = times.at(0);//there should be one and only one solution!
306 Point m_t = m(t), v_t = v(t), a_t = a(t);
307 //Compute tgt length according to curvature (not exceeding tgtlength) so that
308 // dist to origninal curve ~ 4 * (parallel_offset+tremble_size).
309 //TODO: put this 4 as a parameter in the UI...
310 //TODO: what if with v=0?
311 double l = tgtlength*(1-tgtlength_rdm)/v_t.length();
312 double r = pow(v_t.length(),3)/cross(a_t,v_t);
313 r = sqrt((2*fabs(r)-tgtscale)*tgtscale)/v_t.length();
314 l=(r<l)?r:l;
315 //collect the tgt segment into output.
316 D2<SBasis> tgt = D2<SBasis>();
317 for (unsigned dim=0; dim<2; dim++){
318 tgt[dim] = SBasis(Linear(m_t[dim]-v_t[dim]*l, m_t[dim]+v_t[dim]*l));
319 }
320 output.concat(Piecewise<D2<SBasis> >(tgt));
321 }
322 return output;
323 }
325 /* ######################## */
327 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
328 } /* namespace Inkscape */
330 /*
331 Local Variables:
332 mode:c++
333 c-file-style:"stroustrup"
334 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
335 indent-tabs-mode:nil
336 fill-column:99
337 End:
338 */
339 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :