Code

2 LPE things:
[inkscape.git] / src / live_effects / lpe-skeletalstrokes.cpp
1 #define INKSCAPE_LPE_SKELETAL_STROKES_CPP\r
2 \r
3 /*\r
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
5  *\r
6  * Released under GNU GPL, read the file 'COPYING' for more information\r
7  */\r
8 \r
9 #include "live_effects/lpe-skeletalstrokes.h"\r
10 #include "sp-shape.h"\r
11 #include "display/curve.h"\r
12 #include <libnr/n-art-bpath.h>\r
13 #include "live_effects/n-art-bpath-2geom.h"\r
14 #include "svg/svg.h"\r
15 \r
16 #include <2geom/sbasis.h>\r
17 #include <2geom/sbasis-geometric.h>\r
18 #include <2geom/bezier-to-sbasis.h>\r
19 #include <2geom/sbasis-to-bezier.h>\r
20 #include <2geom/d2.h>\r
21 #include <2geom/piecewise.h>\r
22 \r
23 #include <algorithm>\r
24 using std::vector;\r
25 \r
26 \r
27 /* Theory in e-mail from J.F. Barraud\r
28 Let B be the skeleton path, and P the pattern (the path to be deformed).\r
29 \r
30 P is a map t --> P(t) = ( x(t), y(t) ).\r
31 B is a map t --> B(t) = ( a(t), b(t) ).\r
32 \r
33 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\r
34 U(s) is s itself.\r
35 \r
36 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).\r
37 \r
38 The basic deformation associated to B is then given by:\r
39 \r
40    (x,y) --> U(x)+y*N(x)\r
41 \r
42 (i.e. we go for distance x along the path, and then for distance y along the normal)\r
43 \r
44 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\r
45 first) but I think we can first forget about them.\r
46 */\r
47 \r
48 namespace Inkscape {\r
49 namespace LivePathEffect {\r
50 \r
51 static const Util::EnumData<SkelCopyType> SkelCopyTypeData[SSCT_END] = {\r
52     {SSCT_SINGLE,               N_("Single"),               "single"},\r
53     {SSCT_SINGLE_STRETCHED,     N_("Single, stretched"),    "single_stretched"},\r
54     {SSCT_REPEATED,             N_("Repeated"),             "repeated"},\r
55     {SSCT_REPEATED_STRETCHED,   N_("Repeated, stretched"),  "repeated_stretched"}\r
56 };\r
57 static const Util::EnumDataConverter<SkelCopyType> SkelCopyTypeConverter(SkelCopyTypeData, SSCT_END);\r
58 \r
59 LPESkeletalStrokes::LPESkeletalStrokes(LivePathEffectObject *lpeobject) :\r
60     Effect(lpeobject),\r
61     pattern(_("Pattern"), _("Path to put along path"), "pattern", &wr, this, "M0,0 L1,1"),\r
62     copytype(_("Copytype"), _("How to shape the pattern path along the path"), "copytype", SkelCopyTypeConverter, &wr, this, SSCT_SINGLE_STRETCHED)\r
63 {\r
64     registerParameter( dynamic_cast<Parameter *>(&pattern) );\r
65     registerParameter( dynamic_cast<Parameter *>(&copytype) );\r
66 }\r
67 \r
68 LPESkeletalStrokes::~LPESkeletalStrokes()\r
69 {\r
70 \r
71 }\r
72 \r
73 \r
74 Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
75 LPESkeletalStrokes::doEffect (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)\r
76 {\r
77     using namespace Geom;\r
78 \r
79 /* LOTS OF CODE COPIED FROM 2geom/src/toys/path-along-path.cpp\r
80  * All credits should go to jfb and mgsloan of lib2geom development! */\r
81 \r
82     SkelCopyType type = copytype.get_value();\r
83 \r
84     Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(Piecewise<D2<SBasis> >(pwd2_in),2,.1);\r
85     uskeleton = remove_short_cuts(uskeleton,.01);\r
86     Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));\r
87     n = force_continuity(remove_short_cuts(n,.1));\r
88 \r
89     D2<Piecewise<SBasis> > patternd2 = make_cuts_independant(pattern);\r
90     Piecewise<SBasis> x=Piecewise<SBasis>(patternd2[0]);\r
91     Piecewise<SBasis> y=Piecewise<SBasis>(patternd2[1]);\r
92     Interval pattBnds = bounds_exact(x);\r
93     x -= pattBnds.min();\r
94     Interval pattBndsY = bounds_exact(y);\r
95     y -= (pattBndsY.max()+pattBndsY.min())/2;\r
96 \r
97 \r
98     int nbCopies = int(uskeleton.cuts.back()/pattBnds.extent());\r
99     double scaling = 1;\r
100 \r
101     switch(type) {\r
102         case SSCT_REPEATED:\r
103             break;\r
104 \r
105         case SSCT_SINGLE:\r
106             nbCopies = (nbCopies > 0) ? 1 : 0;\r
107             break;\r
108 \r
109         case SSCT_SINGLE_STRETCHED:\r
110             nbCopies = 1;\r
111             scaling = uskeleton.cuts.back()/pattBnds.extent();\r
112             break;\r
113 \r
114         case SSCT_REPEATED_STRETCHED:\r
115              scaling = uskeleton.cuts.back()/(((double)nbCopies)*pattBnds.extent());\r
116             break;\r
117 \r
118         default:\r
119             return pwd2_in;\r
120     };\r
121 \r
122     double pattWidth = pattBnds.extent() * scaling;\r
123 \r
124     if (scaling != 1)\r
125         x*=scaling;\r
126 \r
127     double offs = 0;\r
128     Piecewise<D2<SBasis> > output;\r
129     for (int i=0; i<nbCopies; i++){\r
130         output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));\r
131         offs+=pattWidth;\r
132     }\r
133     return output;\r
134 }\r
135 \r
136 \r
137 } // namespace LivePathEffect\r
138 } /* namespace Inkscape */\r
139 \r
140 /*\r
141   Local Variables:\r
142   mode:c++\r
143   c-file-style:"stroustrup"\r
144   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
145   indent-tabs-mode:nil\r
146   fill-column:99\r
147   End:\r
148 */\r
149 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r