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++;
100 }
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)
106 {
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);
124 }
126 /**
127 * Virtual release of Persp3D members before destruction.
128 */
129 static void persp3d_release(SPObject *object) {
130 //Persp3D *persp = (Persp3D *) object;
132 SP_OBJECT_REPR(object)->removeListenerByData(object);
134 // FIXME: What precisely does this do and is it necessary for perspectives?
135 /**
136 if (SP_OBJECT_DOCUMENT(object)) {
137 // Unregister ourselves
138 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "persp3d", SP_OBJECT(object));
139 }
140 **/
141 }
144 /**
145 * Virtual set: set attribute to value.
146 */
147 // FIXME: Currently we only read the finite positions of vanishing points;
148 // should we move VPs into their own repr (as it's done for SPStop, e.g.)?
149 static void
150 persp3d_set(SPObject *object, unsigned key, gchar const *value)
151 {
152 Persp3D *persp = SP_PERSP3D (object);
154 switch (key) {
155 case SP_ATTR_INKSCAPE_PERSP3D_VP_X: {
156 if (value) {
157 Proj::Pt2 new_image (value);
158 persp3d_update_with_point (persp, Proj::X, new_image);
159 }
160 break;
161 }
162 case SP_ATTR_INKSCAPE_PERSP3D_VP_Y: {
163 if (value) {
164 Proj::Pt2 new_image (value);
165 persp3d_update_with_point (persp, Proj::Y, new_image);
166 break;
167 }
168 }
169 case SP_ATTR_INKSCAPE_PERSP3D_VP_Z: {
170 if (value) {
171 Proj::Pt2 new_image (value);
172 persp3d_update_with_point (persp, Proj::Z, new_image);
173 break;
174 }
175 }
176 case SP_ATTR_INKSCAPE_PERSP3D_ORIGIN: {
177 if (value) {
178 Proj::Pt2 new_image (value);
179 persp3d_update_with_point (persp, Proj::W, new_image);
180 break;
181 }
182 }
183 default: {
184 if (((SPObjectClass *) persp3d_parent_class)->set)
185 (* ((SPObjectClass *) persp3d_parent_class)->set)(object, key, value);
186 break;
187 }
188 }
190 // FIXME: Is this the right place for resetting the draggers?
191 SPEventContext *ec = inkscape_active_event_context();
192 if (SP_IS_BOX3D_CONTEXT(ec)) {
193 Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
194 bc->_vpdrag->updateDraggers();
195 bc->_vpdrag->updateLines();
196 bc->_vpdrag->updateBoxHandles();
197 bc->_vpdrag->updateBoxReprs();
198 }
199 }
201 static void
202 persp3d_update(SPObject *object, SPCtx *ctx, guint flags)
203 {
204 if (flags & SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG) {
206 /* TODO: Should we update anything here? */
208 }
210 if (((SPObjectClass *) persp3d_parent_class)->update)
211 ((SPObjectClass *) persp3d_parent_class)->update(object, ctx, flags);
212 }
214 Persp3D *
215 persp3d_create_xml_element (SPDocument *document, Persp3D *dup) {// if dup is given, copy the attributes over
216 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
217 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
218 Inkscape::XML::Node *repr;
219 if (dup) {
220 repr = SP_OBJECT_REPR(dup)->duplicate (xml_doc);
221 } else {
222 repr = xml_doc->createElement("inkscape:perspective");
223 repr->setAttribute("sodipodi:type", "inkscape:persp3d");
224 }
226 /* Append the new persp3d to defs */
227 SP_OBJECT_REPR(defs)->addChild(repr, NULL);
228 Inkscape::GC::release(repr);
230 return (Persp3D *) sp_object_get_child_by_repr (SP_OBJECT(defs), repr);
231 }
233 /**
234 * Virtual write: write object attributes to repr.
235 */
236 static Inkscape::XML::Node *
237 persp3d_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
238 {
239 SPDocument *document = SP_OBJECT_DOCUMENT(object);
240 Persp3D *persp = SP_PERSP3D(object);
242 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
243 repr = SP_OBJECT_REPR(persp3d_create_xml_element (document));
244 }
246 if (flags & SP_OBJECT_WRITE_EXT) {
247 gchar *str = NULL; // FIXME: Should this be freed each time we set an attribute or only in the end or at all?
248 str = persp3d_pt_to_str (persp, Proj::X);
249 repr->setAttribute("inkscape:vp_x", str);
251 str = persp3d_pt_to_str (persp, Proj::Y);
252 repr->setAttribute("inkscape:vp_y", str);
254 str = persp3d_pt_to_str (persp, Proj::Z);
255 repr->setAttribute("inkscape:vp_z", str);
257 str = persp3d_pt_to_str (persp, Proj::W);
258 repr->setAttribute("inkscape:persp3d-origin", str);
259 }
261 if (((SPObjectClass *) persp3d_parent_class)->write)
262 (* ((SPObjectClass *) persp3d_parent_class)->write)(object, repr, flags);
264 return repr;
265 }
267 /* convenience wrapper around persp3d_get_finite_dir() and persp3d_get_infinite_dir() */
268 NR::Point persp3d_get_PL_dir_from_pt (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
269 if (persp3d_VP_is_finite(persp, axis)) {
270 return persp3d_get_finite_dir(persp, pt, axis);
271 } else {
272 return persp3d_get_infinite_dir(persp, axis);
273 }
274 }
276 NR::Point
277 persp3d_get_finite_dir (Persp3D *persp, NR::Point const &pt, Proj::Axis axis) {
278 Box3D::PerspectiveLine pl(pt, axis, persp);
279 return pl.direction();
280 }
282 NR::Point
283 persp3d_get_infinite_dir (Persp3D *persp, Proj::Axis axis) {
284 Proj::Pt2 vp(persp3d_get_VP(persp, axis));
285 if (vp[2] != 0.0) {
286 g_print ("VP should be infinite but is (%f : %f : %f)\n", vp[0], vp[1], vp[2]);
287 g_return_val_if_fail(vp[2] != 0.0, NR::Point(0.0, 0.0));
288 }
289 return NR::Point(vp[0], vp[1]);
290 }
292 double
293 persp3d_get_infinite_angle (Persp3D *persp, Proj::Axis axis) {
294 return persp->tmat.get_infinite_angle(axis);
295 }
297 bool
298 persp3d_VP_is_finite (Persp3D *persp, Proj::Axis axis) {
299 return persp->tmat.has_finite_image(axis);
300 }
302 void
303 persp3d_toggle_VP (Persp3D *persp, Proj::Axis axis, bool set_undo) {
304 persp->tmat.toggle_finite(axis);
305 // FIXME: Remove this repr update and rely on vp_drag_sel_modified() to do this for us
306 // On the other hand, vp_drag_sel_modified() would update all boxes;
307 // here we can confine ourselves to the boxes of this particular perspective.
308 persp3d_update_box_reprs (persp);
309 persp3d_update_z_orders (persp);
310 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
311 if (set_undo) {
312 sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
313 _("Toggle vanishing point"));
314 }
315 }
317 /* toggle VPs for the same axis in all perspectives of a given list */
318 void
319 persp3d_toggle_VPs (std::set<Persp3D *> p, Proj::Axis axis) {
320 for (std::set<Persp3D *>::iterator i = p.begin(); i != p.end(); ++i) {
321 persp3d_toggle_VP((*i), axis, false);
322 }
323 sp_document_done(sp_desktop_document(inkscape_active_desktop()), SP_VERB_CONTEXT_3DBOX,
324 _("Toggle multiple vanishing points"));
325 }
327 void
328 persp3d_set_VP_state (Persp3D *persp, Proj::Axis axis, Proj::VPState state) {
329 if (persp3d_VP_is_finite(persp, axis) != (state == Proj::FINITE)) {
330 persp3d_toggle_VP(persp, axis);
331 }
332 }
334 void
335 persp3d_rotate_VP (Persp3D *persp, Proj::Axis axis, double angle, bool alt_pressed) { // angle is in degrees
336 // FIXME: Most of this functionality should be moved to trans_mat_3x4.(h|cpp)
337 if (persp->tmat.has_finite_image(axis)) {
338 // don't rotate anything for finite VPs
339 return;
340 }
341 Proj::Pt2 v_dir_proj (persp->tmat.column(axis));
342 NR::Point v_dir (v_dir_proj[0], v_dir_proj[1]);
343 double a = NR::atan2 (v_dir) * 180/M_PI;
344 a += alt_pressed ? 0.5 * ((angle > 0 ) - (angle < 0)) : angle; // the r.h.s. yields +/-0.5 or angle
345 persp->tmat.set_infinite_direction (axis, a);
347 persp3d_update_box_reprs (persp);
348 persp3d_update_z_orders (persp);
349 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
350 }
352 void
353 persp3d_update_with_point (Persp3D *persp, Proj::Axis const axis, Proj::Pt2 const &new_image) {
354 persp->tmat.set_image_pt (axis, new_image);
355 }
357 void
358 persp3d_apply_affine_transformation (Persp3D *persp, NR::Matrix const &xform) {
359 persp->tmat *= xform;
360 persp3d_update_box_reprs(persp);
361 SP_OBJECT(persp)->updateRepr(SP_OBJECT_WRITE_EXT);
362 }
364 gchar *
365 persp3d_pt_to_str (Persp3D *persp, Proj::Axis const axis)
366 {
367 return persp->tmat.pt_to_str(axis);
368 }
370 void
371 persp3d_add_box (Persp3D *persp, SPBox3D *box) {
372 if (!box) {
373 //g_warning ("Trying to add NULL box to perspective.\n");
374 return;
375 }
376 if (std::find (persp->boxes.begin(), persp->boxes.end(), box) != persp->boxes.end()) {
377 //g_warning ("Attempting to add already existent box to perspective.\n");
378 return;
379 }
380 persp->boxes.push_back(box);
381 //SP_OBJECT_REPR(box)->setAttribute("inkscape:perspectiveID", SP_OBJECT_REPR(persp)->attribute("id"));
382 }
384 void
385 persp3d_remove_box (Persp3D *persp, SPBox3D *box) {
386 std::vector<SPBox3D *>::iterator i = std::find (persp->boxes.begin(), persp->boxes.end(), box);
387 if (i != persp->boxes.end()) {
388 persp->boxes.erase(i);
389 }
390 }
392 bool
393 persp3d_has_box (Persp3D *persp, SPBox3D *box) {
394 // FIXME: For some reason, std::find() does not seem to compare pointers "correctly" (or do we need to
395 // provide a proper comparison function?), so we manually traverse the list.
396 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
397 if ((*i) == box) {
398 return true;
399 }
400 }
401 return false;
402 }
404 void
405 persp3d_update_box_displays (Persp3D *persp) {
406 if (persp->boxes.empty())
407 return;
408 //g_print ("Requesting display update for %d boxes in the perspective.\n", persp->boxes.size());
409 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
410 box3d_position_set(*i);
411 }
412 }
414 void
415 persp3d_update_box_reprs (Persp3D *persp) {
416 if (persp->boxes.empty())
417 return;
418 //g_print ("Requesting repr update for %d boxes in the perspective.\n", persp->boxes.size());
419 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
420 SP_OBJECT(*i)->updateRepr(SP_OBJECT_WRITE_EXT);
421 }
422 }
424 void
425 persp3d_update_z_orders (Persp3D *persp) {
426 if (persp->boxes.empty())
427 return;
428 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
429 box3d_set_z_orders(*i);
430 }
431 }
433 // FIXME: For some reason we seem to require a vector instead of a list in Persp3D, but in vp_knot_moved_handler()
434 // we need a list of boxes. If we can store a list in Persp3D right from the start, this function becomes
435 // obsolete. We should do this.
436 std::list<SPBox3D *>
437 persp3d_list_of_boxes(Persp3D *persp) {
438 std::list<SPBox3D *> bx_lst;
439 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
440 bx_lst.push_back(*i);
441 }
442 return bx_lst;
443 }
445 bool
446 persp3d_perspectives_coincide(const Persp3D *lhs, const Persp3D *rhs)
447 {
448 return lhs->tmat == rhs->tmat;
449 }
451 void
452 persp3d_absorb(Persp3D *persp1, Persp3D *persp2) {
453 /* double check if we are called in sane situations */
454 g_return_if_fail (persp3d_perspectives_coincide(persp1, persp2) && persp1 != persp2);
456 std::vector<SPBox3D *>::iterator boxes;
458 // Note: We first need to copy the boxes of persp2 into a separate list;
459 // otherwise the loop below gets confused when perspectives are reattached.
460 std::list<SPBox3D *> boxes_of_persp2 = persp3d_list_of_boxes(persp2);
462 Inkscape::XML::Node *persp_repr = SP_OBJECT_REPR(persp1);
463 const gchar *persp_id = persp_repr->attribute("id");
464 gchar *href = g_strdup_printf("#%s", persp_id);
466 for (std::list<SPBox3D *>::iterator i = boxes_of_persp2.begin(); i != boxes_of_persp2.end(); ++i) {
467 SP_OBJECT_REPR(*i)->setAttribute("inkscape:perspectiveID", href);
468 }
469 g_free(href);
471 persp1->boxes.insert(persp1->boxes.begin(), persp2->boxes.begin(), persp2->boxes.end());
472 }
474 static void
475 persp3d_on_repr_attr_changed ( Inkscape::XML::Node * /*repr*/,
476 const gchar */*key*/,
477 const gchar */*oldval*/,
478 const gchar */*newval*/,
479 bool /*is_interactive*/,
480 void * data )
481 {
482 //g_print("persp3d_on_repr_attr_changed!!!! TODO: Do we need to trigger any further updates than the box reprs?");
484 if (!data)
485 return;
487 Persp3D *persp = (Persp3D*) data;
488 persp3d_update_box_displays (persp);
490 //lpeobj->requestModified(SP_OBJECT_MODIFIED_FLAG);
491 }
493 /* returns a std::set() of all perspectives of the currently selected boxes */
494 std::set<Persp3D *>
495 persp3d_currently_selected_persps (SPEventContext *ec) {
496 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
498 std::set<Persp3D *> p;
499 for (GSList *i = (GSList *) selection->itemList(); i != NULL; i = i->next) {
500 if (SP_IS_BOX3D (i->data)) {
501 p.insert(SP_BOX3D(i->data)->persp_ref->getObject());
502 }
503 }
504 return p;
505 }
507 /* checks whether all boxes linked to this perspective are currently selected */
508 bool
509 persp3d_has_all_boxes_in_selection (Persp3D *persp) {
510 const GSList *selection = sp_desktop_selection (inkscape_active_desktop())->itemList();
512 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
513 if (g_slist_find((GSList *) selection, *i) == NULL) {
514 // we have an unselected box in the perspective
515 return false;
516 }
517 }
518 return true;
519 }
521 std::list<SPBox3D *>
522 persp3d_selected_boxes (Persp3D *persp) {
523 const GSList *selection = sp_desktop_selection (inkscape_active_desktop())->itemList();
524 std::list<SPBox3D *> sel;
526 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
527 if (g_slist_find((GSList *) selection, *i) != NULL) {
528 sel.push_back(SP_BOX3D(*i));
529 }
530 }
531 return sel;
532 }
534 void
535 persp3d_print_debugging_info (Persp3D *persp) {
536 g_print ("=== Info for Persp3D %d ===\n", persp->my_counter);
537 gchar * cstr;
538 for (int i = 0; i < 4; ++i) {
539 cstr = persp3d_get_VP(persp, Proj::axes[i]).coord_string();
540 g_print (" VP %s: %s\n", Proj::string_from_axis(Proj::axes[i]), cstr);
541 g_free(cstr);
542 }
543 cstr = persp3d_get_VP(persp, Proj::W).coord_string();
544 g_print (" Origin: %s\n", cstr);
545 g_free(cstr);
547 g_print (" Boxes: ");
548 for (std::vector<SPBox3D *>::iterator i = persp->boxes.begin(); i != persp->boxes.end(); ++i) {
549 g_print ("%d (%d)", (*i)->my_counter, (*i)->persp_ref->getObject()->my_counter);
550 }
551 g_print ("\n");
552 g_print ("========================\n");
553 }
555 void
556 persp3d_print_debugging_info_all(SPDocument *document) {
557 SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
558 Inkscape::XML::Node *repr;
559 for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
560 repr = SP_OBJECT_REPR(child);
561 if (SP_IS_PERSP3D(child)) {
562 persp3d_print_debugging_info(SP_PERSP3D(child));
563 }
564 }
565 }
567 /*
568 Local Variables:
569 mode:c++
570 c-file-style:"stroustrup"
571 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
572 indent-tabs-mode:nil
573 fill-column:99
574 End:
575 */
576 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :