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>
25 #include <2geom/transforms.h>
27 namespace Inkscape {
28 namespace LivePathEffect {
30 namespace TtC {
32 class KnotHolderEntityAttachPt : public KnotHolderEntity
33 {
34 public:
35 virtual bool isLPEParam() { return true; }
37 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
38 virtual NR::Point knot_get();
39 };
41 class KnotHolderEntityLeftEnd : public KnotHolderEntity
42 {
43 public:
44 virtual bool isLPEParam() { return true; }
46 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
47 virtual NR::Point knot_get();
48 };
50 class KnotHolderEntityRightEnd : public KnotHolderEntity
51 {
52 public:
53 virtual bool isLPEParam() { return true; }
55 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
56 virtual NR::Point knot_get();
57 };
59 } // namespace TtC
61 LPETangentToCurve::LPETangentToCurve(LivePathEffectObject *lpeobject) :
62 Effect(lpeobject),
63 angle(_("Angle"), _("Additional angle between tangent and curve"), "angle", &wr, this, 0.0),
64 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),
65 length_left(_("Length left"), _("Specifies the left end of the tangent"), "length-left", &wr, this, 150),
66 length_right(_("Length right"), _("Specifies the right end of the tangent"), "length-right", &wr, this, 150)
67 {
68 show_orig_path = true;
70 registerParameter( dynamic_cast<Parameter *>(&angle) );
71 registerParameter( dynamic_cast<Parameter *>(&t_attach) );
72 registerParameter( dynamic_cast<Parameter *>(&length_left) );
73 registerParameter( dynamic_cast<Parameter *>(&length_right) );
75 registerKnotHolderHandle(new TtC::KnotHolderEntityAttachPt(), _("Adjust the \"left\" end of the tangent"));
76 registerKnotHolderHandle(new TtC::KnotHolderEntityLeftEnd(), _("Adjust the \"right\" end of the tangent"));
77 registerKnotHolderHandle(new TtC::KnotHolderEntityRightEnd(), _("Adjust the point of attachment of the tangent"));
78 }
80 LPETangentToCurve::~LPETangentToCurve()
81 {
82 }
84 Geom::Piecewise<Geom::D2<Geom::SBasis> >
85 LPETangentToCurve::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
86 {
87 using namespace Geom;
88 Piecewise<D2<SBasis> > output;
90 ptA = pwd2_in.valueAt(t_attach);
91 derivA = unit_vector(derivative(pwd2_in).valueAt(t_attach));
93 // TODO: Why are positive angles measured clockwise, not counterclockwise?
94 Geom::Rotate rot(Geom::Rotate::from_degrees(-angle));
95 derivA = derivA * rot;
97 C = ptA - derivA * length_left;
98 D = ptA + derivA * length_right;
100 output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
102 return output;
103 }
105 namespace TtC {
107 // TODO: make this more generic
108 static LPETangentToCurve *
109 get_effect(SPItem *item)
110 {
111 Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
112 if (effect->effectType() != TANGENT_TO_CURVE) {
113 g_print ("Warning: Effect is not of type LPETangentToCurve!\n");
114 return NULL;
115 }
116 return static_cast<LPETangentToCurve *>(effect);
117 }
119 void
120 KnotHolderEntityAttachPt::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
121 {
122 using namespace Geom;
124 LPETangentToCurve* lpe = get_effect(item);
126 // FIXME: There must be a better way of converting the path's SPCurve* to pwd2.
127 SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(item));
128 Geom::PathVector pathv = curve->get_pathvector();
129 Piecewise<D2<SBasis> > pwd2;
130 for (unsigned int i=0; i < pathv.size(); i++) {
131 pwd2.concat(pathv[i].toPwSb());
132 }
134 double t0 = nearest_point(p.to_2geom(), pwd2);
135 lpe->t_attach.param_set_value(t0);
137 // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
138 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
139 }
141 void
142 KnotHolderEntityLeftEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
143 {
144 LPETangentToCurve *lpe = get_effect(item);
146 double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->derivA);
147 lpe->length_left.param_set_value(-lambda);
149 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
150 }
152 void
153 KnotHolderEntityRightEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
154 {
155 LPETangentToCurve *lpe = get_effect(item);
157 double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->derivA);
158 lpe->length_right.param_set_value(lambda);
160 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
161 }
163 NR::Point
164 KnotHolderEntityAttachPt::knot_get()
165 {
166 LPETangentToCurve* lpe = get_effect(item);
167 return lpe->ptA;
168 }
170 NR::Point
171 KnotHolderEntityLeftEnd::knot_get()
172 {
173 LPETangentToCurve *lpe = get_effect(item);
174 return lpe->C;
175 }
177 NR::Point
178 KnotHolderEntityRightEnd::knot_get()
179 {
180 LPETangentToCurve *lpe = get_effect(item);
181 return lpe->D;
182 }
184 } // namespace TtC
186 } //namespace LivePathEffect
187 } /* namespace Inkscape */
189 /*
190 Local Variables:
191 mode:c++
192 c-file-style:"stroustrup"
193 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
194 indent-tabs-mode:nil
195 fill-column:99
196 End:
197 */
198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :