Code

fix pasting style after copying a text span
[inkscape.git] / src / live_effects / lpe-rough-hatches.cpp
1 #define INKSCAPE_LPE_ROUGH_HATCHES_CPP\r
2 /** \file\r
3  * LPE Curve Stitching implementation, used as an example for a base starting class\r
4  * when implementing new LivePathEffects.\r
5  *\r
6  */\r
7 /*\r
8  * Authors:\r
9  *   JF Barraud.\r
10 *\r
11 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
12  *\r
13  * Released under GNU GPL, read the file 'COPYING' for more information\r
14  */\r
15 \r
16 #include "live_effects/lpe-rough-hatches.h"\r
17 \r
18 #include "sp-item.h"\r
19 #include "sp-path.h"\r
20 #include "svg/svg.h"\r
21 #include "xml/repr.h"\r
22 \r
23 #include <2geom/path.h>\r
24 #include <2geom/piecewise.h>\r
25 #include <2geom/sbasis.h>\r
26 #include <2geom/sbasis-math.h>\r
27 #include <2geom/sbasis-geometric.h>\r
28 #include <2geom/bezier-to-sbasis.h>\r
29 #include <2geom/sbasis-to-bezier.h>\r
30 #include <2geom/d2.h>\r
31 #include <2geom/matrix.h>\r
32 \r
33 #include "ui/widget/scalar.h"\r
34 #include "libnr/nr-values.h"\r
35 \r
36 namespace Inkscape {\r
37 namespace LivePathEffect {\r
38 \r
39 using namespace Geom;\r
40 \r
41 //------------------------------------------------\r
42 // Some goodies to navigate through curve's levels.\r
43 //------------------------------------------------\r
44 struct LevelCrossing{\r
45     Point pt;\r
46     double t;\r
47     bool sign;\r
48     bool used;\r
49     std::pair<unsigned,unsigned> next_on_curve;\r
50     std::pair<unsigned,unsigned> prev_on_curve;\r
51 };\r
52 struct LevelCrossingOrder {\r
53     bool operator()(LevelCrossing a, LevelCrossing b) {\r
54         return a.pt[Y] < b.pt[Y];\r
55     }\r
56 };\r
57 struct LevelCrossingInfo{\r
58     double t;\r
59     unsigned level;\r
60     unsigned idx;\r
61 };\r
62 struct LevelCrossingInfoOrder {\r
63     bool operator()(LevelCrossingInfo a, LevelCrossingInfo b) {\r
64         return a.t < b.t;\r
65     }\r
66 };\r
67 \r
68 typedef std::vector<LevelCrossing> LevelCrossings;\r
69 \r
70 std::vector<double>\r
71 discontinuities(Piecewise<D2<SBasis> > const &f){\r
72     std::vector<double> result;\r
73     if (f.size()==0) return result;\r
74     result.push_back(f.cuts[0]);\r
75     Point prev_pt = f.segs[0].at1();\r
76     //double old_t  = f.cuts[0];\r
77     for(unsigned i=1; i<f.size(); i++){\r
78         if ( f.segs[i].at0()!=prev_pt){\r
79             result.push_back(f.cuts[i]);\r
80             //old_t = f.cuts[i];\r
81             //assert(f.segs[i-1].at1()==f.valueAt(old_t));\r
82         }\r
83         prev_pt = f.segs[i].at1();\r
84     }\r
85     result.push_back(f.cuts.back());\r
86     //assert(f.segs.back().at1()==f.valueAt(old_t));\r
87     return result;\r
88 }\r
89 \r
90 class LevelsCrossings: public std::vector<LevelCrossings>{\r
91 public:\r
92     LevelsCrossings():std::vector<LevelCrossings>(){};\r
93     LevelsCrossings(std::vector<std::vector<double> > const &times,\r
94                     Piecewise<D2<SBasis> > const &f,\r
95                     Piecewise<SBasis> const &dx){\r
96         \r
97         for (unsigned i=0; i<times.size(); i++){\r
98             LevelCrossings lcs;\r
99             for (unsigned j=0; j<times[i].size(); j++){\r
100                 LevelCrossing lc;\r
101                 lc.pt = f.valueAt(times[i][j]);\r
102                 lc.t = times[i][j];\r
103                 lc.sign = ( dx.valueAt(times[i][j])>0 );\r
104                 lc.used = false;\r
105                 lcs.push_back(lc);\r
106             }\r
107             std::sort(lcs.begin(), lcs.end(), LevelCrossingOrder());\r
108             push_back(lcs);\r
109         }\r
110         //Now create time ordering.\r
111         std::vector<LevelCrossingInfo>temp;\r
112         for (unsigned i=0; i<size(); i++){\r
113             for (unsigned j=0; j<(*this)[i].size(); j++){\r
114                 LevelCrossingInfo elem;\r
115                 elem.t = (*this)[i][j].t;\r
116                 elem.level = i;\r
117                 elem.idx = j;\r
118                 temp.push_back(elem);\r
119             }\r
120         }\r
121         std::sort(temp.begin(),temp.end(),LevelCrossingInfoOrder());\r
122         std::vector<double> jumps = discontinuities(f);\r
123         unsigned jump_idx = 0;\r
124         unsigned first_in_comp = 0;\r
125         for (unsigned i=0; i<temp.size(); i++){\r
126             unsigned lvl = temp[i].level, idx = temp[i].idx;\r
127             if ( i == temp.size()-1 || temp[i+1].t > jumps[jump_idx+1]){\r
128                 std::pair<unsigned,unsigned>next_data(temp[first_in_comp].level,temp[first_in_comp].idx);\r
129                 (*this)[lvl][idx].next_on_curve = next_data;\r
130                 first_in_comp = i+1;\r
131                 jump_idx += 1;\r
132             }else{\r
133                 std::pair<unsigned,unsigned> next_data(temp[i+1].level,temp[i+1].idx);\r
134                 (*this)[lvl][idx].next_on_curve = next_data;\r
135             }\r
136         }\r
137 \r
138         for (unsigned i=0; i<size(); i++){\r
139             for (unsigned j=0; j<(*this)[i].size(); j++){\r
140                 std::pair<unsigned,unsigned> next = (*this)[i][j].next_on_curve;\r
141                 (*this)[next.first][next.second].prev_on_curve = std::pair<unsigned,unsigned>(i,j);\r
142             }\r
143         }\r
144     }\r
145 \r
146     void findFirstUnused(unsigned &level, unsigned &idx){\r
147         level = size();\r
148         idx = 0;\r
149         for (unsigned i=0; i<size(); i++){\r
150             for (unsigned j=0; j<(*this)[i].size(); j++){\r
151                 if (!(*this)[i][j].used){\r
152                     level = i;\r
153                     idx = j;\r
154                     return;\r
155                 }\r
156             }\r
157         }\r
158     }\r
159     //set indexes to point to the next point in the "snake walk"\r
160     //follow_level's meaning: \r
161     //  0=yes upward\r
162     //  1=no, last move was upward,\r
163     //  2=yes downward\r
164     //  3=no, last move was downward.\r
165     void step(unsigned &level, unsigned &idx, int &direction){\r
166         if ( direction % 2 == 0 ){\r
167             if (direction == 0) {\r
168                 if ( idx >= (*this)[level].size()-1 || (*this)[level][idx+1].used ) {\r
169                     level = size();\r
170                     return;\r
171                 }\r
172                 idx += 1;\r
173             }else{\r
174                 if ( idx <= 0  || (*this)[level][idx-1].used ) {\r
175                     level = size();\r
176                     return;\r
177                 }\r
178                 idx -= 1;\r
179             }\r
180             direction += 1;\r
181             return;\r
182         }\r
183         double t = (*this)[level][idx].t;\r
184         double sign = ((*this)[level][idx].sign ? 1 : -1);\r
185         double next_t = t;\r
186         //level += 1;\r
187         direction = (direction + 1)%4;\r
188         if (level == size()){\r
189             return;\r
190         }\r
191 \r
192         std::pair<unsigned,unsigned> next;\r
193         if ( sign > 0 ){\r
194             next = (*this)[level][idx].next_on_curve;\r
195         }else{\r
196             next = (*this)[level][idx].prev_on_curve;\r
197         }\r
198 \r
199         if ( level+1 != next.first || (*this)[next.first][next.second].used ) {\r
200             level = size();\r
201             return;\r
202         }\r
203         level = next.first;\r
204         idx = next.second;\r
205 \r
206 /*********************\r
207         //look for next time on the same level\r
208         for (unsigned j=0; j<(*this)[level].size(); j++){\r
209             double tj = (*this)[level][j].t;\r
210             if ( sign*(tj-t) > 0 ){\r
211                 if( next_t == t ||  sign*(tj-next_t)<0 ){\r
212                     next_t = tj;\r
213                     idx = j;\r
214                 }\r
215             }\r
216         }\r
217         if ( next_t == t ){//not found? look at max/min time in this component, as time is "periodic".\r
218             for (unsigned j=0; j<(*this)[level].size(); j++){\r
219                 double tj = (*this)[level][j].t;\r
220                 if ( -sign*(tj-next_t) > 0 ){\r
221                     next_t = tj;\r
222                     idx = j;\r
223                 }\r
224             }\r
225         }\r
226         if ( next_t == t ){//still not found? houch! this should not happen.\r
227             level = size();\r
228             return;\r
229         }\r
230         if ( (*this)[level][idx].used ) {\r
231             level = size();\r
232             return;\r
233         }\r
234 *************************/\r
235         return;\r
236     }\r
237 };\r
238 \r
239 //-------------------------------------------------------\r
240 // Bend a path...\r
241 //-------------------------------------------------------\r
242 \r
243 Piecewise<D2<SBasis> > bend(Piecewise<D2<SBasis> > const &f, Piecewise<SBasis> bending){\r
244     D2<Piecewise<SBasis> > ff = make_cuts_independent(f);\r
245     ff[X] += compose(bending, ff[Y]);\r
246     return sectionize(ff);\r
247 }\r
248 \r
249 //--------------------------------------------------------\r
250 // The RoughHatches lpe.\r
251 //--------------------------------------------------------\r
252 LPERoughHatches::LPERoughHatches(LivePathEffectObject *lpeobject) :\r
253     Effect(lpeobject),\r
254     direction(_("Hatches width and dir"), _("Defines hatches frequency and direction"), "direction", &wr, this, Geom::Point(50,0)),\r
255     dist_rdm(_("Frequency randomness"), _("Variation of dist between hatches, in %."), "dist_rdm", &wr, this, 75),\r
256     growth(_("Growth"), _("Growth of distance between hatches."), "growth", &wr, this, 0.),\r
257 //FIXME: top/bottom names are inverted in the UI/svg and in the code!!\r
258     scale_tf(_("Half turns smoothness: 1st side, in"), _("Set smoothness/sharpness of path when reaching a 'bottom' halfturn. 0=sharp, 1=default"), "scale_bf", &wr, this, 1.),\r
259     scale_tb(_("1st side, out"), _("Set smoothness/sharpness of path when leaving a 'bottom' halfturn. 0=sharp, 1=default"), "scale_bb", &wr, this, 1.),\r
260     scale_bf(_("2nd side, in "), _("Set smoothness/sharpness of path when reaching a 'top' halfturn. 0=sharp, 1=default"), "scale_tf", &wr, this, 1.),\r
261     scale_bb(_("2nd side, out"), _("Set smoothness/sharpness of path when leaving a 'top' halfturn. 0=sharp, 1=default"), "scale_tb", &wr, this, 1.),\r
262     top_smth_variation(_("variance: 1st side"), _("Randomness of 'bottom' halfturns smoothness"), "bottom_smth_variation", &wr, this, 0),\r
263     bot_smth_variation(_("2nd side"), _("Randomness of 'top' halfturns smoothness"), "bottom_smth_variation", &wr, this, 0),\r
264 //\r
265     top_edge_variation(_("Magnitude jitter: 1st side"), _("Randomly moves 'bottom' halfsturns to produce magnitude variations."), "bottom_edge_variation", &wr, this, 0),\r
266     bot_edge_variation(_("2nd side"), _("Randomly moves 'top' halfsturns to produce magnitude variations."), "top_edge_variation", &wr, this, 0),\r
267     top_tgt_variation(_("Parallelism jitter: 1st side"), _("Add direction randomness by moving 'bottom' halfsturns tangentially to the boundary."), "bottom_tgt_variation", &wr, this, 0),\r
268     bot_tgt_variation(_("2nd side"), _("Add direction randomness by randomly moving 'top' halfsturns tangentially to the boundary."), "top_tgt_variation", &wr, this, 0),\r
269 //\r
270     do_bend(_("Bend hatches"), _("Add a global bend to the hatches (slower)"), "do_bend", &wr, this, true),\r
271     //bender(_("Global bending"), _("Relative position to ref point defines global bending direction and amount"), "bender", &wr, this, NULL, Geom::Point(-5,0)),\r
272     bender(_("Global bending"), _("Relative position to ref point defines global bending direction and amount"), "bender", &wr, this, Geom::Point(-5,0)),\r
273 //\r
274     fat_output(_("Generate thick/thin path"), _("Simulate a stroke of varrying width"), "fat_output", &wr, this, true),\r
275     stroke_width_top(_("Thikness: at 1st side"), _("Width at 'bottom' half turns"), "stroke_width_bottom", &wr, this, 1.),\r
276     stroke_width_bot(_("at 2nd side"), _("Width at 'top' halfturns"), "stroke_width_bottom", &wr, this, 1.),\r
277     front_thickness(_("from 2nd to 1st side"), _("Width of paths from 'top' to 'bottom' halfturns"), "front_thickness", &wr, this, 1.),\r
278     back_thickness(_("from 1st to 2nd side"), _("Width of paths from 'top' to 'bottom' halfturns"), "back_thickness", &wr, this, .25)\r
279 {\r
280     registerParameter( dynamic_cast<Parameter *>(&direction) );\r
281     registerParameter( dynamic_cast<Parameter *>(&dist_rdm) );\r
282     registerParameter( dynamic_cast<Parameter *>(&growth) );\r
283     registerParameter( dynamic_cast<Parameter *>(&do_bend) );\r
284     registerParameter( dynamic_cast<Parameter *>(&bender) );\r
285     registerParameter( dynamic_cast<Parameter *>(&top_edge_variation) );\r
286     registerParameter( dynamic_cast<Parameter *>(&bot_edge_variation) );\r
287     registerParameter( dynamic_cast<Parameter *>(&top_tgt_variation) );\r
288     registerParameter( dynamic_cast<Parameter *>(&bot_tgt_variation) );\r
289     registerParameter( dynamic_cast<Parameter *>(&scale_tf) );\r
290     registerParameter( dynamic_cast<Parameter *>(&scale_tb) );\r
291     registerParameter( dynamic_cast<Parameter *>(&scale_bf) );\r
292     registerParameter( dynamic_cast<Parameter *>(&scale_bb) );\r
293     registerParameter( dynamic_cast<Parameter *>(&top_smth_variation) );\r
294     registerParameter( dynamic_cast<Parameter *>(&bot_smth_variation) );\r
295     registerParameter( dynamic_cast<Parameter *>(&fat_output) );\r
296     registerParameter( dynamic_cast<Parameter *>(&stroke_width_top) );\r
297     registerParameter( dynamic_cast<Parameter *>(&stroke_width_bot) );\r
298     registerParameter( dynamic_cast<Parameter *>(&front_thickness) );\r
299     registerParameter( dynamic_cast<Parameter *>(&back_thickness) );\r
300 \r
301     //hatch_dist.param_set_range(0.1, NR_HUGE);\r
302     growth.param_set_range(-0.95, NR_HUGE);\r
303     dist_rdm.param_set_range(0, 99.);\r
304     stroke_width_top.param_set_range(0,  NR_HUGE);\r
305     stroke_width_bot.param_set_range(0,  NR_HUGE);\r
306     front_thickness.param_set_range(0, NR_HUGE);\r
307     back_thickness.param_set_range(0, NR_HUGE);\r
308 \r
309     concatenate_before_pwd2 = true;\r
310     show_orig_path = true;\r
311 }\r
312 \r
313 LPERoughHatches::~LPERoughHatches()\r
314 {\r
315 \r
316 }\r
317 \r
318 Geom::Piecewise<Geom::D2<Geom::SBasis> > \r
319 LPERoughHatches::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in){\r
320 \r
321     Piecewise<D2<SBasis> > result;\r
322     \r
323     Piecewise<D2<SBasis> > transformed_pwd2_in = pwd2_in;\r
324     Point transformed_org = direction.getOrigin();\r
325     Piecewise<SBasis> tilter;//used to bend the hatches\r
326     Matrix bend_mat;//used to bend the hatches\r
327 \r
328     if (do_bend.get_value()){\r
329         //Point bend_dir = -rot90(unit_vector(direction.getOrigin() - bender));\r
330         //double bend_amount = L2(direction.getOrigin() - bender);\r
331         Point bend_dir = -rot90(unit_vector(direction.getVector()));\r
332         double bend_amount = L2(direction.getVector());\r
333         bend_mat = Matrix(-bend_dir[Y], bend_dir[X], bend_dir[X], bend_dir[Y],0,0);\r
334         transformed_pwd2_in = pwd2_in * bend_mat;\r
335         tilter = Piecewise<SBasis>(shift(Linear(bend_amount),1));\r
336         OptRect bbox = bounds_exact( transformed_pwd2_in );\r
337         if (not(bbox)) return pwd2_in;\r
338         tilter.setDomain((*bbox)[Y]);\r
339         transformed_pwd2_in = bend(transformed_pwd2_in, tilter);\r
340         transformed_pwd2_in = transformed_pwd2_in * bend_mat.inverse();\r
341     }\r
342     hatch_dist = Geom::L2(direction.getVector())/5;\r
343     Point hatches_dir = rot90(unit_vector(direction.getVector()));\r
344     Matrix mat(-hatches_dir[Y], hatches_dir[X], hatches_dir[X], hatches_dir[Y],0,0);\r
345     transformed_pwd2_in = transformed_pwd2_in * mat;\r
346     transformed_org *= mat;\r
347         \r
348     std::vector<std::vector<Point> > snakePoints;\r
349     snakePoints = linearSnake(transformed_pwd2_in, transformed_org);\r
350     if ( snakePoints.size() > 0 ){\r
351         Piecewise<D2<SBasis> >smthSnake = smoothSnake(snakePoints); \r
352         smthSnake = smthSnake*mat.inverse();\r
353         if (do_bend.get_value()){\r
354             smthSnake = smthSnake*bend_mat;\r
355             smthSnake = bend(smthSnake, -tilter);\r
356             smthSnake = smthSnake*bend_mat.inverse();\r
357         }\r
358         return (smthSnake);\r
359     }\r
360     return pwd2_in;\r
361 }\r
362 \r
363 //------------------------------------------------\r
364 // Generate the levels with random, growth...\r
365 //------------------------------------------------\r
366 std::vector<double>\r
367 LPERoughHatches::generateLevels(Interval const &domain, double x_org){\r
368     std::vector<double> result;\r
369     int n = int((domain.min()-x_org)/hatch_dist);\r
370     double x = x_org +  n * hatch_dist;\r
371     //double x = domain.min() + double(hatch_dist)/2.;\r
372     double step = double(hatch_dist);\r
373     double scale = 1+(hatch_dist*growth/domain.extent());\r
374     while (x < domain.max()){\r
375         result.push_back(x);\r
376         double rdm = 1;\r
377         if (dist_rdm.get_value() != 0) \r
378             rdm = 1.+ double((2*dist_rdm - dist_rdm.get_value()))/100.;\r
379         x+= step*rdm;\r
380         step*=scale;//(1.+double(growth));\r
381     }\r
382     return result;\r
383 }\r
384 \r
385 \r
386 //-------------------------------------------------------\r
387 // Walk through the intersections to create linear hatches\r
388 //-------------------------------------------------------\r
389 std::vector<std::vector<Point> > \r
390 LPERoughHatches::linearSnake(Piecewise<D2<SBasis> > const &f, Point const &org){\r
391 \r
392     std::vector<std::vector<Point> > result;\r
393 \r
394     Piecewise<SBasis> x = make_cuts_independent(f)[X];\r
395     //Remark: derivative is computed twice in the 2 lines below!!\r
396     Piecewise<SBasis> dx = derivative(x);\r
397     OptInterval range = bounds_exact(x);\r
398 \r
399     if (not range) return result;\r
400     std::vector<double> levels = generateLevels(*range, org[X]);\r
401     std::vector<std::vector<double> > times;\r
402     times = multi_roots(x,levels);\r
403 \r
404 //TODO: fix multi_roots!!!*****************************************\r
405 //remove doubles :-(\r
406     std::vector<std::vector<double> > cleaned_times(levels.size(),std::vector<double>());\r
407     for (unsigned i=0; i<times.size(); i++){\r
408         if ( times[i].size()>0 ){\r
409             double last_t = times[i][0]-1;//ugly hack!!\r
410             for (unsigned j=0; j<times[i].size(); j++){\r
411                 if (times[i][j]-last_t >0.000001){\r
412                     last_t = times[i][j];\r
413                     cleaned_times[i].push_back(last_t);\r
414                 }\r
415             }\r
416         }\r
417     }\r
418     times = cleaned_times;\r
419 //     for (unsigned i=0; i<times.size(); i++){\r
420 //         std::cout << "roots on level "<<i<<": ";\r
421 //         for (unsigned j=0; j<times[i].size(); j++){\r
422 //             std::cout << times[i][j] <<" ";\r
423 //         }\r
424 //         std::cout <<"\n";\r
425 //     }\r
426 //*******************************************************************\r
427     LevelsCrossings lscs(times,f,dx);\r
428     unsigned i,j;\r
429     lscs.findFirstUnused(i,j);\r
430     \r
431     std::vector<Point> result_component;\r
432     int n = int((range->min()-org[X])/hatch_dist);\r
433     while ( i < lscs.size() ){ \r
434         int dir = 0;\r
435         //switch orientation of first segment according to org position.\r
436         if (n % 2 == 0){\r
437             j = lscs[i].size()-1;\r
438             dir = 2;\r
439         }\r
440         while ( i < lscs.size() ){\r
441             result_component.push_back(lscs[i][j].pt);\r
442             lscs[i][j].used = true;\r
443             lscs.step(i,j, dir);\r
444         }\r
445         result.push_back(result_component);\r
446         result_component = std::vector<Point>();\r
447         lscs.findFirstUnused(i,j);\r
448     }\r
449     return result;\r
450 }\r
451 \r
452 //-------------------------------------------------------\r
453 // Smooth the linear hatches according to params...\r
454 //-------------------------------------------------------\r
455 Piecewise<D2<SBasis> > \r
456 LPERoughHatches::smoothSnake(std::vector<std::vector<Point> > const &linearSnake){\r
457 \r
458     Piecewise<D2<SBasis> > result;\r
459     for (unsigned comp=0; comp<linearSnake.size(); comp++){\r
460         if (linearSnake[comp].size()>=2){\r
461             bool is_top = true;//Inversion here; due to downward y? \r
462             Point last_pt = linearSnake[comp][0];\r
463             Point last_top = linearSnake[comp][0];\r
464             Point last_bot = linearSnake[comp][0];\r
465             Point last_hdle = linearSnake[comp][0];\r
466             Point last_top_hdle = linearSnake[comp][0];\r
467             Point last_bot_hdle = linearSnake[comp][0];\r
468             Geom::Path res_comp(last_pt);\r
469             Geom::Path res_comp_top(last_pt);\r
470             Geom::Path res_comp_bot(last_pt);\r
471             unsigned i=1;\r
472             while( i+1<linearSnake[comp].size() ){\r
473                 Point pt0 = linearSnake[comp][i];\r
474                 Point pt1 = linearSnake[comp][i+1];\r
475                 Point new_pt = (pt0+pt1)/2;\r
476                 double scale_in = (is_top ? scale_tf : scale_bf );\r
477                 double scale_out = (is_top ? scale_tb : scale_bb );\r
478                 if (is_top){\r
479                     if (top_edge_variation.get_value() != 0) \r
480                         new_pt[Y] += double(top_edge_variation)-top_edge_variation.get_value()/2.;\r
481                     if (top_tgt_variation.get_value() != 0) \r
482                         new_pt[X] += double(top_tgt_variation)-top_tgt_variation.get_value()/2.;\r
483                     if (top_smth_variation.get_value() != 0) {\r
484                         scale_in*=(100.-double(top_smth_variation))/100.;\r
485                         scale_out*=(100.-double(top_smth_variation))/100.;\r
486                     }\r
487                 }else{\r
488                     if (bot_edge_variation.get_value() != 0) \r
489                         new_pt[Y] += double(bot_edge_variation)-bot_edge_variation.get_value()/2.;\r
490                     if (bot_tgt_variation.get_value() != 0) \r
491                         new_pt[X] += double(bot_tgt_variation)-bot_tgt_variation.get_value()/2.;\r
492                     if (bot_smth_variation.get_value() != 0) {\r
493                         scale_in*=(100.-double(bot_smth_variation))/100.;\r
494                         scale_out*=(100.-double(bot_smth_variation))/100.;\r
495                     }\r
496                 }\r
497                 Point new_hdle_in  = new_pt + (pt0-pt1) * (scale_in /2.);\r
498                 Point new_hdle_out = new_pt - (pt0-pt1) * (scale_out/2.);\r
499                 \r
500                 if ( fat_output.get_value() ){\r
501                     double scaled_width = double((is_top ? stroke_width_top : stroke_width_bot))/(pt1[X]-pt0[X]);\r
502                     Point hdle_offset = (pt1-pt0)*scaled_width;\r
503                     Point inside = new_pt;\r
504                     Point inside_hdle_in;\r
505                     Point inside_hdle_out;\r
506                     inside[Y]+= double((is_top ? -stroke_width_top : stroke_width_bot));\r
507                     inside_hdle_in  = inside + (new_hdle_in -new_pt) + hdle_offset * double((is_top ? front_thickness : back_thickness));\r
508                     inside_hdle_out = inside + (new_hdle_out-new_pt) - hdle_offset * double((is_top ? back_thickness : front_thickness));\r
509                     //TODO: find a good way to handle limit cases (small smthness, large stroke).\r
510                     //if (inside_hdle_in[X]  > inside[X]) inside_hdle_in = inside;\r
511                     //if (inside_hdle_out[X] < inside[X]) inside_hdle_out = inside;\r
512                     \r
513                     if (is_top){\r
514                         res_comp_top.appendNew<CubicBezier>(last_top_hdle,new_hdle_in,new_pt);\r
515                         res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,inside_hdle_in,inside);\r
516                         last_top_hdle = new_hdle_out;\r
517                         last_bot_hdle = inside_hdle_out;\r
518                     }else{\r
519                         res_comp_top.appendNew<CubicBezier>(last_top_hdle,inside_hdle_in,inside);\r
520                         res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,new_hdle_in,new_pt);\r
521                         last_top_hdle = inside_hdle_out;\r
522                         last_bot_hdle = new_hdle_out;\r
523                     }\r
524                 }else{\r
525                     res_comp.appendNew<CubicBezier>(last_hdle,new_hdle_in,new_pt);\r
526                 }\r
527             \r
528                 last_hdle = new_hdle_out;\r
529                 i+=2;\r
530                 is_top = !is_top;\r
531             }\r
532             if ( i<linearSnake[comp].size() )\r
533                 if ( fat_output.get_value() ){\r
534                     res_comp_top.appendNew<CubicBezier>(last_top_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
535                     res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
536                 }else{\r
537                     res_comp.appendNew<CubicBezier>(last_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
538                 }\r
539             if ( fat_output.get_value() ){\r
540                 res_comp = res_comp_bot;\r
541                 res_comp.append(res_comp_top.reverse(),Geom::Path::STITCH_DISCONTINUOUS);\r
542             }    \r
543             result.concat(res_comp.toPwSb());\r
544         }\r
545     }\r
546     return result;\r
547 }\r
548 \r
549 void\r
550 LPERoughHatches::doBeforeEffect (SPLPEItem */*lpeitem*/)\r
551 {\r
552     using namespace Geom;\r
553     top_edge_variation.resetRandomizer();\r
554     bot_edge_variation.resetRandomizer();\r
555     top_tgt_variation.resetRandomizer();\r
556     bot_tgt_variation.resetRandomizer();\r
557     top_smth_variation.resetRandomizer();\r
558     bot_smth_variation.resetRandomizer();\r
559     dist_rdm.resetRandomizer();\r
560 \r
561     //original_bbox(lpeitem);\r
562 }\r
563 \r
564 \r
565 void\r
566 LPERoughHatches::resetDefaults(SPItem * item)\r
567 {\r
568     Geom::OptRect bbox = item->getBounds(Geom::identity(), SPItem::GEOMETRIC_BBOX);\r
569     Geom::Point origin(0.,0.);\r
570     Geom::Point vector(50.,0.);\r
571     if (bbox) {\r
572         origin = bbox->midpoint();\r
573         vector = Geom::Point((*bbox)[X].extent()/4, 0.);\r
574         top_edge_variation.param_set_value( (*bbox)[Y].extent()/10, 0 );\r
575         bot_edge_variation.param_set_value( (*bbox)[Y].extent()/10, 0 );\r
576     }\r
577     //direction.set_and_write_new_values(origin, vector);\r
578     //bender.param_set_and_write_new_value( origin + Geom::Point(5,0) );\r
579     direction.set_and_write_new_values(origin + Geom::Point(0,-5), vector);\r
580     bender.set_and_write_new_values( origin, Geom::Point(5,0) );\r
581     hatch_dist = Geom::L2(vector)/2;\r
582 }\r
583 \r
584 \r
585 } //namespace LivePathEffect\r
586 } /* namespace Inkscape */\r
587 \r
588 /*\r
589   Local Variables:\r
590   mode:c++\r
591   c-file-style:"stroustrup"\r
592   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
593   indent-tabs-mode:nil\r
594   fill-column:99\r
595   End:\r
596 */\r
597 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r