Code

fa05cb5ff00c73bf0898585e0e8ba295c333d7e7
[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 "live_effects/lpe-vonkoch.h"
10 #include "sp-shape.h"
11 #include "sp-item.h"
12 #include "sp-path.h"
13 #include "display/curve.h"
14 #include <libnr/n-art-bpath.h>
15 #include <libnr/nr-matrix-fns.h>
16 #include "live_effects/n-art-bpath-2geom.h"
17 #include "svg/svg.h"
18 #include "ui/widget/scalar.h"
20 #include <2geom/sbasis.h>
21 #include <2geom/sbasis-geometric.h>
22 #include <2geom/bezier-to-sbasis.h>
23 #include <2geom/sbasis-to-bezier.h>
24 #include <2geom/d2.h>
25 #include <2geom/piecewise.h>
27 #include <algorithm>
28 using std::vector;
31 namespace Inkscape {
32 namespace LivePathEffect {
34 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
35     Effect(lpeobject),
36     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"),
37     nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
38     drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, false),
39     vertical_pattern(_("Original path is vertical"), _("Rotates the original 90 degrees, before generating the fractal"), "vertical", &wr, this, false)
40 {
41     registerParameter( dynamic_cast<Parameter *>(&generator) );
42     registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
43     registerParameter( dynamic_cast<Parameter *>(&drawall) );
44     registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
46     nbgenerations.param_make_integer();
47     nbgenerations.param_set_range(0, NR_HUGE);
48 }
50 LPEVonKoch::~LPEVonKoch()
51 {
53 }
56 std::vector<Geom::Path>
57 LPEVonKoch::doEffect_path (std::vector<Geom::Path> & path_in)
58 {
59     using namespace Geom;
60     std::vector<Geom::Path> generating_path = path_from_piecewise(generator,.01);//TODO what should that tolerance be?
61     Point p = generating_path[0].pointAt(0.001);
62     Point u = generating_path[0].pointAt(0.999)-generating_path[0].pointAt(0.001);
63     Matrix m0 = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
64     m0 = m0.inverse();
66     std::vector<Matrix> transforms;
67     for (unsigned i=0; i<generating_path.size(); i++){
68         for (unsigned j = (i==0)?1:0; j<generating_path[i].size(); j++){   
69             p = generating_path[i].pointAt(j);
70             u = generating_path[i].pointAt(j+0.999)-generating_path[i].pointAt(j+0.001);
71             Matrix m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
72             m = m0*m;
73             transforms.push_back(m);
74         }
75     }
76 /*
77     assert(generating_path.size()>0);
79     std::vector<Matrix> transforms;
80     transforms.push_back(Matrix(.5, 0., 0., .5,   0.,50.));
81     transforms.push_back(Matrix(.5, 0., 0., .5, 100., 0.));
82 */
83     
84     std::vector<Geom::Path> pathi = path_in;
85     std::vector<Geom::Path> path_out = path_in;
86    
87     for (unsigned i = 0; i<nbgenerations; i++){
88         path_out = (drawall.get_value())? path_in : std::vector<Geom::Path>();
89         for (unsigned j = 0; j<transforms.size(); j++){
90             for (unsigned k = 0; k<pathi.size(); k++){
91                 path_out.push_back(pathi[k]*transforms[j]); 
92             }
93         }
94         pathi = path_out;
95     }
96     return path_out;
97 }
99 void
100 LPEVonKoch::resetDefaults(SPItem * item)
102     if (!SP_IS_PATH(item)) return;
104     using namespace Geom;
106     // set the bend path to run horizontally in the middle of the bounding box of the original path
107     Piecewise<D2<SBasis> > pwd2;
108     std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
109     for (unsigned int i=0; i < temppath.size(); i++) {
110         pwd2.concat( temppath[i].toPwSb() );
111     }
113     D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
114     Interval bndsX = bounds_exact(d2pw[0]);
115     Interval bndsY = bounds_exact(d2pw[1]);
116     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
117     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
119     std::vector<Geom::Path> paths;
120     Geom::Path path;
121     path = Geom::Path();
122     path.start( start );
123     path.appendNew<Geom::LineSegment>( end );
124     paths.push_back(path);
125     paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + bndsY.extent()/2));
126     paths.push_back(path * Matrix(1./3,0,0,1./3,  end[X]*2./3,  end[Y]*2./3 + bndsY.extent()/2));
128     //generator.param_set_and_write_new_value( path.toPwSb() );
129     generator.param_set_and_write_new_value( paths_to_pw(paths) );
132 //     Piecewise<D2<SBasis> > default_gen;
133 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.min(),bndsX.max()),Linear((bndsY.min()+bndsY.max())/2))));
134 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.max(),bndsX.max()+bndsX.extent()/2),Linear((bndsY.min()+bndsY.max())/2))));
135 //     generator.param_set_and_write_new_value( default_gen );
138 void
139 LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
141     // TODO: implement correct transformation instead of this default behavior
142     Effect::transform_multiply(postmul, set);
146 } // namespace LivePathEffect
147 } /* namespace Inkscape */
149 /*
150   Local Variables:
151   mode:c++
152   c-file-style:"stroustrup"
153   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
154   indent-tabs-mode:nil
155   fill-column:99
156   End:
157 */
158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :