Code

UI fixes (a.o. Bug #560751 )
[inkscape.git] / src / live_effects / lpe-interpolate.cpp
1 #define INKSCAPE_LPE_INTERPOLATE_CPP
2 /** \file
3  * LPE interpolate implementation
4  */
5 /*
6  * Authors:
7  *   Johan Engelen
8  *
9  * Copyright (C) Johan Engelen 2007-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-interpolate.h"
16 #include <2geom/path.h>
17 #include <2geom/sbasis-to-bezier.h>
18 #include <2geom/d2-sbasis.h>
19 #include <2geom/piecewise.h>
20 #include <2geom/sbasis-geometric.h>
22 #include "sp-path.h"
23 #include "display/curve.h"
25 namespace Inkscape {
26 namespace LivePathEffect {
28 LPEInterpolate::LPEInterpolate(LivePathEffectObject *lpeobject) :
29     Effect(lpeobject),
30     trajectory_path(_("Trajectory:"), _("Path along which intermediate steps are created."), "trajectory", &wr, this, "M0,0 L0,0"),
31     number_of_steps(_("Steps:"), _("Determines the number of steps from start to end path."), "steps", &wr, this, 5),
32     equidistant_spacing(_("Equidistant spacing"), _("If true, the spacing between intermediates is constant along the length of the path. If false, the distance depends on the location of the nodes of the trajectory path."), "equidistant_spacing", &wr, this, true)
33 {
34     show_orig_path = true;
36     registerParameter( dynamic_cast<Parameter *>(&trajectory_path) );
37     registerParameter( dynamic_cast<Parameter *>(&equidistant_spacing) );
38     registerParameter( dynamic_cast<Parameter *>(&number_of_steps) );
40     number_of_steps.param_make_integer();
41     number_of_steps.param_set_range(2, NR_HUGE);
42 }
44 LPEInterpolate::~LPEInterpolate()
45 {
47 }
49 /*
50  * interpolate path_in[0] to path_in[1]
51  */
52 Geom::PathVector
53 LPEInterpolate::doEffect_path (Geom::PathVector const & path_in)
54 {
55     if ( (path_in.size() < 2) || (number_of_steps < 2)) {
56         return path_in;
57     }
58     // Don't allow empty path parameter:
59     if ( trajectory_path.get_pathvector().empty() ) {
60         return path_in;
61     }
63     std::vector<Geom::Path> path_out;
65     Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_A = path_in[0].toPwSb();
66     Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2_B = path_in[1].toPwSb();
68     // Transform both paths to (0,0) midpoint, so they can easily be positioned along interpolate_path
69     if (Geom::OptRect bounds = Geom::bounds_exact(pwd2_A)) {
70         pwd2_A -= bounds->midpoint();
71     }
72     if (Geom::OptRect bounds = Geom::bounds_exact(pwd2_B)) {
73         pwd2_B -= bounds->midpoint();
74     }
76     // Make sure both paths have the same number of segments and cuts at the same locations
77     pwd2_B.setDomain(pwd2_A.domain());
78     Geom::Piecewise<Geom::D2<Geom::SBasis> > pA = Geom::partition(pwd2_A, pwd2_B.cuts);
79     Geom::Piecewise<Geom::D2<Geom::SBasis> > pB = Geom::partition(pwd2_B, pwd2_A.cuts);
81     Geom::Piecewise<Geom::D2<Geom::SBasis> > trajectory = trajectory_path.get_pathvector()[0].toPwSb();
82     if (equidistant_spacing)
83         trajectory = Geom::arc_length_parametrization(trajectory);
85     Geom::Interval trajectory_domain = trajectory.domain();
87     for (int i = 0; i < number_of_steps; ++i) {
88         double fraction = i / (number_of_steps-1);
90         Geom::Piecewise<Geom::D2<Geom::SBasis> > pResult = pA*(1-fraction)  +  pB*fraction;
91         pResult += trajectory.valueAt(trajectory_domain.min() + fraction*trajectory_domain.extent());
93         Geom::PathVector pathv = Geom::path_from_piecewise(pResult, LPE_CONVERSION_TOLERANCE);
94         path_out.push_back( pathv[0] );
95     }
97     return path_out;
98 }
100 void
101 LPEInterpolate::resetDefaults(SPItem * item)
103     Effect::resetDefaults(item);
105     if (!SP_IS_PATH(item))
106         return;
108     SPCurve const *crv = sp_path_get_curve_reference(SP_PATH(item));
109     Geom::PathVector const &pathv = crv->get_pathvector();
110     if ( (pathv.size() < 2) )
111         return;
113     Geom::OptRect bounds_A = pathv[0].boundsExact();
114     Geom::OptRect bounds_B = pathv[1].boundsExact();
116     if (bounds_A && bounds_B) {
117         Geom::PathVector traj_pathv;
118         traj_pathv.push_back( Geom::Path() );
119         traj_pathv[0].start( bounds_A->midpoint() );
120         traj_pathv[0].appendNew<Geom::LineSegment>( bounds_B->midpoint() );
121         trajectory_path.set_new_value( traj_pathv, true );
122     } else {
123         trajectory_path.param_set_and_write_default();
124     }
127 } //namespace LivePathEffect
128 } /* namespace Inkscape */
130 /*
131   Local Variables:
132   mode:c++
133   c-file-style:"stroustrup"
134   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135   indent-tabs-mode:nil
136   fill-column:99
137   End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :