2eef0cd5b21b5cecb855150ec4424ae363865667
1 #define INKSCAPE_LPE_PERSPECTIVE_PATH_CPP
2 /** \file
3 * LPE <perspective_path> implementation, used as an example for a base starting class
4 * when implementing new LivePathEffects.
5 *
6 * In vi, three global search-and-replaces will let you rename everything
7 * in this and the .h file:
8 *
9 * :%s/PERSPECTIVE_PATH/YOURNAME/g
10 * :%s/PerspectivePath/Yourname/g
11 * :%s/perspective_path/yourname/g
12 */
13 /*
14 * Authors:
15 * Johan Engelen
16 * Maximilian Albert
17 *
18 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
19 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
20 *
21 * Released under GNU GPL, read the file 'COPYING' for more information
22 */
24 #include "persp3d.h"
25 //#include "transf_mat_3x4.h"
26 #include "document.h"
28 #include "live_effects/lpe-perspective_path.h"
29 #include "sp-item-group.h"
31 #include "inkscape.h"
33 #include <2geom/path.h>
35 namespace Inkscape {
36 namespace LivePathEffect {
38 namespace PP {
40 class KnotHolderEntityOffset : public LPEKnotHolderEntity
41 {
42 public:
43 virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
44 virtual Geom::Point knot_get();
45 };
47 } // namespace PP
49 LPEPerspectivePath::LPEPerspectivePath(LivePathEffectObject *lpeobject) :
50 Effect(lpeobject),
51 // initialise your parameters here:
52 scalex(_("Scale x"), _("Scale factor in x direction"), "scalex", &wr, this, 1.0),
53 scaley(_("Scale y"), _("Scale factor in y direction"), "scaley", &wr, this, 1.0),
54 offsetx(_("Offset x"), _("Offset in x direction"), "offsetx", &wr, this, 0.0),
55 offsety(_("Offset y"), _("Offset in y direction"), "offsety", &wr, this, 0.0),
56 uses_plane_xy(_("Uses XY plane?"), _("If true, put the path on the left side of an imaginary box, otherwise on the right side"), "uses_plane_xy", &wr, this, true)
57 {
58 // register all your parameters here, so Inkscape knows which parameters this effect has:
59 registerParameter( dynamic_cast<Parameter *>(&scalex) );
60 registerParameter( dynamic_cast<Parameter *>(&scaley) );
61 registerParameter( dynamic_cast<Parameter *>(&offsetx) );
62 registerParameter( dynamic_cast<Parameter *>(&offsety) );
63 registerParameter( dynamic_cast<Parameter *>(&uses_plane_xy) );
65 registerKnotHolderHandle(new PP::KnotHolderEntityOffset(), _("Adjust the origin"));
67 concatenate_before_pwd2 = true; // don't split the path into its subpaths
69 Persp3D *persp = persp3d_document_first_persp(inkscape_active_document());
71 Proj::TransfMat3x4 pmat = persp->tmat;
73 pmat.copy_tmat(tmat);
74 }
76 LPEPerspectivePath::~LPEPerspectivePath()
77 {
79 }
81 void
82 LPEPerspectivePath::doBeforeEffect (SPLPEItem *lpeitem)
83 {
84 original_bbox(lpeitem, true);
85 }
87 Geom::Piecewise<Geom::D2<Geom::SBasis> >
88 LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
89 {
90 using namespace Geom;
92 Piecewise<D2<SBasis> > path_a_pw = pwd2_in;
94 // FIXME: the minus sign is there because the SVG coordinate system goes down;
95 // remove this once we have unified coordinate systems
96 path_a_pw = path_a_pw + Geom::Point(offsetx, -offsety);
98 D2<Piecewise<SBasis> > B = make_cuts_independent(path_a_pw);
99 Piecewise<SBasis> preimage[4];
101 //Geom::Point orig = Geom::Point(bounds_X.min(), bounds_Y.middle());
102 //orig = Geom::Point(orig[X], sp_document_height(inkscape_active_document()) - orig[Y]);
104 //double offset = uses_plane_xy ? boundingbox_X.extent() : 0.0;
106 orig = Point(uses_plane_xy ? boundingbox_X.max() : boundingbox_X.min(), boundingbox_Y.middle());
108 /**
109 g_print ("Orig: (%8.2f, %8.2f)\n", orig[X], orig[Y]);
111 g_print ("B[1] - orig[1]: %8.2f\n", (B[1] - orig[1])[0].valueAt(0));
112 g_print ("B[0] - orig[0]: %8.2f\n", (B[0] - orig[0])[0].valueAt(0));
113 **/
115 if (uses_plane_xy) {
116 preimage[0] = (-B[0] + orig[0]) * scalex / 200.0;
117 preimage[1] = ( B[1] - orig[1]) * scaley / 400.0;
118 preimage[2] = B[0] - B[0]; // hack!
119 } else {
120 preimage[0] = B[0] - B[0]; // hack!
121 preimage[1] = (B[1] - orig[1]) * scaley / 400.0;
122 preimage[2] = (B[0] - orig[0]) * scalex / 200.0;
123 }
125 /* set perspective origin to first point of path */
126 tmat[0][3] = orig[0];
127 tmat[1][3] = orig[1];
129 /**
130 g_print ("preimage[1]: %8.2f\n", preimage[1][0].valueAt(0));
131 g_print ("preimage[2]: %8.2f\n", preimage[2][0].valueAt(0));
132 **/
134 Piecewise<SBasis> res[3];
135 for (int j = 0; j < 3; ++j) {
136 res[j] =
137 preimage[0] * tmat[j][0]
138 + preimage[1] * tmat[j][1]
139 + preimage[2] * tmat[j][2]
140 + tmat[j][3];
141 }
142 D2<Piecewise<SBasis> > result(divide(res[0],res[2], 3),
143 divide(res[1],res[2], 3));
145 Piecewise<D2<SBasis> > output = sectionize(result);
147 return output;
148 }
150 namespace PP {
152 // TODO: make this more generic
153 static LPEPerspectivePath *
154 get_effect(SPItem *item)
155 {
156 Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
157 if (effect->effectType() != PERSPECTIVE_PATH) {
158 g_print ("Warning: Effect is not of type LPEPerspectivePath!\n");
159 return NULL;
160 }
161 return static_cast<LPEPerspectivePath *>(effect);
162 }
164 void
165 KnotHolderEntityOffset::knot_set(Geom::Point const &p, Geom::Point const &origin, guint /*state*/)
166 {
167 using namespace Geom;
169 LPEPerspectivePath* lpe = get_effect(item);
171 Geom::Point const s = snap_knot_position(p);
173 lpe->offsetx.param_set_value((s - origin)[Geom::X]);
174 lpe->offsety.param_set_value(-(s - origin)[Geom::Y]); // additional minus sign is due to coordinate system flipping
176 // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
177 sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
178 }
180 Geom::Point
181 KnotHolderEntityOffset::knot_get()
182 {
183 LPEPerspectivePath* lpe = get_effect(item);
184 return lpe->orig + Geom::Point(lpe->offsetx, -lpe->offsety);
185 }
187 } // namespace PP
189 } //namespace LivePathEffect
190 } /* namespace Inkscape */
192 /*
193 Local Variables:
194 mode:c++
195 c-file-style:"stroustrup"
196 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
197 indent-tabs-mode:nil
198 fill-column:99
199 End:
200 */
201 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :