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
|
#define INKSCAPE_LPE_VONKOCH_CPP
/*
* Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <cstdio>
#include "live_effects/lpe-vonkoch.h"
#include "svg/svg.h"
#include "ui/widget/scalar.h"
#include "nodepath.h"
#include <2geom/sbasis.h>
#include <2geom/sbasis-geometric.h>
#include <2geom/bezier-to-sbasis.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/d2.h>
#include <2geom/piecewise.h>
#include <algorithm>
using std::vector;
namespace Inkscape {
namespace LivePathEffect {
VonKochPathParam::~VonKochPathParam()
{
}
void
VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
{
PathParam::param_setup_nodepath(np);
sp_nodepath_make_straight_path(np);
}
static const Util::EnumData<VonKochRefType> VonKochRefTypeData[VKREF_END] = {
{VKREF_BBOX, N_("Bounding box"), "bbox"},
{VKREF_SEG, N_("Last gen. segment"), "lastseg"},
};
static const Util::EnumDataConverter<VonKochRefType> VonKochRefTypeConverter(VonKochRefTypeData, VKREF_END);
LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
Effect(lpeobject),
nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
generator(_("Generating path"), _("Path whos segments define the fractal"), "generator", &wr, this, "M0,0 L3,0 M0,1 L1,1 M 2,1 L3,1"),
drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
reftype(_("Reference"), _("Generating path segments define transforms in reference to bbox or last segment"), "reftype", VonKochRefTypeConverter, &wr, this, VKREF_BBOX),
maxComplexity(_("Max complexity"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
{
registerParameter( dynamic_cast<Parameter *>(&generator) );
registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
registerParameter( dynamic_cast<Parameter *>(&drawall) );
registerParameter( dynamic_cast<Parameter *>(&reftype) );
registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
nbgenerations.param_make_integer();
nbgenerations.param_set_range(0, NR_HUGE);
maxComplexity.param_make_integer();
maxComplexity.param_set_range(0, NR_HUGE);
}
LPEVonKoch::~LPEVonKoch()
{
}
std::vector<Geom::Path>
LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
{
using namespace Geom;
std::vector<Geom::Path> generating_path = path_from_piecewise(generator.get_pwd2(),.01);//TODO what should that tolerance be?
//Collect transform matrices.
//FIXME: fusing/cutting nodes mix up component order in the path. This is why the last segment is used.
Matrix m0;
VonKochRefType type = reftype.get_value();
if (type==VKREF_BBOX){
Rect bbox = path_in[0].boundsExact();
for(unsigned i=1; i < path_in.size(); i++){
bbox.unionWith(path_in[i].boundsExact());
}
m0 = Matrix(bbox[X].extent(),0,0,bbox[X].extent(), bbox.min()[X], (bbox.min()[Y]+bbox.max()[Y])/2);
}else{
if (generating_path.size()==0) return path_in;
Point p = generating_path.back().back().pointAt(0);
Point u = generating_path.back().back().pointAt(0.999)-p;
m0 = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
}
m0 = m0.inverse();
std::vector<Matrix> transforms;
for (unsigned i=0; i<generating_path.size(); i++){
unsigned end = generating_path[i].size();
if(type==VKREF_SEG && i==generating_path.size()-1) end-=1;
for (unsigned j=0; j<end; j++){
Point p = generating_path[i].pointAt(j);
Point u = generating_path[i].pointAt(j+0.999)-generating_path[i].pointAt(j+0.001);
Matrix m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
m = m0*m;
transforms.push_back(m);
}
}
if (transforms.size()==0) return path_in;
//Do nothing if the output is too complex...
int path_in_complexity = 0;
for (unsigned k = 0; k < path_in.size(); k++){
path_in_complexity+=path_in[k].size();
}
double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
if (drawall.get_value()){
int k = transforms.size();
if(k>1){
complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
}else{
complexity = nbgenerations*k*path_in_complexity;
}
}else{
complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
}
if (complexity > double(maxComplexity)){
return path_in;
}
//Generate path:
std::vector<Geom::Path> pathi = path_in;
std::vector<Geom::Path> 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 = std::vector<Geom::Path>();
complexity = 0;
}
for (unsigned j = 0; j<transforms.size(); j++){
for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
path_out.push_back(pathi[k]*transforms[j]);
complexity+=pathi[k].size();
}
}
pathi = path_out;
}
return path_out;
}
void
LPEVonKoch::resetDefaults(SPItem * item)
{
if (!SP_IS_PATH(item)) return;
using namespace Geom;
// set the bend path to run horizontally in the middle of the bounding box of the original path
Piecewise<D2<SBasis> > pwd2;
std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
for (unsigned int i=0; i < temppath.size(); i++) {
pwd2.concat( temppath[i].toPwSb() );
}
D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
Interval bndsX = bounds_exact(d2pw[0]);
Interval bndsY = bounds_exact(d2pw[1]);
Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
std::vector<Geom::Path> paths;
Geom::Path path;
path = Geom::Path();
path.start( start );
path.appendNew<Geom::LineSegment>( end );
paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + bndsY.extent()/2));
paths.push_back(path * Matrix(1./3,0,0,1./3, end[X]*2./3, end[Y]*2./3 + bndsY.extent()/2));
paths.push_back(path);
//generator.param_set_and_write_new_value( path.toPwSb() );
generator.param_set_and_write_new_value( paths_to_pw(paths) );
// Piecewise<D2<SBasis> > default_gen;
// default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.min(),bndsX.max()),Linear((bndsY.min()+bndsY.max())/2))));
// default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.max(),bndsX.max()+bndsX.extent()/2),Linear((bndsY.min()+bndsY.max())/2))));
// generator.param_set_and_write_new_value( default_gen );
}
void
LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
{
// TODO: implement correct transformation instead of this default behavior
Effect::transform_multiply(postmul, set);
}
} // 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 :
|