631d5cfc1d27be44ea582aaddfb4eb0344114fc5
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"
38 #include "desktop.h"
39 #include "macros.h"
41 static void box3d_class_init(SPBox3DClass *klass);
42 static void box3d_init(SPBox3D *box3d);
44 static void box3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
45 static void box3d_release(SPObject *object);
46 static void box3d_set(SPObject *object, unsigned int key, const gchar *value);
47 static void box3d_update(SPObject *object, SPCtx *ctx, guint flags);
48 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
50 static gchar *box3d_description(SPItem *item);
51 static NR::Matrix box3d_set_transform(SPItem *item, NR::Matrix const &xform);
53 static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, SPBox3D *box);
54 static void box3d_ref_modified(SPObject *href, guint flags, SPBox3D *box);
55 //static void box3d_ref_changed(SPObject *old_ref, SPObject *ref, Persp3D *persp);
56 //static void box3d_ref_modified(SPObject *href, guint flags, Persp3D *persp);
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 }
104 static void
105 box3d_init(SPBox3D *box)
106 {
107 box->persp_href = NULL;
108 box->persp_ref = new Persp3DReference(SP_OBJECT(box));
109 new (&box->modified_connection) sigc::connection();
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 g_print ("No document for the box!!!!\n");
132 return;
133 }
134 /**
135 if (!box->persp3d) {
136 g_print ("Box seems to be newly created since no perspective is referenced yet. We reference the current perspective.\n");
137 box->persp3d = doc->current_persp3d;
138 }
139 **/
141 box->persp_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(box3d_ref_changed), box));
143 sp_object_read_attr(object, "inkscape:perspectiveID");
144 sp_object_read_attr(object, "inkscape:corner0");
145 sp_object_read_attr(object, "inkscape:corner7");
146 }
148 /**
149 * Virtual release of SPBox3D members before destruction.
150 */
151 static void
152 box3d_release(SPObject *object)
153 {
154 SPBox3D *box = (SPBox3D *) object;
156 if (box->persp_href) {
157 g_free(box->persp_href);
158 }
159 if (box->persp_ref) {
160 box->persp_ref->detach();
161 delete box->persp_ref;
162 box->persp_ref = NULL;
163 }
165 box->modified_connection.disconnect();
166 box->modified_connection.~connection();
168 //persp3d_remove_box (box3d_get_perspective(box), box);
170 if (((SPObjectClass *) parent_class)->release)
171 ((SPObjectClass *) parent_class)->release(object);
172 }
174 static void
175 box3d_set(SPObject *object, unsigned int key, const gchar *value)
176 {
177 SPBox3D *box = SP_BOX3D(object);
179 switch (key) {
180 case SP_ATTR_INKSCAPE_BOX3D_PERSPECTIVE_ID:
181 if ( value && box->persp_href && ( strcmp(value, box->persp_href) == 0 ) ) {
182 /* No change, do nothing. */
183 } else {
184 if (box->persp_href) {
185 g_free(box->persp_href);
186 box->persp_href = NULL;
187 }
188 if (value) {
189 box->persp_href = g_strdup(value);
191 // Now do the attaching, which emits the changed signal.
192 try {
193 box->persp_ref->attach(Inkscape::URI(value));
194 } catch (Inkscape::BadURIException &e) {
195 g_warning("%s", e.what());
196 box->persp_ref->detach();
197 }
198 } else {
199 // Detach, which emits the changed signal.
200 box->persp_ref->detach();
201 // TODO: Clean this up (also w.r.t the surrounding if construct)
202 /***
203 g_print ("No perspective given. Attaching to current perspective instead.\n");
204 g_free(box->persp_href);
205 Inkscape::XML::Node *repr = SP_OBJECT_REPR(inkscape_active_document()->current_persp3d);
206 box->persp_href = g_strdup(repr->attribute("id"));
207 box->persp_ref->attach(Inkscape::URI(box->persp_href));
208 ***/
209 }
210 }
212 // FIXME: Is the following update doubled by some call in either persp3d.cpp or vanishing_point_new.cpp?
213 box3d_position_set(box);
214 break;
215 case SP_ATTR_INKSCAPE_BOX3D_CORNER0:
216 if (value && strcmp(value, "0 : 0 : 0 : 0")) {
217 box->orig_corner0 = Proj::Pt3(value);
218 box->save_corner0 = box->orig_corner0;
219 box3d_position_set(box);
220 }
221 break;
222 case SP_ATTR_INKSCAPE_BOX3D_CORNER7:
223 if (value && strcmp(value, "0 : 0 : 0 : 0")) {
224 box->orig_corner7 = Proj::Pt3(value);
225 box->save_corner7 = box->orig_corner7;
226 box3d_position_set(box);
227 }
228 break;
229 default:
230 if (((SPObjectClass *) (parent_class))->set) {
231 ((SPObjectClass *) (parent_class))->set(object, key, value);
232 }
233 break;
234 }
235 //object->updateRepr(); // This ensures correct update of the box after undo/redo. FIXME: Why is this not present in sp-rect.cpp and similar files?
236 }
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 box->modified_connection.disconnect();
254 box->modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&box3d_ref_modified), box));
255 box3d_ref_modified(ref, 0, box);
256 persp3d_add_box (SP_PERSP3D(ref), box);
257 /* Note: This sometimes leads to attempts to add boxes twice to the list of selected/transformed
258 boxes in a perspectives, but this should be uncritical. */
259 persp3d_add_box_transform (SP_PERSP3D(ref), box);
260 }
261 }
263 static void
264 box3d_update(SPObject *object, SPCtx *ctx, guint flags)
265 {
266 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
268 /* FIXME?: Perhaps the display updates of box sides should be instantiated from here, but this
269 causes evil update loops so it's all done from box3d_position_set, which is called from
270 various other places (like the handlers in object-edit.cpp, vanishing-point.cpp, etc. */
272 }
274 // Invoke parent method
275 if (((SPObjectClass *) (parent_class))->update)
276 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
277 }
280 static Inkscape::XML::Node *box3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
281 {
282 SPBox3D *box = SP_BOX3D(object);
284 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
285 // this is where we end up when saving as plain SVG (also in other circumstances?)
286 // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:g
287 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
288 repr = xml_doc->createElement("svg:g");
289 }
291 if (flags & SP_OBJECT_WRITE_EXT) {
293 if (box->persp_href) {
294 repr->setAttribute("inkscape:perspectiveID", box->persp_href);
295 } else {
296 /* box is not yet linked to a perspective; use the document's current perspective */
297 SPDocument *doc = inkscape_active_document();
298 if (box->persp_ref->getURI()) {
299 gchar *uri_string = box->persp_ref->getURI()->toString();
300 repr->setAttribute("inkscape:perspectiveID", uri_string);
301 g_free(uri_string);
302 } else if (doc) {
303 //persp3d_add_box (doc->current_persp3d, box);
304 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(doc->current_persp3d);
305 const gchar *persp_id = persp_repr->attribute("id");
306 gchar *href = g_strdup_printf("#%s", persp_id);
307 repr->setAttribute("inkscape:perspectiveID", href);
308 g_free(href);
309 } else {
310 g_print ("No active document while creating perspective!!!\n");
311 }
312 }
314 gchar *coordstr0 = box->orig_corner0.coord_string();
315 gchar *coordstr7 = box->orig_corner7.coord_string();
316 repr->setAttribute("inkscape:corner0", coordstr0);
317 repr->setAttribute("inkscape:corner7", coordstr7);
318 g_free(coordstr0);
319 g_free(coordstr7);
321 box->orig_corner0.normalize();
322 box->orig_corner7.normalize();
324 box->save_corner0 = box->orig_corner0;
325 box->save_corner7 = box->orig_corner7;
326 }
328 if (((SPObjectClass *) (parent_class))->write) {
329 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
330 }
332 return repr;
333 }
335 static gchar *
336 box3d_description(SPItem *item)
337 {
338 g_return_val_if_fail(SP_IS_BOX3D(item), NULL);
340 return g_strdup(_("<b>3D Box</b>"));
341 }
343 void
344 box3d_position_set (SPBox3D *box)
345 {
346 /* This draws the curve and calls requestDisplayUpdate() for each side (the latter is done in
347 box3d_side_position_set() to avoid update conflicts with the parent box) */
348 for (SPObject *child = sp_object_first_child(SP_OBJECT (box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
349 box3d_side_position_set (SP_BOX3D_SIDE (child));
350 }
351 }
353 static NR::Matrix
354 box3d_set_transform(SPItem *item, NR::Matrix const &xform)
355 {
356 SPBox3D *box = SP_BOX3D(item);
358 /* check whether we need to unlink any boxes from their perspectives */
359 Persp3D *persp = box3d_get_perspective(box);
360 Persp3D *transf_persp;
362 if (!persp3d_has_all_boxes_in_selection (persp)) {
363 std::list<SPBox3D *> sel = persp3d_selected_boxes (persp);
365 /* create a new perspective as a copy of the current one and link the selected boxes to it */
366 transf_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
368 for (std::list<SPBox3D *>::iterator b = sel.begin(); b != sel.end(); ++b) {
369 box3d_switch_perspectives(*b, persp, transf_persp);
370 }
371 } else {
372 transf_persp = persp;
373 }
375 /* only transform the perspective once, even if it has several selected boxes */
376 if(!persp3d_was_transformed (transf_persp)) {
377 /* concatenate the affine transformation with the perspective mapping; this
378 function also triggers repr updates of boxes and the perspective itself */
379 persp3d_apply_affine_transformation(transf_persp, xform);
380 }
382 box3d_mark_transformed(box);
384 if (persp3d_all_transformed(transf_persp)) {
385 /* all boxes were transformed; make perspective sensitive for further transformations */
386 persp3d_unset_transforms(transf_persp);
387 }
389 NR::Matrix ret(NR::transform(xform));
390 gdouble const sw = hypot(ret[0], ret[1]);
391 gdouble const sh = hypot(ret[2], ret[3]);
393 SPItem *sideitem = NULL;
394 for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
395 sideitem = SP_ITEM(side);
397 // Adjust stroke width
398 sp_item_adjust_stroke(sideitem, sqrt(fabs(sw * sh)));
400 // Adjust pattern fill
401 sp_item_adjust_pattern(sideitem, xform);
403 // Adjust gradient fill
404 sp_item_adjust_gradient(sideitem, xform);
406 // Adjust LPE
407 sp_item_adjust_livepatheffect(item, xform);
408 }
410 return NR::identity();
411 }
414 /**
415 * Gets called when persp(?) repr contents change: i.e. parameter change.
416 */
417 static void
418 box3d_ref_modified(SPObject */*href*/, guint /*flags*/, SPBox3D */*box*/)
419 {
420 /***
421 g_print ("FIXME: box3d_ref_modified was called. What should we do?\n");
422 g_print ("Here is at least the the href's id: %s\n", SP_OBJECT_REPR(href)->attribute("id"));
423 g_print (" ... and the box's, too: %s\n", SP_OBJECT_REPR(box)->attribute("id"));
424 ***/
426 }
428 Proj::Pt3
429 box3d_get_proj_corner (guint id, Proj::Pt3 const &c0, Proj::Pt3 const &c7) {
430 return Proj::Pt3 ((id & Box3D::X) ? c7[Proj::X] : c0[Proj::X],
431 (id & Box3D::Y) ? c7[Proj::Y] : c0[Proj::Y],
432 (id & Box3D::Z) ? c7[Proj::Z] : c0[Proj::Z],
433 1.0);
434 }
436 Proj::Pt3
437 box3d_get_proj_corner (SPBox3D const *box, guint id) {
438 return Proj::Pt3 ((id & Box3D::X) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
439 (id & Box3D::Y) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
440 (id & Box3D::Z) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
441 1.0);
442 }
444 NR::Point
445 box3d_get_corner_screen (SPBox3D const *box, guint id) {
446 Proj::Pt3 proj_corner (box3d_get_proj_corner (box, id));
447 if (!box3d_get_perspective(box)) {
448 //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
449 return NR::Point (NR_HUGE, NR_HUGE);
450 }
451 return box3d_get_perspective(box)->tmat.image(proj_corner).affine();
452 }
454 Proj::Pt3
455 box3d_get_proj_center (SPBox3D *box) {
456 box->orig_corner0.normalize();
457 box->orig_corner7.normalize();
458 return Proj::Pt3 ((box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2,
459 (box->orig_corner0[Proj::Y] + box->orig_corner7[Proj::Y]) / 2,
460 (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2,
461 1.0);
462 }
464 NR::Point
465 box3d_get_center_screen (SPBox3D *box) {
466 Proj::Pt3 proj_center (box3d_get_proj_center (box));
467 if (!box3d_get_perspective(box)) {
468 //g_print ("No perspective present in box!! Should we simply use the currently active perspective?\n");
469 return NR::Point (NR_HUGE, NR_HUGE);
470 }
471 return box3d_get_perspective(box)->tmat.image(proj_center).affine();
472 }
474 /*
475 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
476 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
477 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
478 */
480 // Should we make the threshold settable in the preferences?
481 static double remember_snap_threshold = 30;
482 //static guint remember_snap_index = 0;
483 static guint remember_snap_index_center = 0;
485 static Proj::Pt3
486 box3d_snap (SPBox3D *box, int id, Proj::Pt3 const &pt_proj, Proj::Pt3 const &start_pt) {
487 double z_coord = start_pt[Proj::Z];
488 double diff_x = box->save_corner7[Proj::X] - box->save_corner0[Proj::X];
489 double diff_y = box->save_corner7[Proj::Y] - box->save_corner0[Proj::Y];
490 double x_coord = start_pt[Proj::X];
491 double y_coord = start_pt[Proj::Y];
492 Proj::Pt3 A_proj (x_coord, y_coord, z_coord, 1.0);
493 Proj::Pt3 B_proj (x_coord + diff_x, y_coord, z_coord, 1.0);
494 Proj::Pt3 C_proj (x_coord + diff_x, y_coord + diff_y, z_coord, 1.0);
495 Proj::Pt3 D_proj (x_coord, y_coord + diff_y, z_coord, 1.0);
496 Proj::Pt3 E_proj (x_coord - diff_x, y_coord + diff_y, z_coord, 1.0);
498 Persp3D *persp = box3d_get_perspective(box);
499 NR::Point A = persp->tmat.image(A_proj).affine();
500 NR::Point B = persp->tmat.image(B_proj).affine();
501 NR::Point C = persp->tmat.image(C_proj).affine();
502 NR::Point D = persp->tmat.image(D_proj).affine();
503 NR::Point E = persp->tmat.image(E_proj).affine();
504 NR::Point pt = persp->tmat.image(pt_proj).affine();
506 // TODO: Replace these lines between corners with lines from a corner to a vanishing point
507 // (this might help to prevent rounding errors if the box is small)
508 Box3D::Line pl1(A, B);
509 Box3D::Line pl2(A, D);
510 Box3D::Line diag1(A, (id == -1 || (!(id & Box3D::X) == !(id & Box3D::Y))) ? C : E);
511 Box3D::Line diag2(A, E); // diag2 is only taken into account if id equals -1, i.e., if we are snapping the center
513 int num_snap_lines = (id != -1) ? 3 : 4;
514 NR::Point snap_pts[num_snap_lines];
516 snap_pts[0] = pl1.closest_to (pt);
517 snap_pts[1] = pl2.closest_to (pt);
518 snap_pts[2] = diag1.closest_to (pt);
519 if (id == -1) {
520 snap_pts[3] = diag2.closest_to (pt);
521 }
523 gdouble const zoom = inkscape_active_desktop()->current_zoom();
525 // determine the distances to all potential snapping points
526 double snap_dists[num_snap_lines];
527 for (int i = 0; i < num_snap_lines; ++i) {
528 snap_dists[i] = NR::L2 (snap_pts[i] - pt) * zoom;
529 }
531 // while we are within a given tolerance of the starting point,
532 // keep snapping to the same point to avoid jumping
533 bool within_tolerance = true;
534 for (int i = 0; i < num_snap_lines; ++i) {
535 if (snap_dists[i] > remember_snap_threshold) {
536 within_tolerance = false;
537 break;
538 }
539 }
541 // find the closest snapping point
542 int snap_index = -1;
543 double snap_dist = NR_HUGE;
544 for (int i = 0; i < num_snap_lines; ++i) {
545 if (snap_dists[i] < snap_dist) {
546 snap_index = i;
547 snap_dist = snap_dists[i];
548 }
549 }
551 // snap to the closest point (or the previously remembered one
552 // if we are within tolerance of the starting point)
553 NR::Point result;
554 if (within_tolerance) {
555 result = snap_pts[remember_snap_index_center];
556 } else {
557 remember_snap_index_center = snap_index;
558 result = snap_pts[snap_index];
559 }
560 return box3d_get_perspective(box)->tmat.preimage (result, z_coord, Proj::Z);
561 }
563 void
564 box3d_set_corner (SPBox3D *box, const guint id, NR::Point const &new_pos, const Box3D::Axis movement, bool constrained) {
565 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
567 box->orig_corner0.normalize();
568 box->orig_corner7.normalize();
570 /* update corners 0 and 7 according to which handle was moved and to the axes of movement */
571 if (!(movement & Box3D::Z)) {
572 Proj::Pt3 pt_proj (box3d_get_perspective(box)->tmat.preimage (new_pos, (id < 4) ? box->orig_corner0[Proj::Z] :
573 box->orig_corner7[Proj::Z], Proj::Z));
574 if (constrained) {
575 pt_proj = box3d_snap (box, id, pt_proj, box3d_get_proj_corner (id, box->save_corner0, box->save_corner7));
576 }
578 // normalizing pt_proj is essential because we want to mingle affine coordinates
579 pt_proj.normalize();
580 box->orig_corner0 = Proj::Pt3 ((id & Box3D::X) ? box->save_corner0[Proj::X] : pt_proj[Proj::X],
581 (id & Box3D::Y) ? box->save_corner0[Proj::Y] : pt_proj[Proj::Y],
582 box->save_corner0[Proj::Z],
583 1.0);
584 box->orig_corner7 = Proj::Pt3 ((id & Box3D::X) ? pt_proj[Proj::X] : box->save_corner7[Proj::X],
585 (id & Box3D::Y) ? pt_proj[Proj::Y] : box->save_corner7[Proj::Y],
586 box->save_corner7[Proj::Z],
587 1.0);
588 } else {
589 Persp3D *persp = box3d_get_perspective(box);
590 Box3D::PerspectiveLine pl(persp->tmat.image(
591 box3d_get_proj_corner (id, box->save_corner0, box->save_corner7)).affine(),
592 Proj::Z, persp);
593 NR::Point new_pos_snapped(pl.closest_to(new_pos));
594 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped,
595 box3d_get_proj_corner (box, id)[(movement & Box3D::Y) ? Proj::X : Proj::Y],
596 (movement & Box3D::Y) ? Proj::X : Proj::Y));
597 bool corner0_move_x = !(id & Box3D::X) && (movement & Box3D::X);
598 bool corner0_move_y = !(id & Box3D::Y) && (movement & Box3D::Y);
599 bool corner7_move_x = (id & Box3D::X) && (movement & Box3D::X);
600 bool corner7_move_y = (id & Box3D::Y) && (movement & Box3D::Y);
601 // normalizing pt_proj is essential because we want to mingle affine coordinates
602 pt_proj.normalize();
603 box->orig_corner0 = Proj::Pt3 (corner0_move_x ? pt_proj[Proj::X] : box->orig_corner0[Proj::X],
604 corner0_move_y ? pt_proj[Proj::Y] : box->orig_corner0[Proj::Y],
605 (id & Box3D::Z) ? box->orig_corner0[Proj::Z] : pt_proj[Proj::Z],
606 1.0);
607 box->orig_corner7 = Proj::Pt3 (corner7_move_x ? pt_proj[Proj::X] : box->orig_corner7[Proj::X],
608 corner7_move_y ? pt_proj[Proj::Y] : box->orig_corner7[Proj::Y],
609 (id & Box3D::Z) ? pt_proj[Proj::Z] : box->orig_corner7[Proj::Z],
610 1.0);
611 }
612 // FIXME: Should we update the box here? If so, how?
613 }
615 void box3d_set_center (SPBox3D *box, NR::Point const &new_pos, NR::Point const &old_pos, const Box3D::Axis movement, bool constrained) {
616 g_return_if_fail ((movement != Box3D::NONE) && (movement != Box3D::XYZ));
618 Persp3D *persp = box3d_get_perspective(box);
619 if (!(movement & Box3D::Z)) {
620 double coord = (box->orig_corner0[Proj::Z] + box->orig_corner7[Proj::Z]) / 2;
621 double radx = (box->orig_corner7[Proj::X] - box->orig_corner0[Proj::X]) / 2;
622 double rady = (box->orig_corner7[Proj::Y] - box->orig_corner0[Proj::Y]) / 2;
624 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos, coord, Proj::Z));
625 if (constrained) {
626 Proj::Pt3 old_pos_proj (persp->tmat.preimage (old_pos, coord, Proj::Z));
627 pt_proj = box3d_snap (box, -1, pt_proj, old_pos_proj);
628 }
629 // normalizing pt_proj is essential because we want to mingle affine coordinates
630 pt_proj.normalize();
631 box->orig_corner0 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] - radx : box->orig_corner0[Proj::X],
632 (movement & Box3D::Y) ? pt_proj[Proj::Y] - rady : box->orig_corner0[Proj::Y],
633 box->orig_corner0[Proj::Z],
634 1.0);
635 box->orig_corner7 = Proj::Pt3 ((movement & Box3D::X) ? pt_proj[Proj::X] + radx : box->orig_corner7[Proj::X],
636 (movement & Box3D::Y) ? pt_proj[Proj::Y] + rady : box->orig_corner7[Proj::Y],
637 box->orig_corner7[Proj::Z],
638 1.0);
639 } else {
640 double coord = (box->orig_corner0[Proj::X] + box->orig_corner7[Proj::X]) / 2;
641 double radz = (box->orig_corner7[Proj::Z] - box->orig_corner0[Proj::Z]) / 2;
643 Box3D::PerspectiveLine pl(old_pos, Proj::Z, persp);
644 NR::Point new_pos_snapped(pl.closest_to(new_pos));
645 Proj::Pt3 pt_proj (persp->tmat.preimage (new_pos_snapped, coord, Proj::X));
647 /* normalizing pt_proj is essential because we want to mingle affine coordinates */
648 pt_proj.normalize();
649 box->orig_corner0 = Proj::Pt3 (box->orig_corner0[Proj::X],
650 box->orig_corner0[Proj::Y],
651 pt_proj[Proj::Z] - radz,
652 1.0);
653 box->orig_corner7 = Proj::Pt3 (box->orig_corner7[Proj::X],
654 box->orig_corner7[Proj::Y],
655 pt_proj[Proj::Z] + radz,
656 1.0);
657 }
658 }
660 /*
661 * Manipulates corner1 through corner4 to contain the indices of the corners
662 * from which the perspective lines in the direction of 'axis' emerge
663 */
664 void box3d_corners_for_PLs (const SPBox3D * box, Proj::Axis axis,
665 NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
666 {
667 Persp3D *persp = box3d_get_perspective(box);
668 g_return_if_fail (persp);
669 //box->orig_corner0.normalize();
670 //box->orig_corner7.normalize();
671 double coord = (box->orig_corner0[axis] > box->orig_corner7[axis]) ?
672 box->orig_corner0[axis] :
673 box->orig_corner7[axis];
675 Proj::Pt3 c1, c2, c3, c4;
676 // FIXME: This can certainly be done more elegantly/efficiently than by a case-by-case analysis.
677 switch (axis) {
678 case Proj::X:
679 c1 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
680 c2 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner0[Proj::Z], 1.0);
681 c3 = Proj::Pt3 (coord, box->orig_corner7[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
682 c4 = Proj::Pt3 (coord, box->orig_corner0[Proj::Y], box->orig_corner7[Proj::Z], 1.0);
683 break;
684 case Proj::Y:
685 c1 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
686 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner0[Proj::Z], 1.0);
687 c3 = Proj::Pt3 (box->orig_corner7[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
688 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], coord, box->orig_corner7[Proj::Z], 1.0);
689 break;
690 case Proj::Z:
691 c1 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
692 c2 = Proj::Pt3 (box->orig_corner7[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
693 c3 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner0[Proj::Y], coord, 1.0);
694 c4 = Proj::Pt3 (box->orig_corner0[Proj::X], box->orig_corner7[Proj::Y], coord, 1.0);
695 break;
696 default:
697 return;
698 }
699 corner1 = persp->tmat.image(c1).affine();
700 corner2 = persp->tmat.image(c2).affine();
701 corner3 = persp->tmat.image(c3).affine();
702 corner4 = persp->tmat.image(c4).affine();
703 }
705 /* Auxiliary function: Checks whether the half-line from A to B crosses the line segment joining C and D */
706 static bool
707 box3d_half_line_crosses_joining_line (Geom::Point const &A, Geom::Point const &B,
708 Geom::Point const &C, Geom::Point const &D) {
709 Geom::Point E; // the point of intersection
710 Geom::Point n0 = (B - A).ccw();
711 double d0 = dot(n0,A);
713 Geom::Point n1 = (D - C).ccw();
714 double d1 = dot(n1,C);
715 Geom::IntersectorKind intersects = Geom::line_intersection(n0, d0, n1, d1, E);
716 if (intersects == Geom::coincident || intersects == Geom::parallel) {
717 return false;
718 }
720 if ((dot(C,n0) < d0) == (dot(D,n0) < d0)) {
721 // C and D lie on the same side of the line AB
722 return false;
723 }
724 if ((dot(A,n1) < d1) != (dot(B,n1) < d1)) {
725 // A and B lie on different sides of the line CD
726 return true;
727 } else if (Geom::distance(E,A) < Geom::distance(E,B)) {
728 // The line CD passes on the "wrong" side of A
729 return false;
730 }
732 // The line CD passes on the "correct" side of A
733 return true;
734 }
736 static bool
737 box3d_XY_axes_are_swapped (SPBox3D *box) {
738 Persp3D *persp = box3d_get_perspective(box);
739 g_return_val_if_fail(persp, false);
740 Box3D::PerspectiveLine l1(box3d_get_corner_screen(box, 3), Proj::X, persp);
741 Box3D::PerspectiveLine l2(box3d_get_corner_screen(box, 3), Proj::Y, persp);
742 NR::Point v1(l1.direction());
743 NR::Point v2(l2.direction());
744 v1.normalize();
745 v2.normalize();
747 return (v1[NR::X]*v2[NR::Y] - v1[NR::Y]*v2[NR::X] > 0);
748 }
750 static inline void
751 box3d_aux_set_z_orders (int z_orders[6], int a, int b, int c, int d, int e, int f) {
752 z_orders[0] = a;
753 z_orders[1] = b;
754 z_orders[2] = c;
755 z_orders[3] = d;
756 z_orders[4] = e;
757 z_orders[5] = f;
758 }
760 static inline void
761 box3d_swap_z_orders (int z_orders[6]) {
762 int tmp;
763 for (int i = 0; i < 3; ++i) {
764 tmp = z_orders[i];
765 z_orders[i] = z_orders[5-i];
766 z_orders[5-i] = tmp;
767 }
768 }
770 /*
771 * In standard perspective we have:
772 * 2 = front face
773 * 1 = top face
774 * 0 = left face
775 * 3 = right face
776 * 4 = bottom face
777 * 5 = rear face
778 */
780 /* All VPs infinite */
781 static void
782 box3d_set_new_z_orders_case0 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis) {
783 Persp3D *persp = box3d_get_perspective(box);
784 NR::Point xdir(persp3d_get_infinite_dir(persp, Proj::X));
785 NR::Point ydir(persp3d_get_infinite_dir(persp, Proj::Y));
786 NR::Point zdir(persp3d_get_infinite_dir(persp, Proj::Z));
788 bool swapped = box3d_XY_axes_are_swapped(box);
790 //g_print ("3 infinite VPs; ");
791 switch(central_axis) {
792 case Box3D::X:
793 if (!swapped) {
794 //g_print ("central axis X (case a)");
795 box3d_aux_set_z_orders (z_orders, 2, 0, 4, 1, 3, 5);
796 } else {
797 //g_print ("central axis X (case b)");
798 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
799 }
800 break;
801 case Box3D::Y:
802 if (!swapped) {
803 //g_print ("central axis Y (case a)");
804 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
805 } else {
806 //g_print ("central axis Y (case b)");
807 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
808 }
809 break;
810 case Box3D::Z:
811 if (!swapped) {
812 //g_print ("central axis Z (case a)");
813 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
814 } else {
815 //g_print ("central axis Z (case b)");
816 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
817 }
818 break;
819 case Box3D::NONE:
820 if (!swapped) {
821 //g_print ("central axis NONE (case a)");
822 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
823 } else {
824 //g_print ("central axis NONE (case b)");
825 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
826 }
827 break;
828 default:
829 g_assert_not_reached();
830 break;
831 }
832 /**
833 if (swapped) {
834 g_print ("; swapped");
835 }
836 g_print ("\n");
837 **/
838 }
840 /* Precisely one finite VP */
841 static void
842 box3d_set_new_z_orders_case1 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis fin_axis) {
843 Persp3D *persp = box3d_get_perspective(box);
844 NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(fin_axis)).affine());
846 // note: in some of the case distinctions below we rely upon the fact that oaxis1 and oaxis2 are ordered
847 Box3D::Axis oaxis1 = Box3D::get_remaining_axes(fin_axis).first;
848 Box3D::Axis oaxis2 = Box3D::get_remaining_axes(fin_axis).second;
849 //g_print ("oaxis1 = %s, oaxis2 = %s\n", Box3D::string_from_axes(oaxis1), Box3D::string_from_axes(oaxis2));
850 int inside1 = 0;
851 int inside2 = 0;
852 inside1 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis2, oaxis1);
853 inside2 = box3d_pt_lies_in_PL_sector (box, vp, 3, 3 ^ oaxis1, oaxis2);
854 //g_print ("inside1 = %d, inside2 = %d\n", inside1, inside2);
856 bool swapped = box3d_XY_axes_are_swapped(box);
858 //g_print ("2 infinite VPs; ");
859 //g_print ("finite axis: %s; ", Box3D::string_from_axes(fin_axis));
860 switch(central_axis) {
861 case Box3D::X:
862 if (!swapped) {
863 //g_print ("central axis X (case a)");
864 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
865 } else {
866 //if (inside2) {
867 //g_print ("central axis X (case b)");
868 box3d_aux_set_z_orders (z_orders, 5, 3, 1, 0, 2, 4);
869 //} else {
870 //g_print ("central axis X (case c)");
871 //box3d_aux_set_z_orders (z_orders, 5, 3, 1, 2, 0, 4);
872 //}
873 }
874 break;
875 case Box3D::Y:
876 if (inside2 > 0) {
877 //g_print ("central axis Y (case a)");
878 box3d_aux_set_z_orders (z_orders, 1, 2, 3, 0, 5, 4);
879 } else if (inside2 < 0) {
880 //g_print ("central axis Y (case b)");
881 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 4, 0, 5);
882 } else {
883 if (!swapped) {
884 //g_print ("central axis Y (case c1)");
885 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
886 } else {
887 //g_print ("central axis Y (case c2)");
888 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
889 }
890 }
891 break;
892 case Box3D::Z:
893 if (inside2) {
894 if (!swapped) {
895 //g_print ("central axis Z (case a1)");
896 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 4, 5);
897 } else {
898 //g_print ("central axis Z (case a2)");
899 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 1, 2);
900 }
901 } else if (inside1) {
902 if (!swapped) {
903 //g_print ("central axis Z (case b1)");
904 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
905 } else {
906 //g_print ("central axis Z (case b2)");
907 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 1, 0, 2);
908 //box3d_aux_set_z_orders (z_orders, 5, 3, 0, 1, 2, 4);
909 }
910 } else {
911 // "regular" case
912 if (!swapped) {
913 //g_print ("central axis Z (case c1)");
914 box3d_aux_set_z_orders (z_orders, 0, 1, 2, 5, 4, 3);
915 } else {
916 //g_print ("central axis Z (case c2)");
917 box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
918 //box3d_aux_set_z_orders (z_orders, 5, 3, 4, 0, 2, 1);
919 }
920 }
921 break;
922 case Box3D::NONE:
923 if (!swapped) {
924 //g_print ("central axis NONE (case a)");
925 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 5, 0, 1);
926 } else {
927 //g_print ("central axis NONE (case b)");
928 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 3, 2, 4);
929 //box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
930 }
931 break;
932 default:
933 g_assert_not_reached();
934 }
935 /**
936 if (swapped) {
937 g_print ("; swapped");
938 }
939 g_print ("\n");
940 **/
941 }
943 /* Precisely 2 finite VPs */
944 static void
945 box3d_set_new_z_orders_case2 (SPBox3D *box, int z_orders[6], Box3D::Axis central_axis, Box3D::Axis /*infinite_axis*/) {
946 Persp3D *persp = box3d_get_perspective(box);
948 NR::Point c3(box3d_get_corner_screen(box, 3));
949 NR::Point xdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::X));
950 NR::Point ydir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Y));
951 NR::Point zdir(persp3d_get_PL_dir_from_pt(persp, c3, Proj::Z));
953 bool swapped = box3d_XY_axes_are_swapped(box);
955 int insidexy = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Z, Box3D::Y);
956 int insidexz = box3d_VP_lies_in_PL_sector (box, Proj::X, 3, 3 ^ Box3D::Y, Box3D::Z);
958 int insideyx = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::Z, Box3D::X);
959 int insideyz = box3d_VP_lies_in_PL_sector (box, Proj::Y, 3, 3 ^ Box3D::X, Box3D::Z);
961 int insidezx = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::Y, Box3D::X);
962 int insidezy = box3d_VP_lies_in_PL_sector (box, Proj::Z, 3, 3 ^ Box3D::X, Box3D::Y);
964 //g_print ("Insides: xy = %d, xz = %d, yx = %d, yz = %d, zx = %d, zy = %d\n",
965 // insidexy, insidexz, insideyx, insideyz, insidezx, insidezy);
966 (void)insidexz;
967 (void)insideyx;
968 (void)insidezx;
970 //g_print ("1 infinite VP; ");
971 switch(central_axis) {
972 case Box3D::X:
973 if (!swapped) {
974 if (insidezy == -1) {
975 //g_print ("central axis X (case a1)");
976 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
977 } else if (insidexy == 1) {
978 //g_print ("central axis X (case a2)");
979 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
980 } else {
981 //g_print ("central axis X (case a3)");
982 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 1, 3, 5);
983 }
984 } else {
985 if (insideyz == -1) {
986 //g_print ("central axis X (case b1)");
987 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
988 } else {
989 if (!swapped) {
990 //g_print ("central axis X (case b2)");
991 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 2, 4, 0);
992 } else {
993 //g_print ("central axis X (case b3)");
994 box3d_aux_set_z_orders (z_orders, 3, 1, 5, 0, 2, 4);
995 }
996 }
997 }
998 break;
999 case Box3D::Y:
1000 if (!swapped) {
1001 if (insideyz == 1) {
1002 //g_print ("central axis Y (case a1)");
1003 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 0, 5, 4);
1004 } else {
1005 //g_print ("central axis Y (case a2)");
1006 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1007 }
1008 } else {
1009 //g_print ("central axis Y (case b)");
1010 box3d_aux_set_z_orders (z_orders, 5, 0, 4, 1, 3, 2);
1011 }
1012 break;
1013 case Box3D::Z:
1014 if (!swapped) {
1015 if (insidezy == 1) {
1016 //g_print ("central axis Z (case a1)");
1017 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 3, 5);
1018 } else if (insidexy == -1) {
1019 //g_print ("central axis Z (case a2)");
1020 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 4, 3);
1021 } else {
1022 //g_print ("central axis Z (case a3)");
1023 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 5, 3, 4);
1024 }
1025 } else {
1026 //g_print ("central axis Z (case b)");
1027 box3d_aux_set_z_orders (z_orders, 3, 4, 5, 1, 0, 2);
1028 }
1029 break;
1030 case Box3D::NONE:
1031 if (!swapped) {
1032 //g_print ("central axis NONE (case a)");
1033 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1034 } else {
1035 //g_print ("central axis NONE (case b)");
1036 box3d_aux_set_z_orders (z_orders, 5, 0, 1, 4, 3, 2);
1037 }
1038 break;
1039 default:
1040 g_assert_not_reached();
1041 break;
1042 }
1043 /**
1044 if (swapped) {
1045 g_print ("; swapped");
1046 }
1047 g_print ("\n");
1048 **/
1049 }
1051 /*
1052 * It can happen that during dragging the box is everted.
1053 * In this case the opposite sides in this direction need to be swapped
1054 */
1055 static Box3D::Axis
1056 box3d_everted_directions (SPBox3D *box) {
1057 Box3D::Axis ev = Box3D::NONE;
1059 box->orig_corner0.normalize();
1060 box->orig_corner7.normalize();
1062 if (box->orig_corner0[Proj::X] < box->orig_corner7[Proj::X])
1063 ev = (Box3D::Axis) (ev ^ Box3D::X);
1064 if (box->orig_corner0[Proj::Y] < box->orig_corner7[Proj::Y])
1065 ev = (Box3D::Axis) (ev ^ Box3D::Y);
1066 if (box->orig_corner0[Proj::Z] > box->orig_corner7[Proj::Z]) // FIXME: Remove the need to distinguish signs among the cases
1067 ev = (Box3D::Axis) (ev ^ Box3D::Z);
1069 return ev;
1070 }
1072 static void
1073 box3d_swap_sides(int z_orders[6], Box3D::Axis axis) {
1074 int pos1 = -1;
1075 int pos2 = -1;
1077 for (int i = 0; i < 6; ++i) {
1078 if (!(Box3D::int_to_face(z_orders[i]) & axis)) {
1079 if (pos1 == -1) {
1080 pos1 = i;
1081 } else {
1082 pos2 = i;
1083 break;
1084 }
1085 }
1086 }
1088 int tmp = z_orders[pos1];
1089 z_orders[pos1] = z_orders[pos2];
1090 z_orders[pos2] = tmp;
1091 }
1094 bool
1095 box3d_recompute_z_orders (SPBox3D *box) {
1096 Persp3D *persp = box3d_get_perspective(box);
1098 //g_return_val_if_fail(persp, false);
1099 if (!persp)
1100 return false;
1102 int z_orders[6];
1104 NR::Point c3(box3d_get_corner_screen(box, 3));
1106 // determine directions from corner3 to the VPs
1107 int num_finite = 0;
1108 Box3D::Axis axis_finite = Box3D::NONE;
1109 Box3D::Axis axis_infinite = Box3D::NONE;
1110 NR::Point dirs[3];
1111 for (int i = 0; i < 3; ++i) {
1112 dirs[i] = persp3d_get_PL_dir_from_pt(persp, c3, Box3D::toProj(Box3D::axes[i]));
1113 if (persp3d_VP_is_finite(persp, Proj::axes[i])) {
1114 num_finite++;
1115 axis_finite = Box3D::axes[i];
1116 } else {
1117 axis_infinite = Box3D::axes[i];
1118 }
1119 }
1121 // determine the "central" axis (if there is one)
1122 Box3D::Axis central_axis = Box3D::NONE;
1123 if(Box3D::lies_in_sector(dirs[0], dirs[1], dirs[2])) {
1124 central_axis = Box3D::Z;
1125 } else if(Box3D::lies_in_sector(dirs[1], dirs[2], dirs[0])) {
1126 central_axis = Box3D::X;
1127 } else if(Box3D::lies_in_sector(dirs[2], dirs[0], dirs[1])) {
1128 central_axis = Box3D::Y;
1129 }
1131 switch (num_finite) {
1132 case 0:
1133 // TODO: Remark: In this case (and maybe one of the others, too) the z-orders for all boxes
1134 // coincide, hence only need to be computed once in a more central location.
1135 box3d_set_new_z_orders_case0(box, z_orders, central_axis);
1136 break;
1137 case 1:
1138 box3d_set_new_z_orders_case1(box, z_orders, central_axis, axis_finite);
1139 break;
1140 case 2:
1141 case 3:
1142 box3d_set_new_z_orders_case2(box, z_orders, central_axis, axis_infinite);
1143 break;
1144 default:
1145 /*
1146 * For each VP F, check wether the half-line from the corner3 to F crosses the line segment
1147 * joining the other two VPs. If this is the case, it determines the "central" corner from
1148 * which the visible sides can be deduced. Otherwise, corner3 is the central corner.
1149 */
1150 // FIXME: We should eliminate the use of NR::Point altogether
1151 Box3D::Axis central_axis = Box3D::NONE;
1152 NR::Point vp_x = persp3d_get_VP(persp, Proj::X).affine();
1153 NR::Point vp_y = persp3d_get_VP(persp, Proj::Y).affine();
1154 NR::Point vp_z = persp3d_get_VP(persp, Proj::Z).affine();
1155 Geom::Point vpx(vp_x[NR::X], vp_x[NR::Y]);
1156 Geom::Point vpy(vp_y[NR::X], vp_y[NR::Y]);
1157 Geom::Point vpz(vp_z[NR::X], vp_z[NR::Y]);
1159 NR::Point c3 = box3d_get_corner_screen(box, 3);
1160 Geom::Point corner3(c3[NR::X], c3[NR::Y]);
1162 if (box3d_half_line_crosses_joining_line (corner3, vpx, vpy, vpz)) {
1163 central_axis = Box3D::X;
1164 } else if (box3d_half_line_crosses_joining_line (corner3, vpy, vpz, vpx)) {
1165 central_axis = Box3D::Y;
1166 } else if (box3d_half_line_crosses_joining_line (corner3, vpz, vpx, vpy)) {
1167 central_axis = Box3D::Z;
1168 }
1169 //g_print ("Crossing: %s\n", Box3D::string_from_axes(central_axis));
1171 unsigned int central_corner = 3 ^ central_axis;
1172 if (central_axis == Box3D::Z) {
1173 central_corner = central_corner ^ Box3D::XYZ;
1174 }
1175 if (box3d_XY_axes_are_swapped(box)) {
1176 //g_print ("Axes X and Y are swapped\n");
1177 central_corner = central_corner ^ Box3D::XYZ;
1178 }
1180 NR::Point c1(box3d_get_corner_screen(box, 1));
1181 NR::Point c2(box3d_get_corner_screen(box, 2));
1182 NR::Point c7(box3d_get_corner_screen(box, 7));
1184 Geom::Point corner1(c1[NR::X], c1[NR::Y]);
1185 Geom::Point corner2(c2[NR::X], c2[NR::Y]);
1186 Geom::Point corner7(c7[NR::X], c7[NR::Y]);
1187 // FIXME: At present we don't use the information about central_corner computed above.
1188 switch (central_axis) {
1189 case Box3D::Y:
1190 if (!box3d_half_line_crosses_joining_line(vpz, vpy, corner3, corner2)) {
1191 box3d_aux_set_z_orders (z_orders, 2, 3, 1, 5, 0, 4);
1192 } else {
1193 // degenerate case
1194 //g_print ("Degenerate case #1\n");
1195 box3d_aux_set_z_orders (z_orders, 2, 1, 3, 0, 5, 4);
1196 }
1197 break;
1199 case Box3D::Z:
1200 if (box3d_half_line_crosses_joining_line(vpx, vpz, corner3, corner1)) {
1201 // degenerate case
1202 //g_print ("Degenerate case #2\n");
1203 box3d_aux_set_z_orders (z_orders, 2, 0, 1, 4, 3, 5);
1204 } else if (box3d_half_line_crosses_joining_line(vpx, vpy, corner3, corner7)) {
1205 // degenerate case
1206 //g_print ("Degenerate case #3\n");
1207 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 5, 3, 4);
1208 } else {
1209 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 3, 4, 5);
1210 }
1211 break;
1213 case Box3D::X:
1214 if (box3d_half_line_crosses_joining_line(vpz, vpx, corner3, corner1)) {
1215 // degenerate case
1216 //g_print ("Degenerate case #4\n");
1217 box3d_aux_set_z_orders (z_orders, 2, 1, 0, 4, 5, 3);
1218 } else {
1219 box3d_aux_set_z_orders (z_orders, 2, 4, 0, 5, 1, 3);
1220 }
1221 break;
1223 case Box3D::NONE:
1224 box3d_aux_set_z_orders (z_orders, 2, 3, 4, 1, 0, 5);
1225 break;
1227 default:
1228 g_assert_not_reached();
1229 break;
1230 } // end default case
1231 }
1233 // TODO: If there are still errors in z-orders of everted boxes, we need to choose a variable corner
1234 // instead of the hard-coded corner #3 in the computations above
1235 Box3D::Axis ev = box3d_everted_directions(box);
1236 for (int i = 0; i < 3; ++i) {
1237 if (ev & Box3D::axes[i]) {
1238 box3d_swap_sides(z_orders, Box3D::axes[i]);
1239 }
1240 }
1242 // Check whether anything actually changed
1243 for (int i = 0; i < 6; ++i) {
1244 if (box->z_orders[i] != z_orders[i]) {
1245 for (int j = i; j < 6; ++j) {
1246 box->z_orders[j] = z_orders[j];
1247 }
1248 return true;
1249 }
1250 }
1251 return false;
1252 }
1254 static std::map<int, Box3DSide *>
1255 box3d_get_sides (SPBox3D *box) {
1256 std::map<int, Box3DSide *> sides;
1257 for (SPObject *side = sp_object_first_child(box); side != NULL; side = SP_OBJECT_NEXT(side)) {
1258 sides[Box3D::face_to_int(sp_repr_get_int_attribute(SP_OBJECT_REPR(side),
1259 "inkscape:box3dsidetype", -1))] = SP_BOX3D_SIDE(side);
1260 }
1261 sides.erase(-1);
1262 return sides;
1263 }
1266 // TODO: Check whether the box is everted in any direction and swap the sides opposite to this direction
1267 void
1268 box3d_set_z_orders (SPBox3D *box) {
1269 // For efficiency reasons, we only set the new z-orders if something really changed
1270 if (box3d_recompute_z_orders (box)) {
1271 std::map<int, Box3DSide *> sides = box3d_get_sides(box);
1272 std::map<int, Box3DSide *>::iterator side;
1273 for (unsigned int i = 0; i < 6; ++i) {
1274 side = sides.find(box->z_orders[i]);
1275 if (side != sides.end()) {
1276 SP_ITEM((*side).second)->lowerToBottom();
1277 }
1278 }
1279 /**
1280 g_print ("Resetting z-orders: ");
1281 for (int i = 0; i < 6; ++i) {
1282 g_print ("%d ", box->z_orders[i]);
1283 }
1284 g_print ("\n");
1285 **/
1286 }
1287 }
1289 /*
1290 * Auxiliary function for z-order recomputing:
1291 * Determines whether \a pt lies in the sector formed by the two PLs from the corners with IDs
1292 * \a i21 and \a id2 to the VP in direction \a axis. If the VP is infinite, we say that \a pt
1293 * lies in the sector if it lies between the two (parallel) PLs.
1294 * \ret * 0 if \a pt doesn't lie in the sector
1295 * * 1 if \a pt lies in the sector and either VP is finite of VP is infinite and the direction
1296 * from the edge between the two corners to \a pt points towards the VP
1297 * * -1 otherwise
1298 */
1299 // TODO: Maybe it would be useful to have a similar method for projective points pt because then we
1300 // can use it for VPs and perhaps merge the case distinctions during z-order recomputation.
1301 int
1302 box3d_pt_lies_in_PL_sector (SPBox3D const *box, NR::Point const &pt, int id1, int id2, Box3D::Axis axis) {
1303 Persp3D *persp = box3d_get_perspective(box);
1305 // the two corners
1306 NR::Point c1(box3d_get_corner_screen(box, id1));
1307 NR::Point c2(box3d_get_corner_screen(box, id2));
1309 int ret = 0;
1310 if (persp3d_VP_is_finite(persp, Box3D::toProj(axis))) {
1311 NR::Point vp(persp3d_get_VP(persp, Box3D::toProj(axis)).affine());
1312 NR::Point v1(c1 - vp);
1313 NR::Point v2(c2 - vp);
1314 NR::Point w(pt - vp);
1315 ret = static_cast<int>(Box3D::lies_in_sector(v1, v2, w));
1316 //g_print ("Case 0 - returning %d\n", ret);
1317 } else {
1318 Box3D::PerspectiveLine pl1(c1, Box3D::toProj(axis), persp);
1319 Box3D::PerspectiveLine pl2(c2, Box3D::toProj(axis), persp);
1320 if (pl1.lie_on_same_side(pt, c2) && pl2.lie_on_same_side(pt, c1)) {
1321 // test whether pt lies "towards" or "away from" the VP
1322 Box3D::Line edge(c1,c2);
1323 NR::Point c3(box3d_get_corner_screen(box, id1 ^ axis));
1324 if (edge.lie_on_same_side(pt, c3)) {
1325 ret = 1;
1326 } else {
1327 ret = -1;
1328 }
1329 }
1330 //g_print ("Case 1 - returning %d\n", ret);
1331 }
1332 return ret;
1333 }
1335 int
1336 box3d_VP_lies_in_PL_sector (SPBox3D const *box, Proj::Axis vpdir, int id1, int id2, Box3D::Axis axis) {
1337 Persp3D *persp = box3d_get_perspective(box);
1339 if (!persp3d_VP_is_finite(persp, vpdir)) {
1340 return 0;
1341 } else {
1342 return box3d_pt_lies_in_PL_sector(box, persp3d_get_VP(persp, vpdir).affine(), id1, id2, axis);
1343 }
1344 }
1346 /* swap the coordinates of corner0 and corner7 along the specified axis */
1347 static void
1348 box3d_swap_coords(SPBox3D *box, Proj::Axis axis, bool smaller = true) {
1349 box->orig_corner0.normalize();
1350 box->orig_corner7.normalize();
1351 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1352 double tmp = box->orig_corner0[axis];
1353 box->orig_corner0[axis] = box->orig_corner7[axis];
1354 box->orig_corner7[axis] = tmp;
1355 }
1356 // FIXME: Should we also swap the coordinates of save_corner0 and save_corner7?
1357 }
1359 /* ensure that the coordinates of corner0 and corner7 are in the correct order (to prevent everted boxes) */
1360 void
1361 box3d_relabel_corners(SPBox3D *box) {
1362 box3d_swap_coords(box, Proj::X, false);
1363 box3d_swap_coords(box, Proj::Y, false);
1364 box3d_swap_coords(box, Proj::Z, true);
1365 }
1367 static void
1368 box3d_check_for_swapped_coords(SPBox3D *box, Proj::Axis axis, bool smaller) {
1369 box->orig_corner0.normalize();
1370 box->orig_corner7.normalize();
1372 if ((box->orig_corner0[axis] < box->orig_corner7[axis]) != smaller) {
1373 box->swapped = (Box3D::Axis) (box->swapped | Proj::toAffine(axis));
1374 } else {
1375 box->swapped = (Box3D::Axis) (box->swapped & ~Proj::toAffine(axis));
1376 }
1377 }
1379 static void
1380 box3d_exchange_coords(SPBox3D *box) {
1381 box->orig_corner0.normalize();
1382 box->orig_corner7.normalize();
1384 for (int i = 0; i < 3; ++i) {
1385 if (box->swapped & Box3D::axes[i]) {
1386 double tmp = box->orig_corner0[i];
1387 box->orig_corner0[i] = box->orig_corner7[i];
1388 box->orig_corner7[i] = tmp;
1389 }
1390 }
1391 }
1393 void
1394 box3d_check_for_swapped_coords(SPBox3D *box) {
1395 box3d_check_for_swapped_coords(box, Proj::X, false);
1396 box3d_check_for_swapped_coords(box, Proj::Y, false);
1397 box3d_check_for_swapped_coords(box, Proj::Z, true);
1399 box3d_exchange_coords(box);
1400 }
1402 void
1403 box3d_add_to_selection(SPBox3D *box) {
1404 Persp3D *persp = box3d_get_perspective(box);
1405 g_return_if_fail(persp);
1406 persp3d_add_box_transform(persp, box);
1407 }
1409 void
1410 box3d_remove_from_selection(SPBox3D *box) {
1411 Persp3D *persp = box3d_get_perspective(box);
1412 if (!persp) {
1413 /* this can happen if a box is deleted through undo and the persp_ref is already detached;
1414 should we rebuild the boxes of each perspective in this case or is it safe to leave it alone? */
1415 return;
1416 }
1417 persp3d_remove_box_transform(persp, box);
1418 }
1420 void
1421 box3d_mark_transformed(SPBox3D *box) {
1422 Persp3D *persp = box3d_get_perspective(box);
1423 g_return_if_fail(persp);
1424 persp3d_set_box_transformed(persp, box, true);
1425 }
1427 Persp3D *
1428 box3d_get_perspective(SPBox3D const *box) {
1429 return box->persp_ref->getObject();
1430 }
1432 void
1433 box3d_switch_perspectives(SPBox3D *box, Persp3D *old_persp, Persp3D *new_persp, bool recompute_corners) {
1434 if (recompute_corners) {
1435 box->orig_corner0.normalize();
1436 box->orig_corner7.normalize();
1437 double z0 = box->orig_corner0[Proj::Z];
1438 double z7 = box->orig_corner7[Proj::Z];
1439 NR::Point corner0_screen = box3d_get_corner_screen(box, 0);
1440 NR::Point corner7_screen = box3d_get_corner_screen(box, 7);
1442 box->orig_corner0 = new_persp->tmat.preimage(corner0_screen, z0, Proj::Z);
1443 box->orig_corner7 = new_persp->tmat.preimage(corner7_screen, z7, Proj::Z);
1444 }
1446 persp3d_remove_box (old_persp, box);
1447 persp3d_add_box (new_persp, box);
1449 persp3d_remove_box_transform (old_persp, box);
1450 persp3d_add_box_transform (new_persp, box);
1452 gchar *href = g_strdup_printf("#%s", SP_OBJECT_REPR(new_persp)->attribute("id"));
1453 SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", href);
1454 g_free(href);
1455 }
1457 /* Converts the 3D box to an ordinary SPGroup, adds it to the XML tree at the same position as
1458 the original box and deletes the latter */
1459 // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
1460 SPGroup *
1461 box3d_convert_to_group(SPBox3D *box) {
1462 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
1463 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1465 // remember position of the box
1466 int pos = SP_OBJECT_REPR(box)->position();
1468 // create a new group and add the sides (converted to ordinary paths) as its children
1469 Inkscape::XML::Node *grepr = xml_doc->createElement("svg:g");
1471 Inkscape::XML::Node *repr;
1472 for (SPObject *child = sp_object_first_child(SP_OBJECT(box)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
1473 if (SP_IS_BOX3D_SIDE(child)) {
1474 repr = box3d_side_convert_to_path(SP_BOX3D_SIDE(child));
1475 grepr->appendChild(repr);
1476 } else {
1477 g_warning("Non-side object encountered as child of a 3D box.\n");
1478 }
1479 }
1481 // add the new group to the box's parent and set remembered position
1482 SPObject *parent = SP_OBJECT_PARENT(box);
1483 SP_OBJECT_REPR(parent)->appendChild(grepr);
1484 grepr->setPosition(pos);
1486 SP_OBJECT(box)->deleteObject(true);
1488 return SP_GROUP(doc->getObjectByRepr(grepr));
1489 }
1491 static void
1492 box3d_push_back_corner_pair(SPBox3D *box, std::list<std::pair<Geom::Point, Geom::Point> > &pts, int c1, int c2) {
1493 pts.push_back(std::make_pair(box3d_get_corner_screen(box, c1).to_2geom(),
1494 box3d_get_corner_screen(box, c2).to_2geom()));
1495 }
1497 void
1498 box3d_convert_to_guides(SPBox3D *box, bool write_undo) {
1499 SPDocument *doc = SP_OBJECT_DOCUMENT(box);
1500 //SPDesktop *desktop = inkscape_active_desktop();
1501 //Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1503 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1505 /* perspective lines in X direction */
1506 box3d_push_back_corner_pair(box, pts, 0, 1);
1507 box3d_push_back_corner_pair(box, pts, 2, 3);
1508 box3d_push_back_corner_pair(box, pts, 4, 5);
1509 box3d_push_back_corner_pair(box, pts, 6, 7);
1511 /* perspective lines in Y direction */
1512 box3d_push_back_corner_pair(box, pts, 0, 2);
1513 box3d_push_back_corner_pair(box, pts, 1, 3);
1514 box3d_push_back_corner_pair(box, pts, 4, 6);
1515 box3d_push_back_corner_pair(box, pts, 5, 7);
1517 /* perspective lines in Z direction */
1518 box3d_push_back_corner_pair(box, pts, 0, 4);
1519 box3d_push_back_corner_pair(box, pts, 1, 5);
1520 box3d_push_back_corner_pair(box, pts, 2, 6);
1521 box3d_push_back_corner_pair(box, pts, 3, 7);
1523 sp_guide_pt_pairs_to_guides(doc, pts);
1525 SP_OBJECT(box)->deleteObject(true);
1527 if (write_undo) {
1528 sp_document_done(doc, SP_VERB_CONTEXT_3DBOX, _("Convert to guides"));
1529 }
1530 }
1532 /*
1533 Local Variables:
1534 mode:c++
1535 c-file-style:"stroustrup"
1536 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1537 indent-tabs-mode:nil
1538 fill-column:99
1539 End:
1540 */
1541 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :