Code

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