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 "box3d.h"
20 static void sp_3dbox_class_init(SP3DBoxClass *klass);
21 static void sp_3dbox_init(SP3DBox *box3d);
23 static void sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
24 static void sp_shape_release (SPObject *object);
25 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value);
26 static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
27 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
29 //static gchar *sp_3dbox_description(SPItem *item);
31 //static void sp_3dbox_set_shape(SPShape *shape);
32 static void sp_3dbox_set_shape(SP3DBox *box3d);
34 static SPGroupClass *parent_class;
36 GType
37 sp_3dbox_get_type(void)
38 {
39 static GType type = 0;
41 if (!type) {
42 GTypeInfo info = {
43 sizeof(SP3DBoxClass),
44 NULL, /* base_init */
45 NULL, /* base_finalize */
46 (GClassInitFunc) sp_3dbox_class_init,
47 NULL, /* class_finalize */
48 NULL, /* class_data */
49 sizeof(SP3DBox),
50 16, /* n_preallocs */
51 (GInstanceInitFunc) sp_3dbox_init,
52 NULL, /* value_table */
53 };
54 type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
55 }
57 return type;
58 }
60 static void
61 sp_3dbox_class_init(SP3DBoxClass *klass)
62 {
63 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
64 // SPItemClass *item_class = (SPItemClass *) klass;
66 parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
68 sp_object_class->build = sp_3dbox_build;
69 sp_object_class->set = sp_3dbox_set;
70 sp_object_class->write = sp_3dbox_write;
71 sp_object_class->update = sp_3dbox_update;
73 //item_class->description = sp_3dbox_description;
74 }
76 static void
77 sp_3dbox_init(SP3DBox *box3d)
78 {
79 box3d->faces[4] = new Box3DFace (box3d);
80 //box3d->faces[4]->hook_path_to_3dbox();
81 for (int i = 0; i < 6; ++i) {
82 if (i == 4) continue;
83 box3d->faces[i] = NULL;
84 }
85 }
87 static void
88 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
89 {
90 if (((SPObjectClass *) (parent_class))->build) {
91 ((SPObjectClass *) (parent_class))->build(object, document, repr);
92 }
94 //sp_object_read_attr(object, "width");
95 }
97 static void
98 sp_3dbox_release (SPObject *object)
99 {
100 SP3DBox *box3d = SP_3DBOX(object);
101 for (int i = 0; i < 6; ++i) {
102 if (box3d->faces[i]) {
103 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
104 }
105 }
107 if (((SPObjectClass *) parent_class)->release) {
108 ((SPObjectClass *) parent_class)->release (object);
109 }
110 }
113 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
114 {
115 //SP3DBox *box3d = SP_3DBOX(object);
117 switch (key) {
118 /***
119 case SP_ATTR_WIDTH:
120 rect->width.readOrUnset(value);
121 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
122 break;
123 ***/
124 default:
125 if (((SPObjectClass *) (parent_class))->set) {
126 ((SPObjectClass *) (parent_class))->set(object, key, value);
127 }
128 break;
129 }
130 }
133 static void
134 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
135 {
136 //SP3DBox *box3d = SP_3DBOX(object);
138 /* Invoke parent method */
139 if (((SPObjectClass *) (parent_class))->update)
140 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
142 //sp_3dbox_set_shape (box3d);
143 }
147 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
148 {
149 SP3DBox *box3d = SP_3DBOX(object);
150 // FIXME: How to handle other contexts???
151 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
152 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
153 return repr;
154 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
156 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
157 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
158 repr = xml_doc->createElement("svg:g");
159 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
160 }
163 box3d->faces[4]->set_path_repr();
164 if (bc->extruded) {
165 for (int i = 0; i < 6; ++i) {
166 if (i == 4) continue;
167 box3d->faces[i]->set_path_repr();
168 }
169 }
170 if (((SPObjectClass *) (parent_class))->write) {
171 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
172 }
174 return repr;
175 }
177 void
178 sp_3dbox_position_set (SP3DBoxContext &bc)
179 {
180 SP3DBox *box3d = SP_3DBOX(bc.item);
182 sp_3dbox_set_shape(box3d);
184 // FIXME: Why does the following call not automatically update the children
185 // of box3d (which is an SPGroup, which should do this)?
186 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
188 /**
189 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
190 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
191 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
193 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
194 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
195 ***/
196 }
198 static void
199 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
200 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
201 // called from sp_3dbox_update().
202 //sp_3dbox_set_shape(SPShape *shape)
203 sp_3dbox_set_shape(SP3DBox *box3d)
204 {
205 // FIXME: How to handle other contexts???
206 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
207 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
208 return;
209 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
211 /* Only update the curves during dragging; setting the svg representations
212 is expensive and only done once at the end */
213 sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
214 if (bc->extruded) {
215 box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
216 box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
217 box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
218 box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
219 box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
220 }
221 box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
223 sp_3dbox_update_curves (box3d);
224 }
227 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
228 {
229 sp_3dbox_move_corner_in_XY_plane (box, 2, A);
230 sp_3dbox_move_corner_in_XY_plane (box, 1, B);
231 sp_3dbox_move_corner_in_Z_direction (box, 5, C);
232 }
234 void
235 sp_3dbox_update_curves (SP3DBox *box) {
236 for (int i = 0; i < 6; ++i) {
237 if (box->faces[i]) box->faces[i]->set_curve();
238 }
239 }
241 void
242 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
243 {
244 NR::Point A (box->corners[id ^ Box3D::XY]);
245 if (Box3D::is_single_axis_direction (axes)) {
246 pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
247 }
249 /* set the 'front' corners */
250 box->corners[id] = pt;
252 Box3D::PerspectiveLine pl_one (A, Box3D::Y);
253 Box3D::PerspectiveLine pl_two (pt, Box3D::X);
254 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
256 pl_one = Box3D::PerspectiveLine (A, Box3D::X);
257 pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
258 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
260 /* set the 'rear' corners */
261 NR::Point B (box->corners[id ^ Box3D::XYZ]);
263 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
264 pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
265 box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
267 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
268 pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
269 box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
271 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
272 pl_two = Box3D::PerspectiveLine (B, Box3D::X);
273 box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
275 }
277 void
278 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
279 {
280 if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
282 /* set the four corners of the face containing corners[id] */
283 box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
285 Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
286 Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
287 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
289 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
290 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
291 box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
293 pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
294 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
295 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
296 }
298 NR::Maybe<NR::Point>
299 sp_3dbox_get_center (SP3DBox *box)
300 {
301 return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
302 }
304 NR::Maybe<NR::Point>
305 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
306 {
307 Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
309 // Is all this sufficiently precise also for degenerate cases?
310 if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
311 Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
313 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
314 Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
315 NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
317 if (!adjacent_face_center) return NR::Nothing();
319 Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
320 return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
321 } else {
322 Box3D::Axis dir = Box3D::extract_single_axis_direction (corner_axes);
323 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
324 Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
325 return diag1.intersect(diag2);
326 }
327 }
329 /*
330 Local Variables:
331 mode:c++
332 c-file-style:"stroustrup"
333 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
334 indent-tabs-mode:nil
335 fill-column:99
336 End:
337 */
338 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :