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_3dbox_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;
72 //sp_object_class->release = sp_3dbox_release;
74 //item_class->description = sp_3dbox_description;
75 }
77 static void
78 sp_3dbox_init(SP3DBox *box3d)
79 {
80 box3d->faces[4] = new Box3DFace (box3d);
81 //box3d->faces[4]->hook_path_to_3dbox();
82 for (int i = 0; i < 6; ++i) {
83 if (i == 4) continue;
84 box3d->faces[i] = NULL;
85 }
86 }
88 static void
89 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
90 {
91 if (((SPObjectClass *) (parent_class))->build) {
92 ((SPObjectClass *) (parent_class))->build(object, document, repr);
93 }
95 //sp_object_read_attr(object, "width");
96 }
98 /*
99 static void
100 sp_3dbox_release (SPObject *object)
101 {
102 SP3DBox *box3d = SP_3DBOX(object);
103 for (int i = 0; i < 6; ++i) {
104 if (box3d->faces[i]) {
105 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
106 }
107 }
109 if (((SPObjectClass *) parent_class)->release) {
110 ((SPObjectClass *) parent_class)->release (object);
111 }
112 }
113 */
115 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
116 {
117 //SP3DBox *box3d = SP_3DBOX(object);
119 switch (key) {
120 /***
121 case SP_ATTR_WIDTH:
122 rect->width.readOrUnset(value);
123 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
124 break;
125 ***/
126 default:
127 if (((SPObjectClass *) (parent_class))->set) {
128 ((SPObjectClass *) (parent_class))->set(object, key, value);
129 }
130 break;
131 }
132 }
135 static void
136 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
137 {
138 //SP3DBox *box3d = SP_3DBOX(object);
140 /* Invoke parent method */
141 if (((SPObjectClass *) (parent_class))->update)
142 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
144 //sp_3dbox_set_shape (box3d);
145 }
149 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
150 {
151 SP3DBox *box3d = SP_3DBOX(object);
152 // FIXME: How to handle other contexts???
153 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
154 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
155 return repr;
156 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
158 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
159 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
160 repr = xml_doc->createElement("svg:g");
161 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
162 }
165 box3d->faces[4]->set_path_repr();
166 if (bc->extruded) {
167 for (int i = 0; i < 6; ++i) {
168 if (i == 4) continue;
169 box3d->faces[i]->set_path_repr();
170 }
171 }
172 if (((SPObjectClass *) (parent_class))->write) {
173 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
174 }
176 return repr;
177 }
179 void
180 sp_3dbox_position_set (SP3DBoxContext &bc)
181 {
182 SP3DBox *box3d = SP_3DBOX(bc.item);
184 sp_3dbox_set_shape(box3d);
186 // FIXME: Why does the following call not automatically update the children
187 // of box3d (which is an SPGroup, which should do this)?
188 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
190 /**
191 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
193 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
194 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
195 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
196 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
197 ***/
198 }
200 static void
201 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
202 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
203 // called from sp_3dbox_update().
204 //sp_3dbox_set_shape(SPShape *shape)
205 sp_3dbox_set_shape(SP3DBox *box3d)
206 {
207 // FIXME: How to handle other contexts???
208 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
209 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
210 return;
211 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
213 /* Only update the curves during dragging; setting the svg representations
214 is expensive and only done once at the end */
215 sp_3dbox_recompute_corners (box3d, bc->drag_origin, bc->drag_ptB, bc->drag_ptC);
216 if (bc->extruded) {
217 box3d->faces[0]->set_corners (box3d->corners[0], box3d->corners[4], box3d->corners[6], box3d->corners[2]);
218 box3d->faces[1]->set_corners (box3d->corners[1], box3d->corners[5], box3d->corners[7], box3d->corners[3]);
219 box3d->faces[2]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[5], box3d->corners[4]);
220 box3d->faces[3]->set_corners (box3d->corners[2], box3d->corners[3], box3d->corners[7], box3d->corners[6]);
221 box3d->faces[5]->set_corners (box3d->corners[4], box3d->corners[5], box3d->corners[7], box3d->corners[6]);
222 }
223 box3d->faces[4]->set_corners (box3d->corners[0], box3d->corners[1], box3d->corners[3], box3d->corners[2]);
225 sp_3dbox_update_curves (box3d);
226 }
229 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
230 {
231 sp_3dbox_move_corner_in_XY_plane (box, 2, A);
232 sp_3dbox_move_corner_in_XY_plane (box, 1, B);
233 sp_3dbox_move_corner_in_Z_direction (box, 5, C);
234 }
236 void
237 sp_3dbox_update_curves (SP3DBox *box) {
238 for (int i = 0; i < 6; ++i) {
239 if (box->faces[i]) box->faces[i]->set_curve();
240 }
241 }
243 void
244 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt, Box3D::Axis axes)
245 {
246 NR::Point A (box->corners[id ^ Box3D::XY]);
247 if (Box3D::is_single_axis_direction (axes)) {
248 pt = Box3D::PerspectiveLine (box->corners[id], axes).closest_to(pt);
249 }
251 /* set the 'front' corners */
252 box->corners[id] = pt;
254 Box3D::PerspectiveLine pl_one (A, Box3D::Y);
255 Box3D::PerspectiveLine pl_two (pt, Box3D::X);
256 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
258 pl_one = Box3D::PerspectiveLine (A, Box3D::X);
259 pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
260 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
262 /* set the 'rear' corners */
263 NR::Point B (box->corners[id ^ Box3D::XYZ]);
265 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
266 pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
267 box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
269 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
270 pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
271 box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
273 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
274 pl_two = Box3D::PerspectiveLine (B, Box3D::X);
275 box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
277 }
279 void
280 sp_3dbox_move_corner_in_Z_direction (SP3DBox *box, guint id, NR::Point pt, bool constrained)
281 {
282 if (!constrained) sp_3dbox_move_corner_in_XY_plane (box, id, pt, Box3D::XY);
284 /* set the four corners of the face containing corners[id] */
285 box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
287 Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
288 Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
289 box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
291 pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
292 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
293 box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
295 pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
296 pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
297 box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
298 }
300 NR::Maybe<NR::Point>
301 sp_3dbox_get_center (SP3DBox *box)
302 {
303 return sp_3dbox_get_midpoint_between_corners (box, 0, 7);
304 }
306 NR::Maybe<NR::Point>
307 sp_3dbox_get_midpoint_between_corners (SP3DBox *box, guint id_corner1, guint id_corner2)
308 {
309 Box3D::Axis corner_axes = (Box3D::Axis) (id_corner1 ^ id_corner2);
311 // Is all this sufficiently precise also for degenerate cases?
312 if (sp_3dbox_corners_are_adjacent (id_corner1, id_corner2)) {
313 Box3D::Axis orth_dir = get_perpendicular_axis_direction (corner_axes);
315 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2 ^ orth_dir]);
316 Box3D::Line diag2 (box->corners[id_corner1 ^ orth_dir], box->corners[id_corner2]);
317 NR::Maybe<NR::Point> adjacent_face_center = diag1.intersect(diag2);
319 if (!adjacent_face_center) return NR::Nothing();
321 Box3D::PerspectiveLine pl (*adjacent_face_center, orth_dir);
322 return pl.intersect(Box3D::PerspectiveLine(box->corners[id_corner1], corner_axes));
323 } else {
324 Box3D::Axis dir = Box3D::extract_single_axis_direction (corner_axes);
325 Box3D::Line diag1 (box->corners[id_corner1], box->corners[id_corner2]);
326 Box3D::Line diag2 (box->corners[id_corner1 ^ dir], box->corners[id_corner2 ^ dir]);
327 return diag1.intersect(diag2);
328 }
329 }
331 /*
332 Local Variables:
333 mode:c++
334 c-file-style:"stroustrup"
335 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
336 indent-tabs-mode:nil
337 fill-column:99
338 End:
339 */
340 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :