Code

Make curvature work again by fixing a minor omission
[inkscape.git] / src / live_effects / lpe-angle_bisector.cpp
1 #define INKSCAPE_LPE_ANGLE_BISECTOR_CPP
2 /** \file
3  * LPE <angle_bisector> implementation, used as an example for a base starting class
4  * when implementing new LivePathEffects.
5  *
6  * In vi, three global search-and-replaces will let you rename everything
7  * in this and the .h file:
8  *
9  *   :%s/ANGLE_BISECTOR/YOURNAME/g
10  *   :%s/AngleBisector/Yourname/g
11  *   :%s/angle_bisector/yourname/g
12  */
13 /*
14  * Authors:
15  *   Johan Engelen
16  *
17  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
18  *
19  * Released under GNU GPL, read the file 'COPYING' for more information
20  */
22 #include "live_effects/lpe-angle_bisector.h"
24 // You might need to include other 2geom files. You can add them here:
25 #include <2geom/path.h>
26 #include <2geom/sbasis-to-bezier.h>
28 namespace Inkscape {
29 namespace LivePathEffect {
31 namespace AB {
33 class KnotHolderEntityLeftEnd : public LPEKnotHolderEntity
34 {
35 public:
36     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
37     virtual Geom::Point knot_get();
38 };
40 class KnotHolderEntityRightEnd : public LPEKnotHolderEntity
41 {
42 public:
43     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
44     virtual Geom::Point knot_get();
45 };
47 } // namespace TtC
49 LPEAngleBisector::LPEAngleBisector(LivePathEffectObject *lpeobject) :
50     Effect(lpeobject),
51     length_left(_("Length left"), _("Specifies the left end of the bisector"), "length-left", &wr, this, 0),
52     length_right(_("Length right"), _("Specifies the right end of the bisector"), "length-right", &wr, this, 250)
53 {
54     show_orig_path = true;
56     registerParameter( dynamic_cast<Parameter *>(&length_left) );
57     registerParameter( dynamic_cast<Parameter *>(&length_right) );
59     registerKnotHolderHandle(new AB::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the bisector"));
60     registerKnotHolderHandle(new AB::KnotHolderEntityRightEnd(), _("Adjust the \"right\" of the bisector"));
61 }
63 LPEAngleBisector::~LPEAngleBisector()
64 {
65 }
67 std::vector<Geom::Path>
68 LPEAngleBisector::doEffect_path (std::vector<Geom::Path> const & path_in)
69 {
70     using namespace Geom;
72     std::vector<Geom::Path> path_out;
74     // we assume that the path has >= 3 nodes
75     ptA = path_in[0].pointAt(1);
76     Point B = path_in[0].initialPoint();
77     Point C = path_in[0].pointAt(2);
79     double angle = angle_between(B - ptA, C - ptA);
81     dir = unit_vector(B - ptA) * Rotate(angle/2);
83     Geom::Point D = ptA - dir * length_left;
84     Geom::Point E = ptA + dir * length_right;
86     Piecewise<D2<SBasis> > output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(D[X], E[X]), Linear(D[Y], E[Y])));
88     return path_from_piecewise(output, LPE_CONVERSION_TOLERANCE);
89 }
90 namespace AB {
92 // TODO: make this more generic
93 static LPEAngleBisector *
94 get_effect(SPItem *item)
95 {
96     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
97     if (effect->effectType() != ANGLE_BISECTOR) {
98         g_print ("Warning: Effect is not of type LPEAngleBisector!\n");
99         return NULL;
100     }
101     return static_cast<LPEAngleBisector *>(effect);
104 void
105 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
107     LPEAngleBisector *lpe = get_effect(item);
108     
109     Geom::Point const s = snap_knot_position(p);
111     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->dir);
112     lpe->length_left.param_set_value(-lambda);
114     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
117 void
118 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
120     LPEAngleBisector *lpe = get_effect(item);
121     
122     Geom::Point const s = snap_knot_position(p);
124     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->dir);
125     lpe->length_right.param_set_value(lambda);
127     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
130 Geom::Point
131 KnotHolderEntityLeftEnd::knot_get()
133     LPEAngleBisector *lpe = get_effect(item);
134     return lpe->ptA - lpe->dir * lpe->length_left;
137 Geom::Point
138 KnotHolderEntityRightEnd::knot_get()
140     LPEAngleBisector *lpe = get_effect(item);
141     return lpe->ptA + lpe->dir * lpe->length_right;
144 } // namespace AB
146 /* ######################## */
148 } //namespace LivePathEffect
149 } /* namespace Inkscape */
151 /*
152   Local Variables:
153   mode:c++
154   c-file-style:"stroustrup"
155   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156   indent-tabs-mode:nil
157   fill-column:99
158   End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :