Code

Extensions. Add option to choose dxf output units
[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 #include "sp-lpe-item.h"
30 namespace Inkscape {
31 namespace LivePathEffect {
33 namespace AB {
35 class KnotHolderEntityLeftEnd : public LPEKnotHolderEntity
36 {
37 public:
38     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
39     virtual Geom::Point knot_get();
40 };
42 class KnotHolderEntityRightEnd : public LPEKnotHolderEntity
43 {
44 public:
45     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
46     virtual Geom::Point knot_get();
47 };
49 } // namespace TtC
51 LPEAngleBisector::LPEAngleBisector(LivePathEffectObject *lpeobject) :
52     Effect(lpeobject),
53     length_left(_("Length left"), _("Specifies the left end of the bisector"), "length-left", &wr, this, 0),
54     length_right(_("Length right"), _("Specifies the right end of the bisector"), "length-right", &wr, this, 250)
55 {
56     show_orig_path = true;
58     registerParameter( dynamic_cast<Parameter *>(&length_left) );
59     registerParameter( dynamic_cast<Parameter *>(&length_right) );
61     registerKnotHolderHandle(new AB::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the bisector"));
62     registerKnotHolderHandle(new AB::KnotHolderEntityRightEnd(), _("Adjust the \"right\" of the bisector"));
63 }
65 LPEAngleBisector::~LPEAngleBisector()
66 {
67 }
69 std::vector<Geom::Path>
70 LPEAngleBisector::doEffect_path (std::vector<Geom::Path> const & path_in)
71 {
72     using namespace Geom;
74     std::vector<Geom::Path> path_out;
76     // we assume that the path has >= 3 nodes
77     ptA = path_in[0].pointAt(1);
78     Point B = path_in[0].initialPoint();
79     Point C = path_in[0].pointAt(2);
81     double angle = angle_between(B - ptA, C - ptA);
83     dir = unit_vector(B - ptA) * Rotate(angle/2);
85     Geom::Point D = ptA - dir * length_left;
86     Geom::Point E = ptA + dir * length_right;
88     Piecewise<D2<SBasis> > output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(D[X], E[X]), Linear(D[Y], E[Y])));
90     return path_from_piecewise(output, LPE_CONVERSION_TOLERANCE);
91 }
92 namespace AB {
94 // TODO: make this more generic
95 static LPEAngleBisector *
96 get_effect(SPItem *item)
97 {
98     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
99     if (effect->effectType() != ANGLE_BISECTOR) {
100         g_print ("Warning: Effect is not of type LPEAngleBisector!\n");
101         return NULL;
102     }
103     return static_cast<LPEAngleBisector *>(effect);
106 void
107 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
109     LPEAngleBisector *lpe = get_effect(item);
110     
111     Geom::Point const s = snap_knot_position(p);
113     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->dir);
114     lpe->length_left.param_set_value(-lambda);
116     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
119 void
120 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
122     LPEAngleBisector *lpe = get_effect(item);
123     
124     Geom::Point const s = snap_knot_position(p);
126     double lambda = Geom::nearest_point(s, lpe->ptA, lpe->dir);
127     lpe->length_right.param_set_value(lambda);
129     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
132 Geom::Point
133 KnotHolderEntityLeftEnd::knot_get()
135     LPEAngleBisector *lpe = get_effect(item);
136     return lpe->ptA - lpe->dir * lpe->length_left;
139 Geom::Point
140 KnotHolderEntityRightEnd::knot_get()
142     LPEAngleBisector *lpe = get_effect(item);
143     return lpe->ptA + lpe->dir * lpe->length_right;
146 } // namespace AB
148 /* ######################## */
150 } //namespace LivePathEffect
151 } /* namespace Inkscape */
153 /*
154   Local Variables:
155   mode:c++
156   c-file-style:"stroustrup"
157   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
158   indent-tabs-mode:nil
159   fill-column:99
160   End:
161 */
162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :