Code

change doEffect functions to use const& parameters
[inkscape.git] / src / live_effects / lpe-curvestitch.cpp
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"
22 #include "xml/repr.h"
24 #include <2geom/path.h>
25 #include <2geom/piecewise.h>
26 #include <2geom/sbasis.h>
27 #include <2geom/sbasis-geometric.h>
28 #include <2geom/bezier-to-sbasis.h>
29 #include <2geom/sbasis-to-bezier.h>
30 #include <2geom/d2.h>
31 #include <2geom/matrix.h>
34 #include "ui/widget/scalar.h"
35 #include "libnr/nr-values.h"
37 namespace Inkscape {
38 namespace LivePathEffect {
40 using namespace Geom;
42 LPECurveStitch::LPECurveStitch(LivePathEffectObject *lpeobject) :
43     Effect(lpeobject),
44     strokepath(_("Stroke path"), _("The path that will be used as stitch."), "strokepath", &wr, this, "M0,0 L1,0"),
45     nrofpaths(_("Number of paths"), _("The number of paths that will be generated."), "count", &wr, this, 5),
46     startpoint_edge_variation(_("Start edge variance"), _("The amount of random jitter to move the start points of the stitches inside & outside the guide path"), "startpoint_edge_variation", &wr, this, 0),
47     startpoint_spacing_variation(_("Start spacing variance"), _("The amount of random shifting to move the start points of the stitches back & forth along the guide path"), "startpoint_spacing_variation", &wr, this, 0),
48     endpoint_edge_variation(_("End edge variance"), _("The amount of randomness that moves the end points of the stitches inside & outside the guide path"), "endpoint_edge_variation", &wr, this, 0),
49     endpoint_spacing_variation(_("End spacing variance"), _("The amount of random shifting to move the end points of the stitches back & forth along the guide path"), "endpoint_spacing_variation", &wr, this, 0),
50     prop_scale(_("Scale width"), _("Scaling of the width of the stroke path"), "prop_scale", &wr, this, 1),
51     scale_y_rel(_("Scale width relative"), _("Scale the width of the stroke path relative to its length"), "scale_y_rel", &wr, this, false)
52 {
53     registerParameter( dynamic_cast<Parameter *>(&nrofpaths) );
54     registerParameter( dynamic_cast<Parameter *>(&startpoint_edge_variation) );
55     registerParameter( dynamic_cast<Parameter *>(&startpoint_spacing_variation) );
56     registerParameter( dynamic_cast<Parameter *>(&endpoint_edge_variation) );
57     registerParameter( dynamic_cast<Parameter *>(&endpoint_spacing_variation) );
58     registerParameter( dynamic_cast<Parameter *>(&strokepath) );
59     registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
60     registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
62     nrofpaths.param_make_integer();
63     nrofpaths.param_set_range(2, NR_HUGE);
65     prop_scale.param_set_digits(3);
66     prop_scale.param_set_increments(0.01, 0.10);
67 }
69 LPECurveStitch::~LPECurveStitch()
70 {
72 }
74 std::vector<Geom::Path>
75 LPECurveStitch::doEffect_path (std::vector<Geom::Path> const & path_in)
76 {
77     if (path_in.size() >= 2) {
78         startpoint_edge_variation.resetRandomizer();
79         endpoint_edge_variation.resetRandomizer();
80         startpoint_spacing_variation.resetRandomizer();
81         endpoint_spacing_variation.resetRandomizer();
83         D2<Piecewise<SBasis> > stroke = make_cuts_independant(strokepath.get_pwd2());
84         Interval bndsStroke = bounds_exact(stroke[0]);
85         gdouble scaling = bndsStroke.max() - bndsStroke.min();
86         Interval bndsStrokeY = bounds_exact(stroke[1]);
87         Point stroke_origin(bndsStroke.min(), (bndsStrokeY.max()+bndsStrokeY.min())/2);
89         std::vector<Geom::Path> path_out;
91         // do this for all permutations (ii,jj) if there are more than 2 paths? realllly cool!
92         for (unsigned ii = 0   ; ii < path_in.size() - 1; ii++)
93         for (unsigned jj = ii+1; jj < path_in.size(); jj++)
94         {
95             Piecewise<D2<SBasis> > A = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[ii].toPwSb()),2,.1);
96             Piecewise<D2<SBasis> > B = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[jj].toPwSb()),2,.1);
97             Interval bndsA = A.domain();
98             Interval bndsB = B.domain();
99             gdouble incrementA = (bndsA.max()-bndsA.min()) / (nrofpaths-1);
100             gdouble incrementB = (bndsB.max()-bndsB.min()) / (nrofpaths-1);
101             gdouble tA = bndsA.min();
102             gdouble tB = bndsB.min();
103             gdouble tAclean = tA; // the tA without spacing_variation
104             gdouble tBclean = tB; // the tB without spacing_variation
106             for (int i = 0; i < nrofpaths; i++) {
107                 Point start = A(tA);
108                 Point end = B(tB);
109                 if (startpoint_edge_variation.get_value() != 0)
110                     start = start + (startpoint_edge_variation - startpoint_edge_variation.get_value()/2) * (end - start);
111                 if (endpoint_edge_variation.get_value() != 0)
112                     end = end + (endpoint_edge_variation - endpoint_edge_variation.get_value()/2)* (end - start);
113         
114                 if (!Geom::are_near(start,end)) {
115                     gdouble scaling_y = 1.0;
116                     if (scale_y_rel.get_value()) {
117                         scaling_y = (L2(end-start)/scaling)*prop_scale;
118                     } else {
119                         scaling_y = prop_scale;
120                     }
122                     Matrix transform;
123                     transform.setXAxis( (end-start) / scaling );
124                     transform.setYAxis( rot90(unit_vector(end-start)) * scaling_y);
125                     transform.setTranslation( start );
126                     Piecewise<D2<SBasis> > pwd2_out = (strokepath.get_pwd2()-stroke_origin) * transform;
128                     // add stuff to one big pw<d2<sbasis> > and then outside the loop convert to path?
129                     // No: this way, the separate result paths are kept separate which might come in handy some time!
130                     std::vector<Geom::Path> result = Geom::path_from_piecewise(pwd2_out, LPE_CONVERSION_TOLERANCE);
131                     path_out.push_back(result[0]);
132                 }
133                 gdouble svA = startpoint_spacing_variation - startpoint_spacing_variation.get_value()/2;
134                 gdouble svB = endpoint_spacing_variation - endpoint_spacing_variation.get_value()/2;
135                 tAclean += incrementA;
136                 tBclean += incrementB;
137                 tA = tAclean + incrementA * svA;
138                 tB = tBclean + incrementB * svB;
139                 if (tA > bndsA.max())
140                     tA = bndsA.max();
141                 if (tB > bndsB.max())
142                     tB = bndsB.max();
143             }
144         }
146         return path_out;
147     } else {
148         return path_in;
149     }
152 void
153 LPECurveStitch::resetDefaults(SPItem * item)
155     if (!SP_IS_PATH(item)) return;
157     using namespace Geom;
159     // set the stroke path to run horizontally in the middle of the bounding box of the original path
160     
161     // calculate bounding box:  (isn't there a simpler way?)
162     Piecewise<D2<SBasis> > pwd2;
163     std::vector<Geom::Path> temppath = SVGD_to_2GeomPath( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
164     for (unsigned int i=0; i < temppath.size(); i++) {
165         pwd2.concat( temppath[i].toPwSb() );
166     }
167     D2<Piecewise<SBasis> > d2pw = make_cuts_independant(pwd2);
168     Interval bndsX = bounds_exact(d2pw[0]);
169     Interval bndsY = bounds_exact(d2pw[1]);
170     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
171     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
173     if ( !Geom::are_near(start,end) ) {
174         Geom::Path path;
175         path.start( start );
176         path.appendNew<Geom::LineSegment>( end );
177         strokepath.param_set_and_write_new_value( path.toPwSb() );
178     } else {
179         // bounding box is too small to make decent path. set to default default. :-)
180         strokepath.param_set_and_write_default();
181     }
184 void
185 LPECurveStitch::transform_multiply(Geom::Matrix const& postmul, bool set)
187     // only take translations into account
188     if (postmul.isTranslation()) {
189         strokepath.param_transform_multiply(postmul, set);
190     } else if (!scale_y_rel.get_value()) {
191   // this basically means that for this transformation, the result should be the same as normal scaling the result path
192   // don't know how to do this yet.
193 //        Geom::Matrix new_postmul;
194         //new_postmul.setIdentity();
195 //        new_postmul.setTranslation(postmul.translation());
196 //        Effect::transform_multiply(new_postmul, set);
197     }
200 } //namespace LivePathEffect
201 } /* namespace Inkscape */
203 /*
204   Local Variables:
205   mode:c++
206   c-file-style:"stroustrup"
207   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
208   indent-tabs-mode:nil
209   fill-column:99
210   End:
211 */
212 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :