d193b6d66591ae483d9e4915c7b9422dc6c5b7f5
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>
22 #include <2geom/angle.h>
24 namespace Inkscape {
25 namespace LivePathEffect {
27 namespace CR {
29 class KnotHolderEntityStartingAngle : public LPEKnotHolderEntity
30 {
31 public:
32 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
33 virtual NR::Point knot_get();
34 };
36 class KnotHolderEntityRotationAngle : public LPEKnotHolderEntity
37 {
38 public:
39 virtual void knot_set(NR::Point const &p, NR::Point const &origin, guint state);
40 virtual NR::Point knot_get();
41 };
43 } // namespace CR
45 LPECopyRotate::LPECopyRotate(LivePathEffectObject *lpeobject) :
46 Effect(lpeobject),
47 starting_angle(_("Starting"), _("Angle of the first copy"), "starting_angle", &wr, this, 0.0),
48 rotation_angle(_("Rotation angle"), _("Angle between two successive copies"), "rotation_angle", &wr, this, 30.0),
49 num_copies(_("Number of copies"), _("Number of copies of the original path"), "num_copies", &wr, this, 5),
50 origin(_("Origin"), _("Origin of the rotation"), "origin", &wr, this, "Adjust the origin of the rotation"),
51 dist_angle_handle(100)
52 {
53 show_orig_path = true;
55 // register all your parameters here, so Inkscape knows which parameters this effect has:
56 registerParameter( dynamic_cast<Parameter *>(&starting_angle) );
57 registerParameter( dynamic_cast<Parameter *>(&rotation_angle) );
58 registerParameter( dynamic_cast<Parameter *>(&num_copies) );
59 registerParameter( dynamic_cast<Parameter *>(&origin) );
61 registerKnotHolderHandle(new CR::KnotHolderEntityStartingAngle(), _("Adjust the starting angle"));
62 registerKnotHolderHandle(new CR::KnotHolderEntityRotationAngle(), _("Adjust the rotation angle"));
64 num_copies.param_make_integer(true);
65 num_copies.param_set_range(0, 1000);
67 }
69 LPECopyRotate::~LPECopyRotate()
70 {
72 }
74 void
75 LPECopyRotate::doOnApply(SPLPEItem *lpeitem)
76 {
77 SPCurve *curve = SP_SHAPE(lpeitem)->curve;
79 A = curve->first_point();
80 B = curve->last_point();
82 origin.param_setValue(A);
84 dir = unit_vector(B - A);
85 dist_angle_handle = L2(B - A);
86 }
88 Geom::Piecewise<Geom::D2<Geom::SBasis> >
89 LPECopyRotate::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
90 {
91 using namespace Geom;
93 A = pwd2_in.firstValue();
94 B = pwd2_in.lastValue();
95 dir = unit_vector(B - A);
97 Piecewise<D2<SBasis> > output;
99 Matrix pre = Translate(-origin) * Rotate(-deg_to_rad(starting_angle));
100 for (int i = 0; i < num_copies; ++i) {
101 // I first suspected the minus sign to be a bug in 2geom but it is
102 // likely due to SVG's choice of coordinate system orientation (max)
103 Rotate rot(-deg_to_rad(rotation_angle * i));
104 Matrix t = pre * rot * Translate(origin);
105 output.concat(pwd2_in * t);
106 }
108 return output;
109 }
111 namespace CR {
113 using namespace Geom;
115 // TODO: make this more generic
116 static LPECopyRotate *
117 get_effect(SPItem *item)
118 {
119 Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
120 if (effect->effectType() != COPY_ROTATE) {
121 g_print ("Warning: Effect is not of type LPECopyRotate!\n");
122 return NULL;
123 }
124 return static_cast<LPECopyRotate *>(effect);
125 }
127 void
128 KnotHolderEntityStartingAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
129 {
130 LPECopyRotate* lpe = get_effect(item);
132 NR::Point const s = snap_knot_position(p);
134 // I first suspected the minus sign to be a bug in 2geom but it is
135 // likely due to SVG's choice of coordinate system orientation (max)
136 lpe->starting_angle.param_set_value(rad_to_deg(-angle_between(lpe->dir, s.to_2geom() - lpe->origin)));
137 if (state & GDK_SHIFT_MASK) {
138 lpe->dist_angle_handle = L2(lpe->B - lpe->A);
139 } else {
140 lpe->dist_angle_handle = L2(p - lpe->origin);
141 }
143 // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
144 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
145 }
147 void
148 KnotHolderEntityRotationAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
149 {
150 LPECopyRotate* lpe = get_effect(item);
152 NR::Point const s = snap_knot_position(p);
154 // I first suspected the minus sign to be a bug in 2geom but it is
155 // likely due to SVG's choice of coordinate system orientation (max)
156 lpe->rotation_angle.param_set_value(rad_to_deg(-angle_between(lpe->dir, s.to_2geom() - lpe->origin)) - lpe->starting_angle);
157 if (state & GDK_SHIFT_MASK) {
158 lpe->dist_angle_handle = L2(lpe->B - lpe->A);
159 } else {
160 lpe->dist_angle_handle = L2(p - lpe->origin);
161 }
163 // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
164 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
165 }
167 NR::Point
168 KnotHolderEntityStartingAngle::knot_get()
169 {
170 LPECopyRotate* lpe = get_effect(item);
171 // I first suspected the minus sign to be a bug in 2geom but it is
172 // likely due to SVG's choice of coordinate system orientation (max)
173 Point d = lpe->dir * Rotate(-deg_to_rad(lpe->starting_angle)) * lpe->dist_angle_handle;
175 return snap_knot_position(lpe->origin + d);
176 }
178 NR::Point
179 KnotHolderEntityRotationAngle::knot_get()
180 {
181 LPECopyRotate* lpe = get_effect(item);
182 // I first suspected the minus sign to be a bug in 2geom but it is
183 // likely due to SVG's choice of coordinate system orientation (max)
184 Point d = lpe->dir * Rotate(-deg_to_rad(lpe->starting_angle + lpe->rotation_angle)) * lpe->dist_angle_handle;
186 return snap_knot_position(lpe->origin + d);
187 }
189 } // namespace CR
191 /* ######################## */
193 } //namespace LivePathEffect
194 } /* namespace Inkscape */
196 /*
197 Local Variables:
198 mode:c++
199 c-file-style:"stroustrup"
200 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201 indent-tabs-mode:nil
202 fill-column:99
203 End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :