1 #define INKSCAPE_LPE_PARALLEL_CPP
2 /** \file
3 * LPE <parallel> implementation
4 */
5 /*
6 * Authors:
7 * Maximilian Albert
8 *
9 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
10 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "live_effects/lpe-parallel.h"
16 #include "sp-shape.h"
17 #include "display/curve.h"
19 #include <2geom/path.h>
20 #include <2geom/transforms.h>
22 namespace Inkscape {
23 namespace LivePathEffect {
25 namespace Pl {
27 class KnotHolderEntityLeftEnd : public KnotHolderEntity
28 {
29 public:
30 virtual bool isLPEParam() { return true; }
32 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
33 virtual NR::Point knot_get();
34 };
36 class KnotHolderEntityRightEnd : public KnotHolderEntity
37 {
38 public:
39 virtual bool isLPEParam() { return true; }
41 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
42 virtual NR::Point knot_get();
43 };
45 } // namespace Pl
47 LPEParallel::LPEParallel(LivePathEffectObject *lpeobject) :
48 Effect(lpeobject),
49 // initialise your parameters here:
50 offset_pt(_("Offset"), _("Adjust the offset"), "offset_pt", &wr, this),
51 length_left(_("Length left"), _("Specifies the left end of the parallel"), "length-left", &wr, this, 150),
52 length_right(_("Length right"), _("Specifies the right end of the parallel"), "length-right", &wr, this, 150)
53 {
54 show_orig_path = true;
56 registerParameter(dynamic_cast<Parameter *>(&offset_pt));
57 registerParameter( dynamic_cast<Parameter *>(&length_left) );
58 registerParameter( dynamic_cast<Parameter *>(&length_right) );
60 registerKnotHolderHandle(new Pl::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the parallel"));
61 registerKnotHolderHandle(new Pl::KnotHolderEntityRightEnd(), _("Adjust the \"right\" end of the parallel"));
62 }
64 LPEParallel::~LPEParallel()
65 {
67 }
69 void
70 LPEParallel::doOnApply (SPLPEItem *lpeitem)
71 {
72 SPCurve *curve = SP_SHAPE(lpeitem)->curve;
74 A = curve->first_point().to_2geom();
75 B = curve->last_point().to_2geom();
76 dir = unit_vector(B - A);
78 offset_pt.param_set_and_write_new_value((A + B)/2 + dir.ccw() * 100);
79 }
81 Geom::Piecewise<Geom::D2<Geom::SBasis> >
82 LPEParallel::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
83 {
84 using namespace Geom;
86 Piecewise<D2<SBasis> > output;
88 A = pwd2_in.firstValue();
89 B = pwd2_in.lastValue();
90 dir = unit_vector(B - A);
92 C = offset_pt - dir * length_left;
93 D = offset_pt + dir * length_right;
95 output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
97 return output + dir;
98 }
100 namespace Pl {
102 // TODO: make this more generic
103 static LPEParallel *
104 get_effect(SPItem *item)
105 {
106 Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
107 if (effect->effectType() != PARALLEL) {
108 g_print ("Warning: Effect is not of type LPEParallel!\n");
109 return NULL;
110 }
111 return static_cast<LPEParallel *>(effect);
112 }
114 void
115 KnotHolderEntityLeftEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
116 {
117 using namespace Geom;
119 LPEParallel *lpe = get_effect(item);
121 double lambda = L2(p - lpe->offset_pt) * sgn(dot(p.to_2geom() - lpe->offset_pt, lpe->dir));
122 lpe->length_left.param_set_value(-lambda);
124 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
125 }
127 void
128 KnotHolderEntityRightEnd::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
129 {
130 using namespace Geom;
132 LPEParallel *lpe = get_effect(item);
134 double lambda = L2(p - lpe->offset_pt) * sgn(dot(p.to_2geom() - lpe->offset_pt, lpe->dir));
135 lpe->length_right.param_set_value(lambda);
137 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
138 }
140 NR::Point
141 KnotHolderEntityLeftEnd::knot_get()
142 {
143 LPEParallel *lpe = get_effect(item);
144 return lpe->C;
145 }
147 NR::Point
148 KnotHolderEntityRightEnd::knot_get()
149 {
150 LPEParallel *lpe = get_effect(item);
151 return lpe->D;
152 }
154 } // namespace Pl
156 /* ######################## */
158 } //namespace LivePathEffect
159 } /* namespace Inkscape */
161 /*
162 Local Variables:
163 mode:c++
164 c-file-style:"stroustrup"
165 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166 indent-tabs-mode:nil
167 fill-column:99
168 End:
169 */
170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :