Code

fix converting 3d boxes to path and ungrouping - do not lose selection, preserve...
[inkscape.git] / src / box3d.cpp
index c9f3bb7d296b63b2533f1cdad606838d2caf921e..5beee4accab9e3fb439ddb846bdadb88c99ea5fd 100644 (file)
@@ -32,6 +32,9 @@
 #include "persp3d-reference.h"
 #include "uri.h"
 #include "2geom/geom.h"
+#include "sp-guide.h"
+#include "sp-namedview.h"
+#include "prefs-utils.h"
 
 #include "desktop.h"
 #include "macros.h"
@@ -47,11 +50,9 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *r
 
 static gchar *box3d_description(SPItem *item);
 static NR::Matrix box3d_set_transform(SPItem *item, NR::Matrix const &xform);
+static void box3d_convert_to_guides(SPItem *item);
 
 static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
-static void box3d_ref_modified(SPObject *href, guint flags, SPBox3D *box);
-//static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, Persp3D *persp);
-//static void box3d_ref_modified(SPObject *href, guint flags, Persp3D *persp);
 
 static SPGroupClass *parent_class;
 
@@ -97,6 +98,7 @@ box3d_class_init(SPBox3DClass *klass)
 
     item_class->description = box3d_description;
     item_class->set_transform = box3d_set_transform;
+    item_class->convert_to_guides = box3d_convert_to_guides;
 }
 
 static void
@@ -104,7 +106,6 @@ box3d_init(SPBox3D *box)
 {
     box->persp_href = NULL;
     box->persp_ref = new Persp3DReference(SP_OBJECT(box));
-    new (&box->modified_connection) sigc::connection();
 }
 
 static void
@@ -129,12 +130,6 @@ box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
         g_print ("No document for the box!!!!\n");
         return;
     }
-    /**
-    if (!box->persp3d) {
-        g_print ("Box seems to be newly created since no perspective is referenced yet. We reference the current perspective.\n");
-        box->persp3d = doc->current_persp3d;
-    }
-    **/
 
     box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
 
@@ -160,10 +155,7 @@ box3d_release(SPObject *object)
         box->persp_ref = NULL;
     }
 
-    box->modified_connection.disconnect();
-    box->modified_connection.~connection();
-
-    //persp3d_remove_box (box->persp_ref->getObject(), box);
+    //persp3d_remove_box (box3d_get_perspective(box), box);
 
     if (((SPObjectClass *) parent_class)->release)
         ((SPObjectClass *) parent_class)->release(object);
@@ -196,14 +188,6 @@ box3d_set(SPObject *object, unsigned int key, const gchar *value)
                 } else {
                     // Detach, which emits the changed signal.
                     box->persp_ref->detach();
-                        // TODO: Clean this up (also w.r.t the surrounding if construct)
-                        /***
-                        g_print ("No perspective given. Attaching to current perspective instead.\n");
-                        g_free(box->persp_href);
-                        Inkscape::XML::Node *repr = SP_OBJECT_REPR(inkscape_active_document()->current_persp3d);
-                        box->persp_href = g_strdup(repr->attribute("id"));
-                        box->persp_ref->attach(Inkscape::URI(box->persp_href));
-                        ***/
                 }
             }
 
@@ -230,7 +214,6 @@ box3d_set(SPObject *object, unsigned int key, const gchar *value)
             }
             break;
     }
-    //object->updateRepr(); // This ensures correct update of the box after undo/redo. FIXME: Why is this not present in sp-rect.cpp and similar files?
 }
 
 /**
@@ -242,13 +225,16 @@ box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box)
     if (old_ref) {
         sp_signal_disconnect_by_data(old_ref, box);
         persp3d_remove_box (SP_PERSP3D(old_ref), box);
+        /* Note: This sometimes leads to attempts to remove boxes twice from the list of selected/transformed
+           boxes in a perspectives, but this should be uncritical. */
+        persp3d_remove_box_transform (SP_PERSP3D(old_ref), box);
     }
     if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
     {
-        box->modified_connection.disconnect();
-        box->modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&box3d_ref_modified), box));
-        box3d_ref_modified(ref, 0, box);
         persp3d_add_box (SP_PERSP3D(ref), box);
+        /* Note: This sometimes leads to attempts to add boxes twice to the list of selected/transformed
+           boxes in a perspectives, but this should be uncritical. */
+        persp3d_add_box_transform (SP_PERSP3D(ref), box);
     }
 }
 
@@ -274,10 +260,10 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *r
     SPBox3D *box = SP_BOX3D(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        g_print ("Do we ever end up here?\n");
+        // this is where we end up when saving as plain SVG (also in other circumstances?)
+        // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:g
         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:g");
-        repr->setAttribute("sodipodi:type", "inkscape:box3d");
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {
@@ -292,7 +278,6 @@ static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *r
                 repr->setAttribute("inkscape:perspectiveID", uri_string);
                 g_free(uri_string);
             } else if (doc) {
-                //persp3d_add_box (doc->current_persp3d, box);
                 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
                 const gchar *persp_id = persp_repr->attribute("id");
                 gchar *href = g_strdup_printf("#%s", persp_id);
@@ -347,12 +332,37 @@ box3d_set_transform(SPItem *item, NR::Matrix const &xform)
 {
     SPBox3D *box = SP_BOX3D(item);
 
-    Persp3D *persp = box->persp_ref->getObject();
+    /* check whether we need to unlink any boxes from their perspectives */
+    Persp3D *persp = box3d_get_perspective(box);
+    Persp3D *transf_persp;
+
+    if (!persp3d_has_all_boxes_in_selection (persp)) {
+        std::list<SPBox3D *> sel = persp3d_selected_boxes (persp);
+
+        /* create a new perspective as a copy of the current one and link the selected boxes to it */
+        transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
+
+        for (std::list<SPBox3D *>::iterator b = sel.begin(); b != sel.end(); ++b) {
+            box3d_switch_perspectives(*b, persp, transf_persp);
+        }
+    } else {
+        transf_persp = persp;
+    }
+
+    /* only transform the perspective once, even if it has several selected boxes */
+    if(!persp3d_was_transformed (transf_persp)) {
+        /* concatenate the affine transformation with the perspective mapping; this
+           function also triggers repr updates of boxes and the perspective itself */
+        persp3d_apply_affine_transformation(transf_persp, xform);
+    }
 
-    persp3d_apply_affine_transformation(persp, xform); // also triggers repr updates
+    box3d_mark_transformed(box);
+
+    if (persp3d_all_transformed(transf_persp)) {
+        /* all boxes were transformed; make perspective sensitive for further transformations */
+        persp3d_unset_transforms(transf_persp);
+    }
 
-    /***
-    // FIXME: We somehow have to apply the transformation to strokes, patterns, and gradients. How?
     NR::Matrix ret(NR::transform(xform));
     gdouble const sw = hypot(ret[0], ret[1]);
     gdouble const sh = hypot(ret[2], ret[3]);
@@ -369,27 +379,14 @@ box3d_set_transform(SPItem *item, NR::Matrix const &xform)
 
         // Adjust gradient fill
         sp_item_adjust_gradient(sideitem, xform);
+
+        // Adjust LPE
+        sp_item_adjust_livepatheffect(item, xform);
     }
-    ***/
 
     return NR::identity();
 }
 
-
-/**
- * Gets called when persp(?) repr contents change: i.e. parameter change.
- */
-static void
-box3d_ref_modified(SPObject *href, guint flags, SPBox3D *box)
-{
-    /***
-    g_print ("FIXME: box3d_ref_modified was called. What should we do?\n");
-    g_print ("Here is at least the the href's id: %s\n", SP_OBJECT_REPR(href)->attribute("id"));
-    g_print ("             ... and the box's, too: %s\n", SP_OBJECT_REPR(box)->attribute("id"));
-    ***/
-    
-}
-
 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],
@@ -409,11 +406,10 @@ box3d_get_proj_corner (SPBox3D const *box, guint id) {
 NR::Point
 box3d_get_corner_screen (SPBox3D const *box, guint id) {
     Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
-    if (!box->persp_ref->getObject()) {
-        //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
+    if (!box3d_get_perspective(box)) {
         return NR::Point (NR_HUGE, NR_HUGE);
     }
-    return box->persp_ref->getObject()->tmat.image(proj_corner).affine();
+    return box3d_get_perspective(box)->tmat.image(proj_corner).affine();
 }
 
 Proj::Pt3
@@ -429,14 +425,13 @@ box3d_get_proj_center (SPBox3D *box) {
 NR::Point
 box3d_get_center_screen (SPBox3D *box) {
     Proj::Pt3 proj_center (box3d_get_proj_center (box));
-    if (!box->persp_ref->getObject()) {
-        //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
+    if (!box3d_get_perspective(box)) {
         return NR::Point (NR_HUGE, NR_HUGE);
     }
-    return box->persp_ref->getObject()->tmat.image(proj_center).affine();
+    return box3d_get_perspective(box)->tmat.image(proj_center).affine();
 }
 
-/* 
+/*
  * 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.
@@ -444,8 +439,7 @@ box3d_get_center_screen (SPBox3D *box) {
 
 // Should we make the threshold settable in the preferences?
 static double remember_snap_threshold = 30;
-//static guint remember_snap_index = 0;
-static guint remember_snap_index_center = 0;
+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) {
@@ -460,12 +454,13 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
     Proj::Pt3 D_proj (x_coord,          y_coord + diff_y, z_coord, 1.0);
     Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
 
-    NR::Point A = box->persp_ref->getObject()->tmat.image(A_proj).affine();
-    NR::Point B = box->persp_ref->getObject()->tmat.image(B_proj).affine();
-    NR::Point C = box->persp_ref->getObject()->tmat.image(C_proj).affine();
-    NR::Point D = box->persp_ref->getObject()->tmat.image(D_proj).affine();
-    NR::Point E = box->persp_ref->getObject()->tmat.image(E_proj).affine();
-    NR::Point pt = box->persp_ref->getObject()->tmat.image(pt_proj).affine();
+    Persp3D *persp = box3d_get_perspective(box);
+    NR::Point A = persp->tmat.image(A_proj).affine();
+    NR::Point B = persp->tmat.image(B_proj).affine();
+    NR::Point C = persp->tmat.image(C_proj).affine();
+    NR::Point D = persp->tmat.image(D_proj).affine();
+    NR::Point E = persp->tmat.image(E_proj).affine();
+    NR::Point pt = persp->tmat.image(pt_proj).affine();
 
     // 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)
@@ -516,12 +511,12 @@ box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &sta
     // if we are within tolerance of the starting point)
     NR::Point result;
     if (within_tolerance) {
-        result = snap_pts[remember_snap_index_center];
+        result = snap_pts[remember_snap_index];
     } else {
-        remember_snap_index_center = snap_index;
+        remember_snap_index = snap_index;
         result = snap_pts[snap_index];
     }
-    return box->persp_ref->getObject()->tmat.preimage (result, z_coord, Proj::Z);
+    return box3d_get_perspective(box)->tmat.preimage (result, z_coord, Proj::Z);
 }
 
 void
@@ -533,9 +528,8 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
 
     /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
     if (!(movement & Box3D::Z)) {
-        Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
-                                                                                box->orig_corner7[Proj::Z],
-                                                            Proj::Z));
+        Proj::Pt3 pt_proj (box3d_get_perspective(box)->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
+                                                                      box->orig_corner7[Proj::Z], Proj::Z));
         if (constrained) {
             pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
         }
@@ -551,12 +545,12 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
                                        box->save_corner7[Proj::Z],
                                        1.0);
     } else {
-        Box3D::PerspectiveLine pl(box->persp_ref->getObject()->tmat.image(
+        Persp3D *persp = box3d_get_perspective(box);
+        Box3D::PerspectiveLine pl(persp->tmat.image(
                                       box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
-                                  Proj::Z, box->persp_ref->getObject());
+                                  Proj::Z, persp);
         NR::Point new_pos_snapped(pl.closest_to(new_pos));
-        Proj::Pt3 pt_proj (box->persp_ref->getObject()->
-                           tmat.preimage (new_pos_snapped,
+        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped,
                                           box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
                                           (movement & Box3D::Y) ? Proj::X : Proj::Y));
         bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
@@ -564,7 +558,7 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
         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();        
+        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],
@@ -580,18 +574,23 @@ box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const
 void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_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();
+
+    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;
 
-        Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, coord, Proj::Z));
+        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos, coord, Proj::Z));
         if (constrained) {
-            Proj::Pt3 old_pos_proj (box->persp_ref->getObject()->tmat.preimage (old_pos, coord, Proj::Z));
+            Proj::Pt3 old_pos_proj (persp->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();        
+        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],
@@ -604,12 +603,12 @@ void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &
         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, box->persp_ref->getObject());
+        Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
         NR::Point new_pos_snapped(pl.closest_to(new_pos));
-        Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos_snapped, coord, Proj::X));
+        Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped, coord, Proj::X));
 
         /* normalizing pt_proj is essential because we want to mingle affine coordinates */
-        pt_proj.normalize();        
+        pt_proj.normalize();
         box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
                                        box->orig_corner0[Proj::Y],
                                        pt_proj[Proj::Z] - radz,
@@ -625,10 +624,11 @@ void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &
  * Manipulates corner1 through corner4 to contain the indices of the corners
  * from which the perspective lines in the direction of 'axis' emerge
  */
-void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis, 
+void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
                             NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
 {
-    g_return_if_fail (box->persp_ref->getObject());
+    Persp3D *persp = box3d_get_perspective(box);
+    g_return_if_fail (persp);
     //box->orig_corner0.normalize();
     //box->orig_corner7.normalize();
     double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
@@ -659,10 +659,10 @@ void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
         default:
             return;
     }
-    corner1 = box->persp_ref->getObject()->tmat.image(c1).affine();
-    corner2 = box->persp_ref->getObject()->tmat.image(c2).affine();
-    corner3 = box->persp_ref->getObject()->tmat.image(c3).affine();
-    corner4 = box->persp_ref->getObject()->tmat.image(c4).affine();
+    corner1 = persp->tmat.image(c1).affine();
+    corner2 = persp->tmat.image(c2).affine();
+    corner3 = persp->tmat.image(c3).affine();
+    corner4 = persp->tmat.image(c4).affine();
 }
 
 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
@@ -698,7 +698,7 @@ box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B
 
 static bool
 box3d_XY_axes_are_swapped (SPBox3D *box) {
-    Persp3D *persp = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
     g_return_val_if_fail(persp, false);
     Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3), Proj::X, persp);
     Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3), Proj::Y, persp);
@@ -731,60 +731,51 @@ box3d_swap_z_orders (int z_orders[6]) {
 }
 
 /*
- * In der Standard-Perspektive:
- * 2 = vorne
- * 1 = oben
- * 0 = links
- * 3 = rechts
- * 4 = unten
- * 5 = hinten
+ * In standard perspective we have:
+ * 2 = front face
+ * 1 = top face
+ * 0 = left face
+ * 3 = right face
+ * 4 = bottom face
+ * 5 = rear face
  */
 
 /* All VPs infinite */
 static void
 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
-    Persp3D *persp = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
     NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
     NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
     NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
 
     bool swapped = box3d_XY_axes_are_swapped(box);
 
-    //g_print ("3 infinite VPs; ");
     switch(central_axis) {
         case Box3D::X:
             if (!swapped) {
-                //g_print ("central axis X (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
             } else {
-                //g_print ("central axis X (case b)");
                 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
             }
             break;
         case Box3D::Y:
             if (!swapped) {
-                //g_print ("central axis Y (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
             } else {
-                //g_print ("central axis Y (case b)");
                 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
             }
             break;
         case Box3D::Z:
             if (!swapped) {
-                //g_print ("central axis Z (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
             } else {
-                //g_print ("central axis Z (case b)");
                 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
             }
             break;
         case Box3D::NONE:
             if (!swapped) {
-                //g_print ("central axis NONE (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
             } else {
-                //g_print ("central axis NONE (case b)");
                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
             }
             break;
@@ -792,62 +783,41 @@ box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central
             g_assert_not_reached();
             break;
     }
-    /**
-    if (swapped) {
-        g_print ("; swapped");
-    }
-    g_print ("\n");
-    **/
 }
 
 /* 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 = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
     NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
 
     // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
     Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
     Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
-    //g_print ("oaxis1  = %s, oaxis2  = %s\n", Box3D::string_from_axes(oaxis1), Box3D::string_from_axes(oaxis2));
     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);
-    //g_print ("inside1 = %d, inside2 = %d\n", inside1, inside2);
 
     bool swapped = box3d_XY_axes_are_swapped(box);
 
-    //g_print ("2 infinite VPs; ");
-    //g_print ("finite axis: %s; ", Box3D::string_from_axes(fin_axis));
     switch(central_axis) {
         case Box3D::X:
             if (!swapped) {
-                //g_print ("central axis X (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
             } else {
-                //if (inside2) {
-                    //g_print ("central axis X (case b)");
-                    box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
-                //} else {
-                    //g_print ("central axis X (case c)");
-                    //box3d_aux_set_z_orders (z_orders, 5, 3, 1, 2, 0, 4);
-                //}
+                box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
             }
             break;
         case Box3D::Y:
             if (inside2 > 0) {
-                //g_print ("central axis Y (case a)");
                 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
             } else if (inside2 < 0) {
-                //g_print ("central axis Y (case b)");
                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
             } else {
                 if (!swapped) {
-                    //g_print ("central axis Y (case c1)");
                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
                 } else {
-                    //g_print ("central axis Y (case c2)");
                     box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
                 }
             }
@@ -855,58 +825,41 @@ box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central
         case Box3D::Z:
             if (inside2) {
                 if (!swapped) {
-                    //g_print ("central axis Z (case a1)");
                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
                 } else {
-                    //g_print ("central axis Z (case a2)");
                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
                 }
             } else if (inside1) {
                 if (!swapped) {
-                    //g_print ("central axis Z (case b1)");
                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
                 } else {
-                    //g_print ("central axis Z (case b2)");
                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
-                    //box3d_aux_set_z_orders (z_orders, 5, 3, 0, 1, 2, 4);
-                }                    
+                }
             } else {
                 // "regular" case
                 if (!swapped) {
-                    //g_print ("central axis Z (case c1)");
                     box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
                 } else {
-                    //g_print ("central axis Z (case c2)");
                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
-                    //box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
                 }
             }
             break;
         case Box3D::NONE:
             if (!swapped) {
-                //g_print ("central axis NONE (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
             } else {
-                //g_print ("central axis NONE (case b)");
                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
-                //box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
             }
             break;
         default:
             g_assert_not_reached();
     }
-    /**
-    if (swapped) {
-        g_print ("; swapped");
-    }
-    g_print ("\n");
-    **/
 }
 
 /* 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 = box->persp_ref->getObject();
+box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
+    Persp3D *persp = box3d_get_perspective(box);
 
     NR::Point c3(box3d_get_corner_screen(box, 3));
     NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
@@ -916,42 +869,36 @@ box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central
     bool swapped = box3d_XY_axes_are_swapped(box);
 
     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);
+    //int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
 
     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);
 
-    int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
+    //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);
 
-    //g_print ("Insides: xy = %d, xz = %d, yx = %d, yz = %d, zx = %d, zy = %d\n",
-    //         insidexy, insidexz, insideyx, insideyz, insidezx, insidezy);
-
-    //g_print ("1 infinite VP; ");
     switch(central_axis) {
         case Box3D::X:
             if (!swapped) {
                 if (insidezy == -1) {
-                    //g_print ("central axis X (case a1)");
                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
                 } else if (insidexy == 1) {
-                    //g_print ("central axis X (case a2)");
                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
                 } else {
-                    //g_print ("central axis X (case a3)");
                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
                 }
             } else {
                 if (insideyz == -1) {
-                    //g_print ("central axis X (case b1)");
                     box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
                 } else {
                     if (!swapped) {
-                        //g_print ("central axis X (case b2)");
                         box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
                     } else {
-                        //g_print ("central axis X (case b3)");
-                        box3d_aux_set_z_orders (z_orders, 1, 3, 5, 0, 2, 4);
+                        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);
+                        }
                     }
                 }
             }
@@ -959,40 +906,35 @@ box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central
         case Box3D::Y:
             if (!swapped) {
                 if (insideyz == 1) {
-                    //g_print ("central axis Y (case a1)");
                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
                 } else {
-                    //g_print ("central axis Y (case a2)");
                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
                 }
             } else {
-                //g_print ("central axis Y (case b)");
-                box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
+                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) {
-                    //g_print ("central axis Z (case a1)");
                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
                 } else if (insidexy == -1) {
-                    //g_print ("central axis Z (case a2)");
                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
                 } else {
-                    //g_print ("central axis Z (case a3)");
                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
                 }
             } else {
-                //g_print ("central axis Z (case b)");
-                box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
+                box3d_aux_set_z_orders (z_orders, 3, 4, 5, 1, 0, 2);
             }
             break;
         case Box3D::NONE:
             if (!swapped) {
-                //g_print ("central axis NONE (case a)");
                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
             } else {
-                //g_print ("central axis NONE (case b)");
                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
             }
             break;
@@ -1000,12 +942,6 @@ box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central
             g_assert_not_reached();
             break;
     }
-    /**
-    if (swapped) {
-        g_print ("; swapped");
-    }
-    g_print ("\n");
-    **/
 }
 
 /*
@@ -1053,9 +989,8 @@ box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
 
 bool
 box3d_recompute_z_orders (SPBox3D *box) {
-    Persp3D *persp = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
 
-    //g_return_val_if_fail(persp, false);
     if (!persp)
         return false;
 
@@ -1126,14 +1061,12 @@ box3d_recompute_z_orders (SPBox3D *box) {
         } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
             central_axis = Box3D::Z;
         }
-        //g_print ("Crossing: %s\n", Box3D::string_from_axes(central_axis));
 
         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)) {
-            //g_print ("Axes X and Y are swapped\n");
             central_corner = central_corner ^ Box3D::XYZ;
         }
 
@@ -1151,7 +1084,6 @@ box3d_recompute_z_orders (SPBox3D *box) {
                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
                 } else {
                     // degenerate case
-                    //g_print ("Degenerate case #1\n");
                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
                 }
                 break;
@@ -1159,11 +1091,9 @@ box3d_recompute_z_orders (SPBox3D *box) {
             case Box3D::Z:
                 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
                     // degenerate case
-                    //g_print ("Degenerate case #2\n");
                     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
-                    //g_print ("Degenerate case #3\n");
                     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);
@@ -1173,7 +1103,6 @@ box3d_recompute_z_orders (SPBox3D *box) {
             case Box3D::X:
                 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
                     // degenerate case
-                    //g_print ("Degenerate case #4\n");
                     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);
@@ -1236,13 +1165,6 @@ box3d_set_z_orders (SPBox3D *box) {
                 SP_ITEM((*side).second)->lowerToBottom();
             }
         }
-        /**
-        g_print ("Resetting z-orders: ");
-        for (int i = 0; i < 6; ++i) {
-            g_print ("%d ", box->z_orders[i]);
-        }
-        g_print ("\n");
-        **/
     }
 }
 
@@ -1260,7 +1182,7 @@ box3d_set_z_orders (SPBox3D *box) {
 //       can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
 int
 box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
-    Persp3D *persp = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
 
     // the two corners
     NR::Point c1(box3d_get_corner_screen(box, id1));
@@ -1273,7 +1195,6 @@ box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, in
         NR::Point v2(c2 - vp);
         NR::Point w(pt - vp);
         ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
-        //g_print ("Case 0 - returning %d\n", ret);
     } else {
         Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
         Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
@@ -1287,14 +1208,13 @@ box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, in
                 ret = -1;
             }
         }
-        //g_print ("Case 1 - returning %d\n", ret);
     }
     return ret;
 }
 
 int
 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
-    Persp3D *persp = box->persp_ref->getObject();
+    Persp3D *persp = box3d_get_perspective(box);
 
     if (!persp3d_VP_is_finite(persp, vpdir)) {
         return 0;
@@ -1313,7 +1233,7 @@ box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
         box->orig_corner0[axis] = box->orig_corner7[axis];
         box->orig_corner7[axis] = tmp;
     }
-    // FIXME: Should we also swap the coordinates of save_corner0 and save_corner7?
+    // Should we also swap the coordinates of save_corner0 and save_corner7?
 }
 
 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
@@ -1324,6 +1244,183 @@ box3d_relabel_corners(SPBox3D *box) {
     box3d_swap_coords(box, Proj::Z, true);
 }
 
+static void
+box3d_check_for_swapped_coords(SPBox3D *box, Proj::Axis axis, bool smaller) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
+
+    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));
+    }
+}
+
+static void
+box3d_exchange_coords(SPBox3D *box) {
+    box->orig_corner0.normalize();
+    box->orig_corner7.normalize();
+
+    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;
+        }
+    }
+}
+
+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);
+
+    box3d_exchange_coords(box);
+}
+
+void
+box3d_add_to_selection(SPBox3D *box) {
+    Persp3D *persp = box3d_get_perspective(box);
+    g_return_if_fail(persp);
+    persp3d_add_box_transform(persp, box);
+}
+
+void
+box3d_remove_from_selection(SPBox3D *box) {
+    Persp3D *persp = box3d_get_perspective(box);
+    if (!persp) {
+        /* this can happen if a box is deleted through undo and the persp_ref is already detached;
+           should we rebuild the boxes of each perspective in this case or is it safe to leave it alone? */
+        return;
+    }
+    persp3d_remove_box_transform(persp, box);
+}
+
+void
+box3d_mark_transformed(SPBox3D *box) {
+    Persp3D *persp = box3d_get_perspective(box);
+    g_return_if_fail(persp);
+    persp3d_set_box_transformed(persp, box, true);
+}
+
+Persp3D *
+box3d_get_perspective(SPBox3D const *box) {
+    return box->persp_ref->getObject();
+}
+
+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];
+        NR::Point corner0_screen = box3d_get_corner_screen(box, 0);
+        NR::Point corner7_screen = box3d_get_corner_screen(box, 7);
+
+        box->orig_corner0 = new_persp->tmat.preimage(corner0_screen, z0, Proj::Z);
+        box->orig_corner7 = new_persp->tmat.preimage(corner7_screen, z7, Proj::Z);
+    }
+
+    persp3d_remove_box (old_persp, box);
+    persp3d_add_box (new_persp, box);
+
+    persp3d_remove_box_transform (old_persp, box);
+    persp3d_add_box_transform (new_persp, box);
+
+    gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
+    SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
+    g_free(href);
+}
+
+/* Converts the 3D box to an ordinary SPGroup, adds it to the XML tree at the same position as
+   the original box and deletes the latter */
+Inkscape::XML::Node *
+box3d_convert_to_group(SPBox3D *box) {
+    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+
+    // remember position of the box
+    int pos = SP_OBJECT_REPR(box)->position();
+
+    // remember important attributes
+    Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(box);
+    gchar const *id = repr_source->attribute("id");
+    gchar const *style = repr_source->attribute("style");
+    gchar const *mask = repr_source->attribute("mask");
+    gchar const *clip_path = repr_source->attribute("clip-path");
+
+    // create a new group and add the sides (converted to ordinary paths) as its children
+    Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
+
+    Inkscape::XML::Node *repr;
+    for (SPObject *child = sp_object_first_child(SP_OBJECT(box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
+        if (SP_IS_BOX3D_SIDE(child)) {
+            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.");
+        }
+    }
+
+    // add the new group to the box's parent and set remembered position
+    SPObject *parent = SP_OBJECT_PARENT(box);
+    SP_OBJECT_REPR(parent)->appendChild(grepr);
+    grepr->setPosition(pos);
+    grepr->setAttribute("style", style);
+    if (mask)
+       grepr->setAttribute("mask", mask);
+    if (clip_path)
+       grepr->setAttribute("clip-path", clip_path);
+
+    SP_OBJECT(box)->deleteObject(true);
+
+    grepr->setAttribute("id", id);
+
+    return grepr;
+}
+
+static inline void
+box3d_push_back_corner_pair(SPBox3D *box, std::list<std::pair<Geom::Point, Geom::Point> > &pts, int c1, int c2) {
+    pts.push_back(std::make_pair(box3d_get_corner_screen(box, c1).to_2geom(),
+                                 box3d_get_corner_screen(box, c2).to_2geom()));
+}
+
+void
+box3d_convert_to_guides(SPItem *item) {
+    SPBox3D *box = SP_BOX3D(item);
+
+    if (prefs_get_int_attribute("tools.shapes.3dbox", "convertguides", 1) == 0) {
+        sp_item_convert_to_guides(SP_ITEM(box));
+        return;
+    }
+
+    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+
+    std::list<std::pair<Geom::Point, Geom::Point> > pts;
+
+    /* 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);
+
+    /* 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);
+
+    /* 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);
+
+    sp_guide_pt_pairs_to_guides(doc, pts);
+}
+
 /*
   Local Variables:
   mode:c++