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