1 #define INKSCAPE_LPE_POWERSTROKE_CPP
2 /** \file
3 * @brief PowerStroke LPE implementation. Creates curves with modifiable stroke width.
4 */
5 /* Authors:
6 * Johan Engelen <j.b.c.engelen@utwente.nl>
7 *
8 * Copyright (C) 2010 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include "live_effects/lpe-powerstroke.h"
15 #include "sp-shape.h"
16 #include "display/curve.h"
18 #include <2geom/path.h>
19 #include <2geom/piecewise.h>
20 #include <2geom/sbasis-geometric.h>
21 #include <2geom/svg-elliptical-arc.h>
22 #include <2geom/transforms.h>
24 namespace Inkscape {
25 namespace LivePathEffect {
27 LPEPowerStroke::LPEPowerStroke(LivePathEffectObject *lpeobject) :
28 Effect(lpeobject),
29 offset_1(_("Start Offset"), _("Handle to control the distance of the offset from the curve"), "offset_1", &wr, this),
30 offset_2(_("End Offset"), _("Handle to control the distance of the offset from the curve"), "offset_2", &wr, this),
31 offset_3(_("End Offset"), _("Handle to control the distance of the offset from the curve"), "offset_3", &wr, this),
32 offset_points(_("Offset points"), _("Offset points"), "offset_points", &wr, this)
33 {
34 show_orig_path = true;
36 registerParameter( dynamic_cast<Parameter *>(&offset_1) );
37 registerParameter( dynamic_cast<Parameter *>(&offset_2) );
38 registerParameter( dynamic_cast<Parameter *>(&offset_3) );
39 registerParameter( dynamic_cast<Parameter *>(&offset_points) );
40 }
42 LPEPowerStroke::~LPEPowerStroke()
43 {
45 }
48 void
49 LPEPowerStroke::doOnApply(SPLPEItem *lpeitem)
50 {
51 offset_1.param_set_and_write_new_value(*(SP_SHAPE(lpeitem)->curve->first_point()));
52 offset_2.param_set_and_write_new_value(*(SP_SHAPE(lpeitem)->curve->last_point()));
53 offset_3.param_set_and_write_new_value(*(SP_SHAPE(lpeitem)->curve->last_point()));
54 }
56 static void append_half_circle(Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
57 Geom::Point const center, Geom::Point const &dir) {
58 using namespace Geom;
60 double r = L2(dir);
61 SVGEllipticalArc cap(center + dir, r, r, angle_between(Point(1,0), dir), false, false, center - dir);
62 Piecewise<D2<SBasis> > cap_pwd2(cap.toSBasis());
63 pwd2.continuousConcat(cap_pwd2);
64 }
66 Geom::Piecewise<Geom::D2<Geom::SBasis> >
67 LPEPowerStroke::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
68 {
69 using namespace Geom;
71 double t1 = nearest_point(offset_1, pwd2_in);
72 double offset1 = L2(pwd2_in.valueAt(t1) - offset_1);
74 double t2 = nearest_point(offset_2, pwd2_in);
75 double offset2 = L2(pwd2_in.valueAt(t2) - offset_2);
77 double t3 = nearest_point(offset_3, pwd2_in);
78 double offset3 = L2(pwd2_in.valueAt(t3) - offset_3);
80 /*
81 unsigned number_of_points = offset_points.data.size();
82 double* t = new double[number_of_points];
83 Point* offset = new double[number_of_points];
84 for (unsigned i = 0; i < number_of_points; ++i) {
85 t[i] = nearest_point(offset_points.data[i], pwd2_in);
86 Point A = pwd2_in.valueAt(t[i]);
87 offset[i] = L2(A - offset_points.data[i]);
88 }
89 */
91 // create stroke path where points (x,y) = (t, offset)
92 Path strokepath;
93 strokepath.start( Point(pwd2_in.domain().min(),0) );
94 strokepath.appendNew<Geom::LineSegment>( Point(t1, offset1) );
95 strokepath.appendNew<Geom::LineSegment>( Point(t2, offset2) );
96 strokepath.appendNew<Geom::LineSegment>( Point(t3, offset3) );
97 strokepath.appendNew<Geom::LineSegment>( Point(pwd2_in.domain().max(), 0) );
98 strokepath.appendNew<Geom::LineSegment>( Point(t3, -offset3) );
99 strokepath.appendNew<Geom::LineSegment>( Point(t2, -offset2) );
100 strokepath.appendNew<Geom::LineSegment>( Point(t1, -offset1) );
101 strokepath.close();
103 D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(strokepath.toPwSb());
104 Piecewise<SBasis> x = Piecewise<SBasis>(patternd2[0]);
105 Piecewise<SBasis> y = Piecewise<SBasis>(patternd2[1]);
107 Piecewise<D2<SBasis> > der = unitVector(derivative(pwd2_in));
108 Piecewise<D2<SBasis> > n = rot90(der);
110 // output = pwd2_in + n * offset;
111 // append_half_circle(output, pwd2_in.lastValue(), n.lastValue() * offset);
112 // output.continuousConcat(reverse(pwd2_in - n * offset));
113 // append_half_circle(output, pwd2_in.firstValue(), -n.firstValue() * offset);
115 Piecewise<D2<SBasis> > output = compose(pwd2_in,x) + y*compose(n,x);
116 return output;
117 }
119 /* ######################## */
121 } //namespace LivePathEffect
122 } /* namespace Inkscape */
124 /*
125 Local Variables:
126 mode:c++
127 c-file-style:"stroustrup"
128 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
129 indent-tabs-mode:nil
130 fill-column:99
131 End:
132 */
133 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :