Code

Partially fix bug #167500 (rulers being off)
[inkscape.git] / src / live_effects / lpe-skeletalstrokes.cpp
1 #define INKSCAPE_LPE_SKELETAL_STROKES_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "live_effects/lpe-skeletalstrokes.h"
10 #include "sp-shape.h"
11 #include "display/curve.h"
12 #include <libnr/n-art-bpath.h>
13 #include "live_effects/n-art-bpath-2geom.h"
14 #include "svg/svg.h"
15 #include "ui/widget/scalar.h"
17 #include <2geom/sbasis.h>
18 #include <2geom/sbasis-geometric.h>
19 #include <2geom/bezier-to-sbasis.h>
20 #include <2geom/sbasis-to-bezier.h>
21 #include <2geom/d2.h>
22 #include <2geom/piecewise.h>
24 #include <algorithm>
25 using std::vector;
28 /* Theory in e-mail from J.F. Barraud
29 Let B be the skeleton path, and P the pattern (the path to be deformed).
31 P is a map t --> P(t) = ( x(t), y(t) ).
32 B is a map t --> B(t) = ( a(t), b(t) ).
34 The first step is to re-parametrize B by its arc length: this is the parametrization in which a point p on B is located by its distance s from start. One obtains a new map s --> U(s) = (a'(s),b'(s)), that still describes the same path B, but where the distance along B from start to
35 U(s) is s itself.
37 We also need a unit normal to the path. This can be obtained by computing a unit tangent vector, and rotate it by 90°. Call this normal vector N(s).
39 The basic deformation associated to B is then given by:
41    (x,y) --> U(x)+y*N(x)
43 (i.e. we go for distance x along the path, and then for distance y along the normal)
45 Of course this formula needs some minor adaptations (as is it depends on the absolute position of P for instance, so a little translation is needed
46 first) but I think we can first forget about them.
47 */
49 namespace Inkscape {
50 namespace LivePathEffect {
52 static const Util::EnumData<SkelCopyType> SkelCopyTypeData[SSCT_END] = {
53     {SSCT_SINGLE,               N_("Single"),               "single"},
54     {SSCT_SINGLE_STRETCHED,     N_("Single, stretched"),    "single_stretched"},
55     {SSCT_REPEATED,             N_("Repeated"),             "repeated"},
56     {SSCT_REPEATED_STRETCHED,   N_("Repeated, stretched"),  "repeated_stretched"}
57 };
58 static const Util::EnumDataConverter<SkelCopyType> SkelCopyTypeConverter(SkelCopyTypeData, SSCT_END);
60 LPESkeletalStrokes::LPESkeletalStrokes(LivePathEffectObject *lpeobject) :
61     Effect(lpeobject),
62     pattern(_("Pattern source"), _("Path to put along the skeleton path"), "pattern", &wr, this, "M0,0 L1,0"),
63     copytype(_("Pattern copies"), _("How many pattern copies to place along the skeleton path"), "copytype", SkelCopyTypeConverter, &wr, this, SSCT_SINGLE_STRETCHED),
64     prop_scale(_("Width"), _("Width of the pattern"), "prop_scale", &wr, this, 1),
65     scale_y_rel(_("Width in units of length"), _("Scale the width of the pattern in units of its length"), "scale_y_rel", &wr, this, false),
66     spacing(_("Spacing"), _("Space between copies of the pattern"), "spacing", &wr, this, 0),
67     normal_offset(_("Normal offset"), "", "normal_offset", &wr, this, 0),
68     tang_offset(_("Tangential offset"), "", "tang_offset", &wr, this, 0),
69     vertical_pattern(_("Pattern is vertical"), "Rotate pattern 90 deg before applying", "vertical_pattern", &wr, this, false)
70 {
71     registerParameter( dynamic_cast<Parameter *>(&pattern) );
72     registerParameter( dynamic_cast<Parameter *>(&copytype) );
73     registerParameter( dynamic_cast<Parameter *>(&prop_scale) );
74     registerParameter( dynamic_cast<Parameter *>(&scale_y_rel) );
75 //    registerParameter( dynamic_cast<Parameter *>(&spacing) );
76 //    registerParameter( dynamic_cast<Parameter *>(&normal_offset) );
77 //    registerParameter( dynamic_cast<Parameter *>(&tang_offset) );
78     registerParameter( dynamic_cast<Parameter *>(&vertical_pattern) );
80     prop_scale.param_set_digits(3);
81     prop_scale.param_set_increments(0.01, 0.10);
82 }
84 LPESkeletalStrokes::~LPESkeletalStrokes()
85 {
87 }
90 Geom::Piecewise<Geom::D2<Geom::SBasis> >
91 LPESkeletalStrokes::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
92 {
93     using namespace Geom;
95 /* Much credit should go to jfb and mgsloan of lib2geom development for the code below! */
97     SkelCopyType type = copytype.get_value();
99     Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(Piecewise<D2<SBasis> >(pwd2_in),2,.1);
100     uskeleton = remove_short_cuts(uskeleton,.01);
101     Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));
102     n = force_continuity(remove_short_cuts(n,.1));
104     D2<Piecewise<SBasis> > patternd2 = make_cuts_independant(pattern);
105     Piecewise<SBasis> x = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[1]) : Piecewise<SBasis>(patternd2[0]);
106     Piecewise<SBasis> y = vertical_pattern.get_value() ? Piecewise<SBasis>(patternd2[0]) : Piecewise<SBasis>(patternd2[1]);
107     Interval pattBnds = bounds_exact(x);
108     x -= pattBnds.min();
109     Interval pattBndsY = bounds_exact(y);
110     y -= pattBndsY.middle();
112     int nbCopies = int(uskeleton.cuts.back()/pattBnds.extent());
113     double scaling = 1;
115     switch(type) {
116         case SSCT_REPEATED:
117             break;
119         case SSCT_SINGLE:
120             nbCopies = (nbCopies > 0) ? 1 : 0;
121             break;
123         case SSCT_SINGLE_STRETCHED:
124             nbCopies = 1;
125             scaling = uskeleton.cuts.back()/pattBnds.extent();
126             break;
128         case SSCT_REPEATED_STRETCHED:
129              scaling = uskeleton.cuts.back()/(((double)nbCopies)*pattBnds.extent());
130             break;
132         default:
133             return pwd2_in;
134     };
136     double pattWidth = pattBnds.extent() * scaling;
138     if (scaling != 1.0) {
139         x*=scaling;
140     }
141     if ( scale_y_rel.get_value() ) {
142         y*=(scaling*prop_scale);
143     } else {
144         if (prop_scale != 1.0) y *= prop_scale;
145     }
147     double offs = 0;
148     Piecewise<D2<SBasis> > output;
149     for (int i=0; i<nbCopies; i++){
150         output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));
151         offs+=pattWidth;
152     }
153     return output;
157 } // namespace LivePathEffect
158 } /* namespace Inkscape */
160 /*
161   Local Variables:
162   mode:c++
163   c-file-style:"stroustrup"
164   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165   indent-tabs-mode:nil
166   fill-column:99
167   End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :