eb67269ce3975acec57e4340b3f6b0207ec9be81
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 "box3d.h"
22 static void sp_3dbox_class_init(SP3DBoxClass *klass);
23 static void sp_3dbox_init(SP3DBox *box3d);
25 static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
26 //static void sp_3dbox_release (SPObject *object);
27 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
28 static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
29 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
31 static gchar *sp_3dbox_description(SPItem *item);
33 //static void sp_3dbox_set_shape(SPShape *shape);
34 static void sp_3dbox_set_shape(SP3DBox *box3d);
36 static void sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value);
37 static gchar * sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id);
38 static std::pair<gdouble, gdouble> sp_3dbox_get_coord_pair_from_string (const gchar *);
40 static SPGroupClass *parent_class;
42 GType
43 sp_3dbox_get_type(void)
44 {
45 static GType type = 0;
47 if (!type) {
48 GTypeInfo info = {
49 sizeof(SP3DBoxClass),
50 NULL, /* base_init */
51 NULL, /* base_finalize */
52 (GClassInitFunc) sp_3dbox_class_init,
53 NULL, /* class_finalize */
54 NULL, /* class_data */
55 sizeof(SP3DBox),
56 16, /* n_preallocs */
57 (GInstanceInitFunc) sp_3dbox_init,
58 NULL, /* value_table */
59 };
60 type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
61 }
63 return type;
64 }
66 static void
67 sp_3dbox_class_init(SP3DBoxClass *klass)
68 {
69 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
70 SPItemClass *item_class = (SPItemClass *) klass;
72 parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
74 sp_object_class->build = sp_3dbox_build;
75 sp_object_class->set = sp_3dbox_set;
76 sp_object_class->write = sp_3dbox_write;
77 sp_object_class->update = sp_3dbox_update;
78 //sp_object_class->release = sp_3dbox_release;
80 item_class->description = sp_3dbox_description;
81 }
83 static void
84 sp_3dbox_init(SP3DBox *box)
85 {
86 for (int i = 0; i < 8; ++i) box->corners[i] = NR::Point(0,0);
87 for (int i = 0; i < 6; ++i) box->faces[i] = NULL;
88 }
90 static void
91 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
92 {
93 if (((SPObjectClass *) (parent_class))->build) {
94 ((SPObjectClass *) (parent_class))->build(object, document, repr);
95 }
97 SP3DBox *box = SP_3DBOX (object);
99 sp_object_read_attr(object, "inkscape:box3dcornerA");
100 sp_object_read_attr(object, "inkscape:box3dcornerB");
101 sp_object_read_attr(object, "inkscape:box3dcornerC");
103 // TODO: We create all faces in the beginning, but only the non-degenerate ones
104 // should be written to the svg representation later in sp_3dbox_write.
105 Box3D::Axis cur_plane, axis, dir1, dir2;
106 Box3D::FrontOrRear cur_pos;
107 for (int i = 0; i < 3; ++i) {
108 for (int j = 0; j < 2; ++j) {
109 cur_plane = Box3D::planes[i];
110 cur_pos = Box3D::face_positions[j];
111 // FIXME: The following code could theoretically be moved to
112 // the constructor of Box3DFace (but see the comment there).
113 axis = (cur_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (cur_plane));
114 dir1 = extract_first_axis_direction (cur_plane);
115 dir2 = extract_second_axis_direction (cur_plane);
117 box->faces[Box3D::face_to_int(cur_plane ^ cur_pos)] =
118 new Box3DFace (box, box->corners[axis], box->corners[axis ^ dir1],
119 box->corners[axis ^ dir1 ^ dir2], box->corners[axis ^ dir2],
120 cur_plane, cur_pos);
121 }
122 }
124 // Check whether the paths of the faces of the box need to be linked to existing paths in the
125 // document (e.g., after a 'redo' operation or after opening a file) and do so if necessary.
126 sp_3dbox_link_to_existing_paths (box, repr);
127 }
129 /*
130 static void
131 sp_3dbox_release (SPObject *object)
132 {
133 SP3DBox *box3d = SP_3DBOX(object);
134 for (int i = 0; i < 6; ++i) {
135 if (box3d->faces[i]) {
136 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
137 }
138 }
140 if (((SPObjectClass *) parent_class)->release) {
141 ((SPObjectClass *) parent_class)->release (object);
142 }
143 }
144 */
146 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
147 {
148 switch (key) {
149 case SP_ATTR_INKSCAPE_3DBOX_CORNER_A:
150 sp_3dbox_update_corner_with_value_from_svg (object, 2, value);
151 break;
152 case SP_ATTR_INKSCAPE_3DBOX_CORNER_B:
153 sp_3dbox_update_corner_with_value_from_svg (object, 1, value);
154 break;
155 case SP_ATTR_INKSCAPE_3DBOX_CORNER_C:
156 sp_3dbox_update_corner_with_value_from_svg (object, 5, value);
157 break;
158 default:
159 if (((SPObjectClass *) (parent_class))->set) {
160 ((SPObjectClass *) (parent_class))->set(object, key, value);
161 }
162 break;
163 }
164 }
166 static void
167 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
168 {
169 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
170 SP3DBox *box = SP_3DBOX(object);
171 sp_3dbox_link_to_existing_paths (box, SP_OBJECT_REPR(object));
172 }
174 /* Invoke parent method */
175 if (((SPObjectClass *) (parent_class))->update)
176 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
177 }
181 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
182 {
183 SP3DBox *box = SP_3DBOX(object);
184 // FIXME: How to handle other contexts???
185 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
186 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
187 return repr;
189 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
190 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
191 repr = xml_doc->createElement("svg:g");
192 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
193 /* Hook paths to the faces of the box */
194 for (int i = 0; i < 6; ++i) {
195 box->faces[i]->hook_path_to_3dbox();
196 }
197 }
199 for (int i = 0; i < 6; ++i) {
200 box->faces[i]->set_path_repr();
201 }
203 if (flags & SP_OBJECT_WRITE_EXT) {
204 gchar *coords;
205 coords = sp_3dbox_get_corner_coords_string (box, 2);
206 repr->setAttribute("inkscape:box3dcornerA", coords);
208 coords = sp_3dbox_get_corner_coords_string (box, 1);
209 repr->setAttribute("inkscape:box3dcornerB", coords);
211 coords = sp_3dbox_get_corner_coords_string (box, 5);
212 repr->setAttribute("inkscape:box3dcornerC", coords);
214 g_free ((void *) coords);
215 }
217 if (((SPObjectClass *) (parent_class))->write) {
218 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
219 }
221 return repr;
222 }
224 static gchar *
225 sp_3dbox_description(SPItem *item)
226 {
227 g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
229 return g_strdup(_("<b>3D Box</b>"));
230 }
232 void
233 sp_3dbox_position_set (SP3DBoxContext &bc)
234 {
235 SP3DBox *box3d = SP_3DBOX(bc.item);
237 sp_3dbox_set_shape(box3d);
239 // FIXME: Why does the following call not automatically update the children
240 // of box3d (which is an SPGroup, which should do this)?
241 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
243 /**
244 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
245 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
246 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
247 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
248 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
249 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
250 ***/
251 }
253 static void
254 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
255 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
256 // called from sp_3dbox_update().
257 //sp_3dbox_set_shape(SPShape *shape)
258 sp_3dbox_set_shape(SP3DBox *box3d)
259 {
260 // FIXME: How to handle other contexts???
261 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
262 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
263 return;
264 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
266 /* Only update the curves during dragging; setting the svg representations
267 is expensive and only done once at the end */
268 sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
269 if (bc->extruded) {
270 box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
271 box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
272 box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
273 box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
274 box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
275 }
276 box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
278 sp_3dbox_update_curves (box3d);
279 }
282 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
283 {
284 sp_3dbox_move_corner_in_XY_plane (box, 2, A);
285 sp_3dbox_move_corner_in_XY_plane (box, 1, B);
286 sp_3dbox_move_corner_in_Z_direction (box, 5, C);
287 }
289 void
290 sp_3dbox_update_curves (SP3DBox *box) {
291 for (int i = 0; i < 6; ++i) {
292 if (box->faces[i]) box->faces[i]->set_curve();
293 }
294 }
296 /**
297 * In some situations (e.g., after cloning boxes, undo & redo, or reading boxes from a file) there are
298 * paths already present in the document which correspond to the faces of newly created boxes, but their
299 * 'path' members don't link to them yet. The following function corrects this if necessary.
300 */
301 void
302 sp_3dbox_link_to_existing_paths (SP3DBox *box, Inkscape::XML::Node *repr) {
303 // TODO: We should probably destroy the existing paths and recreate them because we don't know
304 // precisely which path corresponds to which face. Does this make a difference?
305 // In sp_3dbox_write we write the correct paths anyway, don't we? But we could get into
306 // trouble at a later stage when we only write single faces for degenerate boxes.
308 SPDocument *document = SP_OBJECT_DOCUMENT(box);
309 guint face_id = 0;
311 for (Inkscape::XML::Node *i = sp_repr_children(repr); i != NULL; i = sp_repr_next(i)) {
312 if (face_id > 5) {
313 g_warning ("SVG representation of 3D boxes must contain 6 paths or less.\n");
314 break;
315 }
317 SPObject *face_object = document->getObjectByRepr((Inkscape::XML::Node *) i);
318 if (!SP_IS_PATH(face_object)) {
319 g_warning ("SVG representation of 3D boxes should only contain paths.\n");
320 continue;
321 }
322 box->faces[face_id]->hook_path_to_3dbox(SP_PATH(face_object));
323 ++face_id;
324 }
325 if (face_id < 6) {
326 //g_warning ("SVG representation of 3D boxes should contain exactly 6 paths (degenerate boxes are not yet supported).\n");
327 // TODO: Check whether it is safe to add the remaining paths to the box and do so in case it is.
328 // (But we also land here for newly created boxes where we shouldn't add any paths because
329 // This is done in sp_3dbox_write later on.
330 }
331 }
333 void
334 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
335 {
336 NR::Point A (box->corners[id ^ Box3D::XY]);
337 if (Box3D::is_single_axis_direction (axes)) {
338 pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
339 }
341 /* set the 'front' corners */
342 box->corners[id] = pt;
344 Box3D::PerspectiveLine pl_one (A, Box3D::Y);
345 Box3D::PerspectiveLine pl_two (pt, Box3D::X);
346 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
348 pl_one = Box3D::PerspectiveLine (A, Box3D::X);
349 pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
350 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
352 /* set the 'rear' corners */
353 NR::Point B (box->corners[id ^ Box3D::XYZ]);
355 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
356 pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
357 box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
359 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
360 pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
361 box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
363 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
364 pl_two = Box3D::PerspectiveLine (B, Box3D::X);
365 box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
367 }
369 void
370 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
371 {
372 if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
374 /* set the four corners of the face containing corners[id] */
375 box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
377 Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
378 Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
379 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
381 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
382 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
383 box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
385 pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
386 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
387 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
388 }
390 NR::Maybe<NR::Point>
391 sp_3dbox_get_center (SP3DBox *box)
392 {
393 return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
394 }
396 NR::Maybe<NR::Point>
397 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
398 {
399 Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
401 // Is all this sufficiently precise also for degenerate cases?
402 if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
403 Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
405 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
406 Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
407 NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
409 if (!adjacent_face_center) return NR::Nothing();
411 Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
412 return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
413 } else {
414 Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
415 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
416 Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
417 return diag1.intersect(diag2);
418 }
419 }
421 static gchar *
422 sp_3dbox_get_corner_coords_string (SP3DBox *box, guint id)
423 {
424 id = id % 8;
425 return g_strdup_printf ("%f,%f", box->corners[id][NR::X], box->corners[id][NR::Y]);
426 }
428 static std::pair<gdouble, gdouble>
429 sp_3dbox_get_coord_pair_from_string (const gchar *coords)
430 {
431 gchar **coordlist = g_strsplit( coords, ",", 0);
432 // We might as well rely on g_strtod to convert the NULL pointer to 0.0,
433 // but we include the following test anyway
434 if (coordlist[0] == NULL || coordlist[1] == NULL) {
435 g_strfreev (coordlist);
436 g_warning ("Coordinate conversion failed.\n");
437 return std::make_pair(0.0, 0.0);
438 }
440 gdouble coord1 = g_strtod(coordlist[0], NULL);
441 gdouble coord2 = g_strtod(coordlist[1], NULL);
442 g_strfreev (coordlist);
444 return std::make_pair(coord1, coord2);
445 }
447 // auxiliary function
448 static void
449 sp_3dbox_update_corner_with_value_from_svg (SPObject *object, guint corner_id, const gchar *value)
450 {
451 if (value == NULL) return;
452 SP3DBox *box = SP_3DBOX(object);
454 std::pair<gdouble, gdouble> coord_pair = sp_3dbox_get_coord_pair_from_string (value);
455 box->corners[corner_id] = NR::Point (coord_pair.first, coord_pair.second);
456 sp_3dbox_recompute_corners (box, box->corners[2], box->corners[1], box->corners[5]);
457 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
458 }
460 /*
461 Local Variables:
462 mode:c++
463 c-file-style:"stroustrup"
464 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
465 indent-tabs-mode:nil
466 fill-column:99
467 End:
468 */
469 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :