Code

Store cached icons to disk between runs, and invalidate/purge as needed.
[inkscape.git] / src / persp3d.cpp
1 /*
2  * Class modelling a 3D perspective as an SPObject
3  *
4  * Authors:
5  *   Maximilian Albert <Anhalter42@gmx.de>
6  *   Jon A. Cruz <jon@joncruz.org>
7  *   Abhishek Sharma
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 using Inkscape::DocumentUndo;
28 static void persp3d_class_init(Persp3DClass *klass);
29 static void persp3d_init(Persp3D *persp);
31 static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static void persp3d_release(SPObject *object);
33 static void persp3d_set(SPObject *object, unsigned key, gchar const *value);
34 static void persp3d_update(SPObject *object, SPCtx *ctx, guint flags);
35 static Inkscape::XML::Node *persp3d_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
37 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);
39 static void persp3d_update_with_point (Persp3DImpl *persp_impl, Proj::Axis const axis, Proj::Pt2 const &new_image);
40 static gchar * persp3d_pt_to_str (Persp3DImpl *persp_impl, Proj::Axis const axis);
42 static SPObjectClass *persp3d_parent_class;
44 static int global_counter = 0;
46 /* Constructor/destructor for the internal class */
48 Persp3DImpl::Persp3DImpl() {
49     tmat = Proj::TransfMat3x4 ();
50     document = NULL;
52     my_counter = global_counter++;
53 }
55 /**
56  * Registers Persp3d class and returns its type.
57  */
58 GType
59 persp3d_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(Persp3DClass),
65             NULL, NULL,
66             (GClassInitFunc) persp3d_class_init,
67             NULL, NULL,
68             sizeof(Persp3D),
69             16,
70             (GInstanceInitFunc) persp3d_init,
71             NULL,   /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_OBJECT, "Persp3D", &info, (GTypeFlags)0);
74     }
75     return type;
76 }
78 static Inkscape::XML::NodeEventVector const persp3d_repr_events = {
79     NULL, /* child_added */
80     NULL, /* child_removed */
81     persp3d_on_repr_attr_changed,
82     NULL, /* content_changed */
83     NULL  /* order_changed */
84 };
86 /**
87  * Callback to initialize Persp3D vtable.
88  */
89 static void persp3d_class_init(Persp3DClass *klass)
90 {
91     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
93     persp3d_parent_class = (SPObjectClass *) g_type_class_ref(SP_TYPE_OBJECT);
95     sp_object_class->build = persp3d_build;
96     sp_object_class->release = persp3d_release;
97     sp_object_class->set = persp3d_set;
98     sp_object_class->update = persp3d_update;
99     sp_object_class->write = persp3d_write;
102 /**
103  * Callback to initialize Persp3D object.
104  */
105 static void
106 persp3d_init(Persp3D *persp)
108     persp->perspective_impl = new Persp3DImpl();
111 /**
112  * Virtual build: set persp3d attributes from its associated XML node.
113  */
114 static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
116     if (((SPObjectClass *) persp3d_parent_class)->build)
117         (* ((SPObjectClass *) persp3d_parent_class)->build)(object, document, repr);
119     /* calls sp_object_set for the respective attributes */
120     // The transformation matrix is updated according to the values we read for the VPs
121     object->readAttr( "inkscape:vp_x" );
122     object->readAttr( "inkscape:vp_y" );
123     object->readAttr( "inkscape:vp_z" );
124     object->readAttr( "inkscape:persp3d-origin" );
126     if (repr) {
127         repr->addListener (&persp3d_repr_events, object);
128     }
131 /**
132  * Virtual release of Persp3D members before destruction.
133  */
134 static void persp3d_release(SPObject *object) {
135     Persp3D *persp = SP_PERSP3D(object);
136     delete persp->perspective_impl;
137     SP_OBJECT_REPR(object)->removeListenerByData(object);
141 /**
142  * Virtual set: set attribute to value.
143  */
144 // FIXME: Currently we only read the finite positions of vanishing points;
145 //        should we move VPs into their own repr (as it's done for SPStop, e.g.)?
146 static void
147 persp3d_set(SPObject *object, unsigned key, gchar const *value)
149     Persp3DImpl *persp_impl = SP_PERSP3D(object)->perspective_impl;
151     switch (key) {
152         case SP_ATTR_INKSCAPE_PERSP3D_VP_X: {
153             if (value) {
154                 Proj::Pt2 new_image (value);
155                 persp3d_update_with_point (persp_impl, Proj::X, new_image);
156             }
157             break;
158         }
159         case SP_ATTR_INKSCAPE_PERSP3D_VP_Y: {
160             if (value) {
161                 Proj::Pt2 new_image (value);
162                 persp3d_update_with_point (persp_impl, Proj::Y, new_image);
163                 break;
164             }
165         }
166         case SP_ATTR_INKSCAPE_PERSP3D_VP_Z: {
167             if (value) {
168                 Proj::Pt2 new_image (value);
169                 persp3d_update_with_point (persp_impl, Proj::Z, new_image);
170                 break;
171             }
172         }
173         case SP_ATTR_INKSCAPE_PERSP3D_ORIGIN: {
174             if (value) {
175                 Proj::Pt2 new_image (value);
176                 persp3d_update_with_point (persp_impl, Proj::W, new_image);
177                 break;
178             }
179         }
180         default: {
181             if (((SPObjectClass *) persp3d_parent_class)->set)
182                 (* ((SPObjectClass *) persp3d_parent_class)->set)(object, key, value);
183             break;
184         }
185     }
187     // FIXME: Is this the right place for resetting the draggers?
188     SPEventContext *ec = inkscape_active_event_context();
189     if (SP_IS_BOX3D_CONTEXT(ec)) {
190         Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
191         bc->_vpdrag->updateDraggers();
192         bc->_vpdrag->updateLines();
193         bc->_vpdrag->updateBoxHandles();
194         bc->_vpdrag->updateBoxReprs();
195     }
198 static void
199 persp3d_update(SPObject *object, SPCtx *ctx, guint flags)
201     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
203         /* TODO: Should we update anything here? */
205     }
207     if (((SPObjectClass *) persp3d_parent_class)->update)
208         ((SPObjectClass *) persp3d_parent_class)->update(object, ctx, flags);
211 Persp3D *persp3d_create_xml_element(SPDocument *document, Persp3DImpl *dup) {// if dup is given, copy the attributes over
212     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
213     Inkscape::XML::Document *xml_doc = document->getReprDoc();
214     Inkscape::XML::Node *repr;
216     /* if no perspective is given, create a default one */
217     repr = xml_doc->createElement("inkscape:perspective");
218     repr->setAttribute("sodipodi:type", "inkscape:persp3d");
220     Proj::Pt2 proj_vp_x = Proj::Pt2 (0.0, document->getHeight()/2, 1.0);
221     Proj::Pt2 proj_vp_y = Proj::Pt2 (0.0, 1000.0, 0.0);
222     Proj::Pt2 proj_vp_z = Proj::Pt2 (document->getWidth(), document->getHeight()/2, 1.0);
223     Proj::Pt2 proj_origin = Proj::Pt2 (document->getWidth()/2, document->getHeight()/3, 1.0);
225     if (dup) {
226         proj_vp_x = dup->tmat.column (Proj::X);
227         proj_vp_y = dup->tmat.column (Proj::Y);
228         proj_vp_z = dup->tmat.column (Proj::Z);
229         proj_origin = dup->tmat.column (Proj::W);
230     }
232     gchar *str = NULL;
233     str = proj_vp_x.coord_string();
234     repr->setAttribute("inkscape:vp_x", str);
235     g_free (str);
236     str = proj_vp_y.coord_string();
237     repr->setAttribute("inkscape:vp_y", str);
238     g_free (str);
239     str = proj_vp_z.coord_string();
240     repr->setAttribute("inkscape:vp_z", str);
241     g_free (str);
242     str = proj_origin.coord_string();
243     repr->setAttribute("inkscape:persp3d-origin", str);
244     g_free (str);
246     /* Append the new persp3d to defs */
247     SP_OBJECT_REPR(defs)->addChild(repr, NULL);
248     Inkscape::GC::release(repr);
250     return (Persp3D *) SP_OBJECT(defs)->get_child_by_repr (repr);
253 Persp3D *persp3d_document_first_persp(SPDocument *document)
255     Persp3D *first = 0;
256     for ( SPObject *child = SP_DOCUMENT_DEFS(document)->firstChild(); child && !first; child = child->getNext() ) {
257         if (SP_IS_PERSP3D(child)) {
258             first = SP_PERSP3D(child);
259         }
260     }
261     return first;
264 /**
265  * Virtual write: write object attributes to repr.
266  */
267 static Inkscape::XML::Node *
268 persp3d_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
270     Persp3DImpl *persp_impl = SP_PERSP3D(object)->perspective_impl;
272     if ((flags & SP_OBJECT_WRITE_BUILD & SP_OBJECT_WRITE_EXT) && !repr) {
273         // this is where we end up when saving as plain SVG (also in other circumstances?);
274         // hence we don't set the sodipodi:type attribute
275         repr = xml_doc->createElement("inkscape:perspective");
276     }
278     if (flags & SP_OBJECT_WRITE_EXT) {
279         gchar *str = NULL; // FIXME: Should this be freed each time we set an attribute or only in the end or at all?
280         str = persp3d_pt_to_str (persp_impl, Proj::X);
281         repr->setAttribute("inkscape:vp_x", str);
283         str = persp3d_pt_to_str (persp_impl, Proj::Y);
284         repr->setAttribute("inkscape:vp_y", str);
286         str = persp3d_pt_to_str (persp_impl, Proj::Z);
287         repr->setAttribute("inkscape:vp_z", str);
289         str = persp3d_pt_to_str (persp_impl, Proj::W);
290         repr->setAttribute("inkscape:persp3d-origin", str);
291     }
293     if (((SPObjectClass *) persp3d_parent_class)->write)
294         (* ((SPObjectClass *) persp3d_parent_class)->write)(object, xml_doc, repr, flags);
296     return repr;
299 /* convenience wrapper around persp3d_get_finite_dir() and persp3d_get_infinite_dir() */
300 Geom::Point persp3d_get_PL_dir_from_pt (Persp3D *persp, Geom::Point const &pt, Proj::Axis axis) {
301     if (persp3d_VP_is_finite(persp->perspective_impl, axis)) {
302         return persp3d_get_finite_dir(persp, pt, axis);
303     } else {
304         return persp3d_get_infinite_dir(persp, axis);
305     }
308 Geom::Point
309 persp3d_get_finite_dir (Persp3D *persp, Geom::Point const &pt, Proj::Axis axis) {
310     Box3D::PerspectiveLine pl(pt, axis, persp);
311     return pl.direction();
314 Geom::Point
315 persp3d_get_infinite_dir (Persp3D *persp, Proj::Axis axis) {
316     Proj::Pt2 vp(persp3d_get_VP(persp, axis));
317     if (vp[2] != 0.0) {
318         g_print ("VP should be infinite but is (%f : %f : %f)\n", vp[0], vp[1], vp[2]);
319         g_return_val_if_fail(vp[2] != 0.0, Geom::Point(0.0, 0.0));
320     }
321     return Geom::Point(vp[0], vp[1]);
324 double
325 persp3d_get_infinite_angle (Persp3D *persp, Proj::Axis axis) {
326     return persp->perspective_impl->tmat.get_infinite_angle(axis);
329 bool
330 persp3d_VP_is_finite (Persp3DImpl *persp_impl, Proj::Axis axis) {
331     return persp_impl->tmat.has_finite_image(axis);
334 void
335 persp3d_toggle_VP (Persp3D *persp, Proj::Axis axis, bool set_undo) {
336     persp->perspective_impl->tmat.toggle_finite(axis);
337     // FIXME: Remove this repr update and rely on vp_drag_sel_modified() to do this for us
338     //        On the other hand, vp_drag_sel_modified() would update all boxes;
339     //        here we can confine ourselves to the boxes of this particular perspective.
340     persp3d_update_box_reprs (persp);
341     SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
342     if (set_undo) {
343         DocumentUndo::done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
344                            _("Toggle vanishing point"));
345     }
348 /* toggle VPs for the same axis in all perspectives of a given list */
349 void
350 persp3d_toggle_VPs (std::list<Persp3D *> p, Proj::Axis axis) {
351     for (std::list<Persp3D *>::iterator i = p.begin(); i != p.end(); ++i) {
352         persp3d_toggle_VP((*i), axis, false);
353     }
354     DocumentUndo::done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
355                        _("Toggle multiple vanishing points"));
358 void
359 persp3d_set_VP_state (Persp3D *persp, Proj::Axis axis, Proj::VPState state) {
360     if (persp3d_VP_is_finite(persp->perspective_impl, axis) != (state == Proj::VP_FINITE)) {
361         persp3d_toggle_VP(persp, axis);
362     }
365 void
366 persp3d_rotate_VP (Persp3D *persp, Proj::Axis axis, double angle, bool alt_pressed) { // angle is in degrees
367     // FIXME: Most of this functionality should be moved to trans_mat_3x4.(h|cpp)
368     if (persp->perspective_impl->tmat.has_finite_image(axis)) {
369         // don't rotate anything for finite VPs
370         return;
371     }
372     Proj::Pt2 v_dir_proj (persp->perspective_impl->tmat.column(axis));
373     Geom::Point v_dir (v_dir_proj[0], v_dir_proj[1]);
374     double a = Geom::atan2 (v_dir) * 180/M_PI;
375     a += alt_pressed ? 0.5 * ((angle > 0 ) - (angle < 0)) : angle; // the r.h.s. yields +/-0.5 or angle
376     persp->perspective_impl->tmat.set_infinite_direction (axis, a);
378     persp3d_update_box_reprs (persp);
379     SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
382 void
383 persp3d_update_with_point (Persp3DImpl *persp_impl, Proj::Axis const axis, Proj::Pt2 const &new_image) {
384     persp_impl->tmat.set_image_pt (axis, new_image);
387 void
388 persp3d_apply_affine_transformation (Persp3D *persp, Geom::Matrix const &xform) {
389     persp->perspective_impl->tmat *= xform;
390     persp3d_update_box_reprs(persp);
391     SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
394 gchar *
395 persp3d_pt_to_str (Persp3DImpl *persp_impl, Proj::Axis const axis)
397     return persp_impl->tmat.pt_to_str(axis);
400 void
401 persp3d_add_box (Persp3D *persp, SPBox3D *box) {
402     Persp3DImpl *persp_impl = persp->perspective_impl;
404     if (!box) {
405         return;
406     }
407     if (std::find (persp_impl->boxes.begin(), persp_impl->boxes.end(), box) != persp_impl->boxes.end()) {
408         return;
409     }
410     persp_impl->boxes.push_back(box);
413 void
414 persp3d_remove_box (Persp3D *persp, SPBox3D *box) {
415     Persp3DImpl *persp_impl = persp->perspective_impl;
417     std::vector<SPBox3D *>::iterator i = std::find (persp_impl->boxes.begin(), persp_impl->boxes.end(), box);
418     if (i != persp_impl->boxes.end())
419         persp_impl->boxes.erase(i);
422 bool
423 persp3d_has_box (Persp3D *persp, SPBox3D *box) {
424     Persp3DImpl *persp_impl = persp->perspective_impl;
426     // FIXME: For some reason, std::find() does not seem to compare pointers "correctly" (or do we need to
427     //        provide a proper comparison function?), so we manually traverse the list.
428     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
429         if ((*i) == box) {
430             return true;
431         }
432     }
433     return false;
436 void
437 persp3d_update_box_displays (Persp3D *persp) {
438     Persp3DImpl *persp_impl = persp->perspective_impl;
440     if (persp_impl->boxes.empty())
441         return;
442     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
443         box3d_position_set(*i);
444     }
447 void
448 persp3d_update_box_reprs (Persp3D *persp) {
449     if (!persp) {
450         // Hmm, is it an error if this happens?
451         return;
452     }
453     Persp3DImpl *persp_impl = persp->perspective_impl;
455     if (persp_impl->boxes.empty())
456         return;
457     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
458         SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT);
459         box3d_set_z_orders(*i);
460     }
463 void
464 persp3d_update_z_orders (Persp3D *persp) {
465     Persp3DImpl *persp_impl = persp->perspective_impl;
467     if (persp_impl->boxes.empty())
468         return;
469     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
470         box3d_set_z_orders(*i);
471     }
474 // FIXME: For some reason we seem to require a vector instead of a list in Persp3D, but in vp_knot_moved_handler()
475 //        we need a list of boxes. If we can store a list in Persp3D right from the start, this function becomes
476 //        obsolete. We should do this.
477 std::list<SPBox3D *>
478 persp3d_list_of_boxes(Persp3D *persp) {
479     Persp3DImpl *persp_impl = persp->perspective_impl;
481     std::list<SPBox3D *> bx_lst;
482     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
483         bx_lst.push_back(*i);
484     }
485     return bx_lst;
488 bool
489 persp3d_perspectives_coincide(const Persp3D *lhs, const Persp3D *rhs)
491     return lhs->perspective_impl->tmat == rhs->perspective_impl->tmat;
494 void
495 persp3d_absorb(Persp3D *persp1, Persp3D *persp2) {
496     /* double check if we are called in sane situations */
497     g_return_if_fail (persp3d_perspectives_coincide(persp1, persp2) && persp1 != persp2);
499     std::vector<SPBox3D *>::iterator boxes;
501     // Note: We first need to copy the boxes of persp2 into a separate list;
502     //       otherwise the loop below gets confused when perspectives are reattached.
503     std::list<SPBox3D *> boxes_of_persp2 = persp3d_list_of_boxes(persp2);
505     for (std::list<SPBox3D *>::iterator i = boxes_of_persp2.begin(); i != boxes_of_persp2.end(); ++i) {
506         box3d_switch_perspectives((*i), persp2, persp1, true);
507         SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT); // so that undo/redo can do its job properly
508     }
511 static void
512 persp3d_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
513                                const gchar */*key*/,
514                                const gchar */*oldval*/,
515                                const gchar */*newval*/,
516                                bool /*is_interactive*/,
517                                void * data )
519     if (!data)
520         return;
522     Persp3D *persp = (Persp3D*) data;
523     persp3d_update_box_displays (persp);
526 /* checks whether all boxes linked to this perspective are currently selected */
527 bool
528 persp3d_has_all_boxes_in_selection (Persp3D *persp, Inkscape::Selection *selection) {
529     Persp3DImpl *persp_impl = persp->perspective_impl;
531     std::list<SPBox3D *> selboxes = selection->box3DList();
533     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
534         if (std::find(selboxes.begin(), selboxes.end(), *i) == selboxes.end()) {
535             // we have an unselected box in the perspective
536             return false;
537         }
538     }
539     return true;
542 /* some debugging stuff follows */
544 void
545 persp3d_print_debugging_info (Persp3D *persp) {
546     Persp3DImpl *persp_impl = persp->perspective_impl;
547     g_print ("=== Info for Persp3D %d ===\n", persp_impl->my_counter);
548     gchar * cstr;
549     for (int i = 0; i < 4; ++i) {
550         cstr = persp3d_get_VP(persp, Proj::axes[i]).coord_string();
551         g_print ("  VP %s:   %s\n", Proj::string_from_axis(Proj::axes[i]), cstr);
552         g_free(cstr);
553     }
554     cstr = persp3d_get_VP(persp, Proj::W).coord_string();
555     g_print ("  Origin: %s\n", cstr);
556     g_free(cstr);
558     g_print ("  Boxes: ");
559     for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin(); i != persp_impl->boxes.end(); ++i) {
560         g_print ("%d (%d)  ", (*i)->my_counter, box3d_get_perspective(*i)->perspective_impl->my_counter);
561     }
562     g_print ("\n");
563     g_print ("========================\n");
566 void persp3d_print_debugging_info_all(SPDocument *document)
568     for ( SPObject *child = SP_DOCUMENT_DEFS(document)->firstChild(); child; child = child->getNext() ) {
569         if (SP_IS_PERSP3D(child)) {
570             persp3d_print_debugging_info(SP_PERSP3D(child));
571         }
572     }
573     persp3d_print_all_selected();
576 void
577 persp3d_print_all_selected() {
578     g_print ("\n======================================\n");
579     g_print ("Selected perspectives and their boxes:\n");
581     std::list<Persp3D *> sel_persps = sp_desktop_selection(inkscape_active_desktop())->perspList();
583     for (std::list<Persp3D *>::iterator j = sel_persps.begin(); j != sel_persps.end(); ++j) {
584         Persp3D *persp = SP_PERSP3D(*j);
585         Persp3DImpl *persp_impl = persp->perspective_impl;
586         g_print ("  %s (%d):  ", SP_OBJECT_REPR(persp)->attribute("id"), persp->perspective_impl->my_counter);
587         for (std::vector<SPBox3D *>::iterator i = persp_impl->boxes.begin();
588              i != persp_impl->boxes.end(); ++i) {
589             g_print ("%d ", (*i)->my_counter);
590         }
591         g_print ("\n");
592     }
593     g_print ("======================================\n\n");
594  }
596 void print_current_persp3d(gchar *func_name, Persp3D *persp) {
597     g_print ("%s: current_persp3d is now %s\n",
598              func_name,
599              persp ? SP_OBJECT_REPR(persp)->attribute("id") : "NULL");
602 /*
603   Local Variables:
604   mode:c++
605   c-file-style:"stroustrup"
606   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
607   indent-tabs-mode:nil
608   fill-column:99
609   End:
610 */
611 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :