de7d5ce7c6e846372cbcd3e951bf5f78ac8759ec
1 #define INKSCAPE_LPE_CURVESTITCH_CPP
2 /** \file
3 * LPE Curve Stitching implementation, used as an example for a base starting class
4 * when implementing new LivePathEffects.
5 *
6 */
7 /*
8 * Authors:
9 * Johan Engelen
10 *
11 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "live_effects/lpe-curvestitch.h"
17 #include "display/curve.h"
18 #include <libnr/n-art-bpath.h>
19 #include "sp-item.h"
20 #include "sp-path.h"
21 #include "live_effects/n-art-bpath-2geom.h"
23 #include <2geom/path.h>
24 #include <2geom/piecewise.h>
25 #include <2geom/sbasis.h>
26 #include <2geom/sbasis-geometric.h>
27 #include <2geom/bezier-to-sbasis.h>
28 #include <2geom/sbasis-to-bezier.h>
29 #include <2geom/d2.h>
30 #include <2geom/matrix.h>
33 #include "ui/widget/scalar.h"
34 #include "libnr/nr-values.h"
36 namespace Inkscape {
37 namespace LivePathEffect {
39 using namespace Geom;
41 LPECurveStitch::LPECurveStitch(LivePathEffectObject *lpeobject) :
42 Effect(lpeobject),
43 strokepath(_("Stroke path"), _("The path that will be used as stitch."), "strokepath", &wr, this, "M0,0 L1,0"),
44 nrofpaths(_("Number of paths"), _("The number of paths that will be generated."), "count", &wr, this, 5),
45 startpoint_variation(_("Start point jitter"), _("The amount of random jitter to apply to the start points of the stitches"), "startpoint_variation", &wr, this, 0),
46 endpoint_variation(_("End point jitter"), _("The amount of random jitter to apply to the end points of the stitches"), "endpoint_variation", &wr, this, 0),
47 spacing_variation(_("Spacing variation"), _("Determines whether lines cluster together or have an equal spacing between each other."), "spacing_variation", &wr, this, 0),
48 prop_scale(_("Scale width"), _("Scaling of the width of the stroke path"), "prop_scale", &wr, this, 1),
49 scale_y_rel(_("Scale width relative"), _("Scale the width of the stroke path relative to its length"), "scale_y_rel", &wr, this, false)
50 {
51 registerParameter( dynamic_cast<Parameter *>(&nrofpaths) );
52 registerParameter( dynamic_cast<Parameter *>(&startpoint_variation) );
53 registerParameter( dynamic_cast<Parameter *>(&endpoint_variation) );
54 registerParameter( dynamic_cast<Parameter *>(&spacing_variation) );
55 registerParameter( dynamic_cast<Parameter *>(&strokepath) );
56 registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
57 registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
59 nrofpaths.param_make_integer();
60 nrofpaths.param_set_range(2, NR_HUGE);
62 prop_scale.param_set_digits(3);
63 prop_scale.param_set_increments(0.01, 0.10);
64 }
66 LPECurveStitch::~LPECurveStitch()
67 {
69 }
71 std::vector<Geom::Path>
72 LPECurveStitch::doEffect_path (std::vector<Geom::Path> & path_in)
73 {
74 if (path_in.size() >= 2) {
75 startpoint_variation.resetRandomizer();
76 endpoint_variation.resetRandomizer();
77 spacing_variation.resetRandomizer();
79 D2<Piecewise<SBasis> > stroke = make_cuts_independant(strokepath);
80 Interval bndsStroke = bounds_exact(stroke[0]);
81 gdouble scaling = bndsStroke.max() - bndsStroke.min();
82 Interval bndsStrokeY = bounds_exact(stroke[1]);
83 Point stroke_origin(bndsStroke.min(), (bndsStrokeY.max()+bndsStrokeY.min())/2);
85 std::vector<Geom::Path> path_out (nrofpaths);
87 // do this for all permutations if there are more than 2 paths? realllly cool!
88 Piecewise<D2<SBasis> > A = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[0].toPwSb()),2,.1);
89 Piecewise<D2<SBasis> > B = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[1].toPwSb()),2,.1);
90 Interval bndsA = A.domain();
91 Interval bndsB = B.domain();
92 gdouble incrementA = (bndsA.max()-bndsA.min()) / (nrofpaths-1);
93 gdouble incrementB = (bndsB.max()-bndsB.min()) / (nrofpaths-1);
94 gdouble tA = bndsA.min();
95 gdouble tB = bndsB.min();
96 for (int i = 0; i < nrofpaths; i++) {
97 Point start = A(tA);
98 Point end = B(tB);
99 if (startpoint_variation.get_value() != 0)
100 start = start + (startpoint_variation - startpoint_variation.get_value()/2) * (end - start);
101 if (endpoint_variation.get_value() != 0)
102 end = end + (endpoint_variation - endpoint_variation.get_value()/2)* (end - start);
104 gdouble scaling_y = 1.0;
105 if (scale_y_rel.get_value()) {
106 scaling_y = (L2(end-start)/scaling)*prop_scale;
107 } else {
108 scaling_y = prop_scale;
109 }
111 Matrix transform;
112 transform.setXAxis( (end-start) / scaling );
113 transform.setYAxis( rot90(unit_vector(end-start)) * scaling_y);
114 transform.setTranslation( start );
115 Piecewise<D2<SBasis> > pwd2_out = (strokepath-stroke_origin) * transform;
116 // add stuff to one big pw<d2<sbasis> > and then outside the loop convert to path?
117 std::vector<Geom::Path> result = Geom::path_from_piecewise(pwd2_out, LPE_CONVERSION_TOLERANCE);
118 path_out[i] = result[0];
119 gdouble sv = spacing_variation;
120 tA += incrementA * (1 + sv - spacing_variation.get_value()/2);
121 tB += incrementB * (1 + sv - spacing_variation.get_value()/2);
122 if (tA > bndsA.max())
123 tA = bndsA.max();
124 if (tB > bndsB.max())
125 tB = bndsB.max();
126 }
128 return path_out;
129 } else {
130 return path_in;
131 }
132 }
134 void
135 LPECurveStitch::resetDefaults(SPItem * item)
136 {
137 if (!SP_IS_PATH(item)) return;
139 using namespace Geom;
141 // set the stroke path to run horizontally in the middle of the bounding box of the original path
142 Piecewise<D2<SBasis> > pwd2;
143 std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
144 for (unsigned int i=0; i < temppath.size(); i++) {
145 pwd2.concat( temppath[i].toPwSb() );
146 }
148 D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
149 Interval bndsX = bounds_exact(d2pw[0]);
150 Interval bndsY = bounds_exact(d2pw[1]);
151 Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
152 Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
154 Geom::Path path;
155 path.start( start );
156 path.appendNew<Geom::LineSegment>( end );
157 strokepath.param_set_and_write_new_value( path.toPwSb() );
158 }
160 } //namespace LivePathEffect
161 } /* namespace Inkscape */
163 /*
164 Local Variables:
165 mode:c++
166 c-file-style:"stroustrup"
167 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168 indent-tabs-mode:nil
169 fill-column:99
170 End:
171 */
172 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :