Code

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