Code

fix for 2geom change (pointAndDerivative)
[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"
16 #include <2geom/path.h>
17 #include <2geom/d2.h>
18 #include <2geom/crossing.h>
19 #include <2geom/path-intersection.h>
21 namespace Inkscape {
22 namespace LivePathEffect {
24 LPEKnot::LPEKnot(LivePathEffectObject *lpeobject) :
25     Effect(lpeobject),
26     // initialise your parameters here:
27     interruption_width(_("Gap width"), _("The width of the gap in the path where it self-intersects"), "interruption_width", &wr, this, 10)
28 {
29     // register all your parameters here, so Inkscape knows which parameters this effect has:
30     registerParameter( dynamic_cast<Parameter *>(&interruption_width) );
31 }
33 LPEKnot::~LPEKnot()
34 {
36 }
38 //remove an interval from an union of intervals.
39 std::vector<Geom::Interval> complementOf(Geom::Interval I, std::vector<Geom::Interval> domain){
40     std::vector<Geom::Interval> ret;
41     double min = domain.front().min();
42     double max = domain.back().max();
43     Geom::Interval I1 = Geom::Interval(min,I.min());
44     Geom::Interval I2 = Geom::Interval(I.max(),max);
46     for (unsigned i = 0; i<domain.size(); i++){
47         boost::optional<Geom::Interval> I1i = intersect(domain.at(i),I1);
48         if (I1i) ret.push_back(I1i.get());
49         boost::optional<Geom::Interval> I2i = intersect(domain.at(i),I2);
50         if (I2i) ret.push_back(I2i.get());
51     }
52     return ret;
53 }
55 Geom::Interval
56 findShadowedTime(Geom::Path &patha, 
57                  Geom::Path &pathb, 
58                  Geom::Crossing crossing, 
59                  unsigned idx, double width){
60     using namespace Geom;
61     double curveidx, timeoncurve = modf(crossing.getOtherTime(idx),&curveidx);
62     if(curveidx == pathb.size() && timeoncurve == 0) { curveidx--; timeoncurve = 0.99999;}
63     assert(curveidx >= 0 && curveidx < pathb.size());
64     
65     std::vector<Point> MV = pathb[unsigned(curveidx)].pointAndDerivatives(timeoncurve,1);
66     Point T = unit_vector(MV.at(1));
67     Point N = T.cw();
68     Point A = MV.at(0)-10*width*T, B = MV.at(0)+10*width*T;
69     
70     std::vector<Geom::Path> cutter;
71     Geom::Path cutterLeft  = Geom::Path();
72     Geom::Path cutterRight = Geom::Path();
73     cutterLeft.append (LineSegment (A-width*N, B-width*N));
74     cutterRight.append(LineSegment (A+width*N, B+width*N));
75     cutter.push_back(cutterLeft);
76     cutter.push_back(cutterRight);
77     
78     std::vector<Geom::Path> patha_as_vect = std::vector<Geom::Path>(1,patha);
79     
80     CrossingSet crossingTable = crossings (patha_as_vect, cutter);
81     double t0 = crossing.getTime(idx);
82     double tmin = 0,tmax = patha.size()-0.0001;
83     assert(crossingTable.size()>=1);
84     for (unsigned c=0; c<crossingTable.front().size(); c++){
85         double t = crossingTable.front().at(c).ta;
86         assert(crossingTable.front().at(c).a==0);
87         if (t>tmin and t<t0) tmin = t;
88         if (t<tmax and t>t0) tmax = t;
89     }
90     //return Interval(t0-0.1,t0+0.1);
91     return Interval(tmin,tmax);
92 }                
94 //Just a try; this should be moved to 2geom if ever it works.
95 std::vector<Geom::Path>
96 split_loopy_bezier (std::vector<Geom::Path> const & path_in){
98     std::vector<Geom::Path> ret; 
99     std::vector<Geom::Path>::const_iterator pi=path_in.begin();
100     for(; pi != path_in.end(); pi++) {
101         ret.push_back(Geom::Path());
102         for (Geom::Path::const_iterator curve(pi->begin()),end(pi->end()); curve != end; ++curve){
104             //is the current curve a cubic bezier?
105             if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const *>(&(*curve))){
106                 Geom::CubicBezier theCurve = *cubic_bezier;
107                 std::vector<Geom::Point> A = theCurve.points();
109                 //is there a crossing in the polygon?
110                 if( cross(A[2]-A[0],A[1]-A[0])*cross(A[3]-A[0],A[1]-A[0])<0 &&
111                     cross(A[0]-A[3],A[2]-A[3])*cross(A[1]-A[3],A[2]-A[3])<0){
113                     //split the curve where the tangent is parallel to the chord through end points.
114                     double a = cross(A[3]-A[0],A[1]-A[2]);
115                     double c = cross(A[3]-A[0],A[1]-A[0]);
116                     double t; //where to split; solution of 3*at^2-(a+c)t +c = 0. 
117                     //TODO: don't we have a clean deg 2 equation solver?...
118                     if (fabs(a)<.0001){
119                         t = .5;
120                     }else{
121                         double delta = a*a-a*c+c*c;
122                         t = ((a+c)-sqrt(delta))/2/a;
123                         if ( t<0 || t>1 ) {t = ((a+c)+sqrt(delta))/2/a;}
124                     }
125                     //TODO: shouldn't Path have subdivide method?
126                     std::pair<Geom::BezierCurve<3>, Geom::BezierCurve<3> > splitCurve;
127                     splitCurve = theCurve.subdivide(t);
128                     ret.back().append(splitCurve.first);
129                     ret.back().append(splitCurve.second);
131                 }else{//cubic bezier but no crossing.
132                     ret.back().append(*curve);
133                 }
134             }else{//not cubic bezier.
135                 ret.back().append(*curve);
136             }
137         }
138     }
139     return ret;
143 std::vector<Geom::Path>
144 LPEKnot::doEffect_path (std::vector<Geom::Path> const & input_path)
146     using namespace Geom;
147     std::vector<Geom::Path> path_out;
148     double width = interruption_width;
150     std::vector<Geom::Path> path_in = split_loopy_bezier(input_path);
152     CrossingSet crossingTable = crossings_among(path_in);
153     for (unsigned i = 0; i < crossingTable.size(); i++){
154         std::vector<Interval> dom;
155         dom.push_back(Interval(0.,path_in.at(i).size()-0.00001));
156         //TODO: handle closed curves...
157         for (unsigned crs = 0; crs < crossingTable.at(i).size(); crs++){
158             Crossing crossing = crossingTable.at(i).at(crs);
159             unsigned j = crossing.getOther(i);
160             //TODO: select dir according to a parameter...
161             if ((crossing.dir and crossing.a==i) or (not crossing.dir and crossing.b==i) or (i==j)){
162                 if (i==j and not crossing.dir) {
163                     double temp = crossing.ta;
164                     crossing.ta = crossing.tb; 
165                     crossing.tb = temp; 
166                     crossing.dir = not crossing.dir;
167                 }
168                 Interval hidden = findShadowedTime(path_in.at(i),path_in.at(j),crossing,i,width);                
169                 dom = complementOf(hidden,dom);
170             }
171         }
172         for (unsigned comp = 0; comp < dom.size(); comp++){
173             assert(dom.at(comp).min() >=0 and dom.at(comp).max() < path_in.at(i).size());
174             path_out.push_back(path_in.at(i).portion(dom.at(comp)));
175         }
176     }   
177     return path_out;
181 /*
182 Geom::Piecewise<Geom::D2<Geom::SBasis> >
183 addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
184     using namespace Geom;
185     Piecewise<D2<SBasis> > output;
186     Piecewise<D2<SBasis> > start;
187     Piecewise<D2<SBasis> > end;
188     double x,y,vx,vy;
190     x  = m.segs.front()[0].at0();
191     y  = m.segs.front()[1].at0();
192     vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
193     vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
194     start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
195     start.offsetDomain(m.cuts.front()-1.);
197     x  = m.segs.back()[0].at1();
198     y  = m.segs.back()[1].at1();
199     vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
200     vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
201     end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
202     //end.offsetDomain(m.cuts.back());
204     output = start;
205     output.concat(m);
206     output.concat(end);
207     return output;
210 Geom::Piecewise<Geom::D2<Geom::SBasis> >
211 LPEKnot::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
213     using namespace Geom;
216     Piecewise<D2<SBasis> > output;
217     Piecewise<D2<SBasis> > m = addLinearEnds(pwd2_in);
219     Piecewise<D2<SBasis> > v = derivative(pwd2_in);
220     Piecewise<D2<SBasis> > n = unitVector(v);
222 // // -------- Pleins et delies vs courbure ou direction...
223 //     Piecewise<D2<SBasis> > a = derivative(v);
224 //     Piecewise<SBasis> a_cross_n = cross(a,n);
225 //     Piecewise<SBasis> v_dot_n = dot(v,n);
226 //     //Piecewise<D2<SBasis> > rfrac = sectionize(D2<Piecewise<SBasis> >(a_cross_n,v_dot_n));
227 //     //Piecewise<SBasis> h = atan2(rfrac)*interruption_width;
228 //     Piecewise<SBasis> h = reciprocal(curvature(pwd2_in))*interruption_width;
229 //
230 // //    Piecewise<D2<SBasis> > dir = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(0),Linear(-1)));
231 // //    Piecewise<SBasis> h = dot(n,dir)+1.;
232 // //    h *= h*(interruption_width/4.);
233 //
234 //     n = rot90(n);
235 //     output = pwd2_in+h*n;
236 //     output.concat(pwd2_in-h*n);
237 //
238 // //-----------
240     //output.concat(m);
241     return output;
243 */
245 /* ######################## */
247 } //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
248 } /* namespace Inkscape */
250 /*
251   Local Variables:
252   mode:c++
253   c-file-style:"stroustrup"
254   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
255   indent-tabs-mode:nil
256   fill-column:99
257   End:
258 */
259 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :