Code

2geomify pov-out extension
authorjohanengelen <johanengelen@users.sourceforge.net>
Sun, 13 Jul 2008 18:43:05 +0000 (18:43 +0000)
committerjohanengelen <johanengelen@users.sourceforge.net>
Sun, 13 Jul 2008 18:43:05 +0000 (18:43 +0000)
src/display/curve.cpp
src/extension/internal/pov-out.cpp
src/helper/geom.cpp
src/helper/geom.h

index c229ec28a533ebc5b9b2ff28c7a4b366d50bead3..e10ebcb32c8dde67c3aab84f1406bf1048c6aa52 100644 (file)
@@ -226,6 +226,7 @@ SPCurve::get_length() const
 
 /*
  * Returns the number of segments of all paths summed
+ * This count includes the closing line segment of a closed path.
  */
 guint
 SPCurve::get_segment_count() const
index bfbb0e4d40abd5698987427784913c67a5287a8f..2ccbfd0f2a2d932c19f6f77f1e2e31d82079d832 100644 (file)
@@ -28,7 +28,9 @@
 #include <display/curve.h>
 #include <libnr/n-art-bpath.h>
 #include <extension/system.h>
-
+#include <2geom/pathvector.h>
+#include <2geom/rect.h>
+#include "helper/geom.h"
 #include <io/sys.h>
 
 #include <string>
@@ -257,6 +259,9 @@ void PovOutput::doTail()
  */
 void PovOutput::doCurves(SPDocument *doc)
 {
+    using Geom::X;
+    using Geom::Y;
+
     std::vector<Inkscape::XML::Node *>results;
     //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path");
     findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL);
@@ -287,7 +292,7 @@ void PovOutput::doCurves(SPDocument *doc)
             continue;
 
         SPItem *item = SP_ITEM(reprobj);
-        NR::Matrix tf = from_2geom(sp_item_i2d_affine(item));
+        Geom::Matrix tf = sp_item_i2d_affine(item);
 
         //### Get the Shape
         if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion.  Allow all shapes
@@ -327,21 +332,16 @@ void PovOutput::doCurves(SPDocument *doc)
 
         povShapes.push_back(shapeInfo); //passed all tests.  save the info
 
-        int curveLength = SP_CURVE_LENGTH(curve);
-
-        //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++;
+        // convert the path to only lineto's and cubic curveto's:
+        Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf );
 
-        double cminx  =  bignum;
-        double cmaxx  = -bignum;
-        double cminy  =  bignum;
-        double cmaxy  = -bignum;
-        double lastx  = 0.0;
-        double lasty  = 0.0;
+        //Count the NR_CURVETOs/LINETOs (including closing line segment)
+        guint segmentCount = 0;
+        for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
+            segmentCount += (*it).size();
+            if (it->closed())
+                segmentCount += 1;
+        }
 
         out("/*###################################################\n");
         out("### PRISM:  %s\n", id.c_str());
@@ -353,94 +353,54 @@ void PovOutput::doCurves(SPDocument *doc)
         out("    0.0, //bottom\n");
         out("    %d //nr points\n", segmentCount * 4);
         int segmentNr = 0;
-        bp = curve->get_bpath();
-        
-        nrSegments += curveLength;
 
-        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)
+        nrSegments += segmentCount;
+
+        Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() );  // at moment of writing, 2geom lacks proper initialization of empty intervals in rect...
+        for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
+
+            cminmax.expandTo(pit->initialPoint());
+
+            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);
+                            cit->initialPoint()[X], cit->initialPoint()[Y], cit->initialPoint()[X], cit->initialPoint()[Y],
+                            cit->finalPoint()[X], cit->finalPoint()[Y], cit->finalPoint()[X], cit->finalPoint()[Y] );
                     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);
+                }
+                else if(Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit)) {
+                    std::vector<Geom::Point> points = cubic->points();
                     segment(segmentNr++,
-                            lastx, lasty, lastx, lasty, x3, y3, x3, y3);
+                            points[0][X],points[0][Y], points[1][X],points[1][Y], points[2][X],points[2][Y], points[3][X],points[3][Y]);
                     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;
-                    }
                 }
-            lastx = x3;
-            lasty = y3;
-            bp++;
+                else {
+                    g_error ("logical error, because pathv_to_linear_and_cubic_beziers was used");
+                }
+
+                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];
+                     
         //# prefix for following declarations
-           char *pfx = (char *)id.c_str();
+        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());
index 39dd2414aafa097d73961f873bed1850d5e9c774..f509485bda4274de91cfcd873923f52b779f1787 100644 (file)
@@ -481,6 +481,44 @@ pathv_matrix_point_bbox_wind_distance (Geom::PathVector const & pathv, NR::Matri
 }
 //#################################################################################
 
+/*
+ * Converts all segments in all paths to Geom::LineSegment or Geom::HLineSegment or
+ * Geom::VLineSegment or Geom::CubicBezier.
+ */
+Geom::PathVector
+pathv_to_linear_and_cubic_beziers( Geom::PathVector const &pathv )
+{
+    Geom::PathVector output;
+
+    for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
+        output.push_back( Geom::Path() );
+        output.back().start( pit->initialPoint() );
+        output.back().close( pit->closed() );
+
+        for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit) {
+            if( dynamic_cast<Geom::LineSegment const*>(&*cit) ||
+                dynamic_cast<Geom::HLineSegment const*>(&*cit) ||
+                dynamic_cast<Geom::VLineSegment const*>(&*cit) )
+            {
+                output.back().append(*cit);
+            }
+            else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&*cit)) {
+                output.back().append(*cit);
+            }
+            else {
+                // convert all other curve types to cubicbeziers
+                Geom::Path cubicbezier_path = Geom::cubicbezierpath_from_sbasis(cit->toSBasis(), 0.1);
+
+                for(Geom::Path::iterator iter = cubicbezier_path.begin(); iter != cubicbezier_path.end(); ++iter) {
+                    output.back().append(*iter);
+                }
+            }
+        }
+    }
+    
+    return output;
+}
+
 
 
 
index e8eef075bbf9eb4108dd834b3b4f72c374ae17d4..9b6dd0f35b8aaffaaa1a1d311effc42b14328f83 100644 (file)
@@ -26,6 +26,8 @@ void pathv_matrix_point_bbox_wind_distance ( Geom::PathVector const & pathv, Geo
                                              Geom::Rect *bbox, int *wind, Geom::Coord *dist,
                                              Geom::Coord tolerance, Geom::Rect const *viewbox);
 
+Geom::PathVector pathv_to_linear_and_cubic_beziers( Geom::PathVector const &pathv );
+
 #endif  // INKSCAPE_HELPER_GEOM_H
 
 /*