Code

c9f3bb7d296b63b2533f1cdad606838d2caf921e
[inkscape.git] / src / box3d.cpp
1 #define __SP_BOX3D_C__
3 /*
4  * SVG <box3d> implementation
5  *
6  * Authors:
7  *   Maximilian Albert <Anhalter42@gmx.de>
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 2007      Authors
12  * Copyright (C) 1999-2002 Lauris Kaplinski
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <glibmm/i18n.h>
19 #include "attributes.h"
20 #include "xml/document.h"
21 #include "xml/repr.h"
23 #include "box3d.h"
24 #include "box3d-side.h"
25 #include "box3d-context.h"
26 #include "proj_pt.h"
27 #include "transf_mat_3x4.h"
28 #include "perspective-line.h"
29 #include "inkscape.h"
30 #include "persp3d.h"
31 #include "line-geometry.h"
32 #include "persp3d-reference.h"
33 #include "uri.h"
34 #include "2geom/geom.h"
36 #include "desktop.h"
37 #include "macros.h"
39 static void box3d_class_init(SPBox3DClass *klass);
40 static void box3d_init(SPBox3D *box3d);
42 static void box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
43 static void box3d_release(SPObject *object);
44 static void box3d_set(SPObject *object, unsigned int key, const gchar *value);
45 static void box3d_update(SPObject *object, SPCtx *ctx, guint flags);
46 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
48 static gchar *box3d_description(SPItem *item);
49 static NR::Matrix box3d_set_transform(SPItem *item, NR::Matrix const &xform);
51 static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
52 static void box3d_ref_modified(SPObject *href, guint flags, SPBox3D *box);
53 //static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, Persp3D *persp);
54 //static void box3d_ref_modified(SPObject *href, guint flags, Persp3D *persp);
56 static SPGroupClass *parent_class;
58 static gint counter = 0;
60 GType
61 box3d_get_type(void)
62 {
63     static GType type = 0;
65     if (!type) {
66         GTypeInfo info = {
67             sizeof(SPBox3DClass),
68             NULL,   /* base_init */
69             NULL,   /* base_finalize */
70             (GClassInitFunc) box3d_class_init,
71             NULL,   /* class_finalize */
72             NULL,   /* class_data */
73             sizeof(SPBox3D),
74             16,     /* n_preallocs */
75             (GInstanceInitFunc) box3d_init,
76             NULL,   /* value_table */
77         };
78         type = g_type_register_static(SP_TYPE_GROUP, "SPBox3D", &info, (GTypeFlags) 0);
79     }
81     return type;
82 }
84 static void
85 box3d_class_init(SPBox3DClass *klass)
86 {
87     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
88     SPItemClass *item_class = (SPItemClass *) klass;
90     parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
92     sp_object_class->build = box3d_build;
93     sp_object_class->release = box3d_release;
94     sp_object_class->set = box3d_set;
95     sp_object_class->write = box3d_write;
96     sp_object_class->update = box3d_update;
98     item_class->description = box3d_description;
99     item_class->set_transform = box3d_set_transform;
102 static void
103 box3d_init(SPBox3D *box)
105     box->persp_href = NULL;
106     box->persp_ref = new Persp3DReference(SP_OBJECT(box));
107     new (&box->modified_connection) sigc::connection();
110 static void
111 box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
113     if (((SPObjectClass *) (parent_class))->build) {
114         ((SPObjectClass *) (parent_class))->build(object, document, repr);
115     }
117     SPBox3D *box = SP_BOX3D (object);
118     box->my_counter = counter++;
120     /* we initialize the z-orders to zero so that they are updated during dragging */
121     for (int i = 0; i < 6; ++i) {
122         box->z_orders[i] = 0;
123     }
125     // TODO: Create/link to the correct perspective
127     SPDocument *doc = SP_OBJECT_DOCUMENT(box);
128     if (!doc) {
129         g_print ("No document for the box!!!!\n");
130         return;
131     }
132     /**
133     if (!box->persp3d) {
134         g_print ("Box seems to be newly created since no perspective is referenced yet. We reference the current perspective.\n");
135         box->persp3d = doc->current_persp3d;
136     }
137     **/
139     box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
141     sp_object_read_attr(object, "inkscape:perspectiveID");
142     sp_object_read_attr(object, "inkscape:corner0");
143     sp_object_read_attr(object, "inkscape:corner7");
146 /**
147  * Virtual release of SPBox3D members before destruction.
148  */
149 static void
150 box3d_release(SPObject *object)
152     SPBox3D *box = (SPBox3D *) object;
154     if (box->persp_href) {
155         g_free(box->persp_href);
156     }
157     if (box->persp_ref) {
158         box->persp_ref->detach();
159         delete box->persp_ref;
160         box->persp_ref = NULL;
161     }
163     box->modified_connection.disconnect();
164     box->modified_connection.~connection();
166     //persp3d_remove_box (box->persp_ref->getObject(), box);
168     if (((SPObjectClass *) parent_class)->release)
169         ((SPObjectClass *) parent_class)->release(object);
172 static void
173 box3d_set(SPObject *object, unsigned int key, const gchar *value)
175     SPBox3D *box = SP_BOX3D(object);
177     switch (key) {
178         case SP_ATTR_INKSCAPE_BOX3D_PERSPECTIVE_ID:
179             if ( value && box->persp_href && ( strcmp(value, box->persp_href) == 0 ) ) {
180                 /* No change, do nothing. */
181             } else {
182                 if (box->persp_href) {
183                     g_free(box->persp_href);
184                     box->persp_href = NULL;
185                 }
186                 if (value) {
187                     box->persp_href = g_strdup(value);
189                     // Now do the attaching, which emits the changed signal.
190                     try {
191                         box->persp_ref->attach(Inkscape::URI(value));
192                     } catch (Inkscape::BadURIException &e) {
193                         g_warning("%s", e.what());
194                         box->persp_ref->detach();
195                     }
196                 } else {
197                     // Detach, which emits the changed signal.
198                     box->persp_ref->detach();
199                         // TODO: Clean this up (also w.r.t the surrounding if construct)
200                         /***
201                         g_print ("No perspective given. Attaching to current perspective instead.\n");
202                         g_free(box->persp_href);
203                         Inkscape::XML::Node *repr = SP_OBJECT_REPR(inkscape_active_document()->current_persp3d);
204                         box->persp_href = g_strdup(repr->attribute("id"));
205                         box->persp_ref->attach(Inkscape::URI(box->persp_href));
206                         ***/
207                 }
208             }
210             // FIXME: Is the following update doubled by some call in either persp3d.cpp or vanishing_point_new.cpp?
211             box3d_position_set(box);
212             break;
213         case SP_ATTR_INKSCAPE_BOX3D_CORNER0:
214             if (value && strcmp(value, "0 : 0 : 0 : 0")) {
215                 box->orig_corner0 = Proj::Pt3(value);
216                 box->save_corner0 = box->orig_corner0;
217                 box3d_position_set(box);
218             }
219             break;
220         case SP_ATTR_INKSCAPE_BOX3D_CORNER7:
221             if (value && strcmp(value, "0 : 0 : 0 : 0")) {
222                 box->orig_corner7 = Proj::Pt3(value);
223                 box->save_corner7 = box->orig_corner7;
224                 box3d_position_set(box);
225             }
226             break;
227         default:
228             if (((SPObjectClass *) (parent_class))->set) {
229                 ((SPObjectClass *) (parent_class))->set(object, key, value);
230             }
231             break;
232     }
233     //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?
236 /**
237  * Gets called when (re)attached to another perspective.
238  */
239 static void
240 box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box)
242     if (old_ref) {
243         sp_signal_disconnect_by_data(old_ref, box);
244         persp3d_remove_box (SP_PERSP3D(old_ref), box);
245     }
246     if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
247     {
248         box->modified_connection.disconnect();
249         box->modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&box3d_ref_modified), box));
250         box3d_ref_modified(ref, 0, box);
251         persp3d_add_box (SP_PERSP3D(ref), box);
252     }
255 static void
256 box3d_update(SPObject *object, SPCtx *ctx, guint flags)
258     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
260         /* FIXME?: Perhaps the display updates of box sides should be instantiated from here, but this
261            causes evil update loops so it's all done from box3d_position_set, which is called from
262            various other places (like the handlers in object-edit.cpp, vanishing-point.cpp, etc. */
264     }
266     // Invoke parent method
267     if (((SPObjectClass *) (parent_class))->update)
268         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
272 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
274     SPBox3D *box = SP_BOX3D(object);
276     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
277         g_print ("Do we ever end up here?\n");
278         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
279         repr = xml_doc->createElement("svg:g");
280         repr->setAttribute("sodipodi:type", "inkscape:box3d");
281     }
283     if (flags & SP_OBJECT_WRITE_EXT) {
285         if (box->persp_href) {
286             repr->setAttribute("inkscape:perspectiveID", box->persp_href);
287         } else {
288             /* box is not yet linked to a perspective; use the document's current perspective */
289             SPDocument *doc = inkscape_active_document();
290             if (box->persp_ref->getURI()) {
291                 gchar *uri_string = box->persp_ref->getURI()->toString();
292                 repr->setAttribute("inkscape:perspectiveID", uri_string);
293                 g_free(uri_string);
294             } else if (doc) {
295                 //persp3d_add_box (doc->current_persp3d, box);
296                 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
297                 const gchar *persp_id = persp_repr->attribute("id");
298                 gchar *href = g_strdup_printf("#%s", persp_id);
299                 repr->setAttribute("inkscape:perspectiveID", href);
300                 g_free(href);
301             } else {
302                 g_print ("No active document while creating perspective!!!\n");
303             }
304         }
306         gchar *coordstr0 = box->orig_corner0.coord_string();
307         gchar *coordstr7 = box->orig_corner7.coord_string();
308         repr->setAttribute("inkscape:corner0", coordstr0);
309         repr->setAttribute("inkscape:corner7", coordstr7);
310         g_free(coordstr0);
311         g_free(coordstr7);
313         box->orig_corner0.normalize();
314         box->orig_corner7.normalize();
316         box->save_corner0 = box->orig_corner0;
317         box->save_corner7 = box->orig_corner7;
318     }
320     if (((SPObjectClass *) (parent_class))->write) {
321         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
322     }
324     return repr;
327 static gchar *
328 box3d_description(SPItem *item)
330     g_return_val_if_fail(SP_IS_BOX3D(item), NULL);
332     return g_strdup(_("<b>3D Box</b>"));
335 void
336 box3d_position_set (SPBox3D *box)
338     /* This draws the curve and calls requestDisplayUpdate() for each side (the latter is done in
339        box3d_side_position_set() to avoid update conflicts with the parent box) */
340     for (SPObject *child = sp_object_first_child(SP_OBJECT (box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
341         box3d_side_position_set (SP_BOX3D_SIDE (child));
342     }
345 static NR::Matrix
346 box3d_set_transform(SPItem *item, NR::Matrix const &xform)
348     SPBox3D *box = SP_BOX3D(item);
350     Persp3D *persp = box->persp_ref->getObject();
352     persp3d_apply_affine_transformation(persp, xform); // also triggers repr updates
354     /***
355     // FIXME: We somehow have to apply the transformation to strokes, patterns, and gradients. How?
356     NR::Matrix ret(NR::transform(xform));
357     gdouble const sw = hypot(ret[0], ret[1]);
358     gdouble const sh = hypot(ret[2], ret[3]);
360     SPItem *sideitem = NULL;
361     for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
362         sideitem = SP_ITEM(side);
364         // Adjust stroke width
365         sp_item_adjust_stroke(sideitem, sqrt(fabs(sw * sh)));
367         // Adjust pattern fill
368         sp_item_adjust_pattern(sideitem, xform);
370         // Adjust gradient fill
371         sp_item_adjust_gradient(sideitem, xform);
372     }
373     ***/
375     return NR::identity();
379 /**
380  * Gets called when persp(?) repr contents change: i.e. parameter change.
381  */
382 static void
383 box3d_ref_modified(SPObject *href, guint flags, SPBox3D *box)
385     /***
386     g_print ("FIXME: box3d_ref_modified was called. What should we do?\n");
387     g_print ("Here is at least the the href's id: %s\n", SP_OBJECT_REPR(href)->attribute("id"));
388     g_print ("             ... and the box's, too: %s\n", SP_OBJECT_REPR(box)->attribute("id"));
389     ***/
390     
393 Proj::Pt3
394 box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
395     return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
396                       (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
397                       (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
398                       1.0);
401 Proj::Pt3
402 box3d_get_proj_corner (SPBox3D const *box, guint id) {
403     return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
404                       (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
405                       (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
406                       1.0);
409 NR::Point
410 box3d_get_corner_screen (SPBox3D const *box, guint id) {
411     Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
412     if (!box->persp_ref->getObject()) {
413         //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
414         return NR::Point (NR_HUGE, NR_HUGE);
415     }
416     return box->persp_ref->getObject()->tmat.image(proj_corner).affine();
419 Proj::Pt3
420 box3d_get_proj_center (SPBox3D *box) {
421     box->orig_corner0.normalize();
422     box->orig_corner7.normalize();
423     return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
424                       (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
425                       (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
426                       1.0);
429 NR::Point
430 box3d_get_center_screen (SPBox3D *box) {
431     Proj::Pt3 proj_center (box3d_get_proj_center (box));
432     if (!box->persp_ref->getObject()) {
433         //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
434         return NR::Point (NR_HUGE, NR_HUGE);
435     }
436     return box->persp_ref->getObject()->tmat.image(proj_center).affine();
439 /* 
440  * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
441  * their intersection, we remember the last snapped line and keep snapping to this specific line as long
442  * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
443  */
445 // Should we make the threshold settable in the preferences?
446 static double remember_snap_threshold = 30;
447 //static guint remember_snap_index = 0;
448 static guint remember_snap_index_center = 0;
450 static Proj::Pt3
451 box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
452     double z_coord = start_pt[Proj::Z];
453     double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
454     double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
455     double x_coord = start_pt[Proj::X];
456     double y_coord = start_pt[Proj::Y];
457     Proj::Pt3 A_proj (x_coord,          y_coord,          z_coord, 1.0);
458     Proj::Pt3 B_proj (x_coord + diff_x, y_coord,          z_coord, 1.0);
459     Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
460     Proj::Pt3 D_proj (x_coord,          y_coord + diff_y, z_coord, 1.0);
461     Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
463     NR::Point A = box->persp_ref->getObject()->tmat.image(A_proj).affine();
464     NR::Point B = box->persp_ref->getObject()->tmat.image(B_proj).affine();
465     NR::Point C = box->persp_ref->getObject()->tmat.image(C_proj).affine();
466     NR::Point D = box->persp_ref->getObject()->tmat.image(D_proj).affine();
467     NR::Point E = box->persp_ref->getObject()->tmat.image(E_proj).affine();
468     NR::Point pt = box->persp_ref->getObject()->tmat.image(pt_proj).affine();
470     // TODO: Replace these lines between corners with lines from a corner to a vanishing point
471     //       (this might help to prevent rounding errors if the box is small)
472     Box3D::Line pl1(A, B);
473     Box3D::Line pl2(A, D);
474     Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
475     Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
477     int num_snap_lines = (id != -1) ? 3 : 4;
478     NR::Point snap_pts[num_snap_lines];
480     snap_pts[0] = pl1.closest_to (pt);
481     snap_pts[1] = pl2.closest_to (pt);
482     snap_pts[2] = diag1.closest_to (pt);
483     if (id == -1) {
484         snap_pts[3] = diag2.closest_to (pt);
485     }
487     gdouble const zoom = inkscape_active_desktop()->current_zoom();
489     // determine the distances to all potential snapping points
490     double snap_dists[num_snap_lines];
491     for (int i = 0; i < num_snap_lines; ++i) {
492         snap_dists[i] = NR::L2 (snap_pts[i] - pt) * zoom;
493     }
495     // while we are within a given tolerance of the starting point,
496     // keep snapping to the same point to avoid jumping
497     bool within_tolerance = true;
498     for (int i = 0; i < num_snap_lines; ++i) {
499         if (snap_dists[i] > remember_snap_threshold) {
500             within_tolerance = false;
501             break;
502         }
503     }
505     // find the closest snapping point
506     int snap_index = -1;
507     double snap_dist = NR_HUGE;
508     for (int i = 0; i < num_snap_lines; ++i) {
509         if (snap_dists[i] < snap_dist) {
510             snap_index = i;
511             snap_dist = snap_dists[i];
512         }
513     }
515     // snap to the closest point (or the previously remembered one
516     // if we are within tolerance of the starting point)
517     NR::Point result;
518     if (within_tolerance) {
519         result = snap_pts[remember_snap_index_center];
520     } else {
521         remember_snap_index_center = snap_index;
522         result = snap_pts[snap_index];
523     }
524     return box->persp_ref->getObject()->tmat.preimage (result, z_coord, Proj::Z);
527 void
528 box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
529     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
531     box->orig_corner0.normalize();
532     box->orig_corner7.normalize();
534     /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
535     if (!(movement & Box3D::Z)) {
536         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
537                                                                                 box->orig_corner7[Proj::Z],
538                                                             Proj::Z));
539         if (constrained) {
540             pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
541         }
543         // normalizing pt_proj is essential because we want to mingle affine coordinates
544         pt_proj.normalize();
545         box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
546                                        (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
547                                        box->save_corner0[Proj::Z],
548                                        1.0);
549         box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
550                                        (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
551                                        box->save_corner7[Proj::Z],
552                                        1.0);
553     } else {
554         Box3D::PerspectiveLine pl(box->persp_ref->getObject()->tmat.image(
555                                       box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
556                                   Proj::Z, box->persp_ref->getObject());
557         NR::Point new_pos_snapped(pl.closest_to(new_pos));
558         Proj::Pt3 pt_proj (box->persp_ref->getObject()->
559                            tmat.preimage (new_pos_snapped,
560                                           box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
561                                           (movement & Box3D::Y) ? Proj::X : Proj::Y));
562         bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
563         bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
564         bool corner7_move_x =  (id & Box3D::X) && (movement & Box3D::X);
565         bool corner7_move_y =  (id & Box3D::Y) && (movement & Box3D::Y);
566         // normalizing pt_proj is essential because we want to mingle affine coordinates
567         pt_proj.normalize();        
568         box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
569                                        corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
570                                        (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
571                                        1.0);
572         box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
573                                        corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
574                                        (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
575                                        1.0);
576     }
577     // FIXME: Should we update the box here? If so, how?
580 void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
581     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
583     if (!(movement & Box3D::Z)) {
584         double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
585         double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
586         double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
588         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, coord, Proj::Z));
589         if (constrained) {
590             Proj::Pt3 old_pos_proj (box->persp_ref->getObject()->tmat.preimage (old_pos, coord, Proj::Z));
591             pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
592         }
593         // normalizing pt_proj is essential because we want to mingle affine coordinates
594         pt_proj.normalize();        
595         box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
596                                        (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
597                                        box->orig_corner0[Proj::Z],
598                                        1.0);
599         box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
600                                        (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
601                                        box->orig_corner7[Proj::Z],
602                                        1.0);
603     } else {
604         double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
605         double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
607         Box3D::PerspectiveLine pl(old_pos, Proj::Z, box->persp_ref->getObject());
608         NR::Point new_pos_snapped(pl.closest_to(new_pos));
609         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos_snapped, coord, Proj::X));
611         /* normalizing pt_proj is essential because we want to mingle affine coordinates */
612         pt_proj.normalize();        
613         box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
614                                        box->orig_corner0[Proj::Y],
615                                        pt_proj[Proj::Z] - radz,
616                                        1.0);
617         box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
618                                        box->orig_corner7[Proj::Y],
619                                        pt_proj[Proj::Z] + radz,
620                                        1.0);
621     }
624 /*
625  * Manipulates corner1 through corner4 to contain the indices of the corners
626  * from which the perspective lines in the direction of 'axis' emerge
627  */
628 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis, 
629                             NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
631     g_return_if_fail (box->persp_ref->getObject());
632     //box->orig_corner0.normalize();
633     //box->orig_corner7.normalize();
634     double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
635         box->orig_corner0[axis] :
636         box->orig_corner7[axis];
638     Proj::Pt3 c1, c2, c3, c4;
639     // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
640     switch (axis) {
641         case Proj::X:
642             c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
643             c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
644             c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
645             c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
646             break;
647         case Proj::Y:
648             c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
649             c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
650             c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
651             c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
652             break;
653         case Proj::Z:
654             c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
655             c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
656             c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
657             c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
658             break;
659         default:
660             return;
661     }
662     corner1 = box->persp_ref->getObject()->tmat.image(c1).affine();
663     corner2 = box->persp_ref->getObject()->tmat.image(c2).affine();
664     corner3 = box->persp_ref->getObject()->tmat.image(c3).affine();
665     corner4 = box->persp_ref->getObject()->tmat.image(c4).affine();
668 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
669 static bool
670 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
671                                       Geom::Point const &C, Geom::Point const &D) {
672     Geom::Point E; // the point of intersection
673     Geom::Point n0 = (B - A).ccw();
674     double d0 = dot(n0,A);
676     Geom::Point n1 = (D - C).ccw();
677     double d1 = dot(n1,C);
678     Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
679     if (intersects == Geom::coincident || intersects == Geom::parallel) {
680         return false;
681     }
683     if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
684         // C and D lie on the same side of the line AB
685         return false;
686     }
687     if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
688         // A and B lie on different sides of the line CD
689         return true;
690     } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
691         // The line CD passes on the "wrong" side of A
692         return false;
693     }
695     // The line CD passes on the "correct" side of A
696     return true;
699 static bool
700 box3d_XY_axes_are_swapped (SPBox3D *box) {
701     Persp3D *persp = box->persp_ref->getObject();
702     g_return_val_if_fail(persp, false);
703     Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3), Proj::X, persp);
704     Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3), Proj::Y, persp);
705     NR::Point v1(l1.direction());
706     NR::Point v2(l2.direction());
707     v1.normalize();
708     v2.normalize();
710     return (v1[NR::X]*v2[NR::Y] - v1[NR::Y]*v2[NR::X] > 0);
713 static inline void
714 box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
715     z_orders[0] = a;
716     z_orders[1] = b;
717     z_orders[2] = c;
718     z_orders[3] = d;
719     z_orders[4] = e;
720     z_orders[5] = f;
723 static inline void
724 box3d_swap_z_orders (int z_orders[6]) {
725     int tmp;
726     for (int i = 0; i < 3; ++i) {
727         tmp = z_orders[i];
728         z_orders[i] = z_orders[5-i];
729         z_orders[5-i] = tmp;
730     }
733 /*
734  * In der Standard-Perspektive:
735  * 2 = vorne
736  * 1 = oben
737  * 0 = links
738  * 3 = rechts
739  * 4 = unten
740  * 5 = hinten
741  */
743 /* All VPs infinite */
744 static void
745 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
746     Persp3D *persp = box->persp_ref->getObject();
747     NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
748     NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
749     NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
751     bool swapped = box3d_XY_axes_are_swapped(box);
753     //g_print ("3 infinite VPs; ");
754     switch(central_axis) {
755         case Box3D::X:
756             if (!swapped) {
757                 //g_print ("central axis X (case a)");
758                 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
759             } else {
760                 //g_print ("central axis X (case b)");
761                 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
762             }
763             break;
764         case Box3D::Y:
765             if (!swapped) {
766                 //g_print ("central axis Y (case a)");
767                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
768             } else {
769                 //g_print ("central axis Y (case b)");
770                 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
771             }
772             break;
773         case Box3D::Z:
774             if (!swapped) {
775                 //g_print ("central axis Z (case a)");
776                 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
777             } else {
778                 //g_print ("central axis Z (case b)");
779                 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
780             }
781             break;
782         case Box3D::NONE:
783             if (!swapped) {
784                 //g_print ("central axis NONE (case a)");
785                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
786             } else {
787                 //g_print ("central axis NONE (case b)");
788                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
789             }
790             break;
791         default:
792             g_assert_not_reached();
793             break;
794     }
795     /**
796     if (swapped) {
797         g_print ("; swapped");
798     }
799     g_print ("\n");
800     **/
803 /* Precisely one finite VP */
804 static void
805 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
806     Persp3D *persp = box->persp_ref->getObject();
807     NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
809     // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
810     Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
811     Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
812     //g_print ("oaxis1  = %s, oaxis2  = %s\n", Box3D::string_from_axes(oaxis1), Box3D::string_from_axes(oaxis2));
813     int inside1 = 0;
814     int inside2 = 0;
815     inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
816     inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
817     //g_print ("inside1 = %d, inside2 = %d\n", inside1, inside2);
819     bool swapped = box3d_XY_axes_are_swapped(box);
821     //g_print ("2 infinite VPs; ");
822     //g_print ("finite axis: %s; ", Box3D::string_from_axes(fin_axis));
823     switch(central_axis) {
824         case Box3D::X:
825             if (!swapped) {
826                 //g_print ("central axis X (case a)");
827                 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
828             } else {
829                 //if (inside2) {
830                     //g_print ("central axis X (case b)");
831                     box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
832                 //} else {
833                     //g_print ("central axis X (case c)");
834                     //box3d_aux_set_z_orders (z_orders, 5, 3, 1, 2, 0, 4);
835                 //}
836             }
837             break;
838         case Box3D::Y:
839             if (inside2 > 0) {
840                 //g_print ("central axis Y (case a)");
841                 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
842             } else if (inside2 < 0) {
843                 //g_print ("central axis Y (case b)");
844                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
845             } else {
846                 if (!swapped) {
847                     //g_print ("central axis Y (case c1)");
848                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
849                 } else {
850                     //g_print ("central axis Y (case c2)");
851                     box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
852                 }
853             }
854             break;
855         case Box3D::Z:
856             if (inside2) {
857                 if (!swapped) {
858                     //g_print ("central axis Z (case a1)");
859                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
860                 } else {
861                     //g_print ("central axis Z (case a2)");
862                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
863                 }
864             } else if (inside1) {
865                 if (!swapped) {
866                     //g_print ("central axis Z (case b1)");
867                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
868                 } else {
869                     //g_print ("central axis Z (case b2)");
870                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
871                     //box3d_aux_set_z_orders (z_orders, 5, 3, 0, 1, 2, 4);
872                 }                    
873             } else {
874                 // "regular" case
875                 if (!swapped) {
876                     //g_print ("central axis Z (case c1)");
877                     box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
878                 } else {
879                     //g_print ("central axis Z (case c2)");
880                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
881                     //box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
882                 }
883             }
884             break;
885         case Box3D::NONE:
886             if (!swapped) {
887                 //g_print ("central axis NONE (case a)");
888                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
889             } else {
890                 //g_print ("central axis NONE (case b)");
891                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
892                 //box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
893             }
894             break;
895         default:
896             g_assert_not_reached();
897     }
898     /**
899     if (swapped) {
900         g_print ("; swapped");
901     }
902     g_print ("\n");
903     **/
906 /* Precisely 2 finite VPs */
907 static void
908 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis infinite_axis) {
909     Persp3D *persp = box->persp_ref->getObject();
911     NR::Point c3(box3d_get_corner_screen(box, 3));
912     NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
913     NR::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
914     NR::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
916     bool swapped = box3d_XY_axes_are_swapped(box);
918     int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
919     int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
921     int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
922     int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
924     int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
925     int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
927     //g_print ("Insides: xy = %d, xz = %d, yx = %d, yz = %d, zx = %d, zy = %d\n",
928     //         insidexy, insidexz, insideyx, insideyz, insidezx, insidezy);
930     //g_print ("1 infinite VP; ");
931     switch(central_axis) {
932         case Box3D::X:
933             if (!swapped) {
934                 if (insidezy == -1) {
935                     //g_print ("central axis X (case a1)");
936                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
937                 } else if (insidexy == 1) {
938                     //g_print ("central axis X (case a2)");
939                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
940                 } else {
941                     //g_print ("central axis X (case a3)");
942                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
943                 }
944             } else {
945                 if (insideyz == -1) {
946                     //g_print ("central axis X (case b1)");
947                     box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
948                 } else {
949                     if (!swapped) {
950                         //g_print ("central axis X (case b2)");
951                         box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
952                     } else {
953                         //g_print ("central axis X (case b3)");
954                         box3d_aux_set_z_orders (z_orders, 1, 3, 5, 0, 2, 4);
955                     }
956                 }
957             }
958             break;
959         case Box3D::Y:
960             if (!swapped) {
961                 if (insideyz == 1) {
962                     //g_print ("central axis Y (case a1)");
963                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
964                 } else {
965                     //g_print ("central axis Y (case a2)");
966                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
967                 }
968             } else {
969                 //g_print ("central axis Y (case b)");
970                 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
971             }
972             break;
973         case Box3D::Z:
974             if (!swapped) {
975                 if (insidezy == 1) {
976                     //g_print ("central axis Z (case a1)");
977                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
978                 } else if (insidexy == -1) {
979                     //g_print ("central axis Z (case a2)");
980                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
981                 } else {
982                     //g_print ("central axis Z (case a3)");
983                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
984                 }
985             } else {
986                 //g_print ("central axis Z (case b)");
987                 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
988             }
989             break;
990         case Box3D::NONE:
991             if (!swapped) {
992                 //g_print ("central axis NONE (case a)");
993                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
994             } else {
995                 //g_print ("central axis NONE (case b)");
996                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
997             }
998             break;
999         default:
1000             g_assert_not_reached();
1001             break;
1002     }
1003     /**
1004     if (swapped) {
1005         g_print ("; swapped");
1006     }
1007     g_print ("\n");
1008     **/
1011 /*
1012  * It can happen that during dragging the box is everted.
1013  * In this case the opposite sides in this direction need to be swapped
1014  */
1015 static Box3D::Axis
1016 box3d_everted_directions (SPBox3D *box) {
1017     Box3D::Axis ev = Box3D::NONE;
1019     box->orig_corner0.normalize();
1020     box->orig_corner7.normalize();
1022     if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
1023         ev = (Box3D::Axis) (ev ^ Box3D::X);
1024     if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
1025         ev = (Box3D::Axis) (ev ^ Box3D::Y);
1026     if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
1027         ev = (Box3D::Axis) (ev ^ Box3D::Z);
1029     return ev;
1032 static void
1033 box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
1034     int pos1 = -1;
1035     int pos2 = -1;
1037     for (int i = 0; i < 6; ++i) {
1038         if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
1039             if (pos1 == -1) {
1040                 pos1 = i;
1041             } else {
1042                 pos2 = i;
1043                 break;
1044             }
1045         }
1046     }
1048     int tmp = z_orders[pos1];
1049     z_orders[pos1] = z_orders[pos2];
1050     z_orders[pos2] = tmp;
1054 bool
1055 box3d_recompute_z_orders (SPBox3D *box) {
1056     Persp3D *persp = box->persp_ref->getObject();
1058     //g_return_val_if_fail(persp, false);
1059     if (!persp)
1060         return false;
1062     int z_orders[6];
1064     NR::Point c3(box3d_get_corner_screen(box, 3));
1066     // determine directions from corner3 to the VPs
1067     int num_finite = 0;
1068     Box3D::Axis axis_finite = Box3D::NONE;
1069     Box3D::Axis axis_infinite = Box3D::NONE;
1070     NR::Point dirs[3];
1071     for (int i = 0; i < 3; ++i) {
1072         dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
1073         if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
1074             num_finite++;
1075             axis_finite = Box3D::axes[i];
1076         } else {
1077             axis_infinite = Box3D::axes[i];
1078         }
1079     }
1081     // determine the "central" axis (if there is one)
1082     Box3D::Axis central_axis = Box3D::NONE;
1083     if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
1084         central_axis = Box3D::Z;
1085     } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
1086         central_axis = Box3D::X;
1087     } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
1088         central_axis = Box3D::Y;
1089     }
1091     switch (num_finite) {
1092         case 0:
1093             // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
1094             //               coincide, hence only need to be computed once in a more central location.
1095             box3d_set_new_z_orders_case0(box, z_orders, central_axis);
1096             break;
1097         case 1:
1098             box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
1099             break;
1100         case 2:
1101         case 3:
1102             box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
1103             break;
1104         default:
1105         /*
1106          * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
1107          * joining the other two VPs. If this is the case, it determines the "central" corner from
1108          * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
1109          */
1110         // FIXME: We should eliminate the use of NR::Point altogether
1111         Box3D::Axis central_axis = Box3D::NONE;
1112         NR::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
1113         NR::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
1114         NR::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
1115         Geom::Point vpx(vp_x[NR::X], vp_x[NR::Y]);
1116         Geom::Point vpy(vp_y[NR::X], vp_y[NR::Y]);
1117         Geom::Point vpz(vp_z[NR::X], vp_z[NR::Y]);
1119         NR::Point c3 = box3d_get_corner_screen(box, 3);
1120         Geom::Point corner3(c3[NR::X], c3[NR::Y]);
1122         if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
1123             central_axis = Box3D::X;
1124         } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
1125             central_axis = Box3D::Y;
1126         } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
1127             central_axis = Box3D::Z;
1128         }
1129         //g_print ("Crossing: %s\n", Box3D::string_from_axes(central_axis));
1131         unsigned int central_corner = 3 ^ central_axis;
1132         if (central_axis == Box3D::Z) {
1133             central_corner = central_corner ^ Box3D::XYZ;
1134         }
1135         if (box3d_XY_axes_are_swapped(box)) {
1136             //g_print ("Axes X and Y are swapped\n");
1137             central_corner = central_corner ^ Box3D::XYZ;
1138         }
1140         NR::Point c1(box3d_get_corner_screen(box, 1));
1141         NR::Point c2(box3d_get_corner_screen(box, 2));
1142         NR::Point c7(box3d_get_corner_screen(box, 7));
1144         Geom::Point corner1(c1[NR::X], c1[NR::Y]);
1145         Geom::Point corner2(c2[NR::X], c2[NR::Y]);
1146         Geom::Point corner7(c7[NR::X], c7[NR::Y]);
1147         // FIXME: At present we don't use the information about central_corner computed above.
1148         switch (central_axis) {
1149             case Box3D::Y:
1150                 if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
1151                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1152                 } else {
1153                     // degenerate case
1154                     //g_print ("Degenerate case #1\n");
1155                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
1156                 }
1157                 break;
1159             case Box3D::Z:
1160                 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
1161                     // degenerate case
1162                     //g_print ("Degenerate case #2\n");
1163                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
1164                 } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
1165                     // degenerate case
1166                     //g_print ("Degenerate case #3\n");
1167                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
1168                 } else {
1169                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
1170                 }
1171                 break;
1173             case Box3D::X:
1174                 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
1175                     // degenerate case
1176                     //g_print ("Degenerate case #4\n");
1177                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
1178                 } else {
1179                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
1180                 }
1181                 break;
1183             case Box3D::NONE:
1184                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1185                 break;
1187             default:
1188                 g_assert_not_reached();
1189                 break;
1190         } // end default case
1191     }
1193     // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
1194     //       instead of the hard-coded corner #3 in the computations above
1195     Box3D::Axis ev = box3d_everted_directions(box);
1196     for (int i = 0; i < 3; ++i) {
1197         if (ev & Box3D::axes[i]) {
1198             box3d_swap_sides(z_orders, Box3D::axes[i]);
1199         }
1200     }
1202     // Check whether anything actually changed
1203     for (int i = 0; i < 6; ++i) {
1204         if (box->z_orders[i] != z_orders[i]) {
1205             for (int j = i; j < 6; ++j) {
1206                 box->z_orders[j] = z_orders[j];
1207             }
1208             return true;
1209         }
1210     }
1211     return false;
1214 static std::map<int, Box3DSide *>
1215 box3d_get_sides (SPBox3D *box) {
1216     std::map<int, Box3DSide *> sides;
1217     for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
1218         sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
1219                                                            "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
1220     }
1221     sides.erase(-1);
1222     return sides;
1226 // TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
1227 void
1228 box3d_set_z_orders (SPBox3D *box) {
1229     // For efficiency reasons, we only set the new z-orders if something really changed
1230     if (box3d_recompute_z_orders (box)) {
1231         std::map<int, Box3DSide *> sides = box3d_get_sides(box);
1232         std::map<int, Box3DSide *>::iterator side;
1233         for (unsigned int i = 0; i < 6; ++i) {
1234             side = sides.find(box->z_orders[i]);
1235             if (side != sides.end()) {
1236                 SP_ITEM((*side).second)->lowerToBottom();
1237             }
1238         }
1239         /**
1240         g_print ("Resetting z-orders: ");
1241         for (int i = 0; i < 6; ++i) {
1242             g_print ("%d ", box->z_orders[i]);
1243         }
1244         g_print ("\n");
1245         **/
1246     }
1249 /*
1250  * Auxiliary function for z-order recomputing:
1251  * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
1252  * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
1253  * lies in the sector if it lies between the two (parallel) PLs.
1254  * \ret *  0 if \a pt doesn't lie in the sector
1255  *      *  1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
1256  *           from the edge between the two corners to \a pt points towards the VP
1257  *      * -1 otherwise
1258  */
1259 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
1260 //       can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
1261 int
1262 box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
1263     Persp3D *persp = box->persp_ref->getObject();
1265     // the two corners
1266     NR::Point c1(box3d_get_corner_screen(box, id1));
1267     NR::Point c2(box3d_get_corner_screen(box, id2));
1269     int ret = 0;
1270     if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
1271         NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
1272         NR::Point v1(c1 - vp);
1273         NR::Point v2(c2 - vp);
1274         NR::Point w(pt - vp);
1275         ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
1276         //g_print ("Case 0 - returning %d\n", ret);
1277     } else {
1278         Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
1279         Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
1280         if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
1281             // test whether pt lies "towards" or "away from" the VP
1282             Box3D::Line edge(c1,c2);
1283             NR::Point c3(box3d_get_corner_screen(box, id1 ^ axis));
1284             if (edge.lie_on_same_side(pt, c3)) {
1285                 ret = 1;
1286             } else {
1287                 ret = -1;
1288             }
1289         }
1290         //g_print ("Case 1 - returning %d\n", ret);
1291     }
1292     return ret;
1295 int
1296 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
1297     Persp3D *persp = box->persp_ref->getObject();
1299     if (!persp3d_VP_is_finite(persp, vpdir)) {
1300         return 0;
1301     } else {
1302         return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
1303     }
1306 /* swap the coordinates of corner0 and corner7 along the specified axis */
1307 static void
1308 box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
1309     box->orig_corner0.normalize();
1310     box->orig_corner7.normalize();
1311     if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1312         double tmp = box->orig_corner0[axis];
1313         box->orig_corner0[axis] = box->orig_corner7[axis];
1314         box->orig_corner7[axis] = tmp;
1315     }
1316     // FIXME: Should we also swap the coordinates of save_corner0 and save_corner7?
1319 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
1320 void
1321 box3d_relabel_corners(SPBox3D *box) {
1322     box3d_swap_coords(box, Proj::X, false);
1323     box3d_swap_coords(box, Proj::Y, false);
1324     box3d_swap_coords(box, Proj::Z, true);
1327 /*
1328   Local Variables:
1329   mode:c++
1330   c-file-style:"stroustrup"
1331   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1332   indent-tabs-mode:nil
1333   fill-column:99
1334   End:
1335 */
1336 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :