Code

Rename LPE: mirror reflect --> mirror symmetry
[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 KnotHolderEntity
34 {
35 public:
36     virtual bool isLPEParam() { return true; }
38     virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
39     virtual NR::Point knot_get();
40 };
42 class KnotHolderEntityRightEnd : public KnotHolderEntity
43 {
44 public:
45     virtual bool isLPEParam() { return true; }
47     virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
48     virtual NR::Point knot_get();
49 };
51 } // namespace TtC
53 LPEAngleBisector::LPEAngleBisector(LivePathEffectObject *lpeobject) :
54     Effect(lpeobject),
55     length_left(_("Length left"), _("Specifies the left end of the bisector"), "length-left", &wr, this, 0),
56     length_right(_("Length right"), _("Specifies the right end of the bisector"), "length-right", &wr, this, 250)
57 {
58     show_orig_path = true;
60     registerParameter( dynamic_cast<Parameter *>(&length_left) );
61     registerParameter( dynamic_cast<Parameter *>(&length_right) );
63     /* we disable the handles until we support both knotholders and nodepaths */
64     //registerKnotHolderHandle(new AB::KnotHolderEntityLeftEnd(), _("Adjust the \"right\" end of the bisector"));
65     //registerKnotHolderHandle(new AB::KnotHolderEntityRightEnd(), _("Adjust the point of attachment of the bisector"));
66 }
68 LPEAngleBisector::~LPEAngleBisector()
69 {
70 }
72 std::vector<Geom::Path>
73 LPEAngleBisector::doEffect_path (std::vector<Geom::Path> const & path_in)
74 {
75     using namespace Geom;
77     std::vector<Geom::Path> path_out;
79     // we assume that the path has >= 3 nodes
80     ptA = path_in[0].pointAt(1);
81     Point B = path_in[0].initialPoint();
82     Point C = path_in[0].pointAt(2);
84     double angle = angle_between(B - ptA, C - ptA);
86     dir = unit_vector(B - ptA) * Rotate(angle/2);
88     Geom::Point D = ptA - dir * length_left;
89     Geom::Point E = ptA + dir * length_right;
91     Piecewise<D2<SBasis> > output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(D[X], E[X]), Linear(D[Y], E[Y])));
93     return path_from_piecewise(output, LPE_CONVERSION_TOLERANCE);
94 }
95 namespace AB {
97 // TODO: make this more generic
98 static LPEAngleBisector *
99 get_effect(SPItem *item)
101     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
102     if (effect->effectType() != ANGLE_BISECTOR) {
103         g_print ("Warning: Effect is not of type LPEAngleBisector!\n");
104         return NULL;
105     }
106     return static_cast<LPEAngleBisector *>(effect);
109 void
110 KnotHolderEntityLeftEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
112     LPEAngleBisector *lpe = get_effect(item);
113     
114     double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->dir);
115     lpe->length_left.param_set_value(-lambda);
117     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
120 void
121 KnotHolderEntityRightEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
123     LPEAngleBisector *lpe = get_effect(item);
124     
125     double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->dir);
126     lpe->length_right.param_set_value(lambda);
128     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
131 NR::Point
132 KnotHolderEntityLeftEnd::knot_get()
134     LPEAngleBisector *lpe = get_effect(item);
135     return lpe->ptA - lpe->dir * lpe->length_left;
138 NR::Point
139 KnotHolderEntityRightEnd::knot_get()
141     LPEAngleBisector *lpe = get_effect(item);
142     return lpe->ptA + lpe->dir * lpe->length_right;
145 } // namespace AB
147 /* ######################## */
149 } //namespace LivePathEffect
150 } /* namespace Inkscape */
152 /*
153   Local Variables:
154   mode:c++
155   c-file-style:"stroustrup"
156   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157   indent-tabs-mode:nil
158   fill-column:99
159   End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :