Code

ede13e2d45cb30203e22cef7ec1df42443b37576
[inkscape.git] / src / live_effects / lpe-tangent_to_curve.cpp
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 LPEKnotHolderEntity
33 {
34 public:
35     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
36     virtual Geom::Point knot_get();
37 };
39 class KnotHolderEntityLeftEnd : public LPEKnotHolderEntity
40 {
41 public:
42     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
43     virtual Geom::Point knot_get();
44 };
46 class KnotHolderEntityRightEnd : public LPEKnotHolderEntity
47 {
48 public:
49     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
50     virtual Geom::Point knot_get();
51 };
53 } // namespace TtC
55 LPETangentToCurve::LPETangentToCurve(LivePathEffectObject *lpeobject) :
56     Effect(lpeobject),
57     angle(_("Angle"), _("Additional angle between tangent and curve"), "angle", &wr, this, 0.0),
58     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),
59     length_left(_("Length left"), _("Specifies the left end of the tangent"), "length-left", &wr, this, 150),
60     length_right(_("Length right"), _("Specifies the right end of the tangent"), "length-right", &wr, this, 150)
61 {
62     show_orig_path = true;
64     registerParameter( dynamic_cast<Parameter *>(&angle) );
65     registerParameter( dynamic_cast<Parameter *>(&t_attach) );
66     registerParameter( dynamic_cast<Parameter *>(&length_left) );
67     registerParameter( dynamic_cast<Parameter *>(&length_right) );
69     registerKnotHolderHandle(new TtC::KnotHolderEntityAttachPt(), _("Adjust the point of attachment of the tangent"));
70     registerKnotHolderHandle(new TtC::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the tangent"));
71     registerKnotHolderHandle(new TtC::KnotHolderEntityRightEnd(), _("Adjust the \"right\" end of the tangent"));
72 }
74 LPETangentToCurve::~LPETangentToCurve()
75 {
76 }
78 Geom::Piecewise<Geom::D2<Geom::SBasis> >
79 LPETangentToCurve::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
80 {
81     using namespace Geom;
82     Piecewise<D2<SBasis> > output;
84     ptA = pwd2_in.valueAt(t_attach);
85     derivA = unit_vector(derivative(pwd2_in).valueAt(t_attach));
87     // TODO: Why are positive angles measured clockwise, not counterclockwise?
88     Geom::Rotate rot(Geom::Rotate::from_degrees(-angle));
89     derivA = derivA * rot;
91     C = ptA - derivA * length_left;
92     D = ptA + derivA * length_right;
94     output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
96     return output;
97 }
99 namespace TtC {
101 // TODO: make this more generic
102 static LPETangentToCurve *
103 get_effect(SPItem *item)
105     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
106     if (effect->effectType() != TANGENT_TO_CURVE) {
107         g_print ("Warning: Effect is not of type LPETangentToCurve!\n");
108         return NULL;
109     }
110     return static_cast<LPETangentToCurve *>(effect);
113 void
114 KnotHolderEntityAttachPt::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
116     using namespace Geom;
118     LPETangentToCurve* lpe = get_effect(item);
120     Geom::Point const s = snap_knot_position(p);
122     // FIXME: There must be a better way of converting the path's SPCurve* to pwd2.
123     SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(item));
124     Geom::PathVector pathv = curve->get_pathvector();
125     Piecewise<D2<SBasis> > pwd2;
126     for (unsigned int i=0; i < pathv.size(); i++) {
127         pwd2.concat(pathv[i].toPwSb());
128     }
130     double t0 = nearest_point(s, pwd2);
131     lpe->t_attach.param_set_value(t0);
133     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
134     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
137 void
138 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
140     LPETangentToCurve *lpe = get_effect(item);
142     Geom::Point const s = snap_knot_position(p);
144     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->derivA);
145     lpe->length_left.param_set_value(-lambda);
147     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
150 void
151 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
153     LPETangentToCurve *lpe = get_effect(item);
154     
155     Geom::Point const s = snap_knot_position(p);
157     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->derivA);
158     lpe->length_right.param_set_value(lambda);
160     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
163 Geom::Point
164 KnotHolderEntityAttachPt::knot_get()
166     LPETangentToCurve* lpe = get_effect(item);
167     return lpe->ptA;
170 Geom::Point
171 KnotHolderEntityLeftEnd::knot_get()
173     LPETangentToCurve *lpe = get_effect(item);
174     return lpe->C;
177 Geom::Point
178 KnotHolderEntityRightEnd::knot_get()
180     LPETangentToCurve *lpe = get_effect(item);
181     return lpe->D;
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 :