Code

add LPEConstructGrid
[inkscape.git] / src / live_effects / lpe-constructgrid.cpp
1 #define INKSCAPE_LPE_CONSTRUCTGRID_CPP\r
2 /** \file\r
3  * LPE Construct Grid implementation\r
4  */\r
5 /*\r
6  * Authors:\r
7  *   Johan Engelen\r
8 *\r
9 * Copyright (C) Johan Engelen 2008 <j.b.c.engelen@utwente.nl>\r
10  *\r
11  * Released under GNU GPL, read the file 'COPYING' for more information\r
12  */\r
13 \r
14 #include "live_effects/lpe-constructgrid.h"\r
15 \r
16 #include <2geom/path.h>\r
17 #include <2geom/transforms.h>\r
18 \r
19 #include "nodepath.h"\r
20 \r
21 namespace Inkscape {\r
22 namespace LivePathEffect {\r
23 \r
24 using namespace Geom;\r
25 \r
26 LPEConstructGrid::LPEConstructGrid(LivePathEffectObject *lpeobject) :\r
27     Effect(lpeobject),\r
28     nr_x(_("Size X"), _("The size of the grid in X direction."), "nr_x", &wr, this, 5),\r
29     nr_y(_("Size Y"), _("The size of the grid in Y direction."), "nr_y", &wr, this, 5)\r
30 {\r
31     registerParameter( dynamic_cast<Parameter *>(&nr_x) );\r
32     registerParameter( dynamic_cast<Parameter *>(&nr_y) );\r
33 \r
34     nr_x.param_make_integer();\r
35     nr_y.param_make_integer();\r
36     nr_x.param_set_range(1, NR_HUGE);\r
37     nr_y.param_set_range(1, NR_HUGE);\r
38 }\r
39 \r
40 LPEConstructGrid::~LPEConstructGrid()\r
41 {\r
42 \r
43 }\r
44 \r
45 std::vector<Geom::Path>\r
46 LPEConstructGrid::doEffect_path (std::vector<Geom::Path> const & path_in)\r
47 {\r
48   // Check that the path has at least 3 nodes (i.e. 2 segments), more nodes are ignored\r
49     if (path_in[0].size() >= 2) {\r
50         // read the first 3 nodes:\r
51         Geom::Path::const_iterator it ( path_in[0].begin() );\r
52         Geom::Point first_p  = (*it++).initialPoint();\r
53         Geom::Point origin   = (*it++).initialPoint();\r
54         Geom::Point second_p = (*it++).initialPoint();\r
55         // make first_p and second_p be the construction *vectors* of the grid:\r
56         first_p  -= origin;\r
57         second_p -= origin;\r
58         Geom::Translate first_translation( first_p );\r
59         Geom::Translate second_translation( second_p );\r
60 \r
61         // create the gridpaths of the two directions\r
62         Geom::Path first_path( origin );\r
63         first_path.appendNew<LineSegment>( origin + first_p*nr_y );\r
64         Geom::Path second_path( origin );\r
65         second_path.appendNew<LineSegment>( origin + second_p*nr_x );\r
66 \r
67         // use the gridpaths and set them in the correct grid\r
68         std::vector<Geom::Path> path_out;\r
69         path_out.push_back(first_path);\r
70         for (int ix = 0; ix < nr_x; ix++) {\r
71             path_out.push_back(path_out.back() * second_translation );\r
72         }\r
73         path_out.push_back(second_path);\r
74         for (int iy = 0; iy < nr_y; iy++) {\r
75             path_out.push_back(path_out.back() * first_translation );\r
76         }\r
77 \r
78         return path_out;\r
79     } else {\r
80         return path_in;\r
81     }\r
82 }\r
83 \r
84 void\r
85 LPEConstructGrid::setup_nodepath(Inkscape::NodePath::Path *np)\r
86 {\r
87     Effect::setup_nodepath(np);\r
88     sp_nodepath_make_straight_path(np);\r
89 }\r
90 \r
91 } //namespace LivePathEffect\r
92 } /* namespace Inkscape */\r
93 \r
94 /*\r
95   Local Variables:\r
96   mode:c++\r
97   c-file-style:"stroustrup"\r
98   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
99   indent-tabs-mode:nil\r
100   fill-column:99\r
101   End:\r
102 */\r
103 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r