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 "box3d.h"
21 static void sp_3dbox_class_init(SP3DBoxClass *klass);
22 static void sp_3dbox_init(SP3DBox *box3d);
24 static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
25 //static void sp_3dbox_release (SPObject *object);
26 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
27 static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
28 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
30 static gchar *sp_3dbox_description(SPItem *item);
32 //static void sp_3dbox_set_shape(SPShape *shape);
33 static void sp_3dbox_set_shape(SP3DBox *box3d);
35 static SPGroupClass *parent_class;
37 GType
38 sp_3dbox_get_type(void)
39 {
40 static GType type = 0;
42 if (!type) {
43 GTypeInfo info = {
44 sizeof(SP3DBoxClass),
45 NULL, /* base_init */
46 NULL, /* base_finalize */
47 (GClassInitFunc) sp_3dbox_class_init,
48 NULL, /* class_finalize */
49 NULL, /* class_data */
50 sizeof(SP3DBox),
51 16, /* n_preallocs */
52 (GInstanceInitFunc) sp_3dbox_init,
53 NULL, /* value_table */
54 };
55 type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
56 }
58 return type;
59 }
61 static void
62 sp_3dbox_class_init(SP3DBoxClass *klass)
63 {
64 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
65 SPItemClass *item_class = (SPItemClass *) klass;
67 parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
69 sp_object_class->build = sp_3dbox_build;
70 sp_object_class->set = sp_3dbox_set;
71 sp_object_class->write = sp_3dbox_write;
72 sp_object_class->update = sp_3dbox_update;
73 //sp_object_class->release = sp_3dbox_release;
75 item_class->description = sp_3dbox_description;
76 }
78 static void
79 sp_3dbox_init(SP3DBox *box)
80 {
81 // We create all faces in the beginning (but only the non-degenerate ones
82 // should be written to the svg representation later in sp_3dbox_write).
83 Box3D::Axis cur_plane, axis, dir1, dir2;
84 Box3D::FrontOrRear cur_pos;
85 for (int i = 0; i < 3; ++i) {
86 for (int j = 0; j < 2; ++j) {
87 cur_plane = Box3D::planes[i];
88 cur_pos = Box3D::face_positions[j];
89 // FIXME: The following code could theoretically be moved to
90 // the constructor of Box3DFace (but see the comment there).
91 axis = (cur_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (cur_plane));
92 dir1 = extract_first_axis_direction (cur_plane);
93 dir2 = extract_second_axis_direction (cur_plane);
95 box->faces[Box3D::face_to_int(cur_plane ^ cur_pos)] =
96 new Box3DFace (box, box->corners[axis], box->corners[axis ^ dir1],
97 box->corners[axis ^ dir1 ^ dir2], box->corners[axis ^ dir2],
98 cur_plane, cur_pos);
99 }
100 }
101 }
103 static void
104 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
105 {
106 if (((SPObjectClass *) (parent_class))->build) {
107 ((SPObjectClass *) (parent_class))->build(object, document, repr);
108 }
110 //sp_object_read_attr(object, "width");
111 }
113 /*
114 static void
115 sp_3dbox_release (SPObject *object)
116 {
117 SP3DBox *box3d = SP_3DBOX(object);
118 for (int i = 0; i < 6; ++i) {
119 if (box3d->faces[i]) {
120 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
121 }
122 }
124 if (((SPObjectClass *) parent_class)->release) {
125 ((SPObjectClass *) parent_class)->release (object);
126 }
127 }
128 */
130 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
131 {
132 //SP3DBox *box3d = SP_3DBOX(object);
134 switch (key) {
135 /***
136 case SP_ATTR_WIDTH:
137 rect->width.readOrUnset(value);
138 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
139 break;
140 ***/
141 default:
142 if (((SPObjectClass *) (parent_class))->set) {
143 ((SPObjectClass *) (parent_class))->set(object, key, value);
144 }
145 break;
146 }
147 }
150 static void
151 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
152 {
153 //SP3DBox *box3d = SP_3DBOX(object);
155 /* Invoke parent method */
156 if (((SPObjectClass *) (parent_class))->update)
157 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
159 //sp_3dbox_set_shape (box3d);
160 }
164 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
165 {
166 SP3DBox *box3d = SP_3DBOX(object);
167 // FIXME: How to handle other contexts???
168 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
169 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
170 return repr;
172 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
173 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
174 repr = xml_doc->createElement("svg:g");
175 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
176 }
178 for (int i = 0; i < 6; ++i) {
179 box3d->faces[i]->set_path_repr();
180 }
182 if (((SPObjectClass *) (parent_class))->write) {
183 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
184 }
186 return repr;
187 }
189 static gchar *
190 sp_3dbox_description(SPItem *item)
191 {
192 g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
194 return g_strdup(_("<b>3D Box</b>"));
195 }
197 void
198 sp_3dbox_position_set (SP3DBoxContext &bc)
199 {
200 SP3DBox *box3d = SP_3DBOX(bc.item);
202 sp_3dbox_set_shape(box3d);
204 // FIXME: Why does the following call not automatically update the children
205 // of box3d (which is an SPGroup, which should do this)?
206 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
208 /**
209 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
210 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
211 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
212 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
213 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
214 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
215 ***/
216 }
218 static void
219 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
220 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
221 // called from sp_3dbox_update().
222 //sp_3dbox_set_shape(SPShape *shape)
223 sp_3dbox_set_shape(SP3DBox *box3d)
224 {
225 // FIXME: How to handle other contexts???
226 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
227 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
228 return;
229 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
231 /* Only update the curves during dragging; setting the svg representations
232 is expensive and only done once at the end */
233 sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
234 if (bc->extruded) {
235 box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
236 box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
237 box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
238 box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
239 box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
240 }
241 box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
243 sp_3dbox_update_curves (box3d);
244 }
247 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
248 {
249 sp_3dbox_move_corner_in_XY_plane (box, 2, A);
250 sp_3dbox_move_corner_in_XY_plane (box, 1, B);
251 sp_3dbox_move_corner_in_Z_direction (box, 5, C);
252 }
254 void
255 sp_3dbox_update_curves (SP3DBox *box) {
256 for (int i = 0; i < 6; ++i) {
257 if (box->faces[i]) box->faces[i]->set_curve();
258 }
259 }
261 void
262 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
263 {
264 NR::Point A (box->corners[id ^ Box3D::XY]);
265 if (Box3D::is_single_axis_direction (axes)) {
266 pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
267 }
269 /* set the 'front' corners */
270 box->corners[id] = pt;
272 Box3D::PerspectiveLine pl_one (A, Box3D::Y);
273 Box3D::PerspectiveLine pl_two (pt, Box3D::X);
274 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
276 pl_one = Box3D::PerspectiveLine (A, Box3D::X);
277 pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
278 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
280 /* set the 'rear' corners */
281 NR::Point B (box->corners[id ^ Box3D::XYZ]);
283 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
284 pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
285 box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
287 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
288 pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
289 box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
291 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
292 pl_two = Box3D::PerspectiveLine (B, Box3D::X);
293 box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
295 }
297 void
298 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
299 {
300 if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
302 /* set the four corners of the face containing corners[id] */
303 box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
305 Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
306 Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
307 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
309 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
310 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
311 box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
313 pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
314 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
315 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
316 }
318 NR::Maybe<NR::Point>
319 sp_3dbox_get_center (SP3DBox *box)
320 {
321 return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
322 }
324 NR::Maybe<NR::Point>
325 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
326 {
327 Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
329 // Is all this sufficiently precise also for degenerate cases?
330 if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
331 Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
333 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
334 Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
335 NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
337 if (!adjacent_face_center) return NR::Nothing();
339 Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
340 return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
341 } else {
342 Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
343 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
344 Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
345 return diag1.intersect(diag2);
346 }
347 }
349 /*
350 Local Variables:
351 mode:c++
352 c-file-style:"stroustrup"
353 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
354 indent-tabs-mode:nil
355 fill-column:99
356 End:
357 */
358 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :