Code

The declaration of doEffect_path() in lpe-skeleton.h should match the one in live_eff...
[inkscape.git] / src / live_effects / lpe-vonkoch.cpp
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>
11 #include "live_effects/lpe-vonkoch.h"
12 #include "svg/svg.h"
13 #include "ui/widget/scalar.h"
14 #include "nodepath.h"
16 #include <2geom/sbasis.h>
17 #include <2geom/sbasis-geometric.h>
18 #include <2geom/bezier-to-sbasis.h>
19 #include <2geom/sbasis-to-bezier.h>
20 #include <2geom/d2.h>
21 #include <2geom/piecewise.h>
23 #include <algorithm>
24 using std::vector;
27 namespace Inkscape {
28 namespace LivePathEffect {
30 VonKochPathParam::~VonKochPathParam()
31 {  
32 }
34 void
35 VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
36 {  
37     PathParam::param_setup_nodepath(np);
38     sp_nodepath_make_straight_path(np);
39 }
41 static const Util::EnumData<VonKochRefType> VonKochRefTypeData[VKREF_END] = {
42     {VKREF_BBOX,     N_("Bounding box"),               "bbox"},
43     {VKREF_SEG, N_("Last gen. segment"), "lastseg"},
44 };
45 static const Util::EnumDataConverter<VonKochRefType> VonKochRefTypeConverter(VonKochRefTypeData, VKREF_END);
47 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
48     Effect(lpeobject),
49     nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
50     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"),
51     drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
52     reftype(_("Reference"), _("Generating path segments define transforms in reference to bbox or last segment"), "reftype", VonKochRefTypeConverter, &wr, this, VKREF_BBOX),
53     maxComplexity(_("Max complexity"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
54 {
55     registerParameter( dynamic_cast<Parameter *>(&generator) );
56     registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
57     registerParameter( dynamic_cast<Parameter *>(&drawall) );
58     registerParameter( dynamic_cast<Parameter *>(&reftype) );
59     registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
61     nbgenerations.param_make_integer();
62     nbgenerations.param_set_range(0, NR_HUGE);
63     maxComplexity.param_make_integer();
64     maxComplexity.param_set_range(0, NR_HUGE);
65 }
67 LPEVonKoch::~LPEVonKoch()
68 {
70 }
72 std::vector<Geom::Path>
73 LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
74 {
75     using namespace Geom;
76     std::vector<Geom::Path> generating_path = path_from_piecewise(generator.get_pwd2(),.01);//TODO what should that tolerance be?
78     //Collect transform matrices.
79     //FIXME: fusing/cutting nodes mix up component order in the path. This is why the last segment is used.
80     Matrix m0;
81     VonKochRefType type = reftype.get_value();
82     if (type==VKREF_BBOX){
83         Rect bbox = path_in[0].boundsExact();
84         for(unsigned i=1; i < path_in.size(); i++){
85             bbox.unionWith(path_in[i].boundsExact());
86         }
87         m0 = Matrix(bbox[X].extent(),0,0,bbox[X].extent(), bbox.min()[X], (bbox.min()[Y]+bbox.max()[Y])/2);
88     }else{
89         if (generating_path.size()==0) return path_in;
90         Point p = generating_path.back().back().pointAt(0);
91         Point u = generating_path.back().back().pointAt(0.999)-p;
92         m0 = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
93     }
94     m0 = m0.inverse();
96     std::vector<Matrix> transforms;
97     for (unsigned i=0; i<generating_path.size(); i++){
98         unsigned end = generating_path[i].size(); 
99         if(type==VKREF_SEG && i==generating_path.size()-1) end-=1;
100         for (unsigned j=0; j<end; j++){   
101             Point p = generating_path[i].pointAt(j);
102             Point u = generating_path[i].pointAt(j+0.999)-generating_path[i].pointAt(j+0.001);
103             Matrix m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
104             m = m0*m;
105             transforms.push_back(m);
106         }
107     }
108     if (transforms.size()==0) return path_in;
110     //Do nothing if the output is too complex... 
111     int path_in_complexity = 0;
112     for (unsigned k = 0; k < path_in.size(); k++){
113             path_in_complexity+=path_in[k].size();
114     }    
115     double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
116     if (drawall.get_value()){
117         int k = transforms.size();
118         if(k>1){
119             complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
120         }else{
121             complexity = nbgenerations*k*path_in_complexity;
122         }
123     }else{
124         complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
125     }
126     if (complexity > double(maxComplexity)){
127         return path_in;
128     }
130     //Generate path:
131     std::vector<Geom::Path> pathi = path_in;
132     std::vector<Geom::Path> path_out = path_in;
133     
134     for (unsigned i = 0; i<nbgenerations; i++){
135         if (drawall.get_value()){
136             path_out =  path_in;
137             complexity = path_in_complexity;
138         }else{
139             path_out = std::vector<Geom::Path>();
140             complexity = 0;
141         }
142         for (unsigned j = 0; j<transforms.size(); j++){
143             for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
144                 path_out.push_back(pathi[k]*transforms[j]); 
145                 complexity+=pathi[k].size();
146             }
147         }
148         pathi = path_out;
149     }
150     return path_out;
153 void
154 LPEVonKoch::resetDefaults(SPItem * item)
156     if (!SP_IS_PATH(item)) return;
158     using namespace Geom;
160     // set the bend path to run horizontally in the middle of the bounding box of the original path
161     Piecewise<D2<SBasis> > pwd2;
162     std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
163     for (unsigned int i=0; i < temppath.size(); i++) {
164         pwd2.concat( temppath[i].toPwSb() );
165     }
167     D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
168     Interval bndsX = bounds_exact(d2pw[0]);
169     Interval bndsY = bounds_exact(d2pw[1]);
170     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
171     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
173     std::vector<Geom::Path> paths;
174     Geom::Path path;
175     path = Geom::Path();
176     path.start( start );
177     path.appendNew<Geom::LineSegment>( end );
178     paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + bndsY.extent()/2));
179     paths.push_back(path * Matrix(1./3,0,0,1./3,  end[X]*2./3,  end[Y]*2./3 + bndsY.extent()/2));
180     paths.push_back(path);
182     //generator.param_set_and_write_new_value( path.toPwSb() );
183     generator.param_set_and_write_new_value( paths_to_pw(paths) );
185     
188 //     Piecewise<D2<SBasis> > default_gen;
189 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.min(),bndsX.max()),Linear((bndsY.min()+bndsY.max())/2))));
190 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.max(),bndsX.max()+bndsX.extent()/2),Linear((bndsY.min()+bndsY.max())/2))));
191 //     generator.param_set_and_write_new_value( default_gen );
194 void
195 LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
197     // TODO: implement correct transformation instead of this default behavior
198     Effect::transform_multiply(postmul, set);
202 } // namespace LivePathEffect
203 } /* namespace Inkscape */
205 /*
206   Local Variables:
207   mode:c++
208   c-file-style:"stroustrup"
209   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
210   indent-tabs-mode:nil
211   fill-column:99
212   End:
213 */
214 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :