Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / box3d.cpp
index 8df5c3b52bd9b975a3cdb048185434aa9a8a4301..d51df9df3a1e18e3895e3d6a82c2517489317e7a 100644 (file)
@@ -1,12 +1,11 @@
-#define __SP_3DBOX_C__
-
 /*
  * SVG <box3d> implementation
  *
  * Authors:
+ *   Maximilian Albert <Anhalter42@gmx.de>
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
- *   Maximilian Albert <Anhalter42@gmx.de>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007      Authors
  * Copyright (C) 1999-2002 Lauris Kaplinski
 
 #include <glibmm/i18n.h>
 #include "attributes.h"
-#include "svg/stringstream.h"
+#include "xml/document.h"
+#include "xml/repr.h"
+
 #include "box3d.h"
+#include "box3d-side.h"
+#include "box3d-context.h"
+#include "proj_pt.h"
+#include "transf_mat_3x4.h"
+#include "perspective-line.h"
+#include "inkscape.h"
+#include "persp3d.h"
+#include "line-geometry.h"
+#include "persp3d-reference.h"
+#include "uri.h"
+#include <2geom/line.h>
+#include "sp-guide.h"
+#include "sp-namedview.h"
+#include "preferences.h"
+
+#include "desktop.h"
 #include "desktop-handles.h"
+#include "macros.h"
 
-static void sp_3dbox_class_init(SP3DBoxClass *klass);
-static void sp_3dbox_init(SP3DBox *box3d);
+static void box3d_class_init(SPBox3DClass *klass);
+static void box3d_init(SPBox3D *box3d);
 
-static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static void sp_3dbox_release (SPObject *object);
-static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
-static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
-static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static void box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
+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::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
-static gchar *sp_3dbox_description(SPItem *item);
+static gchar *box3d_description(SPItem *item);
+static Geom::Matrix box3d_set_transform(SPItem *item, Geom::Matrix const &xform);
+static void box3d_convert_to_guides(SPItem *item);
 
-//static void sp_3dbox_set_shape(SPShape *shape);
-//static void sp_3dbox_set_shape(SP3DBox *box3d);
-
-static void sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value);
-static void sp_3dbox_update_perspective (Box3D::Perspective3D *persp, const gchar *value);
-static gchar * sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id);
-static std::pair<gdouble, gdouble> sp_3dbox_get_coord_pair_from_string (const gchar *);
-static gchar * sp_3dbox_get_perspective_string (SP3DBox *box);
+static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
 
 static SPGroupClass *parent_class;
 
 static gint counter = 0;
 
 GType
-sp_3dbox_get_type(void)
+box3d_get_type(void)
 {
     static GType type = 0;
 
     if (!type) {
         GTypeInfo info = {
-            sizeof(SP3DBoxClass),
+            sizeof(SPBox3DClass),
             NULL,   /* base_init */
             NULL,   /* base_finalize */
-            (GClassInitFunc) sp_3dbox_class_init,
+            (GClassInitFunc) box3d_class_init,
             NULL,   /* class_finalize */
             NULL,   /* class_data */
-            sizeof(SP3DBox),
+            sizeof(SPBox3D),
             16,     /* n_preallocs */
-            (GInstanceInitFunc) sp_3dbox_init,
+            (GInstanceInitFunc) box3d_init,
             NULL,   /* value_table */
         };
-        type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
+        type = g_type_register_static(SP_TYPE_GROUP, "SPBox3D", &info, (GTypeFlags) 0);
     }
 
     return type;
 }
 
 static void
-sp_3dbox_class_init(SP3DBoxClass *klass)
+box3d_class_init(SPBox3DClass *klass)
 {
     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
     SPItemClass *item_class = (SPItemClass *) klass;
 
     parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
 
-    sp_object_class->build = sp_3dbox_build;
-    sp_object_class->set = sp_3dbox_set;
-    sp_object_class->write = sp_3dbox_write;
-    sp_object_class->update = sp_3dbox_update;
-    sp_object_class->release = sp_3dbox_release;
+    sp_object_class->build = box3d_build;
+    sp_object_class->release = box3d_release;
+    sp_object_class->set = box3d_set;
+    sp_object_class->write = box3d_write;
+    sp_object_class->update = box3d_update;
 
-    item_class->description = sp_3dbox_description;
+    item_class->description = box3d_description;
+    item_class->set_transform = box3d_set_transform;
+    item_class->convert_to_guides = box3d_convert_to_guides;
 }
 
 static void
-sp_3dbox_init(SP3DBox *box)
+box3d_init(SPBox3D *box)
 {
-    for (int i = 0; i < 8; ++i) box->corners[i] = NR::Point(0,0);
-    for (int i = 0; i < 6; ++i) box->faces[i] = NULL;
+    box->persp_href = NULL;
+    box->persp_ref = new Persp3DReference(SP_OBJECT(box));
 }
 
 static void
-sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
+box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
 {
     if (((SPObjectClass *) (parent_class))->build) {
         ((SPObjectClass *) (parent_class))->build(object, document, repr);
     }
 
-    SP3DBox *box = SP_3DBOX (object);
-
+    SPBox3D *box = SP_BOX3D (object);
     box->my_counter = counter++;
 
     /* we initialize the z-orders to zero so that they are updated during dragging */
@@ -109,119 +123,110 @@ sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr
         box->z_orders[i] = 0;
     }
 
-    box->front_bits = 0x0;
+    // TODO: Create/link to the correct perspective
 
-    
-    if (repr->attribute ("inkscape:perspective") == NULL) {
-        // we are creating a new box; link it to the current perspective
-        document->current_perspective->add_box (box);
-    } else {
-        // create a new perspective that we can compare with existing ones
-        Box3D::Perspective3D *persp = new Box3D::Perspective3D (Box3D::VanishingPoint (0,0),
-                                                                Box3D::VanishingPoint (0,0),
-                                                                Box3D::VanishingPoint (0,0),
-                                                                document);
-        sp_3dbox_update_perspective (persp, repr->attribute ("inkscape:perspective"));
-        Box3D::Perspective3D *comp =  document->find_perspective (persp);
-        if (comp == NULL) {
-            // perspective doesn't exist yet
-            document->add_perspective (persp);
-            persp->add_box (box);
-        } else {
-            // link the box to the existing perspective and delete the temporary one
-            comp->add_box (box);
-            delete persp;
-            //g_assert (Box3D::get_persp_of_box (box) == comp);
-
-            // FIXME: If the paths of the box's faces do not correspond to the svg representation of the perspective
-            //        the box is shown with a "wrong" initial shape that is only corrected after dragging.
-            //        Should we "repair" this by updating the paths at the end of sp_3dbox_build()?
-            //        Maybe it would be better to simply destroy and rebuild them in sp_3dbox_link_to_existing_paths().
-        }
-    }
+    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+    if (!doc)
+        return;
 
-    sp_object_read_attr(object, "inkscape:box3dcornerA");
-    sp_object_read_attr(object, "inkscape:box3dcornerB");
-    sp_object_read_attr(object, "inkscape:box3dcornerC");
+    box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
 
-    // TODO: We create all faces in the beginning, but only the non-degenerate ones
-    //       should be written to the svg representation later in sp_3dbox_write.
-    Box3D::Axis cur_plane, axis, dir1, dir2;
-    Box3D::FrontOrRear cur_pos;
-    for (int i = 0; i < 3; ++i) {
-        for (int j = 0; j < 2; ++j) {
-            cur_plane = Box3D::planes[i];
-            cur_pos = Box3D::face_positions[j];
-            // FIXME: The following code could theoretically be moved to
-            //        the constructor of Box3DFace (but see the comment there).
-            axis = (cur_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (cur_plane));
-            dir1 = extract_first_axis_direction (cur_plane);
-            dir2 = extract_second_axis_direction (cur_plane);
-            
-            box->faces[Box3D::face_to_int(cur_plane ^ cur_pos)] =
-                new Box3DFace (box, box->corners[axis], box->corners[axis ^ dir1],
-                                    box->corners[axis ^ dir1 ^ dir2], box->corners[axis ^ dir2],
-                                    cur_plane, cur_pos);
-        }
-    }
+    object->readAttr( "inkscape:perspectiveID" );
+    object->readAttr( "inkscape:corner0" );
+    object->readAttr( "inkscape:corner7" );
+}
 
-    // Check whether the paths of the faces of the box need to be linked to existing paths in the
-    // document (e.g., after a 'redo' operation or after opening a file) and do so if necessary.
-    sp_3dbox_link_to_existing_paths (box, repr);
+/**
+ * Virtual release of SPBox3D members before destruction.
+ */
+static void
+box3d_release(SPObject *object)
+{
+    SPBox3D *box = (SPBox3D *) object;
 
-    sp_3dbox_set_ratios (box, Box3D::XYZ);
+    if (box->persp_href) {
+        g_free(box->persp_href);
+    }
 
-    // Store the center (if it already exists) and certain corners for later use during center-dragging
-    NR::Maybe<NR::Point> cen = sp_3dbox_get_center (box);
-    if (cen) {
-        box->old_center = *cen;
+    // 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;
     }
-    box->old_corner2 = box->corners[2];
-    box->old_corner1 = box->corners[1];
-    box->old_corner0 = box->corners[0];
-    box->old_corner3 = box->corners[3];
-    box->old_corner5 = box->corners[5];
-    box->old_corner7 = box->corners[7];
-}
 
-static void
-sp_3dbox_release (SPObject *object)
-{
-       SP3DBox *box = SP_3DBOX(object);
-        for (int i = 0; i < 6; ++i) {
-            if (box->faces[i]) {
-                delete box->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
-            }
+    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));
         }
+        */
+    }
 
-        // FIXME: We do not duplicate perspectives if they are the same for several boxes.
-        //        Thus, don't delete the perspective when deleting a box but rather unlink the box from it.
-        SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box)->remove_box (box);
-
-       if (((SPObjectClass *) parent_class)->release) {
-         ((SPObjectClass *) parent_class)->release (object);
-       }
+    if (((SPObjectClass *) parent_class)->release)
+        ((SPObjectClass *) parent_class)->release(object);
 }
 
-static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
+static void
+box3d_set(SPObject *object, unsigned int key, const gchar *value)
 {
+    SPBox3D *box = SP_BOX3D(object);
+
     switch (key) {
-        case SP_ATTR_INKSCAPE_3DBOX_CORNER_A:
-            sp_3dbox_update_corner_with_value_from_svg (object, 2, value);
-            break;
-        case SP_ATTR_INKSCAPE_3DBOX_CORNER_B:
-            sp_3dbox_update_corner_with_value_from_svg (object, 1, value);
+        case SP_ATTR_INKSCAPE_BOX3D_PERSPECTIVE_ID:
+            if ( value && box->persp_href && ( strcmp(value, box->persp_href) == 0 ) ) {
+                /* No change, do nothing. */
+            } else {
+                if (box->persp_href) {
+                    g_free(box->persp_href);
+                    box->persp_href = NULL;
+                }
+                if (value) {
+                    box->persp_href = g_strdup(value);
+
+                    // Now do the attaching, which emits the changed signal.
+                    try {
+                        box->persp_ref->attach(Inkscape::URI(value));
+                    } catch (Inkscape::BadURIException &e) {
+                        g_warning("%s", e.what());
+                        box->persp_ref->detach();
+                    }
+                } else {
+                    // Detach, which emits the changed signal.
+                    box->persp_ref->detach();
+                }
+            }
+
+            // FIXME: Is the following update doubled by some call in either persp3d.cpp or vanishing_point_new.cpp?
+            box3d_position_set(box);
             break;
-        case SP_ATTR_INKSCAPE_3DBOX_CORNER_C:
-            sp_3dbox_update_corner_with_value_from_svg (object, 5, value);
+        case SP_ATTR_INKSCAPE_BOX3D_CORNER0:
+            if (value && strcmp(value, "0 : 0 : 0 : 0")) {
+                box->orig_corner0 = Proj::Pt3(value);
+                box->save_corner0 = box->orig_corner0;
+                box3d_position_set(box);
+            }
             break;
-        case SP_ATTR_INKSCAPE_3DBOX_PERSPECTIVE:
-        {
-            SP3DBox *box = SP_3DBOX (object);
-            sp_3dbox_update_perspective (SP_OBJECT_DOCUMENT (object)->get_persp_of_box (box), value);
+        case SP_ATTR_INKSCAPE_BOX3D_CORNER7:
+            if (value && strcmp(value, "0 : 0 : 0 : 0")) {
+                box->orig_corner7 = Proj::Pt3(value);
+                box->save_corner7 = box->orig_corner7;
+                box3d_position_set(box);
+            }
             break;
-        }
-       default:
+        default:
             if (((SPObjectClass *) (parent_class))->set) {
                 ((SPObjectClass *) (parent_class))->set(object, key, value);
             }
@@ -229,1044 +234,1194 @@ static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
     }
 }
 
+/**
+ * Gets called when (re)attached to another perspective.
+ */
+static void
+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);
+    }
+    if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
+    {
+        persp3d_add_box (SP_PERSP3D(ref), box);
+    }
+}
+
 static void
-sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
+box3d_update(SPObject *object, SPCtx *ctx, guint flags)
 {
     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
-        SP3DBox *box = SP_3DBOX(object);
-        sp_3dbox_link_to_existing_paths (box, SP_OBJECT_REPR(object));
+
+        /* FIXME?: Perhaps the display updates of box sides should be instantiated from here, but this
+           causes evil update loops so it's all done from box3d_position_set, which is called from
+           various other places (like the handlers in object-edit.cpp, vanishing-point.cpp, etc. */
+
     }
 
-    /* Invoke parent method */
+    // Invoke parent method
     if (((SPObjectClass *) (parent_class))->update)
         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
 }
 
 
-
-static Inkscape::XML::Node *sp_3dbox_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)
 {
-    SP3DBox *box = SP_3DBOX(object);
-    // FIXME: How to handle other contexts???
-    // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
-    if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
-        return repr;
+    SPBox3D *box = SP_BOX3D(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+        // 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
         repr = xml_doc->createElement("svg:g");
-        repr->setAttribute("sodipodi:type", "inkscape:3dbox");
-        /* Hook paths to the faces of the box */
-        for (int i = 0; i < 6; ++i) {
-            box->faces[i]->hook_path_to_3dbox();
-        }
-    }
-
-    for (int i = 0; i < 6; ++i) {
-        box->faces[i]->set_path_repr();
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {
-        gchar *str;
-        str = sp_3dbox_get_corner_coords_string (box, 2);
-        repr->setAttribute("inkscape:box3dcornerA", str);
-
-        str = sp_3dbox_get_corner_coords_string (box, 1);
-        repr->setAttribute("inkscape:box3dcornerB", str);
 
-        str = sp_3dbox_get_corner_coords_string (box, 5);
-        repr->setAttribute("inkscape:box3dcornerC", str);
+        if (box->persp_href) {
+            repr->setAttribute("inkscape:perspectiveID", box->persp_href);
+        } else {
+            /* box is not yet linked to a perspective; use the document's current perspective */
+            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 {
+                Glib::ustring href = "#";
+                href += doc->getCurrentPersp3D()->getId();
+                repr->setAttribute("inkscape:perspectiveID", href.c_str());
+            }
+        }
 
-        str = sp_3dbox_get_perspective_string (box);
-        repr->setAttribute("inkscape:perspective", str);
-        sp_3dbox_set_ratios (box);
+        gchar *coordstr0 = box->orig_corner0.coord_string();
+        gchar *coordstr7 = box->orig_corner7.coord_string();
+        repr->setAttribute("inkscape:corner0", coordstr0);
+        repr->setAttribute("inkscape:corner7", coordstr7);
+        g_free(coordstr0);
+        g_free(coordstr7);
 
-        g_free ((void *) str);
+        box->orig_corner0.normalize();
+        box->orig_corner7.normalize();
 
-        /* store center and construction-corners for later use during center-dragging */
-        NR::Maybe<NR::Point> cen = sp_3dbox_get_center (box);
-        if (cen) {
-            box->old_center = *cen;
-        }
-        box->old_corner2 = box->corners[2];
-        box->old_corner1 = box->corners[1];
-        box->old_corner0 = box->corners[0];
-        box->old_corner3 = box->corners[3];
-        box->old_corner5 = box->corners[5];
-        box->old_corner7 = box->corners[7];
+        box->save_corner0 = box->orig_corner0;
+        box->save_corner7 = box->orig_corner7;
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     return repr;
 }
 
 static gchar *
-sp_3dbox_description(SPItem *item)
+box3d_description(SPItem *item)
 {
-    g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
+    g_return_val_if_fail(SP_IS_BOX3D(item), NULL);
 
     return g_strdup(_("<b>3D Box</b>"));
 }
 
-void sp_3dbox_set_ratios (SP3DBox *box, Box3D::Axis axes)
+void box3d_position_set(SPBox3D *box)
 {
-    Box3D::Perspective3D *persp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box);
-    NR::Point pt;
-
-    if (axes & Box3D::X) {
-        pt = persp->get_vanishing_point (Box3D::X)->get_pos();
-        box->ratio_x = NR::L2 (pt - box->corners[2]) / NR::L2 (pt - box->corners[3]);
-    }
-
-    if (axes & Box3D::Y) {
-        pt = persp->get_vanishing_point (Box3D::Y)->get_pos();
-        box->ratio_y = NR::L2 (pt - box->corners[2]) / NR::L2 (pt - box->corners[0]);
-    }
-
-    if (axes & Box3D::Z) {
-        pt = persp->get_vanishing_point (Box3D::Z)->get_pos();
-        box->ratio_z = NR::L2 (pt - box->corners[4]) / NR::L2 (pt - box->corners[0]);
+    /* 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 = box->firstChild(); child; child = child->getNext() ) {
+        if (SP_IS_BOX3D_SIDE(child)) {
+            box3d_side_position_set(SP_BOX3D_SIDE(child));
+        }
     }
 }
 
-void
-sp_3dbox_switch_front_face (SP3DBox *box, Box3D::Axis axis)
+static Geom::Matrix
+box3d_set_transform(SPItem *item, Geom::Matrix const &xform)
 {
-    if (SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box)->get_vanishing_point (axis)->is_finite()) {
-        box->front_bits = box->front_bits ^ axis;
-    }
-}
+    SPBox3D *box = SP_BOX3D(item);
 
+    // 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.
 
-void
-sp_3dbox_position_set (SP3DBoxContext &bc)
-{
-    SP3DBox *box3d = SP_3DBOX(bc.item);
-
-    sp_3dbox_set_shape(box3d);
-
-    // FIXME: Why does the following call not automatically update the children
-    //        of box3d (which is an SPGroup, which should do this)?
-    //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-
-    /**
-    SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    ***/
-}
+    Geom::Matrix ret(Geom::Matrix(xform).without_translation());
+    gdouble const sw = hypot(ret[0], ret[1]);
+    gdouble const sh = hypot(ret[2], ret[3]);
 
-static void
-sp_3dbox_set_shape_from_points (SP3DBox *box, NR::Point const &cornerA, NR::Point const &cornerB, NR::Point const &cornerC)
-{
-    sp_3dbox_recompute_corners (box, cornerA, cornerB, cornerC);
+    for ( SPObject *child = box->firstChild(); child; child = child->getNext() ) {
+        if (SP_IS_ITEM(child)) {
+            SPItem *childitem = SP_ITEM(child);
 
-    // FIXME: How to handle other contexts???
-    // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
-    if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
-        return;
-    SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
-
-    if (bc->extruded) {
-        box->faces[0]->set_corners (box->corners[0], box->corners[4], box->corners[6], box->corners[2]);
-        box->faces[1]->set_corners (box->corners[1], box->corners[5], box->corners[7], box->corners[3]);
-        box->faces[2]->set_corners (box->corners[0], box->corners[1], box->corners[5], box->corners[4]);
-        box->faces[3]->set_corners (box->corners[2], box->corners[3], box->corners[7], box->corners[6]);
-        box->faces[5]->set_corners (box->corners[4], box->corners[5], box->corners[7], box->corners[6]);
-    }
-    box->faces[4]->set_corners (box->corners[0], box->corners[1], box->corners[3], box->corners[2]);
+            // Adjust stroke width
+            childitem->adjust_stroke(sqrt(fabs(sw * sh)));
 
-    sp_3dbox_update_curves (box);
-}
+            // Adjust pattern fill
+            childitem->adjust_pattern(xform);
 
-void
-// FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
-//        since SP3DBox is inherited from SPGroup. The following method is "artificially"
-//        called from sp_3dbox_update().
-//sp_3dbox_set_shape(SPShape *shape)
-sp_3dbox_set_shape(SP3DBox *box, bool use_previous_corners)
-{
-    if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
-        return;
-    SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
+            // Adjust gradient fill
+            childitem->adjust_gradient(xform);
 
-    if (!use_previous_corners) {
-        sp_3dbox_set_shape_from_points (box, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
-    } else {
-        sp_3dbox_set_shape_from_points (box, box->corners[2], box->corners[1], box->corners[5]);
+            // Adjust LPE
+            childitem->adjust_livepatheffect(xform);
+        }
     }
-}
 
-
-void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
-{
-    sp_3dbox_move_corner_in_XY_plane (box, 2, A);
-    sp_3dbox_move_corner_in_XY_plane (box, 1, B);
-    sp_3dbox_move_corner_in_Z_direction (box, 5, C);
+    return Geom::identity();
 }
 
-inline static double
-normalized_angle (double angle) {
-    if (angle < -M_PI) {
-        return angle + 2*M_PI;
-    } else if (angle > M_PI) {
-        return angle - 2*M_PI;
-    }
-    return angle;
+Proj::Pt3
+box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
+    return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
+                      (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
+                      (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
+                      1.0);
 }
 
-static gdouble
-sp_3dbox_corner_angle_to_VP (SP3DBox *box, Box3D::Axis axis, guint extreme_corner)
-{
-    Box3D::VanishingPoint *vp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box)->get_vanishing_point (axis);
-    NR::Point dir;
+Proj::Pt3
+box3d_get_proj_corner (SPBox3D const *box, guint id) {
+    return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
+                      (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
+                      (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
+                      1.0);
+}
 
-    if (vp->is_finite()) {
-        dir = NR::unit_vector (vp->get_pos() - box->corners[extreme_corner]);
+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 Geom::Point (NR_HUGE, NR_HUGE);
+    }
+    Geom::Matrix const i2d (SP_ITEM(box)->i2d_affine ());
+    if (item_coords) {
+        return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_corner).affine() * i2d.inverse();
     } else {
-        dir = NR::unit_vector (vp->v_dir);
+        return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_corner).affine();
     }
-
-    return atan2 (dir[NR::Y], dir[NR::X]);
 }
 
+Proj::Pt3
+box3d_get_proj_center (SPBox3D *box) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
+    return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
+                      (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
+                      (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
+                      1.0);
+}
 
-bool sp_3dbox_recompute_z_orders (SP3DBox *box)
-{
-    gint new_z_orders[6];
+Geom::Point
+box3d_get_center_screen (SPBox3D *box) {
+    Proj::Pt3 proj_center (box3d_get_proj_center (box));
+    if (!box3d_get_perspective(box)) {
+        return Geom::Point (NR_HUGE, NR_HUGE);
+    }
+    Geom::Matrix const i2d (SP_ITEM(box)->i2d_affine ());
+    return box3d_get_perspective(box)->perspective_impl->tmat.image(proj_center).affine() * i2d.inverse();
+}
 
-    // TODO: Determine the front corner depending on the distance from VPs and/or the user presets
-    guint front_corner = sp_3dbox_get_front_corner_id (box);
+/*
+ * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
+ * their intersection, we remember the last snapped line and keep snapping to this specific line as long
+ * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
+ */
 
-    gdouble dir_1x = sp_3dbox_corner_angle_to_VP (box, Box3D::X, front_corner);
-    gdouble dir_3x = sp_3dbox_corner_angle_to_VP (box, Box3D::X, front_corner ^ Box3D::Y);
+// Should we make the threshold settable in the preferences?
+static double remember_snap_threshold = 30;
+static guint remember_snap_index = 0;
+
+static Proj::Pt3
+box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
+    double z_coord = start_pt[Proj::Z];
+    double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
+    double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
+    double x_coord = start_pt[Proj::X];
+    double y_coord = start_pt[Proj::Y];
+    Proj::Pt3 A_proj (x_coord,          y_coord,          z_coord, 1.0);
+    Proj::Pt3 B_proj (x_coord + diff_x, y_coord,          z_coord, 1.0);
+    Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
+    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);
+
+    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)
+    Box3D::Line pl1(A, B);
+    Box3D::Line pl2(A, D);
+    Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
+    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;
+    Geom::Point snap_pts[num_snap_lines];
+
+    snap_pts[0] = pl1.closest_to (pt);
+    snap_pts[1] = pl2.closest_to (pt);
+    snap_pts[2] = diag1.closest_to (pt);
+    if (id == -1) {
+        snap_pts[3] = diag2.closest_to (pt);
+    }
 
-    gdouble dir_1y = sp_3dbox_corner_angle_to_VP (box, Box3D::Y, front_corner);
-    //gdouble dir_0y = sp_3dbox_corner_angle_to_VP (box, Box3D::Y, front_corner ^ Box3D::X);
+    gdouble const zoom = inkscape_active_desktop()->current_zoom();
 
-    gdouble dir_1z = sp_3dbox_corner_angle_to_VP (box, Box3D::Z, front_corner);
-    gdouble dir_3z = sp_3dbox_corner_angle_to_VP (box, Box3D::Z, front_corner ^ Box3D::Y);
+    // 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] = Geom::L2 (snap_pts[i] - pt) * zoom;
+    }
 
-    // Still not perfect, but only fails in some rather degenerate cases.
-    // I suspect that there is a more elegant model, though. :)
-    new_z_orders[0] = Box3D::face_containing_corner (Box3D::XY, front_corner);
-    if (normalized_angle (dir_1y - dir_1z) > 0) {
-        new_z_orders[1] = Box3D::face_containing_corner (Box3D::YZ, front_corner);
-        if (normalized_angle (dir_1x - dir_1z) > 0) {
-            new_z_orders[2] = Box3D::face_containing_corner (Box3D::XZ, front_corner ^ Box3D::Y);
-        } else {
-            new_z_orders[2] = Box3D::face_containing_corner (Box3D::XZ, front_corner);
-        }
-    } else {
-        if (normalized_angle (dir_3x - dir_3z) > 0) {
-            new_z_orders[1] = Box3D::face_containing_corner (Box3D::XZ, front_corner ^ Box3D::Y);
-            new_z_orders[2] = Box3D::face_containing_corner (Box3D::YZ, front_corner ^ Box3D::X);
-        } else {
-            if (normalized_angle (dir_1x - dir_1z) > 0) {
-                new_z_orders[1] = Box3D::face_containing_corner (Box3D::YZ, front_corner ^ Box3D::X);
-                new_z_orders[2] = Box3D::face_containing_corner (Box3D::XZ, front_corner);
-            } else {
-                new_z_orders[1] = Box3D::face_containing_corner (Box3D::XZ, front_corner);
-                new_z_orders[2] = Box3D::face_containing_corner (Box3D::YZ, front_corner ^ Box3D::X);
-            }
+    // while we are within a given tolerance of the starting point,
+    // keep snapping to the same point to avoid jumping
+    bool within_tolerance = true;
+    for (int i = 0; i < num_snap_lines; ++i) {
+        if (snap_dists[i] > remember_snap_threshold) {
+            within_tolerance = false;
+            break;
         }
     }
 
-    new_z_orders[3] = Box3D::opposite_face (new_z_orders[2]);
-    new_z_orders[4] = Box3D::opposite_face (new_z_orders[1]);
-    new_z_orders[5] = Box3D::opposite_face (new_z_orders[0]);
-
-    /* We only need to look for changes among the topmost three faces because the order
-       of the other ones is just inverted. */
-    if ((box->z_orders[0] != new_z_orders[0]) ||
-        (box->z_orders[1] != new_z_orders[1]) ||
-        (box->z_orders[2] != new_z_orders[2]))
-    {
-        for (int i = 0; i < 6; ++i) {
-            box->z_orders[i] = new_z_orders[i];
+    // find the closest snapping point
+    int snap_index = -1;
+    double snap_dist = NR_HUGE;
+    for (int i = 0; i < num_snap_lines; ++i) {
+        if (snap_dists[i] < snap_dist) {
+            snap_index = i;
+            snap_dist = snap_dists[i];
         }
-        return true;
     }
 
-    return false;
+    // snap to the closest point (or the previously remembered one
+    // if we are within tolerance of the starting point)
+    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)->perspective_impl->tmat.preimage (result, z_coord, Proj::Z);
 }
 
-// convenience
-static bool sp_3dbox_is_subset_or_superset (std::vector<gint> const &list1, std::vector<gint> const &list2)
+SPBox3D * SPBox3D::createBox3D(SPItem * parent)
 {
-    return (std::includes (list1.begin(), list1.end(), list2.begin(), list2.end()) ||
-            std::includes (list2.begin(), list2.end(), list1.begin(), list1.end()));
+    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;
 }
 
-static bool sp_3dbox_differ_by_opposite_faces (std::vector<gint> const &list1, std::vector<gint> const &list2)
-{
-    std::vector<gint> diff1;
-    std::vector<gint> diff2;
-    std::set_difference (list1.begin(), list1.end(), list2.begin(), list2.end(),
-                         std::insert_iterator<std::vector<gint> >(diff1, diff1.begin()));
-    std::set_difference (list2.begin(), list2.end(), list1.begin(), list1.end(),
-                         std::insert_iterator<std::vector<gint> >(diff2, diff2.begin()));
-
-    if (diff1.size() == 3 || diff1.size() != diff2.size())
-        return false;
-
-    for (guint i = 0; i < diff1.size(); ++i) {
-        if (std::find (diff2.begin(), diff2.end(), Box3D::opposite_face (diff1[i])) == diff2.end()) {
-            return false;
+void
+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();
+    box->orig_corner7.normalize();
+
+    /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
+    if (!(movement & Box3D::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));
         }
-    }
-    return true;
-}
 
-static gint
-sp_3dbox_face_containing_diagonal_corners (guint corner1, guint corner2)
-{
-    Box3D::Axis plane = (Box3D::Axis) (corner1 ^ corner2);
-    if (!Box3D::is_plane (plane)) {
-        g_warning ("Corners %d and %d should span a plane.\n", corner1, corner2);
-        return 0;
+        // normalizing pt_proj is essential because we want to mingle affine coordinates
+        pt_proj.normalize();
+        box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
+                                       (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
+                                       box->save_corner0[Proj::Z],
+                                       1.0);
+        box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
+                                       (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
+                                       box->save_corner7[Proj::Z],
+                                       1.0);
+    } else {
+        Persp3D *persp = box3d_get_perspective(box);
+        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);
+        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);
+        bool corner7_move_y =  (id & Box3D::Y) && (movement & Box3D::Y);
+        // normalizing pt_proj is essential because we want to mingle affine coordinates
+        pt_proj.normalize();
+        box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
+                                       corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
+                                       (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
+                                       1.0);
+        box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
+                                       corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
+                                       (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
+                                       1.0);
     }
-
-    return Box3D::face_containing_corner (plane, corner1);
+    // FIXME: Should we update the box here? If so, how?
 }
 
-static std::vector<gint> sp_3dbox_adjacent_faces_of_edge (guint corner1, guint corner2) {
-    std::vector<gint> adj_faces;
-    Box3D::Axis edge = (Box3D::Axis) (corner1 ^ corner2);
-    if (!Box3D::is_single_axis_direction (edge)) {
-        return adj_faces;
-    }
+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));
 
-    Box3D::Axis plane = Box3D::orth_plane_or_axis (edge);
-    Box3D::Axis axis1 = Box3D::extract_first_axis_direction (plane);
-    Box3D::Axis axis2 = Box3D::extract_second_axis_direction (plane);
-    adj_faces.push_back (Box3D::face_containing_corner ((Box3D::Axis) (edge ^ axis1), corner1));
-    adj_faces.push_back (Box3D::face_containing_corner ((Box3D::Axis) (edge ^ axis2), corner1));
-    return adj_faces;
-}
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
 
-static std::vector<gint> sp_3dbox_faces_meeting_in_corner (guint corner) {
-    std::vector<gint> faces;
-    for (int i = 0; i < 3; ++i) {
-        faces.push_back (sp_3dbox_face_containing_diagonal_corners (corner, corner ^ Box3D::planes[i]));
-    }
-    return faces;
-}
+    Persp3D *persp = box3d_get_perspective(box);
+    if (!(movement & Box3D::Z)) {
+        double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
+        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;
 
-static void sp_3dbox_remaining_faces (std::vector<gint> const &faces, std::vector<gint> &rem_faces)
-{
-    rem_faces.clear();
-    for (gint i = 0; i < 6; ++i) {
-        if (std::find (faces.begin(), faces.end(), i) == faces.end()) {
-            rem_faces.push_back (i);
+        Proj::Pt3 pt_proj (persp->perspective_impl->tmat.preimage (new_pos, coord, Proj::Z));
+        if (constrained) {
+            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);
         }
+        // normalizing pt_proj is essential because we want to mingle affine coordinates
+        pt_proj.normalize();
+        box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
+                                       (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
+                                       box->orig_corner0[Proj::Z],
+                                       1.0);
+        box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
+                                       (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
+                                       box->orig_corner7[Proj::Z],
+                                       1.0);
+    } else {
+        double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
+        double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
+
+        Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
+        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();
+        box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
+                                       box->orig_corner0[Proj::Y],
+                                       pt_proj[Proj::Z] - radz,
+                                       1.0);
+        box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
+                                       box->orig_corner7[Proj::Y],
+                                       pt_proj[Proj::Z] + radz,
+                                       1.0);
     }
 }
 
 /*
- * Given two adjacent edges (\a c2,\a c1) and (\a c2, \a c3) of \a box (with common corner \a c2),
- * check whether both lie on the convex hull of the point configuration given by \a box's corners.
- */
-static bool
-sp_3dbox_is_border_edge_pair (SP3DBox *box, guint const c1, guint const c2, guint const c3)
-{
-    Box3D::Axis edge21 = (Box3D::Axis) (c2 ^ c1);
-    Box3D::Axis edge23 = (Box3D::Axis) (c2 ^ c3);
-    Box3D::Axis rear_axis = Box3D::orth_plane_or_axis ((Box3D::Axis) (edge21 ^ edge23));
-    NR::Point corner2 = box->corners[c2];
-    NR::Point dir21 = box->corners[c1] - corner2;
-    NR::Point dir23 = box->corners[c3] - corner2;
-
-    if (!Box3D::lies_in_sector (dir21, dir23, box->corners[c2 ^ edge21 ^ edge23] - corner2) ||
-        !Box3D::lies_in_sector (dir21, dir23, box->corners[c2 ^ rear_axis] - corner2) ||
-        !Box3D::lies_in_sector (dir21, dir23, box->corners[c2 ^ rear_axis ^ edge21] - corner2) ||
-        !Box3D::lies_in_sector (dir21, dir23, box->corners[c2 ^ rear_axis ^ edge21 ^ edge23] - corner2) ||
-        !Box3D::lies_in_sector (dir21, dir23, box->corners[c2 ^ rear_axis ^ edge23] - corner2)) {
-        // corner triple c1, c2, c3 doesn't bound the convex hull
-        return false;
-    }
-    // corner triple c1, c2, c3 bounds the convex hull
-    return true;    
-}
-
-/*
- * Test whether there are any adjacent corners of \a corner (i.e., connected with it along one of the axes)
- * such that the corresponding edges bound the convex hull of the box (as a point configuration in the plane)
- * If this is the case, return the corresponding two adjacent corners; otherwise return (-1, -1).
+ * Manipulates corner1 through corner4 to contain the indices of the corners
+ * from which the perspective lines in the direction of 'axis' emerge
  */
-static Box3D::Axis
-sp_3dbox_axis_pair_bounding_convex_hull (SP3DBox *box, guint corner)
- {
-    guint adj1 = corner ^ Box3D::X;
-    guint adj2 = corner ^ Box3D::Y;
-    guint adj3 = corner ^ Box3D::Z;
-
-    if (sp_3dbox_is_border_edge_pair (box, adj1, corner, adj2)) {
-        return Box3D::XY;
-    }
-    if (sp_3dbox_is_border_edge_pair (box, adj1, corner, adj3)) {
-        return Box3D::XZ;
-    }
-    if (sp_3dbox_is_border_edge_pair (box, adj2, corner, adj3)) {
-        return Box3D::YZ;
-    }
-    return Box3D::NONE;
-}
-
-// inside_hull is modified 'in place' by the following function
-static void sp_3dbox_corner_configuration (SP3DBox *box, std::vector<gint> &on_hull, std::vector<gint> &inside_hull)
+void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
+                            Geom::Point &corner1, Geom::Point &corner2, Geom::Point &corner3, Geom::Point &corner4)
 {
-    for (int i = 0; i < 8; ++i) {
-        Box3D::Axis bounding_edges = sp_3dbox_axis_pair_bounding_convex_hull (box, i);
-        if (bounding_edges != Box3D::NONE) {
-            on_hull.push_back (i);
-        } else {
-            inside_hull.push_back (i);
-        }
+    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]) ?
+        box->orig_corner0[axis] :
+        box->orig_corner7[axis];
+
+    Proj::Pt3 c1, c2, c3, c4;
+    // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
+    switch (axis) {
+        case Proj::X:
+            c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
+            c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
+            c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
+            c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
+            break;
+        case Proj::Y:
+            c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
+            c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
+            c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
+            c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
+            break;
+        case Proj::Z:
+            c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
+            c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
+            c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
+            c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
+            break;
+        default:
+            return;
     }
+    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();
 }
 
-/* returns true if there was a change in the z-orders (which triggers an update of the repr) */
-static bool sp_3dbox_recompute_z_orders_by_corner_configuration (SP3DBox *box)
-{
-    gint new_z_orders[6];
-    Box3D::Axis front_rear_axis = Box3D::Z;
-
-    std::vector<gint> on_hull;
-    std::vector<gint> inside_hull;
-    std::vector<gint> visible_faces;
+/* 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 n0 = (B - A).ccw();
+    double d0 = dot(n0,A);
 
-    sp_3dbox_corner_configuration (box, on_hull, inside_hull);
+    Geom::Point n1 = (D - C).ccw();
+    double d1 = dot(n1,C);
 
-    switch (on_hull.size()) {
-        case 4:
-            {
-                // the following works because on_hull is sorted
-                gint front_face = sp_3dbox_face_containing_diagonal_corners (on_hull[0], on_hull[3]);
-                visible_faces.push_back (front_face);
-            }
-            break;
+    Geom::Line lineAB(A,B);
+    Geom::Line lineCD(C,D);
 
-        case 6:
-        {
-            guint c1 = inside_hull[0] ^ Box3D::XYZ;
-            guint c2 = inside_hull[1] ^ Box3D::XYZ;
-            Box3D::Axis edge = (Box3D::Axis) (c1 ^ c2);
-            if (Box3D::is_single_axis_direction (edge)) {
-                visible_faces = sp_3dbox_adjacent_faces_of_edge (c1, c2);
-            } else if (c1 == c2 ^ Box3D::XYZ) {
-                guint c_cmp = sp_3dbox_get_corner_id_along_edge (box, 0, front_rear_axis, Box3D::FRONT);
-                guint visible_front_corner = (((c_cmp & front_rear_axis) == (c1 & front_rear_axis)) ? c1 : c2);
-                visible_faces = sp_3dbox_faces_meeting_in_corner (visible_front_corner);
-            } else {
-                g_print ("Warning: Unhandled case. Current z-orders remain unchanged.\n");
-                return false;
-            }
-            break;
-        }
-
-        default:
-            g_print ("Warning: Unhandled case. Current z-orders are not changed.\n");
-            return false;
+    Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
+    try
+    {
+        inters = Geom::intersection(lineAB, lineCD);
     }
-
-    // check for weird corner configurations that cannot be handled by the above code
-    if (std::find (visible_faces.begin(), visible_faces.end(), -1) != visible_faces.end()) {
-        g_warning ("Theoretically impossible corner configuration\n");
+    catch (Geom::InfiniteSolutions e)
+    {
+        // We're probably dealing with parallel lines, so they don't really cross
         return false;
     }
 
-    // sort the list of visible faces for later use (although it may be already sorted anyway)
-    std::sort (visible_faces.begin(), visible_faces.end());
-
-    std::vector<gint> invisible_faces;
-    sp_3dbox_remaining_faces (visible_faces, invisible_faces);
-
-
-    if (!sp_3dbox_is_subset_or_superset (visible_faces, box->currently_visible_faces) &&
-        !sp_3dbox_differ_by_opposite_faces (visible_faces, box->currently_visible_faces)) {
-        std::swap (visible_faces, invisible_faces);
-        if (!sp_3dbox_is_subset_or_superset (visible_faces, box->currently_visible_faces) &&
-            !sp_3dbox_differ_by_opposite_faces (visible_faces, box->currently_visible_faces)) {
-            // FIXME: Hopefully this case is only caused by rounding errors or something similar;
-            //        does it need further investigation?
-            g_warning ("Can't find out which faces are visible and which aren't ...\n");
-            return false;
-        }
+    if (!inters) {
+        return false;
     }
 
-    box->currently_visible_faces = visible_faces;
+    Geom::Point E = lineAB.pointAt((*inters).ta); // the point of intersection
 
-    // set new z-orders according to the visible/invisible faces
-    guint vis_size = visible_faces.size();
-    for (guint i = 0; i < vis_size; ++i) {
-        new_z_orders[i] = visible_faces[i];
+    if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
+        // C and D lie on the same side of the line AB
+        return false;
     }
-    for (guint i = 0; i < invisible_faces.size(); ++i) {
-        new_z_orders[vis_size + i] = invisible_faces[i];
+    if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
+        // A and B lie on different sides of the line CD
+        return true;
+    } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
+        // The line CD passes on the "wrong" side of A
+        return false;
     }
 
-    // test whether any z-orders actually changed and indicate this in the return status
-    for (int i = 0; i < 6; ++i) {
-        if (box->z_orders[i] != new_z_orders[i]) {
-            // we update the z-orders starting from the index where the change occurs
-            for (int j = i; j < 6; ++j) {
-                box->z_orders[j] = new_z_orders[j];
-            }
-            return true;
-        }
-    }
-    return false;
+    // The line CD passes on the "correct" side of A
+    return true;
 }
 
-// FIXME: Can we unify this and the next function for setting the z-orders?
-void sp_3dbox_set_z_orders_in_the_first_place (SP3DBox *box)
-{
-    // For efficiency reasons, we only set the new z-orders if something really changed
-    if (sp_3dbox_recompute_z_orders (box)) {
-        box->faces[box->z_orders[0]]->lower_to_bottom ();
-        box->faces[box->z_orders[1]]->lower_to_bottom ();
-        box->faces[box->z_orders[2]]->lower_to_bottom ();
-        box->faces[box->z_orders[3]]->lower_to_bottom ();
-        box->faces[box->z_orders[4]]->lower_to_bottom ();
-        box->faces[box->z_orders[5]]->lower_to_bottom ();
-    }
+static bool
+box3d_XY_axes_are_swapped (SPBox3D *box) {
+    Persp3D *persp = box3d_get_perspective(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);
+    Geom::Point v1(l1.direction());
+    Geom::Point v2(l2.direction());
+    v1.normalize();
+    v2.normalize();
+
+    return (v1[Geom::X]*v2[Geom::Y] - v1[Geom::Y]*v2[Geom::X] > 0);
 }
 
-void sp_3dbox_set_z_orders_later_on (SP3DBox *box)
-{
-    // For efficiency reasons, we only set the new z-orders if something really changed
-    if (sp_3dbox_recompute_z_orders_by_corner_configuration (box)) {
-        box->faces[box->z_orders[0]]->lower_to_bottom ();
-        box->faces[box->z_orders[1]]->lower_to_bottom ();
-        box->faces[box->z_orders[2]]->lower_to_bottom ();
-        box->faces[box->z_orders[3]]->lower_to_bottom ();
-        box->faces[box->z_orders[4]]->lower_to_bottom ();
-        box->faces[box->z_orders[5]]->lower_to_bottom ();
-    }
+static inline void
+box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
+    z_orders[0] = a;
+    z_orders[1] = b;
+    z_orders[2] = c;
+    z_orders[3] = d;
+    z_orders[4] = e;
+    z_orders[5] = f;
 }
 
-void
-sp_3dbox_update_curves (SP3DBox *box) {
-    for (int i = 0; i < 6; ++i) {
-        if (box->faces[i]) box->faces[i]->set_curve();
+static inline void
+box3d_swap_z_orders (int z_orders[6]) {
+    int tmp;
+    for (int i = 0; i < 3; ++i) {
+        tmp = z_orders[i];
+        z_orders[i] = z_orders[5-i];
+        z_orders[5-i] = tmp;
     }
 }
 
-/**
- * In some situations (e.g., after cloning boxes, undo & redo, or reading boxes from a file) there are
- * paths already present in the document which correspond to the faces of newly created boxes, but their
- * 'path' members don't link to them yet. The following function corrects this if necessary.
+/*
+ * In standard perspective we have:
+ * 2 = front face
+ * 1 = top face
+ * 0 = left face
+ * 3 = right face
+ * 4 = bottom face
+ * 5 = rear face
  */
-void
-sp_3dbox_link_to_existing_paths (SP3DBox *box, Inkscape::XML::Node *repr) {
-    // TODO: We should probably destroy the existing paths and recreate them because we don't know
-    //       precisely which path corresponds to which face. Does this make a difference?
-    //       In sp_3dbox_write we write the correct paths anyway, don't we? But we could get into
-    //       trouble at a later stage when we only write single faces for degenerate boxes.
-
-    SPDocument *document = SP_OBJECT_DOCUMENT(box);
-    guint face_id = 0;
-
-    for (Inkscape::XML::Node *i = sp_repr_children(repr); i != NULL; i = sp_repr_next(i)) {
-        if (face_id > 5) {
-            g_warning ("SVG representation of 3D boxes must contain 6 paths or less.\n");
-            break;
-        }
 
-        SPObject *face_object = document->getObjectByRepr((Inkscape::XML::Node *) i);
-        if (!SP_IS_PATH(face_object)) {
-            g_warning ("SVG representation of 3D boxes should only contain paths.\n");
-            continue;
-        }
-        // TODO: Currently we don't check whether all paths are being linked to different faces.
-        //       This is no problem with valid SVG files. It may lead to crashes, however,
-        //       in case a file is corrupt (e.g., two or more faces have identical descriptions).
-        gint id = Box3DFace::descr_to_id (i->attribute ("inkscape:box3dface"));
-        box->faces[id]->hook_path_to_3dbox(SP_PATH(face_object));
-        ++face_id;
-    }
-    if (face_id < 6) {
-        //g_warning ("SVG representation of 3D boxes should contain exactly 6 paths (degenerate boxes are not yet supported).\n");
-        // TODO: Check whether it is safe to add the remaining paths to the box and do so in case it is.
-        //       (But we also land here for newly created boxes where we shouldn't add any paths because
-        //       This is done in sp_3dbox_write later on.
+/* All VPs infinite */
+static void
+box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
+    Persp3D *persp = box3d_get_perspective(box);
+    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);
+
+    switch(central_axis) {
+        case Box3D::X:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
+            }
+            break;
+        case Box3D::Y:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
+            }
+            break;
+        case Box3D::Z:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
+            }
+            break;
+        case Box3D::NONE:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
+            }
+            break;
+        default:
+            g_assert_not_reached();
+            break;
     }
 }
 
-void
-sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
-{
-    Box3D::Perspective3D * persp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box);
+/* Precisely one finite VP */
+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);
+    Geom::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
 
-    NR::Point A (box->corners[id ^ Box3D::XY]);
-    if (Box3D::is_single_axis_direction (axes)) {
-        pt = Box3D::PerspectiveLine (box->corners[id], axes, persp).closest_to(pt);
-    }
+    // 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;
+    Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
+    int inside1 = 0;
+    int inside2 = 0;
+    inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
+    inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
 
-    /* set the 'front' corners */
-    box->corners[id] = pt;
+    bool swapped = box3d_XY_axes_are_swapped(box);
 
-    Box3D::PerspectiveLine pl_one (A, Box3D::Y, persp);
-    Box3D::PerspectiveLine pl_two (pt, Box3D::X, persp);
-    box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
+    switch(central_axis) {
+        case Box3D::X:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
+            }
+            break;
+        case Box3D::Y:
+            if (inside2 > 0) {
+                box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
+            } else if (inside2 < 0) {
+                box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
+            } else {
+                if (!swapped) {
+                    box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
+                }
+            }
+            break;
+        case Box3D::Z:
+            if (inside2) {
+                if (!swapped) {
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
+                }
+            } else if (inside1) {
+                if (!swapped) {
+                    box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
+                }
+            } else {
+                // "regular" case
+                if (!swapped) {
+                    box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
+                }
+            }
+            break;
+        case Box3D::NONE:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
+            }
+            break;
+        default:
+            g_assert_not_reached();
+    }
+}
 
-    pl_one = Box3D::PerspectiveLine (A, Box3D::X, persp);
-    pl_two = Box3D::PerspectiveLine (pt, Box3D::Y, persp);
-    box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
+/* Precisely 2 finite VPs */
+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);
 
-    /* set the 'rear' corners */
-    NR::Point B (box->corners[id ^ Box3D::XYZ]);
+    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));
 
-    pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z, persp);
-    pl_two = Box3D::PerspectiveLine (B, Box3D::Y, persp);
-    box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
+    bool swapped = box3d_XY_axes_are_swapped(box);
 
-    pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X, persp);
-    pl_two = Box3D::PerspectiveLine (pt, Box3D::Z, persp);
-    box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
+    int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
+    //int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
 
-    pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y, persp);
-    pl_two = Box3D::PerspectiveLine (B, Box3D::X, persp);
-    box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
-    
-}
+    int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
+    int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
 
-void
-sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
-{
-    if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
+    //int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
+    int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
 
-    Box3D::Perspective3D * persp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box);
+    switch(central_axis) {
+        case Box3D::X:
+            if (!swapped) {
+                if (insidezy == -1) {
+                    box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
+                } else if (insidexy == 1) {
+                    box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
+                }
+            } else {
+                if (insideyz == -1) {
+                    box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
+                } else {
+                    if (!swapped) {
+                        box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
+                    } else {
+                        if (insidexy == 0) {
+                            box3d_aux_set_z_orders (z_orders, 3, 5, 1, 0, 2, 4);
+                        } else {
+                            box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
+                        }
+                    }
+                }
+            }
+            break;
+        case Box3D::Y:
+            if (!swapped) {
+                if (insideyz == 1) {
+                    box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
+                }
+            } else {
+                if (insideyx == 1) {
+                    box3d_aux_set_z_orders (z_orders, 4, 0, 5, 1, 3, 2);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
+                }
+            }
+            break;
+        case Box3D::Z:
+            if (!swapped) {
+                if (insidezy == 1) {
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
+                } else if (insidexy == -1) {
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
+                }
+            } else {
+                box3d_aux_set_z_orders (z_orders, 3, 4, 5, 1, 0, 2);
+            }
+            break;
+        case Box3D::NONE:
+            if (!swapped) {
+                box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
+            } else {
+                box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
+            }
+            break;
+        default:
+            g_assert_not_reached();
+            break;
+    }
+}
 
-    /* set the four corners of the face containing corners[id] */
-    box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z, persp).closest_to(pt);
+/*
+ * It can happen that during dragging the box is everted.
+ * In this case the opposite sides in this direction need to be swapped
+ */
+static Box3D::Axis
+box3d_everted_directions (SPBox3D *box) {
+    Box3D::Axis ev = Box3D::NONE;
 
-    Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X, persp);
-    Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z, persp);
-    box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
 
-    pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y, persp);
-    pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z, persp);
-    box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
+    if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
+        ev = (Box3D::Axis) (ev ^ Box3D::X);
+    if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
+        ev = (Box3D::Axis) (ev ^ Box3D::Y);
+    if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
+        ev = (Box3D::Axis) (ev ^ Box3D::Z);
 
-    pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y, persp);
-    pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z, persp);
-    box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
+    return ev;
 }
 
 static void
-sp_3dbox_reshape_edge_after_VP_toggling (SP3DBox *box, const guint corner, const Box3D::Axis axis, Box3D::Perspective3D *persp)
-{
-    /* Hmm, perhaps we should simply use one of the corners as the pivot point.
-       But this way we minimize the amount of reshaping.
-       On second thought, we need to find a way to ensure that all boxes sharing the same
-       perspective are updated consistently _as a group_. That is, they should also retain
-       their relative positions towards each other. */
-    NR::Maybe<NR::Point> pt = sp_3dbox_get_midpoint_between_corners (box, corner, corner ^ axis);
-    g_return_if_fail (pt);
-
-    Box3D::Axis axis2 = ((axis == Box3D::Y) ? Box3D::X : Box3D::Y);
+box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
+    int pos1 = -1;
+    int pos2 = -1;
 
-    Box3D::PerspectiveLine line1 (box->corners[corner], axis2, persp);
-    Box3D::PerspectiveLine line2 (box->corners[corner ^ axis], axis2, persp);
-
-    Box3D::PerspectiveLine line3 (*pt, axis, persp);
-
-    NR::Point new_corner1 = line1.meet (line3);
-    NR::Point new_corner2 = line2.meet (line3);
+    for (int i = 0; i < 6; ++i) {
+        if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
+            if (pos1 == -1) {
+                pos1 = i;
+            } else {
+                pos2 = i;
+                break;
+            }
+        }
+    }
 
-    box->corners[corner] = new_corner1;
-    box->corners[corner ^ axis] = new_corner2;
+    int tmp = z_orders[pos1];
+    z_orders[pos1] = z_orders[pos2];
+    z_orders[pos2] = tmp;
 }
 
-void
-sp_3dbox_reshape_after_VP_toggling (SP3DBox *box, Box3D::Axis axis)
-{
-    Box3D::Perspective3D *persp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box);
-    std::pair<Box3D::Axis, Box3D::Axis> dirs = Box3D::get_remaining_axes (axis);
 
-    sp_3dbox_reshape_edge_after_VP_toggling (box, 0, axis, persp);
-    sp_3dbox_reshape_edge_after_VP_toggling (box, 0 ^ dirs.first, axis, persp);
-    sp_3dbox_reshape_edge_after_VP_toggling (box, 0 ^ dirs.first ^ dirs.second, axis, persp);
-    sp_3dbox_reshape_edge_after_VP_toggling (box, 0 ^ dirs.second, axis, persp);
-}
+bool
+box3d_recompute_z_orders (SPBox3D *box) {
+    Persp3D *persp = box3d_get_perspective(box);
 
-NR::Maybe<NR::Point>
-sp_3dbox_get_center (SP3DBox *box)
-{
-    return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
-}
+    if (!persp)
+        return false;
 
-NR::Point
-sp_3dbox_get_midpoint_in_axis_direction (NR::Point const &C, NR::Point const &D, Box3D::Axis axis, Box3D::Perspective3D *persp)
-{
-    Box3D::PerspectiveLine pl (D, axis, persp);
-    return pl.pt_with_given_cross_ratio (C, D, -1.0);
-}
+    int z_orders[6];
 
-// TODO: The following function can probably be rewritten in a much more elegant and robust way
-//        by using projective coordinates for all points and using the cross ratio.
-NR::Maybe<NR::Point>
-sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
-{
-    Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
+    Geom::Point c3(box3d_get_corner_screen(box, 3, false));
 
-    // Is all this sufficiently precise also for degenerate cases?
-    if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
-        Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
+    // determine directions from corner3 to the VPs
+    int num_finite = 0;
+    Box3D::Axis axis_finite = Box3D::NONE;
+    Box3D::Axis axis_infinite = Box3D::NONE;
+    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->perspective_impl, Proj::axes[i])) {
+            num_finite++;
+            axis_finite = Box3D::axes[i];
+        } else {
+            axis_infinite = Box3D::axes[i];
+        }
+    }
 
-        Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
-        Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
-        NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
+    // determine the "central" axis (if there is one)
+    Box3D::Axis central_axis = Box3D::NONE;
+    if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
+        central_axis = Box3D::Z;
+    } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
+        central_axis = Box3D::X;
+    } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
+        central_axis = Box3D::Y;
+    }
 
-        if (!adjacent_face_center) return NR::Nothing();
+    switch (num_finite) {
+        case 0:
+            // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
+            //               coincide, hence only need to be computed once in a more central location.
+            box3d_set_new_z_orders_case0(box, z_orders, central_axis);
+            break;
+        case 1:
+            box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
+            break;
+        case 2:
+        case 3:
+            box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
+            break;
+        default:
+        /*
+         * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
+         * 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 Geom::Point altogether
+        Box3D::Axis central_axis = Box3D::NONE;
+        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]);
+
+        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;
+        } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
+            central_axis = Box3D::Y;
+        } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
+            central_axis = Box3D::Z;
+        }
 
-        Box3D::Perspective3D * persp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box);
+        unsigned int central_corner = 3 ^ central_axis;
+        if (central_axis == Box3D::Z) {
+            central_corner = central_corner ^ Box3D::XYZ;
+        }
+        if (box3d_XY_axes_are_swapped(box)) {
+            central_corner = central_corner ^ Box3D::XYZ;
+        }
 
-        Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir, persp);
-        return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes, persp));
-    } else {
-        Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
-        Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
-        Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
-        return diag1.intersect(diag2);
+        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[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:
+                if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
+                    box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
+                } else {
+                    // degenerate case
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
+                }
+                break;
+
+            case Box3D::Z:
+                if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
+                    // degenerate case
+                    box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
+                } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
+                    // degenerate case
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
+                }
+                break;
+
+            case Box3D::X:
+                if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
+                    // degenerate case
+                    box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
+                } else {
+                    box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
+                }
+                break;
+
+            case Box3D::NONE:
+                box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
+                break;
+
+            default:
+                g_assert_not_reached();
+                break;
+        } // end default case
     }
-}
 
-static gchar *
-sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id)
-{
-    id = id % 8;
-    Inkscape::SVGOStringStream os;
-    os << box->corners[id][NR::X] << "," << box->corners[id][NR::Y];
-    return g_strdup(os.str().c_str());
+    // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
+    //       instead of the hard-coded corner #3 in the computations above
+    Box3D::Axis ev = box3d_everted_directions(box);
+    for (int i = 0; i < 3; ++i) {
+        if (ev & Box3D::axes[i]) {
+            box3d_swap_sides(z_orders, Box3D::axes[i]);
+        }
+    }
+
+    // Check whether anything actually changed
+    for (int i = 0; i < 6; ++i) {
+        if (box->z_orders[i] != z_orders[i]) {
+            for (int j = i; j < 6; ++j) {
+                box->z_orders[j] = z_orders[j];
+            }
+            return true;
+        }
+    }
+    return false;
 }
 
-static std::pair<gdouble, gdouble>
-sp_3dbox_get_coord_pair_from_string (const gchar *coords)
+static std::map<int, Box3DSide *> box3d_get_sides(SPBox3D *box)
 {
-    gchar **coordpair = g_strsplit( coords, ",", 0);
-    // We might as well rely on g_ascii_strtod to convert the NULL pointer to 0.0,
-    // but we include the following test anyway
-    if (coordpair[0] == NULL || coordpair[1] == NULL) {
-        g_strfreev (coordpair);
-        g_warning ("Coordinate conversion failed.\n");
-        return std::make_pair(0.0, 0.0);
+    std::map<int, Box3DSide *> sides;
+    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;
+}
 
-    gdouble coord1 = g_ascii_strtod(coordpair[0], NULL);
-    gdouble coord2 = g_ascii_strtod(coordpair[1], NULL);
-    g_strfreev (coordpair);
 
-    return std::make_pair(coord1, coord2);
+// TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
+void
+box3d_set_z_orders (SPBox3D *box) {
+    // For efficiency reasons, we only set the new z-orders if something really changed
+    if (box3d_recompute_z_orders (box)) {
+        std::map<int, Box3DSide *> sides = box3d_get_sides(box);
+        std::map<int, Box3DSide *>::iterator side;
+        for (unsigned int i = 0; i < 6; ++i) {
+            side = sides.find(box->z_orders[i]);
+            if (side != sides.end()) {
+                SP_ITEM((*side).second)->lowerToBottom();
+            }
+        }
+    }
 }
 
-static gchar *
-sp_3dbox_get_perspective_string (SP3DBox *box)
-{
-    
-    return sp_3dbox_get_svg_descr_of_persp (SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box));
-}
-  
-gchar *
-sp_3dbox_get_svg_descr_of_persp (Box3D::Perspective3D *persp)
-{
-    // FIXME: We should move this code to perspective3d.cpp, but this yields compiler errors. Why?
-    Inkscape::SVGOStringStream os;
-
-    Box3D::VanishingPoint vp = *(persp->get_vanishing_point (Box3D::X));
-    os << vp[NR::X] << "," << vp[NR::Y] << ",";
-    os << vp.v_dir[NR::X] << "," << vp.v_dir[NR::Y] << ",";
-    if (vp.is_finite()) {
-        os << "finite,";
+/*
+ * Auxiliary function for z-order recomputing:
+ * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
+ * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
+ * lies in the sector if it lies between the two (parallel) PLs.
+ * \ret *  0 if \a pt doesn't lie in the sector
+ *      *  1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
+ *           from the edge between the two corners to \a pt points towards the VP
+ *      * -1 otherwise
+ */
+// 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, Geom::Point const &pt, int id1, int id2, Box3D::Axis axis) {
+    Persp3D *persp = box3d_get_perspective(box);
+
+    // the two corners
+    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->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 {
-        os << "infinite,";
+        Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
+        Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
+        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);
+            Geom::Point c3(box3d_get_corner_screen(box, id1 ^ axis, false));
+            if (edge.lie_on_same_side(pt, c3)) {
+                ret = 1;
+            } else {
+                ret = -1;
+            }
+        }
     }
+    return ret;
+}
 
-    vp = *(persp->get_vanishing_point (Box3D::Y));
-    os << vp[NR::X] << "," << vp[NR::Y] << ",";
-    os << vp.v_dir[NR::X] << "," << vp.v_dir[NR::Y] << ",";
-    if (vp.is_finite()) {
-        os << "finite,";
-    } else {
-        os << "infinite,";
-    }
+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);
 
-    vp = *(persp->get_vanishing_point (Box3D::Z));
-    os << vp[NR::X] << "," << vp[NR::Y] << ",";
-    os << vp.v_dir[NR::X] << "," << vp.v_dir[NR::Y] << ",";
-    if (vp.is_finite()) {
-        os << "finite";
+    if (!persp3d_VP_is_finite(persp->perspective_impl, vpdir)) {
+        return 0;
     } else {
-        os << "infinite";
+        return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
     }
-
-    return g_strdup(os.str().c_str());
 }
 
-// auxiliary function
-static std::pair<NR::Point, NR::Point>
-sp_3dbox_new_midpoints (Box3D::Perspective3D *persp, Box3D::Axis axis, NR::Point const &M0, NR::Point const &M, NR::Point const &A, NR::Point const &B)
-{
-    double cr1 = Box3D::cross_ratio (*persp->get_vanishing_point (axis), M0, M, A);
-    double cr2 = Box3D::cross_ratio (*persp->get_vanishing_point (axis), M, B, M0);
-    if (fabs (cr1 - 1) < Box3D::epsilon) {
-        // FIXME: cr == 1 is a degenerate case; how should we deal with it?
-        return std::make_pair (NR::Point (0,0), NR::Point (0,0));
+/* swap the coordinates of corner0 and corner7 along the specified axis */
+static void
+box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
+    if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
+        double tmp = box->orig_corner0[axis];
+        box->orig_corner0[axis] = box->orig_corner7[axis];
+        box->orig_corner7[axis] = tmp;
     }
-    Box3D::PerspectiveLine pl (M0, axis, persp);
-    NR::Point B_new = pl.pt_with_given_cross_ratio (M0, M, cr1 / (cr1 - 1));
-    NR::Point A_new = pl.pt_with_given_cross_ratio (M0, M, 1 - cr2);
-    return std::make_pair (A_new, B_new);
+    // Should we also swap the coordinates of save_corner0 and save_corner7?
 }
 
-void sp_3dbox_recompute_Z_corners_from_new_center (SP3DBox *box, NR::Point const new_center)
-{
-    // TODO: Clean this function up
-
-    Box3D::Perspective3D *persp = sp_desktop_document (inkscape_active_desktop())->get_persp_of_box (box);
-    NR::Point old_center = box->old_center;
+/* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
+void
+box3d_relabel_corners(SPBox3D *box) {
+    box3d_swap_coords(box, Proj::X, false);
+    box3d_swap_coords(box, Proj::Y, false);
+    box3d_swap_coords(box, Proj::Z, true);
+}
 
-    Box3D::PerspectiveLine aux_line1 (old_center, Box3D::Z, persp);
-    Box3D::PerspectiveLine aux_line2 (new_center, Box3D::Y, persp);
-    NR::Point Z1 = aux_line1.meet (aux_line2);
+static void
+box3d_check_for_swapped_coords(SPBox3D *box, Proj::Axis axis, bool smaller) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
 
-    NR::Point A0 (sp_3dbox_get_midpoint_in_axis_direction (box->old_corner2, box->old_corner0, Box3D::Y, persp));
-    NR::Point B0 (sp_3dbox_get_midpoint_in_axis_direction (box->old_corner7, box->old_corner5, Box3D::Y, persp));
-    Box3D::PerspectiveLine aux_line3 (A0, Box3D::X, persp);
-    Box3D::PerspectiveLine aux_line4 (B0, Box3D::X, persp);
+    if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
+        box->swapped = (Box3D::Axis) (box->swapped | Proj::toAffine(axis));
+    } else {
+        box->swapped = (Box3D::Axis) (box->swapped & ~Proj::toAffine(axis));
+    }
+}
 
-    NR::Point C0 = aux_line3.meet (aux_line1);
-    NR::Point D0 = aux_line4.meet (aux_line1);
+static void
+box3d_exchange_coords(SPBox3D *box) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
 
-    std::pair<NR::Point, NR::Point> new_midpts = sp_3dbox_new_midpoints (persp, Box3D::Z, old_center, Z1, C0, D0);
-    NR::Point C1 (new_midpts.first);
-    NR::Point D1 (new_midpts.second);
-    Box3D::PerspectiveLine aux_line5 (C1, Box3D::X, persp);
-    Box3D::PerspectiveLine aux_line6 (D1, Box3D::X, persp);
+    for (int i = 0; i < 3; ++i) {
+        if (box->swapped & Box3D::axes[i]) {
+            double tmp = box->orig_corner0[i];
+            box->orig_corner0[i] = box->orig_corner7[i];
+            box->orig_corner7[i] = tmp;
+        }
+    }
+}
 
-    Box3D::PerspectiveLine aux_line7 (A0, Box3D::Z, persp);
-    Box3D::PerspectiveLine aux_line8 (B0, Box3D::Z, persp);
+void
+box3d_check_for_swapped_coords(SPBox3D *box) {
+    box3d_check_for_swapped_coords(box, Proj::X, false);
+    box3d_check_for_swapped_coords(box, Proj::Y, false);
+    box3d_check_for_swapped_coords(box, Proj::Z, true);
 
-    NR::Point A1 = aux_line5.meet (aux_line7);
-    NR::Point B1 = aux_line6.meet (aux_line8);
+    box3d_exchange_coords(box);
+}
 
-    Box3D::PerspectiveLine aux_line9  (box->old_corner2, Box3D::Z, persp);
-    Box3D::PerspectiveLine aux_line10 (box->old_corner5, Box3D::Z, persp);
+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 = obj->firstChild(); child; child = child->getNext() ) {
+            box3d_extract_boxes_rec(child, boxes);
+        }
+    }
+}
 
-    Box3D::PerspectiveLine aux_line11 (A1, Box3D::Y, persp);
-    Box3D::PerspectiveLine aux_line12 (B1, Box3D::Y, persp);
+std::list<SPBox3D *>
+box3d_extract_boxes(SPObject *obj) {
+    std::list<SPBox3D *> boxes;
+    box3d_extract_boxes_rec(obj, boxes);
+    return boxes;
+}
 
-    NR::Point new_corner2 = aux_line9.meet (aux_line11);
-    NR::Point new_corner5 = aux_line10.meet (aux_line12);
+Persp3D *
+box3d_get_perspective(SPBox3D const *box) {
+    return box->persp_ref->getObject();
+}
 
-    Box3D::PerspectiveLine aux_line13 (A1, Box3D::X, persp);
-    NR::Point E1 = aux_line13.meet (aux_line8);
-    Box3D::PerspectiveLine aux_line14 (E1, Box3D::Y, persp);
+void
+box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp, bool recompute_corners) {
+    if (recompute_corners) {
+        box->orig_corner0.normalize();
+        box->orig_corner7.normalize();
+        double z0 = box->orig_corner0[Proj::Z];
+        double z7 = box->orig_corner7[Proj::Z];
+        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->perspective_impl->tmat.preimage(corner0_screen, z0, Proj::Z);
+        box->orig_corner7 = new_persp->perspective_impl->tmat.preimage(corner7_screen, z7, Proj::Z);
+    }
 
-    NR::Point new_corner1 = aux_line10.meet (aux_line14);
+    persp3d_remove_box (old_persp, box);
+    persp3d_add_box (new_persp, box);
 
-    sp_3dbox_set_shape_from_points (box, new_corner2, new_corner1, new_corner5);
+    Glib::ustring href = "#";
+    href += new_persp->getId();
+    box->setAttribute("inkscape:perspectiveID", href.c_str());
 }
 
-void sp_3dbox_recompute_XY_corners_from_new_center (SP3DBox *box, NR::Point const new_center)
+/* 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)
 {
-    // TODO: Clean this function up
-
-    Box3D::Perspective3D *persp = sp_desktop_document (inkscape_active_desktop())->get_persp_of_box (box);
-    NR::Point old_center = box->old_center;
+    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
-    NR::Point A0 (sp_3dbox_get_midpoint_in_axis_direction (box->old_corner2, box->old_corner0, Box3D::Y, persp));
-    NR::Point B0 (sp_3dbox_get_midpoint_in_axis_direction (box->old_corner1, box->old_corner3, Box3D::Y, persp));
+    // remember position of the box
+    int pos = box->getPosition();
 
-    /* we first move the box along the X-axis ... */
-    Box3D::PerspectiveLine aux_line1 (old_center, Box3D::X, persp);
-    Box3D::PerspectiveLine aux_line2 (new_center, Box3D::Y, persp);
-    NR::Point Z1 = aux_line1.meet (aux_line2);
+    // remember important attributes
+    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");
 
-    Box3D::PerspectiveLine ref_line (B0, Box3D::X, persp);
-    Box3D::PerspectiveLine pline2 (old_center, Box3D::Z, persp);
-    Box3D::PerspectiveLine pline3 (Z1, Box3D::Z, persp);
-    NR::Point M0 = ref_line.meet (pline2);
-    NR::Point M1 = ref_line.meet (pline3);
+    // create a new group and add the sides (converted to ordinary paths) as its children
+    Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
 
-    std::pair<NR::Point, NR::Point> new_midpts = sp_3dbox_new_midpoints (persp, Box3D::X, M0, M1, A0, B0);
-    NR::Point A1 (new_midpts.first);
-    NR::Point B1 (new_midpts.second);
-
-    /* ... and then along the Y-axis */
-    Box3D::PerspectiveLine pline4 (box->old_corner1, Box3D::X, persp);
-    Box3D::PerspectiveLine pline5 (box->old_corner3, Box3D::X, persp);
-    Box3D::PerspectiveLine aux_line3 (M1, Box3D::Y, persp);
-    NR::Point C1 = aux_line3.meet (pline4);
-    NR::Point D1 = aux_line3.meet (pline5);
-
-    Box3D::PerspectiveLine aux_line4 (new_center, Box3D::Z, persp);
-    NR::Point M2 = aux_line4.meet (aux_line3);
-
-    std::pair<NR::Point, NR::Point> other_new_midpts = sp_3dbox_new_midpoints (persp, Box3D::Y, M1, M2, C1, D1);
-    NR::Point C2 (other_new_midpts.first);
-    NR::Point D2 (other_new_midpts.second);
-
-    Box3D::PerspectiveLine plXC (C2, Box3D::X, persp);
-    Box3D::PerspectiveLine plXD (D2, Box3D::X, persp);
-    Box3D::PerspectiveLine plYA (A1, Box3D::Y, persp);
-    Box3D::PerspectiveLine plYB (B1, Box3D::Y, persp);
+    for ( SPObject *child = box->firstChild(); child; child = child->getNext() ) {
+        if (SP_IS_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.");
+        }
+    }
 
-    NR::Point new_corner2 (plXD.meet (plYA));
-    NR::Point new_corner1 (plXC.meet (plYB));
+    // add the new group to the box's parent and set remembered position
+    SPObject *parent = SP_OBJECT_PARENT(box);
+    parent->appendChild(grepr);
+    grepr->setPosition(pos);
+    grepr->setAttribute("style", style);
+    if (mask)
+       grepr->setAttribute("mask", mask);
+    if (clip_path)
+       grepr->setAttribute("clip-path", clip_path);
 
-    NR::Point tmp_corner1 (pline4.meet (plYB));
-    Box3D::PerspectiveLine pline6 (box->old_corner5, Box3D::X, persp);
-    Box3D::PerspectiveLine pline7 (tmp_corner1, Box3D::Z, persp);
-    NR::Point tmp_corner5 (pline6.meet (pline7));
+    SP_OBJECT(box)->deleteObject(true);
 
-    Box3D::PerspectiveLine pline8 (tmp_corner5, Box3D::Y, persp);
-    Box3D::PerspectiveLine pline9 (new_corner1, Box3D::Z, persp);
-    NR::Point new_corner5 (pline8.meet (pline9));
+    grepr->setAttribute("id", id);
 
-    sp_3dbox_set_shape_from_points (box, new_corner2, new_corner1, new_corner5);
+    return SP_GROUP(doc->getObjectByRepr(grepr));
 }
 
-void sp_3dbox_update_perspective_lines()
-{
-    SPEventContext *ec = inkscape_active_event_context();
-    if (!SP_IS_3DBOX_CONTEXT (ec))
-        return;
-
-    SP_3DBOX_CONTEXT (ec)->_vpdrag->updateLines();
+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),
+                                 box3d_get_corner_screen(box, c2, false)));
 }
 
-/*
- * Manipulates corner1 through corner4 to contain the indices of the corners
- * from which the perspective lines in the direction of 'axis' emerge
- */
-void sp_3dbox_corners_for_perspective_lines (const SP3DBox * box, Box3D::Axis axis, 
-                                            NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
-{
-    // along which axis to switch when takint
-    Box3D::Axis switch_axis;
-    if (axis == Box3D::X || axis == Box3D::Y) {
-        switch_axis = (box->front_bits & axis) ? Box3D::Z : Box3D::NONE;
-    } else {
-        switch_axis = (box->front_bits & axis) ? Box3D::X : Box3D::NONE;
-    }
-
-    switch (axis) {
-        case Box3D::X:
-            corner1 = sp_3dbox_get_corner_along_edge (box, 0 ^ switch_axis, axis, Box3D::REAR);
-            corner2 = sp_3dbox_get_corner_along_edge (box, 2 ^ switch_axis, axis, Box3D::REAR);
-            corner3 = sp_3dbox_get_corner_along_edge (box, 4 ^ switch_axis, axis, Box3D::REAR);
-            corner4 = sp_3dbox_get_corner_along_edge (box, 6 ^ switch_axis, axis, Box3D::REAR);
-            break;
-        case Box3D::Y:
-            corner1 = sp_3dbox_get_corner_along_edge (box, 0 ^ switch_axis, axis, Box3D::REAR);
-            corner2 = sp_3dbox_get_corner_along_edge (box, 1 ^ switch_axis, axis, Box3D::REAR);
-            corner3 = sp_3dbox_get_corner_along_edge (box, 4 ^ switch_axis, axis, Box3D::REAR);
-            corner4 = sp_3dbox_get_corner_along_edge (box, 5 ^ switch_axis, axis, Box3D::REAR);
-            break;
-        case Box3D::Z:
-            corner1 = sp_3dbox_get_corner_along_edge (box, 1 ^ switch_axis, axis, Box3D::REAR);
-            corner2 = sp_3dbox_get_corner_along_edge (box, 3 ^ switch_axis, axis, Box3D::REAR);
-            corner3 = sp_3dbox_get_corner_along_edge (box, 0 ^ switch_axis, axis, Box3D::REAR);
-            corner4 = sp_3dbox_get_corner_along_edge (box, 2 ^ switch_axis, axis, Box3D::REAR);
-            break;
-        default:
-            // do nothing
-            break;
-    }            
-}
+void
+box3d_convert_to_guides(SPItem *item) {
+    SPBox3D *box = SP_BOX3D(item);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
-/**
- * Returns the id of the corner on the edge along 'axis' and passing through 'corner' that
- * lies on the front/rear face in this direction.
- */
-guint
-sp_3dbox_get_corner_id_along_edge (const SP3DBox *box, guint corner, Box3D::Axis axis, Box3D::FrontOrRear rel_pos)
-{
-    guint result;
-    guint other_corner = corner ^ axis;
-    Box3D::VanishingPoint *vp = SP_OBJECT_DOCUMENT (G_OBJECT (box))->get_persp_of_box (box)->get_vanishing_point(axis);
-    if (vp->is_finite()) {
-        result = (  NR::L2 (vp->get_pos() - box->corners[corner])
-                  < NR::L2 (vp->get_pos() - box->corners[other_corner]) ? other_corner : corner);
-    } else {
-        // clear the axis bit and switch to the appropriate corner along axis, depending on the value of front_bits
-        result = ((corner & (0xF ^ axis)) ^ (box->front_bits & axis));
+    if (!prefs->getBool("/tools/shapes/3dbox/convertguides", true)) {
+        box->convert_to_guides();
+        return;
     }
 
-    if (rel_pos == Box3D::FRONT) {
-        return result;
-    } else {
-        return result ^ axis;
-    }
-}
+    std::list<std::pair<Geom::Point, Geom::Point> > pts;
 
-NR::Point
-sp_3dbox_get_corner_along_edge (const SP3DBox *box, guint corner, Box3D::Axis axis, Box3D::FrontOrRear rel_pos)
-{
-    return box->corners[sp_3dbox_get_corner_id_along_edge (box, corner, axis, rel_pos)];
-}
+    /* perspective lines in X direction */
+    box3d_push_back_corner_pair(box, pts, 0, 1);
+    box3d_push_back_corner_pair(box, pts, 2, 3);
+    box3d_push_back_corner_pair(box, pts, 4, 5);
+    box3d_push_back_corner_pair(box, pts, 6, 7);
 
-guint
-sp_3dbox_get_front_corner_id (const SP3DBox *box)
-{
-    guint front_corner = 1; // this could in fact be any corner, but we choose the one that is normally in front
-    front_corner = sp_3dbox_get_corner_id_along_edge (box, front_corner, Box3D::X, Box3D::FRONT);
-    front_corner = sp_3dbox_get_corner_id_along_edge (box, front_corner, Box3D::Y, Box3D::FRONT);
-    front_corner = sp_3dbox_get_corner_id_along_edge (box, front_corner, Box3D::Z, Box3D::FRONT);
-    return front_corner;
-}
-
-// auxiliary functions
-static void
-sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value)
-{
-    if (value == NULL) return;
-    SP3DBox *box = SP_3DBOX(object);
-
-    std::pair<gdouble, gdouble> coord_pair = sp_3dbox_get_coord_pair_from_string (value);
-    box->corners[corner_id] = NR::Point (coord_pair.first, coord_pair.second);
-    sp_3dbox_recompute_corners (box, box->corners[2], box->corners[1], box->corners[5]);
-    object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-}
+    /* perspective lines in Y direction */
+    box3d_push_back_corner_pair(box, pts, 0, 2);
+    box3d_push_back_corner_pair(box, pts, 1, 3);
+    box3d_push_back_corner_pair(box, pts, 4, 6);
+    box3d_push_back_corner_pair(box, pts, 5, 7);
 
-static void
-sp_3dbox_update_perspective (Box3D::Perspective3D *persp, const gchar *value)
-{
-    // WARNING! This function changes the perspective associated to 'box'. Since there may be
-    // many other boxes linked to the same perspective, their perspective is also changed.
-    // If this behaviour is not desired in all cases, we need a different function.
-    if (value == NULL) return;
-
-    gchar **vps = g_strsplit( value, ",", 0);
-    for (int i = 0; i < 15; ++i) {
-        if (vps[i] == NULL) {
-            g_warning ("Malformed svg attribute 'perspective'\n");
-            return;
-        }
-    }
+    /* perspective lines in Z direction */
+    box3d_push_back_corner_pair(box, pts, 0, 4);
+    box3d_push_back_corner_pair(box, pts, 1, 5);
+    box3d_push_back_corner_pair(box, pts, 2, 6);
+    box3d_push_back_corner_pair(box, pts, 3, 7);
 
-    persp->set_vanishing_point (Box3D::X, g_ascii_strtod (vps[0], NULL), g_ascii_strtod (vps[1], NULL),
-                                          g_ascii_strtod (vps[2], NULL), g_ascii_strtod (vps[3], NULL),
-                                          strcmp (vps[4], "finite") == 0 ? Box3D::VP_FINITE : Box3D::VP_INFINITE);
-    persp->set_vanishing_point (Box3D::Y, g_ascii_strtod (vps[5], NULL), g_ascii_strtod (vps[6], NULL),
-                                          g_ascii_strtod (vps[7], NULL), g_ascii_strtod (vps[8], NULL),
-                                          strcmp (vps[9], "finite") == 0 ? Box3D::VP_FINITE : Box3D::VP_INFINITE);
-    persp->set_vanishing_point (Box3D::Z, g_ascii_strtod (vps[10], NULL), g_ascii_strtod (vps[11], NULL),
-                                          g_ascii_strtod (vps[12], NULL), g_ascii_strtod (vps[13], NULL),
-                                          strcmp (vps[14], "finite") == 0 ? Box3D::VP_FINITE : Box3D::VP_INFINITE);
-
-    // update the other boxes linked to the same perspective
-    persp->reshape_boxes (Box3D::XYZ);
+    sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
 }
 
 /*