Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / trace / trace.cpp
index 13885eb5bf2082a2c133a7faa897fc8d1941a5fe..cff464e9f1fba1c57e71fd953c17eced7f9a59f5 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * A generic interface for plugging different
  *  autotracers into Inkscape.
  *
@@ -27,6 +27,8 @@
 #include <sp-item.h>
 #include <sp-shape.h>
 #include <sp-image.h>
+#include <libnr/nr-matrix-ops.h>
+#include <2geom/transforms.h>
 
 #include <display/nr-arena.h>
 #include <display/nr-arena-shape.h>
@@ -180,7 +182,7 @@ public:
      *  Informs the observer how much has been completed.
      *  Return false if the processing should be aborted.
      */
-    virtual bool progress(float percentCompleted)
+    virtual bool progress(float /*percentCompleted*/)
         {
         //Tracer *tracer = (Tracer *)context;
         //## Allow the GUI to update
@@ -194,7 +196,7 @@ public:
      *  Send an error string to the Observer.  Processing will
      *  be halted.
      */
-    virtual void error(const std::string &msg)
+    virtual void error(const std::string &/*msg*/)
         {
         //Tracer *tracer = (Tracer *)context;
         }
@@ -244,7 +246,7 @@ Tracer::sioxProcessImage(SPImage *img,
         return Glib::RefPtr<Gdk::Pixbuf>(NULL);
         }
 
-    NRArenaItem *aImg = sp_item_get_arenaitem(img, desktop->dkey);
+    NRArenaItem *aImg = img->get_arenaitem(desktop->dkey);
     //g_message("img: %d %d %d %d\n", aImg->bbox.x0, aImg->bbox.y0,
     //                                aImg->bbox.x1, aImg->bbox.y1);
 
@@ -262,7 +264,7 @@ Tracer::sioxProcessImage(SPImage *img,
     for (iter = sioxShapes.begin() ; iter!=sioxShapes.end() ; iter++)
         {
         SPItem *item = *iter;
-        NRArenaItem *aItem = sp_item_get_arenaitem(item, desktop->dkey);
+        NRArenaItem *aItem = item->get_arenaitem(desktop->dkey);
         arenaItems.push_back(aItem);
         }
 
@@ -280,8 +282,9 @@ Tracer::sioxProcessImage(SPImage *img,
             {
             //Get absolute X,Y position
             double xpos = ((double)aImg->bbox.x0) + iwscale * (double)col;
-            NR::Point point(xpos, ypos);
-            point *= aImg->transform;
+            Geom::Point point(xpos, ypos);
+            if (aImg->transform)
+                point *= *aImg->transform;
             //point *= imgMat;
             //point = desktop->doc2dt(point);
             //g_message("x:%f    y:%f\n", point[0], point[1]);
@@ -303,11 +306,12 @@ Tracer::sioxProcessImage(SPImage *img,
                 {
                 //g_message("hit!\n");
                 //dumpMap->setPixelLong(dumpMap, col, row, 0L);
-                simage.setConfidence(col, row, 
+                simage.setConfidence(col, row,
                         Siox::UNKNOWN_REGION_CONFIDENCE);
                 }
             else
                 {
+                //g_message("miss!\n");
                 //dumpMap->setPixelLong(dumpMap, col, row,
                 //        simage.getPixel(col, row));
                 simage.setConfidence(col, row,
@@ -439,7 +443,7 @@ void Tracer::traceThread()
         return;
         }
     SPDocument *doc = SP_ACTIVE_DOCUMENT;
-    sp_document_ensure_up_to_date(doc);
+    doc->ensure_up_to_date();
 
 
     SPImage *img = getSelectedSPImage();
@@ -462,19 +466,24 @@ void Tracer::traceThread()
         return;
         }
 
-    int nrPaths;
-    TracingEngineResult *results = engine->trace(pixbuf, &nrPaths);
-    //printf("nrPaths:%d\n", nrPaths);
+    msgStack->flash(Inkscape::NORMAL_MESSAGE, _("Trace: Starting trace..."));
+    desktop->updateCanvasNow();
+
+    std::vector<TracingEngineResult> results =
+                engine->trace(pixbuf);
+    //printf("nrPaths:%d\n", results.size());
+    int nrPaths = results.size();
 
     //### Check if we should stop
-    if (!keepGoing || !results || nrPaths<1)
+    if (!keepGoing || nrPaths<1)
         {
         engine = NULL;
         return;
         }
 
     //### Get pointers to the <image> and its parent
-    Inkscape::XML::Node *imgRepr   = SP_OBJECT(img)->repr;
+       //XML Tree being used directly here while it shouldn't be.
+    Inkscape::XML::Node *imgRepr   = SP_OBJECT(img)->getRepr();
     Inkscape::XML::Node *par       = sp_repr_parent(imgRepr);
 
     //### Get some information for the new transform()
@@ -494,43 +503,42 @@ void Tracer::traceThread()
     if (sp_repr_get_double(imgRepr, "height", &dval))
         height = dval;
 
-    NR::Matrix trans(NR::translate(x, y));
-
     double iwidth  = (double)pixbuf->get_width();
     double iheight = (double)pixbuf->get_height();
 
     double iwscale = width  / iwidth;
     double ihscale = height / iheight;
 
-    NR::Matrix scal(NR::scale(iwscale, ihscale));
+    Geom::Translate trans(x, y);
+    Geom::Scale scal(iwscale, ihscale);
 
     //# Convolve scale, translation, and the original transform
-    NR::Matrix tf(scal);
-    tf *= trans;
+    Geom::Matrix tf(scal * trans);
     tf *= img->transform;
 
 
     //#OK.  Now let's start making new nodes
 
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
     Inkscape::XML::Node *groupRepr = NULL;
 
     //# if more than 1, make a <g>roup of <path>s
     if (nrPaths > 1)
         {
-        groupRepr = sp_repr_new("svg:g");
+        groupRepr = xml_doc->createElement("svg:g");
         par->addChild(groupRepr, imgRepr);
         }
 
     long totalNodeCount = 0L;
 
-    for (TracingEngineResult *result=results ;
-                  result ; result=result->next)
+    for (unsigned int i=0 ; i<results.size() ; i++)
         {
-        totalNodeCount += result->getNodeCount();
+        TracingEngineResult result = results[i];
+        totalNodeCount += result.getNodeCount();
 
-        Inkscape::XML::Node *pathRepr = sp_repr_new("svg:path");
-        pathRepr->setAttribute("style", result->getStyle());
-        pathRepr->setAttribute("d",     result->getPathData());
+        Inkscape::XML::Node *pathRepr = xml_doc->createElement("svg:path");
+        pathRepr->setAttribute("style", result.getStyle().c_str());
+        pathRepr->setAttribute("d",     result.getPathData().c_str());
 
         if (nrPaths > 1)
             groupRepr->addChild(pathRepr, NULL);
@@ -542,7 +550,7 @@ void Tracer::traceThread()
         if (reprobj)
             {
             SPItem *newItem = SP_ITEM(reprobj);
-            sp_item_write_transform(newItem, pathRepr, tf, NULL);
+            newItem->doWriteTransform(pathRepr, tf, NULL);
             }
         if (nrPaths == 1)
             {
@@ -552,8 +560,6 @@ void Tracer::traceThread()
         Inkscape::GC::release(pathRepr);
         }
 
-    delete results;
-
     // If we have a group, then focus on, then forget it
     if (nrPaths > 1)
         {
@@ -563,8 +569,7 @@ void Tracer::traceThread()
         }
 
     //## inform the document, so we can undo
-    sp_document_done(doc, SP_VERB_NONE, 
-                    /* TODO: annotate */ "trace.cpp:567");
+    SPDocumentUndo::done(doc, SP_VERB_SELECTION_TRACE, _("Trace bitmap"));
 
     engine = NULL;