Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[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"
23 #include <2geom/path.h>
24 #include <2geom/transforms.h>
26 namespace Inkscape {
27 namespace LivePathEffect {
29 namespace TtC {
31 class KnotHolderEntityAttachPt : public LPEKnotHolderEntity
32 {
33 public:
34     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
35     virtual Geom::Point knot_get();
36 };
38 class KnotHolderEntityLeftEnd : public LPEKnotHolderEntity
39 {
40 public:
41     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
42     virtual Geom::Point knot_get();
43 };
45 class KnotHolderEntityRightEnd : public LPEKnotHolderEntity
46 {
47 public:
48     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
49     virtual Geom::Point knot_get();
50 };
52 } // namespace TtC
54 LPETangentToCurve::LPETangentToCurve(LivePathEffectObject *lpeobject) :
55     Effect(lpeobject),
56     angle(_("Angle"), _("Additional angle between tangent and curve"), "angle", &wr, this, 0.0),
57     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),
58     length_left(_("Length left"), _("Specifies the left end of the tangent"), "length-left", &wr, this, 150),
59     length_right(_("Length right"), _("Specifies the right end of the tangent"), "length-right", &wr, this, 150)
60 {
61     show_orig_path = true;
63     registerParameter( dynamic_cast<Parameter *>(&angle) );
64     registerParameter( dynamic_cast<Parameter *>(&t_attach) );
65     registerParameter( dynamic_cast<Parameter *>(&length_left) );
66     registerParameter( dynamic_cast<Parameter *>(&length_right) );
68     registerKnotHolderHandle(new TtC::KnotHolderEntityAttachPt(), _("Adjust the point of attachment of the tangent"));
69     registerKnotHolderHandle(new TtC::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the tangent"));
70     registerKnotHolderHandle(new TtC::KnotHolderEntityRightEnd(), _("Adjust the \"right\" end of the tangent"));
71 }
73 LPETangentToCurve::~LPETangentToCurve()
74 {
75 }
77 Geom::Piecewise<Geom::D2<Geom::SBasis> >
78 LPETangentToCurve::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
79 {
80     using namespace Geom;
81     Piecewise<D2<SBasis> > output;
83     ptA = pwd2_in.valueAt(t_attach);
84     derivA = unit_vector(derivative(pwd2_in).valueAt(t_attach));
86     // TODO: Why are positive angles measured clockwise, not counterclockwise?
87     Geom::Rotate rot(Geom::Rotate::from_degrees(-angle));
88     derivA = derivA * rot;
90     C = ptA - derivA * length_left;
91     D = ptA + derivA * length_right;
93     output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
95     return output;
96 }
98 namespace TtC {
100 // TODO: make this more generic
101 static LPETangentToCurve *
102 get_effect(SPItem *item)
104     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
105     if (effect->effectType() != TANGENT_TO_CURVE) {
106         g_print ("Warning: Effect is not of type LPETangentToCurve!\n");
107         return NULL;
108     }
109     return static_cast<LPETangentToCurve *>(effect);
112 void
113 KnotHolderEntityAttachPt::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
115     using namespace Geom;
117     LPETangentToCurve* lpe = get_effect(item);
119     Geom::Point const s = snap_knot_position(p);
121     // FIXME: There must be a better way of converting the path's SPCurve* to pwd2.
122     SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(item));
123     Geom::PathVector pathv = curve->get_pathvector();
124     Piecewise<D2<SBasis> > pwd2;
125     for (unsigned int i=0; i < pathv.size(); i++) {
126         pwd2.concat(pathv[i].toPwSb());
127     }
129     double t0 = nearest_point(s, pwd2);
130     lpe->t_attach.param_set_value(t0);
132     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
133     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
136 void
137 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
139     LPETangentToCurve *lpe = get_effect(item);
141     Geom::Point const s = snap_knot_position(p);
143     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->derivA);
144     lpe->length_left.param_set_value(-lambda);
146     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
149 void
150 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
152     LPETangentToCurve *lpe = get_effect(item);
153     
154     Geom::Point const s = snap_knot_position(p);
156     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->derivA);
157     lpe->length_right.param_set_value(lambda);
159     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
162 Geom::Point
163 KnotHolderEntityAttachPt::knot_get()
165     LPETangentToCurve* lpe = get_effect(item);
166     return lpe->ptA;
169 Geom::Point
170 KnotHolderEntityLeftEnd::knot_get()
172     LPETangentToCurve *lpe = get_effect(item);
173     return lpe->C;
176 Geom::Point
177 KnotHolderEntityRightEnd::knot_get()
179     LPETangentToCurve *lpe = get_effect(item);
180     return lpe->D;
183 } // namespace TtC
185 } //namespace LivePathEffect
186 } /* namespace Inkscape */
188 /*
189   Local Variables:
190   mode:c++
191   c-file-style:"stroustrup"
192   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
193   indent-tabs-mode:nil
194   fill-column:99
195   End:
196 */
197 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :