Code

Rename LPE: mirror reflect --> mirror symmetry
[inkscape.git] / src / live_effects / lpe-knot.cpp
index 3e87bc51bbf03bb03d8548f9f2364e316ae314a5..678bbf8ba070a0aaad8c2b9f69e8f36e5b507fce 100644 (file)
-#define INKSCAPE_LPE_KNOT_CPP\r
-/** \file\r
- * LPE <knot> implementation\r
- */\r
-/*\r
- * Authors:\r
- *   Johan Engelen\r
-*\r
-* Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
- *\r
- * Released under GNU GPL, read the file 'COPYING' for more information\r
- */\r
-\r
-#include "live_effects/lpe-knot.h"\r
-#include "display/curve.h"\r
-#include <libnr/n-art-bpath.h>\r
-\r
-// You might need to include other 2geom files. You can add them here:\r
-#include <2geom/path.h>\r
-#include <2geom/sbasis.h>\r
-#include <2geom/sbasis-geometric.h>\r
-#include <2geom/bezier-to-sbasis.h>\r
-#include <2geom/sbasis-to-bezier.h>\r
-#include <2geom/d2.h>\r
-#include <2geom/sbasis-math.h>\r
-#include <2geom/piecewise.h>\r
-#include <2geom/crossing.h>\r
-#include <2geom/path-intersection.h>\r
-\r
-namespace Inkscape {\r
-namespace LivePathEffect {\r
-\r
-LPEKnot::LPEKnot(LivePathEffectObject *lpeobject) :\r
-    Effect(lpeobject),\r
-    // initialise your parameters here:\r
-    interruption_width(_("Interruption width"), _("Howmuch the lower strand is obscured by the upper."), "interruption_width", &wr, this, 1.2)\r
-{\r
-    // register all your parameters here, so Inkscape knows which parameters this effect has:\r
-    registerParameter( dynamic_cast<Parameter *>(&interruption_width) );\r
-}\r
-\r
-LPEKnot::~LPEKnot()\r
-{\r
-\r
-}\r
-\r
-\r
-/* ########################\r
- *  Choose to implement one of the doEffect functions. You can delete or comment out the others.\r
-*/\r
-\r
-/*\r
-void\r
-LPEKnot::doEffect (SPCurve * curve)\r
-{\r
-    // spice this up to make the effect actually *do* something!\r
-}\r
-\r
-NArtBpath *\r
-LPEKnot::doEffect_nartbpath (NArtBpath * path_in)\r
-{\r
-        NArtBpath *path_out;\r
-        unsigned ret = 0;\r
-        while ( path_in[ret].code != NR_END ) {\r
-            ++ret;\r
-        }\r
-        unsigned len = ++ret;\r
-        path_out = g_new(NArtBpath, len);\r
-\r
-        memcpy(path_out, path_in, len * sizeof(NArtBpath));   // spice this up to make the effect actually *do* something!\r
-\r
-        return path_out;\r
-}\r
-*/\r
-\r
-std::vector<Geom::Interval> complementOf(Geom::Interval I, std::vector<Geom::Interval> domain){\r
-    std::vector<Geom::Interval> ret;\r
-    double min = domain.front().min();\r
-    double max = domain.back().max();\r
-    Geom::Interval I1 = Geom::Interval(min,I.min());\r
-    Geom::Interval I2 = Geom::Interval(I.max(),max);\r
-\r
-    for (unsigned i = 0; i<domain.size(); i++){\r
-        boost::optional<Geom::Interval> I1i = intersect(domain.at(i),I1);\r
-        if (I1i) ret.push_back(I1i.get());\r
-        boost::optional<Geom::Interval> I2i = intersect(domain.at(i),I2);\r
-        if (I2i) ret.push_back(I2i.get());\r
-    }\r
-    return ret;\r
-}\r
-\r
-Geom::Interval\r
-findShadowedTime(Geom::Path &patha, \r
-                 Geom::Path &pathb, \r
-                 Geom::Crossing crossing, \r
-                 unsigned idx, double width){\r
-    using namespace Geom;\r
-    double curveidx, timeoncurve = modf(crossing.getOtherTime(idx),&curveidx);\r
-    if(curveidx == pathb.size() && timeoncurve == 0) { curveidx--; timeoncurve = 0.99999;}\r
-    assert(curveidx >= 0 && curveidx < pathb.size());\r
-    \r
-    std::vector<Point> MV = pathb[unsigned(curveidx)].pointAndDerivatives(timeoncurve,2);\r
-    Point T = unit_vector(MV.at(1));\r
-    Point N = T.cw();\r
-    Point A = MV.at(0)-10*width*T, B = MV.at(0)+10*width*T;\r
-    \r
-    std::vector<Geom::Path> cutter;\r
-    Geom::Path cutterLeft  = Geom::Path();\r
-    Geom::Path cutterRight = Geom::Path();\r
-    cutterLeft.append (LineSegment (A-width*N, B-width*N));\r
-    cutterRight.append(LineSegment (A+width*N, B+width*N));\r
-    cutter.push_back(cutterLeft);\r
-    cutter.push_back(cutterRight);\r
-    \r
-    std::vector<Geom::Path> patha_as_vect = std::vector<Geom::Path>(1,patha);\r
-    \r
-    CrossingSet crossingTable = crossings (patha_as_vect, cutter);\r
-    double t0 = crossing.getTime(idx);\r
-    double tmin = 0,tmax = patha.size()-0.0001;\r
-    assert(crossingTable.size()>=1);\r
-    for (unsigned c=0; c<crossingTable.front().size(); c++){\r
-        double t = crossingTable.front().at(c).ta;\r
-        assert(crossingTable.front().at(c).a==0);\r
-        if (t>tmin and t<t0) tmin = t;\r
-        if (t<tmax and t>t0) tmax = t;\r
-    }\r
-    //return Interval(t0-0.1,t0+0.1);\r
-    return Interval(tmin,tmax);\r
-}                \r
-\r
-\r
-std::vector<Geom::Path>\r
-LPEKnot::doEffect_path (std::vector<Geom::Path> & path_in)\r
-{\r
-    using namespace Geom;\r
-    std::vector<Geom::Path> path_out;\r
-    double width = interruption_width;\r
-    \r
-    CrossingSet crossingTable = crossings_among(path_in);\r
-    for (unsigned i = 0; i < crossingTable.size(); i++){\r
-        std::vector<Interval> dom;\r
-        dom.push_back(Interval(0.,path_in.at(i).size()-0.00001));\r
-        //TODO: handle closed curves...\r
-        for (unsigned crs = 0; crs < crossingTable.at(i).size(); crs++){\r
-            Crossing crossing = crossingTable.at(i).at(crs);\r
-            unsigned j = crossing.getOther(i);\r
-            //TODO: select dir according to a parameter...\r
-            if ((crossing.dir and crossing.a==i) or (not crossing.dir and crossing.b==i) or (i==j)){\r
-                if (i==j and not crossing.dir) {\r
-                    double temp = crossing.ta;\r
-                    crossing.ta = crossing.tb; \r
-                    crossing.tb = temp; \r
-                    crossing.dir = not crossing.dir;\r
-                }\r
-                Interval hidden = findShadowedTime(path_in.at(i),path_in.at(j),crossing,i,width);                \r
-                dom = complementOf(hidden,dom);\r
-            }\r
-        }\r
-        for (unsigned comp = 0; comp < dom.size(); comp++){\r
-            assert(dom.at(comp).min() >=0 and dom.at(comp).max() < path_in.at(i).size());\r
-            path_out.push_back(path_in.at(i).portion(dom.at(comp)));\r
-        }\r
-        \r
-//         std::vector<Point> MV = path_in[0][0].pointAndDerivatives(crossingTable[0][0].getTime(0),2);\r
-//         Point U = unit_vector(MV[1]);\r
-//         Point N = U.cw();\r
-//         Point A = MV[0]-10.*width*U, B = MV[0]+10*width*U;\r
-        \r
-//         Geom::Path cutter;\r
-//         cutter = Geom::Path();\r
-//         cutter.append( LineSegment(A+width*N,B+width*N));\r
-//         path_out.push_back(cutter);\r
-//         cutter = Geom::Path();\r
-//         cutter.append( LineSegment(A-width*N,B-width*N));\r
-//         path_out.push_back(cutter);\r
-\r
-    }   \r
-    return path_out;\r
-}\r
-\r
-\r
-/*\r
-Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
-addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){\r
-    using namespace Geom;\r
-    Piecewise<D2<SBasis> > output;\r
-    Piecewise<D2<SBasis> > start;\r
-    Piecewise<D2<SBasis> > end;\r
-    double x,y,vx,vy;\r
-\r
-    x  = m.segs.front()[0].at0();\r
-    y  = m.segs.front()[1].at0();\r
-    vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);\r
-    vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);\r
-    start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));\r
-    start.offsetDomain(m.cuts.front()-1.);\r
-\r
-    x  = m.segs.back()[0].at1();\r
-    y  = m.segs.back()[1].at1();\r
-    vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;\r
-    vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;\r
-    end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));\r
-    //end.offsetDomain(m.cuts.back());\r
-\r
-    output = start;\r
-    output.concat(m);\r
-    output.concat(end);\r
-    return output;\r
-}\r
-\r
-Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
-LPEKnot::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)\r
-{\r
-    using namespace Geom;\r
-\r
-\r
-    Piecewise<D2<SBasis> > output;\r
-    Piecewise<D2<SBasis> > m = addLinearEnds(pwd2_in);\r
-\r
-    Piecewise<D2<SBasis> > v = derivative(pwd2_in);\r
-    Piecewise<D2<SBasis> > n = unitVector(v);\r
-\r
-// // -------- Pleins et delies vs courbure ou direction...\r
-//     Piecewise<D2<SBasis> > a = derivative(v);\r
-//     Piecewise<SBasis> a_cross_n = cross(a,n);\r
-//     Piecewise<SBasis> v_dot_n = dot(v,n);\r
-//     //Piecewise<D2<SBasis> > rfrac = sectionize(D2<Piecewise<SBasis> >(a_cross_n,v_dot_n));\r
-//     //Piecewise<SBasis> h = atan2(rfrac)*interruption_width;\r
-//     Piecewise<SBasis> h = reciprocal(curvature(pwd2_in))*interruption_width;\r
-//\r
-// //    Piecewise<D2<SBasis> > dir = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(0),Linear(-1)));\r
-// //    Piecewise<SBasis> h = dot(n,dir)+1.;\r
-// //    h *= h*(interruption_width/4.);\r
-//\r
-//     n = rot90(n);\r
-//     output = pwd2_in+h*n;\r
-//     output.concat(pwd2_in-h*n);\r
-//\r
-// //-----------\r
-\r
-    //output.concat(m);\r
-    return output;\r
-}\r
-*/\r
-\r
-/* ######################## */\r
-\r
-} //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")\r
-} /* namespace Inkscape */\r
-\r
-/*\r
-  Local Variables:\r
-  mode:c++\r
-  c-file-style:"stroustrup"\r
-  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
-  indent-tabs-mode:nil\r
-  fill-column:99\r
-  End:\r
-*/\r
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r
+#define INKSCAPE_LPE_KNOT_CPP
+/** \file
+ * LPE <knot> implementation
+ */
+/*
+ * Authors:
+ *   JF Barraud
+*
+* Copyright (C) JF Barraud 2007 <jf.barraud@gmail.com>
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "live_effects/lpe-knot.h"
+
+#include <2geom/path.h>
+#include <2geom/d2.h>
+#include <2geom/crossing.h>
+#include <2geom/path-intersection.h>
+
+namespace Inkscape {
+namespace LivePathEffect {
+
+LPEKnot::LPEKnot(LivePathEffectObject *lpeobject) :
+    Effect(lpeobject),
+    // initialise your parameters here:
+    interruption_width(_("Gap width"), _("The width of the gap in the path where it self-intersects"), "interruption_width", &wr, this, 10)
+{
+    // register all your parameters here, so Inkscape knows which parameters this effect has:
+    registerParameter( dynamic_cast<Parameter *>(&interruption_width) );
+}
+
+LPEKnot::~LPEKnot()
+{
+
+}
+
+//remove an interval from an union of intervals.
+std::vector<Geom::Interval> complementOf(Geom::Interval I, std::vector<Geom::Interval> domain){
+    std::vector<Geom::Interval> ret;
+    double min = domain.front().min();
+    double max = domain.back().max();
+    Geom::Interval I1 = Geom::Interval(min,I.min());
+    Geom::Interval I2 = Geom::Interval(I.max(),max);
+
+    for (unsigned i = 0; i<domain.size(); i++){
+        boost::optional<Geom::Interval> I1i = intersect(domain.at(i),I1);
+        if (I1i) ret.push_back(I1i.get());
+        boost::optional<Geom::Interval> I2i = intersect(domain.at(i),I2);
+        if (I2i) ret.push_back(I2i.get());
+    }
+    return ret;
+}
+
+Geom::Interval
+findShadowedTime(Geom::Path &patha, 
+                 Geom::Path &pathb, 
+                 Geom::Crossing crossing, 
+                 unsigned idx, double width){
+    using namespace Geom;
+    double curveidx, timeoncurve = modf(crossing.getOtherTime(idx),&curveidx);
+    if(curveidx == pathb.size() && timeoncurve == 0) { curveidx--; timeoncurve = 0.99999;}
+    assert(curveidx >= 0 && curveidx < pathb.size());
+    
+    std::vector<Point> MV = pathb[unsigned(curveidx)].pointAndDerivatives(timeoncurve,1);
+    Point T = unit_vector(MV.at(1));
+    Point N = T.cw();
+    Point A = MV.at(0)-10*width*T, B = MV.at(0)+10*width*T;
+    
+    std::vector<Geom::Path> cutter;
+    Geom::Path cutterLeft  = Geom::Path();
+    Geom::Path cutterRight = Geom::Path();
+    cutterLeft.append (LineSegment (A-width*N, B-width*N));
+    cutterRight.append(LineSegment (A+width*N, B+width*N));
+    cutter.push_back(cutterLeft);
+    cutter.push_back(cutterRight);
+    
+    std::vector<Geom::Path> patha_as_vect = std::vector<Geom::Path>(1,patha);
+    
+    CrossingSet crossingTable = crossings (patha_as_vect, cutter);
+    double t0 = crossing.getTime(idx);
+    double tmin = 0,tmax = patha.size()-0.0001;
+    assert(crossingTable.size()>=1);
+    for (unsigned c=0; c<crossingTable.front().size(); c++){
+        double t = crossingTable.front().at(c).ta;
+        assert(crossingTable.front().at(c).a==0);
+        if (t>tmin and t<t0) tmin = t;
+        if (t<tmax and t>t0) tmax = t;
+    }
+    //return Interval(t0-0.1,t0+0.1);
+    return Interval(tmin,tmax);
+}                
+
+//Just a try; this should be moved to 2geom if ever it works.
+std::vector<Geom::Path>
+split_loopy_bezier (std::vector<Geom::Path> const & path_in){
+
+    std::vector<Geom::Path> ret; 
+    std::vector<Geom::Path>::const_iterator pi=path_in.begin();
+    for(; pi != path_in.end(); pi++) {
+        ret.push_back(Geom::Path());
+        for (Geom::Path::const_iterator curve(pi->begin()),end(pi->end()); curve != end; ++curve){
+
+            //is the current curve a cubic bezier?
+            if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const *>(&(*curve))){
+                Geom::CubicBezier theCurve = *cubic_bezier;
+                std::vector<Geom::Point> A = theCurve.points();
+
+                //is there a crossing in the polygon?
+                if( cross(A[2]-A[0],A[1]-A[0])*cross(A[3]-A[0],A[1]-A[0])<0 &&
+                    cross(A[0]-A[3],A[2]-A[3])*cross(A[1]-A[3],A[2]-A[3])<0){
+
+                    //split the curve where the tangent is parallel to the chord through end points.
+                    double a = cross(A[3]-A[0],A[1]-A[2]);
+                    double c = cross(A[3]-A[0],A[1]-A[0]);
+                    double t; //where to split; solution of 3*at^2-(a+c)t +c = 0. 
+                    //TODO: don't we have a clean deg 2 equation solver?...
+                    if (fabs(a)<.0001){
+                        t = .5;
+                    }else{
+                        double delta = a*a-a*c+c*c;
+                        t = ((a+c)-sqrt(delta))/2/a;
+                        if ( t<0 || t>1 ) {t = ((a+c)+sqrt(delta))/2/a;}
+                    }
+                    //TODO: shouldn't Path have subdivide method?
+                    std::pair<Geom::BezierCurve<3>, Geom::BezierCurve<3> > splitCurve;
+                    splitCurve = theCurve.subdivide(t);
+                    ret.back().append(splitCurve.first);
+                    ret.back().append(splitCurve.second);
+
+                }else{//cubic bezier but no crossing.
+                    ret.back().append(*curve);
+                }
+            }else{//not cubic bezier.
+                ret.back().append(*curve);
+            }
+        }
+    }
+    return ret;
+}
+
+
+std::vector<Geom::Path>
+LPEKnot::doEffect_path (std::vector<Geom::Path> const & input_path)
+{
+    using namespace Geom;
+    std::vector<Geom::Path> path_out;
+    double width = interruption_width;
+
+    std::vector<Geom::Path> path_in = split_loopy_bezier(input_path);
+
+    CrossingSet crossingTable = crossings_among(path_in);
+    for (unsigned i = 0; i < crossingTable.size(); i++){
+        std::vector<Interval> dom;
+        dom.push_back(Interval(0.,path_in.at(i).size()-0.00001));
+        //TODO: handle closed curves...
+        for (unsigned crs = 0; crs < crossingTable.at(i).size(); crs++){
+            Crossing crossing = crossingTable.at(i).at(crs);
+            unsigned j = crossing.getOther(i);
+            //TODO: select dir according to a parameter...
+            if ((crossing.dir and crossing.a==i) or (not crossing.dir and crossing.b==i) or (i==j)){
+                if (i==j and not crossing.dir) {
+                    double temp = crossing.ta;
+                    crossing.ta = crossing.tb; 
+                    crossing.tb = temp; 
+                    crossing.dir = not crossing.dir;
+                }
+                Interval hidden = findShadowedTime(path_in.at(i),path_in.at(j),crossing,i,width);                
+                dom = complementOf(hidden,dom);
+            }
+        }
+        for (unsigned comp = 0; comp < dom.size(); comp++){
+            assert(dom.at(comp).min() >=0 and dom.at(comp).max() < path_in.at(i).size());
+            path_out.push_back(path_in.at(i).portion(dom.at(comp)));
+        }
+    }   
+    return path_out;
+}
+
+
+/*
+Geom::Piecewise<Geom::D2<Geom::SBasis> >
+addLinearEnds (Geom::Piecewise<Geom::D2<Geom::SBasis> > & m){
+    using namespace Geom;
+    Piecewise<D2<SBasis> > output;
+    Piecewise<D2<SBasis> > start;
+    Piecewise<D2<SBasis> > end;
+    double x,y,vx,vy;
+
+    x  = m.segs.front()[0].at0();
+    y  = m.segs.front()[1].at0();
+    vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]);
+    vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]);
+    start = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x-vx,x),Linear (y-vy,y)));
+    start.offsetDomain(m.cuts.front()-1.);
+
+    x  = m.segs.back()[0].at1();
+    y  = m.segs.back()[1].at1();
+    vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);;
+    vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);;
+    end = Piecewise<D2<SBasis> >(D2<SBasis>(Linear (x,x+vx),Linear (y,y+vy)));
+    //end.offsetDomain(m.cuts.back());
+
+    output = start;
+    output.concat(m);
+    output.concat(end);
+    return output;
+}
+
+Geom::Piecewise<Geom::D2<Geom::SBasis> >
+LPEKnot::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > & pwd2_in)
+{
+    using namespace Geom;
+
+
+    Piecewise<D2<SBasis> > output;
+    Piecewise<D2<SBasis> > m = addLinearEnds(pwd2_in);
+
+    Piecewise<D2<SBasis> > v = derivative(pwd2_in);
+    Piecewise<D2<SBasis> > n = unitVector(v);
+
+// // -------- Pleins et delies vs courbure ou direction...
+//     Piecewise<D2<SBasis> > a = derivative(v);
+//     Piecewise<SBasis> a_cross_n = cross(a,n);
+//     Piecewise<SBasis> v_dot_n = dot(v,n);
+//     //Piecewise<D2<SBasis> > rfrac = sectionize(D2<Piecewise<SBasis> >(a_cross_n,v_dot_n));
+//     //Piecewise<SBasis> h = atan2(rfrac)*interruption_width;
+//     Piecewise<SBasis> h = reciprocal(curvature(pwd2_in))*interruption_width;
+//
+// //    Piecewise<D2<SBasis> > dir = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(0),Linear(-1)));
+// //    Piecewise<SBasis> h = dot(n,dir)+1.;
+// //    h *= h*(interruption_width/4.);
+//
+//     n = rot90(n);
+//     output = pwd2_in+h*n;
+//     output.concat(pwd2_in-h*n);
+//
+// //-----------
+
+    //output.concat(m);
+    return output;
+}
+*/
+
+/* ######################## */
+
+} //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN")
+} /* namespace Inkscape */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :