Code

changed vonkoch to use bbox of input object as reference.
[inkscape.git] / src / live_effects / lpe-vonkoch.cpp
1 #define INKSCAPE_LPE_VONKOCH_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include <cstdio>
11 #include "live_effects/lpe-vonkoch.h"
12 #include "sp-shape.h"
13 #include "sp-item.h"
14 #include "sp-path.h"
15 #include "display/curve.h"
16 #include <libnr/n-art-bpath.h>
17 #include <libnr/nr-matrix-fns.h>
18 #include "live_effects/n-art-bpath-2geom.h"
19 #include "svg/svg.h"
20 #include "ui/widget/scalar.h"
22 #include <2geom/sbasis.h>
23 #include <2geom/sbasis-geometric.h>
24 #include <2geom/bezier-to-sbasis.h>
25 #include <2geom/sbasis-to-bezier.h>
26 #include <2geom/d2.h>
27 #include <2geom/piecewise.h>
29 #include <algorithm>
30 using std::vector;
33 namespace Inkscape {
34 namespace LivePathEffect {
36 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
37     Effect(lpeobject),
38     nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
39     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"),
40     drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, false),
41     vertical_pattern(_("Original path is vertical"), _("Rotates the original 90 degrees, before generating the fractal"), "vertical", &wr, this, false)
42 {
43     registerParameter( dynamic_cast<Parameter *>(&generator) );
44     registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
45     registerParameter( dynamic_cast<Parameter *>(&drawall) );
46     registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
48     nbgenerations.param_make_integer();
49     nbgenerations.param_set_range(0, NR_HUGE);
50 }
52 LPEVonKoch::~LPEVonKoch()
53 {
55 }
58 std::vector<Geom::Path>
59 LPEVonKoch::doEffect_path (std::vector<Geom::Path> & path_in)
60 {
61     using namespace Geom;
62     std::vector<Geom::Path> generating_path = path_from_piecewise(generator,.01);//TODO what should that tolerance be?
63     Rect bbox = path_in[0].boundsExact();
64     for(unsigned i=1; i < path_in.size(); i++){
65         bbox.unionWith(path_in[i].boundsExact());
66     }
67     Matrix m0 = Matrix(bbox[X].extent(),0,0,bbox[X].extent(), bbox.min()[X], bbox.min()[Y]);
68     m0 = m0.inverse();
70     std::vector<Matrix> transforms;
71     for (unsigned i=0; i<generating_path.size(); i++){
72         for (unsigned j = 0; j<generating_path[i].size(); j++){   
73             Point p = generating_path[i].pointAt(j);
74             Point u = generating_path[i].pointAt(j+0.999)-generating_path[i].pointAt(j+0.001);
75             Matrix m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
76             m = m0*m;
77             transforms.push_back(m);
78         }
79     }
80 /*
81     assert(generating_path.size()>0);
83     std::vector<Matrix> transforms;
84     transforms.push_back(Matrix(.5, 0., 0., .5,   0.,50.));
85     transforms.push_back(Matrix(.5, 0., 0., .5, 100., 0.));
86 */
87     
88     std::vector<Geom::Path> pathi = path_in;
89     std::vector<Geom::Path> path_out = path_in;
90    
91     for (unsigned i = 0; i<nbgenerations; i++){
92         path_out = (drawall.get_value())? path_in : std::vector<Geom::Path>();
93         for (unsigned j = 0; j<transforms.size(); j++){
94             for (unsigned k = 0; k<pathi.size(); k++){
95                 path_out.push_back(pathi[k]*transforms[j]); 
96             }
97         }
98         pathi = path_out;
99     }
100     return path_out;
103 void
104 LPEVonKoch::resetDefaults(SPItem * item)
106     if (!SP_IS_PATH(item)) return;
108     using namespace Geom;
110     // set the bend path to run horizontally in the middle of the bounding box of the original path
111     Piecewise<D2<SBasis> > pwd2;
112     std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
113     for (unsigned int i=0; i < temppath.size(); i++) {
114         pwd2.concat( temppath[i].toPwSb() );
115     }
117     D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
118     Interval bndsX = bounds_exact(d2pw[0]);
119     Interval bndsY = bounds_exact(d2pw[1]);
120     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
121     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
123     std::vector<Geom::Path> paths;
124     Geom::Path path;
125     path = Geom::Path();
126     path.start( start );
127     path.appendNew<Geom::LineSegment>( end );
128     paths.push_back(path);
129     paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + bndsY.extent()/2));
130     paths.push_back(path * Matrix(1./3,0,0,1./3,  end[X]*2./3,  end[Y]*2./3 + bndsY.extent()/2));
132     //generator.param_set_and_write_new_value( path.toPwSb() );
133     generator.param_set_and_write_new_value( paths_to_pw(paths) );
135     
138 //     Piecewise<D2<SBasis> > default_gen;
139 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.min(),bndsX.max()),Linear((bndsY.min()+bndsY.max())/2))));
140 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.max(),bndsX.max()+bndsX.extent()/2),Linear((bndsY.min()+bndsY.max())/2))));
141 //     generator.param_set_and_write_new_value( default_gen );
144 void
145 LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
147     // TODO: implement correct transformation instead of this default behavior
148     Effect::transform_multiply(postmul, set);
152 } // namespace LivePathEffect
153 } /* namespace Inkscape */
155 /*
156   Local Variables:
157   mode:c++
158   c-file-style:"stroustrup"
159   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
160   indent-tabs-mode:nil
161   fill-column:99
162   End:
163 */
164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :