Code

0f8abd71d0fd5bc7192883e2d3e92e35e1d6f950
[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     /* check whether we need to unlink any boxes from their perspectives */
349     std::set<Persp3D *> p_sel = persp3d_currently_selected_persps(inkscape_active_event_context());
350     Persp3D *persp;
351     Persp3D *transf_persp;
352     for (std::set<Persp3D *>::iterator p = p_sel.begin(); p != p_sel.end(); ++p) {
353         persp = (*p);
354         if (!persp3d_has_all_boxes_in_selection (persp)) {
355             std::list<SPBox3D *> sel = persp3d_selected_boxes (persp);
357             /* create a new perspective as a copy of the current one and link the selected boxes to it */
358             transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
360             for (std::list<SPBox3D *>::iterator b = sel.begin(); b != sel.end(); ++b) {
361                 box3d_switch_perspectives(*b, persp, transf_persp);
362             }
363         } else {
364             transf_persp = persp;
365         }
367         /* concatenate the affine transformation with the perspective mapping; this
368            function also triggers repr updates of boxes and the perspective itself */
369         persp3d_apply_affine_transformation(transf_persp, xform);
370     }
372     /***
373     // FIXME: We somehow have to apply the transformation to strokes, patterns, and gradients. How?
374     NR::Matrix ret(NR::transform(xform));
375     gdouble const sw = hypot(ret[0], ret[1]);
376     gdouble const sh = hypot(ret[2], ret[3]);
378     SPItem *sideitem = NULL;
379     for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
380         sideitem = SP_ITEM(side);
382         // Adjust stroke width
383         sp_item_adjust_stroke(sideitem, sqrt(fabs(sw * sh)));
385         // Adjust pattern fill
386         sp_item_adjust_pattern(sideitem, xform);
388         // Adjust gradient fill
389         sp_item_adjust_gradient(sideitem, xform);
390     }
391     ***/
393     return NR::identity();
397 /**
398  * Gets called when persp(?) repr contents change: i.e. parameter change.
399  */
400 static void
401 box3d_ref_modified(SPObject */*href*/, guint /*flags*/, SPBox3D */*box*/)
403     /***
404     g_print ("FIXME: box3d_ref_modified was called. What should we do?\n");
405     g_print ("Here is at least the the href's id: %s\n", SP_OBJECT_REPR(href)->attribute("id"));
406     g_print ("             ... and the box's, too: %s\n", SP_OBJECT_REPR(box)->attribute("id"));
407     ***/
411 Proj::Pt3
412 box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
413     return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
414                       (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
415                       (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
416                       1.0);
419 Proj::Pt3
420 box3d_get_proj_corner (SPBox3D const *box, guint id) {
421     return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
422                       (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
423                       (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
424                       1.0);
427 NR::Point
428 box3d_get_corner_screen (SPBox3D const *box, guint id) {
429     Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
430     if (!box->persp_ref->getObject()) {
431         //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
432         return NR::Point (NR_HUGE, NR_HUGE);
433     }
434     return box->persp_ref->getObject()->tmat.image(proj_corner).affine();
437 Proj::Pt3
438 box3d_get_proj_center (SPBox3D *box) {
439     box->orig_corner0.normalize();
440     box->orig_corner7.normalize();
441     return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
442                       (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
443                       (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
444                       1.0);
447 NR::Point
448 box3d_get_center_screen (SPBox3D *box) {
449     Proj::Pt3 proj_center (box3d_get_proj_center (box));
450     if (!box->persp_ref->getObject()) {
451         //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
452         return NR::Point (NR_HUGE, NR_HUGE);
453     }
454     return box->persp_ref->getObject()->tmat.image(proj_center).affine();
457 /*
458  * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
459  * their intersection, we remember the last snapped line and keep snapping to this specific line as long
460  * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
461  */
463 // Should we make the threshold settable in the preferences?
464 static double remember_snap_threshold = 30;
465 //static guint remember_snap_index = 0;
466 static guint remember_snap_index_center = 0;
468 static Proj::Pt3
469 box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
470     double z_coord = start_pt[Proj::Z];
471     double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
472     double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
473     double x_coord = start_pt[Proj::X];
474     double y_coord = start_pt[Proj::Y];
475     Proj::Pt3 A_proj (x_coord,          y_coord,          z_coord, 1.0);
476     Proj::Pt3 B_proj (x_coord + diff_x, y_coord,          z_coord, 1.0);
477     Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
478     Proj::Pt3 D_proj (x_coord,          y_coord + diff_y, z_coord, 1.0);
479     Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
481     NR::Point A = box->persp_ref->getObject()->tmat.image(A_proj).affine();
482     NR::Point B = box->persp_ref->getObject()->tmat.image(B_proj).affine();
483     NR::Point C = box->persp_ref->getObject()->tmat.image(C_proj).affine();
484     NR::Point D = box->persp_ref->getObject()->tmat.image(D_proj).affine();
485     NR::Point E = box->persp_ref->getObject()->tmat.image(E_proj).affine();
486     NR::Point pt = box->persp_ref->getObject()->tmat.image(pt_proj).affine();
488     // TODO: Replace these lines between corners with lines from a corner to a vanishing point
489     //       (this might help to prevent rounding errors if the box is small)
490     Box3D::Line pl1(A, B);
491     Box3D::Line pl2(A, D);
492     Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
493     Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
495     int num_snap_lines = (id != -1) ? 3 : 4;
496     NR::Point snap_pts[num_snap_lines];
498     snap_pts[0] = pl1.closest_to (pt);
499     snap_pts[1] = pl2.closest_to (pt);
500     snap_pts[2] = diag1.closest_to (pt);
501     if (id == -1) {
502         snap_pts[3] = diag2.closest_to (pt);
503     }
505     gdouble const zoom = inkscape_active_desktop()->current_zoom();
507     // determine the distances to all potential snapping points
508     double snap_dists[num_snap_lines];
509     for (int i = 0; i < num_snap_lines; ++i) {
510         snap_dists[i] = NR::L2 (snap_pts[i] - pt) * zoom;
511     }
513     // while we are within a given tolerance of the starting point,
514     // keep snapping to the same point to avoid jumping
515     bool within_tolerance = true;
516     for (int i = 0; i < num_snap_lines; ++i) {
517         if (snap_dists[i] > remember_snap_threshold) {
518             within_tolerance = false;
519             break;
520         }
521     }
523     // find the closest snapping point
524     int snap_index = -1;
525     double snap_dist = NR_HUGE;
526     for (int i = 0; i < num_snap_lines; ++i) {
527         if (snap_dists[i] < snap_dist) {
528             snap_index = i;
529             snap_dist = snap_dists[i];
530         }
531     }
533     // snap to the closest point (or the previously remembered one
534     // if we are within tolerance of the starting point)
535     NR::Point result;
536     if (within_tolerance) {
537         result = snap_pts[remember_snap_index_center];
538     } else {
539         remember_snap_index_center = snap_index;
540         result = snap_pts[snap_index];
541     }
542     return box->persp_ref->getObject()->tmat.preimage (result, z_coord, Proj::Z);
545 void
546 box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
547     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
549     box->orig_corner0.normalize();
550     box->orig_corner7.normalize();
552     /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
553     if (!(movement & Box3D::Z)) {
554         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
555                                                                                 box->orig_corner7[Proj::Z],
556                                                             Proj::Z));
557         if (constrained) {
558             pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
559         }
561         // normalizing pt_proj is essential because we want to mingle affine coordinates
562         pt_proj.normalize();
563         box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
564                                        (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
565                                        box->save_corner0[Proj::Z],
566                                        1.0);
567         box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
568                                        (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
569                                        box->save_corner7[Proj::Z],
570                                        1.0);
571     } else {
572         Box3D::PerspectiveLine pl(box->persp_ref->getObject()->tmat.image(
573                                       box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
574                                   Proj::Z, box->persp_ref->getObject());
575         NR::Point new_pos_snapped(pl.closest_to(new_pos));
576         Proj::Pt3 pt_proj (box->persp_ref->getObject()->
577                            tmat.preimage (new_pos_snapped,
578                                           box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
579                                           (movement & Box3D::Y) ? Proj::X : Proj::Y));
580         bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
581         bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
582         bool corner7_move_x =  (id & Box3D::X) && (movement & Box3D::X);
583         bool corner7_move_y =  (id & Box3D::Y) && (movement & Box3D::Y);
584         // normalizing pt_proj is essential because we want to mingle affine coordinates
585         pt_proj.normalize();
586         box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
587                                        corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
588                                        (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
589                                        1.0);
590         box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
591                                        corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
592                                        (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
593                                        1.0);
594     }
595     // FIXME: Should we update the box here? If so, how?
598 void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
599     g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
601     if (!(movement & Box3D::Z)) {
602         double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
603         double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
604         double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
606         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos, coord, Proj::Z));
607         if (constrained) {
608             Proj::Pt3 old_pos_proj (box->persp_ref->getObject()->tmat.preimage (old_pos, coord, Proj::Z));
609             pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
610         }
611         // normalizing pt_proj is essential because we want to mingle affine coordinates
612         pt_proj.normalize();
613         box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
614                                        (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
615                                        box->orig_corner0[Proj::Z],
616                                        1.0);
617         box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
618                                        (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
619                                        box->orig_corner7[Proj::Z],
620                                        1.0);
621     } else {
622         double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
623         double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
625         Box3D::PerspectiveLine pl(old_pos, Proj::Z, box->persp_ref->getObject());
626         NR::Point new_pos_snapped(pl.closest_to(new_pos));
627         Proj::Pt3 pt_proj (box->persp_ref->getObject()->tmat.preimage (new_pos_snapped, coord, Proj::X));
629         /* normalizing pt_proj is essential because we want to mingle affine coordinates */
630         pt_proj.normalize();
631         box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
632                                        box->orig_corner0[Proj::Y],
633                                        pt_proj[Proj::Z] - radz,
634                                        1.0);
635         box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
636                                        box->orig_corner7[Proj::Y],
637                                        pt_proj[Proj::Z] + radz,
638                                        1.0);
639     }
642 /*
643  * Manipulates corner1 through corner4 to contain the indices of the corners
644  * from which the perspective lines in the direction of 'axis' emerge
645  */
646 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
647                             NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
649     g_return_if_fail (box->persp_ref->getObject());
650     //box->orig_corner0.normalize();
651     //box->orig_corner7.normalize();
652     double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
653         box->orig_corner0[axis] :
654         box->orig_corner7[axis];
656     Proj::Pt3 c1, c2, c3, c4;
657     // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
658     switch (axis) {
659         case Proj::X:
660             c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
661             c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
662             c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
663             c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
664             break;
665         case Proj::Y:
666             c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
667             c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
668             c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
669             c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
670             break;
671         case Proj::Z:
672             c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
673             c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
674             c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
675             c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
676             break;
677         default:
678             return;
679     }
680     corner1 = box->persp_ref->getObject()->tmat.image(c1).affine();
681     corner2 = box->persp_ref->getObject()->tmat.image(c2).affine();
682     corner3 = box->persp_ref->getObject()->tmat.image(c3).affine();
683     corner4 = box->persp_ref->getObject()->tmat.image(c4).affine();
686 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
687 static bool
688 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
689                                       Geom::Point const &C, Geom::Point const &D) {
690     Geom::Point E; // the point of intersection
691     Geom::Point n0 = (B - A).ccw();
692     double d0 = dot(n0,A);
694     Geom::Point n1 = (D - C).ccw();
695     double d1 = dot(n1,C);
696     Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
697     if (intersects == Geom::coincident || intersects == Geom::parallel) {
698         return false;
699     }
701     if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
702         // C and D lie on the same side of the line AB
703         return false;
704     }
705     if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
706         // A and B lie on different sides of the line CD
707         return true;
708     } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
709         // The line CD passes on the "wrong" side of A
710         return false;
711     }
713     // The line CD passes on the "correct" side of A
714     return true;
717 static bool
718 box3d_XY_axes_are_swapped (SPBox3D *box) {
719     Persp3D *persp = box->persp_ref->getObject();
720     g_return_val_if_fail(persp, false);
721     Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3), Proj::X, persp);
722     Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3), Proj::Y, persp);
723     NR::Point v1(l1.direction());
724     NR::Point v2(l2.direction());
725     v1.normalize();
726     v2.normalize();
728     return (v1[NR::X]*v2[NR::Y] - v1[NR::Y]*v2[NR::X] > 0);
731 static inline void
732 box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
733     z_orders[0] = a;
734     z_orders[1] = b;
735     z_orders[2] = c;
736     z_orders[3] = d;
737     z_orders[4] = e;
738     z_orders[5] = f;
741 static inline void
742 box3d_swap_z_orders (int z_orders[6]) {
743     int tmp;
744     for (int i = 0; i < 3; ++i) {
745         tmp = z_orders[i];
746         z_orders[i] = z_orders[5-i];
747         z_orders[5-i] = tmp;
748     }
751 /*
752  * In der Standard-Perspektive:
753  * 2 = vorne
754  * 1 = oben
755  * 0 = links
756  * 3 = rechts
757  * 4 = unten
758  * 5 = hinten
759  */
761 /* All VPs infinite */
762 static void
763 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
764     Persp3D *persp = box->persp_ref->getObject();
765     NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
766     NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
767     NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
769     bool swapped = box3d_XY_axes_are_swapped(box);
771     //g_print ("3 infinite VPs; ");
772     switch(central_axis) {
773         case Box3D::X:
774             if (!swapped) {
775                 //g_print ("central axis X (case a)");
776                 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
777             } else {
778                 //g_print ("central axis X (case b)");
779                 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
780             }
781             break;
782         case Box3D::Y:
783             if (!swapped) {
784                 //g_print ("central axis Y (case a)");
785                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
786             } else {
787                 //g_print ("central axis Y (case b)");
788                 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
789             }
790             break;
791         case Box3D::Z:
792             if (!swapped) {
793                 //g_print ("central axis Z (case a)");
794                 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
795             } else {
796                 //g_print ("central axis Z (case b)");
797                 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
798             }
799             break;
800         case Box3D::NONE:
801             if (!swapped) {
802                 //g_print ("central axis NONE (case a)");
803                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
804             } else {
805                 //g_print ("central axis NONE (case b)");
806                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
807             }
808             break;
809         default:
810             g_assert_not_reached();
811             break;
812     }
813     /**
814     if (swapped) {
815         g_print ("; swapped");
816     }
817     g_print ("\n");
818     **/
821 /* Precisely one finite VP */
822 static void
823 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
824     Persp3D *persp = box->persp_ref->getObject();
825     NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
827     // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
828     Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
829     Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
830     //g_print ("oaxis1  = %s, oaxis2  = %s\n", Box3D::string_from_axes(oaxis1), Box3D::string_from_axes(oaxis2));
831     int inside1 = 0;
832     int inside2 = 0;
833     inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
834     inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
835     //g_print ("inside1 = %d, inside2 = %d\n", inside1, inside2);
837     bool swapped = box3d_XY_axes_are_swapped(box);
839     //g_print ("2 infinite VPs; ");
840     //g_print ("finite axis: %s; ", Box3D::string_from_axes(fin_axis));
841     switch(central_axis) {
842         case Box3D::X:
843             if (!swapped) {
844                 //g_print ("central axis X (case a)");
845                 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
846             } else {
847                 //if (inside2) {
848                     //g_print ("central axis X (case b)");
849                     box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
850                 //} else {
851                     //g_print ("central axis X (case c)");
852                     //box3d_aux_set_z_orders (z_orders, 5, 3, 1, 2, 0, 4);
853                 //}
854             }
855             break;
856         case Box3D::Y:
857             if (inside2 > 0) {
858                 //g_print ("central axis Y (case a)");
859                 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
860             } else if (inside2 < 0) {
861                 //g_print ("central axis Y (case b)");
862                 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
863             } else {
864                 if (!swapped) {
865                     //g_print ("central axis Y (case c1)");
866                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
867                 } else {
868                     //g_print ("central axis Y (case c2)");
869                     box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
870                 }
871             }
872             break;
873         case Box3D::Z:
874             if (inside2) {
875                 if (!swapped) {
876                     //g_print ("central axis Z (case a1)");
877                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
878                 } else {
879                     //g_print ("central axis Z (case a2)");
880                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
881                 }
882             } else if (inside1) {
883                 if (!swapped) {
884                     //g_print ("central axis Z (case b1)");
885                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
886                 } else {
887                     //g_print ("central axis Z (case b2)");
888                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
889                     //box3d_aux_set_z_orders (z_orders, 5, 3, 0, 1, 2, 4);
890                 }
891             } else {
892                 // "regular" case
893                 if (!swapped) {
894                     //g_print ("central axis Z (case c1)");
895                     box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
896                 } else {
897                     //g_print ("central axis Z (case c2)");
898                     box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
899                     //box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
900                 }
901             }
902             break;
903         case Box3D::NONE:
904             if (!swapped) {
905                 //g_print ("central axis NONE (case a)");
906                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
907             } else {
908                 //g_print ("central axis NONE (case b)");
909                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
910                 //box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
911             }
912             break;
913         default:
914             g_assert_not_reached();
915     }
916     /**
917     if (swapped) {
918         g_print ("; swapped");
919     }
920     g_print ("\n");
921     **/
924 /* Precisely 2 finite VPs */
925 static void
926 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
927     Persp3D *persp = box->persp_ref->getObject();
929     NR::Point c3(box3d_get_corner_screen(box, 3));
930     NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
931     NR::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
932     NR::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
934     bool swapped = box3d_XY_axes_are_swapped(box);
936     int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
937     int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
939     int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
940     int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
942     int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
943     int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
945     //g_print ("Insides: xy = %d, xz = %d, yx = %d, yz = %d, zx = %d, zy = %d\n",
946     //         insidexy, insidexz, insideyx, insideyz, insidezx, insidezy);
947     (void)insidexz;
948     (void)insideyx;
949     (void)insidezx;
951     //g_print ("1 infinite VP; ");
952     switch(central_axis) {
953         case Box3D::X:
954             if (!swapped) {
955                 if (insidezy == -1) {
956                     //g_print ("central axis X (case a1)");
957                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
958                 } else if (insidexy == 1) {
959                     //g_print ("central axis X (case a2)");
960                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
961                 } else {
962                     //g_print ("central axis X (case a3)");
963                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
964                 }
965             } else {
966                 if (insideyz == -1) {
967                     //g_print ("central axis X (case b1)");
968                     box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
969                 } else {
970                     if (!swapped) {
971                         //g_print ("central axis X (case b2)");
972                         box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
973                     } else {
974                         //g_print ("central axis X (case b3)");
975                         box3d_aux_set_z_orders (z_orders, 1, 3, 5, 0, 2, 4);
976                     }
977                 }
978             }
979             break;
980         case Box3D::Y:
981             if (!swapped) {
982                 if (insideyz == 1) {
983                     //g_print ("central axis Y (case a1)");
984                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
985                 } else {
986                     //g_print ("central axis Y (case a2)");
987                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
988                 }
989             } else {
990                 //g_print ("central axis Y (case b)");
991                 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
992             }
993             break;
994         case Box3D::Z:
995             if (!swapped) {
996                 if (insidezy == 1) {
997                     //g_print ("central axis Z (case a1)");
998                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
999                 } else if (insidexy == -1) {
1000                     //g_print ("central axis Z (case a2)");
1001                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
1002                 } else {
1003                     //g_print ("central axis Z (case a3)");
1004                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
1005                 }
1006             } else {
1007                 //g_print ("central axis Z (case b)");
1008                 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
1009             }
1010             break;
1011         case Box3D::NONE:
1012             if (!swapped) {
1013                 //g_print ("central axis NONE (case a)");
1014                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1015             } else {
1016                 //g_print ("central axis NONE (case b)");
1017                 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
1018             }
1019             break;
1020         default:
1021             g_assert_not_reached();
1022             break;
1023     }
1024     /**
1025     if (swapped) {
1026         g_print ("; swapped");
1027     }
1028     g_print ("\n");
1029     **/
1032 /*
1033  * It can happen that during dragging the box is everted.
1034  * In this case the opposite sides in this direction need to be swapped
1035  */
1036 static Box3D::Axis
1037 box3d_everted_directions (SPBox3D *box) {
1038     Box3D::Axis ev = Box3D::NONE;
1040     box->orig_corner0.normalize();
1041     box->orig_corner7.normalize();
1043     if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
1044         ev = (Box3D::Axis) (ev ^ Box3D::X);
1045     if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
1046         ev = (Box3D::Axis) (ev ^ Box3D::Y);
1047     if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
1048         ev = (Box3D::Axis) (ev ^ Box3D::Z);
1050     return ev;
1053 static void
1054 box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
1055     int pos1 = -1;
1056     int pos2 = -1;
1058     for (int i = 0; i < 6; ++i) {
1059         if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
1060             if (pos1 == -1) {
1061                 pos1 = i;
1062             } else {
1063                 pos2 = i;
1064                 break;
1065             }
1066         }
1067     }
1069     int tmp = z_orders[pos1];
1070     z_orders[pos1] = z_orders[pos2];
1071     z_orders[pos2] = tmp;
1075 bool
1076 box3d_recompute_z_orders (SPBox3D *box) {
1077     Persp3D *persp = box->persp_ref->getObject();
1079     //g_return_val_if_fail(persp, false);
1080     if (!persp)
1081         return false;
1083     int z_orders[6];
1085     NR::Point c3(box3d_get_corner_screen(box, 3));
1087     // determine directions from corner3 to the VPs
1088     int num_finite = 0;
1089     Box3D::Axis axis_finite = Box3D::NONE;
1090     Box3D::Axis axis_infinite = Box3D::NONE;
1091     NR::Point dirs[3];
1092     for (int i = 0; i < 3; ++i) {
1093         dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
1094         if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
1095             num_finite++;
1096             axis_finite = Box3D::axes[i];
1097         } else {
1098             axis_infinite = Box3D::axes[i];
1099         }
1100     }
1102     // determine the "central" axis (if there is one)
1103     Box3D::Axis central_axis = Box3D::NONE;
1104     if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
1105         central_axis = Box3D::Z;
1106     } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
1107         central_axis = Box3D::X;
1108     } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
1109         central_axis = Box3D::Y;
1110     }
1112     switch (num_finite) {
1113         case 0:
1114             // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
1115             //               coincide, hence only need to be computed once in a more central location.
1116             box3d_set_new_z_orders_case0(box, z_orders, central_axis);
1117             break;
1118         case 1:
1119             box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
1120             break;
1121         case 2:
1122         case 3:
1123             box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
1124             break;
1125         default:
1126         /*
1127          * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
1128          * joining the other two VPs. If this is the case, it determines the "central" corner from
1129          * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
1130          */
1131         // FIXME: We should eliminate the use of NR::Point altogether
1132         Box3D::Axis central_axis = Box3D::NONE;
1133         NR::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
1134         NR::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
1135         NR::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
1136         Geom::Point vpx(vp_x[NR::X], vp_x[NR::Y]);
1137         Geom::Point vpy(vp_y[NR::X], vp_y[NR::Y]);
1138         Geom::Point vpz(vp_z[NR::X], vp_z[NR::Y]);
1140         NR::Point c3 = box3d_get_corner_screen(box, 3);
1141         Geom::Point corner3(c3[NR::X], c3[NR::Y]);
1143         if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
1144             central_axis = Box3D::X;
1145         } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
1146             central_axis = Box3D::Y;
1147         } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
1148             central_axis = Box3D::Z;
1149         }
1150         //g_print ("Crossing: %s\n", Box3D::string_from_axes(central_axis));
1152         unsigned int central_corner = 3 ^ central_axis;
1153         if (central_axis == Box3D::Z) {
1154             central_corner = central_corner ^ Box3D::XYZ;
1155         }
1156         if (box3d_XY_axes_are_swapped(box)) {
1157             //g_print ("Axes X and Y are swapped\n");
1158             central_corner = central_corner ^ Box3D::XYZ;
1159         }
1161         NR::Point c1(box3d_get_corner_screen(box, 1));
1162         NR::Point c2(box3d_get_corner_screen(box, 2));
1163         NR::Point c7(box3d_get_corner_screen(box, 7));
1165         Geom::Point corner1(c1[NR::X], c1[NR::Y]);
1166         Geom::Point corner2(c2[NR::X], c2[NR::Y]);
1167         Geom::Point corner7(c7[NR::X], c7[NR::Y]);
1168         // FIXME: At present we don't use the information about central_corner computed above.
1169         switch (central_axis) {
1170             case Box3D::Y:
1171                 if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
1172                     box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1173                 } else {
1174                     // degenerate case
1175                     //g_print ("Degenerate case #1\n");
1176                     box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
1177                 }
1178                 break;
1180             case Box3D::Z:
1181                 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
1182                     // degenerate case
1183                     //g_print ("Degenerate case #2\n");
1184                     box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
1185                 } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
1186                     // degenerate case
1187                     //g_print ("Degenerate case #3\n");
1188                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
1189                 } else {
1190                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
1191                 }
1192                 break;
1194             case Box3D::X:
1195                 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
1196                     // degenerate case
1197                     //g_print ("Degenerate case #4\n");
1198                     box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
1199                 } else {
1200                     box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
1201                 }
1202                 break;
1204             case Box3D::NONE:
1205                 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1206                 break;
1208             default:
1209                 g_assert_not_reached();
1210                 break;
1211         } // end default case
1212     }
1214     // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
1215     //       instead of the hard-coded corner #3 in the computations above
1216     Box3D::Axis ev = box3d_everted_directions(box);
1217     for (int i = 0; i < 3; ++i) {
1218         if (ev & Box3D::axes[i]) {
1219             box3d_swap_sides(z_orders, Box3D::axes[i]);
1220         }
1221     }
1223     // Check whether anything actually changed
1224     for (int i = 0; i < 6; ++i) {
1225         if (box->z_orders[i] != z_orders[i]) {
1226             for (int j = i; j < 6; ++j) {
1227                 box->z_orders[j] = z_orders[j];
1228             }
1229             return true;
1230         }
1231     }
1232     return false;
1235 static std::map<int, Box3DSide *>
1236 box3d_get_sides (SPBox3D *box) {
1237     std::map<int, Box3DSide *> sides;
1238     for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
1239         sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
1240                                                            "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
1241     }
1242     sides.erase(-1);
1243     return sides;
1247 // TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
1248 void
1249 box3d_set_z_orders (SPBox3D *box) {
1250     // For efficiency reasons, we only set the new z-orders if something really changed
1251     if (box3d_recompute_z_orders (box)) {
1252         std::map<int, Box3DSide *> sides = box3d_get_sides(box);
1253         std::map<int, Box3DSide *>::iterator side;
1254         for (unsigned int i = 0; i < 6; ++i) {
1255             side = sides.find(box->z_orders[i]);
1256             if (side != sides.end()) {
1257                 SP_ITEM((*side).second)->lowerToBottom();
1258             }
1259         }
1260         /**
1261         g_print ("Resetting z-orders: ");
1262         for (int i = 0; i < 6; ++i) {
1263             g_print ("%d ", box->z_orders[i]);
1264         }
1265         g_print ("\n");
1266         **/
1267     }
1270 /*
1271  * Auxiliary function for z-order recomputing:
1272  * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
1273  * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
1274  * lies in the sector if it lies between the two (parallel) PLs.
1275  * \ret *  0 if \a pt doesn't lie in the sector
1276  *      *  1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
1277  *           from the edge between the two corners to \a pt points towards the VP
1278  *      * -1 otherwise
1279  */
1280 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
1281 //       can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
1282 int
1283 box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
1284     Persp3D *persp = box->persp_ref->getObject();
1286     // the two corners
1287     NR::Point c1(box3d_get_corner_screen(box, id1));
1288     NR::Point c2(box3d_get_corner_screen(box, id2));
1290     int ret = 0;
1291     if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
1292         NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
1293         NR::Point v1(c1 - vp);
1294         NR::Point v2(c2 - vp);
1295         NR::Point w(pt - vp);
1296         ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
1297         //g_print ("Case 0 - returning %d\n", ret);
1298     } else {
1299         Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
1300         Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
1301         if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
1302             // test whether pt lies "towards" or "away from" the VP
1303             Box3D::Line edge(c1,c2);
1304             NR::Point c3(box3d_get_corner_screen(box, id1 ^ axis));
1305             if (edge.lie_on_same_side(pt, c3)) {
1306                 ret = 1;
1307             } else {
1308                 ret = -1;
1309             }
1310         }
1311         //g_print ("Case 1 - returning %d\n", ret);
1312     }
1313     return ret;
1316 int
1317 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
1318     Persp3D *persp = box->persp_ref->getObject();
1320     if (!persp3d_VP_is_finite(persp, vpdir)) {
1321         return 0;
1322     } else {
1323         return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
1324     }
1327 /* swap the coordinates of corner0 and corner7 along the specified axis */
1328 static void
1329 box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
1330     box->orig_corner0.normalize();
1331     box->orig_corner7.normalize();
1332     if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1333         double tmp = box->orig_corner0[axis];
1334         box->orig_corner0[axis] = box->orig_corner7[axis];
1335         box->orig_corner7[axis] = tmp;
1336     }
1337     // FIXME: Should we also swap the coordinates of save_corner0 and save_corner7?
1340 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
1341 void
1342 box3d_relabel_corners(SPBox3D *box) {
1343     box3d_swap_coords(box, Proj::X, false);
1344     box3d_swap_coords(box, Proj::Y, false);
1345     box3d_swap_coords(box, Proj::Z, true);
1348 void
1349 box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp) {
1350     persp3d_remove_box (old_persp, box);
1351     persp3d_add_box (new_persp, box);
1352     gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
1353     SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
1354     g_free(href);
1358 /*
1359   Local Variables:
1360   mode:c++
1361   c-file-style:"stroustrup"
1362   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1363   indent-tabs-mode:nil
1364   fill-column:99
1365   End:
1366 */
1367 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :