~centralelyon2010/inkscape/imagelinks2

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