Code

Store a global list of existing perspectives; for each perspective hold a list of...
[inkscape.git] / src / box3d.cpp
1 #define __SP_3DBOX_C__
3 /*
4  * SVG <box3d> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   Maximilian Albert <Anhalter42@gmx.de>
10  *
11  * Copyright (C) 2007      Authors
12  * Copyright (C) 1999-2002 Lauris Kaplinski
13  * Copyright (C) 2000-2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include <glibmm/i18n.h>
19 #include "attributes.h"
20 #include "svg/stringstream.h"
21 #include "box3d.h"
23 static void sp_3dbox_class_init(SP3DBoxClass *klass);
24 static void sp_3dbox_init(SP3DBox *box3d);
26 static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
27 static void sp_3dbox_release (SPObject *object);
28 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
29 static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
30 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
32 static gchar *sp_3dbox_description(SPItem *item);
34 //static void sp_3dbox_set_shape(SPShape *shape);
35 static void sp_3dbox_set_shape(SP3DBox *box3d);
37 static void sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value);
38 static gchar * sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id);
39 static std::pair<gdouble, gdouble> sp_3dbox_get_coord_pair_from_string (const gchar *);
41 static SPGroupClass *parent_class;
43 GType
44 sp_3dbox_get_type(void)
45 {
46     static GType type = 0;
48     if (!type) {
49         GTypeInfo info = {
50             sizeof(SP3DBoxClass),
51             NULL,   /* base_init */
52             NULL,   /* base_finalize */
53             (GClassInitFunc) sp_3dbox_class_init,
54             NULL,   /* class_finalize */
55             NULL,   /* class_data */
56             sizeof(SP3DBox),
57             16,     /* n_preallocs */
58             (GInstanceInitFunc) sp_3dbox_init,
59             NULL,   /* value_table */
60         };
61         type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
62     }
64     return type;
65 }
67 static void
68 sp_3dbox_class_init(SP3DBoxClass *klass)
69 {
70     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
71     SPItemClass *item_class = (SPItemClass *) klass;
73     parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
75     sp_object_class->build = sp_3dbox_build;
76     sp_object_class->set = sp_3dbox_set;
77     sp_object_class->write = sp_3dbox_write;
78     sp_object_class->update = sp_3dbox_update;
79     sp_object_class->release = sp_3dbox_release;
81     item_class->description = sp_3dbox_description;
82 }
84 static void
85 sp_3dbox_init(SP3DBox *box)
86 {
87     for (int i = 0; i < 8; ++i) box->corners[i] = NR::Point(0,0);
88     for (int i = 0; i < 6; ++i) box->faces[i] = NULL;
89 }
91 static void
92 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
93 {
94     if (((SPObjectClass *) (parent_class))->build) {
95         ((SPObjectClass *) (parent_class))->build(object, document, repr);
96     }
98     SP3DBox *box = SP_3DBOX (object);
100     Box3D::Perspective3D::current_perspective->add_box (box);
102     sp_object_read_attr(object, "inkscape:box3dcornerA");
103     sp_object_read_attr(object, "inkscape:box3dcornerB");
104     sp_object_read_attr(object, "inkscape:box3dcornerC");
106     // TODO: We create all faces in the beginning, but only the non-degenerate ones
107     //       should be written to the svg representation later in sp_3dbox_write.
108     Box3D::Axis cur_plane, axis, dir1, dir2;
109     Box3D::FrontOrRear cur_pos;
110     for (int i = 0; i < 3; ++i) {
111         for (int j = 0; j < 2; ++j) {
112             cur_plane = Box3D::planes[i];
113             cur_pos = Box3D::face_positions[j];
114             // FIXME: The following code could theoretically be moved to
115             //        the constructor of Box3DFace (but see the comment there).
116             axis = (cur_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (cur_plane));
117             dir1 = extract_first_axis_direction (cur_plane);
118             dir2 = extract_second_axis_direction (cur_plane);
119             
120             box->faces[Box3D::face_to_int(cur_plane ^ cur_pos)] =
121                 new Box3DFace (box, box->corners[axis], box->corners[axis ^ dir1],
122                                     box->corners[axis ^ dir1 ^ dir2], box->corners[axis ^ dir2],
123                                     cur_plane, cur_pos);
124         }
125     }
127     // Check whether the paths of the faces of the box need to be linked to existing paths in the
128     // document (e.g., after a 'redo' operation or after opening a file) and do so if necessary.
129     sp_3dbox_link_to_existing_paths (box, repr);
132 static void
133 sp_3dbox_release (SPObject *object)
135         SP3DBox *box = SP_3DBOX(object);
136         for (int i = 0; i < 6; ++i) {
137             if (box->faces[i]) {
138                 delete box->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
139             }
140         }
142         // FIXME: We do not duplicate perspectives if they are the same for several boxes.
143         //        Thus, don't delete the perspective when deleting a box but rather unlink the box from it.
144         Box3D::get_persp_of_box (box)->remove_box (box);
146         if (((SPObjectClass *) parent_class)->release) {
147           ((SPObjectClass *) parent_class)->release (object);
148         }
151 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
153     switch (key) {
154         case SP_ATTR_INKSCAPE_3DBOX_CORNER_A:
155             sp_3dbox_update_corner_with_value_from_svg (object, 2, value);
156             break;
157         case SP_ATTR_INKSCAPE_3DBOX_CORNER_B:
158             sp_3dbox_update_corner_with_value_from_svg (object, 1, value);
159             break;
160         case SP_ATTR_INKSCAPE_3DBOX_CORNER_C:
161             sp_3dbox_update_corner_with_value_from_svg (object, 5, value);
162             break;
163         default:
164             if (((SPObjectClass *) (parent_class))->set) {
165                 ((SPObjectClass *) (parent_class))->set(object, key, value);
166             }
167             break;
168     }
171 static void
172 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
174     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
175         SP3DBox *box = SP_3DBOX(object);
176         sp_3dbox_link_to_existing_paths (box, SP_OBJECT_REPR(object));
177     }
179     /* Invoke parent method */
180     if (((SPObjectClass *) (parent_class))->update)
181         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
186 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
188     SP3DBox *box = SP_3DBOX(object);
189     // FIXME: How to handle other contexts???
190     // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
191     if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
192         return repr;
194     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
195         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
196         repr = xml_doc->createElement("svg:g");
197         repr->setAttribute("sodipodi:type", "inkscape:3dbox");
198         /* Hook paths to the faces of the box */
199         for (int i = 0; i < 6; ++i) {
200             box->faces[i]->hook_path_to_3dbox();
201         }
202     }
204     for (int i = 0; i < 6; ++i) {
205         box->faces[i]->set_path_repr();
206     }
208     if (flags & SP_OBJECT_WRITE_EXT) {
209         gchar *coords;
210         coords = sp_3dbox_get_corner_coords_string (box, 2);
211         repr->setAttribute("inkscape:box3dcornerA", coords);
213         coords = sp_3dbox_get_corner_coords_string (box, 1);
214         repr->setAttribute("inkscape:box3dcornerB", coords);
216         coords = sp_3dbox_get_corner_coords_string (box, 5);
217         repr->setAttribute("inkscape:box3dcornerC", coords);
219         g_free ((void *) coords);
220     }
222     if (((SPObjectClass *) (parent_class))->write) {
223         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
224     }
226     return repr;
229 static gchar *
230 sp_3dbox_description(SPItem *item)
232     g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
234     return g_strdup(_("<b>3D Box</b>"));
237 void
238 sp_3dbox_position_set (SP3DBoxContext &bc)
240     SP3DBox *box3d = SP_3DBOX(bc.item);
242     sp_3dbox_set_shape(box3d);
244     // FIXME: Why does the following call not automatically update the children
245     //        of box3d (which is an SPGroup, which should do this)?
246     //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
248     /**
249     SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
250     SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
251     SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
252     SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
253     SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
254     SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
255     ***/
258 static void
259 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
260 //        since SP3DBox is inherited from SPGroup. The following method is "artificially"
261 //        called from sp_3dbox_update().
262 //sp_3dbox_set_shape(SPShape *shape)
263 sp_3dbox_set_shape(SP3DBox *box3d)
265     // FIXME: How to handle other contexts???
266     // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
267     if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
268         return;
269     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
271     /* Only update the curves during dragging; setting the svg representations 
272        is expensive and only done once at the end */
273     sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
274     if (bc->extruded) {
275         box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
276         box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
277         box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
278         box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
279         box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
280     }
281     box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
283     sp_3dbox_update_curves (box3d);
287 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
289     sp_3dbox_move_corner_in_XY_plane (box, 2, A);
290     sp_3dbox_move_corner_in_XY_plane (box, 1, B);
291     sp_3dbox_move_corner_in_Z_direction (box, 5, C);
294 void
295 sp_3dbox_update_curves (SP3DBox *box) {
296     for (int i = 0; i < 6; ++i) {
297         if (box->faces[i]) box->faces[i]->set_curve();
298     }
301 /**
302  * In some situations (e.g., after cloning boxes, undo & redo, or reading boxes from a file) there are
303  * paths already present in the document which correspond to the faces of newly created boxes, but their
304  * 'path' members don't link to them yet. The following function corrects this if necessary.
305  */
306 void
307 sp_3dbox_link_to_existing_paths (SP3DBox *box, Inkscape::XML::Node *repr) {
308     // TODO: We should probably destroy the existing paths and recreate them because we don't know
309     //       precisely which path corresponds to which face. Does this make a difference?
310     //       In sp_3dbox_write we write the correct paths anyway, don't we? But we could get into
311     //       trouble at a later stage when we only write single faces for degenerate boxes.
313     SPDocument *document = SP_OBJECT_DOCUMENT(box);
314     guint face_id = 0;
316     for (Inkscape::XML::Node *i = sp_repr_children(repr); i != NULL; i = sp_repr_next(i)) {
317         if (face_id > 5) {
318             g_warning ("SVG representation of 3D boxes must contain 6 paths or less.\n");
319             break;
320         }
322         SPObject *face_object = document->getObjectByRepr((Inkscape::XML::Node *) i);
323         if (!SP_IS_PATH(face_object)) {
324             g_warning ("SVG representation of 3D boxes should only contain paths.\n");
325             continue;
326         }
327         box->faces[face_id]->hook_path_to_3dbox(SP_PATH(face_object));
328         ++face_id;
329     }
330     if (face_id < 6) {
331         //g_warning ("SVG representation of 3D boxes should contain exactly 6 paths (degenerate boxes are not yet supported).\n");
332         // TODO: Check whether it is safe to add the remaining paths to the box and do so in case it is.
333         //       (But we also land here for newly created boxes where we shouldn't add any paths because
334         //       This is done in sp_3dbox_write later on.
335     }
338 void
339 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
341     Box3D::Perspective3D * persp = Box3D::get_persp_of_box (box);
343     NR::Point A (box->corners[id ^ Box3D::XY]);
344     if (Box3D::is_single_axis_direction (axes)) {
345         pt = Box3D::PerspectiveLine (box->corners[id], axes, persp).closest_to(pt);
346     }
348     /* set the 'front' corners */
349     box->corners[id] = pt;
351     Box3D::PerspectiveLine pl_one (A, Box3D::Y, persp);
352     Box3D::PerspectiveLine pl_two (pt, Box3D::X, persp);
353     box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
355     pl_one = Box3D::PerspectiveLine (A, Box3D::X, persp);
356     pl_two = Box3D::PerspectiveLine (pt, Box3D::Y, persp);
357     box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
359     /* set the 'rear' corners */
360     NR::Point B (box->corners[id ^ Box3D::XYZ]);
362     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z, persp);
363     pl_two = Box3D::PerspectiveLine (B, Box3D::Y, persp);
364     box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
366     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X, persp);
367     pl_two = Box3D::PerspectiveLine (pt, Box3D::Z, persp);
368     box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
370     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y, persp);
371     pl_two = Box3D::PerspectiveLine (B, Box3D::X, persp);
372     box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
373     
376 void
377 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
379     if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
381     Box3D::Perspective3D * persp = Box3D::get_persp_of_box (box);
383     /* set the four corners of the face containing corners[id] */
384     box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z, persp).closest_to(pt);
386     Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X, persp);
387     Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z, persp);
388     box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
390     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y, persp);
391     pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z, persp);
392     box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
394     pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y, persp);
395     pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z, persp);
396     box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
399 NR::Maybe<NR::Point>
400 sp_3dbox_get_center (SP3DBox *box)
402     return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
405 NR::Maybe<NR::Point>
406 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
408     Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
410     // Is all this sufficiently precise also for degenerate cases?
411     if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
412         Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
414         Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
415         Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
416         NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
418         if (!adjacent_face_center) return NR::Nothing();
420         Box3D::Perspective3D * persp = Box3D::get_persp_of_box (box);
422         Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir, persp);
423         return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes, persp));
424     } else {
425         Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
426         Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
427         Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
428         return diag1.intersect(diag2);
429     }
432 static gchar *
433 sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id)
435     id = id % 8;
436     Inkscape::SVGOStringStream os;
437     os << box->corners[id][NR::X] << "," << box->corners[id][NR::Y];
438     return g_strdup(os.str().c_str());
441 static std::pair<gdouble, gdouble>
442 sp_3dbox_get_coord_pair_from_string (const gchar *coords)
444     gchar **coordpair = g_strsplit( coords, ",", 0);
445     // We might as well rely on g_ascii_strtod to convert the NULL pointer to 0.0,
446     // but we include the following test anyway
447     if (coordpair[0] == NULL || coordpair[1] == NULL) {
448         g_strfreev (coordpair);
449         g_warning ("Coordinate conversion failed.\n");
450         return std::make_pair(0.0, 0.0);
451     }
453     gdouble coord1 = g_ascii_strtod(coordpair[0], NULL);
454     gdouble coord2 = g_ascii_strtod(coordpair[1], NULL);
455     g_strfreev (coordpair);
457     return std::make_pair(coord1, coord2);
460 // auxiliary function
461 static void
462 sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value)
464     if (value == NULL) return;
465     SP3DBox *box = SP_3DBOX(object);
467     std::pair<gdouble, gdouble> coord_pair = sp_3dbox_get_coord_pair_from_string (value);
468     box->corners[corner_id] = NR::Point (coord_pair.first, coord_pair.second);
469     sp_3dbox_recompute_corners (box, box->corners[2], box->corners[1], box->corners[5]);
470     object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
473 /*
474   Local Variables:
475   mode:c++
476   c-file-style:"stroustrup"
477   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478   indent-tabs-mode:nil
479   fill-column:99
480   End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :