1 #define INKSCAPE_LPE_RULER_CPP
3 /** \file
4 * LPE <ruler> implementation, see lpe-ruler.cpp.
5 */
7 /*
8 * Authors:
9 * Maximilian Albert
10 *
11 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "live_effects/lpe-ruler.h"
17 #include <2geom/piecewise.h>
18 #include <2geom/sbasis-geometric.h>
19 #include "inkscape.h"
20 #include "desktop.h"
23 namespace Inkscape {
24 namespace LivePathEffect {
26 static const Util::EnumData<MarkDirType> MarkDirData[] = {
27 {MARKDIR_LEFT , N_("Left"), "left"},
28 {MARKDIR_RIGHT , N_("Right"), "right"},
29 {MARKDIR_BOTH , N_("Both"), "both"},
30 };
31 static const Util::EnumDataConverter<MarkDirType> MarkDirTypeConverter(MarkDirData, sizeof(MarkDirData)/sizeof(*MarkDirData));
33 static const Util::EnumData<BorderMarkType> BorderMarkData[] = {
34 {BORDERMARK_NONE , N_("None"), "none"},
35 {BORDERMARK_START , N_("Start"), "start"},
36 {BORDERMARK_END , N_("End"), "end"},
37 {BORDERMARK_BOTH , N_("Both"), "both"},
38 };
39 static const Util::EnumDataConverter<BorderMarkType> BorderMarkTypeConverter(BorderMarkData, sizeof(BorderMarkData)/sizeof(*BorderMarkData));
41 LPERuler::LPERuler(LivePathEffectObject *lpeobject) :
42 Effect(lpeobject),
43 mark_distance(_("Mark distance:"), _("Distance between successive ruler marks"), "mark_distance", &wr, this, 20.0),
44 unit(_("Unit:"), _("Unit"), "unit", &wr, this),
45 mark_length(_("Major length:"), _("Length of major ruler marks"), "mark_length", &wr, this, 14.0),
46 minor_mark_length(_("Minor length:"), _("Length of minor ruler marks"), "minor_mark_length", &wr, this, 7.0),
47 major_mark_steps(_("Major steps:"), _("Draw a major mark every ... steps"), "major_mark_steps", &wr, this, 5),
48 shift(_("Shift marks by:"), _("Shift marks by this many steps"), "shift", &wr, this, 0),
49 mark_dir(_("Mark direction:"), _("Direction of marks (when viewing along the path from start to end)"), "mark_dir", MarkDirTypeConverter, &wr, this, MARKDIR_LEFT),
50 offset(_("Offset:"), _("Offset of first mark"), "offset", &wr, this, 0.0),
51 border_marks(_("Border marks:"), _("Choose whether to draw marks at the beginning and end of the path"), "border_marks", BorderMarkTypeConverter, &wr, this, BORDERMARK_BOTH)
52 {
53 registerParameter(dynamic_cast<Parameter *>(&unit));
54 registerParameter(dynamic_cast<Parameter *>(&mark_distance));
55 registerParameter(dynamic_cast<Parameter *>(&mark_length));
56 registerParameter(dynamic_cast<Parameter *>(&minor_mark_length));
57 registerParameter(dynamic_cast<Parameter *>(&major_mark_steps));
58 registerParameter(dynamic_cast<Parameter *>(&shift));
59 registerParameter(dynamic_cast<Parameter *>(&offset));
60 registerParameter(dynamic_cast<Parameter *>(&mark_dir));
61 registerParameter(dynamic_cast<Parameter *>(&border_marks));
63 major_mark_steps.param_make_integer();
64 major_mark_steps.param_set_range(1, 1000);
65 shift.param_make_integer();
67 mark_length.param_set_increments(1.0, 10.0);
68 minor_mark_length.param_set_increments(1.0, 10.0);
69 offset.param_set_increments(1.0, 10.0);
70 }
72 LPERuler::~LPERuler()
73 {
75 }
77 Geom::Point LPERuler::n_major;
78 Geom::Point LPERuler::n_minor;
80 Geom::Piecewise<Geom::D2<Geom::SBasis> >
81 LPERuler::ruler_mark(Geom::Point const &A, Geom::Point const &n, MarkType const &marktype)
82 {
83 using namespace Geom;
85 gboolean success;
86 double real_mark_length = mark_length;
87 success = sp_convert_distance(&real_mark_length, unit, &sp_unit_get_by_id(SP_UNIT_PX));
88 double real_minor_mark_length = minor_mark_length;
89 success = sp_convert_distance(&real_minor_mark_length, unit, &sp_unit_get_by_id(SP_UNIT_PX));
91 n_major = real_mark_length * n;
92 n_minor = real_minor_mark_length * n;
93 if (mark_dir == MARKDIR_BOTH) {
94 n_major = n_major * 0.5;
95 n_minor = n_minor * 0.5;
96 }
98 Point C, D;
99 switch (marktype) {
100 case MARK_MAJOR:
101 C = A;
102 D = A + n_major;
103 if (mark_dir == MARKDIR_BOTH)
104 C -= n_major;
105 break;
106 case MARK_MINOR:
107 C = A;
108 D = A + n_minor;
109 if (mark_dir == MARKDIR_BOTH)
110 C -= n_minor;
111 break;
112 default:
113 // do nothing
114 break;
115 }
117 Piecewise<D2<SBasis> > seg(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
118 return seg;
119 }
121 Geom::Piecewise<Geom::D2<Geom::SBasis> >
122 LPERuler::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
123 {
124 using namespace Geom;
126 const int mminterval = static_cast<int>(major_mark_steps);
127 const int i_shift = static_cast<int>(shift) % mminterval;
128 int sign = (mark_dir == MARKDIR_RIGHT ? 1 : -1 );
130 Piecewise<D2<SBasis> >output(pwd2_in);
131 Piecewise<D2<SBasis> >speed = derivative(pwd2_in);
132 Piecewise<SBasis> arclength = arcLengthSb(pwd2_in);
133 double totlength = arclength.lastValue();
135 //find at which times to draw a mark:
136 std::vector<double> s_cuts;
138 double real_mark_distance = mark_distance;
139 gboolean success = sp_convert_distance(&real_mark_distance, unit, &sp_unit_get_by_id(SP_UNIT_PX));
141 double real_offset = offset;
142 success = sp_convert_distance(&real_offset, unit, &sp_unit_get_by_id(SP_UNIT_PX));
143 for (double s = real_offset; s<totlength; s+=real_mark_distance){
144 s_cuts.push_back(s);
145 }
146 std::vector<std::vector<double> > roots = multi_roots(arclength, s_cuts);
147 std::vector<double> t_cuts;
148 for (unsigned v=0; v<roots.size();v++){
149 //FIXME: 2geom multi_roots solver seem to sometimes "repeat" solutions.
150 //Here, we are supposed to have one and only one solution for each s.
151 if(roots[v].size()>0)
152 t_cuts.push_back(roots[v][0]);
153 }
154 //draw the marks
155 for (unsigned i=0; i<t_cuts.size(); i++){
156 Point A = pwd2_in(t_cuts[i]);
157 Point n = rot90(unit_vector(speed(t_cuts[i])))*sign;
158 if ((i % mminterval) == i_shift) {
159 output.concat (ruler_mark(A, n, MARK_MAJOR));
160 } else {
161 output.concat (ruler_mark(A, n, MARK_MINOR));
162 }
163 }
164 //eventually draw a mark at start
165 if ((border_marks == BORDERMARK_START || border_marks == BORDERMARK_BOTH) && (offset != 0.0 || i_shift != 0)){
166 Point A = pwd2_in.firstValue();
167 Point n = rot90(unit_vector(speed.firstValue()))*sign;
168 output.concat (ruler_mark(A, n, MARK_MAJOR));
169 }
170 //eventually draw a mark at end
171 if (border_marks == BORDERMARK_END || border_marks == BORDERMARK_BOTH){
172 Point A = pwd2_in.lastValue();
173 Point n = rot90(unit_vector(speed.lastValue()))*sign;
174 //speed.lastValue() is somtimes wrong when the path is closed: a tiny line seg might added at the end to fix rounding errors...
175 //TODO: Find a better fix!! (How do we know if the path was closed?)
176 if ( A == pwd2_in.firstValue() &&
177 speed.segs.size() > 1 &&
178 speed.segs.back()[X].size() <= 1 &&
179 speed.segs.back()[Y].size() <= 1 &&
180 speed.segs.back()[X].tailError(0) <= 1e-10 &&
181 speed.segs.back()[Y].tailError(0) <= 1e-10
182 ){
183 n = rot90(unit_vector(speed.segs[speed.segs.size()-2].at1()))*sign;
184 }
185 output.concat (ruler_mark(A, n, MARK_MAJOR));
186 }
188 return output;
189 }
191 } //namespace LivePathEffect
192 } /* namespace Inkscape */
194 /*
195 Local Variables:
196 mode:c++
197 c-file-style:"stroustrup"
198 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
199 indent-tabs-mode:nil
200 fill-column:99
201 End:
202 */
203 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :