Code

Rename LPE: mirror reflect --> mirror symmetry
[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"
18 #include "sp-item.h"
19 #include "sp-path.h"
20 #include "svg/svg.h"
21 #include "xml/repr.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>
32 #include "ui/widget/scalar.h"
33 #include "libnr/nr-values.h"
35 namespace Inkscape {
36 namespace LivePathEffect {
38 using namespace Geom;
40 LPECurveStitch::LPECurveStitch(LivePathEffectObject *lpeobject) :
41     Effect(lpeobject),
42     strokepath(_("Stitch path"), _("The path that will be used as stitch."), "strokepath", &wr, this, "M0,0 L1,0"),
43     nrofpaths(_("Number of paths"), _("The number of paths that will be generated."), "count", &wr, this, 5),
44     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),
45     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),
46     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),
47     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),
48     prop_scale(_("Scale width"), _("Scale the width of the stitch path"), "prop_scale", &wr, this, 1),
49     scale_y_rel(_("Scale width relative to length"), _("Scale the width of the stitch path relative to its length"), "scale_y_rel", &wr, this, false)
50 {
51     registerParameter( dynamic_cast<Parameter *>(&nrofpaths) );
52     registerParameter( dynamic_cast<Parameter *>(&startpoint_edge_variation) );
53     registerParameter( dynamic_cast<Parameter *>(&startpoint_spacing_variation) );
54     registerParameter( dynamic_cast<Parameter *>(&endpoint_edge_variation) );
55     registerParameter( dynamic_cast<Parameter *>(&endpoint_spacing_variation) );
56     registerParameter( dynamic_cast<Parameter *>(&strokepath) );
57     registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
58     registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
60     nrofpaths.param_make_integer();
61     nrofpaths.param_set_range(2, NR_HUGE);
63     prop_scale.param_set_digits(3);
64     prop_scale.param_set_increments(0.01, 0.10);
65 }
67 LPECurveStitch::~LPECurveStitch()
68 {
70 }
72 std::vector<Geom::Path>
73 LPECurveStitch::doEffect_path (std::vector<Geom::Path> const & path_in)
74 {
75     if (path_in.size() >= 2) {
76         startpoint_edge_variation.resetRandomizer();
77         endpoint_edge_variation.resetRandomizer();
78         startpoint_spacing_variation.resetRandomizer();
79         endpoint_spacing_variation.resetRandomizer();
81         D2<Piecewise<SBasis> > stroke = make_cuts_independent(strokepath.get_pwd2());
82         Interval bndsStroke = bounds_exact(stroke[0]);
83         gdouble scaling = bndsStroke.max() - bndsStroke.min();
84         Interval bndsStrokeY = bounds_exact(stroke[1]);
85         Point stroke_origin(bndsStroke.min(), (bndsStrokeY.max()+bndsStrokeY.min())/2);
87         std::vector<Geom::Path> path_out;
89         // do this for all permutations (ii,jj) if there are more than 2 paths? realllly cool!
90         for (unsigned ii = 0   ; ii < path_in.size() - 1; ii++)
91         for (unsigned jj = ii+1; jj < path_in.size(); jj++)
92         {
93             Piecewise<D2<SBasis> > A = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[ii].toPwSb()),2,.1);
94             Piecewise<D2<SBasis> > B = arc_length_parametrization(Piecewise<D2<SBasis> >(path_in[jj].toPwSb()),2,.1);
95             Interval bndsA = A.domain();
96             Interval bndsB = B.domain();
97             gdouble incrementA = (bndsA.max()-bndsA.min()) / (nrofpaths-1);
98             gdouble incrementB = (bndsB.max()-bndsB.min()) / (nrofpaths-1);
99             gdouble tA = bndsA.min();
100             gdouble tB = bndsB.min();
101             gdouble tAclean = tA; // the tA without spacing_variation
102             gdouble tBclean = tB; // the tB without spacing_variation
104             for (int i = 0; i < nrofpaths; i++) {
105                 Point start = A(tA);
106                 Point end = B(tB);
107                 if (startpoint_edge_variation.get_value() != 0)
108                     start = start + (startpoint_edge_variation - startpoint_edge_variation.get_value()/2) * (end - start);
109                 if (endpoint_edge_variation.get_value() != 0)
110                     end = end + (endpoint_edge_variation - endpoint_edge_variation.get_value()/2)* (end - start);
111         
112                 if (!Geom::are_near(start,end)) {
113                     gdouble scaling_y = 1.0;
114                     if (scale_y_rel.get_value()) {
115                         scaling_y = (L2(end-start)/scaling)*prop_scale;
116                     } else {
117                         scaling_y = prop_scale;
118                     }
120                     Matrix transform;
121                     transform.setXAxis( (end-start) / scaling );
122                     transform.setYAxis( rot90(unit_vector(end-start)) * scaling_y);
123                     transform.setTranslation( start );
124                     Piecewise<D2<SBasis> > pwd2_out = (strokepath.get_pwd2()-stroke_origin) * transform;
126                     // add stuff to one big pw<d2<sbasis> > and then outside the loop convert to path?
127                     // No: this way, the separate result paths are kept separate which might come in handy some time!
128                     std::vector<Geom::Path> result = Geom::path_from_piecewise(pwd2_out, LPE_CONVERSION_TOLERANCE);
129                     path_out.push_back(result[0]);
130                 }
131                 gdouble svA = startpoint_spacing_variation - startpoint_spacing_variation.get_value()/2;
132                 gdouble svB = endpoint_spacing_variation - endpoint_spacing_variation.get_value()/2;
133                 tAclean += incrementA;
134                 tBclean += incrementB;
135                 tA = tAclean + incrementA * svA;
136                 tB = tBclean + incrementB * svB;
137                 if (tA > bndsA.max())
138                     tA = bndsA.max();
139                 if (tB > bndsB.max())
140                     tB = bndsB.max();
141             }
142         }
144         return path_out;
145     } else {
146         return path_in;
147     }
150 void
151 LPECurveStitch::resetDefaults(SPItem * item)
153     if (!SP_IS_PATH(item)) return;
155     using namespace Geom;
157     // set the stroke path to run horizontally in the middle of the bounding box of the original path
158     
159     // calculate bounding box:  (isn't there a simpler way?)
160     Piecewise<D2<SBasis> > pwd2;
161     std::vector<Geom::Path> temppath = sp_svg_read_pathv( SP_OBJECT_REPR(item)->attribute("inkscape:original-d"));
162     for (unsigned int i=0; i < temppath.size(); i++) {
163         pwd2.concat( temppath[i].toPwSb() );
164     }
165     D2<Piecewise<SBasis> > d2pw = make_cuts_independent(pwd2);
166     Interval bndsX = bounds_exact(d2pw[0]);
167     Interval bndsY = bounds_exact(d2pw[1]);
168     Point start(bndsX.min(), (bndsY.max()+bndsY.min())/2);
169     Point end(bndsX.max(), (bndsY.max()+bndsY.min())/2);
171     if ( !Geom::are_near(start,end) ) {
172         Geom::Path path;
173         path.start( start );
174         path.appendNew<Geom::LineSegment>( end );
175         strokepath.param_set_and_write_new_value( path.toPwSb() );
176     } else {
177         // bounding box is too small to make decent path. set to default default. :-)
178         strokepath.param_set_and_write_default();
179     }
182 void
183 LPECurveStitch::transform_multiply(Geom::Matrix const& postmul, bool set)
185     // only take translations into account
186     if (postmul.isTranslation()) {
187         strokepath.param_transform_multiply(postmul, set);
188     } else if (!scale_y_rel.get_value()) {
189   // this basically means that for this transformation, the result should be the same as normal scaling the result path
190   // don't know how to do this yet.
191 //        Geom::Matrix new_postmul;
192         //new_postmul.setIdentity();
193 //        new_postmul.setTranslation(postmul.translation());
194 //        Effect::transform_multiply(new_postmul, set);
195     }
198 } //namespace LivePathEffect
199 } /* namespace Inkscape */
201 /*
202   Local Variables:
203   mode:c++
204   c-file-style:"stroustrup"
205   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
206   indent-tabs-mode:nil
207   fill-column:99
208   End:
209 */
210 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :