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,0"),\r
62 copytype(_("Copytype"), _("How to shape the pattern path along the path"), "copytype", SkelCopyTypeConverter, &wr, this, SSCT_SINGLE_STRETCHED),\r
63 prop_scale(_("Scale ratio"), _("Ratio between scaling in the x and y direction of the original path"), "prop_scale", &wr, this, 1),\r
64 scale_y(_("Scale pattern y"), _("Scale the height of the pattern path with its length"), "scale_stroke_y", &wr, this, false)\r
65 {\r
66 registerParameter( dynamic_cast<Parameter *>(&pattern) );\r
67 registerParameter( dynamic_cast<Parameter *>(©type) );\r
68 registerParameter( dynamic_cast<Parameter *>(&prop_scale) );\r
69 registerParameter( dynamic_cast<Parameter *>(&scale_y) );\r
70 }\r
71 \r
72 LPESkeletalStrokes::~LPESkeletalStrokes()\r
73 {\r
74 \r
75 }\r
76 \r
77 \r
78 Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
79 LPESkeletalStrokes::doEffect (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)\r
80 {\r
81 using namespace Geom;\r
82 \r
83 /* Much credit should go to jfb and mgsloan of lib2geom development for the code below! */\r
84 \r
85 SkelCopyType type = copytype.get_value();\r
86 \r
87 Piecewise<D2<SBasis> > uskeleton = arc_length_parametrization(Piecewise<D2<SBasis> >(pwd2_in),2,.1);\r
88 uskeleton = remove_short_cuts(uskeleton,.01);\r
89 Piecewise<D2<SBasis> > n = rot90(derivative(uskeleton));\r
90 n = force_continuity(remove_short_cuts(n,.1));\r
91 \r
92 D2<Piecewise<SBasis> > patternd2 = make_cuts_independant(pattern);\r
93 Piecewise<SBasis> x=Piecewise<SBasis>(patternd2[0]);\r
94 Piecewise<SBasis> y=Piecewise<SBasis>(patternd2[1]);\r
95 Interval pattBnds = bounds_exact(x);\r
96 x -= pattBnds.min();\r
97 Interval pattBndsY = bounds_exact(y);\r
98 y -= (pattBndsY.max()+pattBndsY.min())/2;\r
99 \r
100 int nbCopies = int(uskeleton.cuts.back()/pattBnds.extent());\r
101 double scaling = 1;\r
102 \r
103 switch(type) {\r
104 case SSCT_REPEATED:\r
105 break;\r
106 \r
107 case SSCT_SINGLE:\r
108 nbCopies = (nbCopies > 0) ? 1 : 0;\r
109 break;\r
110 \r
111 case SSCT_SINGLE_STRETCHED:\r
112 nbCopies = 1;\r
113 scaling = uskeleton.cuts.back()/pattBnds.extent();\r
114 break;\r
115 \r
116 case SSCT_REPEATED_STRETCHED:\r
117 scaling = uskeleton.cuts.back()/(((double)nbCopies)*pattBnds.extent());\r
118 break;\r
119 \r
120 default:\r
121 return pwd2_in;\r
122 };\r
123 \r
124 double pattWidth = pattBnds.extent() * scaling;\r
125 \r
126 if (scaling != 1.0) {\r
127 x*=scaling;\r
128 }\r
129 if ( scale_y.get_value() && (scaling*prop_scale != 1.0) ) {\r
130 y*=(scaling*prop_scale);\r
131 }\r
132 \r
133 double offs = 0;\r
134 Piecewise<D2<SBasis> > output;\r
135 for (int i=0; i<nbCopies; i++){\r
136 output.concat(compose(uskeleton,x+offs)+y*compose(n,x+offs));\r
137 offs+=pattWidth;\r
138 }\r
139 return output;\r
140 }\r
141 \r
142 \r
143 } // namespace LivePathEffect\r
144 } /* namespace Inkscape */\r
145 \r
146 /*\r
147 Local Variables:\r
148 mode:c++\r
149 c-file-style:"stroustrup"\r
150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
151 indent-tabs-mode:nil\r
152 fill-column:99\r
153 End:\r
154 */\r
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r