1 #define __PERSP3D_C__
3 /*
4 * Class modelling a 3D perspective as an SPObject
5 *
6 * Authors:
7 * Maximilian Albert <Anhalter42@gmx.de>
8 *
9 * Copyright (C) 2007 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "persp3d.h"
15 #include "perspective-line.h"
16 #include "attributes.h"
17 #include "document-private.h"
18 #include "vanishing-point.h"
19 #include "box3d-context.h"
20 #include "box3d.h"
21 #include "xml/document.h"
22 #include "xml/node-event-vector.h"
23 #include "desktop-handles.h"
24 #include <glibmm/i18n.h>
26 static void persp3d_class_init(Persp3DClass *klass);
27 static void persp3d_init(Persp3D *stop);
29 static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
30 static void persp3d_release(SPObject *object);
31 static void persp3d_set(SPObject *object, unsigned key, gchar const *value);
32 static void persp3d_update(SPObject *object, SPCtx *ctx, guint flags);
33 static Inkscape::XML::Node *persp3d_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
35 static void persp3d_on_repr_attr_changed (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive, void * data);
37 static SPObjectClass *persp3d_parent_class;
39 static int global_counter = 0;
41 /**
42 * Registers Persp3d class and returns its type.
43 */
44 GType
45 persp3d_get_type()
46 {
47 static GType type = 0;
48 if (!type) {
49 GTypeInfo info = {
50 sizeof(Persp3DClass),
51 NULL, NULL,
52 (GClassInitFunc) persp3d_class_init,
53 NULL, NULL,
54 sizeof(Persp3D),
55 16,
56 (GInstanceInitFunc) persp3d_init,
57 NULL, /* value_table */
58 };
59 type = g_type_register_static(SP_TYPE_OBJECT, "Persp3D", &info, (GTypeFlags)0);
60 }
61 return type;
62 }
64 static Inkscape::XML::NodeEventVector const persp3d_repr_events = {
65 NULL, /* child_added */
66 NULL, /* child_removed */
67 persp3d_on_repr_attr_changed,
68 NULL, /* content_changed */
69 NULL /* order_changed */
70 };
72 /**
73 * Callback to initialize Persp3D vtable.
74 */
75 static void persp3d_class_init(Persp3DClass *klass)
76 {
77 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
79 persp3d_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
81 sp_object_class->build = persp3d_build;
82 sp_object_class->release = persp3d_release;
83 sp_object_class->set = persp3d_set;
84 sp_object_class->update = persp3d_update;
85 sp_object_class->write = persp3d_write;
86 }
88 /**
89 * Callback to initialize Persp3D object.
90 */
91 static void
92 persp3d_init(Persp3D *persp)
93 {
94 persp->tmat = Proj::TransfMat3x4 ();
96 persp->boxes_transformed = new std::map<SPBox3D *, bool>;
97 persp->boxes_transformed->clear();
98 persp->document = NULL;
100 persp->my_counter = global_counter++;
101 }
103 /**
104 * Virtual build: set persp3d attributes from its associated XML node.
105 */
106 static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
107 {
108 if (((SPObjectClass *) persp3d_parent_class)->build)
109 (* ((SPObjectClass *) persp3d_parent_class)->build)(object, document, repr);
111 /* calls sp_object_set for the respective attributes */
112 // The transformation matrix is updated according to the values we read for the VPs
113 sp_object_read_attr(object, "inkscape:vp_x");
114 sp_object_read_attr(object, "inkscape:vp_y");
115 sp_object_read_attr(object, "inkscape:vp_z");
116 sp_object_read_attr(object, "inkscape:persp3d-origin");
118 if (repr) {
119 repr->addListener (&persp3d_repr_events, object);
120 }
121 }
123 /**
124 * Virtual release of Persp3D members before destruction.
125 */
126 static void persp3d_release(SPObject *object) {
127 Persp3D *persp = SP_PERSP3D (object);
128 delete persp->boxes_transformed;
129 SP_OBJECT_REPR(object)->removeListenerByData(object);
130 }
133 /**
134 * Virtual set: set attribute to value.
135 */
136 // FIXME: Currently we only read the finite positions of vanishing points;
137 // should we move VPs into their own repr (as it's done for SPStop, e.g.)?
138 static void
139 persp3d_set(SPObject *object, unsigned key, gchar const *value)
140 {
141 Persp3D *persp = SP_PERSP3D (object);
143 switch (key) {
144 case SP_ATTR_INKSCAPE_PERSP3D_VP_X: {
145 if (value) {
146 Proj::Pt2 new_image (value);
147 persp3d_update_with_point (persp, Proj::X, new_image);
148 }
149 break;
150 }
151 case SP_ATTR_INKSCAPE_PERSP3D_VP_Y: {
152 if (value) {
153 Proj::Pt2 new_image (value);
154 persp3d_update_with_point (persp, Proj::Y, new_image);
155 break;
156 }
157 }
158 case SP_ATTR_INKSCAPE_PERSP3D_VP_Z: {
159 if (value) {
160 Proj::Pt2 new_image (value);
161 persp3d_update_with_point (persp, Proj::Z, new_image);
162 break;
163 }
164 }
165 case SP_ATTR_INKSCAPE_PERSP3D_ORIGIN: {
166 if (value) {
167 Proj::Pt2 new_image (value);
168 persp3d_update_with_point (persp, Proj::W, new_image);
169 break;
170 }
171 }
172 default: {
173 if (((SPObjectClass *) persp3d_parent_class)->set)
174 (* ((SPObjectClass *) persp3d_parent_class)->set)(object, key, value);
175 break;
176 }
177 }
179 // FIXME: Is this the right place for resetting the draggers?
180 SPEventContext *ec = inkscape_active_event_context();
181 if (SP_IS_BOX3D_CONTEXT(ec)) {
182 Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
183 bc->_vpdrag->updateDraggers();
184 bc->_vpdrag->updateLines();
185 bc->_vpdrag->updateBoxHandles();
186 bc->_vpdrag->updateBoxReprs();
187 }
188 }
190 static void
191 persp3d_update(SPObject *object, SPCtx *ctx, guint flags)
192 {
193 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
195 /* TODO: Should we update anything here? */
197 }
199 if (((SPObjectClass *) persp3d_parent_class)->update)
200 ((SPObjectClass *) persp3d_parent_class)->update(object, ctx, flags);
201 }
203 Persp3D *
204 persp3d_create_xml_element (SPDocument *document, Persp3D *dup) {// if dup is given, copy the attributes over
205 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
206 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
207 Inkscape::XML::Node *repr;
208 if (dup) {
209 repr = SP_OBJECT_REPR(dup)->duplicate (xml_doc);
210 } else {
211 /* if no perspective is given, create a default one */
212 repr = xml_doc->createElement("inkscape:perspective");
213 repr->setAttribute("sodipodi:type", "inkscape:persp3d");
215 Proj::Pt2 proj_vp_x = Proj::Pt2 (0.0, sp_document_height(document)/2, 1.0);
216 Proj::Pt2 proj_vp_y = Proj::Pt2 ( 0.0,1000.0, 0.0);
217 Proj::Pt2 proj_vp_z = Proj::Pt2 (sp_document_width(document), sp_document_height(document)/2, 1.0);
218 Proj::Pt2 proj_origin = Proj::Pt2 (sp_document_width(document)/2, sp_document_height(document)/3, 1.0);
220 gchar *str = NULL;
221 str = proj_vp_x.coord_string();
222 repr->setAttribute("inkscape:vp_x", str);
223 g_free (str);
224 str = proj_vp_y.coord_string();
225 repr->setAttribute("inkscape:vp_y", str);
226 g_free (str);
227 str = proj_vp_z.coord_string();
228 repr->setAttribute("inkscape:vp_z", str);
229 g_free (str);
230 str = proj_origin.coord_string();
231 repr->setAttribute("inkscape:persp3d-origin", str);
232 g_free (str);
233 }
235 /* Append the new persp3d to defs */
236 SP_OBJECT_REPR(defs)->addChild(repr, NULL);
237 Inkscape::GC::release(repr);
239 return (Persp3D *) sp_object_get_child_by_repr (SP_OBJECT(defs), repr);
240 }
242 Persp3D *
243 persp3d_document_first_persp (SPDocument *document) {
244 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
245 Inkscape::XML::Node *repr;
246 for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
247 repr = SP_OBJECT_REPR(child);
248 if (SP_IS_PERSP3D(child)) {
249 return SP_PERSP3D(child);
250 }
251 }
252 return NULL;
253 }
255 /**
256 * Virtual write: write object attributes to repr.
257 */
258 static Inkscape::XML::Node *
259 persp3d_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
260 {
261 Persp3D *persp = SP_PERSP3D(object);
263 if ((flags & SP_OBJECT_WRITE_BUILD & SP_OBJECT_WRITE_EXT) && !repr) {
264 // this is where we end up when saving as plain SVG (also in other circumstances?);
265 // hence we don't set the sodipodi:type attribute
266 repr = xml_doc->createElement("inkscape:perspective");
267 }
269 if (flags & SP_OBJECT_WRITE_EXT) {
270 gchar *str = NULL; // FIXME: Should this be freed each time we set an attribute or only in the end or at all?
271 str = persp3d_pt_to_str (persp, Proj::X);
272 repr->setAttribute("inkscape:vp_x", str);
274 str = persp3d_pt_to_str (persp, Proj::Y);
275 repr->setAttribute("inkscape:vp_y", str);
277 str = persp3d_pt_to_str (persp, Proj::Z);
278 repr->setAttribute("inkscape:vp_z", str);
280 str = persp3d_pt_to_str (persp, Proj::W);
281 repr->setAttribute("inkscape:persp3d-origin", str);
282 }
284 if (((SPObjectClass *) persp3d_parent_class)->write)
285 (* ((SPObjectClass *) persp3d_parent_class)->write)(object, xml_doc, repr, flags);
287 return repr;
288 }
290 /* convenience wrapper around persp3d_get_finite_dir() and persp3d_get_infinite_dir() */
291 Geom::Point persp3d_get_PL_dir_from_pt (Persp3D *persp, Geom::Point const &pt, Proj::Axis axis) {
292 if (persp3d_VP_is_finite(persp, axis)) {
293 return persp3d_get_finite_dir(persp, pt, axis);
294 } else {
295 return persp3d_get_infinite_dir(persp, axis);
296 }
297 }
299 Geom::Point
300 persp3d_get_finite_dir (Persp3D *persp, Geom::Point const &pt, Proj::Axis axis) {
301 Box3D::PerspectiveLine pl(pt, axis, persp);
302 return pl.direction();
303 }
305 Geom::Point
306 persp3d_get_infinite_dir (Persp3D *persp, Proj::Axis axis) {
307 Proj::Pt2 vp(persp3d_get_VP(persp, axis));
308 if (vp[2] != 0.0) {
309 g_print ("VP should be infinite but is (%f : %f : %f)\n", vp[0], vp[1], vp[2]);
310 g_return_val_if_fail(vp[2] != 0.0, Geom::Point(0.0, 0.0));
311 }
312 return Geom::Point(vp[0], vp[1]);
313 }
315 double
316 persp3d_get_infinite_angle (Persp3D *persp, Proj::Axis axis) {
317 return persp->tmat.get_infinite_angle(axis);
318 }
320 bool
321 persp3d_VP_is_finite (Persp3D *persp, Proj::Axis axis) {
322 return persp->tmat.has_finite_image(axis);
323 }
325 void
326 persp3d_toggle_VP (Persp3D *persp, Proj::Axis axis, bool set_undo) {
327 persp->tmat.toggle_finite(axis);
328 // FIXME: Remove this repr update and rely on vp_drag_sel_modified() to do this for us
329 // On the other hand, vp_drag_sel_modified() would update all boxes;
330 // here we can confine ourselves to the boxes of this particular perspective.
331 persp3d_update_box_reprs (persp);
332 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
333 if (set_undo) {
334 sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
335 _("Toggle vanishing point"));
336 }
337 }
339 /* toggle VPs for the same axis in all perspectives of a given list */
340 void
341 persp3d_toggle_VPs (std::list<Persp3D *> p, Proj::Axis axis) {
342 for (std::list<Persp3D *>::iterator i = p.begin(); i != p.end(); ++i) {
343 persp3d_toggle_VP((*i), axis, false);
344 }
345 sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
346 _("Toggle multiple vanishing points"));
347 }
349 void
350 persp3d_set_VP_state (Persp3D *persp, Proj::Axis axis, Proj::VPState state) {
351 if (persp3d_VP_is_finite(persp, axis) != (state == Proj::VP_FINITE)) {
352 persp3d_toggle_VP(persp, axis);
353 }
354 }
356 void
357 persp3d_rotate_VP (Persp3D *persp, Proj::Axis axis, double angle, bool alt_pressed) { // angle is in degrees
358 // FIXME: Most of this functionality should be moved to trans_mat_3x4.(h|cpp)
359 if (persp->tmat.has_finite_image(axis)) {
360 // don't rotate anything for finite VPs
361 return;
362 }
363 Proj::Pt2 v_dir_proj (persp->tmat.column(axis));
364 Geom::Point v_dir (v_dir_proj[0], v_dir_proj[1]);
365 double a = Geom::atan2 (v_dir) * 180/M_PI;
366 a += alt_pressed ? 0.5 * ((angle > 0 ) - (angle < 0)) : angle; // the r.h.s. yields +/-0.5 or angle
367 persp->tmat.set_infinite_direction (axis, a);
369 persp3d_update_box_reprs (persp);
370 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
371 }
373 void
374 persp3d_update_with_point (Persp3D *persp, Proj::Axis const axis, Proj::Pt2 const &new_image) {
375 persp->tmat.set_image_pt (axis, new_image);
376 }
378 void
379 persp3d_apply_affine_transformation (Persp3D *persp, Geom::Matrix const &xform) {
380 persp->tmat *= xform;
381 persp3d_update_box_reprs(persp);
382 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
383 }
385 gchar *
386 persp3d_pt_to_str (Persp3D *persp, Proj::Axis const axis)
387 {
388 return persp->tmat.pt_to_str(axis);
389 }
391 void
392 persp3d_add_box (Persp3D *persp, SPBox3D *box) {
393 if (!box) {
394 return;
395 }
396 if (std::find (persp->boxes.begin(), persp->boxes.end(), box) != persp->boxes.end()) {
397 return;
398 }
399 persp->boxes.push_back(box);
400 }
402 void
403 persp3d_remove_box (Persp3D *persp, SPBox3D *box) {
404 std::vector<SPBox3D *>::iterator i = std::find (persp->boxes.begin(), persp->boxes.end(), box);
405 if (i != persp->boxes.end()) {
406 persp->boxes.erase(i);
407 }
408 }
410 bool
411 persp3d_has_box (Persp3D *persp, SPBox3D *box) {
412 // FIXME: For some reason, std::find() does not seem to compare pointers "correctly" (or do we need to
413 // provide a proper comparison function?), so we manually traverse the list.
414 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
415 if ((*i) == box) {
416 return true;
417 }
418 }
419 return false;
420 }
422 void
423 persp3d_add_box_transform (Persp3D *persp, SPBox3D *box) {
424 std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed->find(box);
425 if (i != persp->boxes_transformed->end() && (*i).second == true) {
426 g_print ("Warning! In %s (%d): trying to add transform status for box %d twice when it's already listed as true.\n", SP_OBJECT_REPR(persp)->attribute("id"), persp->my_counter, box->my_counter);
427 return;
428 }
430 (*persp->boxes_transformed)[box] = false;
431 }
433 void
434 persp3d_remove_box_transform (Persp3D *persp, SPBox3D *box) {
435 persp->boxes_transformed->erase(box);
436 }
438 void
439 persp3d_set_box_transformed (Persp3D *persp, SPBox3D *box, bool transformed) {
440 if (persp->boxes_transformed->find(box) == persp->boxes_transformed->end()) {
441 g_print ("Warning! In %s (%d): trying to set transform status for box %d, but it is not listed in the perspective!! Aborting.\n",
442 SP_OBJECT_REPR(persp)->attribute("id"), persp->my_counter,
443 box->my_counter);
444 return;
445 }
447 (*persp->boxes_transformed)[box] = transformed;
448 }
450 bool
451 persp3d_was_transformed (Persp3D *persp) {
452 if (persp->boxes_transformed->size() == 1) {
453 /* either the transform has not been applied to the single box associated to this perspective yet
454 or the transform was already reset; in both cases we need to return false because upcoming
455 transforms need to be applied */
456 (*persp->boxes_transformed->begin()).second = false; // make sure the box is marked as untransformed (in case more boxes are added later)
457 return false;
458 }
460 for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed->begin();
461 i != persp->boxes_transformed->end(); ++i) {
462 if ((*i).second == true) {
463 // at least one of the boxes in the perspective has already been transformed;
464 return true;
465 }
466 }
467 return false; // all boxes in the perspective are still untransformed; a pending transformation should be applied
468 }
470 bool
471 persp3d_all_transformed(Persp3D *persp) {
472 for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed->begin();
473 i != persp->boxes_transformed->end(); ++i) {
474 if ((*i).second == false) {
475 return false;
476 }
477 }
478 return true;
479 }
481 void
482 persp3d_unset_transforms(Persp3D *persp) {
483 for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed->begin();
484 i != persp->boxes_transformed->end(); ++i) {
485 (*i).second = false;
486 }
487 }
489 void
490 persp3d_update_box_displays (Persp3D *persp) {
491 if (persp->boxes.empty())
492 return;
493 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
494 box3d_position_set(*i);
495 }
496 }
498 void
499 persp3d_update_box_reprs (Persp3D *persp) {
500 if (persp->boxes.empty())
501 return;
502 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
503 SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT);
504 box3d_set_z_orders(*i);
505 }
506 }
508 void
509 persp3d_update_z_orders (Persp3D *persp) {
510 if (persp->boxes.empty())
511 return;
512 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
513 box3d_set_z_orders(*i);
514 }
515 }
517 // FIXME: For some reason we seem to require a vector instead of a list in Persp3D, but in vp_knot_moved_handler()
518 // we need a list of boxes. If we can store a list in Persp3D right from the start, this function becomes
519 // obsolete. We should do this.
520 std::list<SPBox3D *>
521 persp3d_list_of_boxes(Persp3D *persp) {
522 std::list<SPBox3D *> bx_lst;
523 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
524 bx_lst.push_back(*i);
525 }
526 return bx_lst;
527 }
529 bool
530 persp3d_perspectives_coincide(const Persp3D *lhs, const Persp3D *rhs)
531 {
532 return lhs->tmat == rhs->tmat;
533 }
535 void
536 persp3d_absorb(Persp3D *persp1, Persp3D *persp2) {
537 /* double check if we are called in sane situations */
538 g_return_if_fail (persp3d_perspectives_coincide(persp1, persp2) && persp1 != persp2);
540 std::vector<SPBox3D *>::iterator boxes;
542 // Note: We first need to copy the boxes of persp2 into a separate list;
543 // otherwise the loop below gets confused when perspectives are reattached.
544 std::list<SPBox3D *> boxes_of_persp2 = persp3d_list_of_boxes(persp2);
546 for (std::list<SPBox3D *>::iterator i = boxes_of_persp2.begin(); i != boxes_of_persp2.end(); ++i) {
547 box3d_switch_perspectives((*i), persp2, persp1, true);
548 SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT); // so that undo/redo can do its job properly
549 }
550 }
552 static void
553 persp3d_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
554 const gchar */*key*/,
555 const gchar */*oldval*/,
556 const gchar */*newval*/,
557 bool /*is_interactive*/,
558 void * data )
559 {
560 if (!data)
561 return;
563 Persp3D *persp = (Persp3D*) data;
564 persp3d_update_box_displays (persp);
565 }
567 /* checks whether all boxes linked to this perspective are currently selected */
568 bool
569 persp3d_has_all_boxes_in_selection (Persp3D *persp) {
570 std::list<SPBox3D *> selboxes = sp_desktop_selection(inkscape_active_desktop())->box3DList();
572 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
573 if (std::find(selboxes.begin(), selboxes.end(), *i) == selboxes.end()) {
574 // we have an unselected box in the perspective
575 return false;
576 }
577 }
578 return true;
579 }
581 /**
582 * For each perspective having a box in \a selection, determine all its unselected boxes.
583 */
584 // TODO: Check where we can use pass-by-reference (or so) instead of recreating all the lists afresh.
585 std::map<Persp3D *, std::list<SPBox3D *> >
586 persp3d_unselected_boxes(Inkscape::Selection *selection) {
587 std::list<Persp3D *> plist = selection->perspList();
588 std::map<Persp3D *, std::list<SPBox3D *> > punsel;
590 std::list<Persp3D *>::iterator i;
591 std::vector<SPBox3D *>::iterator j;
592 // for all perspectives in the list ...
593 for (i = plist.begin(); i != plist.end(); ++i) {
594 Persp3D *persp = *i;
595 // ... and each box associated to it ...
596 for (j = persp->boxes.begin(); j != persp->boxes.end(); ++j) {
597 SPBox3D *box = *j;
598 // ... check whether it is unselected, and if so add it to the list
599 if (persp->boxes_transformed->find(box) == persp->boxes_transformed->end()) {
600 punsel[persp].push_back(box);
601 }
602 }
603 }
604 return punsel;
605 }
607 /**
608 * Split all perspectives with a box in \a selection by moving their unselected boxes to newly
609 * created perspectives.
610 */
611 void
612 persp3d_split_perspectives_according_to_selection(Inkscape::Selection *selection) {
613 std::map<Persp3D *, std::list<SPBox3D *> > punsel = persp3d_unselected_boxes(selection);
615 std::map<Persp3D *, std::list<SPBox3D *> >::iterator i;
616 std::list<SPBox3D *>::iterator j;
617 // for all perspectives in the list ...
618 for (i = punsel.begin(); i != punsel.end(); ++i) {
619 Persp3D *persp = (*i).first;
620 // ... if the perspective has unselected boxes ...
621 if (!(*i).second.empty()) {
622 // create a new perspective and move these boxes over
623 Persp3D * new_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
624 for (j = (*i).second.begin(); j != (*i).second.end(); ++j) {
625 SPBox3D *box = *j;
626 box3d_switch_perspectives(box, persp, new_persp);
627 }
628 }
629 }
630 }
632 /* some debugging stuff follows */
634 void
635 persp3d_print_debugging_info (Persp3D *persp) {
636 g_print ("=== Info for Persp3D %d ===\n", persp->my_counter);
637 gchar * cstr;
638 for (int i = 0; i < 4; ++i) {
639 cstr = persp3d_get_VP(persp, Proj::axes[i]).coord_string();
640 g_print (" VP %s: %s\n", Proj::string_from_axis(Proj::axes[i]), cstr);
641 g_free(cstr);
642 }
643 cstr = persp3d_get_VP(persp, Proj::W).coord_string();
644 g_print (" Origin: %s\n", cstr);
645 g_free(cstr);
647 g_print (" Boxes: ");
648 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
649 g_print ("%d (%d) ", (*i)->my_counter, box3d_get_perspective(*i)->my_counter);
650 }
651 g_print ("\n");
652 g_print ("========================\n");
653 }
655 void
656 persp3d_print_debugging_info_all(SPDocument *document) {
657 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
658 Inkscape::XML::Node *repr;
659 for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
660 repr = SP_OBJECT_REPR(child);
661 if (SP_IS_PERSP3D(child)) {
662 persp3d_print_debugging_info(SP_PERSP3D(child));
663 }
664 }
665 persp3d_print_all_selected();
666 }
668 void
669 persp3d_print_all_selected() {
670 g_print ("\n======================================\n");
671 g_print ("Selected perspectives and their boxes:\n");
673 std::list<Persp3D *> sel_persps = sp_desktop_selection(inkscape_active_desktop())->perspList();
675 for (std::list<Persp3D *>::iterator j = sel_persps.begin(); j != sel_persps.end(); ++j) {
676 Persp3D *persp = SP_PERSP3D(*j);
677 g_print (" %s (%d): ", SP_OBJECT_REPR(persp)->attribute("id"), persp->my_counter);
678 for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed->begin();
679 i != persp->boxes_transformed->end(); ++i) {
680 g_print ("<%d,%d> ", (*i).first->my_counter, (*i).second);
681 }
682 g_print ("\n");
683 }
684 g_print ("======================================\n\n");
685 }
687 /*
688 Local Variables:
689 mode:c++
690 c-file-style:"stroustrup"
691 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
692 indent-tabs-mode:nil
693 fill-column:99
694 End:
695 */
696 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :