1 #define INKSCAPE_LPE_VONKOCH_CPP
3 /*
4 * Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
5 *
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #include <cstdio>
10 #include "live_effects/lpe-vonkoch.h"
11 #include "nodepath.h"
12 #include <2geom/transforms.h>
14 //using std::vector;
15 namespace Inkscape {
16 namespace LivePathEffect {
18 void
19 VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
20 {
21 PathParam::param_setup_nodepath(np);
22 sp_nodepath_make_straight_path(np);
23 }
25 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
26 void
27 VonKochRefPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
28 {
29 PathParam::param_setup_nodepath(np);
30 sp_nodepath_make_straight_path(np);
31 }
32 bool
33 VonKochRefPathParam::param_readSVGValue(const gchar * strvalue)
34 {
35 std::vector<Geom::Path> old = _pathvector;
36 bool res = PathParam::param_readSVGValue(strvalue);
37 if (res && _pathvector.size()==1 && _pathvector.front().size()==1){
38 return true;
39 }else{
40 _pathvector = old;
41 return false;
42 }
43 }
45 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
46 Effect(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)
57 {
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);
73 }
75 LPEVonKoch::~LPEVonKoch()
76 {
78 }
80 std::vector<Geom::Path>
81 LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
82 {
83 using namespace Geom;
85 std::vector<Geom::Path> generating_path = generator.get_pathvector();
87 if (generating_path.size()==0) {
88 return path_in;
89 }
91 //Collect transform matrices.
92 Matrix m0;
93 Geom::Path refpath = ref_path.get_pathvector().front();
94 Point A = refpath.pointAt(0);
95 Point B = refpath.pointAt(refpath.size());
96 Point u = B-A;
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]);
102 m0 = m0.inverse();
104 std::vector<Matrix> transforms;
105 for (unsigned i=0; i<generating_path.size(); i++){
106 Matrix m;
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]);
111 m = m0*m;
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);
119 v[X] = -u[Y]*sign;
120 v[Y] = u[X]*sign;
121 }
122 m = Matrix(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
123 m = m0*m;
124 transforms.push_back(m);
125 }
126 }
128 if (transforms.size()==0){
129 return path_in;
130 }
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();
136 }
137 double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
138 if (drawall.get_value()){
139 int k = transforms.size();
140 if(k>1){
141 complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
142 }else{
143 complexity = nbgenerations*k*path_in_complexity;
144 }
145 }else{
146 complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
147 }
148 if (complexity > double(maxComplexity)){
149 g_warning("VonKoch lpe's output too complex. Effect bypassed.");
150 return path_in;
151 }
153 //Generate path:
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()){
159 path_out = path_in;
160 complexity = path_in_complexity;
161 }else{
162 path_out = std::vector<Geom::Path>();
163 complexity = 0;
164 }
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();
169 }
170 }
171 pathi = path_out;
172 }
173 return path_out;
174 }
177 //Usefull??
178 //void
179 //LPEVonKoch::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
180 /*{
181 using namespace Geom;
182 if (draw_boxes.get_value()){
183 double ratio = .5;
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;
195 BB1 -= v;
196 BB2 -= v;
197 BB3 += v;
198 BB4 += v;
199 Geom::Path refbox(BB1);
200 refbox.appendNew<LineSegment>(BB2);
201 refbox.appendNew<LineSegment>(BB3);
202 refbox.appendNew<LineSegment>(BB4);
203 refbox.close();
204 PathVector refbox_as_vect;
205 refbox_as_vect.push_back(refbox);
206 hp_vec.push_back(refbox_as_vect);
207 }
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;
222 }else{
223 v = (generating_path[i].pointAt(0) - BB1)*ratio;
224 }
225 }
226 BB1 -= v;
227 BB2 -= v;
228 BB3 += v;
229 BB4 += v;
230 Geom::Path path(BB1);
231 path.appendNew<LineSegment>(BB2);
232 path.appendNew<LineSegment>(BB3);
233 path.appendNew<LineSegment>(BB4);
234 path.close();
235 PathVector pathv;
236 pathv.push_back(path);
237 hp_vec.push_back(pathv);
238 }
239 }
240 }
241 */
243 void
244 LPEVonKoch::doBeforeEffect (SPLPEItem *lpeitem)
245 {
246 using namespace Geom;
247 original_bbox(lpeitem);
249 std::vector<Geom::Path> paths = ref_path.get_pathvector();
250 Geom::Point A,B;
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());
257 }else{
258 A = paths.front().pointAt(0);
259 B = paths.front().pointAt(paths.front().size());
260 }
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);
267 }
268 }
271 void
272 LPEVonKoch::resetDefaults(SPItem * item)
273 {
274 Effect::resetDefaults(item);
276 using namespace Geom;
277 original_bbox(SP_LPE_ITEM(item));
279 Point A,B;
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);
308 }
310 } // namespace LivePathEffect
311 } /* namespace Inkscape */
313 /*
314 Local Variables:
315 mode:c++
316 c-file-style:"stroustrup"
317 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
318 indent-tabs-mode:nil
319 fill-column:99
320 End:
321 */
322 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :