Code

3dafba30de3559afa8ce45624be96c55a26ae65f
[inkscape.git] / src / persp3d.cpp
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::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 = NULL;
97     persp->document = NULL;
99     persp->my_counter = global_counter++;
102 /**
103  * Virtual build: set persp3d attributes from its associated XML node.
104  */
105 static void persp3d_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
107     if (((SPObjectClass *) persp3d_parent_class)->build)
108         (* ((SPObjectClass *) persp3d_parent_class)->build)(object, document, repr);
110     /* calls sp_object_set for the respective attributes */
111     // The transformation matrix is updated according to the values we read for the VPs
112     sp_object_read_attr(object, "inkscape:vp_x");
113     sp_object_read_attr(object, "inkscape:vp_y");
114     sp_object_read_attr(object, "inkscape:vp_z");
115     sp_object_read_attr(object, "inkscape:persp3d-origin");
117     if (repr) {
118         repr->addListener (&persp3d_repr_events, object);
119     }
121     // FIXME: What precisely does this do and is it necessary for perspectives?
122     /* Register ourselves */
123     //sp_document_add_resource(document, "persp3d", object);
126 /**
127  * Virtual release of Persp3D members before destruction.
128  */
129 static void persp3d_release(SPObject *object) {
130     //Persp3D *persp = (Persp3D *) object;
132     // FIXME: What precisely does this do and is it necessary for perspectives?
133     /**
134     if (SP_OBJECT_DOCUMENT(object)) {
135         // Unregister ourselves
136         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "persp3d", SP_OBJECT(object));
137     }
138     **/
142 /**
143  * Virtual set: set attribute to value.
144  */
145 // FIXME: Currently we only read the finite positions of vanishing points;
146 //        should we move VPs into their own repr (as it's done for SPStop, e.g.)?
147 static void
148 persp3d_set(SPObject *object, unsigned key, gchar const *value)
150     Persp3D *persp = SP_PERSP3D (object);
152     switch (key) {
153         case SP_ATTR_INKSCAPE_PERSP3D_VP_X: {
154             if (value) {
155                 Proj::Pt2 new_image (value);
156                 persp3d_update_with_point (persp, Proj::X, new_image);
157             }
158             break;
159         }
160         case SP_ATTR_INKSCAPE_PERSP3D_VP_Y: {
161             if (value) {
162                 Proj::Pt2 new_image (value);
163                 persp3d_update_with_point (persp, Proj::Y, new_image);
164                 break;
165             }
166         }
167         case SP_ATTR_INKSCAPE_PERSP3D_VP_Z: {
168             if (value) {
169                 Proj::Pt2 new_image (value);
170                 persp3d_update_with_point (persp, Proj::Z, new_image);
171                 break;
172             }
173         }
174         case SP_ATTR_INKSCAPE_PERSP3D_ORIGIN: {
175             if (value) {
176                 Proj::Pt2 new_image (value);
177                 persp3d_update_with_point (persp, Proj::W, new_image);
178                 break;
179             }
180         }
181         default: {
182             if (((SPObjectClass *) persp3d_parent_class)->set)
183                 (* ((SPObjectClass *) persp3d_parent_class)->set)(object, key, value);
184             break;
185         }
186     }
188     // FIXME: Is this the right place for resetting the draggers?
189     SPEventContext *ec = inkscape_active_event_context();
190     if (SP_IS_BOX3D_CONTEXT(ec)) {
191         Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
192         bc->_vpdrag->updateDraggers();
193         bc->_vpdrag->updateLines();
194         bc->_vpdrag->updateBoxHandles();
195         bc->_vpdrag->updateBoxReprs();
196     }
199 static void
200 persp3d_update(SPObject *object, SPCtx *ctx, guint flags)
202     if (flags & SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG) {
204         /* TODO: Should we update anything here? */
206     }
208     if (((SPObjectClass *) persp3d_parent_class)->update)
209         ((SPObjectClass *) persp3d_parent_class)->update(object, ctx, flags);
212 Persp3D *
213 persp3d_create_xml_element (SPDocument *document, Persp3D *dup) {// if dup is given, copy the attributes over
214     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
215     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
216     Inkscape::XML::Node *repr;
217     if (dup) {
218         repr = SP_OBJECT_REPR(dup)->duplicate (xml_doc);
219     } else {
220         repr = xml_doc->createElement("inkscape:perspective");
221         repr->setAttribute("sodipodi:type", "inkscape:persp3d");
222     }
224     /* Append the new persp3d to defs */
225     SP_OBJECT_REPR(defs)->addChild(repr, NULL);
226     Inkscape::GC::release(repr);
228     return (Persp3D *) sp_object_get_child_by_repr (SP_OBJECT(defs), repr);
231 /**
232  * Virtual write: write object attributes to repr.
233  */
234 static Inkscape::XML::Node *
235 persp3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
237     SPDocument *document = SP_OBJECT_DOCUMENT(object);
238     Persp3D *persp = SP_PERSP3D(object);
240     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
241         repr = SP_OBJECT_REPR(persp3d_create_xml_element (document));
242     }
244     if (flags & SP_OBJECT_WRITE_EXT) {
245         gchar *str = NULL; // FIXME: Should this be freed each time we set an attribute or only in the end or at all?
246         str = persp3d_pt_to_str (persp, Proj::X);
247         repr->setAttribute("inkscape:vp_x", str);
249         str = persp3d_pt_to_str (persp, Proj::Y);
250         repr->setAttribute("inkscape:vp_y", str);
252         str = persp3d_pt_to_str (persp, Proj::Z);
253         repr->setAttribute("inkscape:vp_z", str);
255         str = persp3d_pt_to_str (persp, Proj::W);
256         repr->setAttribute("inkscape:persp3d-origin", str);
257     }
259     if (((SPObjectClass *) persp3d_parent_class)->write)
260         (* ((SPObjectClass *) persp3d_parent_class)->write)(object, repr, flags);
262     return repr;
265 /* convenience wrapper around persp3d_get_finite_dir() and persp3d_get_infinite_dir() */
266 NR::Point persp3d_get_PL_dir_from_pt (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
267     if (persp3d_VP_is_finite(persp, axis)) {
268         return persp3d_get_finite_dir(persp, pt, axis);
269     } else {
270         return persp3d_get_infinite_dir(persp, axis);
271     }
274 NR::Point
275 persp3d_get_finite_dir (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
276     Box3D::PerspectiveLine pl(pt, axis, persp);
277     return pl.direction();
280 NR::Point
281 persp3d_get_infinite_dir (Persp3D *persp, Proj::Axis axis) {
282     Proj::Pt2 vp(persp3d_get_VP(persp, axis));
283     if (vp[2] != 0.0) {
284         g_print ("VP should be infinite but is (%f : %f : %f)\n", vp[0], vp[1], vp[2]);
285         g_return_val_if_fail(vp[2] != 0.0, NR::Point(0.0, 0.0));
286     }
287     return NR::Point(vp[0], vp[1]);
290 double
291 persp3d_get_infinite_angle (Persp3D *persp, Proj::Axis axis) {
292     return persp->tmat.get_infinite_angle(axis);
295 bool
296 persp3d_VP_is_finite (Persp3D *persp, Proj::Axis axis) {
297     return persp->tmat.has_finite_image(axis);
300 void
301 persp3d_toggle_VP (Persp3D *persp, Proj::Axis axis, bool set_undo) {
302     persp->tmat.toggle_finite(axis);
303     // FIXME: Remove this repr update and rely on vp_drag_sel_modified() to do this for us
304     //        On the other hand, vp_drag_sel_modified() would update all boxes;
305     //        here we can confine ourselves to the boxes of this particular perspective.
306     persp3d_update_box_reprs (persp);
307     persp3d_update_z_orders (persp);
308     SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
309     if (set_undo) {
310         sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
311                          _("Toggle vanishing point"));
312     }
315 /* toggle VPs for the same axis in all perspectives of a given list */
316 void
317 persp3d_toggle_VPs (std::set<Persp3D *> p, Proj::Axis axis) {
318     for (std::set<Persp3D *>::iterator i = p.begin(); i != p.end(); ++i) {
319         persp3d_toggle_VP((*i), axis, false);
320     }
321     sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
322                      _("Toggle multiple vanishing points"));
325 void
326 persp3d_set_VP_state (Persp3D *persp, Proj::Axis axis, Proj::VPState state) {
327     if (persp3d_VP_is_finite(persp, axis) != (state == Proj::FINITE)) {
328         persp3d_toggle_VP(persp, axis);
329     }
332 void
333 persp3d_rotate_VP (Persp3D *persp, Proj::Axis axis, double angle, bool alt_pressed) { // angle is in degrees
334     // FIXME: Most of this functionality should be moved to trans_mat_3x4.(h|cpp)
335     if (persp->tmat.has_finite_image(axis)) {
336         // don't rotate anything for finite VPs
337         return;
338     }
339     Proj::Pt2 v_dir_proj (persp->tmat.column(axis));
340     NR::Point v_dir (v_dir_proj[0], v_dir_proj[1]);
341     double a = NR::atan2 (v_dir) * 180/M_PI;
342     a += alt_pressed ? 0.5 * ((angle > 0 ) - (angle < 0)) : angle; // the r.h.s. yields +/-0.5 or angle
343     persp->tmat.set_infinite_direction (axis, a);
345     persp3d_update_box_reprs (persp);
346     persp3d_update_z_orders (persp);
347     SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
350 void
351 persp3d_update_with_point (Persp3D *persp, Proj::Axis const axis, Proj::Pt2 const &new_image) {
352     persp->tmat.set_image_pt (axis, new_image);
355 void
356 persp3d_apply_affine_transformation (Persp3D *persp, NR::Matrix const &xform) {
357     persp->tmat *= xform;
358     persp3d_update_box_reprs(persp);
361 gchar *
362 persp3d_pt_to_str (Persp3D *persp, Proj::Axis const axis)
364     return persp->tmat.pt_to_str(axis);
367 void
368 persp3d_add_box (Persp3D *persp, SPBox3D *box) {
369     if (!box) {
370         //g_warning ("Trying to add NULL box to perspective.\n");
371         return;
372     }
373     if (std::find (persp->boxes.begin(), persp->boxes.end(), box) != persp->boxes.end()) {
374         //g_warning ("Attempting to add already existent box to perspective.\n");
375         return;
376     }
377     persp->boxes.push_back(box);
378     //SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", SP_OBJECT_REPR(persp)->attribute("id"));
381 void
382 persp3d_remove_box (Persp3D *persp, SPBox3D *box) {
383     std::vector<SPBox3D *>::iterator i = std::find (persp->boxes.begin(), persp->boxes.end(), box);
384     if (i != persp->boxes.end()) {
385         persp->boxes.erase(i);
386     }
389 bool
390 persp3d_has_box (Persp3D *persp, SPBox3D *box) {
391     // FIXME: For some reason, std::find() does not seem to compare pointers "correctly" (or do we need to
392     //        provide a proper comparison function?), so we manually traverse the list.
393     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
394         if ((*i) == box) {
395             return true;
396         }
397     }
398     return false;
401 void
402 persp3d_update_box_displays (Persp3D *persp) {
403     if (persp->boxes.empty())
404         return;
405     //g_print ("Requesting display update for %d boxes in the perspective.\n", persp->boxes.size());
406     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
407         box3d_position_set(*i);
408     }
411 void
412 persp3d_update_box_reprs (Persp3D *persp) {
413     if (persp->boxes.empty())
414         return;
415     //g_print ("Requesting repr update for %d boxes in the perspective.\n", persp->boxes.size());
416     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
417         SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT);
418     }
421 void
422 persp3d_update_z_orders (Persp3D *persp) {
423     if (persp->boxes.empty())
424         return;
425     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
426         box3d_set_z_orders(*i);
427     }
430 // FIXME: For some reason we seem to require a vector instead of a list in Persp3D, but in vp_knot_moved_handler()
431 //        we need a list of boxes. If we can store a list in Persp3D right from the start, this function becomes
432 //        obsolete. We should do this.
433 std::list<SPBox3D *>
434 persp3d_list_of_boxes(Persp3D *persp) {
435     std::list<SPBox3D *> bx_lst;
436     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
437         bx_lst.push_back(*i);
438     }
439     return bx_lst;
442 bool
443 persp3d_perspectives_coincide(const Persp3D *lhs, const Persp3D *rhs)
445     return lhs->tmat == rhs->tmat;
448 void
449 persp3d_absorb(Persp3D *persp1, Persp3D *persp2) {
450     /* double check if we are called in sane situations */
451     g_return_if_fail (persp3d_perspectives_coincide(persp1, persp2) && persp1 != persp2);
453     std::vector<SPBox3D *>::iterator boxes;
455     // Note: We first need to copy the boxes of persp2 into a separate list;
456     //       otherwise the loop below gets confused when perspectives are reattached.
457     std::list<SPBox3D *> boxes_of_persp2 = persp3d_list_of_boxes(persp2);
459     Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(persp1);
460     const gchar *persp_id = persp_repr->attribute("id");
461     gchar *href = g_strdup_printf("#%s", persp_id);
463     for (std::list<SPBox3D *>::iterator i = boxes_of_persp2.begin(); i != boxes_of_persp2.end(); ++i) {
464         SP_OBJECT_REPR(*i)->setAttribute("inkscape:perspectiveID", href);
465     }
466     g_free(href);
468     persp1->boxes.insert(persp1->boxes.begin(), persp2->boxes.begin(), persp2->boxes.end());
471 static void 
472 persp3d_on_repr_attr_changed ( Inkscape::XML::Node * repr, 
473                                const gchar *key, 
474                                const gchar *oldval, 
475                                const gchar *newval, 
476                                bool is_interactive, 
477                                void * data )
479     //g_print("persp3d_on_repr_attr_changed!!!! TODO: Do we need to trigger any further updates than the box reprs?");
481     if (!data)
482         return;
484     Persp3D *persp = (Persp3D*) data;
485     persp3d_update_box_displays (persp);
487     //lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
490 /* returns a std::set() of all perspectives of the currently selected boxes */
491 std::set<Persp3D *>
492 persp3d_currently_selected (Box3DContext *bc) {
493     Inkscape::Selection *selection = sp_desktop_selection (bc->desktop);
495     std::set<Persp3D *> p;
496     for (GSList *i = (GSList *) selection->itemList(); i != NULL; i = i->next) {
497         if (SP_IS_BOX3D (i->data)) {
498             p.insert(SP_BOX3D(i->data)->persp_ref->getObject());
499         }
500     }
501     return p;
504 void
505 persp3d_print_debugging_info (Persp3D *persp) {
506     g_print ("=== Info for Persp3D %d ===\n", persp->my_counter);
507     gchar * cstr;
508     for (int i = 0; i < 4; ++i) {
509         cstr = persp3d_get_VP(persp, Proj::axes[i]).coord_string();
510         g_print ("  VP %s:   %s\n", Proj::string_from_axis(Proj::axes[i]), cstr);
511         g_free(cstr);
512     }
513     cstr = persp3d_get_VP(persp, Proj::W).coord_string();
514     g_print ("  Origin: %s\n", cstr);
515     g_free(cstr);
517     g_print ("  Boxes: ");
518     for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
519         g_print ("%d (%d)", (*i)->my_counter, (*i)->persp_ref->getObject()->my_counter);
520     }
521     g_print ("\n");
522     g_print ("========================\n");
525 void
526 persp3d_print_debugging_info_all(SPDocument *document) {
527     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
528     Inkscape::XML::Node *repr;
529     for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
530         repr = SP_OBJECT_REPR(child);
531         if (SP_IS_PERSP3D(child)) {
532             persp3d_print_debugging_info(SP_PERSP3D(child));
533         }
534     }
537 /*
538   Local Variables:
539   mode:c++
540   c-file-style:"stroustrup"
541   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
542   indent-tabs-mode:nil
543   fill-column:99
544   End:
545 */
546 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :