1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3 * LPE <perp_bisector> implementation.
4 */
5 /*
6 * Authors:
7 * Maximilian Albert
8 * Johan Engelen
9 *
10 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
11 * Copyright (C) Maximilin Albert 2008 <maximilian.albert@gmail.com>
12 *
13 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14 */
15 #include "live_effects/lpe-perp_bisector.h"
16 #include "display/curve.h"
17 #include "line-geometry.h"
18
19 #include "object/sp-path.h"
20
21 #include "ui/knot/knot-holder.h"
22 #include "ui/knot/knot-holder-entity.h"
23
24 // TODO due to internal breakage in glibmm headers, this must be last:
25 #include <glibmm/i18n.h>
26
27 namespace Inkscape {
28 namespace LivePathEffect {
29 namespace PB {
30
31 class KnotHolderEntityEnd : public LPEKnotHolderEntity {
32 public:
KnotHolderEntityEnd(LPEPerpBisector * effect)33 KnotHolderEntityEnd(LPEPerpBisector *effect) : LPEKnotHolderEntity(effect) {};
34 void bisector_end_set(Geom::Point const &p, guint state, bool left = true);
35 };
36
37 class KnotHolderEntityLeftEnd : public KnotHolderEntityEnd {
38 public:
KnotHolderEntityLeftEnd(LPEPerpBisector * effect)39 KnotHolderEntityLeftEnd(LPEPerpBisector *effect) : KnotHolderEntityEnd(effect) {};
40 void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state) override;
41 Geom::Point knot_get() const override;
42 };
43
44 class KnotHolderEntityRightEnd : public KnotHolderEntityEnd {
45 public:
KnotHolderEntityRightEnd(LPEPerpBisector * effect)46 KnotHolderEntityRightEnd(LPEPerpBisector *effect) : KnotHolderEntityEnd(effect) {};
47 void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state) override;
48 Geom::Point knot_get() const override;
49 };
50
51 Geom::Point
knot_get() const52 KnotHolderEntityLeftEnd::knot_get() const {
53 LPEPerpBisector const* lpe = dynamic_cast<LPEPerpBisector const*>(_effect);
54 return Geom::Point(lpe->C);
55 }
56
57 Geom::Point
knot_get() const58 KnotHolderEntityRightEnd::knot_get() const {
59 LPEPerpBisector const* lpe = dynamic_cast<LPEPerpBisector const*>(_effect);
60 return Geom::Point(lpe->D);
61 }
62
63 void
bisector_end_set(Geom::Point const & p,guint state,bool left)64 KnotHolderEntityEnd::bisector_end_set(Geom::Point const &p, guint state, bool left) {
65 LPEPerpBisector *lpe = dynamic_cast<LPEPerpBisector *>(_effect);
66 if (!lpe) return;
67
68 Geom::Point const s = snap_knot_position(p, state);
69
70 double lambda = Geom::nearest_time(s, lpe->M, lpe->perp_dir);
71 if (left) {
72 lpe->C = lpe->M + lpe->perp_dir * lambda;
73 lpe->length_left.param_set_value(lambda);
74 } else {
75 lpe->D = lpe->M + lpe->perp_dir * lambda;
76 lpe->length_right.param_set_value(-lambda);
77 }
78
79 // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
80 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), true, true);
81 }
82
83 void
knot_set(Geom::Point const & p,Geom::Point const &,guint state)84 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state) {
85 bisector_end_set(p, state);
86 }
87
88 void
knot_set(Geom::Point const & p,Geom::Point const &,guint state)89 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state) {
90 bisector_end_set(p, state, false);
91 }
92
93 } //namescape PB
94
LPEPerpBisector(LivePathEffectObject * lpeobject)95 LPEPerpBisector::LPEPerpBisector(LivePathEffectObject *lpeobject) :
96 Effect(lpeobject),
97 length_left(_("Length left:"), _("Specifies the left end of the bisector"), "length-left", &wr, this, 200),
98 length_right(_("Length right:"), _("Specifies the right end of the bisector"), "length-right", &wr, this, 200),
99 A(0,0), B(0,0), M(0,0), C(0,0), D(0,0), perp_dir(0,0)
100 {
101 show_orig_path = true;
102 _provides_knotholder_entities = true;
103
104 // register all your parameters here, so Inkscape knows which parameters this effect has:
105 registerParameter(&length_left);
106 registerParameter(&length_right);
107 }
108
109 LPEPerpBisector::~LPEPerpBisector()
110 = default;
111
112 void
doOnApply(SPLPEItem const *)113 LPEPerpBisector::doOnApply (SPLPEItem const*/*lpeitem*/)
114 {
115 /* make the path a straight line */
116 /**
117 SPCurve* curve = sp_path_getCurveForEdit (SP_PATH(lpeitem)); // TODO: Should we use sp_shape_get_curve()?
118
119 Geom::Point A(curve->first_point());
120 Geom::Point B(curve->last_point());
121
122 SPCurve *c = new SPCurve();
123 c->moveto(A);
124 c->lineto(B);
125 // TODO: Why doesn't sp_path_set_curve_before_LPE(SP_PATH(lpeitem), c, TRUE, true) work?
126 SP_PATH(lpeitem)->original_curve = c->ref();
127 c->unref();
128 **/
129 }
130
131
132 Geom::Piecewise<Geom::D2<Geom::SBasis> >
doEffect_pwd2(Geom::Piecewise<Geom::D2<Geom::SBasis>> const & pwd2_in)133 LPEPerpBisector::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
134 {
135 using namespace Geom;
136
137 Piecewise<D2<SBasis> > output;
138
139 A = pwd2_in.firstValue();
140 B = pwd2_in.lastValue();
141 M = (A + B)/2;
142
143 perp_dir = unit_vector((B - A).ccw());
144
145 C = M + perp_dir * length_left;
146 D = M - perp_dir * length_right;
147
148 output = Piecewise<D2<SBasis> >(D2<SBasis>(SBasis(C[X], D[X]), SBasis(C[Y], D[Y])));
149
150 return output;
151 }
152
153 void
addKnotHolderEntities(KnotHolder * knotholder,SPDesktop * desktop,SPItem * item)154 LPEPerpBisector::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item) {
155 {
156 KnotHolderEntity *e = new PB::KnotHolderEntityLeftEnd(this);
157 e->create(desktop, item, knotholder, Inkscape::CANVAS_ITEM_CTRL_TYPE_LPE, "LPE:PerpBisectorLeftEnd",
158 _("Adjust the \"left\" end of the bisector"));
159 knotholder->add(e);
160 }
161 {
162 KnotHolderEntity *e = new PB::KnotHolderEntityRightEnd(this);
163 e->create(desktop, item, knotholder, Inkscape::CANVAS_ITEM_CTRL_TYPE_LPE, "LPE:PerpBisectorRightEnd",
164 _("Adjust the \"right\" end of the bisector"));
165 knotholder->add(e);
166 }
167 };
168
169 /* ######################## */
170
171 } //namespace LivePathEffect
172 } /* namespace Inkscape */
173
174 /*
175 Local Variables:
176 mode:c++
177 c-file-style:"stroustrup"
178 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
179 indent-tabs-mode:nil
180 fill-column:99
181 End:
182 */
183 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
184