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"
22 #include "libnr/nr-matrix-fns.h"
24 #include "box3d.h"
25 #include "box3d-side.h"
26 #include "box3d-context.h"
27 #include "proj_pt.h"
28 #include "transf_mat_3x4.h"
29 #include "perspective-line.h"
30 #include "inkscape.h"
31 #include "persp3d.h"
32 #include "line-geometry.h"
33 #include "persp3d-reference.h"
34 #include "uri.h"
35 #include "2geom/geom.h"
36 #include "sp-guide.h"
37 #include "sp-namedview.h"
38 #include "preferences.h"
40 #include "desktop.h"
41 #include "desktop-handles.h"
42 #include "macros.h"
44 static void box3d_class_init(SPBox3DClass *klass);
45 static void box3d_init(SPBox3D *box3d);
47 static void box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
48 static void box3d_release(SPObject *object);
49 static void box3d_set(SPObject *object, unsigned int key, const gchar *value);
50 static void box3d_update(SPObject *object, SPCtx *ctx, guint flags);
51 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
53 static gchar *box3d_description(SPItem *item);
54 static Geom::Matrix box3d_set_transform(SPItem *item, Geom::Matrix const &xform);
55 static void box3d_convert_to_guides(SPItem *item);
57 static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
59 static SPGroupClass *parent_class;
61 static gint counter = 0;
63 GType
64 box3d_get_type(void)
65 {
66 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof(SPBox3DClass),
71 NULL, /* base_init */
72 NULL, /* base_finalize */
73 (GClassInitFunc) box3d_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
76 sizeof(SPBox3D),
77 16, /* n_preallocs */
78 (GInstanceInitFunc) box3d_init,
79 NULL, /* value_table */
80 };
81 type = g_type_register_static(SP_TYPE_GROUP, "SPBox3D", &info, (GTypeFlags) 0);
82 }
84 return type;
85 }
87 static void
88 box3d_class_init(SPBox3DClass *klass)
89 {
90 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
91 SPItemClass *item_class = (SPItemClass *) klass;
93 parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
95 sp_object_class->build = box3d_build;
96 sp_object_class->release = box3d_release;
97 sp_object_class->set = box3d_set;
98 sp_object_class->write = box3d_write;
99 sp_object_class->update = box3d_update;
101 item_class->description = box3d_description;
102 item_class->set_transform = box3d_set_transform;
103 item_class->convert_to_guides = box3d_convert_to_guides;
104 }
106 static void
107 box3d_init(SPBox3D *box)
108 {
109 box->persp_href = NULL;
110 box->persp_ref = new Persp3DReference(SP_OBJECT(box));
111 }
113 static void
114 box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
115 {
116 if (((SPObjectClass *) (parent_class))->build) {
117 ((SPObjectClass *) (parent_class))->build(object, document, repr);
118 }
120 SPBox3D *box = SP_BOX3D (object);
121 box->my_counter = counter++;
123 /* we initialize the z-orders to zero so that they are updated during dragging */
124 for (int i = 0; i < 6; ++i) {
125 box->z_orders[i] = 0;
126 }
128 // TODO: Create/link to the correct perspective
130 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
131 if (!doc) {
132 g_print ("No document for the box!!!!\n");
133 return;
134 }
136 box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
138 sp_object_read_attr(object, "inkscape:perspectiveID");
139 sp_object_read_attr(object, "inkscape:corner0");
140 sp_object_read_attr(object, "inkscape:corner7");
141 }
143 /**
144 * Virtual release of SPBox3D members before destruction.
145 */
146 static void
147 box3d_release(SPObject *object)
148 {
149 SPBox3D *box = (SPBox3D *) object;
151 if (box->persp_href) {
152 g_free(box->persp_href);
153 }
154 if (box->persp_ref) {
155 box->persp_ref->detach();
156 delete box->persp_ref;
157 box->persp_ref = NULL;
158 }
160 //persp3d_remove_box (box3d_get_perspective(box), box);
162 if (((SPObjectClass *) parent_class)->release)
163 ((SPObjectClass *) parent_class)->release(object);
164 }
166 static void
167 box3d_set(SPObject *object, unsigned int key, const gchar *value)
168 {
169 SPBox3D *box = SP_BOX3D(object);
171 switch (key) {
172 case SP_ATTR_INKSCAPE_BOX3D_PERSPECTIVE_ID:
173 if ( value && box->persp_href && ( strcmp(value, box->persp_href) == 0 ) ) {
174 /* No change, do nothing. */
175 } else {
176 if (box->persp_href) {
177 g_free(box->persp_href);
178 box->persp_href = NULL;
179 }
180 if (value) {
181 box->persp_href = g_strdup(value);
183 // Now do the attaching, which emits the changed signal.
184 try {
185 box->persp_ref->attach(Inkscape::URI(value));
186 } catch (Inkscape::BadURIException &e) {
187 g_warning("%s", e.what());
188 box->persp_ref->detach();
189 }
190 } else {
191 // Detach, which emits the changed signal.
192 box->persp_ref->detach();
193 }
194 }
196 // FIXME: Is the following update doubled by some call in either persp3d.cpp or vanishing_point_new.cpp?
197 box3d_position_set(box);
198 break;
199 case SP_ATTR_INKSCAPE_BOX3D_CORNER0:
200 if (value && strcmp(value, "0 : 0 : 0 : 0")) {
201 box->orig_corner0 = Proj::Pt3(value);
202 box->save_corner0 = box->orig_corner0;
203 box3d_position_set(box);
204 }
205 break;
206 case SP_ATTR_INKSCAPE_BOX3D_CORNER7:
207 if (value && strcmp(value, "0 : 0 : 0 : 0")) {
208 box->orig_corner7 = Proj::Pt3(value);
209 box->save_corner7 = box->orig_corner7;
210 box3d_position_set(box);
211 }
212 break;
213 default:
214 if (((SPObjectClass *) (parent_class))->set) {
215 ((SPObjectClass *) (parent_class))->set(object, key, value);
216 }
217 break;
218 }
219 }
221 /**
222 * Gets called when (re)attached to another perspective.
223 */
224 static void
225 box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box)
226 {
227 if (old_ref) {
228 sp_signal_disconnect_by_data(old_ref, box);
229 persp3d_remove_box (SP_PERSP3D(old_ref), box);
230 /* Note: This sometimes leads to attempts to remove boxes twice from the list of selected/transformed
231 boxes in a perspectives, but this should be uncritical. */
232 persp3d_remove_box_transform (SP_PERSP3D(old_ref), box);
233 }
234 if ( SP_IS_PERSP3D(ref) && ref != box ) // FIXME: Comparisons sane?
235 {
236 persp3d_add_box (SP_PERSP3D(ref), box);
237 /* Note: This sometimes leads to attempts to add boxes twice to the list of selected/transformed
238 boxes in a perspectives, but this should be uncritical. */
239 persp3d_add_box_transform (SP_PERSP3D(ref), box);
240 }
241 }
243 static void
244 box3d_update(SPObject *object, SPCtx *ctx, guint flags)
245 {
246 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
248 /* FIXME?: Perhaps the display updates of box sides should be instantiated from here, but this
249 causes evil update loops so it's all done from box3d_position_set, which is called from
250 various other places (like the handlers in object-edit.cpp, vanishing-point.cpp, etc. */
252 }
254 // Invoke parent method
255 if (((SPObjectClass *) (parent_class))->update)
256 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
257 }
260 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
261 {
262 SPBox3D *box = SP_BOX3D(object);
264 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
265 // this is where we end up when saving as plain SVG (also in other circumstances?)
266 // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:g
267 repr = xml_doc->createElement("svg:g");
268 }
270 if (flags & SP_OBJECT_WRITE_EXT) {
272 if (box->persp_href) {
273 repr->setAttribute("inkscape:perspectiveID", box->persp_href);
274 } else {
275 /* box is not yet linked to a perspective; use the document's current perspective */
276 SPDocument *doc = SP_OBJECT_DOCUMENT(object);
277 if (box->persp_ref->getURI()) {
278 gchar *uri_string = box->persp_ref->getURI()->toString();
279 repr->setAttribute("inkscape:perspectiveID", uri_string);
280 g_free(uri_string);
281 } else {
282 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
283 const gchar *persp_id = persp_repr->attribute("id");
284 gchar *href = g_strdup_printf("#%s", persp_id);
285 repr->setAttribute("inkscape:perspectiveID", href);
286 g_free(href);
287 }
288 }
290 gchar *coordstr0 = box->orig_corner0.coord_string();
291 gchar *coordstr7 = box->orig_corner7.coord_string();
292 repr->setAttribute("inkscape:corner0", coordstr0);
293 repr->setAttribute("inkscape:corner7", coordstr7);
294 g_free(coordstr0);
295 g_free(coordstr7);
297 box->orig_corner0.normalize();
298 box->orig_corner7.normalize();
300 box->save_corner0 = box->orig_corner0;
301 box->save_corner7 = box->orig_corner7;
302 }
304 if (((SPObjectClass *) (parent_class))->write) {
305 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
306 }
308 return repr;
309 }
311 static gchar *
312 box3d_description(SPItem *item)
313 {
314 g_return_val_if_fail(SP_IS_BOX3D(item), NULL);
316 return g_strdup(_("<b>3D Box</b>"));
317 }
319 void
320 box3d_position_set (SPBox3D *box)
321 {
322 /* This draws the curve and calls requestDisplayUpdate() for each side (the latter is done in
323 box3d_side_position_set() to avoid update conflicts with the parent box) */
324 for (SPObject *child = sp_object_first_child(SP_OBJECT (box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
325 if (SP_IS_BOX3D_SIDE(child))
326 box3d_side_position_set(SP_BOX3D_SIDE(child));
327 }
328 }
330 static Geom::Matrix
331 box3d_set_transform(SPItem *item, Geom::Matrix const &xform)
332 {
333 SPBox3D *box = SP_BOX3D(item);
335 /* check whether we need to unlink any boxes from their perspectives */
336 Persp3D *persp = box3d_get_perspective(box);
337 Persp3D *transf_persp;
339 if (!persp3d_has_all_boxes_in_selection (persp)) {
340 std::list<SPBox3D *> selboxes = sp_desktop_selection(inkscape_active_desktop())->box3DList();
342 /* create a new perspective as a copy of the current one and link the selected boxes to it */
343 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
345 for (std::list<SPBox3D *>::iterator b = selboxes.begin(); b != selboxes.end(); ++b) {
346 box3d_switch_perspectives(*b, persp, transf_persp);
347 }
348 } else {
349 transf_persp = persp;
350 }
352 /* only transform the perspective once, even if it has several selected boxes */
353 if(!persp3d_was_transformed (transf_persp)) {
354 /* concatenate the affine transformation with the perspective mapping; this
355 function also triggers repr updates of boxes and the perspective itself */
356 persp3d_apply_affine_transformation(transf_persp, xform);
357 }
359 box3d_mark_transformed(box);
361 if (persp3d_all_transformed(transf_persp)) {
362 /* all boxes were transformed; make perspective sensitive for further transformations */
363 persp3d_unset_transforms(transf_persp);
364 }
366 Geom::Matrix ret(Geom::Matrix(xform).without_translation());
367 gdouble const sw = hypot(ret[0], ret[1]);
368 gdouble const sh = hypot(ret[2], ret[3]);
370 for (SPObject *child = sp_object_first_child(box); child != NULL; child = SP_OBJECT_NEXT(child)) {
371 if (SP_IS_ITEM(child)) {
372 SPItem *childitem = SP_ITEM(child);
374 // Adjust stroke width
375 sp_item_adjust_stroke(childitem, sqrt(fabs(sw * sh)));
377 // Adjust pattern fill
378 sp_item_adjust_pattern(childitem, xform);
380 // Adjust gradient fill
381 sp_item_adjust_gradient(childitem, xform);
383 // Adjust LPE
384 sp_item_adjust_livepatheffect(childitem, xform);
385 }
386 }
388 return Geom::identity();
389 }
391 Proj::Pt3
392 box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
393 return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
394 (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
395 (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
396 1.0);
397 }
399 Proj::Pt3
400 box3d_get_proj_corner (SPBox3D const *box, guint id) {
401 return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
402 (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
403 (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
404 1.0);
405 }
407 Geom::Point
408 box3d_get_corner_screen (SPBox3D const *box, guint id, bool item_coords) {
409 Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
410 if (!box3d_get_perspective(box)) {
411 return Geom::Point (NR_HUGE, NR_HUGE);
412 }
413 Geom::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(box)));
414 if (item_coords) {
415 return box3d_get_perspective(box)->tmat.image(proj_corner).affine() * i2d.inverse();
416 } else {
417 return box3d_get_perspective(box)->tmat.image(proj_corner).affine();
418 }
419 }
421 Proj::Pt3
422 box3d_get_proj_center (SPBox3D *box) {
423 box->orig_corner0.normalize();
424 box->orig_corner7.normalize();
425 return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
426 (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
427 (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
428 1.0);
429 }
431 Geom::Point
432 box3d_get_center_screen (SPBox3D *box) {
433 Proj::Pt3 proj_center (box3d_get_proj_center (box));
434 if (!box3d_get_perspective(box)) {
435 return Geom::Point (NR_HUGE, NR_HUGE);
436 }
437 Geom::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(box)));
438 return box3d_get_perspective(box)->tmat.image(proj_center).affine() * i2d.inverse();
439 }
441 /*
442 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
443 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
444 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
445 */
447 // Should we make the threshold settable in the preferences?
448 static double remember_snap_threshold = 30;
449 static guint remember_snap_index = 0;
451 static Proj::Pt3
452 box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
453 double z_coord = start_pt[Proj::Z];
454 double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
455 double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
456 double x_coord = start_pt[Proj::X];
457 double y_coord = start_pt[Proj::Y];
458 Proj::Pt3 A_proj (x_coord, y_coord, z_coord, 1.0);
459 Proj::Pt3 B_proj (x_coord + diff_x, y_coord, z_coord, 1.0);
460 Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
461 Proj::Pt3 D_proj (x_coord, y_coord + diff_y, z_coord, 1.0);
462 Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
464 Persp3D *persp = box3d_get_perspective(box);
465 Geom::Point A = persp->tmat.image(A_proj).affine();
466 Geom::Point B = persp->tmat.image(B_proj).affine();
467 Geom::Point C = persp->tmat.image(C_proj).affine();
468 Geom::Point D = persp->tmat.image(D_proj).affine();
469 Geom::Point E = persp->tmat.image(E_proj).affine();
470 Geom::Point pt = persp->tmat.image(pt_proj).affine();
472 // TODO: Replace these lines between corners with lines from a corner to a vanishing point
473 // (this might help to prevent rounding errors if the box is small)
474 Box3D::Line pl1(A, B);
475 Box3D::Line pl2(A, D);
476 Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
477 Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
479 int num_snap_lines = (id != -1) ? 3 : 4;
480 Geom::Point snap_pts[num_snap_lines];
482 snap_pts[0] = pl1.closest_to (pt);
483 snap_pts[1] = pl2.closest_to (pt);
484 snap_pts[2] = diag1.closest_to (pt);
485 if (id == -1) {
486 snap_pts[3] = diag2.closest_to (pt);
487 }
489 gdouble const zoom = inkscape_active_desktop()->current_zoom();
491 // determine the distances to all potential snapping points
492 double snap_dists[num_snap_lines];
493 for (int i = 0; i < num_snap_lines; ++i) {
494 snap_dists[i] = Geom::L2 (snap_pts[i] - pt) * zoom;
495 }
497 // while we are within a given tolerance of the starting point,
498 // keep snapping to the same point to avoid jumping
499 bool within_tolerance = true;
500 for (int i = 0; i < num_snap_lines; ++i) {
501 if (snap_dists[i] > remember_snap_threshold) {
502 within_tolerance = false;
503 break;
504 }
505 }
507 // find the closest snapping point
508 int snap_index = -1;
509 double snap_dist = NR_HUGE;
510 for (int i = 0; i < num_snap_lines; ++i) {
511 if (snap_dists[i] < snap_dist) {
512 snap_index = i;
513 snap_dist = snap_dists[i];
514 }
515 }
517 // snap to the closest point (or the previously remembered one
518 // if we are within tolerance of the starting point)
519 Geom::Point result;
520 if (within_tolerance) {
521 result = snap_pts[remember_snap_index];
522 } else {
523 remember_snap_index = snap_index;
524 result = snap_pts[snap_index];
525 }
526 return box3d_get_perspective(box)->tmat.preimage (result, z_coord, Proj::Z);
527 }
529 void
530 box3d_set_corner (SPBox3D *box, const guint id, Geom::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
531 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
533 box->orig_corner0.normalize();
534 box->orig_corner7.normalize();
536 /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
537 if (!(movement & Box3D::Z)) {
538 Proj::Pt3 pt_proj (box3d_get_perspective(box)->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
539 box->orig_corner7[Proj::Z], Proj::Z));
540 if (constrained) {
541 pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
542 }
544 // normalizing pt_proj is essential because we want to mingle affine coordinates
545 pt_proj.normalize();
546 box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
547 (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
548 box->save_corner0[Proj::Z],
549 1.0);
550 box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
551 (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
552 box->save_corner7[Proj::Z],
553 1.0);
554 } else {
555 Persp3D *persp = box3d_get_perspective(box);
556 Box3D::PerspectiveLine pl(persp->tmat.image(
557 box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
558 Proj::Z, persp);
559 Geom::Point new_pos_snapped(pl.closest_to(new_pos));
560 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped,
561 box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
562 (movement & Box3D::Y) ? Proj::X : Proj::Y));
563 bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
564 bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
565 bool corner7_move_x = (id & Box3D::X) && (movement & Box3D::X);
566 bool corner7_move_y = (id & Box3D::Y) && (movement & Box3D::Y);
567 // normalizing pt_proj is essential because we want to mingle affine coordinates
568 pt_proj.normalize();
569 box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
570 corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
571 (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
572 1.0);
573 box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
574 corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
575 (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
576 1.0);
577 }
578 // FIXME: Should we update the box here? If so, how?
579 }
581 void box3d_set_center (SPBox3D *box, Geom::Point const &new_pos, Geom::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
582 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
584 box->orig_corner0.normalize();
585 box->orig_corner7.normalize();
587 Persp3D *persp = box3d_get_perspective(box);
588 if (!(movement & Box3D::Z)) {
589 double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
590 double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
591 double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
593 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos, coord, Proj::Z));
594 if (constrained) {
595 Proj::Pt3 old_pos_proj (persp->tmat.preimage (old_pos, coord, Proj::Z));
596 old_pos_proj.normalize();
597 pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
598 }
599 // normalizing pt_proj is essential because we want to mingle affine coordinates
600 pt_proj.normalize();
601 box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
602 (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
603 box->orig_corner0[Proj::Z],
604 1.0);
605 box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
606 (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
607 box->orig_corner7[Proj::Z],
608 1.0);
609 } else {
610 double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
611 double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
613 Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
614 Geom::Point new_pos_snapped(pl.closest_to(new_pos));
615 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped, coord, Proj::X));
617 /* normalizing pt_proj is essential because we want to mingle affine coordinates */
618 pt_proj.normalize();
619 box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
620 box->orig_corner0[Proj::Y],
621 pt_proj[Proj::Z] - radz,
622 1.0);
623 box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
624 box->orig_corner7[Proj::Y],
625 pt_proj[Proj::Z] + radz,
626 1.0);
627 }
628 }
630 /*
631 * Manipulates corner1 through corner4 to contain the indices of the corners
632 * from which the perspective lines in the direction of 'axis' emerge
633 */
634 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
635 Geom::Point &corner1, Geom::Point &corner2, Geom::Point &corner3, Geom::Point &corner4)
636 {
637 Persp3D *persp = box3d_get_perspective(box);
638 g_return_if_fail (persp);
639 //box->orig_corner0.normalize();
640 //box->orig_corner7.normalize();
641 double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
642 box->orig_corner0[axis] :
643 box->orig_corner7[axis];
645 Proj::Pt3 c1, c2, c3, c4;
646 // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
647 switch (axis) {
648 case Proj::X:
649 c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
650 c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
651 c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
652 c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
653 break;
654 case Proj::Y:
655 c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
656 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
657 c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
658 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
659 break;
660 case Proj::Z:
661 c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
662 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
663 c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
664 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
665 break;
666 default:
667 return;
668 }
669 corner1 = persp->tmat.image(c1).affine();
670 corner2 = persp->tmat.image(c2).affine();
671 corner3 = persp->tmat.image(c3).affine();
672 corner4 = persp->tmat.image(c4).affine();
673 }
675 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
676 static bool
677 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
678 Geom::Point const &C, Geom::Point const &D) {
679 Geom::Point E; // the point of intersection
680 Geom::Point n0 = (B - A).ccw();
681 double d0 = dot(n0,A);
683 Geom::Point n1 = (D - C).ccw();
684 double d1 = dot(n1,C);
685 Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
686 if (intersects == Geom::coincident || intersects == Geom::parallel) {
687 return false;
688 }
690 if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
691 // C and D lie on the same side of the line AB
692 return false;
693 }
694 if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
695 // A and B lie on different sides of the line CD
696 return true;
697 } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
698 // The line CD passes on the "wrong" side of A
699 return false;
700 }
702 // The line CD passes on the "correct" side of A
703 return true;
704 }
706 static bool
707 box3d_XY_axes_are_swapped (SPBox3D *box) {
708 Persp3D *persp = box3d_get_perspective(box);
709 g_return_val_if_fail(persp, false);
710 Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3, false), Proj::X, persp);
711 Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3, false), Proj::Y, persp);
712 Geom::Point v1(l1.direction());
713 Geom::Point v2(l2.direction());
714 v1.normalize();
715 v2.normalize();
717 return (v1[Geom::X]*v2[Geom::Y] - v1[Geom::Y]*v2[Geom::X] > 0);
718 }
720 static inline void
721 box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
722 z_orders[0] = a;
723 z_orders[1] = b;
724 z_orders[2] = c;
725 z_orders[3] = d;
726 z_orders[4] = e;
727 z_orders[5] = f;
728 }
730 static inline void
731 box3d_swap_z_orders (int z_orders[6]) {
732 int tmp;
733 for (int i = 0; i < 3; ++i) {
734 tmp = z_orders[i];
735 z_orders[i] = z_orders[5-i];
736 z_orders[5-i] = tmp;
737 }
738 }
740 /*
741 * In standard perspective we have:
742 * 2 = front face
743 * 1 = top face
744 * 0 = left face
745 * 3 = right face
746 * 4 = bottom face
747 * 5 = rear face
748 */
750 /* All VPs infinite */
751 static void
752 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
753 Persp3D *persp = box3d_get_perspective(box);
754 Geom::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
755 Geom::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
756 Geom::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
758 bool swapped = box3d_XY_axes_are_swapped(box);
760 switch(central_axis) {
761 case Box3D::X:
762 if (!swapped) {
763 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
764 } else {
765 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
766 }
767 break;
768 case Box3D::Y:
769 if (!swapped) {
770 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
771 } else {
772 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
773 }
774 break;
775 case Box3D::Z:
776 if (!swapped) {
777 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
778 } else {
779 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
780 }
781 break;
782 case Box3D::NONE:
783 if (!swapped) {
784 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
785 } else {
786 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
787 }
788 break;
789 default:
790 g_assert_not_reached();
791 break;
792 }
793 }
795 /* Precisely one finite VP */
796 static void
797 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
798 Persp3D *persp = box3d_get_perspective(box);
799 Geom::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
801 // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
802 Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
803 Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
804 int inside1 = 0;
805 int inside2 = 0;
806 inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
807 inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
809 bool swapped = box3d_XY_axes_are_swapped(box);
811 switch(central_axis) {
812 case Box3D::X:
813 if (!swapped) {
814 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
815 } else {
816 box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
817 }
818 break;
819 case Box3D::Y:
820 if (inside2 > 0) {
821 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
822 } else if (inside2 < 0) {
823 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
824 } else {
825 if (!swapped) {
826 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
827 } else {
828 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
829 }
830 }
831 break;
832 case Box3D::Z:
833 if (inside2) {
834 if (!swapped) {
835 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
836 } else {
837 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
838 }
839 } else if (inside1) {
840 if (!swapped) {
841 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
842 } else {
843 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
844 }
845 } else {
846 // "regular" case
847 if (!swapped) {
848 box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
849 } else {
850 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
851 }
852 }
853 break;
854 case Box3D::NONE:
855 if (!swapped) {
856 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
857 } else {
858 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
859 }
860 break;
861 default:
862 g_assert_not_reached();
863 }
864 }
866 /* Precisely 2 finite VPs */
867 static void
868 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
869 Persp3D *persp = box3d_get_perspective(box);
871 Geom::Point c3(box3d_get_corner_screen(box, 3, false));
872 Geom::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
873 Geom::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
874 Geom::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
876 bool swapped = box3d_XY_axes_are_swapped(box);
878 int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
879 //int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
881 int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
882 int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
884 //int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
885 int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
887 switch(central_axis) {
888 case Box3D::X:
889 if (!swapped) {
890 if (insidezy == -1) {
891 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
892 } else if (insidexy == 1) {
893 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
894 } else {
895 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
896 }
897 } else {
898 if (insideyz == -1) {
899 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
900 } else {
901 if (!swapped) {
902 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
903 } else {
904 if (insidexy == 0) {
905 box3d_aux_set_z_orders (z_orders, 3, 5, 1, 0, 2, 4);
906 } else {
907 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
908 }
909 }
910 }
911 }
912 break;
913 case Box3D::Y:
914 if (!swapped) {
915 if (insideyz == 1) {
916 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
917 } else {
918 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
919 }
920 } else {
921 if (insideyx == 1) {
922 box3d_aux_set_z_orders (z_orders, 4, 0, 5, 1, 3, 2);
923 } else {
924 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
925 }
926 }
927 break;
928 case Box3D::Z:
929 if (!swapped) {
930 if (insidezy == 1) {
931 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
932 } else if (insidexy == -1) {
933 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
934 } else {
935 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
936 }
937 } else {
938 box3d_aux_set_z_orders (z_orders, 3, 4, 5, 1, 0, 2);
939 }
940 break;
941 case Box3D::NONE:
942 if (!swapped) {
943 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
944 } else {
945 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
946 }
947 break;
948 default:
949 g_assert_not_reached();
950 break;
951 }
952 }
954 /*
955 * It can happen that during dragging the box is everted.
956 * In this case the opposite sides in this direction need to be swapped
957 */
958 static Box3D::Axis
959 box3d_everted_directions (SPBox3D *box) {
960 Box3D::Axis ev = Box3D::NONE;
962 box->orig_corner0.normalize();
963 box->orig_corner7.normalize();
965 if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
966 ev = (Box3D::Axis) (ev ^ Box3D::X);
967 if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
968 ev = (Box3D::Axis) (ev ^ Box3D::Y);
969 if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
970 ev = (Box3D::Axis) (ev ^ Box3D::Z);
972 return ev;
973 }
975 static void
976 box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
977 int pos1 = -1;
978 int pos2 = -1;
980 for (int i = 0; i < 6; ++i) {
981 if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
982 if (pos1 == -1) {
983 pos1 = i;
984 } else {
985 pos2 = i;
986 break;
987 }
988 }
989 }
991 int tmp = z_orders[pos1];
992 z_orders[pos1] = z_orders[pos2];
993 z_orders[pos2] = tmp;
994 }
997 bool
998 box3d_recompute_z_orders (SPBox3D *box) {
999 Persp3D *persp = box3d_get_perspective(box);
1001 if (!persp)
1002 return false;
1004 int z_orders[6];
1006 Geom::Point c3(box3d_get_corner_screen(box, 3, false));
1008 // determine directions from corner3 to the VPs
1009 int num_finite = 0;
1010 Box3D::Axis axis_finite = Box3D::NONE;
1011 Box3D::Axis axis_infinite = Box3D::NONE;
1012 Geom::Point dirs[3];
1013 for (int i = 0; i < 3; ++i) {
1014 dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
1015 if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
1016 num_finite++;
1017 axis_finite = Box3D::axes[i];
1018 } else {
1019 axis_infinite = Box3D::axes[i];
1020 }
1021 }
1023 // determine the "central" axis (if there is one)
1024 Box3D::Axis central_axis = Box3D::NONE;
1025 if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
1026 central_axis = Box3D::Z;
1027 } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
1028 central_axis = Box3D::X;
1029 } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
1030 central_axis = Box3D::Y;
1031 }
1033 switch (num_finite) {
1034 case 0:
1035 // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
1036 // coincide, hence only need to be computed once in a more central location.
1037 box3d_set_new_z_orders_case0(box, z_orders, central_axis);
1038 break;
1039 case 1:
1040 box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
1041 break;
1042 case 2:
1043 case 3:
1044 box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
1045 break;
1046 default:
1047 /*
1048 * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
1049 * joining the other two VPs. If this is the case, it determines the "central" corner from
1050 * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
1051 */
1052 // FIXME: We should eliminate the use of Geom::Point altogether
1053 Box3D::Axis central_axis = Box3D::NONE;
1054 Geom::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
1055 Geom::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
1056 Geom::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
1057 Geom::Point vpx(vp_x[Geom::X], vp_x[Geom::Y]);
1058 Geom::Point vpy(vp_y[Geom::X], vp_y[Geom::Y]);
1059 Geom::Point vpz(vp_z[Geom::X], vp_z[Geom::Y]);
1061 Geom::Point c3 = box3d_get_corner_screen(box, 3, false);
1062 Geom::Point corner3(c3[Geom::X], c3[Geom::Y]);
1064 if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
1065 central_axis = Box3D::X;
1066 } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
1067 central_axis = Box3D::Y;
1068 } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
1069 central_axis = Box3D::Z;
1070 }
1072 unsigned int central_corner = 3 ^ central_axis;
1073 if (central_axis == Box3D::Z) {
1074 central_corner = central_corner ^ Box3D::XYZ;
1075 }
1076 if (box3d_XY_axes_are_swapped(box)) {
1077 central_corner = central_corner ^ Box3D::XYZ;
1078 }
1080 Geom::Point c1(box3d_get_corner_screen(box, 1, false));
1081 Geom::Point c2(box3d_get_corner_screen(box, 2, false));
1082 Geom::Point c7(box3d_get_corner_screen(box, 7, false));
1084 Geom::Point corner1(c1[Geom::X], c1[Geom::Y]);
1085 Geom::Point corner2(c2[Geom::X], c2[Geom::Y]);
1086 Geom::Point corner7(c7[Geom::X], c7[Geom::Y]);
1087 // FIXME: At present we don't use the information about central_corner computed above.
1088 switch (central_axis) {
1089 case Box3D::Y:
1090 if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
1091 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1092 } else {
1093 // degenerate case
1094 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
1095 }
1096 break;
1098 case Box3D::Z:
1099 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
1100 // degenerate case
1101 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
1102 } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
1103 // degenerate case
1104 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
1105 } else {
1106 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
1107 }
1108 break;
1110 case Box3D::X:
1111 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
1112 // degenerate case
1113 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
1114 } else {
1115 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
1116 }
1117 break;
1119 case Box3D::NONE:
1120 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1121 break;
1123 default:
1124 g_assert_not_reached();
1125 break;
1126 } // end default case
1127 }
1129 // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
1130 // instead of the hard-coded corner #3 in the computations above
1131 Box3D::Axis ev = box3d_everted_directions(box);
1132 for (int i = 0; i < 3; ++i) {
1133 if (ev & Box3D::axes[i]) {
1134 box3d_swap_sides(z_orders, Box3D::axes[i]);
1135 }
1136 }
1138 // Check whether anything actually changed
1139 for (int i = 0; i < 6; ++i) {
1140 if (box->z_orders[i] != z_orders[i]) {
1141 for (int j = i; j < 6; ++j) {
1142 box->z_orders[j] = z_orders[j];
1143 }
1144 return true;
1145 }
1146 }
1147 return false;
1148 }
1150 static std::map<int, Box3DSide *>
1151 box3d_get_sides (SPBox3D *box) {
1152 std::map<int, Box3DSide *> sides;
1153 for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
1154 if (SP_IS_BOX3D_SIDE(side))
1155 sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
1156 "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
1157 }
1158 sides.erase(-1);
1159 return sides;
1160 }
1163 // TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
1164 void
1165 box3d_set_z_orders (SPBox3D *box) {
1166 // For efficiency reasons, we only set the new z-orders if something really changed
1167 if (box3d_recompute_z_orders (box)) {
1168 std::map<int, Box3DSide *> sides = box3d_get_sides(box);
1169 std::map<int, Box3DSide *>::iterator side;
1170 for (unsigned int i = 0; i < 6; ++i) {
1171 side = sides.find(box->z_orders[i]);
1172 if (side != sides.end()) {
1173 SP_ITEM((*side).second)->lowerToBottom();
1174 }
1175 }
1176 }
1177 }
1179 /*
1180 * Auxiliary function for z-order recomputing:
1181 * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
1182 * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
1183 * lies in the sector if it lies between the two (parallel) PLs.
1184 * \ret * 0 if \a pt doesn't lie in the sector
1185 * * 1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
1186 * from the edge between the two corners to \a pt points towards the VP
1187 * * -1 otherwise
1188 */
1189 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
1190 // can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
1191 int
1192 box3d_pt_lies_in_PL_sector (SPBox3D const *box, Geom::Point const &pt, int id1, int id2, Box3D::Axis axis) {
1193 Persp3D *persp = box3d_get_perspective(box);
1195 // the two corners
1196 Geom::Point c1(box3d_get_corner_screen(box, id1, false));
1197 Geom::Point c2(box3d_get_corner_screen(box, id2, false));
1199 int ret = 0;
1200 if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
1201 Geom::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
1202 Geom::Point v1(c1 - vp);
1203 Geom::Point v2(c2 - vp);
1204 Geom::Point w(pt - vp);
1205 ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
1206 } else {
1207 Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
1208 Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
1209 if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
1210 // test whether pt lies "towards" or "away from" the VP
1211 Box3D::Line edge(c1,c2);
1212 Geom::Point c3(box3d_get_corner_screen(box, id1 ^ axis, false));
1213 if (edge.lie_on_same_side(pt, c3)) {
1214 ret = 1;
1215 } else {
1216 ret = -1;
1217 }
1218 }
1219 }
1220 return ret;
1221 }
1223 int
1224 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
1225 Persp3D *persp = box3d_get_perspective(box);
1227 if (!persp3d_VP_is_finite(persp, vpdir)) {
1228 return 0;
1229 } else {
1230 return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
1231 }
1232 }
1234 /* swap the coordinates of corner0 and corner7 along the specified axis */
1235 static void
1236 box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
1237 box->orig_corner0.normalize();
1238 box->orig_corner7.normalize();
1239 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1240 double tmp = box->orig_corner0[axis];
1241 box->orig_corner0[axis] = box->orig_corner7[axis];
1242 box->orig_corner7[axis] = tmp;
1243 }
1244 // Should we also swap the coordinates of save_corner0 and save_corner7?
1245 }
1247 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
1248 void
1249 box3d_relabel_corners(SPBox3D *box) {
1250 box3d_swap_coords(box, Proj::X, false);
1251 box3d_swap_coords(box, Proj::Y, false);
1252 box3d_swap_coords(box, Proj::Z, true);
1253 }
1255 static void
1256 box3d_check_for_swapped_coords(SPBox3D *box, Proj::Axis axis, bool smaller) {
1257 box->orig_corner0.normalize();
1258 box->orig_corner7.normalize();
1260 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1261 box->swapped = (Box3D::Axis) (box->swapped | Proj::toAffine(axis));
1262 } else {
1263 box->swapped = (Box3D::Axis) (box->swapped & ~Proj::toAffine(axis));
1264 }
1265 }
1267 static void
1268 box3d_exchange_coords(SPBox3D *box) {
1269 box->orig_corner0.normalize();
1270 box->orig_corner7.normalize();
1272 for (int i = 0; i < 3; ++i) {
1273 if (box->swapped & Box3D::axes[i]) {
1274 double tmp = box->orig_corner0[i];
1275 box->orig_corner0[i] = box->orig_corner7[i];
1276 box->orig_corner7[i] = tmp;
1277 }
1278 }
1279 }
1281 void
1282 box3d_check_for_swapped_coords(SPBox3D *box) {
1283 box3d_check_for_swapped_coords(box, Proj::X, false);
1284 box3d_check_for_swapped_coords(box, Proj::Y, false);
1285 box3d_check_for_swapped_coords(box, Proj::Z, true);
1287 box3d_exchange_coords(box);
1288 }
1290 void
1291 box3d_add_to_selection(SPBox3D *box) {
1292 Persp3D *persp = box3d_get_perspective(box);
1293 g_return_if_fail(persp);
1294 persp3d_add_box_transform(persp, box);
1295 }
1297 void
1298 box3d_remove_from_selection(SPBox3D *box) {
1299 Persp3D *persp = box3d_get_perspective(box);
1300 if (!persp) {
1301 /* this can happen if a box is deleted through undo and the persp_ref is already detached;
1302 should we rebuild the boxes of each perspective in this case or is it safe to leave it alone? */
1303 return;
1304 }
1305 persp3d_remove_box_transform(persp, box);
1306 }
1308 void
1309 box3d_mark_transformed(SPBox3D *box) {
1310 Persp3D *persp = box3d_get_perspective(box);
1311 g_return_if_fail(persp);
1312 persp3d_set_box_transformed(persp, box, true);
1313 }
1315 static void
1316 box3d_extract_boxes_rec(SPObject *obj, std::list<SPBox3D *> &boxes) {
1317 if (SP_IS_BOX3D(obj)) {
1318 boxes.push_back(SP_BOX3D(obj));
1319 } else if (SP_IS_GROUP(obj)) {
1320 for (SPObject *child = sp_object_first_child(obj); child != NULL; child = SP_OBJECT_NEXT(child) ) {
1321 box3d_extract_boxes_rec(child, boxes);
1322 }
1323 }
1324 }
1326 std::list<SPBox3D *>
1327 box3d_extract_boxes(SPObject *obj) {
1328 std::list<SPBox3D *> boxes;
1329 box3d_extract_boxes_rec(obj, boxes);
1330 return boxes;
1331 }
1333 Persp3D *
1334 box3d_get_perspective(SPBox3D const *box) {
1335 return box->persp_ref->getObject();
1336 }
1338 void
1339 box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp, bool recompute_corners) {
1340 if (recompute_corners) {
1341 box->orig_corner0.normalize();
1342 box->orig_corner7.normalize();
1343 double z0 = box->orig_corner0[Proj::Z];
1344 double z7 = box->orig_corner7[Proj::Z];
1345 Geom::Point corner0_screen = box3d_get_corner_screen(box, 0, false);
1346 Geom::Point corner7_screen = box3d_get_corner_screen(box, 7, false);
1348 box->orig_corner0 = new_persp->tmat.preimage(corner0_screen, z0, Proj::Z);
1349 box->orig_corner7 = new_persp->tmat.preimage(corner7_screen, z7, Proj::Z);
1350 }
1352 persp3d_remove_box (old_persp, box);
1353 persp3d_add_box (new_persp, box);
1355 persp3d_remove_box_transform (old_persp, box);
1356 persp3d_add_box_transform (new_persp, box);
1358 gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
1359 SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
1360 g_free(href);
1361 }
1363 /* Converts the 3D box to an ordinary SPGroup, adds it to the XML tree at the same position as
1364 the original box and deletes the latter */
1365 SPGroup *
1366 box3d_convert_to_group(SPBox3D *box) {
1367 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
1368 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1370 // remember position of the box
1371 int pos = SP_OBJECT_REPR(box)->position();
1373 // remember important attributes
1374 Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(box);
1375 gchar const *id = repr_source->attribute("id");
1376 gchar const *style = repr_source->attribute("style");
1377 gchar const *mask = repr_source->attribute("mask");
1378 gchar const *clip_path = repr_source->attribute("clip-path");
1380 // create a new group and add the sides (converted to ordinary paths) as its children
1381 Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
1383 Inkscape::XML::Node *repr;
1384 for (SPObject *child = sp_object_first_child(SP_OBJECT(box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
1385 if (SP_IS_BOX3D_SIDE(child)) {
1386 repr = box3d_side_convert_to_path(SP_BOX3D_SIDE(child));
1387 grepr->appendChild(repr);
1388 } else {
1389 g_warning("Non-side item encountered as child of a 3D box.");
1390 }
1391 }
1393 // add the new group to the box's parent and set remembered position
1394 SPObject *parent = SP_OBJECT_PARENT(box);
1395 SP_OBJECT_REPR(parent)->appendChild(grepr);
1396 grepr->setPosition(pos);
1397 grepr->setAttribute("style", style);
1398 if (mask)
1399 grepr->setAttribute("mask", mask);
1400 if (clip_path)
1401 grepr->setAttribute("clip-path", clip_path);
1403 SP_OBJECT(box)->deleteObject(true);
1405 grepr->setAttribute("id", id);
1407 return SP_GROUP(doc->getObjectByRepr(grepr));
1408 }
1410 static inline void
1411 box3d_push_back_corner_pair(SPBox3D *box, std::list<std::pair<Geom::Point, Geom::Point> > &pts, int c1, int c2) {
1412 pts.push_back(std::make_pair(box3d_get_corner_screen(box, c1, false),
1413 box3d_get_corner_screen(box, c2, false)));
1414 }
1416 void
1417 box3d_convert_to_guides(SPItem *item) {
1418 SPBox3D *box = SP_BOX3D(item);
1419 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1421 if (!prefs->getBool("/tools/shapes/3dbox/convertguides", true)) {
1422 sp_item_convert_to_guides(SP_ITEM(box));
1423 return;
1424 }
1426 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1428 /* perspective lines in X direction */
1429 box3d_push_back_corner_pair(box, pts, 0, 1);
1430 box3d_push_back_corner_pair(box, pts, 2, 3);
1431 box3d_push_back_corner_pair(box, pts, 4, 5);
1432 box3d_push_back_corner_pair(box, pts, 6, 7);
1434 /* perspective lines in Y direction */
1435 box3d_push_back_corner_pair(box, pts, 0, 2);
1436 box3d_push_back_corner_pair(box, pts, 1, 3);
1437 box3d_push_back_corner_pair(box, pts, 4, 6);
1438 box3d_push_back_corner_pair(box, pts, 5, 7);
1440 /* perspective lines in Z direction */
1441 box3d_push_back_corner_pair(box, pts, 0, 4);
1442 box3d_push_back_corner_pair(box, pts, 1, 5);
1443 box3d_push_back_corner_pair(box, pts, 2, 6);
1444 box3d_push_back_corner_pair(box, pts, 3, 7);
1446 sp_guide_pt_pairs_to_guides(inkscape_active_desktop(), pts);
1447 }
1449 /*
1450 Local Variables:
1451 mode:c++
1452 c-file-style:"stroustrup"
1453 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1454 indent-tabs-mode:nil
1455 fill-column:99
1456 End:
1457 */
1458 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :