Code

powerstroke: sort point option
[inkscape.git] / src / live_effects / lpe-powerstroke.cpp
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_points(_("Offset points"), _("Offset points"), "offset_points", &wr, this),
30     sort_points(_("Sort points"), _("Sort offset points according to their time value along the curve."), "sort_points", &wr, this, true)
31 {
32     show_orig_path = true;
34     registerParameter( dynamic_cast<Parameter *>(&offset_points) );
35     registerParameter( dynamic_cast<Parameter *>(&sort_points) );
36 }
38 LPEPowerStroke::~LPEPowerStroke()
39 {
41 }
44 void
45 LPEPowerStroke::doOnApply(SPLPEItem *lpeitem)
46 {
47     std::vector<Geom::Point> points;
48     points.push_back( *(SP_SHAPE(lpeitem)->curve->first_point()) );
49     Geom::Path const *path = SP_SHAPE(lpeitem)->curve->first_path();
50     points.push_back( path->pointAt(path->size()/2) );
51     points.push_back( *(SP_SHAPE(lpeitem)->curve->last_point()) );
52     offset_points.param_set_and_write_new_value(points);
53 }
55 static void append_half_circle(Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
56                                Geom::Point const center, Geom::Point const &dir) {
57     using namespace Geom;
59     double r = L2(dir);
60     SVGEllipticalArc cap(center + dir, r, r, angle_between(Point(1,0), dir), false, false, center - dir);
61     Piecewise<D2<SBasis> > cap_pwd2(cap.toSBasis());
62     pwd2.continuousConcat(cap_pwd2);
63 }
65 static bool compare_offsets (Geom::Point first, Geom::Point second)
66 {
67     return first[Geom::X] <= second[Geom::X];
68 }
71 Geom::Piecewise<Geom::D2<Geom::SBasis> >
72 LPEPowerStroke::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
73 {
74     using namespace Geom;
76     // perhaps use std::list instead of std::vector?
77     std::vector<Geom::Point> ts(offset_points.data().size());
79     for (unsigned int i; i < ts.size(); ++i) {
80         double t = nearest_point(offset_points.data().at(i), pwd2_in);
81         double offset = L2(pwd2_in.valueAt(t) - offset_points.data().at(i));
82         ts.at(i) = Geom::Point(t, offset);
83     }
84     if (sort_points) {
85         sort(ts.begin(), ts.end(), compare_offsets);
86     }
88     // create stroke path where points (x,y) = (t, offset)
89     Path strokepath;
90     strokepath.start( Point(pwd2_in.domain().min(),0) );
91     for (unsigned int i = 0 ; i < ts.size(); ++i) {
92         strokepath.appendNew<Geom::LineSegment>(ts.at(i));
93     }
94     strokepath.appendNew<Geom::LineSegment>( Point(pwd2_in.domain().max(), 0) );
95     for (unsigned int i = 0; i < ts.size(); ++i) {
96         Geom::Point temp = ts.at(ts.size() - 1 - i);
97         strokepath.appendNew<Geom::LineSegment>( Geom::Point(temp[X], - temp[Y]) );
98     }
99     strokepath.close();
101     D2<Piecewise<SBasis> > patternd2 = make_cuts_independent(strokepath.toPwSb());
102     Piecewise<SBasis> x = Piecewise<SBasis>(patternd2[0]);
103     Piecewise<SBasis> y = Piecewise<SBasis>(patternd2[1]);
105     Piecewise<D2<SBasis> > der = unitVector(derivative(pwd2_in));
106     Piecewise<D2<SBasis> > n   = rot90(der);
108 //    output  = pwd2_in + n * offset;
109 //    append_half_circle(output, pwd2_in.lastValue(), n.lastValue() * offset);
110 //    output.continuousConcat(reverse(pwd2_in - n * offset));
111 //    append_half_circle(output, pwd2_in.firstValue(), -n.firstValue() * offset);
113     Piecewise<D2<SBasis> > output = compose(pwd2_in,x) + y*compose(n,x);
114     return output;
117 /* ######################## */
119 } //namespace LivePathEffect
120 } /* namespace Inkscape */
122 /*
123   Local Variables:
124   mode:c++
125   c-file-style:"stroustrup"
126   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
127   indent-tabs-mode:nil
128   fill-column:99
129   End:
130 */
131 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :