Code

Merge from fe-moved
[inkscape.git] / src / live_effects / lpe-hatches.cpp
1 #define INKSCAPE_LPE_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-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 #if 0\r
145         std::cout<<"\n";\r
146         for (unsigned i=0; i<temp.size()-1; i++){\r
147             std::cout<<temp[i].level<<","<<temp[i].idx<<" -> ";\r
148         }\r
149         std::cout<<"\n";\r
150         for (unsigned i=0; i<size(); i++){\r
151             for (unsigned j=0; j<(*this)[i].size(); j++){\r
152                 std::cout<<"level:"<<i<<", idx:"<<j<<" -  ";\r
153                 std::cout<<"next:"<<(*this)[i][j].next_on_curve.first<<",";\r
154                 std::cout<<(*this)[i][j].next_on_curve.second<<" - ";\r
155                 std::cout<<"prev:"<<(*this)[i][j].prev_on_curve.first<<",";\r
156                 std::cout<<(*this)[i][j].prev_on_curve.second<<"\n";\r
157             }\r
158         }\r
159 #endif\r
160     }\r
161 \r
162     void findFirstUnused(unsigned &level, unsigned &idx){\r
163         level = size();\r
164         idx = 0;\r
165         for (unsigned i=0; i<size(); i++){\r
166             for (unsigned j=0; j<(*this)[i].size(); j++){\r
167                 if (!(*this)[i][j].used){\r
168                     level = i;\r
169                     idx = j;\r
170                     return;\r
171                 }\r
172             }\r
173         }\r
174     }\r
175     //set indexes to point to the next point in the "snake walk"\r
176     //follow_level's meaning: \r
177     //  0=yes upward\r
178     //  1=no, last move was upward,\r
179     //  2=yes downward\r
180     //  3=no, last move was downward.\r
181     void step(unsigned &level, unsigned &idx, int &direction){\r
182         if ( direction % 2 == 0 ){\r
183             if (direction == 0) {\r
184                 if ( idx >= (*this)[level].size()-1 || (*this)[level][idx+1].used ) {\r
185                     level = size();\r
186                     return;\r
187                 }\r
188                 idx += 1;\r
189             }else{\r
190                 if ( idx <= 0  || (*this)[level][idx-1].used ) {\r
191                     level = size();\r
192                     return;\r
193                 }\r
194                 idx -= 1;\r
195             }\r
196             direction += 1;\r
197             return;\r
198         }\r
199         double t = (*this)[level][idx].t;\r
200         double sign = ((*this)[level][idx].sign ? 1 : -1);\r
201         double next_t = t;\r
202         //level += 1;\r
203         direction = (direction + 1)%4;\r
204         if (level == size()){\r
205             return;\r
206         }\r
207 \r
208         std::pair<unsigned,unsigned> next;\r
209         if ( sign > 0 ){\r
210             next = (*this)[level][idx].next_on_curve;\r
211         }else{\r
212             next = (*this)[level][idx].prev_on_curve;\r
213         }\r
214 \r
215         if ( level+1 != next.first || (*this)[next.first][next.second].used ) {\r
216             level = size();\r
217             return;\r
218         }\r
219         level = next.first;\r
220         idx = next.second;\r
221 \r
222 /*********************\r
223         //look for next time on the same level\r
224         for (unsigned j=0; j<(*this)[level].size(); j++){\r
225             double tj = (*this)[level][j].t;\r
226             if ( sign*(tj-t) > 0 ){\r
227                 if( next_t == t ||  sign*(tj-next_t)<0 ){\r
228                     next_t = tj;\r
229                     idx = j;\r
230                 }\r
231             }\r
232         }\r
233         if ( next_t == t ){//not found? look at max/min time in this component, as time is "periodic".\r
234             for (unsigned j=0; j<(*this)[level].size(); j++){\r
235                 double tj = (*this)[level][j].t;\r
236                 if ( -sign*(tj-next_t) > 0 ){\r
237                     next_t = tj;\r
238                     idx = j;\r
239                 }\r
240             }\r
241         }\r
242         if ( next_t == t ){//still not found? houch! this should not happen.\r
243             level = size();\r
244             return;\r
245         }\r
246         if ( (*this)[level][idx].used ) {\r
247             level = size();\r
248             return;\r
249         }\r
250 *************************/\r
251         return;\r
252     }\r
253 };\r
254 \r
255 //-------------------------------------------------------\r
256 // Bend a path...\r
257 //-------------------------------------------------------\r
258 \r
259 Piecewise<D2<SBasis> > bend(Piecewise<D2<SBasis> > const &f, Piecewise<SBasis> bending){\r
260     D2<Piecewise<SBasis> > ff = make_cuts_independent(f);\r
261     ff[X] += compose(bending, ff[Y]);\r
262     return sectionize(ff);\r
263 }\r
264 \r
265 //--------------------------------------------------------\r
266 // The Hatches lpe.\r
267 //--------------------------------------------------------\r
268 LPEHatches::LPEHatches(LivePathEffectObject *lpeobject) :\r
269     Effect(lpeobject),\r
270     dist_rdm(_("Dist randomness"), _("Variation of dist between hatches, in %."), "dist_rdm", &wr, this, 75),\r
271     growth(_("Growth"), _("Growth of distance between hatches."), "growth", &wr, this, 0.),\r
272     scale_tf(_("Start smothness (front side)"), _("MISSING DESCRIPTION"), "scale_tf", &wr, this, 1.),\r
273     scale_tb(_("Start smothness (back side)"), _("MISSING DESCRIPTION"), "scale_tb", &wr, this, 1.),\r
274     scale_bf(_("End smothness (front side)"), _("MISSING DESCRIPTION"), "scale_bf", &wr, this, 1.),\r
275     scale_bb(_("End smothness (back side)"), _("MISSING DESCRIPTION"), "scale_bb", &wr, this, 1.),\r
276     top_edge_variation(_("Start edge variance"), _("The amount of random jitter to move the hatches start"), "top_edge_variation", &wr, this, 0),\r
277     bot_edge_variation(_("End edge variance"), _("The amount of random jitter to move the hatches end"), "bot_edge_variation", &wr, this, 0),\r
278     top_tgt_variation(_("Start tangential variance"), _("The amount of random jitter to move the hatches start along the boundary"), "top_tgt_variation", &wr, this, 0),\r
279     bot_tgt_variation(_("End tangential variance"), _("The amount of random jitter to move the hatches end along the boundary"), "bot_tgt_variation", &wr, this, 0),\r
280     top_smth_variation(_("Start smoothness variance"), _("Randomness of the smoothness of the U turn at hatches start"), "top_smth_variation", &wr, this, 0),\r
281     bot_smth_variation(_("End spacing variance"), _("Randomness of the smoothness of the U turn at hatches end"), "bot_smth_variation", &wr, this, 0),\r
282     fat_output(_("Generate thick/thin path"), _("Simulate a stroke of varrying width"), "fat_output", &wr, this, true),\r
283     do_bend(_("Bend hatches"), _("Add a global bend to the hatches (slower)"), "do_bend", &wr, this, true),\r
284     stroke_width_top(_("Stroke width (start side)"), _("Width at hatches 'start'"), "stroke_width_top", &wr, this, 1.),\r
285     stroke_width_bot(_("Stroke width (end side)"), _("Width at hatches 'end'"), "stroke_width_bot", &wr, this, 1.),\r
286     front_thickness(_("Front thickness (%)"), _("MISSING DESCRIPTION"), "front_thickness", &wr, this, 1.),\r
287     back_thickness(_("Back thickness (%)"), _("MISSING DESCRIPTION"), "back_thickness", &wr, this, .25),\r
288     bender(_("Global bending"), _("Relative position to ref point defines global bending direction and amount"), "bender", &wr, this, NULL, Geom::Point(-5,0)),\r
289     direction(_("Hatches width and dir"), _("Defines hatches frequency and direction"), "direction", &wr, this, Geom::Point(50,0))\r
290 {\r
291     registerParameter( dynamic_cast<Parameter *>(&direction) );\r
292     registerParameter( dynamic_cast<Parameter *>(&do_bend) );\r
293     registerParameter( dynamic_cast<Parameter *>(&bender) );\r
294     registerParameter( dynamic_cast<Parameter *>(&dist_rdm) );\r
295     registerParameter( dynamic_cast<Parameter *>(&growth) );\r
296     registerParameter( dynamic_cast<Parameter *>(&top_edge_variation) );\r
297     registerParameter( dynamic_cast<Parameter *>(&bot_edge_variation) );\r
298     registerParameter( dynamic_cast<Parameter *>(&top_tgt_variation) );\r
299     registerParameter( dynamic_cast<Parameter *>(&bot_tgt_variation) );\r
300     registerParameter( dynamic_cast<Parameter *>(&scale_tf) );\r
301     registerParameter( dynamic_cast<Parameter *>(&scale_tb) );\r
302     registerParameter( dynamic_cast<Parameter *>(&scale_bf) );\r
303     registerParameter( dynamic_cast<Parameter *>(&scale_bb) );\r
304     registerParameter( dynamic_cast<Parameter *>(&top_smth_variation) );\r
305     registerParameter( dynamic_cast<Parameter *>(&bot_smth_variation) );\r
306     registerParameter( dynamic_cast<Parameter *>(&fat_output) );\r
307     registerParameter( dynamic_cast<Parameter *>(&stroke_width_top) );\r
308     registerParameter( dynamic_cast<Parameter *>(&stroke_width_bot) );\r
309     registerParameter( dynamic_cast<Parameter *>(&front_thickness) );\r
310     registerParameter( dynamic_cast<Parameter *>(&back_thickness) );\r
311 \r
312     //hatch_dist.param_set_range(0.1, NR_HUGE);\r
313     growth.param_set_range(-0.95, NR_HUGE);\r
314     dist_rdm.param_set_range(0, 99.);\r
315     stroke_width_top.param_set_range(0,  NR_HUGE);\r
316     stroke_width_bot.param_set_range(0,  NR_HUGE);\r
317     front_thickness.param_set_range(0, NR_HUGE);\r
318     back_thickness.param_set_range(0, NR_HUGE);\r
319 \r
320     concatenate_before_pwd2 = true;\r
321     show_orig_path = true;\r
322 }\r
323 \r
324 LPEHatches::~LPEHatches()\r
325 {\r
326 \r
327 }\r
328 \r
329 Geom::Piecewise<Geom::D2<Geom::SBasis> > \r
330 LPEHatches::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in){\r
331 \r
332     Piecewise<D2<SBasis> > result;\r
333     \r
334     Piecewise<D2<SBasis> > transformed_pwd2_in = pwd2_in;\r
335     Piecewise<SBasis> tilter;//used to bend the hatches\r
336     Matrix bend_mat;//used to bend the hatches\r
337 \r
338     if (do_bend.get_value()){\r
339         Point bend_dir = -rot90(unit_vector(direction.getOrigin() - bender));\r
340         double bend_amount = L2(direction.getOrigin() - bender);\r
341         bend_mat = Matrix(-bend_dir[Y], bend_dir[X], bend_dir[X], bend_dir[Y],0,0);\r
342         transformed_pwd2_in = pwd2_in * bend_mat;\r
343         tilter = Piecewise<SBasis>(shift(Linear(bend_amount),1));\r
344         OptRect bbox = bounds_exact( transformed_pwd2_in );\r
345         if (not(bbox)) return pwd2_in;\r
346         tilter.setDomain((*bbox)[Y]);\r
347         transformed_pwd2_in = bend(transformed_pwd2_in, tilter);\r
348         transformed_pwd2_in = transformed_pwd2_in * bend_mat.inverse();\r
349     }\r
350     hatch_dist = Geom::L2(direction.getVector())/5;\r
351     Point hatches_dir = rot90(unit_vector(direction.getVector()));\r
352     Matrix mat(-hatches_dir[Y], hatches_dir[X], hatches_dir[X], hatches_dir[Y],0,0);\r
353     transformed_pwd2_in = transformed_pwd2_in * mat;\r
354         \r
355     std::vector<std::vector<Point> > snakePoints;\r
356     snakePoints = linearSnake(transformed_pwd2_in);\r
357     if ( snakePoints.size() > 0 ){\r
358         Piecewise<D2<SBasis> >smthSnake = smoothSnake(snakePoints); \r
359         smthSnake = smthSnake*mat.inverse();\r
360         if (do_bend.get_value()){\r
361             smthSnake = smthSnake*bend_mat;\r
362             smthSnake = bend(smthSnake, -tilter);\r
363             smthSnake = smthSnake*bend_mat.inverse();\r
364         }\r
365         return (smthSnake);\r
366     }\r
367     return pwd2_in;\r
368 }\r
369 \r
370 //------------------------------------------------\r
371 // Generate the levels with random, growth...\r
372 //------------------------------------------------\r
373 std::vector<double>\r
374 LPEHatches::generateLevels(Interval const &domain){\r
375     std::vector<double> result;\r
376     double x = domain.min() + double(hatch_dist)/2.;\r
377     double step = double(hatch_dist);\r
378     double scale = 1+(hatch_dist*growth/domain.extent());\r
379     while (x < domain.max()){\r
380         result.push_back(x);\r
381         double rdm = 1;\r
382         if (dist_rdm.get_value() != 0) \r
383             rdm = 1.+ double((2*dist_rdm - dist_rdm.get_value()))/100.;\r
384         x+= step*rdm;\r
385         step*=scale;//(1.+double(growth));\r
386     }\r
387     return result;\r
388 }\r
389 \r
390 \r
391 //-------------------------------------------------------\r
392 // Walk through the intersections to create linear hatches\r
393 //-------------------------------------------------------\r
394 std::vector<std::vector<Point> > \r
395 LPEHatches::linearSnake(Piecewise<D2<SBasis> > const &f){\r
396 \r
397     std::vector<std::vector<Point> > result;\r
398 \r
399     Piecewise<SBasis> x = make_cuts_independent(f)[X];\r
400     //Rque: derivative is computed twice in the 2 lines below!!\r
401     Piecewise<SBasis> dx = derivative(x);\r
402     OptInterval range = bounds_exact(x);\r
403 \r
404     if (not range) return result;\r
405     std::vector<double> levels = generateLevels(*range);\r
406     std::vector<std::vector<double> > times;\r
407     times = multi_roots(x,levels);\r
408 \r
409 //TODO: fix multi_roots!!!*****************************************\r
410 //remove doubles :-(\r
411     std::vector<std::vector<double> > cleaned_times(levels.size(),std::vector<double>());\r
412     for (unsigned i=0; i<times.size(); i++){\r
413         if ( times[i].size()>0 ){\r
414             double last_t = times[i][0]-1;//ugly hack!!\r
415             for (unsigned j=0; j<times[i].size(); j++){\r
416                 if (times[i][j]-last_t >0.000001){\r
417                     last_t = times[i][j];\r
418                     cleaned_times[i].push_back(last_t);\r
419                 }\r
420             }\r
421         }\r
422     }\r
423     times = cleaned_times;\r
424 //     for (unsigned i=0; i<times.size(); i++){\r
425 //         std::cout << "roots on level "<<i<<": ";\r
426 //         for (unsigned j=0; j<times[i].size(); j++){\r
427 //             std::cout << times[i][j] <<" ";\r
428 //         }\r
429 //         std::cout <<"\n";\r
430 //     }\r
431 //*******************************************************************\r
432     LevelsCrossings lscs(times,f,dx);\r
433     unsigned i,j;\r
434     lscs.findFirstUnused(i,j);\r
435     std::vector<Point> result_component;\r
436     while ( i < lscs.size() ){ \r
437         int dir = 0;\r
438         while ( i < lscs.size() ){\r
439             result_component.push_back(lscs[i][j].pt);\r
440             lscs[i][j].used = true;\r
441             lscs.step(i,j, dir);\r
442         }\r
443         result.push_back(result_component);\r
444         result_component = std::vector<Point>();\r
445         lscs.findFirstUnused(i,j);\r
446     }\r
447     return result;\r
448 }\r
449 \r
450 //-------------------------------------------------------\r
451 // Smooth the linear hatches according to params...\r
452 //-------------------------------------------------------\r
453 Piecewise<D2<SBasis> > \r
454 LPEHatches::smoothSnake(std::vector<std::vector<Point> > const &linearSnake){\r
455 \r
456     Piecewise<D2<SBasis> > result;\r
457     for (unsigned comp=0; comp<linearSnake.size(); comp++){\r
458         if (linearSnake[comp].size()>=2){\r
459             bool is_top = true;//Inversion here; due to downward y? \r
460             Point last_pt = linearSnake[comp][0];\r
461             Point last_top = linearSnake[comp][0];\r
462             Point last_bot = linearSnake[comp][0];\r
463             Point last_hdle = linearSnake[comp][0];\r
464             Point last_top_hdle = linearSnake[comp][0];\r
465             Point last_bot_hdle = linearSnake[comp][0];\r
466             Geom::Path res_comp(last_pt);\r
467             Geom::Path res_comp_top(last_pt);\r
468             Geom::Path res_comp_bot(last_pt);\r
469             unsigned i=1;\r
470             while( i+1<linearSnake[comp].size() ){\r
471                 Point pt0 = linearSnake[comp][i];\r
472                 Point pt1 = linearSnake[comp][i+1];\r
473                 Point new_pt = (pt0+pt1)/2;\r
474                 double scale_in = (is_top ? scale_tf : scale_bf );\r
475                 double scale_out = (is_top ? scale_tb : scale_bb );\r
476                 if (is_top){\r
477                     if (top_edge_variation.get_value() != 0) \r
478                         new_pt[Y] += double(top_edge_variation)-top_edge_variation.get_value()/2.;\r
479                     if (top_tgt_variation.get_value() != 0) \r
480                         new_pt[X] += double(top_tgt_variation)-top_tgt_variation.get_value()/2.;\r
481                     if (top_smth_variation.get_value() != 0) {\r
482                         scale_in*=(100.-double(top_smth_variation))/100.;\r
483                         scale_out*=(100.-double(top_smth_variation))/100.;\r
484                     }\r
485                 }else{\r
486                     if (bot_edge_variation.get_value() != 0) \r
487                         new_pt[Y] += double(bot_edge_variation)-bot_edge_variation.get_value()/2.;\r
488                     if (bot_tgt_variation.get_value() != 0) \r
489                         new_pt[X] += double(bot_tgt_variation)-bot_tgt_variation.get_value()/2.;\r
490                     if (bot_smth_variation.get_value() != 0) {\r
491                         scale_in*=(100.-double(bot_smth_variation))/100.;\r
492                         scale_out*=(100.-double(bot_smth_variation))/100.;\r
493                     }\r
494                 }\r
495                 Point new_hdle_in  = new_pt + (pt0-pt1) * (scale_in /2.);\r
496                 Point new_hdle_out = new_pt - (pt0-pt1) * (scale_out/2.);\r
497                 \r
498                 if ( fat_output.get_value() ){\r
499                     double scaled_width = double((is_top ? stroke_width_top : stroke_width_bot))/(pt1[X]-pt0[X]);\r
500                     Point hdle_offset = (pt1-pt0)*scaled_width;\r
501                     Point inside = new_pt;\r
502                     Point inside_hdle_in;\r
503                     Point inside_hdle_out;\r
504                     inside[Y]+= double((is_top ? -stroke_width_top : stroke_width_bot));\r
505                     inside_hdle_in  = inside + (new_hdle_in -new_pt) + hdle_offset * double((is_top ? front_thickness : back_thickness));\r
506                     inside_hdle_out = inside + (new_hdle_out-new_pt) - hdle_offset * double((is_top ? back_thickness : front_thickness));\r
507                     //TODO: find a good way to handle limit cases (small smthness, large stroke).\r
508                     //if (inside_hdle_in[X]  > inside[X]) inside_hdle_in = inside;\r
509                     //if (inside_hdle_out[X] < inside[X]) inside_hdle_out = inside;\r
510                     \r
511                     if (is_top){\r
512                         res_comp_top.appendNew<CubicBezier>(last_top_hdle,new_hdle_in,new_pt);\r
513                         res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,inside_hdle_in,inside);\r
514                         last_top_hdle = new_hdle_out;\r
515                         last_bot_hdle = inside_hdle_out;\r
516                     }else{\r
517                         res_comp_top.appendNew<CubicBezier>(last_top_hdle,inside_hdle_in,inside);\r
518                         res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,new_hdle_in,new_pt);\r
519                         last_top_hdle = inside_hdle_out;\r
520                         last_bot_hdle = new_hdle_out;\r
521                     }\r
522                 }else{\r
523                     res_comp.appendNew<CubicBezier>(last_hdle,new_hdle_in,new_pt);\r
524                 }\r
525             \r
526                 last_hdle = new_hdle_out;\r
527                 i+=2;\r
528                 is_top = !is_top;\r
529             }\r
530             if ( i<linearSnake[comp].size() )\r
531                 if ( fat_output.get_value() ){\r
532                     res_comp_top.appendNew<CubicBezier>(last_top_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
533                     res_comp_bot.appendNew<CubicBezier>(last_bot_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
534                 }else{\r
535                     res_comp.appendNew<CubicBezier>(last_hdle,linearSnake[comp][i],linearSnake[comp][i]);\r
536                 }\r
537             if ( fat_output.get_value() ){\r
538                 res_comp = res_comp_bot;\r
539                 res_comp.append(res_comp_top.reverse(),Geom::Path::STITCH_DISCONTINUOUS);\r
540             }    \r
541             result.concat(res_comp.toPwSb());\r
542         }\r
543     }\r
544     return result;\r
545 }\r
546 \r
547 void\r
548 LPEHatches::doBeforeEffect (SPLPEItem */*lpeitem*/)\r
549 {\r
550     using namespace Geom;\r
551     top_edge_variation.resetRandomizer();\r
552     bot_edge_variation.resetRandomizer();\r
553     top_tgt_variation.resetRandomizer();\r
554     bot_tgt_variation.resetRandomizer();\r
555     top_smth_variation.resetRandomizer();\r
556     bot_smth_variation.resetRandomizer();\r
557     dist_rdm.resetRandomizer();\r
558 \r
559     //original_bbox(lpeitem);\r
560 }\r
561 \r
562 \r
563 void\r
564 LPEHatches::resetDefaults(SPItem * item)\r
565 {\r
566     Geom::OptRect bbox = item->getBounds(Geom::identity(), SPItem::GEOMETRIC_BBOX);\r
567     Geom::Point origin(0.,0.);\r
568     Geom::Point vector(50.,0.);\r
569     if (bbox) {\r
570         origin = bbox->midpoint();\r
571         vector = Geom::Point((*bbox)[X].extent()/4, 0.);\r
572         top_edge_variation.param_set_value( (*bbox)[Y].extent()/10, 0 );\r
573         bot_edge_variation.param_set_value( (*bbox)[Y].extent()/10, 0 );\r
574     }\r
575     direction.set_and_write_new_values(origin, vector);\r
576     bender.param_set_and_write_new_value( origin + Geom::Point(5,0) );\r
577     hatch_dist = Geom::L2(vector)/5;\r
578 }\r
579 \r
580 \r
581 } //namespace LivePathEffect\r
582 } /* namespace Inkscape */\r
583 \r
584 /*\r
585   Local Variables:\r
586   mode:c++\r
587   c-file-style:"stroustrup"\r
588   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
589   indent-tabs-mode:nil\r
590   fill-column:99\r
591   End:\r
592 */\r
593 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r