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>
10 #include "live_effects/lpe-vonkoch.h"
11 #include <2geom/transforms.h>
13 //using std::vector;
14 namespace Inkscape {
15 namespace LivePathEffect {
17 void
18 VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
19 {
20 PathParam::param_setup_nodepath(np);
21 //sp_nodepath_make_straight_path(np);
22 }
24 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
25 void
26 VonKochRefPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
27 {
28 PathParam::param_setup_nodepath(np);
29 //sp_nodepath_make_straight_path(np);
30 }
31 bool
32 VonKochRefPathParam::param_readSVGValue(const gchar * strvalue)
33 {
34 Geom::PathVector old = _pathvector;
35 bool res = PathParam::param_readSVGValue(strvalue);
36 if (res && _pathvector.size()==1 && _pathvector.front().size()==1){
37 return true;
38 }else{
39 _pathvector = old;
40 return false;
41 }
42 }
44 LPEVonKoch::LPEVonKoch(LivePathEffectObject *lpeobject) :
45 Effect(lpeobject),
46 nbgenerations(_("Nb of generations:"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
47 generator(_("Generating path:"), _("Path whose segments define the iterated transforms"), "generator", &wr, this, "M0,0 L30,0 M0,10 L10,10 M 20,10 L30,10"),
48 similar_only(_("Use uniform transforms only"), _("2 consecutive segments are used to reverse/preserve orientation only (otherwise, they define a general transform)."), "similar_only", &wr, this, false),
49 drawall(_("Draw all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
50 //,draw_boxes(_("Display boxes"), _("Display boxes instead of paths only"), "draw_boxes", &wr, this, true)
51 ref_path(_("Reference segment:"), _("The reference segment. Defaults to the horizontal midline of the bbox."), "ref_path", &wr, this, "M0,0 L10,0"),
52 //refA(_("Ref Start"), _("Left side middle of the reference box"), "refA", &wr, this),
53 //refB(_("Ref End"), _("Right side middle of the reference box"), "refB", &wr, this),
54 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
55 maxComplexity(_("Max complexity:"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
56 {
57 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
58 registerParameter( dynamic_cast<Parameter *>(&ref_path) );
59 //registerParameter( dynamic_cast<Parameter *>(&refA) );
60 //registerParameter( dynamic_cast<Parameter *>(&refB) );
61 registerParameter( dynamic_cast<Parameter *>(&generator) );
62 registerParameter( dynamic_cast<Parameter *>(&similar_only) );
63 registerParameter( dynamic_cast<Parameter *>(&nbgenerations) );
64 registerParameter( dynamic_cast<Parameter *>(&drawall) );
65 registerParameter( dynamic_cast<Parameter *>(&maxComplexity) );
66 //registerParameter( dynamic_cast<Parameter *>(&draw_boxes) );
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;
84 std::vector<Geom::Path> generating_path = generator.get_pathvector();
86 if (generating_path.size()==0) {
87 return path_in;
88 }
90 //Collect transform matrices.
91 Matrix m0;
92 Geom::Path refpath = ref_path.get_pathvector().front();
93 Point A = refpath.pointAt(0);
94 Point B = refpath.pointAt(refpath.size());
95 Point u = B-A;
96 m0 = Matrix(u[X], u[Y],-u[Y], u[X], A[X], A[Y]);
98 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
99 //Point u = refB-refA;
100 //m0 = Matrix(u[X], u[Y],-u[Y], u[X], refA[X], refA[Y]);
101 m0 = m0.inverse();
103 std::vector<Matrix> transforms;
104 for (unsigned i=0; i<generating_path.size(); i++){
105 Matrix m;
106 if(generating_path[i].size()==1){
107 Point p = generating_path[i].pointAt(0);
108 Point u = generating_path[i].pointAt(1)-p;
109 m = Matrix(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
110 m = m0*m;
111 transforms.push_back(m);
112 }else if(generating_path[i].size()>=2){
113 Point p = generating_path[i].pointAt(1);
114 Point u = generating_path[i].pointAt(2)-p;
115 Point v = p-generating_path[i].pointAt(0);
116 if (similar_only.get_value()){
117 int sign = (u[X]*v[Y]-u[Y]*v[X]>=0?1:-1);
118 v[X] = -u[Y]*sign;
119 v[Y] = u[X]*sign;
120 }
121 m = Matrix(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
122 m = m0*m;
123 transforms.push_back(m);
124 }
125 }
127 if (transforms.size()==0){
128 return path_in;
129 }
131 //Do nothing if the output is too complex...
132 int path_in_complexity = 0;
133 for (unsigned k = 0; k < path_in.size(); k++){
134 path_in_complexity+=path_in[k].size();
135 }
136 double complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
137 if (drawall.get_value()){
138 int k = transforms.size();
139 if(k>1){
140 complexity = (pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
141 }else{
142 complexity = nbgenerations*k*path_in_complexity;
143 }
144 }else{
145 complexity = pow(transforms.size(),nbgenerations)*path_in_complexity;
146 }
147 if (complexity > double(maxComplexity)){
148 g_warning("VonKoch lpe's output too complex. Effect bypassed.");
149 return path_in;
150 }
152 //Generate path:
153 std::vector<Geom::Path> pathi = path_in;
154 std::vector<Geom::Path> path_out = path_in;
156 for (unsigned i = 0; i<nbgenerations; i++){
157 if (drawall.get_value()){
158 path_out = path_in;
159 complexity = path_in_complexity;
160 }else{
161 path_out = std::vector<Geom::Path>();
162 complexity = 0;
163 }
164 for (unsigned j = 0; j<transforms.size(); j++){
165 for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
166 path_out.push_back(pathi[k]*transforms[j]);
167 complexity+=pathi[k].size();
168 }
169 }
170 pathi = path_out;
171 }
172 return path_out;
173 }
176 //Usefull??
177 //void
178 //LPEVonKoch::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
179 /*{
180 using namespace Geom;
181 if (draw_boxes.get_value()){
182 double ratio = .5;
183 if (similar_only.get_value()) ratio = boundingbox_Y.extent()/boundingbox_X.extent()/2;
185 Point BB1,BB2,BB3,BB4,v;
187 //Draw the reference box (ref_path is supposed to consist in one line segment)
188 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
189 Geom::Path refpath = ref_path.get_pathvector().front();
190 if (refpath.size()==1){
191 BB1 = BB4 = refpath.front().pointAt(0);
192 BB2 = BB3 = refpath.front().pointAt(1);
193 v = rot90(BB2 - BB1)*ratio;
194 BB1 -= v;
195 BB2 -= v;
196 BB3 += v;
197 BB4 += v;
198 Geom::Path refbox(BB1);
199 refbox.appendNew<LineSegment>(BB2);
200 refbox.appendNew<LineSegment>(BB3);
201 refbox.appendNew<LineSegment>(BB4);
202 refbox.close();
203 PathVector refbox_as_vect;
204 refbox_as_vect.push_back(refbox);
205 hp_vec.push_back(refbox_as_vect);
206 }
207 //Draw the transformed boxes
208 std::vector<Geom::Path> generating_path = generator.get_pathvector();
209 for (unsigned i=0;i<generating_path.size(); i++){
210 if (generating_path[i].size()==0){
211 //Ooops! this should not happen.
212 }else if (generating_path[i].size()==1){
213 BB1 = BB4 = generating_path[i].pointAt(0);
214 BB2 = BB3 = generating_path[i].pointAt(1);
215 v = rot90(BB2 - BB1)*ratio;
216 }else{//Only tak the first 2 segments into account.
217 BB1 = BB4 = generating_path[i].pointAt(1);
218 BB2 = BB3 = generating_path[i].pointAt(2);
219 if(similar_only.get_value()){
220 v = rot90(BB2 - BB1)*ratio;
221 }else{
222 v = (generating_path[i].pointAt(0) - BB1)*ratio;
223 }
224 }
225 BB1 -= v;
226 BB2 -= v;
227 BB3 += v;
228 BB4 += v;
229 Geom::Path path(BB1);
230 path.appendNew<LineSegment>(BB2);
231 path.appendNew<LineSegment>(BB3);
232 path.appendNew<LineSegment>(BB4);
233 path.close();
234 PathVector pathv;
235 pathv.push_back(path);
236 hp_vec.push_back(pathv);
237 }
238 }
239 }
240 */
242 void
243 LPEVonKoch::doBeforeEffect (SPLPEItem *lpeitem)
244 {
245 using namespace Geom;
246 original_bbox(lpeitem);
248 std::vector<Geom::Path> paths = ref_path.get_pathvector();
249 Geom::Point A,B;
250 if (paths.size()==0||paths.front().size()==0){
251 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
252 //refA.param_setValue( Geom::Point(boundingbox_X.min(), boundingbox_Y.middle()) );
253 //refB.param_setValue( Geom::Point(boundingbox_X.max(), boundingbox_Y.middle()) );
254 A = Point(boundingbox_X.min(), boundingbox_Y.middle());
255 B = Point(boundingbox_X.max(), boundingbox_Y.middle());
256 }else{
257 A = paths.front().pointAt(0);
258 B = paths.front().pointAt(paths.front().size());
259 }
260 if (paths.size()!=1||paths.front().size()!=1){
261 Geom::Path tmp_path(A);
262 tmp_path.appendNew<LineSegment>(B);
263 std::vector<Geom::Path> tmp_pathv;
264 tmp_pathv.push_back(tmp_path);
265 ref_path.set_new_value(tmp_pathv,true);
266 }
267 }
270 void
271 LPEVonKoch::resetDefaults(SPItem * item)
272 {
273 Effect::resetDefaults(item);
275 using namespace Geom;
276 original_bbox(SP_LPE_ITEM(item));
278 Point A,B;
279 A[Geom::X] = boundingbox_X.min();
280 A[Geom::Y] = boundingbox_Y.middle();
281 B[Geom::X] = boundingbox_X.max();
282 B[Geom::Y] = boundingbox_Y.middle();
284 std::vector<Geom::Path> paths,refpaths;
285 Geom::Path path = Geom::Path(A);
286 path.appendNew<Geom::LineSegment>(B);
288 refpaths.push_back(path);
289 ref_path.set_new_value(refpaths, true);
291 paths.push_back(path * Matrix(1./3,0,0,1./3, A[X]*2./3, A[Y]*2./3 + boundingbox_Y.extent()/2));
292 paths.push_back(path * Matrix(1./3,0,0,1./3, B[X]*2./3, B[Y]*2./3 + boundingbox_Y.extent()/2));
293 generator.set_new_value(paths, true);
295 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
296 //refA[Geom::X] = boundingbox_X.min();
297 //refA[Geom::Y] = boundingbox_Y.middle();
298 //refB[Geom::X] = boundingbox_X.max();
299 //refB[Geom::Y] = boundingbox_Y.middle();
300 //std::vector<Geom::Path> paths;
301 //Geom::Path path = Geom::Path( (Point) refA);
302 //path.appendNew<Geom::LineSegment>( (Point) refB );
303 //paths.push_back(path * Matrix(1./3,0,0,1./3, refA[X]*2./3, refA[Y]*2./3 + boundingbox_Y.extent()/2));
304 //paths.push_back(path * Matrix(1./3,0,0,1./3, refB[X]*2./3, refB[Y]*2./3 + boundingbox_Y.extent()/2));
305 //paths.push_back(path);
306 //generator.set_new_value(paths, true);
307 }
309 } // namespace LivePathEffect
310 } /* namespace Inkscape */
312 /*
313 Local Variables:
314 mode:c++
315 c-file-style:"stroustrup"
316 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
317 indent-tabs-mode:nil
318 fill-column:99
319 End:
320 */
321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :