summaryrefslogtreecommitdiffstats
path: root/src/persp3d.cpp
blob: e647af6987499fd793c039b99d3e13cf51cfa0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#define __PERSP3D_C__

/*
 * Class modelling a 3D perspective as an SPObject
 *
 * Authors:
 *   Maximilian Albert <Anhalter42@gmx.de>
 *
 * Copyright (C) 2007 authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "persp3d.h"
#include "perspective-line.h"
#include "attributes.h"
#include "document-private.h"
#include "vanishing-point.h"
#include "box3d-context.h"
#include "box3d.h"
#include "xml/document.h"
#include "xml/node-event-vector.h"
#include "desktop-handles.h"
#include <glibmm/i18n.h>

static void persp3d_class_init(Persp3DClass *klass);
static void persp3d_init(Persp3D *stop);

static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
static void persp3d_release(SPObject *object);
static void persp3d_set(SPObject *object, unsigned key, gchar const *value);
static void persp3d_update(SPObject *object, SPCtx *ctx, guint flags);
static Inkscape::XML::Node *persp3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);

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);

static SPObjectClass *persp3d_parent_class;

static int global_counter = 0;

/**
 * Registers Persp3d class and returns its type.
 */
GType
persp3d_get_type()
{
    static GType type = 0;
    if (!type) {
        GTypeInfo info = {
            sizeof(Persp3DClass),
            NULL, NULL,
            (GClassInitFunc) persp3d_class_init,
            NULL, NULL,
            sizeof(Persp3D),
            16,
            (GInstanceInitFunc) persp3d_init,
            NULL,   /* value_table */
        };
        type = g_type_register_static(SP_TYPE_OBJECT, "Persp3D", &info, (GTypeFlags)0);
    }
    return type;
}

static Inkscape::XML::NodeEventVector const persp3d_repr_events = {
    NULL, /* child_added */
    NULL, /* child_removed */
    persp3d_on_repr_attr_changed,
    NULL, /* content_changed */
    NULL  /* order_changed */
};

/**
 * Callback to initialize Persp3D vtable.
 */
static void persp3d_class_init(Persp3DClass *klass)
{
    SPObjectClass *sp_object_class = (SPObjectClass *) klass;

    persp3d_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);

    sp_object_class->build = persp3d_build;
    sp_object_class->release = persp3d_release;
    sp_object_class->set = persp3d_set;
    sp_object_class->update = persp3d_update;
    sp_object_class->write = persp3d_write;
}

/**
 * Callback to initialize Persp3D object.
 */
static void
persp3d_init(Persp3D *persp)
{
    persp->tmat = Proj::TransfMat3x4 ();

    persp->boxes_transformed.clear();
    persp->document = NULL;

    persp->my_counter = global_counter++;
}

/**
 * Virtual build: set persp3d attributes from its associated XML node.
 */
static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
{
    if (((SPObjectClass *) persp3d_parent_class)->build)
        (* ((SPObjectClass *) persp3d_parent_class)->build)(object, document, repr);

    /* calls sp_object_set for the respective attributes */
    // The transformation matrix is updated according to the values we read for the VPs
    sp_object_read_attr(object, "inkscape:vp_x");
    sp_object_read_attr(object, "inkscape:vp_y");
    sp_object_read_attr(object, "inkscape:vp_z");
    sp_object_read_attr(object, "inkscape:persp3d-origin");

    if (repr) {
        repr->addListener (&persp3d_repr_events, object);
    }
}

/**
 * Virtual release of Persp3D members before destruction.
 */
static void persp3d_release(SPObject *object) {
    SP_OBJECT_REPR(object)->removeListenerByData(object);
}


/**
 * Virtual set: set attribute to value.
 */
// FIXME: Currently we only read the finite positions of vanishing points;
//        should we move VPs into their own repr (as it's done for SPStop, e.g.)?
static void
persp3d_set(SPObject *object, unsigned key, gchar const *value)
{
    Persp3D *persp = SP_PERSP3D (object);

    switch (key) {
        case SP_ATTR_INKSCAPE_PERSP3D_VP_X: {
            if (value) {
                Proj::Pt2 new_image (value);
                persp3d_update_with_point (persp, Proj::X, new_image);
            }
            break;
        }
        case SP_ATTR_INKSCAPE_PERSP3D_VP_Y: {
            if (value) {
                Proj::Pt2 new_image (value);
                persp3d_update_with_point (persp, Proj::Y, new_image);
                break;
            }
        }
        case SP_ATTR_INKSCAPE_PERSP3D_VP_Z: {
            if (value) {
                Proj::Pt2 new_image (value);
                persp3d_update_with_point (persp, Proj::Z, new_image);
                break;
            }
        }
        case SP_ATTR_INKSCAPE_PERSP3D_ORIGIN: {
            if (value) {
                Proj::Pt2 new_image (value);
                persp3d_update_with_point (persp, Proj::W, new_image);
                break;
            }
        }
        default: {
            if (((SPObjectClass *) persp3d_parent_class)->set)
                (* ((SPObjectClass *) persp3d_parent_class)->set)(object, key, value);
            break;
        }
    }

    // FIXME: Is this the right place for resetting the draggers?
    SPEventContext *ec = inkscape_active_event_context();
    if (SP_IS_BOX3D_CONTEXT(ec)) {
        Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
        bc->_vpdrag->updateDraggers();
        bc->_vpdrag->updateLines();
        bc->_vpdrag->updateBoxHandles();
        bc->_vpdrag->updateBoxReprs();
    }
}

static void
persp3d_update(SPObject *object, SPCtx *ctx, guint flags)
{
    if (flags & SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG) {

        /* TODO: Should we update anything here? */

    }

    if (((SPObjectClass *) persp3d_parent_class)->update)
        ((SPObjectClass *) persp3d_parent_class)->update(object, ctx, flags);
}

Persp3D *
persp3d_create_xml_element (SPDocument *document, Persp3D *dup) {// if dup is given, copy the attributes over
    SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
    Inkscape::XML::Node *repr;
    if (dup) {
        repr = SP_OBJECT_REPR(dup)->duplicate (xml_doc);
    } else {
        /* if no perspective is given, create a default one */
        repr = xml_doc->createElement("inkscape:perspective");
        repr->setAttribute("sodipodi:type", "inkscape:persp3d");

        Proj::Pt2 proj_vp_x = Proj::Pt2 (0.0, sp_document_height(document)/2, 1.0);
        Proj::Pt2 proj_vp_y = Proj::Pt2 (  0.0,1000.0, 0.0);
        Proj::Pt2 proj_vp_z = Proj::Pt2 (sp_document_width(document), sp_document_height(document)/2, 1.0);
        Proj::Pt2 proj_origin = Proj::Pt2 (sp_document_width(document)/2, sp_document_height(document)/3, 1.0);

        gchar *str = NULL;
        str = proj_vp_x.coord_string();
        repr->setAttribute("inkscape:vp_x", str);
        g_free (str);
        str = proj_vp_y.coord_string();
        repr->setAttribute("inkscape:vp_y", str);
        g_free (str);
        str = proj_vp_z.coord_string();
        repr->setAttribute("inkscape:vp_z", str);
        g_free (str);
        str = proj_origin.coord_string();
        repr->setAttribute("inkscape:persp3d-origin", str);
        g_free (str);
        Inkscape::GC::release(repr);
    }

    /* Append the new persp3d to defs */
    SP_OBJECT_REPR(defs)->addChild(repr, NULL);
    Inkscape::GC::release(repr);

    return (Persp3D *) sp_object_get_child_by_repr (SP_OBJECT(defs), repr);
}

Persp3D *
persp3d_document_first_persp (SPDocument *document) {
    SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
    Inkscape::XML::Node *repr;
    for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
        repr = SP_OBJECT_REPR(child);
        if (SP_IS_PERSP3D(child)) {
            return SP_PERSP3D(child);
        }
    }
    return NULL;
}

/**
 * Virtual write: write object attributes to repr.
 */
static Inkscape::XML::Node *
persp3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
{
    Persp3D *persp = SP_PERSP3D(object);

    if ((flags & SP_OBJECT_WRITE_BUILD & SP_OBJECT_WRITE_EXT) && !repr) {
        // this is where we end up when saving as plain SVG (also in other circumstances?);
        // hence we don't set the sodipodi:type attribute
        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
        repr = xml_doc->createElement("inkscape:perspective");
    }

    if (flags & SP_OBJECT_WRITE_EXT) {
        gchar *str = NULL; // FIXME: Should this be freed each time we set an attribute or only in the end or at all?
        str = persp3d_pt_to_str (persp, Proj::X);
        repr->setAttribute("inkscape:vp_x", str);

        str = persp3d_pt_to_str (persp, Proj::Y);
        repr->setAttribute("inkscape:vp_y", str);

        str = persp3d_pt_to_str (persp, Proj::Z);
        repr->setAttribute("inkscape:vp_z", str);

        str = persp3d_pt_to_str (persp, Proj::W);
        repr->setAttribute("inkscape:persp3d-origin", str);
    }

    if (((SPObjectClass *) persp3d_parent_class)->write)
        (* ((SPObjectClass *) persp3d_parent_class)->write)(object, repr, flags);

    return repr;
}

/* convenience wrapper around persp3d_get_finite_dir() and persp3d_get_infinite_dir() */
NR::Point persp3d_get_PL_dir_from_pt (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
    if (persp3d_VP_is_finite(persp, axis)) {
        return persp3d_get_finite_dir(persp, pt, axis);
    } else {
        return persp3d_get_infinite_dir(persp, axis);
    }
}

NR::Point
persp3d_get_finite_dir (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
    Box3D::PerspectiveLine pl(pt, axis, persp);
    return pl.direction();
}

NR::Point
persp3d_get_infinite_dir (Persp3D *persp, Proj::Axis axis) {
    Proj::Pt2 vp(persp3d_get_VP(persp, axis));
    if (vp[2] != 0.0) {
        g_print ("VP should be infinite but is (%f : %f : %f)\n", vp[0], vp[1], vp[2]);
        g_return_val_if_fail(vp[2] != 0.0, NR::Point(0.0, 0.0));
    }
    return NR::Point(vp[0], vp[1]);
}

double
persp3d_get_infinite_angle (Persp3D *persp, Proj::Axis axis) {
    return persp->tmat.get_infinite_angle(axis);
}

bool
persp3d_VP_is_finite (Persp3D *persp, Proj::Axis axis) {
    return persp->tmat.has_finite_image(axis);
}

void
persp3d_toggle_VP (Persp3D *persp, Proj::Axis axis, bool set_undo) {
    persp->tmat.toggle_finite(axis);
    // FIXME: Remove this repr update and rely on vp_drag_sel_modified() to do this for us
    //        On the other hand, vp_drag_sel_modified() would update all boxes;
    //        here we can confine ourselves to the boxes of this particular perspective.
    persp3d_update_box_reprs (persp);
    SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
    if (set_undo) {
        sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
                         _("Toggle vanishing point"));
    }
}

/* toggle VPs for the same axis in all perspectives of a given list */
void
persp3d_toggle_VPs (std::list<Persp3D *> p, Proj::Axis axis) {
    for (std::list<Persp3D *>::iterator i = p.begin(); i != p.end(); ++i) {
        persp3d_toggle_VP((*i), axis, false);
    }
    sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
                     _("Toggle multiple vanishing points"));
}

void
persp3d_set_VP_state (Persp3D *persp, Proj::Axis axis, Proj::VPState state) {
    if (persp3d_VP_is_finite(persp, axis) != (state == Proj::VP_FINITE)) {
        persp3d_toggle_VP(persp, axis);
    }
}

void
persp3d_rotate_VP (Persp3D *persp, Proj::Axis axis, double angle, bool alt_pressed) { // angle is in degrees
    // FIXME: Most of this functionality should be moved to trans_mat_3x4.(h|cpp)
    if (persp->tmat.has_finite_image(axis)) {
        // don't rotate anything for finite VPs
        return;
    }
    Proj::Pt2 v_dir_proj (persp->tmat.column(axis));
    NR::Point v_dir (v_dir_proj[0], v_dir_proj[1]);
    double a = NR::atan2 (v_dir) * 180/M_PI;
    a += alt_pressed ? 0.5 * ((angle > 0 ) - (angle < 0)) : angle; // the r.h.s. yields +/-0.5 or angle
    persp->tmat.set_infinite_direction (axis, a);

    persp3d_update_box_reprs (persp);
    SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
}

void
persp3d_update_with_point (Persp3D *persp, Proj::Axis const axis, Proj::Pt2 const &new_image) {
    persp->tmat.set_image_pt (axis, new_image);
}

void
persp3d_apply_affine_transformation (Persp3D *persp, NR::Matrix const &xform) {
    persp->tmat *= xform;
    persp3d_update_box_reprs(persp);
    SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
}

gchar *
persp3d_pt_to_str (Persp3D *persp, Proj::Axis const axis)
{
    return persp->tmat.pt_to_str(axis);
}

void
persp3d_add_box (Persp3D *persp, SPBox3D *box) {
    if (!box) {
        return;
    }
    if (std::find (persp->boxes.begin(), persp->boxes.end(), box) != persp->boxes.end()) {
        return;
    }
    persp->boxes.push_back(box);
}

void
persp3d_remove_box (Persp3D *persp, SPBox3D *box) {
    std::vector<SPBox3D *>::iterator i = std::find (persp->boxes.begin(), persp->boxes.end(), box);
    if (i != persp->boxes.end()) {
        persp->boxes.erase(i);
    }
}

bool
persp3d_has_box (Persp3D *persp, SPBox3D *box) {
    // FIXME: For some reason, std::find() does not seem to compare pointers "correctly" (or do we need to
    //        provide a proper comparison function?), so we manually traverse the list.
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        if ((*i) == box) {
            return true;
        }
    }
    return false;
}

void
persp3d_add_box_transform (Persp3D *persp, SPBox3D *box) {
    std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed.find(box);
    if (i != persp->boxes_transformed.end() && (*i).second == true) {
        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);
        return;
    }
 
    persp->boxes_transformed[box] = false;
}

void
persp3d_remove_box_transform (Persp3D *persp, SPBox3D *box) {
    persp->boxes_transformed.erase(box);
}

void
persp3d_set_box_transformed (Persp3D *persp, SPBox3D *box, bool transformed) {
    if (persp->boxes_transformed.find(box) == persp->boxes_transformed.end()) {
        g_print ("Warning! In %s (%d): trying to set transform status for box %d, but it is not listed in the perspective!! Aborting.\n",
                 SP_OBJECT_REPR(persp)->attribute("id"), persp->my_counter,
                 box->my_counter);
        return;
    }

    persp->boxes_transformed[box] = transformed;
}

bool
persp3d_was_transformed (Persp3D *persp) {
    if (persp->boxes_transformed.size() == 1) {
        /* either the transform has not been applied to the single box associated to this perspective yet
           or the transform was already reset; in both cases we need to return false because upcoming
           transforms need to be applied */
        (*persp->boxes_transformed.begin()).second = false; // make sure the box is marked as untransformed (in case more boxes are added later)
        return false;
    }

    for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed.begin();
         i != persp->boxes_transformed.end(); ++i) {
        if ((*i).second == true) {
            // at least one of the boxes in the perspective has already been transformed;
            return true;
        }
    }
    return false; // all boxes in the perspective are still untransformed; a pending transformation should be applied
}

bool
persp3d_all_transformed(Persp3D *persp) {
    for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed.begin();
         i != persp->boxes_transformed.end(); ++i) {
        if ((*i).second == false) {
            return false;
        }
    }
    return true;
}

void
persp3d_unset_transforms(Persp3D *persp) {
    for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed.begin();
         i != persp->boxes_transformed.end(); ++i) {
        (*i).second = false;
    }
}

void
persp3d_update_box_displays (Persp3D *persp) {
    if (persp->boxes.empty())
        return;
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        box3d_position_set(*i);
    }
}

void
persp3d_update_box_reprs (Persp3D *persp) {
    if (persp->boxes.empty())
        return;
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT);
        box3d_set_z_orders(*i);
    }
}

void
persp3d_update_z_orders (Persp3D *persp) {
    if (persp->boxes.empty())
        return;
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        box3d_set_z_orders(*i);
    }
}

// FIXME: For some reason we seem to require a vector instead of a list in Persp3D, but in vp_knot_moved_handler()
//        we need a list of boxes. If we can store a list in Persp3D right from the start, this function becomes
//        obsolete. We should do this.
std::list<SPBox3D *>
persp3d_list_of_boxes(Persp3D *persp) {
    std::list<SPBox3D *> bx_lst;
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        bx_lst.push_back(*i);
    }
    return bx_lst;
}

bool
persp3d_perspectives_coincide(const Persp3D *lhs, const Persp3D *rhs)
{
    return lhs->tmat == rhs->tmat;
}

void
persp3d_absorb(Persp3D *persp1, Persp3D *persp2) {
    /* double check if we are called in sane situations */
    g_return_if_fail (persp3d_perspectives_coincide(persp1, persp2) && persp1 != persp2);

    std::vector<SPBox3D *>::iterator boxes;

    // Note: We first need to copy the boxes of persp2 into a separate list;
    //       otherwise the loop below gets confused when perspectives are reattached.
    std::list<SPBox3D *> boxes_of_persp2 = persp3d_list_of_boxes(persp2);

    for (std::list<SPBox3D *>::iterator i = boxes_of_persp2.begin(); i != boxes_of_persp2.end(); ++i) {
        box3d_switch_perspectives((*i), persp2, persp1, true);
        SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT); // so that undo/redo can do its job properly
    }
}

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 )
{
    if (!data)
        return;

    Persp3D *persp = (Persp3D*) data;
    persp3d_update_box_displays (persp);
}

/* checks whether all boxes linked to this perspective are currently selected */
bool
persp3d_has_all_boxes_in_selection (Persp3D *persp) {
    std::list<SPBox3D *> selboxes = sp_desktop_selection(inkscape_active_desktop())->box3DList();

    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        if (std::find(selboxes.begin(), selboxes.end(), *i) == selboxes.end()) {
            // we have an unselected box in the perspective
            return false;
        }
    }
    return true;
}

// TODO: Check where we can use pass-by-reference (or so) instead of recreating all the lists afresh.
std::map<Persp3D *, std::list<SPBox3D *> >
persp3d_unselected_boxes(Inkscape::Selection *selection) {
    std::list<Persp3D *> plist = selection->perspList();
    std::map<Persp3D *, std::list<SPBox3D *> > punsel;

    std::list<Persp3D *>::iterator i;
    std::vector<SPBox3D *>::iterator j;
    // for all perspectives in the list ...
    for (i = plist.begin(); i != plist.end(); ++i) {
        Persp3D *persp = *i;
        // ... and each box associated to it ...
        for (j = persp->boxes.begin(); j != persp->boxes.end(); ++j) {
            SPBox3D *box = *j;
            // ... check whether it is unselected, and if so add it to the list
            if (persp->boxes_transformed.find(box) == persp->boxes_transformed.end()) {
                punsel[persp].push_back(box);
            }
        }
    }
    return punsel;
}

void
persp3d_split_perspectives_according_to_selection(Inkscape::Selection *selection) {
    std::map<Persp3D *, std::list<SPBox3D *> > punsel = persp3d_unselected_boxes(selection);

    std::map<Persp3D *, std::list<SPBox3D *> >::iterator i;
    std::list<SPBox3D *>::iterator j;
    // for all perspectives in the list ...
    for (i = punsel.begin(); i != punsel.end(); ++i) {
        Persp3D *persp = (*i).first;
        // ... if the perspective has unselected boxes ...
        if (!(*i).second.empty()) {
            // create a new perspective and move these boxes over
            Persp3D * new_persp = persp3d_create_xml_element (SP_OBJECT_DOCUMENT(persp), persp);
            for (j = (*i).second.begin(); j != (*i).second.end(); ++j) {
                SPBox3D *box = *j;
                box3d_switch_perspectives(box, persp, new_persp);
            }
        }
    }
}

/* some debugging stuff follows */

void
persp3d_print_debugging_info (Persp3D *persp) {
    g_print ("=== Info for Persp3D %d ===\n", persp->my_counter);
    gchar * cstr;
    for (int i = 0; i < 4; ++i) {
        cstr = persp3d_get_VP(persp, Proj::axes[i]).coord_string();
        g_print ("  VP %s:   %s\n", Proj::string_from_axis(Proj::axes[i]), cstr);
        g_free(cstr);
    }
    cstr = persp3d_get_VP(persp, Proj::W).coord_string();
    g_print ("  Origin: %s\n", cstr);
    g_free(cstr);

    g_print ("  Boxes: ");
    for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
        g_print ("%d (%d)  ", (*i)->my_counter, box3d_get_perspective(*i)->my_counter);
    }
    g_print ("\n");
    g_print ("========================\n");
}

void
persp3d_print_debugging_info_all(SPDocument *document) {
    SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
    Inkscape::XML::Node *repr;
    for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
        repr = SP_OBJECT_REPR(child);
        if (SP_IS_PERSP3D(child)) {
            persp3d_print_debugging_info(SP_PERSP3D(child));
        }
    }
    persp3d_print_all_selected();
}

void
persp3d_print_all_selected() {
    g_print ("\n======================================\n");
    g_print ("Selected perspectives and their boxes:\n");

    std::list<Persp3D *> sel_persps = sp_desktop_selection(inkscape_active_desktop())->perspList();

    for (std::list<Persp3D *>::iterator j = sel_persps.begin(); j != sel_persps.end(); ++j) {
        Persp3D *persp = SP_PERSP3D(*j);
        g_print ("  %s (%d):  ", SP_OBJECT_REPR(persp)->attribute("id"), persp->my_counter);
        for (std::map<SPBox3D *, bool>::iterator i = persp->boxes_transformed.begin();
             i != persp->boxes_transformed.end(); ++i) {
            g_print ("<%d,%d> ", (*i).first->my_counter, (*i).second);
        }
        g_print ("\n");
    }
    g_print ("======================================\n\n");
 }

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :