1
#define INKSCAPE_LPE_VONKOCH_CPP
4
* Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
6
* Released under GNU GPL, read the file 'COPYING' for more information
10
#include "live_effects/lpe-vonkoch.h"
12
#include <2geom/transforms.h>
16
namespace LivePathEffect {
19
VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
21
PathParam::param_setup_nodepath(np);
22
sp_nodepath_make_straight_path(np);
25
//FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
27
VonKochRefPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
29
PathParam::param_setup_nodepath(np);
30
sp_nodepath_make_straight_path(np);
33
VonKochRefPathParam::param_readSVGValue(const gchar * strvalue)
35
std::vector<Geom::Path> old = _pathvector;
36
bool res = PathParam::param_readSVGValue(strvalue);
37
if (res && _pathvector.size()==1 && _pathvector.front().size()==1){
45
LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
47
nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
48
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"),
49
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),
50
drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
51
//,draw_boxes(_("Display boxes"), _("Display boxes instead of paths only"), "draw_boxes", &wr, this, true)
52
ref_path(_("Reference segment"), _("The reference segment. Defaults to bbox diameter."), "ref_path", &wr, this, "M0,0 L10,0"),
53
//refA(_("Ref Start"), _("Left side middle of the reference box"), "refA", &wr, this),
54
//refB(_("Ref End"), _("Right side middle of the reference box"), "refB", &wr, this),
55
//FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
56
maxComplexity(_("Max complexity"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
58
//FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
59
registerParameter( dynamic_cast<Parameter *>(&ref_path) );
60
//registerParameter( dynamic_cast<Parameter *>(&refA) );
61
//registerParameter( dynamic_cast<Parameter *>(&refB) );
62
registerParameter( dynamic_cast<Parameter *>(&generator) );
63
registerParameter( dynamic_cast<Parameter *>(&similar_only) );
64
registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
65
registerParameter( dynamic_cast<Parameter *>(&drawall) );
66
registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
67
//registerParameter( dynamic_cast<Parameter *>(&draw_boxes) );
69
nbgenerations.param_make_integer();
70
nbgenerations.param_set_range(0, NR_HUGE);
71
maxComplexity.param_make_integer();
72
maxComplexity.param_set_range(0, NR_HUGE);
75
LPEVonKoch::~LPEVonKoch()
80
std::vector<Geom::Path>
81
LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
85
std::vector<Geom::Path> generating_path = generator.get_pathvector();
87
if (generating_path.size()==0) {
91
//Collect transform matrices.
93
Geom::Path refpath = ref_path.get_pathvector().front();
94
Point A = refpath.pointAt(0);
95
Point B = refpath.pointAt(refpath.size());
97
m0 = Matrix(u[X], u[Y],-u[Y], u[X], A[X], A[Y]);
99
//FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
100
//Point u = refB-refA;
101
//m0 = Matrix(u[X], u[Y],-u[Y], u[X], refA[X], refA[Y]);
104
std::vector<Matrix> transforms;
105
for (unsigned i=0; i<generating_path.size(); i++){
107
if(generating_path[i].size()==1){
108
Point p = generating_path[i].pointAt(0);
109
Point u = generating_path[i].pointAt(1)-p;
110
m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
112
transforms.push_back(m);
113
}else if(generating_path[i].size()>=2){
114
Point p = generating_path[i].pointAt(1);
115
Point u = generating_path[i].pointAt(2)-p;
116
Point v = p-generating_path[i].pointAt(0);
117
if (similar_only.get_value()){
118
int sign = (u[X]*v[Y]-u[Y]*v[X]>=0?1:-1);
122
m = Matrix(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
124
transforms.push_back(m);
128
if (transforms.size()==0){
132
//Do nothing if the output is too complex...
133
int path_in_complexity = 0;
134
for (unsigned k = 0; k < path_in.size(); k++){
135
path_in_complexity+=path_in[k].size();
137
double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
138
if (drawall.get_value()){
139
int k = transforms.size();
141
complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
143
complexity = nbgenerations*k*path_in_complexity;
146
complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
148
if (complexity > double(maxComplexity)){
149
g_warning("VonKoch lpe's output too complex. Effect bypassed.");
154
std::vector<Geom::Path> pathi = path_in;
155
std::vector<Geom::Path> path_out = path_in;
157
for (unsigned i = 0; i<nbgenerations; i++){
158
if (drawall.get_value()){
160
complexity = path_in_complexity;
162
path_out = std::vector<Geom::Path>();
165
for (unsigned j = 0; j<transforms.size(); j++){
166
for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
167
path_out.push_back(pathi[k]*transforms[j]);
168
complexity+=pathi[k].size();
179
//LPEVonKoch::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
181
using namespace Geom;
182
if (draw_boxes.get_value()){
184
if (similar_only.get_value()) ratio = boundingbox_Y.extent()/boundingbox_X.extent()/2;
186
Point BB1,BB2,BB3,BB4,v;
188
//Draw the reference box (ref_path is supposed to consist in one line segment)
189
//FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
190
Geom::Path refpath = ref_path.get_pathvector().front();
191
if (refpath.size()==1){
192
BB1 = BB4 = refpath.front().pointAt(0);
193
BB2 = BB3 = refpath.front().pointAt(1);
194
v = rot90(BB2 - BB1)*ratio;
199
Geom::Path refbox(BB1);
200
refbox.appendNew<LineSegment>(BB2);
201
refbox.appendNew<LineSegment>(BB3);
202
refbox.appendNew<LineSegment>(BB4);
204
PathVector refbox_as_vect;
205
refbox_as_vect.push_back(refbox);
206
hp_vec.push_back(refbox_as_vect);
208
//Draw the transformed boxes
209
std::vector<Geom::Path> generating_path = generator.get_pathvector();
210
for (unsigned i=0;i<generating_path.size(); i++){
211
if (generating_path[i].size()==0){
212
//Ooops! this should not happen.
213
}else if (generating_path[i].size()==1){
214
BB1 = BB4 = generating_path[i].pointAt(0);
215
BB2 = BB3 = generating_path[i].pointAt(1);
216
v = rot90(BB2 - BB1)*ratio;
217
}else{//Only tak the first 2 segments into account.
218
BB1 = BB4 = generating_path[i].pointAt(1);
219
BB2 = BB3 = generating_path[i].pointAt(2);
220
if(similar_only.get_value()){
221
v = rot90(BB2 - BB1)*ratio;
223
v = (generating_path[i].pointAt(0) - BB1)*ratio;
230
Geom::Path path(BB1);
231
path.appendNew<LineSegment>(BB2);
232
path.appendNew<LineSegment>(BB3);
233
path.appendNew<LineSegment>(BB4);
236
pathv.push_back(path);
237
hp_vec.push_back(pathv);
244
LPEVonKoch::doBeforeEffect (SPLPEItem *lpeitem)
246
using namespace Geom;
247
original_bbox(lpeitem);
249
std::vector<Geom::Path> paths = ref_path.get_pathvector();
251
if (paths.size()==0||paths.front().size()==0){
252
//FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
253
//refA.param_setValue( Geom::Point(boundingbox_X.min(), boundingbox_Y.middle()) );
254
//refB.param_setValue( Geom::Point(boundingbox_X.max(), boundingbox_Y.middle()) );
255
A = Point(boundingbox_X.min(), boundingbox_Y.middle());
256
B = Point(boundingbox_X.max(), boundingbox_Y.middle());
258
A = paths.front().pointAt(0);
259
B = paths.front().pointAt(paths.front().size());
261
if (paths.size()!=1||paths.front().size()!=1){
262
Geom::Path tmp_path(A);
263
tmp_path.appendNew<LineSegment>(B);
264
std::vector<Geom::Path> tmp_pathv;
265
tmp_pathv.push_back(tmp_path);
266
ref_path.set_new_value(tmp_pathv,true);
272
LPEVonKoch::resetDefaults(SPItem * item)
274
Effect::resetDefaults(item);
276
using namespace Geom;
277
original_bbox(SP_LPE_ITEM(item));
280
A[Geom::X] = boundingbox_X.min();
281
A[Geom::Y] = boundingbox_Y.middle();
282
B[Geom::X] = boundingbox_X.max();
283
B[Geom::Y] = boundingbox_Y.middle();
285
std::vector<Geom::Path> paths,refpaths;
286
Geom::Path path = Geom::Path(A);
287
path.appendNew<Geom::LineSegment>(B);
289
refpaths.push_back(path);
290
ref_path.set_new_value(refpaths, true);
292
paths.push_back(path * Matrix(1./3,0,0,1./3, A[X]*2./3, A[Y]*2./3 + boundingbox_Y.extent()/2));
293
paths.push_back(path * Matrix(1./3,0,0,1./3, B[X]*2./3, B[Y]*2./3 + boundingbox_Y.extent()/2));
294
generator.set_new_value(paths, true);
296
//FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
297
//refA[Geom::X] = boundingbox_X.min();
298
//refA[Geom::Y] = boundingbox_Y.middle();
299
//refB[Geom::X] = boundingbox_X.max();
300
//refB[Geom::Y] = boundingbox_Y.middle();
301
//std::vector<Geom::Path> paths;
302
//Geom::Path path = Geom::Path( (Point) refA);
303
//path.appendNew<Geom::LineSegment>( (Point) refB );
304
//paths.push_back(path * Matrix(1./3,0,0,1./3, refA[X]*2./3, refA[Y]*2./3 + boundingbox_Y.extent()/2));
305
//paths.push_back(path * Matrix(1./3,0,0,1./3, refB[X]*2./3, refB[Y]*2./3 + boundingbox_Y.extent()/2));
306
//paths.push_back(path);
307
//generator.set_new_value(paths, true);
310
} // namespace LivePathEffect
311
} /* namespace Inkscape */
316
c-file-style:"stroustrup"
317
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
322
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :