Code

ef9d38ee40c0afa5a5bd33345596c5ef23833773
[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     similar_only(_("Use uniform scale/rotation only"), _("If off, 2segments component of generating path can be used to define a general rtansform. If on, they only affect the orientation preserving/reversing of the transform."), "similar_only", &wr, this, false),
52     drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
53     reftype(_("Reference"), _("Generating path segments define transforms in reference to bbox or last segment"), "reftype", VonKochRefTypeConverter, &wr, this, VKREF_BBOX),
54     maxComplexity(_("Max complexity"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
55 {
56     registerParameter( dynamic_cast<Parameter *>(&generator) );
57     registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
58     registerParameter( dynamic_cast<Parameter *>(&drawall) );
59     registerParameter( dynamic_cast<Parameter *>(&reftype) );
60     registerParameter( dynamic_cast<Parameter *>(&similar_only) );
61     registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
63     nbgenerations.param_make_integer();
64     nbgenerations.param_set_range(0, NR_HUGE);
65     maxComplexity.param_make_integer();
66     maxComplexity.param_set_range(0, NR_HUGE);
67 }
69 LPEVonKoch::~LPEVonKoch()
70 {
72 }
74 std::vector<Geom::Path>
75 LPEVonKoch::doEffect_path (std::vector<Geom::Path> const & path_in)
76 {
77     using namespace Geom;
78     std::vector<Geom::Path> generating_path = path_from_piecewise(generator.get_pwd2(),.01);//TODO what should that tolerance be?
80     //Collect transform matrices.
81     //FIXME: fusing/cutting nodes mix up component order in the path. This is why the last segment is used.
82     Matrix m0;
83     VonKochRefType type = reftype.get_value();
84     if (type==VKREF_BBOX){
85         Rect bbox = Rect(boundingbox_X,boundingbox_Y);
86         m0 = Matrix(boundingbox_X.extent(),0,0,boundingbox_X.extent(), boundingbox_X.min(), boundingbox_Y.middle());
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(1)-p;
92         m0 = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
93     }
94     m0 = m0.inverse();
97     std::vector<Matrix> transforms;
98     unsigned end = generating_path.size();
99     if (type==VKREF_SEG) end-=1;
100     for (unsigned i=0; i<generating_path.size(); i++){
101         Matrix m;
102         if(generating_path[i].size()==1){
103             Point p = generating_path[i].pointAt(0);
104             Point u = generating_path[i].pointAt(1)-p;
105             m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
106             m = m0*m;
107             transforms.push_back(m);
108         }else if(generating_path[i].size()>=2){
109             Point p = generating_path[i].pointAt(1);
110             Point u = generating_path[i].pointAt(2)-p;
111             Point v = p-generating_path[i].pointAt(0);
112             if (similar_only){
113                 int sign = (u[X]*v[Y]-u[Y]*v[X]>=0?1:-1);
114                 v[X] = -u[Y]*sign;
115                 v[Y] =  u[X]*sign;
116             }
117             m = Matrix(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
118             m = m0*m;
119             transforms.push_back(m);
120         }
121     }
123     if (transforms.size()==0) return path_in;
125     //Do nothing if the output is too complex...
126     int path_in_complexity = 0;
127     for (unsigned k = 0; k < path_in.size(); k++){
128             path_in_complexity+=path_in[k].size();
129     }
130     double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
131     if (drawall.get_value()){
132         int k = transforms.size();
133         if(k>1){
134             complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
135         }else{
136             complexity = nbgenerations*k*path_in_complexity;
137         }
138     }else{
139         complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
140     }
141     if (complexity > double(maxComplexity)){
142         return path_in;
143     }
145     //Generate path:
146     std::vector<Geom::Path> pathi = path_in;
147     std::vector<Geom::Path> path_out = path_in;
149     for (unsigned i = 0; i<nbgenerations; i++){
150         if (drawall.get_value()){
151             path_out =  path_in;
152             complexity = path_in_complexity;
153         }else{
154             path_out = std::vector<Geom::Path>();
155             complexity = 0;
156         }
157         for (unsigned j = 0; j<transforms.size(); j++){
158             for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
159                 path_out.push_back(pathi[k]*transforms[j]);
160                 complexity+=pathi[k].size();
161             }
162         }
163         pathi = path_out;
164     }
165     return path_out;
168 void
169 LPEVonKoch::doBeforeEffect (SPLPEItem *lpeitem)
171     original_bbox(lpeitem);
175 void
176 LPEVonKoch::resetDefaults(SPItem * item)
178     using namespace Geom;
179     original_bbox(SP_LPE_ITEM(item));
180     Point start(boundingbox_X.min(), boundingbox_Y.middle());
181     Point end(boundingbox_X.max(), boundingbox_Y.middle());
183     std::vector<Geom::Path> paths;
184     Geom::Path path = Geom::Path(start);
185     path.appendNew<Geom::LineSegment>( end );
186     paths.push_back(path * Matrix(1./3,0,0,1./3,start[X]*2./3,start[Y]*2./3 + boundingbox_Y.extent()/2));
187     paths.push_back(path * Matrix(1./3,0,0,1./3,  end[X]*2./3,  end[Y]*2./3 + boundingbox_Y.extent()/2));
188     paths.push_back(path);
190     generator.set_new_value(paths, true);
193 void
194 LPEVonKoch::transform_multiply(Geom::Matrix const& postmul, bool set)
196     // TODO: implement correct transformation instead of this default behavior
197     Effect::transform_multiply(postmul, set);
201 } // namespace LivePathEffect
202 } /* namespace Inkscape */
204 /*
205   Local Variables:
206   mode:c++
207   c-file-style:"stroustrup"
208   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
209   indent-tabs-mode:nil
210   fill-column:99
211   End:
212 */
213 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :