Code

change doEffect functions to use const& parameters
[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 "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 VonKochPathParam::~VonKochPathParam()
37 {  
38 }
40 void
41 VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
42 {  
43     PathParam::param_setup_nodepath(np);
44     np->straight_path = true;
45 }
47 static const Util::EnumData<VonKochRefType> VonKochRefTypeData[VKREF_END] = {
48     {VKREF_BBOX,     N_("Bounding box"),               "bbox"},
49     {VKREF_SEG, N_("Last gen. segment"), "lastseg"},
50 };
51 static const Util::EnumDataConverter<VonKochRefType> VonKochRefTypeConverter(VonKochRefTypeData, VKREF_END);
53 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
54     Effect(lpeobject),
55     nbgenerations(_("Nb of generations"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
56     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"),
57     drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
58     reftype(_("Reference"), _("Generating path segments define transforms in reference to bbox or last segment"), "reftype", VonKochRefTypeConverter, &wr, this, VKREF_BBOX),
59     maxComplexity(_("Max complexity"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
60 {
61     registerParameter( dynamic_cast<Parameter *>(&generator) );
62     registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
63     registerParameter( dynamic_cast<Parameter *>(&drawall) );
64     registerParameter( dynamic_cast<Parameter *>(&reftype) );
65     registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
67     nbgenerations.param_make_integer();
68     nbgenerations.param_set_range(0, NR_HUGE);
69     maxComplexity.param_make_integer();
70     maxComplexity.param_set_range(0, NR_HUGE);
71 }
73 LPEVonKoch::~LPEVonKoch()
74 {
76 }
78 std::vector<Geom::Path>
79 LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
80 {
81     using namespace Geom;
82     std::vector<Geom::Path> generating_path = path_from_piecewise(generator.get_pwd2(),.01);//TODO what should that tolerance be?
84     //Collect transform matrices.
85     //FIXME: fusing/cutting nodes mix up component order in the path. This is why the last segment is used.
86     Matrix m0;
87     VonKochRefType type = reftype.get_value();
88     if (type==VKREF_BBOX){
89         Rect bbox = path_in[0].boundsExact();
90         for(unsigned i=1; i < path_in.size(); i++){
91             bbox.unionWith(path_in[i].boundsExact());
92         }
93         m0 = Matrix(bbox[X].extent(),0,0,bbox[X].extent(), bbox.min()[X], (bbox.min()[Y]+bbox.max()[Y])/2);
94     }else{
95         if (generating_path.size()==0) return path_in;
96         Point p = generating_path.back().back().pointAt(0);
97         Point u = generating_path.back().back().pointAt(0.999)-p;
98         m0 = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
99     }
100     m0 = m0.inverse();
102     std::vector<Matrix> transforms;
103     for (unsigned i=0; i<generating_path.size(); i++){
104         unsigned end = generating_path[i].size(); 
105         if(type==VKREF_SEG && i==generating_path.size()-1) end-=1;
106         for (unsigned j=0; j<end; j++){   
107             Point p = generating_path[i].pointAt(j);
108             Point u = generating_path[i].pointAt(j+0.999)-generating_path[i].pointAt(j+0.001);
109             Matrix m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
110             m = m0*m;
111             transforms.push_back(m);
112         }
113     }
114     if (transforms.size()==0) return path_in;
116     //Do nothing if the output is too complex... 
117     int path_in_complexity = 0;
118     for (unsigned k = 0; k < path_in.size(); k++){
119             path_in_complexity+=path_in[k].size();
120     }    
121     double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
122     if (drawall.get_value()){
123         int k = transforms.size();
124         if(k>1){
125             complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
126         }else{
127             complexity = nbgenerations*k*path_in_complexity;
128         }
129     }else{
130         complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
131     }
132     if (complexity > double(maxComplexity)){
133         return path_in;
134     }
136     //Generate path:
137     std::vector<Geom::Path> pathi = path_in;
138     std::vector<Geom::Path> path_out = path_in;
139     
140     for (unsigned i = 0; i<nbgenerations; i++){
141         if (drawall.get_value()){
142             path_out =  path_in;
143             complexity = path_in_complexity;
144         }else{
145             path_out = std::vector<Geom::Path>();
146             complexity = 0;
147         }
148         for (unsigned j = 0; j<transforms.size(); j++){
149             for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
150                 path_out.push_back(pathi[k]*transforms[j]); 
151                 complexity+=pathi[k].size();
152             }
153         }
154         pathi = path_out;
155     }
156     return path_out;
159 void
160 LPEVonKoch::resetDefaults(SPItem * item)
162     if (!SP_IS_PATH(item)) return;
164     using namespace Geom;
166     // set the bend path to run horizontally in the middle of the bounding box of the original path
167     Piecewise<D2<SBasis> > pwd2;
168     std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
169     for (unsigned int i=0; i < temppath.size(); i++) {
170         pwd2.concat( temppath[i].toPwSb() );
171     }
173     D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
174     Interval bndsX = bounds_exact(d2pw[0]);
175     Interval bndsY = bounds_exact(d2pw[1]);
176     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
177     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
179     std::vector<Geom::Path> paths;
180     Geom::Path path;
181     path = Geom::Path();
182     path.start( start );
183     path.appendNew<Geom::LineSegment>( end );
184     paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + bndsY.extent()/2));
185     paths.push_back(path * Matrix(1./3,0,0,1./3,  end[X]*2./3,  end[Y]*2./3 + bndsY.extent()/2));
186     paths.push_back(path);
188     //generator.param_set_and_write_new_value( path.toPwSb() );
189     generator.param_set_and_write_new_value( paths_to_pw(paths) );
191     
194 //     Piecewise<D2<SBasis> > default_gen;
195 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.min(),bndsX.max()),Linear((bndsY.min()+bndsY.max())/2))));
196 //     default_gen.concat(Piecewise<D2<SBasis> >(D2<SBasis>(Linear(bndsX.max(),bndsX.max()+bndsX.extent()/2),Linear((bndsY.min()+bndsY.max())/2))));
197 //     generator.param_set_and_write_new_value( default_gen );
200 void
201 LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
203     // TODO: implement correct transformation instead of this default behavior
204     Effect::transform_multiply(postmul, set);
208 } // namespace LivePathEffect
209 } /* namespace Inkscape */
211 /*
212   Local Variables:
213   mode:c++
214   c-file-style:"stroustrup"
215   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
216   indent-tabs-mode:nil
217   fill-column:99
218   End:
219 */
220 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :