summaryrefslogtreecommitdiffstats
path: root/src/live_effects/lpe-vonkoch.cpp
blob: 277c5f3da53bc2a61cd114606e9f65c0851beff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <vector>
#include "live_effects/lpe-vonkoch.h"
// TODO due to internal breakage in glibmm headers, this must be last:
#include <glibmm/i18n.h>

namespace Inkscape {
namespace LivePathEffect {

void
VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
{  
    PathParam::param_setup_nodepath(np);
    //sp_nodepath_make_straight_path(np);
}

//FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
void
VonKochRefPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
{  
    PathParam::param_setup_nodepath(np);
    //sp_nodepath_make_straight_path(np);
}
bool
VonKochRefPathParam::param_readSVGValue(const gchar * strvalue)
{  
    Geom::PathVector old = _pathvector;
    bool res = PathParam::param_readSVGValue(strvalue);
    if (res && _pathvector.size()==1 && _pathvector.front().size()==1){
        return true;
    }else{
        _pathvector = old;
        return false;
    }
}

LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
    Effect(lpeobject),
    nbgenerations(_("N_r of generations:"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
    generator(_("Generating path:"), _("Path whose segments define the iterated transforms"), "generator", &wr, this, "M0,0 L30,0 M0,10 L10,10 M 20,10 L30,10"),
    similar_only(_("_Use uniform transforms only"), _("2 consecutive segments are used to reverse/preserve orientation only (otherwise, they define a general transform)."), "similar_only", &wr, this, false),
    drawall(_("Dra_w all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
    //,draw_boxes(_("Display boxes"), _("Display boxes instead of paths only"), "draw_boxes", &wr, this, true)
    ref_path(_("Reference segment:"), _("The reference segment. Defaults to the horizontal midline of the bbox."), "ref_path", &wr, this, "M0,0 L10,0"),
    //refA(_("Ref Start"), _("Left side middle of the reference box"), "refA", &wr, this),
    //refB(_("Ref End"), _("Right side middle of the reference box"), "refB", &wr, this),
    //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
    maxComplexity(_("_Max complexity:"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
{
    //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
    registerParameter(&ref_path);
    //registerParameter(&refA) );
    //registerParameter(&refB) );
    registerParameter(&generator);
    registerParameter(&similar_only);
    registerParameter(&nbgenerations);
    registerParameter(&drawall);
    registerParameter(&maxComplexity);
    //registerParameter(&draw_boxes) );
    apply_to_clippath_and_mask = true;
    nbgenerations.param_make_integer();
    nbgenerations.param_set_range(0, std::numeric_limits<gint>::max());
    maxComplexity.param_make_integer();
    maxComplexity.param_set_range(0, std::numeric_limits<gint>::max());
}

LPEVonKoch::~LPEVonKoch()
= default;

bool 
LPEVonKoch::doOnOpen(SPLPEItem const *lpeitem)
{
    if (!is_load || is_applied) {
        return false;
    }
    generator.reload();
    ref_path.reload();
    return false;
}


Geom::PathVector
LPEVonKoch::doEffect_path (Geom::PathVector const & path_in)
{
    using namespace Geom;
    Geom::Affine affine = generator.get_relative_affine();
    Geom::PathVector generating_path = generator.get_pathvector() * affine;
    
    if (generating_path.empty()) {
        return path_in;
    }
    if (is_load) {
        generator.reload();
        ref_path.reload();
    }

    //Collect transform matrices.
    affine = ref_path.get_relative_affine();

    Affine m0;
    Geom::Path refpath = ref_path.get_pathvector().front() * affine ;
    Point A = refpath.pointAt(0);
    Point B = refpath.pointAt(refpath.size());
    Point u = B-A;
    m0 = Affine(u[X], u[Y],-u[Y], u[X], A[X], A[Y]);
    
    //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
    //Point u = refB-refA;
    //m0 = Affine(u[X], u[Y],-u[Y], u[X], refA[X], refA[Y]);
    m0 = m0.inverse();

    std::vector<Affine> transforms;
    for (const auto & i : generating_path){
        Affine m;
        if(i.size()==1){
            Point p = i.pointAt(0);
            Point u = i.pointAt(1)-p;
            m = Affine(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
            m = m0*m;
            transforms.push_back(m);
        }else if(i.size()>=2){
            Point p = i.pointAt(1);
            Point u = i.pointAt(2)-p;
            Point v = p-i.pointAt(0);
            if (similar_only.get_value()){
                int sign = (u[X]*v[Y]-u[Y]*v[X]>=0?1:-1);
                v[X] = -u[Y]*sign;
                v[Y] =  u[X]*sign;
            }
            m = Affine(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
            m = m0*m;
            transforms.push_back(m);
        }
    }

    if (transforms.empty()){
        return path_in;
    }

    //Do nothing if the output is too complex... 
    int path_in_complexity = 0;
    for (const auto & k : path_in){
            path_in_complexity+=k.size();
    }
    double complexity = std::pow(transforms.size(), nbgenerations) * path_in_complexity;
    if (drawall.get_value()){
        int k = transforms.size();
        if(k>1){
            complexity = (std::pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
        }else{
            complexity = nbgenerations*k*path_in_complexity;
        }
    }
    if (complexity > double(maxComplexity)){
        g_warning("VonKoch lpe's output too complex. Effect bypassed.");
        return path_in;
    }

    //Generate path:
    Geom::PathVector pathi = path_in;
    Geom::PathVector path_out = path_in;
    
    for (unsigned i = 0; i<nbgenerations; i++){
        if (drawall.get_value()){
            path_out =  path_in;
            complexity = path_in_complexity;
        }else{
            path_out = Geom::PathVector();
            complexity = 0;
        }
        for (const auto & transform : transforms){
            for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
                path_out.push_back(pathi[k]*transform); 
                complexity+=pathi[k].size();
            }
        }
        pathi = path_out;
    }
    return path_out;
}


//Useful?? 
//void 
//LPEVonKoch::addCanvasIndicators(SPLPEItem const */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
/*{
    using namespace Geom;
    if (draw_boxes.get_value()){
        double ratio = .5;
        if (similar_only.get_value()) ratio = boundingbox_Y.extent()/boundingbox_X.extent()/2;
        
        Point BB1,BB2,BB3,BB4,v;
        
        //Draw the reference box  (ref_path is supposed to consist in one line segment)
        //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
        Geom::Path refpath = ref_path.get_pathvector().front();
        if (refpath.size()==1){
            BB1 = BB4 = refpath.front().pointAt(0);
            BB2 = BB3 = refpath.front().pointAt(1);
            v = rot90(BB2 - BB1)*ratio;
            BB1 -= v;
            BB2 -= v;
            BB3 += v;
            BB4 += v;
            Geom::Path refbox(BB1);
            refbox.appendNew<LineSegment>(BB2);
            refbox.appendNew<LineSegment>(BB3);
            refbox.appendNew<LineSegment>(BB4);
            refbox.close();
            PathVector refbox_as_vect;
            refbox_as_vect.push_back(refbox);
            hp_vec.push_back(refbox_as_vect);
        }
        //Draw the transformed boxes
        Geom::PathVector generating_path = generator.get_pathvector();
        for (unsigned i=0;i<generating_path.size(); i++){
            if (generating_path[i].size()==0){
                //Ooops! this should not happen.
            }else if (generating_path[i].size()==1){
                BB1 = BB4 = generating_path[i].pointAt(0);
                BB2 = BB3 = generating_path[i].pointAt(1);
                v = rot90(BB2 - BB1)*ratio;
            }else{//Only tak the first 2 segments into account.
                BB1 = BB4 = generating_path[i].pointAt(1);
                BB2 = BB3 = generating_path[i].pointAt(2);
                if(similar_only.get_value()){
                    v = rot90(BB2 - BB1)*ratio;
            }else{
                    v = (generating_path[i].pointAt(0) - BB1)*ratio;
                }
            }
            BB1 -= v;
            BB2 -= v;
            BB3 += v;
            BB4 += v;
            Geom::Path path(BB1);
            path.appendNew<LineSegment>(BB2);
            path.appendNew<LineSegment>(BB3);
            path.appendNew<LineSegment>(BB4);
            path.close();
            PathVector pathv;
            pathv.push_back(path);
            hp_vec.push_back(pathv);
        }
    }
}
*/

void
LPEVonKoch::doBeforeEffect (SPLPEItem const* lpeitem)
{
    using namespace Geom;
    original_bbox(lpeitem, false, true);
    
    Geom::PathVector paths = ref_path.get_pathvector();
    Geom::Point A,B;
    if (paths.empty()||paths.front().size()==0){
        //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
        //refA.param_setValue( Geom::Point(boundingbox_X.min(), boundingbox_Y.middle()) );
        //refB.param_setValue( Geom::Point(boundingbox_X.max(), boundingbox_Y.middle()) );
        A = Point(boundingbox_X.min(), boundingbox_Y.middle());
        B = Point(boundingbox_X.max(), boundingbox_Y.middle());
    }else{
        A = paths.front().pointAt(0);
        B = paths.front().pointAt(paths.front().size());
    }
    if (paths.size()!=1||paths.front().size()!=1){
        Geom::Path tmp_path(A);
        tmp_path.appendNew<LineSegment>(B);
        Geom::PathVector tmp_pathv;
        tmp_pathv.push_back(tmp_path);
        ref_path.set_new_value(tmp_pathv,true);
    }
}


void
LPEVonKoch::resetDefaults(SPItem const* item)
{
    Effect::resetDefaults(item);

    using namespace Geom;
    original_bbox(SP_LPE_ITEM(item), false, true);

    Point A,B;
    A[Geom::X] = boundingbox_X.min();
    A[Geom::Y] = boundingbox_Y.middle();
    B[Geom::X] = boundingbox_X.max();
    B[Geom::Y] = boundingbox_Y.middle();

    Geom::PathVector paths,refpaths;
    Geom::Path path = Geom::Path(A);
    path.appendNew<Geom::LineSegment>(B);

    refpaths.push_back(path);
    ref_path.set_new_value(refpaths, true);

    paths.push_back(path * Affine(1./3,0,0,1./3, A[X]*2./3, A[Y]*2./3 + boundingbox_Y.extent()/2));
    paths.push_back(path * Affine(1./3,0,0,1./3, B[X]*2./3, B[Y]*2./3 + boundingbox_Y.extent()/2));
    generator.set_new_value(paths, true);

    //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
    //refA[Geom::X] = boundingbox_X.min();
    //refA[Geom::Y] = boundingbox_Y.middle();
    //refB[Geom::X] = boundingbox_X.max();
    //refB[Geom::Y] = boundingbox_Y.middle();
    //Geom::PathVector paths;
    //Geom::Path path = Geom::Path( (Point) refA);
    //path.appendNew<Geom::LineSegment>( (Point) refB );
    //paths.push_back(path * Affine(1./3,0,0,1./3, refA[X]*2./3, refA[Y]*2./3 + boundingbox_Y.extent()/2));
    //paths.push_back(path * Affine(1./3,0,0,1./3, refB[X]*2./3, refB[Y]*2./3 + boundingbox_Y.extent()/2));
    //paths.push_back(path);
    //generator.set_new_value(paths, true);
}

} // namespace LivePathEffect
} /* namespace Inkscape */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :