Code

Store cached icons to disk between runs, and invalidate/purge as needed.
[inkscape.git] / src / live_effects / lpe-parallel.cpp
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 LPEKnotHolderEntity
28 {
29 public:
30     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
31     virtual Geom::Point knot_get();
32 };
34 class KnotHolderEntityRightEnd : public LPEKnotHolderEntity
35 {
36 public:
37     virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
38     virtual Geom::Point knot_get();
39 };
41 } // namespace Pl
43 LPEParallel::LPEParallel(LivePathEffectObject *lpeobject) :
44     Effect(lpeobject),
45     // initialise your parameters here:
46     offset_pt(_("Offset"), _("Adjust the offset"), "offset_pt", &wr, this),
47     length_left(_("Length left"), _("Specifies the left end of the parallel"), "length-left", &wr, this, 150),
48     length_right(_("Length right"), _("Specifies the right end of the parallel"), "length-right", &wr, this, 150)
49 {
50     show_orig_path = true;
52     registerParameter(dynamic_cast<Parameter *>(&offset_pt));
53     registerParameter( dynamic_cast<Parameter *>(&length_left) );
54     registerParameter( dynamic_cast<Parameter *>(&length_right) );
56     registerKnotHolderHandle(new Pl::KnotHolderEntityLeftEnd(), _("Adjust the \"left\" end of the parallel"));
57     registerKnotHolderHandle(new Pl::KnotHolderEntityRightEnd(), _("Adjust the \"right\" end of the parallel"));
58 }
60 LPEParallel::~LPEParallel()
61 {
63 }
65 void
66 LPEParallel::doOnApply (SPLPEItem *lpeitem)
67 {
68     SPCurve *curve = SP_SHAPE(lpeitem)->curve;
70     A = *(curve->first_point());
71     B = *(curve->last_point());
72     dir = unit_vector(B - A);
74     offset_pt.param_set_and_write_new_value((A + B)/2 + dir.ccw() * 100);
75 }
77 Geom::Piecewise<Geom::D2<Geom::SBasis> >
78 LPEParallel::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
79 {
80     using namespace Geom;
82     Piecewise<D2<SBasis> > output;
84     A = pwd2_in.firstValue();
85     B = pwd2_in.lastValue();
86     dir = unit_vector(B - A);
88     C = offset_pt - dir * length_left;
89     D = offset_pt + dir * length_right;
91     output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
92     
93     return output + dir;
94 }
96 namespace Pl {
98 // TODO: make this more generic
99 static LPEParallel *
100 get_effect(SPItem *item)
102     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
103     if (effect->effectType() != PARALLEL) {
104         g_print ("Warning: Effect is not of type LPEParallel!\n");
105         return NULL;
106     }
107     return static_cast<LPEParallel *>(effect);
110 void
111 KnotHolderEntityLeftEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
113     using namespace Geom;
115     LPEParallel *lpe = get_effect(item);
116     
117     Geom::Point const s = snap_knot_position(p);
119     double lambda = L2(s - lpe->offset_pt) * sgn(dot(s - lpe->offset_pt, lpe->dir));
120     lpe->length_left.param_set_value(-lambda);
122     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
125 void
126 KnotHolderEntityRightEnd::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
128     using namespace Geom;
130     LPEParallel *lpe = get_effect(item);
131     
132     Geom::Point const s = snap_knot_position(p);
134     double lambda = L2(s - lpe->offset_pt) * sgn(dot(s - 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);
140 Geom::Point
141 KnotHolderEntityLeftEnd::knot_get()
143     LPEParallel *lpe = get_effect(item);
144     return lpe->C;
147 Geom::Point
148 KnotHolderEntityRightEnd::knot_get()
150     LPEParallel *lpe = get_effect(item);
151     return lpe->D;
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 :