Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / box3d.cpp
index f5745210171ed19dc8d9c940177817d4b7f51559..a517ea0a2cbb7c669eb6038c02909595764f5ca5 100644 (file)
@@ -47,7 +47,7 @@ static void box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::N
 static void box3d_release(SPObject *object);
 static void box3d_set(SPObject *object, unsigned int key, const gchar *value);
 static void box3d_update(SPObject *object, SPCtx *ctx, guint flags);
-static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static gchar *box3d_description(SPItem *item);
 static NR::Matrix box3d_set_transform(SPItem *item, NR::Matrix const &xform);
@@ -256,14 +256,13 @@ box3d_update(SPObject *object, SPCtx *ctx, guint flags)
 }
 
 
-static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPBox3D *box = SP_BOX3D(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
         // this is where we end up when saving as plain SVG (also in other circumstances?)
         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:g
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:g");
     }
 
@@ -273,19 +272,17 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *r
             repr->setAttribute("inkscape:perspectiveID", box->persp_href);
         } else {
             /* box is not yet linked to a perspective; use the document's current perspective */
-            SPDocument *doc = inkscape_active_document();
+            SPDocument *doc = SP_OBJECT_DOCUMENT(object);
             if (box->persp_ref->getURI()) {
                 gchar *uri_string = box->persp_ref->getURI()->toString();
                 repr->setAttribute("inkscape:perspectiveID", uri_string);
                 g_free(uri_string);
-            } else if (doc) {
+            } else {
                 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
                 const gchar *persp_id = persp_repr->attribute("id");
                 gchar *href = g_strdup_printf("#%s", persp_id);
                 repr->setAttribute("inkscape:perspectiveID", href);
                 g_free(href);
-            } else {
-                g_print ("No active document while creating perspective!!!\n");
             }
         }
 
@@ -304,7 +301,7 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *r
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     return repr;
@@ -324,7 +321,8 @@ box3d_position_set (SPBox3D *box)
     /* This draws the curve and calls requestDisplayUpdate() for each side (the latter is done in
        box3d_side_position_set() to avoid update conflicts with the parent box) */
     for (SPObject *child = sp_object_first_child(SP_OBJECT (box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
-        box3d_side_position_set (SP_BOX3D_SIDE (child));
+        if (SP_IS_BOX3D_SIDE(child))
+            box3d_side_position_set(SP_BOX3D_SIDE(child));
     }
 }
 
@@ -368,21 +366,22 @@ box3d_set_transform(SPItem *item, NR::Matrix const &xform)
     gdouble const sw = hypot(ret[0], ret[1]);
     gdouble const sh = hypot(ret[2], ret[3]);
 
-    SPItem *sideitem = NULL;
-    for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
-        sideitem = SP_ITEM(side);
+    for (SPObject *child = sp_object_first_child(box); child != NULL; child = SP_OBJECT_NEXT(child)) {
+        if (SP_IS_ITEM(child)) {
+            SPItem *childitem = SP_ITEM(child);
 
-        // Adjust stroke width
-        sp_item_adjust_stroke(sideitem, sqrt(fabs(sw * sh)));
+            // Adjust stroke width
+            sp_item_adjust_stroke(childitem, sqrt(fabs(sw * sh)));
 
-        // Adjust pattern fill
-        sp_item_adjust_pattern(sideitem, xform);
+            // Adjust pattern fill
+            sp_item_adjust_pattern(childitem, xform);
 
-        // Adjust gradient fill
-        sp_item_adjust_gradient(sideitem, xform);
+            // Adjust gradient fill
+            sp_item_adjust_gradient(childitem, xform);
 
-        // Adjust LPE
-        sp_item_adjust_livepatheffect(item, xform);
+            // Adjust LPE
+            sp_item_adjust_livepatheffect(childitem, xform);
+        }
     }
 
     return NR::identity();
@@ -1151,8 +1150,9 @@ static std::map<int, Box3DSide *>
 box3d_get_sides (SPBox3D *box) {
     std::map<int, Box3DSide *> sides;
     for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
-        sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
-                                                           "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
+        if (SP_IS_BOX3D_SIDE(side))
+            sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
+                                                               "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
     }
     sides.erase(-1);
     return sides;
@@ -1361,7 +1361,7 @@ box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp,
 
 /* Converts the 3D box to an ordinary SPGroup, adds it to the XML tree at the same position as
    the original box and deletes the latter */
-Inkscape::XML::Node *
+SPGroup *
 box3d_convert_to_group(SPBox3D *box) {
     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
@@ -1403,13 +1403,13 @@ box3d_convert_to_group(SPBox3D *box) {
 
     grepr->setAttribute("id", id);
 
-    return grepr;
+    return SP_GROUP(doc->getObjectByRepr(grepr));
 }
 
 static inline void
 box3d_push_back_corner_pair(SPBox3D *box, std::list<std::pair<Geom::Point, Geom::Point> > &pts, int c1, int c2) {
-    pts.push_back(std::make_pair(box3d_get_corner_screen(box, c1, false).to_2geom(),
-                                 box3d_get_corner_screen(box, c2, false).to_2geom()));
+    pts.push_back(std::make_pair(box3d_get_corner_screen(box, c1, false),
+                                 box3d_get_corner_screen(box, c2, false)));
 }
 
 void
@@ -1421,8 +1421,6 @@ box3d_convert_to_guides(SPItem *item) {
         return;
     }
 
-    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
-
     std::list<std::pair<Geom::Point, Geom::Point> > pts;
 
     /* perspective lines in X direction */
@@ -1443,7 +1441,7 @@ box3d_convert_to_guides(SPItem *item) {
     box3d_push_back_corner_pair(box, pts, 2, 6);
     box3d_push_back_corner_pair(box, pts, 3, 7);
 
-    sp_guide_pt_pairs_to_guides(doc, pts);
+    sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
 }
 
 /*