Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / box3d.cpp
index a517ea0a2cbb7c669eb6038c02909595764f5ca5..d51df9df3a1e18e3895e3d6a82c2517489317e7a 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_BOX3D_C__
-
 /*
  * SVG <box3d> implementation
  *
@@ -7,6 +5,7 @@
  *   Maximilian Albert <Anhalter42@gmx.de>
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007      Authors
  * Copyright (C) 1999-2002 Lauris Kaplinski
 #include "line-geometry.h"
 #include "persp3d-reference.h"
 #include "uri.h"
-#include "2geom/geom.h"
+#include <2geom/line.h>
 #include "sp-guide.h"
 #include "sp-namedview.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 
 #include "desktop.h"
 #include "desktop-handles.h"
@@ -50,7 +49,7 @@ static void box3d_update(SPObject *object, SPCtx *ctx, 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);
+static Geom::Matrix box3d_set_transform(SPItem *item, Geom::Matrix const &xform);
 static void box3d_convert_to_guides(SPItem *item);
 
 static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
@@ -127,16 +126,14 @@ box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
     // TODO: Create/link to the correct perspective
 
     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
-    if (!doc) {
-        g_print ("No document for the box!!!!\n");
+    if (!doc)
         return;
-    }
 
     box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
 
-    sp_object_read_attr(object, "inkscape:perspectiveID");
-    sp_object_read_attr(object, "inkscape:corner0");
-    sp_object_read_attr(object, "inkscape:corner7");
+    object->readAttr( "inkscape:perspectiveID" );
+    object->readAttr( "inkscape:corner0" );
+    object->readAttr( "inkscape:corner7" );
 }
 
 /**
@@ -150,13 +147,33 @@ box3d_release(SPObject *object)
     if (box->persp_href) {
         g_free(box->persp_href);
     }
+
+    // We have to store this here because the Persp3DReference gets destroyed below, but we need to
+    // access it to call persp3d_remove_box(), which cannot be called earlier because the reference
+    // needs to be destroyed first.
+    Persp3D *persp = box3d_get_perspective(box);
+
     if (box->persp_ref) {
         box->persp_ref->detach();
         delete box->persp_ref;
         box->persp_ref = NULL;
     }
 
-    //persp3d_remove_box (box3d_get_perspective(box), box);
+    if (persp) {
+        persp3d_remove_box (persp, box);
+        /*
+        // TODO: This deletes a perspective when the last box referring to it is gone. Eventually,
+        // it would be nice to have this but currently it crashes when undoing/redoing box deletion
+        // Reason: When redoing a box deletion, the associated perspective is deleted twice, first
+        // by the following code and then again by the redo mechanism! Perhaps we should perform
+        // deletion of the perspective from another location "outside" the undo/redo mechanism?
+        if (persp->perspective_impl->boxes.empty()) {
+            SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+            persp->deleteObject();
+            doc->setCurrentPersp3D(persp3d_document_first_persp(doc));
+        }
+        */
+    }
 
     if (((SPObjectClass *) parent_class)->release)
         ((SPObjectClass *) parent_class)->release(object);
@@ -209,7 +226,7 @@ box3d_set(SPObject *object, unsigned int key, const gchar *value)
                 box3d_position_set(box);
             }
             break;
-       default:
+        default:
             if (((SPObjectClass *) (parent_class))->set) {
                 ((SPObjectClass *) (parent_class))->set(object, key, value);
             }
@@ -226,16 +243,10 @@ box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box)
     if (old_ref) {
         sp_signal_disconnect_by_data(old_ref, box);
         persp3d_remove_box (SP_PERSP3D(old_ref), box);
-        /* Note: This sometimes leads to attempts to remove boxes twice from the list of selected/transformed
-           boxes in a perspectives, but this should be uncritical. */
-        persp3d_remove_box_transform (SP_PERSP3D(old_ref), box);
     }
     if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
     {
         persp3d_add_box (SP_PERSP3D(ref), box);
-        /* Note: This sometimes leads to attempts to add boxes twice to the list of selected/transformed
-           boxes in a perspectives, but this should be uncritical. */
-        persp3d_add_box_transform (SP_PERSP3D(ref), box);
     }
 }
 
@@ -256,7 +267,7 @@ box3d_update(SPObject *object, SPCtx *ctx, guint flags)
 }
 
 
-static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Document *xml_doc, 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);
 
@@ -278,11 +289,9 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Documen
                 repr->setAttribute("inkscape:perspectiveID", uri_string);
                 g_free(uri_string);
             } 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);
+                Glib::ustring href = "#";
+                href += doc->getCurrentPersp3D()->getId();
+                repr->setAttribute("inkscape:perspectiveID", href.c_str());
             }
         }
 
@@ -315,76 +324,48 @@ box3d_description(SPItem *item)
     return g_strdup(_("<b>3D Box</b>"));
 }
 
-void
-box3d_position_set (SPBox3D *box)
+void 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) ) {
-        if (SP_IS_BOX3D_SIDE(child))
+    for ( SPObject *child = box->firstChild(); child; child = child->getNext() ) {
+        if (SP_IS_BOX3D_SIDE(child)) {
             box3d_side_position_set(SP_BOX3D_SIDE(child));
+        }
     }
 }
 
-static NR::Matrix
-box3d_set_transform(SPItem *item, NR::Matrix const &xform)
+static Geom::Matrix
+box3d_set_transform(SPItem *item, Geom::Matrix const &xform)
 {
     SPBox3D *box = SP_BOX3D(item);
 
-    /* check whether we need to unlink any boxes from their perspectives */
-    Persp3D *persp = box3d_get_perspective(box);
-    Persp3D *transf_persp;
-
-    if (!persp3d_has_all_boxes_in_selection (persp)) {
-        std::list<SPBox3D *> selboxes = sp_desktop_selection(inkscape_active_desktop())->box3DList();
+    // We don't apply the transform to the box directly but instead to its perspective (which is
+    // done in sp_selection_apply_affine). Here we only adjust strokes, patterns, etc.
 
-        /* create a new perspective as a copy of the current one and link the selected boxes to it */
-        transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
-
-        for (std::list<SPBox3D *>::iterator b = selboxes.begin(); b != selboxes.end(); ++b) {
-            box3d_switch_perspectives(*b, persp, transf_persp);
-        }
-    } else {
-        transf_persp = persp;
-    }
-
-    /* only transform the perspective once, even if it has several selected boxes */
-    if(!persp3d_was_transformed (transf_persp)) {
-        /* concatenate the affine transformation with the perspective mapping; this
-           function also triggers repr updates of boxes and the perspective itself */
-        persp3d_apply_affine_transformation(transf_persp, xform);
-    }
-
-    box3d_mark_transformed(box);
-
-    if (persp3d_all_transformed(transf_persp)) {
-        /* all boxes were transformed; make perspective sensitive for further transformations */
-        persp3d_unset_transforms(transf_persp);
-    }
-
-    NR::Matrix ret(NR::transform(xform));
+    Geom::Matrix ret(Geom::Matrix(xform).without_translation());
     gdouble const sw = hypot(ret[0], ret[1]);
     gdouble const sh = hypot(ret[2], ret[3]);
 
-    for (SPObject *child = sp_object_first_child(box); child != NULL; child = SP_OBJECT_NEXT(child)) {
+    for ( SPObject *child = box->firstChild(); child; child = child->getNext() ) {
         if (SP_IS_ITEM(child)) {
             SPItem *childitem = SP_ITEM(child);
 
             // Adjust stroke width
-            sp_item_adjust_stroke(childitem, sqrt(fabs(sw * sh)));
+            childitem->adjust_stroke(sqrt(fabs(sw * sh)));
 
             // Adjust pattern fill
-            sp_item_adjust_pattern(childitem, xform);
+            childitem->adjust_pattern(xform);
 
             // Adjust gradient fill
-            sp_item_adjust_gradient(childitem, xform);
+            childitem->adjust_gradient(xform);
 
             // Adjust LPE
-            sp_item_adjust_livepatheffect(childitem, xform);
+            childitem->adjust_livepatheffect(xform);
         }
     }
 
-    return NR::identity();
+    return Geom::identity();
 }
 
 Proj::Pt3
@@ -403,17 +384,17 @@ box3d_get_proj_corner (SPBox3D const *box, guint id) {
                       1.0);
 }
 
-NR::Point
+Geom::Point
 box3d_get_corner_screen (SPBox3D const *box, guint id, bool item_coords) {
     Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
     if (!box3d_get_perspective(box)) {
-        return NR::Point (NR_HUGE, NR_HUGE);
+        return Geom::Point (NR_HUGE, NR_HUGE);
     }
-    NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(box)));
+    Geom::Matrix const i2d (SP_ITEM(box)->i2d_affine ());
     if (item_coords) {
-        return box3d_get_perspective(box)->tmat.image(proj_corner).affine() * i2d.inverse();
+        return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_corner).affine() * i2d.inverse();
     } else {
-        return box3d_get_perspective(box)->tmat.image(proj_corner).affine();
+        return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_corner).affine();
     }
 }
 
@@ -427,14 +408,14 @@ box3d_get_proj_center (SPBox3D *box) {
                       1.0);
 }
 
-NR::Point
+Geom::Point
 box3d_get_center_screen (SPBox3D *box) {
     Proj::Pt3 proj_center (box3d_get_proj_center (box));
     if (!box3d_get_perspective(box)) {
-        return NR::Point (NR_HUGE, NR_HUGE);
+        return Geom::Point (NR_HUGE, NR_HUGE);
     }
-    NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(box)));
-    return box3d_get_perspective(box)->tmat.image(proj_center).affine() * i2d.inverse();
+    Geom::Matrix const i2d (SP_ITEM(box)->i2d_affine ());
+    return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_center).affine() * i2d.inverse();
 }
 
 /*
@@ -460,13 +441,13 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
     Proj::Pt3 D_proj (x_coord,          y_coord + diff_y, z_coord, 1.0);
     Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
 
-    Persp3D *persp = box3d_get_perspective(box);
-    NR::Point A = persp->tmat.image(A_proj).affine();
-    NR::Point B = persp->tmat.image(B_proj).affine();
-    NR::Point C = persp->tmat.image(C_proj).affine();
-    NR::Point D = persp->tmat.image(D_proj).affine();
-    NR::Point E = persp->tmat.image(E_proj).affine();
-    NR::Point pt = persp->tmat.image(pt_proj).affine();
+    Persp3DImpl *persp_impl = box3d_get_perspective(box)->perspective_impl;
+    Geom::Point A = persp_impl->tmat.image(A_proj).affine();
+    Geom::Point B = persp_impl->tmat.image(B_proj).affine();
+    Geom::Point C = persp_impl->tmat.image(C_proj).affine();
+    Geom::Point D = persp_impl->tmat.image(D_proj).affine();
+    Geom::Point E = persp_impl->tmat.image(E_proj).affine();
+    Geom::Point pt = persp_impl->tmat.image(pt_proj).affine();
 
     // TODO: Replace these lines between corners with lines from a corner to a vanishing point
     //       (this might help to prevent rounding errors if the box is small)
@@ -476,7 +457,7 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
     Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
 
     int num_snap_lines = (id != -1) ? 3 : 4;
-    NR::Point snap_pts[num_snap_lines];
+    Geom::Point snap_pts[num_snap_lines];
 
     snap_pts[0] = pl1.closest_to (pt);
     snap_pts[1] = pl2.closest_to (pt);
@@ -490,7 +471,7 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
     // determine the distances to all potential snapping points
     double snap_dists[num_snap_lines];
     for (int i = 0; i < num_snap_lines; ++i) {
-        snap_dists[i] = NR::L2 (snap_pts[i] - pt) * zoom;
+        snap_dists[i] = Geom::L2 (snap_pts[i] - pt) * zoom;
     }
 
     // while we are within a given tolerance of the starting point,
@@ -515,18 +496,28 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
 
     // snap to the closest point (or the previously remembered one
     // if we are within tolerance of the starting point)
-    NR::Point result;
+    Geom::Point result;
     if (within_tolerance) {
         result = snap_pts[remember_snap_index];
     } else {
         remember_snap_index = snap_index;
         result = snap_pts[snap_index];
     }
-    return box3d_get_perspective(box)->tmat.preimage (result, z_coord, Proj::Z);
+    return box3d_get_perspective(box)->perspective_impl->tmat.preimage (result, z_coord, Proj::Z);
+}
+
+SPBox3D * SPBox3D::createBox3D(SPItem * parent)
+{
+    SPBox3D *box3d = 0;
+    Inkscape::XML::Document *xml_doc = parent->document->rdoc;
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
+    repr->setAttribute("sodipodi:type", "inkscape:box3d");
+    box3d = reinterpret_cast<SPBox3D *>(parent->appendChildRepr(repr));
+    return box3d;
 }
 
 void
-box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
+box3d_set_corner (SPBox3D *box, const guint id, Geom::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
 
     box->orig_corner0.normalize();
@@ -534,8 +525,9 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
 
     /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
     if (!(movement & Box3D::Z)) {
-        Proj::Pt3 pt_proj (box3d_get_perspective(box)->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
-                                                                      box->orig_corner7[Proj::Z], Proj::Z));
+        Persp3DImpl *persp_impl = box3d_get_perspective(box)->perspective_impl;
+        Proj::Pt3 pt_proj (persp_impl->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
+                                                      box->orig_corner7[Proj::Z], Proj::Z));
         if (constrained) {
             pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
         }
@@ -552,13 +544,14 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
                                        1.0);
     } else {
         Persp3D *persp = box3d_get_perspective(box);
-        Box3D::PerspectiveLine pl(persp->tmat.image(
+        Persp3DImpl *persp_impl = box3d_get_perspective(box)->perspective_impl;
+        Box3D::PerspectiveLine pl(persp_impl->tmat.image(
                                       box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
                                   Proj::Z, persp);
-        NR::Point new_pos_snapped(pl.closest_to(new_pos));
-        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped,
-                                          box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
-                                          (movement & Box3D::Y) ? Proj::X : Proj::Y));
+        Geom::Point new_pos_snapped(pl.closest_to(new_pos));
+        Proj::Pt3 pt_proj (persp_impl->tmat.preimage (new_pos_snapped,
+                                      box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
+                                                      (movement & Box3D::Y) ? Proj::X : Proj::Y));
         bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
         bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
         bool corner7_move_x =  (id & Box3D::X) && (movement & Box3D::X);
@@ -577,7 +570,7 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
     // FIXME: Should we update the box here? If so, how?
 }
 
-void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
+void box3d_set_center (SPBox3D *box, Geom::Point const &new_pos, Geom::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
 
     box->orig_corner0.normalize();
@@ -589,9 +582,9 @@ void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &
         double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
         double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
 
-        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos, coord, Proj::Z));
+        Proj::Pt3 pt_proj (persp->perspective_impl->tmat.preimage (new_pos, coord, Proj::Z));
         if (constrained) {
-            Proj::Pt3 old_pos_proj (persp->tmat.preimage (old_pos, coord, Proj::Z));
+            Proj::Pt3 old_pos_proj (persp->perspective_impl->tmat.preimage (old_pos, coord, Proj::Z));
             old_pos_proj.normalize();
             pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
         }
@@ -610,8 +603,8 @@ void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &
         double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
 
         Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
-        NR::Point new_pos_snapped(pl.closest_to(new_pos));
-        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped, coord, Proj::X));
+        Geom::Point new_pos_snapped(pl.closest_to(new_pos));
+        Proj::Pt3 pt_proj (persp->perspective_impl->tmat.preimage (new_pos_snapped, coord, Proj::X));
 
         /* normalizing pt_proj is essential because we want to mingle affine coordinates */
         pt_proj.normalize();
@@ -631,10 +624,11 @@ void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &
  * from which the perspective lines in the direction of 'axis' emerge
  */
 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
-                            NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
+                            Geom::Point &corner1, Geom::Point &corner2, Geom::Point &corner3, Geom::Point &corner4)
 {
     Persp3D *persp = box3d_get_perspective(box);
     g_return_if_fail (persp);
+    Persp3DImpl *persp_impl = persp->perspective_impl;
     //box->orig_corner0.normalize();
     //box->orig_corner7.normalize();
     double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
@@ -665,27 +659,42 @@ void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
         default:
             return;
     }
-    corner1 = persp->tmat.image(c1).affine();
-    corner2 = persp->tmat.image(c2).affine();
-    corner3 = persp->tmat.image(c3).affine();
-    corner4 = persp->tmat.image(c4).affine();
+    corner1 = persp_impl->tmat.image(c1).affine();
+    corner2 = persp_impl->tmat.image(c2).affine();
+    corner3 = persp_impl->tmat.image(c3).affine();
+    corner4 = persp_impl->tmat.image(c4).affine();
 }
 
 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
 static bool
 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
                                       Geom::Point const &C, Geom::Point const &D) {
-    Geom::Point E; // the point of intersection
     Geom::Point n0 = (B - A).ccw();
     double d0 = dot(n0,A);
 
     Geom::Point n1 = (D - C).ccw();
     double d1 = dot(n1,C);
-    Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
-    if (intersects == Geom::coincident || intersects == Geom::parallel) {
+
+    Geom::Line lineAB(A,B);
+    Geom::Line lineCD(C,D);
+
+    Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
+    try
+    {
+        inters = Geom::intersection(lineAB, lineCD);
+    }
+    catch (Geom::InfiniteSolutions e)
+    {
+        // We're probably dealing with parallel lines, so they don't really cross
+        return false;
+    }
+
+    if (!inters) {
         return false;
     }
 
+    Geom::Point E = lineAB.pointAt((*inters).ta); // the point of intersection
+
     if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
         // C and D lie on the same side of the line AB
         return false;
@@ -708,12 +717,12 @@ box3d_XY_axes_are_swapped (SPBox3D *box) {
     g_return_val_if_fail(persp, false);
     Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3, false), Proj::X, persp);
     Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3, false), Proj::Y, persp);
-    NR::Point v1(l1.direction());
-    NR::Point v2(l2.direction());
+    Geom::Point v1(l1.direction());
+    Geom::Point v2(l2.direction());
     v1.normalize();
     v2.normalize();
 
-    return (v1[NR::X]*v2[NR::Y] - v1[NR::Y]*v2[NR::X] > 0);
+    return (v1[Geom::X]*v2[Geom::Y] - v1[Geom::Y]*v2[Geom::X] > 0);
 }
 
 static inline void
@@ -750,9 +759,9 @@ box3d_swap_z_orders (int z_orders[6]) {
 static void
 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
     Persp3D *persp = box3d_get_perspective(box);
-    NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
-    NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
-    NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
+    Geom::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
+    Geom::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
+    Geom::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
 
     bool swapped = box3d_XY_axes_are_swapped(box);
 
@@ -795,7 +804,7 @@ box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central
 static void
 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
     Persp3D *persp = box3d_get_perspective(box);
-    NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
+    Geom::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
 
     // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
     Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
@@ -867,10 +876,10 @@ static void
 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
     Persp3D *persp = box3d_get_perspective(box);
 
-    NR::Point c3(box3d_get_corner_screen(box, 3, false));
-    NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
-    NR::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
-    NR::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
+    Geom::Point c3(box3d_get_corner_screen(box, 3, false));
+    Geom::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
+    Geom::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
+    Geom::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
 
     bool swapped = box3d_XY_axes_are_swapped(box);
 
@@ -1002,16 +1011,16 @@ box3d_recompute_z_orders (SPBox3D *box) {
 
     int z_orders[6];
 
-    NR::Point c3(box3d_get_corner_screen(box, 3, false));
+    Geom::Point c3(box3d_get_corner_screen(box, 3, false));
 
     // determine directions from corner3 to the VPs
     int num_finite = 0;
     Box3D::Axis axis_finite = Box3D::NONE;
     Box3D::Axis axis_infinite = Box3D::NONE;
-    NR::Point dirs[3];
+    Geom::Point dirs[3];
     for (int i = 0; i < 3; ++i) {
         dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
-        if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
+        if (persp3d_VP_is_finite(persp->perspective_impl, Proj::axes[i])) {
             num_finite++;
             axis_finite = Box3D::axes[i];
         } else {
@@ -1048,17 +1057,17 @@ box3d_recompute_z_orders (SPBox3D *box) {
          * joining the other two VPs. If this is the case, it determines the "central" corner from
          * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
          */
-        // FIXME: We should eliminate the use of NR::Point altogether
+        // FIXME: We should eliminate the use of Geom::Point altogether
         Box3D::Axis central_axis = Box3D::NONE;
-        NR::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
-        NR::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
-        NR::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
-        Geom::Point vpx(vp_x[NR::X], vp_x[NR::Y]);
-        Geom::Point vpy(vp_y[NR::X], vp_y[NR::Y]);
-        Geom::Point vpz(vp_z[NR::X], vp_z[NR::Y]);
+        Geom::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
+        Geom::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
+        Geom::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
+        Geom::Point vpx(vp_x[Geom::X], vp_x[Geom::Y]);
+        Geom::Point vpy(vp_y[Geom::X], vp_y[Geom::Y]);
+        Geom::Point vpz(vp_z[Geom::X], vp_z[Geom::Y]);
 
-        NR::Point c3 = box3d_get_corner_screen(box, 3, false);
-        Geom::Point corner3(c3[NR::X], c3[NR::Y]);
+        Geom::Point c3 = box3d_get_corner_screen(box, 3, false);
+        Geom::Point corner3(c3[Geom::X], c3[Geom::Y]);
 
         if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
             central_axis = Box3D::X;
@@ -1076,13 +1085,13 @@ box3d_recompute_z_orders (SPBox3D *box) {
             central_corner = central_corner ^ Box3D::XYZ;
         }
 
-        NR::Point c1(box3d_get_corner_screen(box, 1, false));
-        NR::Point c2(box3d_get_corner_screen(box, 2, false));
-        NR::Point c7(box3d_get_corner_screen(box, 7, false));
+        Geom::Point c1(box3d_get_corner_screen(box, 1, false));
+        Geom::Point c2(box3d_get_corner_screen(box, 2, false));
+        Geom::Point c7(box3d_get_corner_screen(box, 7, false));
 
-        Geom::Point corner1(c1[NR::X], c1[NR::Y]);
-        Geom::Point corner2(c2[NR::X], c2[NR::Y]);
-        Geom::Point corner7(c7[NR::X], c7[NR::Y]);
+        Geom::Point corner1(c1[Geom::X], c1[Geom::Y]);
+        Geom::Point corner2(c2[Geom::X], c2[Geom::Y]);
+        Geom::Point corner7(c7[Geom::X], c7[Geom::Y]);
         // FIXME: At present we don't use the information about central_corner computed above.
         switch (central_axis) {
             case Box3D::Y:
@@ -1146,13 +1155,14 @@ box3d_recompute_z_orders (SPBox3D *box) {
     return false;
 }
 
-static std::map<int, Box3DSide *>
-box3d_get_sides (SPBox3D *box) {
+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)) {
-        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);
+    for ( SPObject *side = box->firstChild(); side; side = side->getNext() ) {
+        if (SP_IS_BOX3D_SIDE(side)){
+            Box3DSide *bside = SP_BOX3D_SIDE(side);
+            sides[Box3D::face_to_int(bside->getFaceId())] = bside;
+        }
     }
     sides.erase(-1);
     return sides;
@@ -1188,19 +1198,19 @@ box3d_set_z_orders (SPBox3D *box) {
 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
 //       can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
 int
-box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
+box3d_pt_lies_in_PL_sector (SPBox3D const *box, Geom::Point const &pt, int id1, int id2, Box3D::Axis axis) {
     Persp3D *persp = box3d_get_perspective(box);
 
     // the two corners
-    NR::Point c1(box3d_get_corner_screen(box, id1, false));
-    NR::Point c2(box3d_get_corner_screen(box, id2, false));
+    Geom::Point c1(box3d_get_corner_screen(box, id1, false));
+    Geom::Point c2(box3d_get_corner_screen(box, id2, false));
 
     int ret = 0;
-    if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
-        NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
-        NR::Point v1(c1 - vp);
-        NR::Point v2(c2 - vp);
-        NR::Point w(pt - vp);
+    if (persp3d_VP_is_finite(persp->perspective_impl, Box3D::toProj(axis))) {
+        Geom::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
+        Geom::Point v1(c1 - vp);
+        Geom::Point v2(c2 - vp);
+        Geom::Point w(pt - vp);
         ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
     } else {
         Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
@@ -1208,7 +1218,7 @@ box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, in
         if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
             // test whether pt lies "towards" or "away from" the VP
             Box3D::Line edge(c1,c2);
-            NR::Point c3(box3d_get_corner_screen(box, id1 ^ axis, false));
+            Geom::Point c3(box3d_get_corner_screen(box, id1 ^ axis, false));
             if (edge.lie_on_same_side(pt, c3)) {
                 ret = 1;
             } else {
@@ -1223,7 +1233,7 @@ int
 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
     Persp3D *persp = box3d_get_perspective(box);
 
-    if (!persp3d_VP_is_finite(persp, vpdir)) {
+    if (!persp3d_VP_is_finite(persp->perspective_impl, vpdir)) {
         return 0;
     } else {
         return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
@@ -1286,37 +1296,11 @@ box3d_check_for_swapped_coords(SPBox3D *box) {
     box3d_exchange_coords(box);
 }
 
-void
-box3d_add_to_selection(SPBox3D *box) {
-    Persp3D *persp = box3d_get_perspective(box);
-    g_return_if_fail(persp);
-    persp3d_add_box_transform(persp, box);
-}
-
-void
-box3d_remove_from_selection(SPBox3D *box) {
-    Persp3D *persp = box3d_get_perspective(box);
-    if (!persp) {
-        /* this can happen if a box is deleted through undo and the persp_ref is already detached;
-           should we rebuild the boxes of each perspective in this case or is it safe to leave it alone? */
-        return;
-    }
-    persp3d_remove_box_transform(persp, box);
-}
-
-void
-box3d_mark_transformed(SPBox3D *box) {
-    Persp3D *persp = box3d_get_perspective(box);
-    g_return_if_fail(persp);
-    persp3d_set_box_transformed(persp, box, true);
-}
-
-static void
-box3d_extract_boxes_rec(SPObject *obj, std::list<SPBox3D *> &boxes) {
+static void box3d_extract_boxes_rec(SPObject *obj, std::list<SPBox3D *> &boxes) {
     if (SP_IS_BOX3D(obj)) {
         boxes.push_back(SP_BOX3D(obj));
     } else if (SP_IS_GROUP(obj)) {
-        for (SPObject *child = sp_object_first_child(obj); child != NULL; child = SP_OBJECT_NEXT(child) ) {
+        for ( SPObject *child = obj->firstChild(); child; child = child->getNext() ) {
             box3d_extract_boxes_rec(child, boxes);
         }
     }
@@ -1341,48 +1325,43 @@ box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp,
         box->orig_corner7.normalize();
         double z0 = box->orig_corner0[Proj::Z];
         double z7 = box->orig_corner7[Proj::Z];
-        NR::Point corner0_screen = box3d_get_corner_screen(box, 0, false);
-        NR::Point corner7_screen = box3d_get_corner_screen(box, 7, false);
+        Geom::Point corner0_screen = box3d_get_corner_screen(box, 0, false);
+        Geom::Point corner7_screen = box3d_get_corner_screen(box, 7, false);
 
-        box->orig_corner0 = new_persp->tmat.preimage(corner0_screen, z0, Proj::Z);
-        box->orig_corner7 = new_persp->tmat.preimage(corner7_screen, z7, Proj::Z);
+        box->orig_corner0 = new_persp->perspective_impl->tmat.preimage(corner0_screen, z0, Proj::Z);
+        box->orig_corner7 = new_persp->perspective_impl->tmat.preimage(corner7_screen, z7, Proj::Z);
     }
 
     persp3d_remove_box (old_persp, box);
     persp3d_add_box (new_persp, box);
 
-    persp3d_remove_box_transform (old_persp, box);
-    persp3d_add_box_transform (new_persp, box);
-
-    gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
-    SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
-    g_free(href);
+    Glib::ustring href = "#";
+    href += new_persp->getId();
+    box->setAttribute("inkscape:perspectiveID", href.c_str());
 }
 
 /* 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 */
-SPGroup *
-box3d_convert_to_group(SPBox3D *box) {
+SPGroup *box3d_convert_to_group(SPBox3D *box)
+{
     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
     // remember position of the box
-    int pos = SP_OBJECT_REPR(box)->position();
+    int pos = box->getPosition();
 
     // remember important attributes
-    Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(box);
-    gchar const *id = repr_source->attribute("id");
-    gchar const *style = repr_source->attribute("style");
-    gchar const *mask = repr_source->attribute("mask");
-    gchar const *clip_path = repr_source->attribute("clip-path");
+    gchar const *id = box->getAttribute("id");
+    gchar const *style = box->getAttribute("style");
+    gchar const *mask = box->getAttribute("mask");
+    gchar const *clip_path = box->getAttribute("clip-path");
 
     // create a new group and add the sides (converted to ordinary paths) as its children
     Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
 
-    Inkscape::XML::Node *repr;
-    for (SPObject *child = sp_object_first_child(SP_OBJECT(box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
+    for ( SPObject *child = box->firstChild(); child; child = child->getNext() ) {
         if (SP_IS_BOX3D_SIDE(child)) {
-            repr = box3d_side_convert_to_path(SP_BOX3D_SIDE(child));
+            Inkscape::XML::Node *repr = box3d_side_convert_to_path(SP_BOX3D_SIDE(child));
             grepr->appendChild(repr);
         } else {
             g_warning("Non-side item encountered as child of a 3D box.");
@@ -1391,7 +1370,7 @@ box3d_convert_to_group(SPBox3D *box) {
 
     // add the new group to the box's parent and set remembered position
     SPObject *parent = SP_OBJECT_PARENT(box);
-    SP_OBJECT_REPR(parent)->appendChild(grepr);
+    parent->appendChild(grepr);
     grepr->setPosition(pos);
     grepr->setAttribute("style", style);
     if (mask)
@@ -1415,9 +1394,10 @@ box3d_push_back_corner_pair(SPBox3D *box, std::list<std::pair<Geom::Point, Geom:
 void
 box3d_convert_to_guides(SPItem *item) {
     SPBox3D *box = SP_BOX3D(item);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
-    if (prefs_get_int_attribute("tools.shapes.3dbox", "convertguides", 1) == 0) {
-        sp_item_convert_to_guides(SP_ITEM(box));
+    if (!prefs->getBool("/tools/shapes/3dbox/convertguides", true)) {
+        box->convert_to_guides();
         return;
     }