Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
lpe-vonkoch.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
4 *
5 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
6 */
7
8#include "lpe-vonkoch.h"
9
10#include <vector>
11
12#include <glibmm/i18n.h>
13
14#include "object/sp-lpe-item.h"
15
16namespace Inkscape {
17namespace LivePathEffect {
18
19void
20VonKochPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
21{
23 //sp_nodepath_make_straight_path(np);
24}
25
26//FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
27void
28VonKochRefPathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
29{
31 //sp_nodepath_make_straight_path(np);
32}
33bool
35{
37 bool res = PathParam::param_readSVGValue(strvalue);
38 if (res && _pathvector.size()==1 && _pathvector.front().size()==1){
39 return true;
40 }else{
41 _pathvector = old;
42 return false;
43 }
44}
45
47 Effect(lpeobject),
48 nbgenerations(_("N_r of generations:"), _("Depth of the recursion --- keep low!!"), "nbgenerations", &wr, this, 1),
49 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"),
50 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),
51 drawall(_("Dra_w all generations"), _("If unchecked, draw only the last generation"), "drawall", &wr, this, true),
52 //,draw_boxes(_("Display boxes"), _("Display boxes instead of paths only"), "draw_boxes", &wr, this, true)
53 ref_path(_("Reference segment:"), _("The reference segment. Defaults to the horizontal midline of the bbox."), "ref_path", &wr, this, "M0,0 L10,0"),
54 //refA(_("Ref Start"), _("Left side middle of the reference box"), "refA", &wr, this),
55 //refB(_("Ref End"), _("Right side middle of the reference box"), "refB", &wr, this),
56 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
57 maxComplexity(_("_Max complexity:"), _("Disable effect if the output is too complex"), "maxComplexity", &wr, this, 1000)
58{
59 //FIXME: a path is used here instead of 2 points to work around path/point param incompatibility bug.
61 //registerParameter(&refA) );
62 //registerParameter(&refB) );
68 //registerParameter(&draw_boxes) );
71 nbgenerations.param_set_range(0, std::numeric_limits<gint>::max());
73 maxComplexity.param_set_range(0, std::numeric_limits<gint>::max());
74}
75
76LPEVonKoch::~LPEVonKoch() = default;
77
78bool
80{
81 if (!is_load || is_applied) {
82 return false;
83 }
86 return false;
87}
88
89
92{
93 using namespace Geom;
95 Geom::PathVector generating_path = generator.get_pathvector() * affine;
96
97 if (generating_path.empty()) {
98 return path_in;
99 }
100 if (is_load) {
103 }
104
105 //Collect transform matrices.
106 affine = ref_path.get_relative_affine();
107
108 Affine m0;
109 Geom::Path refpath = ref_path.get_pathvector().front() * affine ;
110 Point A = refpath.pointAt(0);
111 Point B = refpath.pointAt(refpath.size());
112 Point u = B-A;
113 m0 = Affine(u[X], u[Y],-u[Y], u[X], A[X], A[Y]);
114
115 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
116 //Point u = refB-refA;
117 //m0 = Affine(u[X], u[Y],-u[Y], u[X], refA[X], refA[Y]);
118 m0 = m0.inverse();
119
120 std::vector<Affine> transforms;
121 for (const auto & i : generating_path){
122 Affine m;
123 if(i.size()==1){
124 Point p = i.pointAt(0);
125 Point u = i.pointAt(1)-p;
126 m = Affine(u[X], u[Y],-u[Y], u[X], p[X], p[Y]);
127 m = m0*m;
128 transforms.push_back(m);
129 }else if(i.size()>=2){
130 Point p = i.pointAt(1);
131 Point u = i.pointAt(2)-p;
132 Point v = p-i.pointAt(0);
133 if (similar_only.get_value()){
134 int sign = (u[X]*v[Y]-u[Y]*v[X]>=0?1:-1);
135 v[X] = -u[Y]*sign;
136 v[Y] = u[X]*sign;
137 }
138 m = Affine(u[X], u[Y],v[X], v[Y], p[X], p[Y]);
139 m = m0*m;
140 transforms.push_back(m);
141 }
142 }
143
144 if (transforms.empty()){
145 return path_in;
146 }
147
148 //Do nothing if the output is too complex...
149 int path_in_complexity = 0;
150 for (const auto & k : path_in){
151 path_in_complexity+=k.size();
152 }
153 double complexity = std::pow(transforms.size(), nbgenerations) * path_in_complexity;
154 if (drawall.get_value()){
155 int k = transforms.size();
156 if(k>1){
157 complexity = (std::pow(k,nbgenerations+1)-1)/(k-1)*path_in_complexity;
158 }else{
159 complexity = nbgenerations*k*path_in_complexity;
160 }
161 }
162 if (complexity > double(maxComplexity)){
163 g_warning("VonKoch lpe's output too complex. Effect bypassed.");
164 return path_in;
165 }
166
167 //Generate path:
168 Geom::PathVector pathi = path_in;
169 Geom::PathVector path_out = path_in;
170
171 for (unsigned i = 0; i<nbgenerations; i++){
172 if (drawall.get_value()){
173 path_out = path_in;
174 complexity = path_in_complexity;
175 }else{
176 path_out = Geom::PathVector();
177 complexity = 0;
178 }
179 for (const auto & transform : transforms){
180 for (unsigned k = 0; k<pathi.size() && complexity < maxComplexity; k++){
181 path_out.push_back(pathi[k]*transform);
182 complexity+=pathi[k].size();
183 }
184 }
185 pathi = path_out;
186 }
187 return path_out;
188}
189
190
191//Useful??
192//void
193//LPEVonKoch::addCanvasIndicators(SPLPEItem const */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
194/*{
195 using namespace Geom;
196 if (draw_boxes.get_value()){
197 double ratio = .5;
198 if (similar_only.get_value()) ratio = boundingbox_Y.extent()/boundingbox_X.extent()/2;
199
200 Point BB1,BB2,BB3,BB4,v;
201
202 //Draw the reference box (ref_path is supposed to consist in one line segment)
203 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
204 Geom::Path refpath = ref_path.get_pathvector().front();
205 if (refpath.size()==1){
206 BB1 = BB4 = refpath.front().pointAt(0);
207 BB2 = BB3 = refpath.front().pointAt(1);
208 v = rot90(BB2 - BB1)*ratio;
209 BB1 -= v;
210 BB2 -= v;
211 BB3 += v;
212 BB4 += v;
213 Geom::Path refbox(BB1);
214 refbox.appendNew<LineSegment>(BB2);
215 refbox.appendNew<LineSegment>(BB3);
216 refbox.appendNew<LineSegment>(BB4);
217 refbox.close();
218 PathVector refbox_as_vect;
219 refbox_as_vect.push_back(refbox);
220 hp_vec.push_back(refbox_as_vect);
221 }
222 //Draw the transformed boxes
223 Geom::PathVector generating_path = generator.get_pathvector();
224 for (unsigned i=0;i<generating_path.size(); i++){
225 if (generating_path[i].size()==0){
226 //Ooops! this should not happen.
227 }else if (generating_path[i].size()==1){
228 BB1 = BB4 = generating_path[i].pointAt(0);
229 BB2 = BB3 = generating_path[i].pointAt(1);
230 v = rot90(BB2 - BB1)*ratio;
231 }else{//Only tak the first 2 segments into account.
232 BB1 = BB4 = generating_path[i].pointAt(1);
233 BB2 = BB3 = generating_path[i].pointAt(2);
234 if(similar_only.get_value()){
235 v = rot90(BB2 - BB1)*ratio;
236 }else{
237 v = (generating_path[i].pointAt(0) - BB1)*ratio;
238 }
239 }
240 BB1 -= v;
241 BB2 -= v;
242 BB3 += v;
243 BB4 += v;
244 Geom::Path path(BB1);
245 path.appendNew<LineSegment>(BB2);
246 path.appendNew<LineSegment>(BB3);
247 path.appendNew<LineSegment>(BB4);
248 path.close();
249 PathVector pathv;
250 pathv.push_back(path);
251 hp_vec.push_back(pathv);
252 }
253 }
254}
255*/
256
257void
259{
260 using namespace Geom;
261 original_bbox(lpeitem, false, true);
262
264 Geom::Point A,B;
265 if (paths.empty()||paths.front().size()==0){
266 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
267 //refA.param_setValue( Geom::Point(boundingbox_X.min(), boundingbox_Y.middle()) );
268 //refB.param_setValue( Geom::Point(boundingbox_X.max(), boundingbox_Y.middle()) );
271 }else{
272 A = paths.front().pointAt(0);
273 B = paths.front().pointAt(paths.front().size());
274 }
275 if (paths.size()!=1||paths.front().size()!=1){
276 Geom::Path tmp_path(A);
277 tmp_path.appendNew<LineSegment>(B);
278 Geom::PathVector tmp_pathv;
279 tmp_pathv.push_back(tmp_path);
280 ref_path.set_new_value(tmp_pathv,true);
281 }
282}
283
284
285void
287{
289
290 using namespace Geom;
291 original_bbox(cast<SPLPEItem>(item), false, true);
292
293 Point A,B;
298
299 Geom::PathVector paths,refpaths;
300 Geom::Path path = Geom::Path(A);
302
303 refpaths.push_back(path);
304 ref_path.set_new_value(refpaths, true);
305
306 paths.push_back(path * Affine(1./3,0,0,1./3, A[X]*2./3, A[Y]*2./3 + boundingbox_Y.extent()/2));
307 paths.push_back(path * Affine(1./3,0,0,1./3, B[X]*2./3, B[Y]*2./3 + boundingbox_Y.extent()/2));
309
310 //FIXME: a path is used as ref instead of 2 points to work around path/point param incompatibility bug.
311 //refA[Geom::X] = boundingbox_X.min();
312 //refA[Geom::Y] = boundingbox_Y.middle();
313 //refB[Geom::X] = boundingbox_X.max();
314 //refB[Geom::Y] = boundingbox_Y.middle();
315 //Geom::PathVector paths;
316 //Geom::Path path = Geom::Path( (Point) refA);
317 //path.appendNew<Geom::LineSegment>( (Point) refB );
318 //paths.push_back(path * Affine(1./3,0,0,1./3, refA[X]*2./3, refA[Y]*2./3 + boundingbox_Y.extent()/2));
319 //paths.push_back(path * Affine(1./3,0,0,1./3, refB[X]*2./3, refB[Y]*2./3 + boundingbox_Y.extent()/2));
320 //paths.push_back(path);
321 //generator.set_new_value(paths, true);
322}
323
324} // namespace LivePathEffect
325} /* namespace Inkscape */
326
327/*
328 Local Variables:
329 mode:c++
330 c-file-style:"stroustrup"
331 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
332 indent-tabs-mode:nil
333 fill-column:99
334 End:
335*/
336// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
3x3 matrix representing an affine transformation.
Definition affine.h:70
Affine inverse() const
Compute the inverse matrix.
Definition affine.cpp:388
constexpr C extent() const
constexpr C min() const
constexpr C max() const
constexpr C middle() const
Sequence of subpaths.
Definition pathvector.h:122
size_type size() const
Get the number of paths in the vector.
Definition pathvector.h:147
void push_back(Path const &path)
Append a path at the end.
Definition pathvector.h:172
bool empty() const
Check whether the vector contains any paths.
Definition pathvector.h:145
Sequence of contiguous curves, aka spline.
Definition path.h:353
Point pointAt(Coord t) const
Get the point at the specified time value.
Definition path.cpp:449
size_type size() const
Natural size of the path.
Definition path.h:490
void appendNew(Args &&... args)
Append a new curve to the path.
Definition path.h:804
Two-dimensional point that doubles as a vector.
Definition point.h:66
void registerParameter(Parameter *param)
Definition effect.cpp:1710
virtual void resetDefaults(SPItem const *item)
Sets all parameters to their default values and writes them to SVG.
Definition effect.cpp:2014
void original_bbox(SPLPEItem const *lpeitem, bool absolute=false, bool clip_mask=false, Geom::Affine base_transform=Geom::identity())
LPEVonKoch(LivePathEffectObject *lpeobject)
bool doOnOpen(SPLPEItem const *lpeitem) override
Is performed on load document or revert If the item is fixed legacy return true.
Geom::PathVector doEffect_path(Geom::PathVector const &path_in) override
void doBeforeEffect(SPLPEItem const *item) override
Is performed each time before the effect is updated.
void resetDefaults(SPItem const *item) override
Sets all parameters to their default values and writes them to SVG.
Geom::PathVector const & get_pathvector() const
Definition path.cpp:99
bool param_readSVGValue(const gchar *strvalue) override
Definition path.cpp:142
void set_new_value(Geom::PathVector const &newpath, bool write_to_svg)
Definition path.cpp:370
void param_setup_nodepath(Inkscape::NodePath::Path *np) override
Definition path.cpp:308
Geom::PathVector _pathvector
Definition path.h:70
Geom::Affine get_relative_affine()
Definition path.cpp:86
void param_set_range(double min, double max)
void param_setup_nodepath(Inkscape::NodePath::Path *np) override
bool param_readSVGValue(const gchar *strvalue) override
void param_setup_nodepath(Inkscape::NodePath::Path *np) override
Base class for visual SVG elements.
Definition sp-item.h:109
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
SPItem * item
vector< vector< Point > > paths
Definition metro.cpp:36
Various utility functions.
Definition affine.h:22
static float sign(double number)
Returns +1 for positive numbers, -1 for negative numbers, and 0 otherwise.
Helper class to stream background task notifications as a series of messages.
Base class for live path effect items.