a82da903bb4c7168ccb5f6b19e19b66057c4800a
1 #define INKSCAPE_LPE_KNOT_CPP
2 /** \file
3 * LPE <knot> implementation
4 */
5 /*
6 * Authors:
7 * Johan Engelen
8 *
9 * Copyright (C) Johan Engelen 2007 <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-knot.h"
15 #include "display/curve.h"
16 #include <libnr/n-art-bpath.h>
18 // You might need to include other 2geom files. You can add them here:
19 #include <2geom/path.h>
20 #include <2geom/sbasis.h>
21 #include <2geom/sbasis-geometric.h>
22 #include <2geom/bezier-to-sbasis.h>
23 #include <2geom/sbasis-to-bezier.h>
24 #include <2geom/d2.h>
25 #include <2geom/sbasis-math.h>
26 #include <2geom/piecewise.h>
27 #include <2geom/crossing.h>
28 #include <2geom/path-intersection.h>
30 namespace Inkscape {
31 namespace LivePathEffect {
33 LPEKnot::LPEKnot(LivePathEffectObject *lpeobject) :
34 Effect(lpeobject),
35 // initialise your parameters here:
36 interruption_width(_("Interruption width"), _("Howmuch the lower strand is obscured by the upper."), "interruption_width", &wr, this, 10)
37 {
38 // register all your parameters here, so Inkscape knows which parameters this effect has:
39 registerParameter( dynamic_cast<Parameter *>(&interruption_width) );
40 }
42 LPEKnot::~LPEKnot()
43 {
45 }
48 /* ########################
49 * Choose to implement one of the doEffect functions. You can delete or comment out the others.
50 */
52 /*
53 void
54 LPEKnot::doEffect (SPCurve * curve)
55 {
56 // spice this up to make the effect actually *do* something!
57 }
59 NArtBpath *
60 LPEKnot::doEffect_nartbpath (NArtBpath * path_in)
61 {
62 NArtBpath *path_out;
63 unsigned ret = 0;
64 while ( path_in[ret].code != NR_END ) {
65 ++ret;
66 }
67 unsigned len = ++ret;
68 path_out = g_new(NArtBpath, len);
70 memcpy(path_out, path_in, len * sizeof(NArtBpath)); // spice this up to make the effect actually *do* something!
72 return path_out;
73 }
74 */
76 std::vector<Geom::Interval> complementOf(Geom::Interval I, std::vector<Geom::Interval> domain){
77 std::vector<Geom::Interval> ret;
78 double min = domain.front().min();
79 double max = domain.back().max();
80 Geom::Interval I1 = Geom::Interval(min,I.min());
81 Geom::Interval I2 = Geom::Interval(I.max(),max);
83 for (unsigned i = 0; i<domain.size(); i++){
84 boost::optional<Geom::Interval> I1i = intersect(domain.at(i),I1);
85 if (I1i) ret.push_back(I1i.get());
86 boost::optional<Geom::Interval> I2i = intersect(domain.at(i),I2);
87 if (I2i) ret.push_back(I2i.get());
88 }
89 return ret;
90 }
92 Geom::Interval
93 findShadowedTime(Geom::Path &patha,
94 Geom::Path &pathb,
95 Geom::Crossing crossing,
96 unsigned idx, double width){
97 using namespace Geom;
98 double curveidx, timeoncurve = modf(crossing.getOtherTime(idx),&curveidx);
99 if(curveidx == pathb.size() && timeoncurve == 0) { curveidx--; timeoncurve = 0.99999;}
100 assert(curveidx >= 0 && curveidx < pathb.size());
102 std::vector<Point> MV = pathb[unsigned(curveidx)].pointAndDerivatives(timeoncurve,2);
103 Point T = unit_vector(MV.at(1));
104 Point N = T.cw();
105 Point A = MV.at(0)-10*width*T, B = MV.at(0)+10*width*T;
107 std::vector<Geom::Path> cutter;
108 Geom::Path cutterLeft = Geom::Path();
109 Geom::Path cutterRight = Geom::Path();
110 cutterLeft.append (LineSegment (A-width*N, B-width*N));
111 cutterRight.append(LineSegment (A+width*N, B+width*N));
112 cutter.push_back(cutterLeft);
113 cutter.push_back(cutterRight);
115 std::vector<Geom::Path> patha_as_vect = std::vector<Geom::Path>(1,patha);
117 CrossingSet crossingTable = crossings (patha_as_vect, cutter);
118 double t0 = crossing.getTime(idx);
119 double tmin = 0,tmax = patha.size()-0.0001;
120 assert(crossingTable.size()>=1);
121 for (unsigned c=0; c<crossingTable.front().size(); c++){
122 double t = crossingTable.front().at(c).ta;
123 assert(crossingTable.front().at(c).a==0);
124 if (t>tmin and t<t0) tmin = t;
125 if (t<tmax and t>t0) tmax = t;
126 }
127 //return Interval(t0-0.1,t0+0.1);
128 return Interval(tmin,tmax);
129 }
132 std::vector<Geom::Path>
133 LPEKnot::doEffect_path (std::vector<Geom::Path> & path_in)
134 {
135 using namespace Geom;
136 std::vector<Geom::Path> path_out;
137 double width = interruption_width;
139 CrossingSet crossingTable = crossings_among(path_in);
140 for (unsigned i = 0; i < crossingTable.size(); i++){
141 std::vector<Interval> dom;
142 dom.push_back(Interval(0.,path_in.at(i).size()-0.00001));
143 //TODO: handle closed curves...
144 for (unsigned crs = 0; crs < crossingTable.at(i).size(); crs++){
145 Crossing crossing = crossingTable.at(i).at(crs);
146 unsigned j = crossing.getOther(i);
147 //TODO: select dir according to a parameter...
148 if ((crossing.dir and crossing.a==i) or (not crossing.dir and crossing.b==i) or (i==j)){
149 if (i==j and not crossing.dir) {
150 double temp = crossing.ta;
151 crossing.ta = crossing.tb;
152 crossing.tb = temp;
153 crossing.dir = not crossing.dir;
154 }
155 Interval hidden = findShadowedTime(path_in.at(i),path_in.at(j),crossing,i,width);
156 dom = complementOf(hidden,dom);
157 }
158 }
159 for (unsigned comp = 0; comp < dom.size(); comp++){
160 assert(dom.at(comp).min() >=0 and dom.at(comp).max() < path_in.at(i).size());
161 path_out.push_back(path_in.at(i).portion(dom.at(comp)));
162 }
164 // std::vector<Point> MV = path_in[0][0].pointAndDerivatives(crossingTable[0][0].getTime(0),2);
165 // Point U = unit_vector(MV[1]);
166 // Point N = U.cw();
167 // Point A = MV[0]-10.*width*U, B = MV[0]+10*width*U;
169 // Geom::Path cutter;
170 // cutter = Geom::Path();
171 // cutter.append( LineSegment(A+width*N,B+width*N));
172 // path_out.push_back(cutter);
173 // cutter = Geom::Path();
174 // cutter.append( LineSegment(A-width*N,B-width*N));
175 // path_out.push_back(cutter);
177 }
178 return path_out;
179 }
182 /*
183 Geom::Piecewise<Geom::D2<Geom::SBasis> >
184 addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
185 using namespace Geom;
186 Piecewise<D2<SBasis> > output;
187 Piecewise<D2<SBasis> > start;
188 Piecewise<D2<SBasis> > end;
189 double x,y,vx,vy;
191 x = m.segs.front()[0].at0();
192 y = m.segs.front()[1].at0();
193 vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
194 vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
195 start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
196 start.offsetDomain(m.cuts.front()-1.);
198 x = m.segs.back()[0].at1();
199 y = m.segs.back()[1].at1();
200 vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
201 vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
202 end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
203 //end.offsetDomain(m.cuts.back());
205 output = start;
206 output.concat(m);
207 output.concat(end);
208 return output;
209 }
211 Geom::Piecewise<Geom::D2<Geom::SBasis> >
212 LPEKnot::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
213 {
214 using namespace Geom;
217 Piecewise<D2<SBasis> > output;
218 Piecewise<D2<SBasis> > m = addLinearEnds(pwd2_in);
220 Piecewise<D2<SBasis> > v = derivative(pwd2_in);
221 Piecewise<D2<SBasis> > n = unitVector(v);
223 // // -------- Pleins et delies vs courbure ou direction...
224 // Piecewise<D2<SBasis> > a = derivative(v);
225 // Piecewise<SBasis> a_cross_n = cross(a,n);
226 // Piecewise<SBasis> v_dot_n = dot(v,n);
227 // //Piecewise<D2<SBasis> > rfrac = sectionize(D2<Piecewise<SBasis> >(a_cross_n,v_dot_n));
228 // //Piecewise<SBasis> h = atan2(rfrac)*interruption_width;
229 // Piecewise<SBasis> h = reciprocal(curvature(pwd2_in))*interruption_width;
230 //
231 // // Piecewise<D2<SBasis> > dir = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(0),Linear(-1)));
232 // // Piecewise<SBasis> h = dot(n,dir)+1.;
233 // // h *= h*(interruption_width/4.);
234 //
235 // n = rot90(n);
236 // output = pwd2_in+h*n;
237 // output.concat(pwd2_in-h*n);
238 //
239 // //-----------
241 //output.concat(m);
242 return output;
243 }
244 */
246 /* ######################## */
248 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
249 } /* namespace Inkscape */
251 /*
252 Local Variables:
253 mode:c++
254 c-file-style:"stroustrup"
255 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256 indent-tabs-mode:nil
257 fill-column:99
258 End:
259 */
260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :