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