Code

change doEffect functions to use const& parameters
[inkscape.git] / src / live_effects / lpe-knot.cpp
1 #define INKSCAPE_LPE_KNOT_CPP
2 /** \file
3  * LPE <knot> implementation
4  */
5 /*
6  * Authors:
7  *   JF Barraud
8 *
9 * Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
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 #include <2geom/path.h>
19 //#include <2geom/sbasis.h>
20 //#include <2geom/sbasis-geometric.h>
21 //#include <2geom/bezier-to-sbasis.h>
22 //#include <2geom/sbasis-to-bezier.h>
23 #include <2geom/d2.h>
24 //#include <2geom/sbasis-math.h>
25 //#include <2geom/piecewise.h>
26 #include <2geom/crossing.h>
27 #include <2geom/path-intersection.h>
29 namespace Inkscape {
30 namespace LivePathEffect {
32 LPEKnot::LPEKnot(LivePathEffectObject *lpeobject) :
33     Effect(lpeobject),
34     // initialise your parameters here:
35     interruption_width(_("Interruption width"), _("Howmuch the lower strand is obscured by the upper."), "interruption_width", &wr, this, 10)
36 {
37     // register all your parameters here, so Inkscape knows which parameters this effect has:
38     registerParameter( dynamic_cast<Parameter *>(&interruption_width) );
39 }
41 LPEKnot::~LPEKnot()
42 {
44 }
46 //remove an interval from an union of intervals.
47 std::vector<Geom::Interval> complementOf(Geom::Interval I, std::vector<Geom::Interval> domain){
48     std::vector<Geom::Interval> ret;
49     double min = domain.front().min();
50     double max = domain.back().max();
51     Geom::Interval I1 = Geom::Interval(min,I.min());
52     Geom::Interval I2 = Geom::Interval(I.max(),max);
54     for (unsigned i = 0; i<domain.size(); i++){
55         boost::optional<Geom::Interval> I1i = intersect(domain.at(i),I1);
56         if (I1i) ret.push_back(I1i.get());
57         boost::optional<Geom::Interval> I2i = intersect(domain.at(i),I2);
58         if (I2i) ret.push_back(I2i.get());
59     }
60     return ret;
61 }
63 Geom::Interval
64 findShadowedTime(Geom::Path &patha, 
65                  Geom::Path &pathb, 
66                  Geom::Crossing crossing, 
67                  unsigned idx, double width){
68     using namespace Geom;
69     double curveidx, timeoncurve = modf(crossing.getOtherTime(idx),&curveidx);
70     if(curveidx == pathb.size() && timeoncurve == 0) { curveidx--; timeoncurve = 0.99999;}
71     assert(curveidx >= 0 && curveidx < pathb.size());
72     
73     std::vector<Point> MV = pathb[unsigned(curveidx)].pointAndDerivatives(timeoncurve,2);
74     Point T = unit_vector(MV.at(1));
75     Point N = T.cw();
76     Point A = MV.at(0)-10*width*T, B = MV.at(0)+10*width*T;
77     
78     std::vector<Geom::Path> cutter;
79     Geom::Path cutterLeft  = Geom::Path();
80     Geom::Path cutterRight = Geom::Path();
81     cutterLeft.append (LineSegment (A-width*N, B-width*N));
82     cutterRight.append(LineSegment (A+width*N, B+width*N));
83     cutter.push_back(cutterLeft);
84     cutter.push_back(cutterRight);
85     
86     std::vector<Geom::Path> patha_as_vect = std::vector<Geom::Path>(1,patha);
87     
88     CrossingSet crossingTable = crossings (patha_as_vect, cutter);
89     double t0 = crossing.getTime(idx);
90     double tmin = 0,tmax = patha.size()-0.0001;
91     assert(crossingTable.size()>=1);
92     for (unsigned c=0; c<crossingTable.front().size(); c++){
93         double t = crossingTable.front().at(c).ta;
94         assert(crossingTable.front().at(c).a==0);
95         if (t>tmin and t<t0) tmin = t;
96         if (t<tmax and t>t0) tmax = t;
97     }
98     //return Interval(t0-0.1,t0+0.1);
99     return Interval(tmin,tmax);
100 }                
102 //Just a try; this should be moved to 2geom if ever it works.
103 std::vector<Geom::Path>
104 split_loopy_bezier (std::vector<Geom::Path> const & path_in){
106     std::vector<Geom::Path> ret; 
107     std::vector<Geom::Path>::const_iterator pi=path_in.begin();
108     for(; pi != path_in.end(); pi++) {
109         ret.push_back(Geom::Path());
110         for (Geom::Path::const_iterator curve(pi->begin()),end(pi->end()); curve != end; ++curve){
112             //is the current curve a cubic bezier?
113             if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const *>(&(*curve))){
114                 Geom::CubicBezier theCurve = *cubic_bezier;
115                 std::vector<Geom::Point> A = theCurve.points();
117                 //is there a crossing in the polygon?
118                 if( cross(A[2]-A[0],A[1]-A[0])*cross(A[3]-A[0],A[1]-A[0])<0 &&
119                     cross(A[0]-A[3],A[2]-A[3])*cross(A[1]-A[3],A[2]-A[3])<0){
121                     //split the curve where the tangent is parallel to the chord through end points.
122                     double a = cross(A[3]-A[0],A[1]-A[2]);
123                     double c = cross(A[3]-A[0],A[1]-A[0]);
124                     double t; //where to split; solution of 3*at^2-(a+c)t +c = 0. 
125                     //TODO: don't we have a clean deg 2 equation solver?...
126                     if (fabs(a)<.0001){
127                         t = .5;
128                     }else{
129                         double delta = a*a-a*c+c*c;
130                         t = ((a+c)-sqrt(delta))/2/a;
131                         if ( t<0 || t>1 ) {t = ((a+c)+sqrt(delta))/2/a;}
132                     }
133                     //TODO: shouldn't Path have subdivide method?
134                     std::pair<Geom::BezierCurve<3>, Geom::BezierCurve<3> > splitCurve;
135                     splitCurve = theCurve.subdivide(t);
136                     ret.back().append(splitCurve.first);
137                     ret.back().append(splitCurve.second);
139                 }else{//cubic bezier but no crossing.
140                     ret.back().append(*curve);
141                 }
142             }else{//not cubic bezier.
143                 ret.back().append(*curve);
144             }
145         }
146     }
147     return ret;
151 std::vector<Geom::Path>
152 LPEKnot::doEffect_path (std::vector<Geom::Path> const & input_path)
154     using namespace Geom;
155     std::vector<Geom::Path> path_out;
156     double width = interruption_width;
158     std::vector<Geom::Path> path_in = split_loopy_bezier(input_path);
160     CrossingSet crossingTable = crossings_among(path_in);
161     for (unsigned i = 0; i < crossingTable.size(); i++){
162         std::vector<Interval> dom;
163         dom.push_back(Interval(0.,path_in.at(i).size()-0.00001));
164         //TODO: handle closed curves...
165         for (unsigned crs = 0; crs < crossingTable.at(i).size(); crs++){
166             Crossing crossing = crossingTable.at(i).at(crs);
167             unsigned j = crossing.getOther(i);
168             //TODO: select dir according to a parameter...
169             if ((crossing.dir and crossing.a==i) or (not crossing.dir and crossing.b==i) or (i==j)){
170                 if (i==j and not crossing.dir) {
171                     double temp = crossing.ta;
172                     crossing.ta = crossing.tb; 
173                     crossing.tb = temp; 
174                     crossing.dir = not crossing.dir;
175                 }
176                 Interval hidden = findShadowedTime(path_in.at(i),path_in.at(j),crossing,i,width);                
177                 dom = complementOf(hidden,dom);
178             }
179         }
180         for (unsigned comp = 0; comp < dom.size(); comp++){
181             assert(dom.at(comp).min() >=0 and dom.at(comp).max() < path_in.at(i).size());
182             path_out.push_back(path_in.at(i).portion(dom.at(comp)));
183         }
184     }   
185     return path_out;
189 /*
190 Geom::Piecewise<Geom::D2<Geom::SBasis> >
191 addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
192     using namespace Geom;
193     Piecewise<D2<SBasis> > output;
194     Piecewise<D2<SBasis> > start;
195     Piecewise<D2<SBasis> > end;
196     double x,y,vx,vy;
198     x  = m.segs.front()[0].at0();
199     y  = m.segs.front()[1].at0();
200     vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
201     vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
202     start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
203     start.offsetDomain(m.cuts.front()-1.);
205     x  = m.segs.back()[0].at1();
206     y  = m.segs.back()[1].at1();
207     vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
208     vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
209     end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
210     //end.offsetDomain(m.cuts.back());
212     output = start;
213     output.concat(m);
214     output.concat(end);
215     return output;
218 Geom::Piecewise<Geom::D2<Geom::SBasis> >
219 LPEKnot::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
221     using namespace Geom;
224     Piecewise<D2<SBasis> > output;
225     Piecewise<D2<SBasis> > m = addLinearEnds(pwd2_in);
227     Piecewise<D2<SBasis> > v = derivative(pwd2_in);
228     Piecewise<D2<SBasis> > n = unitVector(v);
230 // // -------- Pleins et delies vs courbure ou direction...
231 //     Piecewise<D2<SBasis> > a = derivative(v);
232 //     Piecewise<SBasis> a_cross_n = cross(a,n);
233 //     Piecewise<SBasis> v_dot_n = dot(v,n);
234 //     //Piecewise<D2<SBasis> > rfrac = sectionize(D2<Piecewise<SBasis> >(a_cross_n,v_dot_n));
235 //     //Piecewise<SBasis> h = atan2(rfrac)*interruption_width;
236 //     Piecewise<SBasis> h = reciprocal(curvature(pwd2_in))*interruption_width;
237 //
238 // //    Piecewise<D2<SBasis> > dir = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(0),Linear(-1)));
239 // //    Piecewise<SBasis> h = dot(n,dir)+1.;
240 // //    h *= h*(interruption_width/4.);
241 //
242 //     n = rot90(n);
243 //     output = pwd2_in+h*n;
244 //     output.concat(pwd2_in-h*n);
245 //
246 // //-----------
248     //output.concat(m);
249     return output;
251 */
253 /* ######################## */
255 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
256 } /* namespace Inkscape */
258 /*
259   Local Variables:
260   mode:c++
261   c-file-style:"stroustrup"
262   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
263   indent-tabs-mode:nil
264   fill-column:99
265   End:
266 */
267 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :