Code

c5c7818c07ed4a5284d6bc045ce797c3cf8e4c88
[inkscape.git] / src / live_effects / lpe-copy_rotate.cpp
1 #define INKSCAPE_LPE_COPY_ROTATE_CPP
2 /** \file
3  * LPE <copy_rotate> 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-copy_rotate.h"
16 #include "sp-shape.h"
17 #include "display/curve.h"
19 #include <2geom/path.h>
20 #include <2geom/transforms.h>
21 #include <2geom/d2-sbasis.h>
23 namespace Inkscape {
24 namespace LivePathEffect {
26 namespace CR {
28 class KnotHolderEntityAngle : public KnotHolderEntity
29 {
30 public:
31     virtual bool isLPEParam() { return true; }
33     virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
34     virtual NR::Point knot_get();
35 };
37 } // namespace CR
39 LPECopyRotate::LPECopyRotate(LivePathEffectObject *lpeobject) :
40     Effect(lpeobject),
41     angle(_("Angle"), _("Angle"), "angle", &wr, this, 30.0),
42     num_copies(_("Number of copies"), _("Number of copies of the original path"), "num_copies", &wr, this, 1),
43     origin(_("Origin"), _("Origin of the rotation"), "origin", &wr, this, "Adjust the origin of the rotation"),
44     include_original(_("Include original?"), _(""), "include_original", &wr, this, true),
45     dist_angle_handle(100)
46 {
47     show_orig_path = true;
49     // register all your parameters here, so Inkscape knows which parameters this effect has:
50     registerParameter( dynamic_cast<Parameter *>(&include_original) );
51     registerParameter( dynamic_cast<Parameter *>(&angle) );
52     registerParameter( dynamic_cast<Parameter *>(&num_copies) );
53     registerParameter( dynamic_cast<Parameter *>(&origin) );
55     registerKnotHolderHandle(new CR::KnotHolderEntityAngle(), _("Adjust the angle"));
57     num_copies.param_make_integer(true);
58     num_copies.param_set_range(0, 1000);
60 }
62 LPECopyRotate::~LPECopyRotate()
63 {
65 }
67 void
68 LPECopyRotate::doOnApply(SPLPEItem *lpeitem)
69 {
70     SPCurve *curve = SP_SHAPE(lpeitem)->curve;
72     A = curve->first_point().to_2geom();
73     B = curve->last_point().to_2geom();
75     origin.param_setValue(A);
77     dir = unit_vector(B - A);
78     dist_angle_handle = L2(B - A);
79 }
81 Geom::Piecewise<Geom::D2<Geom::SBasis> >
82 LPECopyRotate::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
83 {
84     using namespace Geom;
86     A = pwd2_in.firstValue();
87     B = pwd2_in.lastValue();
88     dir = unit_vector(B - A);
90     Piecewise<D2<SBasis> > output;
92     if (include_original) {
93         output = pwd2_in;
94     }
96     for (int i = 1; i <= num_copies; ++i) {
97         Rotate rot(deg_to_rad(angle * i));
98         Matrix t = Translate(-origin) * rot * Translate(origin);
99         output.concat(pwd2_in * t);
100     }
102     return output;
105 namespace CR {
107 using namespace Geom;
109 // TODO: make this more generic
110 static LPECopyRotate *
111 get_effect(SPItem *item)
113     Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
114     if (effect->effectType() != COPY_ROTATE) {
115         g_print ("Warning: Effect is not of type LPECopyRotate!\n");
116         return NULL;
117     }
118     return static_cast<LPECopyRotate *>(effect);
121 void
122 KnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
124     LPECopyRotate* lpe = get_effect(item);
126     // FIXME: is the minus sign due to a bug in 2geom?
127     lpe->angle.param_set_value(rad_to_deg(-angle_between(lpe->dir, p.to_2geom() - lpe->origin)));
128     if (state & GDK_SHIFT_MASK) {
129         lpe->dist_angle_handle = L2(lpe->B - lpe->A);
130     } else {
131         lpe->dist_angle_handle = L2(p - lpe->origin);
132     }
134     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
135     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
138 NR::Point
139 KnotHolderEntityAngle::knot_get()
141     LPECopyRotate* lpe = get_effect(item);
143     // FIXME: is the minus sign due to a bug in 2geom?
144     Point d = lpe->dir * Rotate(-deg_to_rad(lpe->angle)) * lpe->dist_angle_handle;
146     return lpe->origin + d;
149 } // namespace CR
151 /* ######################## */
153 } //namespace LivePathEffect
154 } /* namespace Inkscape */
156 /*
157   Local Variables:
158   mode:c++
159   c-file-style:"stroustrup"
160   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
161   indent-tabs-mode:nil
162   fill-column:99
163   End:
164 */
165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :