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