1 #define INKSCAPE_LPE_TANGENT_TO_CURVE_CPP
2 /** \file
3 * Implementation of tangent-to-curve LPE.
4 */
6 /*
7 * Authors:
8 * Johan Engelen
9 * Maximilian Albert
10 *
11 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
12 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include "live_effects/lpe-tangent_to_curve.h"
18 // FIXME: The following are only needed to convert the path's SPCurve* to pwd2.
19 // There must be a more convenient way to achieve this.
20 #include "sp-path.h"
21 #include "display/curve.h"
22 #include "libnr/n-art-bpath-2geom.h"
24 #include <2geom/path.h>
26 namespace Inkscape {
27 namespace LivePathEffect {
29 /* FIXME: We should arguably make these member functions of LPETangentToCurve.
30 Is there an easy way to register member functions with knotholder?
31 */
32 NR::Point attach_pt_get(SPItem *item) {
33 Inkscape::LivePathEffect::LPETangentToCurve *lpe =
34 (Inkscape::LivePathEffect::LPETangentToCurve *) sp_lpe_item_get_livepatheffect(SP_LPE_ITEM(item));
36 return lpe->ptA;
37 }
39 void attach_pt_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state) {
40 Inkscape::LivePathEffect::LPETangentToCurve *lpe =
41 (Inkscape::LivePathEffect::LPETangentToCurve *) sp_lpe_item_get_livepatheffect(SP_LPE_ITEM(item));
43 using namespace Geom;
45 // FIXME: There must be a better way of converting the path's SPCurve* to pwd2.
46 SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(item));
47 const NArtBpath *bpath = curve->get_bpath();
48 Piecewise<D2<SBasis> > pwd2;
49 std::vector<Geom::Path> pathv = BPath_to_2GeomPath(bpath);
50 for (unsigned int i=0; i < pathv.size(); i++) {
51 pwd2.concat(pathv[i].toPwSb());
52 }
54 double t0 = nearest_point(p.to_2geom(), pwd2);
55 lpe->t_attach.param_set_value(t0);
57 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), true);
58 }
60 LPETangentToCurve::LPETangentToCurve(LivePathEffectObject *lpeobject) :
61 Effect(lpeobject),
62 t_attach(_("Location along curve"), _("Location of the point of attachment along the curve (between 0.0 and number-of-segments)"), "t_attach", &wr, this, 0.5)
63 {
64 registerParameter( dynamic_cast<Parameter *>(&t_attach) );
65 registerKnotHolderHandle(attach_pt_set, attach_pt_get);
66 }
68 LPETangentToCurve::~LPETangentToCurve()
69 {
70 }
72 Geom::Piecewise<Geom::D2<Geom::SBasis> >
73 LPETangentToCurve::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
74 {
75 using namespace Geom;
76 Piecewise<D2<SBasis> > output;
78 ptA = pwd2_in.valueAt(t_attach);
79 derivA = unit_vector(derivative(pwd2_in).valueAt(t_attach));
81 Point A = ptA - derivA * 100;
82 Point B = ptA + derivA * 100;
84 output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(A[X], B[X]), Linear(A[Y], B[Y])));
86 return output;
87 }
89 } //namespace LivePathEffect
90 } /* namespace Inkscape */
92 /*
93 Local Variables:
94 mode:c++
95 c-file-style:"stroustrup"
96 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
97 indent-tabs-mode:nil
98 fill-column:99
99 End:
100 */
101 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :