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 *box3d)
80 {
81 box3d->faces[4] = new Box3DFace (box3d);
82 //box3d->faces[4]->hook_path_to_3dbox();
83 for (int i = 0; i < 6; ++i) {
84 if (i == 4) continue;
85 box3d->faces[i] = NULL;
86 }
87 }
89 static void
90 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
91 {
92 if (((SPObjectClass *) (parent_class))->build) {
93 ((SPObjectClass *) (parent_class))->build(object, document, repr);
94 }
96 //sp_object_read_attr(object, "width");
97 }
99 /*
100 static void
101 sp_3dbox_release (SPObject *object)
102 {
103 SP3DBox *box3d = SP_3DBOX(object);
104 for (int i = 0; i < 6; ++i) {
105 if (box3d->faces[i]) {
106 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
107 }
108 }
110 if (((SPObjectClass *) parent_class)->release) {
111 ((SPObjectClass *) parent_class)->release (object);
112 }
113 }
114 */
116 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
117 {
118 //SP3DBox *box3d = SP_3DBOX(object);
120 switch (key) {
121 /***
122 case SP_ATTR_WIDTH:
123 rect->width.readOrUnset(value);
124 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
125 break;
126 ***/
127 default:
128 if (((SPObjectClass *) (parent_class))->set) {
129 ((SPObjectClass *) (parent_class))->set(object, key, value);
130 }
131 break;
132 }
133 }
136 static void
137 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
138 {
139 //SP3DBox *box3d = SP_3DBOX(object);
141 /* Invoke parent method */
142 if (((SPObjectClass *) (parent_class))->update)
143 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
145 //sp_3dbox_set_shape (box3d);
146 }
150 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
151 {
152 SP3DBox *box3d = SP_3DBOX(object);
153 // FIXME: How to handle other contexts???
154 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
155 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
156 return repr;
157 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
159 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
160 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
161 repr = xml_doc->createElement("svg:g");
162 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
163 }
166 box3d->faces[4]->set_path_repr();
167 if (bc->extruded) {
168 for (int i = 0; i < 6; ++i) {
169 if (i == 4) continue;
170 box3d->faces[i]->set_path_repr();
171 }
172 }
173 if (((SPObjectClass *) (parent_class))->write) {
174 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
175 }
177 return repr;
178 }
180 static gchar *
181 sp_3dbox_description(SPItem *item)
182 {
183 g_return_val_if_fail(SP_IS_3DBOX(item), NULL);
185 return g_strdup(_("<b>3D Box</b>"));
186 }
188 void
189 sp_3dbox_position_set (SP3DBoxContext &bc)
190 {
191 SP3DBox *box3d = SP_3DBOX(bc.item);
193 sp_3dbox_set_shape(box3d);
195 // FIXME: Why does the following call not automatically update the children
196 // of box3d (which is an SPGroup, which should do this)?
197 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
199 /**
200 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
201 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
203 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
204 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
205 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 ***/
207 }
209 static void
210 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
211 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
212 // called from sp_3dbox_update().
213 //sp_3dbox_set_shape(SPShape *shape)
214 sp_3dbox_set_shape(SP3DBox *box3d)
215 {
216 // FIXME: How to handle other contexts???
217 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
218 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
219 return;
220 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
222 /* Only update the curves during dragging; setting the svg representations
223 is expensive and only done once at the end */
224 sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
225 if (bc->extruded) {
226 box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
227 box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
228 box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
229 box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
230 box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
231 }
232 box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
234 sp_3dbox_update_curves (box3d);
235 }
238 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
239 {
240 sp_3dbox_move_corner_in_XY_plane (box, 2, A);
241 sp_3dbox_move_corner_in_XY_plane (box, 1, B);
242 sp_3dbox_move_corner_in_Z_direction (box, 5, C);
243 }
245 void
246 sp_3dbox_update_curves (SP3DBox *box) {
247 for (int i = 0; i < 6; ++i) {
248 if (box->faces[i]) box->faces[i]->set_curve();
249 }
250 }
252 void
253 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
254 {
255 NR::Point A (box->corners[id ^ Box3D::XY]);
256 if (Box3D::is_single_axis_direction (axes)) {
257 pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
258 }
260 /* set the 'front' corners */
261 box->corners[id] = pt;
263 Box3D::PerspectiveLine pl_one (A, Box3D::Y);
264 Box3D::PerspectiveLine pl_two (pt, Box3D::X);
265 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
267 pl_one = Box3D::PerspectiveLine (A, Box3D::X);
268 pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
269 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
271 /* set the 'rear' corners */
272 NR::Point B (box->corners[id ^ Box3D::XYZ]);
274 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
275 pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
276 box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
278 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
279 pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
280 box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
282 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
283 pl_two = Box3D::PerspectiveLine (B, Box3D::X);
284 box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
286 }
288 void
289 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
290 {
291 if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
293 /* set the four corners of the face containing corners[id] */
294 box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
296 Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
297 Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
298 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
300 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
301 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
302 box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
304 pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
305 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
306 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
307 }
309 NR::Maybe<NR::Point>
310 sp_3dbox_get_center (SP3DBox *box)
311 {
312 return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
313 }
315 NR::Maybe<NR::Point>
316 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
317 {
318 Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
320 // Is all this sufficiently precise also for degenerate cases?
321 if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
322 Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
324 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
325 Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
326 NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
328 if (!adjacent_face_center) return NR::Nothing();
330 Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
331 return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
332 } else {
333 Box3D::Axis dir = Box3D::extract_first_axis_direction (corner_axes);
334 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
335 Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
336 return diag1.intersect(diag2);
337 }
338 }
340 /*
341 Local Variables:
342 mode:c++
343 c-file-style:"stroustrup"
344 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
345 indent-tabs-mode:nil
346 fill-column:99
347 End:
348 */
349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :