Code

remove many needless references to n-art-bpath.h
[inkscape.git] / src / extension / internal / pov-out.cpp
index bfbb0e4d40abd5698987427784913c67a5287a8f..01b8e64b5b3c91732fc4027c83c2fb853b20bbd7 100644 (file)
 #include <sp-path.h>
 #include <style.h>
 #include <display/curve.h>
-#include <libnr/n-art-bpath.h>
 #include <extension/system.h>
-
+#include <2geom/pathvector.h>
+#include <2geom/rect.h>
+#include <2geom/bezier-curve.h>
+#include <2geom/hvlinesegment.h>
+#include "helper/geom.h"
 #include <io/sys.h>
 
 #include <string>
@@ -198,7 +201,7 @@ void PovOutput::segment(int segNr,
 /**
  * Output the file header
  */
-void PovOutput::doHeader()
+bool PovOutput::doHeader()
 {
     time_t tim = time(NULL);
     out("/*###################################################################\n");
@@ -234,6 +237,7 @@ void PovOutput::doHeader()
     out("##    Nodes    : %d\n", nrNodes);
     out("###################################################################*/\n");
     out("\n\n\n");
+    return true;
 }
 
 
@@ -241,13 +245,14 @@ void PovOutput::doHeader()
 /**
  *  Output the file footer
  */
-void PovOutput::doTail()
+bool PovOutput::doTail()
 {
     out("\n\n");
     out("/*###################################################################\n");
     out("### E N D    F I L E\n");
     out("###################################################################*/\n");
     out("\n\n");
+    return true;
 }
 
 
@@ -255,60 +260,33 @@ void PovOutput::doTail()
 /**
  *  Output the curve data to buffer
  */
-void PovOutput::doCurves(SPDocument *doc)
+bool PovOutput::doCurve(SPItem *item, const String &id)
 {
-    std::vector<Inkscape::XML::Node *>results;
-    //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path");
-    findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL);
-    if (results.size() == 0)
-        return;
+    using Geom::X;
+    using Geom::Y;
 
-    double bignum = 1000000.0;
-    double minx  =  bignum;
-    double maxx  = -bignum;
-    double miny  =  bignum;
-    double maxy  = -bignum;
+    //### Get the Shape
+    if (!SP_IS_SHAPE(item))//Bulia's suggestion.  Allow all shapes
+        return true;
 
-    for (unsigned int indx = 0; indx < results.size() ; indx++)
-        {
-        //### Fetch the object from the repr info
-        Inkscape::XML::Node *rpath = results[indx];
-        char *str  = (char *) rpath->attribute("id");
-        if (!str)
-            continue;
+    SPShape *shape = SP_SHAPE(item);
+    SPCurve *curve = shape->curve;
+    if (curve->is_empty())
+        return true;
 
-        String id = str;
-        SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath);
-        if (!reprobj)
-            continue;
+    nrShapes++;
 
-        //### Get the transform of the item
-        if (!SP_IS_ITEM(reprobj))
-            continue;
+    PovShapeInfo shapeInfo;
+    shapeInfo.id    = id;
+    shapeInfo.color = "";
 
-        SPItem *item = SP_ITEM(reprobj);
-        NR::Matrix tf = from_2geom(sp_item_i2d_affine(item));
-
-        //### Get the Shape
-        if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion.  Allow all shapes
-            continue;
-
-        SPShape *shape = SP_SHAPE(reprobj);
-        SPCurve *curve = shape->curve;
-        if (curve->is_empty())
-            continue;
-            
-        nrShapes++;
-
-        PovShapeInfo shapeInfo;
-        shapeInfo.id    = id;
-        shapeInfo.color = "";
-
-        //Try to get the fill color of the shape
-        SPStyle *style = SP_OBJECT_STYLE(shape);
-        /* fixme: Handle other fill types, even if this means translating gradients to a single
+    //Try to get the fill color of the shape
+    SPStyle *style = SP_OBJECT_STYLE(shape);
+    /* fixme: Handle other fill types, even if this means translating gradients to a single
            flat colour. */
-        if (style && (style->fill.isColor()))
+    if (style)
+        {
+        if (style->fill.isColor())
             {
             // see color.h for how to parse SPColor
             float rgb[3];
@@ -324,150 +302,171 @@ void PovOutput::doCurves(SPDocument *doc)
             rgbf.append(dstr(1.0 - dopacity)); rgbf.append(">");
             shapeInfo.color += rgbf;
             }
+        }
 
-        povShapes.push_back(shapeInfo); //passed all tests.  save the info
+    povShapes.push_back(shapeInfo); //passed all tests.  save the info
 
-        int curveLength = SP_CURVE_LENGTH(curve);
+    // convert the path to only lineto's and cubic curveto's:
+    Geom::Matrix tf = sp_item_i2d_affine(item);
+    Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf );
 
-        //Count the NR_CURVETOs/LINETOs
-        int segmentCount=0;
-        NArtBpath const *bp = curve->get_bpath();
-        for (int curveNr=0 ; curveNr<curveLength ; curveNr++, bp++)
-            if (bp->code == NR_CURVETO || bp->code == NR_LINETO)
-                segmentCount++;
+    //Count the NR_CURVETOs/LINETOs (including closing line segment)
+    int segmentCount = 0;
+    for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it)
+           {
+        segmentCount += (*it).size();
+        if (it->closed())
+            segmentCount += 1;
+        }
 
-        double cminx  =  bignum;
-        double cmaxx  = -bignum;
-        double cminy  =  bignum;
-        double cmaxy  = -bignum;
-        double lastx  = 0.0;
-        double lasty  = 0.0;
+    out("/*###################################################\n");
+    out("### PRISM:  %s\n", id.c_str());
+    out("###################################################*/\n");
+    out("#declare %s = prism {\n", id.c_str());
+    out("    linear_sweep\n");
+    out("    bezier_spline\n");
+    out("    1.0, //top\n");
+    out("    0.0, //bottom\n");
+    out("    %d //nr points\n", segmentCount * 4);
+    int segmentNr = 0;
+
+    nrSegments += segmentCount;
+
+    /**
+     *  at moment of writing, 2geom lacks proper initialization of empty intervals in rect...
+     */     
+    Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() ); 
+   
+   
+    /**
+     * For all Subpaths in the <path>
+     */             
+    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit)
+        {
 
-        out("/*###################################################\n");
-        out("### PRISM:  %s\n", id.c_str());
-        out("###################################################*/\n");
-        out("#declare %s = prism {\n", id.c_str());
-        out("    linear_sweep\n");
-        out("    bezier_spline\n");
-        out("    1.0, //top\n");
-        out("    0.0, //bottom\n");
-        out("    %d //nr points\n", segmentCount * 4);
-        int segmentNr = 0;
-        bp = curve->get_bpath();
-        
-        nrSegments += curveLength;
+        cminmax.expandTo(pit->initialPoint());
 
-        for (int curveNr=0 ; curveNr < curveLength ; curveNr++)
-            {
-            using NR::X;
-            using NR::Y;
-            //transform points.  note overloaded '*'
-            NR::Point const p1(bp->c(1) * tf);
-            NR::Point const p2(bp->c(2) * tf);
-            NR::Point const p3(bp->c(3) * tf);
-            double const x1 = p1[X], y1 = p1[Y];
-            double const x2 = p2[X], y2 = p2[Y];
-            double const x3 = p3[X], y3 = p3[Y];
-
-            switch (bp->code)
+        /**
+         * For all segments in the subpath
+         */                     
+        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit)
+                   {
+
+            if( dynamic_cast<Geom::LineSegment const *> (&*cit) ||
+                    dynamic_cast<Geom::HLineSegment const *>(&*cit) ||
+                    dynamic_cast<Geom::VLineSegment const *>(&*cit) )
                 {
-                case NR_MOVETO:
-                case NR_MOVETO_OPEN:
-                    {
-                    //fprintf(f, "moveto: %f %f\n", bp->x3, bp->y3);
-                    break;
-                    }
-                case NR_CURVETO:
-                    {
-                    //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
-                    //        segmentNr++, lastx, lasty, x1, y1, x2, y2, x3, y3);
-                    segment(segmentNr++,
-                            lastx, lasty, x1, y1, x2, y2, x3, y3);
-                    nrNodes += 8;
-
-                    if (segmentNr < segmentCount)
-                        out(",\n");
-                    else
-                        out("\n");
-
-                    if (lastx < cminx)
-                        cminx = lastx;
-                    if (lastx > cmaxx)
-                        cmaxx = lastx;
-                    if (lasty < cminy)
-                        cminy = lasty;
-                    if (lasty > cmaxy)
-                        cmaxy = lasty;
-                    break;
-                    }
-                case NR_LINETO:
-                    {
-                    //NOTE: we need to carefully handle line->curve and curve->line
-                    //    transitions.
-                    //fprintf(f, "    /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
-                    //        segmentNr++, lastx, lasty, lastx, lasty, x3, y3, x3, y3);
-                    segment(segmentNr++,
-                            lastx, lasty, lastx, lasty, x3, y3, x3, y3);
-                    nrNodes += 8;
-
-                    if (segmentNr < segmentCount)
-                        out(",\n");
-                    else
-                        out("\n");
-
-                    //fprintf(f, "lineto\n");
-                    if (lastx < cminx)
-                        cminx = lastx;
-                    if (lastx > cmaxx)
-                        cmaxx = lastx;
-                    if (lasty < cminy)
-                        cminy = lasty;
-                    if (lasty > cmaxy)
-                        cmaxy = lasty;
-                    break;
-                    }
-                case NR_END:
-                    {
-                    //fprintf(f, "end\n");
-                    break;
-                    }
+                Geom::Point p0 = cit->initialPoint();
+                Geom::Point p1 = cit->finalPoint();
+                segment(segmentNr++,
+                        p0[X], p0[Y], p0[X], p0[Y], p1[X], p1[Y], p1[X], p1[Y] );
+                nrNodes += 8;
                 }
-            lastx = x3;
-            lasty = y3;
-            bp++;
+            else if(Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
+                           {
+                std::vector<Geom::Point> points = cubic->points();
+                Geom::Point p0 = points[0];
+                Geom::Point p1 = points[1];
+                Geom::Point p2 = points[2];
+                Geom::Point p3 = points[3];
+                segment(segmentNr++,
+                            p0[X],p0[Y], p1[X],p1[Y], p2[X],p2[Y], p3[X],p3[Y]);
+                nrNodes += 8;
+                }
+            else
+                           {
+                g_warning("logical error, because pathv_to_linear_and_cubic_beziers was used");
+                return false;
+                }
+
+            if (segmentNr < segmentCount)
+                out(",\n");
+            else
+                out("\n");
+
+            cminmax.expandTo(cit->finalPoint());
+
             }
-        out("}\n");
+        }
 
+    out("}\n");
+
+    double cminx = cminmax.min()[X];
+    double cmaxx = cminmax.max()[X];
+    double cminy = cminmax.min()[Y];
+    double cmaxy = cminmax.max()[Y];
+
+    out("#declare %s_MIN_X    = %s;\n", id.c_str(), dstr(cminx).c_str());
+    out("#declare %s_CENTER_X = %s;\n", id.c_str(), dstr((cmaxx+cminx)/2.0).c_str());
+    out("#declare %s_MAX_X    = %s;\n", id.c_str(), dstr(cmaxx).c_str());
+    out("#declare %s_WIDTH    = %s;\n", id.c_str(), dstr(cmaxx-cminx).c_str());
+    out("#declare %s_MIN_Y    = %s;\n", id.c_str(), dstr(cminy).c_str());
+    out("#declare %s_CENTER_Y = %s;\n", id.c_str(), dstr((cmaxy+cminy)/2.0).c_str());
+    out("#declare %s_MAX_Y    = %s;\n", id.c_str(), dstr(cmaxy).c_str());
+    out("#declare %s_HEIGHT   = %s;\n", id.c_str(), dstr(cmaxy-cminy).c_str());
+    if (shapeInfo.color.length()>0)
+        out("#declare %s_COLOR    = %s;\n",
+                id.c_str(), shapeInfo.color.c_str());
+    out("/*###################################################\n");
+    out("### end %s\n", id.c_str());
+    out("###################################################*/\n\n\n\n");
+
+    if (cminx < minx)
+        minx = cminx;
+    if (cmaxx > maxx)
+        maxx = cmaxx;
+    if (cminy < miny)
+        miny = cminy;
+    if (cmaxy > maxy)
+        maxy = cmaxy;
 
-        //# prefix for following declarations
-           char *pfx = (char *)id.c_str();
-
-        out("#declare %s_MIN_X    = %s;\n", pfx, dstr(cminx).c_str());
-        out("#declare %s_CENTER_X = %s;\n", pfx, dstr((cmaxx+cminx)/2.0).c_str());
-        out("#declare %s_MAX_X    = %s;\n", pfx, dstr(cmaxx).c_str());
-        out("#declare %s_WIDTH    = %s;\n", pfx, dstr(cmaxx-cminx).c_str());
-        out("#declare %s_MIN_Y    = %s;\n", pfx, dstr(cminy).c_str());
-        out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((cmaxy+cminy)/2.0).c_str());
-        out("#declare %s_MAX_Y    = %s;\n", pfx, dstr(cmaxy).c_str());
-        out("#declare %s_HEIGHT   = %s;\n", pfx, dstr(cmaxy-cminy).c_str());
-        if (shapeInfo.color.length()>0)
-            out("#declare %s_COLOR    = %s;\n",
-                    pfx, shapeInfo.color.c_str());
-        out("/*###################################################\n");
-        out("### end %s\n", id.c_str());
-        out("###################################################*/\n\n\n\n");
-        if (cminx < minx)
-            minx = cminx;
-        if (cmaxx > maxx)
-            maxx = cmaxx;
-        if (cminy < miny)
-            miny = cminy;
-        if (cmaxy > maxy)
-            maxy = cmaxy;
+    return true;
+}
 
-        }//for
+/**
+ *  Output the curve data to buffer
+ */
+bool PovOutput::doCurvesRecursive(SPDocument *doc, Inkscape::XML::Node *node)
+{
+    /**
+     * If the object is an Item, try processing it
+     */             
+    char *str  = (char *) node->attribute("id");
+    SPObject *reprobj = doc->getObjectByRepr(node);
+    if (SP_IS_ITEM(reprobj) && str)
+        {
+        SPItem *item = SP_ITEM(reprobj);
+        String id = str;
+        if (!doCurve(item, id))
+            return false;
+        }
 
+    /**
+     * Descend into children
+     */             
+    for (Inkscape::XML::Node *child = node->firstChild() ; child ;
+              child = child->next())
+        {
+               if (!doCurvesRecursive(doc, child))
+                   return false;
+               }
 
+    return true;
+}
+
+/**
+ *  Output the curve data to buffer
+ */
+bool PovOutput::doCurves(SPDocument *doc)
+{
+    double bignum = 1000000.0;
+    minx  =  bignum;
+    maxx  = -bignum;
+    miny  =  bignum;
+    maxy  = -bignum;
+
+    if (!doCurvesRecursive(doc, doc->rroot))
+        return false;
 
     //## Let's make a union of all of the Shapes
     if (povShapes.size()>0)
@@ -552,11 +551,10 @@ void PovOutput::doCurves(SPDocument *doc)
         out("\n\n");
         }
 
+    return true;
 }
 
 
-
-
 //########################################################################
 //# M A I N    O U T P U T
 //########################################################################
@@ -578,7 +576,7 @@ void PovOutput::reset()
 
 
 /**
- * Saves the <paths> of an Inkscape SVG file as PovRay spline definitions
+ * Saves the Shapes of an Inkscape SVG file as PovRay spline definitions
  */
 void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
 {
@@ -586,15 +584,28 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
 
     //###### SAVE IN POV FORMAT TO BUFFER
     //# Lets do the curves first, to get the stats
-    doCurves(doc);
+    if (!doCurves(doc))
+        {
+        g_warning("Could not output curves for %s\n", uri);
+        return;
+        }
+        
     String curveBuf = outbuf;
     outbuf.clear();
 
-    doHeader();
-    
+    if (!doHeader())
+        {
+        g_warning("Could not write header for %s\n", uri);
+        return;
+        }
+
     outbuf.append(curveBuf);
-    
-    doTail();
+
+    if (!doTail())
+        {
+        g_warning("Could not write footer for %s\n", uri);
+        return;
+        }
 
 
 
@@ -610,7 +621,7 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri)
         int ch = *iter;
         fputc(ch, f);
         }
-        
+
     fclose(f);
 }