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;
100 }
102 static void
103 box3d_init(SPBox3D *box)
104 {
105 box->persp_href = NULL;
106 box->persp_ref = new Persp3DReference(SP_OBJECT(box));
107 new (&box->modified_connection) sigc::connection();
108 }
110 static void
111 box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
112 {
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");
144 }
146 /**
147 * Virtual release of SPBox3D members before destruction.
148 */
149 static void
150 box3d_release(SPObject *object)
151 {
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 (box3d_get_perspective(box), box);
168 if (((SPObjectClass *) parent_class)->release)
169 ((SPObjectClass *) parent_class)->release(object);
170 }
172 static void
173 box3d_set(SPObject *object, unsigned int key, const gchar *value)
174 {
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?
234 }
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)
241 {
242 if (old_ref) {
243 sp_signal_disconnect_by_data(old_ref, box);
244 persp3d_remove_box (SP_PERSP3D(old_ref), box);
245 /* Note: This sometimes leads to attempts to remove boxes twice from the list of selected/transformed
246 boxes in a perspectives, but this should be uncritical. */
247 persp3d_remove_box_transform (SP_PERSP3D(old_ref), box);
248 }
249 if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
250 {
251 box->modified_connection.disconnect();
252 box->modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&box3d_ref_modified), box));
253 box3d_ref_modified(ref, 0, box);
254 persp3d_add_box (SP_PERSP3D(ref), box);
255 /* Note: This sometimes leads to attempts to add boxes twice to the list of selected/transformed
256 boxes in a perspectives, but this should be uncritical. */
257 persp3d_add_box_transform (SP_PERSP3D(ref), box);
258 }
259 }
261 static void
262 box3d_update(SPObject *object, SPCtx *ctx, guint flags)
263 {
264 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
266 /* FIXME?: Perhaps the display updates of box sides should be instantiated from here, but this
267 causes evil update loops so it's all done from box3d_position_set, which is called from
268 various other places (like the handlers in object-edit.cpp, vanishing-point.cpp, etc. */
270 }
272 // Invoke parent method
273 if (((SPObjectClass *) (parent_class))->update)
274 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
275 }
278 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
279 {
280 SPBox3D *box = SP_BOX3D(object);
282 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
283 // this is where we end up when saving as plain SVG (also in other circumstances?)
284 // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:g
285 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
286 repr = xml_doc->createElement("svg:g");
287 }
289 if (flags & SP_OBJECT_WRITE_EXT) {
291 if (box->persp_href) {
292 repr->setAttribute("inkscape:perspectiveID", box->persp_href);
293 } else {
294 /* box is not yet linked to a perspective; use the document's current perspective */
295 SPDocument *doc = inkscape_active_document();
296 if (box->persp_ref->getURI()) {
297 gchar *uri_string = box->persp_ref->getURI()->toString();
298 repr->setAttribute("inkscape:perspectiveID", uri_string);
299 g_free(uri_string);
300 } else if (doc) {
301 //persp3d_add_box (doc->current_persp3d, box);
302 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
303 const gchar *persp_id = persp_repr->attribute("id");
304 gchar *href = g_strdup_printf("#%s", persp_id);
305 repr->setAttribute("inkscape:perspectiveID", href);
306 g_free(href);
307 } else {
308 g_print ("No active document while creating perspective!!!\n");
309 }
310 }
312 gchar *coordstr0 = box->orig_corner0.coord_string();
313 gchar *coordstr7 = box->orig_corner7.coord_string();
314 repr->setAttribute("inkscape:corner0", coordstr0);
315 repr->setAttribute("inkscape:corner7", coordstr7);
316 g_free(coordstr0);
317 g_free(coordstr7);
319 box->orig_corner0.normalize();
320 box->orig_corner7.normalize();
322 box->save_corner0 = box->orig_corner0;
323 box->save_corner7 = box->orig_corner7;
324 }
326 if (((SPObjectClass *) (parent_class))->write) {
327 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
328 }
330 return repr;
331 }
333 static gchar *
334 box3d_description(SPItem *item)
335 {
336 g_return_val_if_fail(SP_IS_BOX3D(item), NULL);
338 return g_strdup(_("<b>3D Box</b>"));
339 }
341 void
342 box3d_position_set (SPBox3D *box)
343 {
344 /* This draws the curve and calls requestDisplayUpdate() for each side (the latter is done in
345 box3d_side_position_set() to avoid update conflicts with the parent box) */
346 for (SPObject *child = sp_object_first_child(SP_OBJECT (box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
347 box3d_side_position_set (SP_BOX3D_SIDE (child));
348 }
349 }
351 static NR::Matrix
352 box3d_set_transform(SPItem *item, NR::Matrix const &xform)
353 {
354 SPBox3D *box = SP_BOX3D(item);
356 /* check whether we need to unlink any boxes from their perspectives */
357 Persp3D *persp = box3d_get_perspective(box);
358 Persp3D *transf_persp;
360 if (!persp3d_has_all_boxes_in_selection (persp)) {
361 std::list<SPBox3D *> sel = persp3d_selected_boxes (persp);
363 /* create a new perspective as a copy of the current one and link the selected boxes to it */
364 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
366 for (std::list<SPBox3D *>::iterator b = sel.begin(); b != sel.end(); ++b) {
367 box3d_switch_perspectives(*b, persp, transf_persp);
368 }
369 } else {
370 transf_persp = persp;
371 }
373 /* only transform the perspective once, even if it has several selected boxes */
374 if(!persp3d_was_transformed (transf_persp)) {
375 /* concatenate the affine transformation with the perspective mapping; this
376 function also triggers repr updates of boxes and the perspective itself */
377 persp3d_apply_affine_transformation(transf_persp, xform);
378 }
380 box3d_mark_transformed(box);
382 if (persp3d_all_transformed(transf_persp)) {
383 /* all boxes were transformed; make perspective sensitive for further transformations */
384 persp3d_unset_transforms(transf_persp);
385 }
387 NR::Matrix ret(NR::transform(xform));
388 gdouble const sw = hypot(ret[0], ret[1]);
389 gdouble const sh = hypot(ret[2], ret[3]);
391 SPItem *sideitem = NULL;
392 for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
393 sideitem = SP_ITEM(side);
395 // Adjust stroke width
396 sp_item_adjust_stroke(sideitem, sqrt(fabs(sw * sh)));
398 // Adjust pattern fill
399 sp_item_adjust_pattern(sideitem, xform);
401 // Adjust gradient fill
402 sp_item_adjust_gradient(sideitem, xform);
404 // Adjust LPE
405 sp_item_adjust_livepatheffect(item, xform);
406 }
408 return NR::identity();
409 }
412 /**
413 * Gets called when persp(?) repr contents change: i.e. parameter change.
414 */
415 static void
416 box3d_ref_modified(SPObject */*href*/, guint /*flags*/, SPBox3D */*box*/)
417 {
418 /***
419 g_print ("FIXME: box3d_ref_modified was called. What should we do?\n");
420 g_print ("Here is at least the the href's id: %s\n", SP_OBJECT_REPR(href)->attribute("id"));
421 g_print (" ... and the box's, too: %s\n", SP_OBJECT_REPR(box)->attribute("id"));
422 ***/
424 }
426 Proj::Pt3
427 box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
428 return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
429 (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
430 (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
431 1.0);
432 }
434 Proj::Pt3
435 box3d_get_proj_corner (SPBox3D const *box, guint id) {
436 return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
437 (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
438 (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
439 1.0);
440 }
442 NR::Point
443 box3d_get_corner_screen (SPBox3D const *box, guint id) {
444 Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
445 if (!box3d_get_perspective(box)) {
446 //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
447 return NR::Point (NR_HUGE, NR_HUGE);
448 }
449 return box3d_get_perspective(box)->tmat.image(proj_corner).affine();
450 }
452 Proj::Pt3
453 box3d_get_proj_center (SPBox3D *box) {
454 box->orig_corner0.normalize();
455 box->orig_corner7.normalize();
456 return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
457 (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
458 (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
459 1.0);
460 }
462 NR::Point
463 box3d_get_center_screen (SPBox3D *box) {
464 Proj::Pt3 proj_center (box3d_get_proj_center (box));
465 if (!box3d_get_perspective(box)) {
466 //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
467 return NR::Point (NR_HUGE, NR_HUGE);
468 }
469 return box3d_get_perspective(box)->tmat.image(proj_center).affine();
470 }
472 /*
473 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
474 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
475 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
476 */
478 // Should we make the threshold settable in the preferences?
479 static double remember_snap_threshold = 30;
480 //static guint remember_snap_index = 0;
481 static guint remember_snap_index_center = 0;
483 static Proj::Pt3
484 box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
485 double z_coord = start_pt[Proj::Z];
486 double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
487 double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
488 double x_coord = start_pt[Proj::X];
489 double y_coord = start_pt[Proj::Y];
490 Proj::Pt3 A_proj (x_coord, y_coord, z_coord, 1.0);
491 Proj::Pt3 B_proj (x_coord + diff_x, y_coord, z_coord, 1.0);
492 Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
493 Proj::Pt3 D_proj (x_coord, y_coord + diff_y, z_coord, 1.0);
494 Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
496 Persp3D *persp = box3d_get_perspective(box);
497 NR::Point A = persp->tmat.image(A_proj).affine();
498 NR::Point B = persp->tmat.image(B_proj).affine();
499 NR::Point C = persp->tmat.image(C_proj).affine();
500 NR::Point D = persp->tmat.image(D_proj).affine();
501 NR::Point E = persp->tmat.image(E_proj).affine();
502 NR::Point pt = persp->tmat.image(pt_proj).affine();
504 // TODO: Replace these lines between corners with lines from a corner to a vanishing point
505 // (this might help to prevent rounding errors if the box is small)
506 Box3D::Line pl1(A, B);
507 Box3D::Line pl2(A, D);
508 Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
509 Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
511 int num_snap_lines = (id != -1) ? 3 : 4;
512 NR::Point snap_pts[num_snap_lines];
514 snap_pts[0] = pl1.closest_to (pt);
515 snap_pts[1] = pl2.closest_to (pt);
516 snap_pts[2] = diag1.closest_to (pt);
517 if (id == -1) {
518 snap_pts[3] = diag2.closest_to (pt);
519 }
521 gdouble const zoom = inkscape_active_desktop()->current_zoom();
523 // determine the distances to all potential snapping points
524 double snap_dists[num_snap_lines];
525 for (int i = 0; i < num_snap_lines; ++i) {
526 snap_dists[i] = NR::L2 (snap_pts[i] - pt) * zoom;
527 }
529 // while we are within a given tolerance of the starting point,
530 // keep snapping to the same point to avoid jumping
531 bool within_tolerance = true;
532 for (int i = 0; i < num_snap_lines; ++i) {
533 if (snap_dists[i] > remember_snap_threshold) {
534 within_tolerance = false;
535 break;
536 }
537 }
539 // find the closest snapping point
540 int snap_index = -1;
541 double snap_dist = NR_HUGE;
542 for (int i = 0; i < num_snap_lines; ++i) {
543 if (snap_dists[i] < snap_dist) {
544 snap_index = i;
545 snap_dist = snap_dists[i];
546 }
547 }
549 // snap to the closest point (or the previously remembered one
550 // if we are within tolerance of the starting point)
551 NR::Point result;
552 if (within_tolerance) {
553 result = snap_pts[remember_snap_index_center];
554 } else {
555 remember_snap_index_center = snap_index;
556 result = snap_pts[snap_index];
557 }
558 return box3d_get_perspective(box)->tmat.preimage (result, z_coord, Proj::Z);
559 }
561 void
562 box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
563 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
565 box->orig_corner0.normalize();
566 box->orig_corner7.normalize();
568 /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
569 if (!(movement & Box3D::Z)) {
570 Proj::Pt3 pt_proj (box3d_get_perspective(box)->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
571 box->orig_corner7[Proj::Z], Proj::Z));
572 if (constrained) {
573 pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
574 }
576 // normalizing pt_proj is essential because we want to mingle affine coordinates
577 pt_proj.normalize();
578 box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
579 (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
580 box->save_corner0[Proj::Z],
581 1.0);
582 box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
583 (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
584 box->save_corner7[Proj::Z],
585 1.0);
586 } else {
587 Persp3D *persp = box3d_get_perspective(box);
588 Box3D::PerspectiveLine pl(persp->tmat.image(
589 box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
590 Proj::Z, persp);
591 NR::Point new_pos_snapped(pl.closest_to(new_pos));
592 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped,
593 box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
594 (movement & Box3D::Y) ? Proj::X : Proj::Y));
595 bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
596 bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
597 bool corner7_move_x = (id & Box3D::X) && (movement & Box3D::X);
598 bool corner7_move_y = (id & Box3D::Y) && (movement & Box3D::Y);
599 // normalizing pt_proj is essential because we want to mingle affine coordinates
600 pt_proj.normalize();
601 box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
602 corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
603 (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
604 1.0);
605 box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
606 corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
607 (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
608 1.0);
609 }
610 // FIXME: Should we update the box here? If so, how?
611 }
613 void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
614 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
616 Persp3D *persp = box3d_get_perspective(box);
617 if (!(movement & Box3D::Z)) {
618 double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
619 double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
620 double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
622 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos, coord, Proj::Z));
623 if (constrained) {
624 Proj::Pt3 old_pos_proj (persp->tmat.preimage (old_pos, coord, Proj::Z));
625 pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
626 }
627 // normalizing pt_proj is essential because we want to mingle affine coordinates
628 pt_proj.normalize();
629 box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
630 (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
631 box->orig_corner0[Proj::Z],
632 1.0);
633 box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
634 (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
635 box->orig_corner7[Proj::Z],
636 1.0);
637 } else {
638 double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
639 double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
641 Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
642 NR::Point new_pos_snapped(pl.closest_to(new_pos));
643 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped, coord, Proj::X));
645 /* normalizing pt_proj is essential because we want to mingle affine coordinates */
646 pt_proj.normalize();
647 box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
648 box->orig_corner0[Proj::Y],
649 pt_proj[Proj::Z] - radz,
650 1.0);
651 box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
652 box->orig_corner7[Proj::Y],
653 pt_proj[Proj::Z] + radz,
654 1.0);
655 }
656 }
658 /*
659 * Manipulates corner1 through corner4 to contain the indices of the corners
660 * from which the perspective lines in the direction of 'axis' emerge
661 */
662 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
663 NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
664 {
665 Persp3D *persp = box3d_get_perspective(box);
666 g_return_if_fail (persp);
667 //box->orig_corner0.normalize();
668 //box->orig_corner7.normalize();
669 double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
670 box->orig_corner0[axis] :
671 box->orig_corner7[axis];
673 Proj::Pt3 c1, c2, c3, c4;
674 // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
675 switch (axis) {
676 case Proj::X:
677 c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
678 c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
679 c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
680 c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
681 break;
682 case Proj::Y:
683 c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
684 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
685 c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
686 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
687 break;
688 case Proj::Z:
689 c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
690 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
691 c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
692 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
693 break;
694 default:
695 return;
696 }
697 corner1 = persp->tmat.image(c1).affine();
698 corner2 = persp->tmat.image(c2).affine();
699 corner3 = persp->tmat.image(c3).affine();
700 corner4 = persp->tmat.image(c4).affine();
701 }
703 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
704 static bool
705 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
706 Geom::Point const &C, Geom::Point const &D) {
707 Geom::Point E; // the point of intersection
708 Geom::Point n0 = (B - A).ccw();
709 double d0 = dot(n0,A);
711 Geom::Point n1 = (D - C).ccw();
712 double d1 = dot(n1,C);
713 Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
714 if (intersects == Geom::coincident || intersects == Geom::parallel) {
715 return false;
716 }
718 if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
719 // C and D lie on the same side of the line AB
720 return false;
721 }
722 if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
723 // A and B lie on different sides of the line CD
724 return true;
725 } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
726 // The line CD passes on the "wrong" side of A
727 return false;
728 }
730 // The line CD passes on the "correct" side of A
731 return true;
732 }
734 static bool
735 box3d_XY_axes_are_swapped (SPBox3D *box) {
736 Persp3D *persp = box3d_get_perspective(box);
737 g_return_val_if_fail(persp, false);
738 Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3), Proj::X, persp);
739 Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3), Proj::Y, persp);
740 NR::Point v1(l1.direction());
741 NR::Point v2(l2.direction());
742 v1.normalize();
743 v2.normalize();
745 return (v1[NR::X]*v2[NR::Y] - v1[NR::Y]*v2[NR::X] > 0);
746 }
748 static inline void
749 box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
750 z_orders[0] = a;
751 z_orders[1] = b;
752 z_orders[2] = c;
753 z_orders[3] = d;
754 z_orders[4] = e;
755 z_orders[5] = f;
756 }
758 static inline void
759 box3d_swap_z_orders (int z_orders[6]) {
760 int tmp;
761 for (int i = 0; i < 3; ++i) {
762 tmp = z_orders[i];
763 z_orders[i] = z_orders[5-i];
764 z_orders[5-i] = tmp;
765 }
766 }
768 /*
769 * In der Standard-Perspektive:
770 * 2 = vorne
771 * 1 = oben
772 * 0 = links
773 * 3 = rechts
774 * 4 = unten
775 * 5 = hinten
776 */
778 /* All VPs infinite */
779 static void
780 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
781 Persp3D *persp = box3d_get_perspective(box);
782 NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
783 NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
784 NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
786 bool swapped = box3d_XY_axes_are_swapped(box);
788 //g_print ("3 infinite VPs; ");
789 switch(central_axis) {
790 case Box3D::X:
791 if (!swapped) {
792 //g_print ("central axis X (case a)");
793 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
794 } else {
795 //g_print ("central axis X (case b)");
796 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
797 }
798 break;
799 case Box3D::Y:
800 if (!swapped) {
801 //g_print ("central axis Y (case a)");
802 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
803 } else {
804 //g_print ("central axis Y (case b)");
805 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
806 }
807 break;
808 case Box3D::Z:
809 if (!swapped) {
810 //g_print ("central axis Z (case a)");
811 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
812 } else {
813 //g_print ("central axis Z (case b)");
814 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
815 }
816 break;
817 case Box3D::NONE:
818 if (!swapped) {
819 //g_print ("central axis NONE (case a)");
820 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
821 } else {
822 //g_print ("central axis NONE (case b)");
823 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
824 }
825 break;
826 default:
827 g_assert_not_reached();
828 break;
829 }
830 /**
831 if (swapped) {
832 g_print ("; swapped");
833 }
834 g_print ("\n");
835 **/
836 }
838 /* Precisely one finite VP */
839 static void
840 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
841 Persp3D *persp = box3d_get_perspective(box);
842 NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
844 // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
845 Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
846 Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
847 //g_print ("oaxis1 = %s, oaxis2 = %s\n", Box3D::string_from_axes(oaxis1), Box3D::string_from_axes(oaxis2));
848 int inside1 = 0;
849 int inside2 = 0;
850 inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
851 inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
852 //g_print ("inside1 = %d, inside2 = %d\n", inside1, inside2);
854 bool swapped = box3d_XY_axes_are_swapped(box);
856 //g_print ("2 infinite VPs; ");
857 //g_print ("finite axis: %s; ", Box3D::string_from_axes(fin_axis));
858 switch(central_axis) {
859 case Box3D::X:
860 if (!swapped) {
861 //g_print ("central axis X (case a)");
862 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
863 } else {
864 //if (inside2) {
865 //g_print ("central axis X (case b)");
866 box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
867 //} else {
868 //g_print ("central axis X (case c)");
869 //box3d_aux_set_z_orders (z_orders, 5, 3, 1, 2, 0, 4);
870 //}
871 }
872 break;
873 case Box3D::Y:
874 if (inside2 > 0) {
875 //g_print ("central axis Y (case a)");
876 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
877 } else if (inside2 < 0) {
878 //g_print ("central axis Y (case b)");
879 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
880 } else {
881 if (!swapped) {
882 //g_print ("central axis Y (case c1)");
883 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
884 } else {
885 //g_print ("central axis Y (case c2)");
886 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
887 }
888 }
889 break;
890 case Box3D::Z:
891 if (inside2) {
892 if (!swapped) {
893 //g_print ("central axis Z (case a1)");
894 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
895 } else {
896 //g_print ("central axis Z (case a2)");
897 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
898 }
899 } else if (inside1) {
900 if (!swapped) {
901 //g_print ("central axis Z (case b1)");
902 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
903 } else {
904 //g_print ("central axis Z (case b2)");
905 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
906 //box3d_aux_set_z_orders (z_orders, 5, 3, 0, 1, 2, 4);
907 }
908 } else {
909 // "regular" case
910 if (!swapped) {
911 //g_print ("central axis Z (case c1)");
912 box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
913 } else {
914 //g_print ("central axis Z (case c2)");
915 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
916 //box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
917 }
918 }
919 break;
920 case Box3D::NONE:
921 if (!swapped) {
922 //g_print ("central axis NONE (case a)");
923 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
924 } else {
925 //g_print ("central axis NONE (case b)");
926 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
927 //box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
928 }
929 break;
930 default:
931 g_assert_not_reached();
932 }
933 /**
934 if (swapped) {
935 g_print ("; swapped");
936 }
937 g_print ("\n");
938 **/
939 }
941 /* Precisely 2 finite VPs */
942 static void
943 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
944 Persp3D *persp = box3d_get_perspective(box);
946 NR::Point c3(box3d_get_corner_screen(box, 3));
947 NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
948 NR::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
949 NR::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
951 bool swapped = box3d_XY_axes_are_swapped(box);
953 int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
954 int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
956 int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
957 int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
959 int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
960 int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
962 //g_print ("Insides: xy = %d, xz = %d, yx = %d, yz = %d, zx = %d, zy = %d\n",
963 // insidexy, insidexz, insideyx, insideyz, insidezx, insidezy);
964 (void)insidexz;
965 (void)insideyx;
966 (void)insidezx;
968 //g_print ("1 infinite VP; ");
969 switch(central_axis) {
970 case Box3D::X:
971 if (!swapped) {
972 if (insidezy == -1) {
973 //g_print ("central axis X (case a1)");
974 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
975 } else if (insidexy == 1) {
976 //g_print ("central axis X (case a2)");
977 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
978 } else {
979 //g_print ("central axis X (case a3)");
980 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
981 }
982 } else {
983 if (insideyz == -1) {
984 //g_print ("central axis X (case b1)");
985 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
986 } else {
987 if (!swapped) {
988 //g_print ("central axis X (case b2)");
989 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
990 } else {
991 //g_print ("central axis X (case b3)");
992 box3d_aux_set_z_orders (z_orders, 1, 3, 5, 0, 2, 4);
993 }
994 }
995 }
996 break;
997 case Box3D::Y:
998 if (!swapped) {
999 if (insideyz == 1) {
1000 //g_print ("central axis Y (case a1)");
1001 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
1002 } else {
1003 //g_print ("central axis Y (case a2)");
1004 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1005 }
1006 } else {
1007 //g_print ("central axis Y (case b)");
1008 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
1009 }
1010 break;
1011 case Box3D::Z:
1012 if (!swapped) {
1013 if (insidezy == 1) {
1014 //g_print ("central axis Z (case a1)");
1015 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
1016 } else if (insidexy == -1) {
1017 //g_print ("central axis Z (case a2)");
1018 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
1019 } else {
1020 //g_print ("central axis Z (case a3)");
1021 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
1022 }
1023 } else {
1024 //g_print ("central axis Z (case b)");
1025 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
1026 }
1027 break;
1028 case Box3D::NONE:
1029 if (!swapped) {
1030 //g_print ("central axis NONE (case a)");
1031 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1032 } else {
1033 //g_print ("central axis NONE (case b)");
1034 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
1035 }
1036 break;
1037 default:
1038 g_assert_not_reached();
1039 break;
1040 }
1041 /**
1042 if (swapped) {
1043 g_print ("; swapped");
1044 }
1045 g_print ("\n");
1046 **/
1047 }
1049 /*
1050 * It can happen that during dragging the box is everted.
1051 * In this case the opposite sides in this direction need to be swapped
1052 */
1053 static Box3D::Axis
1054 box3d_everted_directions (SPBox3D *box) {
1055 Box3D::Axis ev = Box3D::NONE;
1057 box->orig_corner0.normalize();
1058 box->orig_corner7.normalize();
1060 if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
1061 ev = (Box3D::Axis) (ev ^ Box3D::X);
1062 if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
1063 ev = (Box3D::Axis) (ev ^ Box3D::Y);
1064 if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
1065 ev = (Box3D::Axis) (ev ^ Box3D::Z);
1067 return ev;
1068 }
1070 static void
1071 box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
1072 int pos1 = -1;
1073 int pos2 = -1;
1075 for (int i = 0; i < 6; ++i) {
1076 if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
1077 if (pos1 == -1) {
1078 pos1 = i;
1079 } else {
1080 pos2 = i;
1081 break;
1082 }
1083 }
1084 }
1086 int tmp = z_orders[pos1];
1087 z_orders[pos1] = z_orders[pos2];
1088 z_orders[pos2] = tmp;
1089 }
1092 bool
1093 box3d_recompute_z_orders (SPBox3D *box) {
1094 Persp3D *persp = box3d_get_perspective(box);
1096 //g_return_val_if_fail(persp, false);
1097 if (!persp)
1098 return false;
1100 int z_orders[6];
1102 NR::Point c3(box3d_get_corner_screen(box, 3));
1104 // determine directions from corner3 to the VPs
1105 int num_finite = 0;
1106 Box3D::Axis axis_finite = Box3D::NONE;
1107 Box3D::Axis axis_infinite = Box3D::NONE;
1108 NR::Point dirs[3];
1109 for (int i = 0; i < 3; ++i) {
1110 dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
1111 if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
1112 num_finite++;
1113 axis_finite = Box3D::axes[i];
1114 } else {
1115 axis_infinite = Box3D::axes[i];
1116 }
1117 }
1119 // determine the "central" axis (if there is one)
1120 Box3D::Axis central_axis = Box3D::NONE;
1121 if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
1122 central_axis = Box3D::Z;
1123 } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
1124 central_axis = Box3D::X;
1125 } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
1126 central_axis = Box3D::Y;
1127 }
1129 switch (num_finite) {
1130 case 0:
1131 // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
1132 // coincide, hence only need to be computed once in a more central location.
1133 box3d_set_new_z_orders_case0(box, z_orders, central_axis);
1134 break;
1135 case 1:
1136 box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
1137 break;
1138 case 2:
1139 case 3:
1140 box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
1141 break;
1142 default:
1143 /*
1144 * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
1145 * joining the other two VPs. If this is the case, it determines the "central" corner from
1146 * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
1147 */
1148 // FIXME: We should eliminate the use of NR::Point altogether
1149 Box3D::Axis central_axis = Box3D::NONE;
1150 NR::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
1151 NR::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
1152 NR::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
1153 Geom::Point vpx(vp_x[NR::X], vp_x[NR::Y]);
1154 Geom::Point vpy(vp_y[NR::X], vp_y[NR::Y]);
1155 Geom::Point vpz(vp_z[NR::X], vp_z[NR::Y]);
1157 NR::Point c3 = box3d_get_corner_screen(box, 3);
1158 Geom::Point corner3(c3[NR::X], c3[NR::Y]);
1160 if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
1161 central_axis = Box3D::X;
1162 } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
1163 central_axis = Box3D::Y;
1164 } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
1165 central_axis = Box3D::Z;
1166 }
1167 //g_print ("Crossing: %s\n", Box3D::string_from_axes(central_axis));
1169 unsigned int central_corner = 3 ^ central_axis;
1170 if (central_axis == Box3D::Z) {
1171 central_corner = central_corner ^ Box3D::XYZ;
1172 }
1173 if (box3d_XY_axes_are_swapped(box)) {
1174 //g_print ("Axes X and Y are swapped\n");
1175 central_corner = central_corner ^ Box3D::XYZ;
1176 }
1178 NR::Point c1(box3d_get_corner_screen(box, 1));
1179 NR::Point c2(box3d_get_corner_screen(box, 2));
1180 NR::Point c7(box3d_get_corner_screen(box, 7));
1182 Geom::Point corner1(c1[NR::X], c1[NR::Y]);
1183 Geom::Point corner2(c2[NR::X], c2[NR::Y]);
1184 Geom::Point corner7(c7[NR::X], c7[NR::Y]);
1185 // FIXME: At present we don't use the information about central_corner computed above.
1186 switch (central_axis) {
1187 case Box3D::Y:
1188 if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
1189 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1190 } else {
1191 // degenerate case
1192 //g_print ("Degenerate case #1\n");
1193 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
1194 }
1195 break;
1197 case Box3D::Z:
1198 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
1199 // degenerate case
1200 //g_print ("Degenerate case #2\n");
1201 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
1202 } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
1203 // degenerate case
1204 //g_print ("Degenerate case #3\n");
1205 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
1206 } else {
1207 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
1208 }
1209 break;
1211 case Box3D::X:
1212 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
1213 // degenerate case
1214 //g_print ("Degenerate case #4\n");
1215 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
1216 } else {
1217 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
1218 }
1219 break;
1221 case Box3D::NONE:
1222 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1223 break;
1225 default:
1226 g_assert_not_reached();
1227 break;
1228 } // end default case
1229 }
1231 // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
1232 // instead of the hard-coded corner #3 in the computations above
1233 Box3D::Axis ev = box3d_everted_directions(box);
1234 for (int i = 0; i < 3; ++i) {
1235 if (ev & Box3D::axes[i]) {
1236 box3d_swap_sides(z_orders, Box3D::axes[i]);
1237 }
1238 }
1240 // Check whether anything actually changed
1241 for (int i = 0; i < 6; ++i) {
1242 if (box->z_orders[i] != z_orders[i]) {
1243 for (int j = i; j < 6; ++j) {
1244 box->z_orders[j] = z_orders[j];
1245 }
1246 return true;
1247 }
1248 }
1249 return false;
1250 }
1252 static std::map<int, Box3DSide *>
1253 box3d_get_sides (SPBox3D *box) {
1254 std::map<int, Box3DSide *> sides;
1255 for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
1256 sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
1257 "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
1258 }
1259 sides.erase(-1);
1260 return sides;
1261 }
1264 // TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
1265 void
1266 box3d_set_z_orders (SPBox3D *box) {
1267 // For efficiency reasons, we only set the new z-orders if something really changed
1268 if (box3d_recompute_z_orders (box)) {
1269 std::map<int, Box3DSide *> sides = box3d_get_sides(box);
1270 std::map<int, Box3DSide *>::iterator side;
1271 for (unsigned int i = 0; i < 6; ++i) {
1272 side = sides.find(box->z_orders[i]);
1273 if (side != sides.end()) {
1274 SP_ITEM((*side).second)->lowerToBottom();
1275 }
1276 }
1277 /**
1278 g_print ("Resetting z-orders: ");
1279 for (int i = 0; i < 6; ++i) {
1280 g_print ("%d ", box->z_orders[i]);
1281 }
1282 g_print ("\n");
1283 **/
1284 }
1285 }
1287 /*
1288 * Auxiliary function for z-order recomputing:
1289 * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
1290 * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
1291 * lies in the sector if it lies between the two (parallel) PLs.
1292 * \ret * 0 if \a pt doesn't lie in the sector
1293 * * 1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
1294 * from the edge between the two corners to \a pt points towards the VP
1295 * * -1 otherwise
1296 */
1297 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
1298 // can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
1299 int
1300 box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
1301 Persp3D *persp = box3d_get_perspective(box);
1303 // the two corners
1304 NR::Point c1(box3d_get_corner_screen(box, id1));
1305 NR::Point c2(box3d_get_corner_screen(box, id2));
1307 int ret = 0;
1308 if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
1309 NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
1310 NR::Point v1(c1 - vp);
1311 NR::Point v2(c2 - vp);
1312 NR::Point w(pt - vp);
1313 ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
1314 //g_print ("Case 0 - returning %d\n", ret);
1315 } else {
1316 Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
1317 Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
1318 if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
1319 // test whether pt lies "towards" or "away from" the VP
1320 Box3D::Line edge(c1,c2);
1321 NR::Point c3(box3d_get_corner_screen(box, id1 ^ axis));
1322 if (edge.lie_on_same_side(pt, c3)) {
1323 ret = 1;
1324 } else {
1325 ret = -1;
1326 }
1327 }
1328 //g_print ("Case 1 - returning %d\n", ret);
1329 }
1330 return ret;
1331 }
1333 int
1334 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
1335 Persp3D *persp = box3d_get_perspective(box);
1337 if (!persp3d_VP_is_finite(persp, vpdir)) {
1338 return 0;
1339 } else {
1340 return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
1341 }
1342 }
1344 /* swap the coordinates of corner0 and corner7 along the specified axis */
1345 static void
1346 box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
1347 box->orig_corner0.normalize();
1348 box->orig_corner7.normalize();
1349 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1350 double tmp = box->orig_corner0[axis];
1351 box->orig_corner0[axis] = box->orig_corner7[axis];
1352 box->orig_corner7[axis] = tmp;
1353 }
1354 // FIXME: Should we also swap the coordinates of save_corner0 and save_corner7?
1355 }
1357 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
1358 void
1359 box3d_relabel_corners(SPBox3D *box) {
1360 box3d_swap_coords(box, Proj::X, false);
1361 box3d_swap_coords(box, Proj::Y, false);
1362 box3d_swap_coords(box, Proj::Z, true);
1363 }
1365 static void
1366 box3d_check_for_swapped_coords(SPBox3D *box, Proj::Axis axis, bool smaller) {
1367 box->orig_corner0.normalize();
1368 box->orig_corner7.normalize();
1370 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1371 box->swapped = (Box3D::Axis) (box->swapped | Proj::toAffine(axis));
1372 } else {
1373 box->swapped = (Box3D::Axis) (box->swapped & ~Proj::toAffine(axis));
1374 }
1375 }
1377 static void
1378 box3d_exchange_coords(SPBox3D *box) {
1379 box->orig_corner0.normalize();
1380 box->orig_corner7.normalize();
1382 for (int i = 0; i < 3; ++i) {
1383 if (box->swapped & Box3D::axes[i]) {
1384 double tmp = box->orig_corner0[i];
1385 box->orig_corner0[i] = box->orig_corner7[i];
1386 box->orig_corner7[i] = tmp;
1387 }
1388 }
1389 }
1391 void
1392 box3d_check_for_swapped_coords(SPBox3D *box) {
1393 box3d_check_for_swapped_coords(box, Proj::X, false);
1394 box3d_check_for_swapped_coords(box, Proj::Y, false);
1395 box3d_check_for_swapped_coords(box, Proj::Z, true);
1397 box3d_exchange_coords(box);
1398 }
1400 void
1401 box3d_add_to_selection(SPBox3D *box) {
1402 Persp3D *persp = box3d_get_perspective(box);
1403 g_return_if_fail(persp);
1404 persp3d_add_box_transform(persp, box);
1405 }
1407 void
1408 box3d_remove_from_selection(SPBox3D *box) {
1409 Persp3D *persp = box3d_get_perspective(box);
1410 if (!persp) {
1411 /* this can happen if a box is deleted through undo and the persp_ref is already detached;
1412 should we rebuild the boxes of each perspective in this case or is it safe to leave it alone? */
1413 return;
1414 }
1415 persp3d_remove_box_transform(persp, box);
1416 }
1418 void
1419 box3d_mark_transformed(SPBox3D *box) {
1420 Persp3D *persp = box3d_get_perspective(box);
1421 g_return_if_fail(persp);
1422 persp3d_set_box_transformed(persp, box, true);
1423 }
1425 Persp3D *
1426 box3d_get_perspective(SPBox3D const *box) {
1427 return box->persp_ref->getObject();
1428 }
1430 void
1431 box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp, bool recompute_corners) {
1432 if (recompute_corners) {
1433 box->orig_corner0.normalize();
1434 box->orig_corner7.normalize();
1435 double z0 = box->orig_corner0[Proj::Z];
1436 double z7 = box->orig_corner7[Proj::Z];
1437 NR::Point corner0_screen = box3d_get_corner_screen(box, 0);
1438 NR::Point corner7_screen = box3d_get_corner_screen(box, 7);
1440 box->orig_corner0 = new_persp->tmat.preimage(corner0_screen, z0, Proj::Z);
1441 box->orig_corner7 = new_persp->tmat.preimage(corner7_screen, z7, Proj::Z);
1442 }
1444 persp3d_remove_box (old_persp, box);
1445 persp3d_add_box (new_persp, box);
1447 persp3d_remove_box_transform (old_persp, box);
1448 persp3d_add_box_transform (new_persp, box);
1450 gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
1451 SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
1452 g_free(href);
1453 }
1455 /* Converts the 3D box to an ordinary SPGroup, adds it to the XML tree at the same position as
1456 the original box and deletes the latter */
1457 // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
1458 SPGroup *
1459 box3d_convert_to_group(SPBox3D *box) {
1460 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
1461 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1463 // remember position of the box
1464 int pos = SP_OBJECT_REPR(box)->position();
1466 // create a new group and add the sides (converted to ordinary paths) as its children
1467 Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
1469 Inkscape::XML::Node *repr;
1470 for (SPObject *child = sp_object_first_child(SP_OBJECT(box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
1471 if (SP_IS_BOX3D_SIDE(child)) {
1472 repr = box3d_side_convert_to_path(SP_BOX3D_SIDE(child));
1473 grepr->appendChild(repr);
1474 } else {
1475 g_warning("Non-side object encountered as child of a 3D box.\n");
1476 }
1477 }
1479 // add the new group to the box's parent and set remembered position
1480 SPObject *parent = SP_OBJECT_PARENT(box);
1481 SP_OBJECT_REPR(parent)->appendChild(grepr);
1482 grepr->setPosition(pos);
1484 SP_OBJECT(box)->deleteObject(true);
1486 return SP_GROUP(doc->getObjectByRepr(grepr));
1487 }
1489 /*
1490 Local Variables:
1491 mode:c++
1492 c-file-style:"stroustrup"
1493 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1494 indent-tabs-mode:nil
1495 fill-column:99
1496 End:
1497 */
1498 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :