Code

Refactoring of 3D box internals.
[inkscape.git] / src / box3d.cpp
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         }
113 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
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     }
133 static void
134 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
136     //SP3DBox *box3d = SP_3DBOX(object);
138     /* Invoke parent method */
139     if (((SPObjectClass *) (parent_class))->update)
140         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
141     
142     //sp_3dbox_set_shape (box3d);
147 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
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;
177 void
178 sp_3dbox_position_set (SP3DBoxContext &bc)
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     ***/
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)
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);
227 void sp_3dbox_recompute_corners (SP3DBox *box, NR::Point const A, NR::Point const B, NR::Point const C)
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_constrained_Z_direction (box, 5, C);
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     }
241 void
242 sp_3dbox_move_corner_in_XY_plane (SP3DBox *box, guint id, NR::Point pt)
244     NR::Point A (box->corners[id ^ Box3D::XY]);
246     /* set the 'front' corners */
247     box->corners[id] = pt;
249     Box3D::PerspectiveLine pl_one (A, Box3D::Y);
250     Box3D::PerspectiveLine pl_two (pt, Box3D::X);
251     box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
253     pl_one = Box3D::PerspectiveLine (A, Box3D::X);
254     pl_two = Box3D::PerspectiveLine (pt, Box3D::Y);
255     box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
257     /* set the 'rear' corners */
258     NR::Point B (box->corners[id ^ Box3D::XYZ]);
260     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Z);
261     pl_two = Box3D::PerspectiveLine (B, Box3D::Y);
262     box->corners[id ^ Box3D::XZ] = pl_one.meet(pl_two);
264     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XZ], Box3D::X);
265     pl_two = Box3D::PerspectiveLine (pt, Box3D::Z);
266     box->corners[id ^ Box3D::Z] = pl_one.meet(pl_two);
268     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::Z], Box3D::Y);
269     pl_two = Box3D::PerspectiveLine (B, Box3D::X);
270     box->corners[id ^ Box3D::YZ] = pl_one.meet(pl_two);
271     
274 void
275 sp_3dbox_move_corner_in_constrained_Z_direction (SP3DBox *box, guint id, NR::Point pt)
277     /* set the four corners of the face containing corners[id] */
278     box->corners[id] = Box3D::PerspectiveLine (box->corners[id], Box3D::Z).closest_to(pt);
280     Box3D::PerspectiveLine pl_one (box->corners[id], Box3D::X);
281     Box3D::PerspectiveLine pl_two (box->corners[id ^ Box3D::XZ], Box3D::Z);
282     box->corners[id ^ Box3D::X] = pl_one.meet(pl_two);
284     pl_one = Box3D::PerspectiveLine (box->corners[id ^ Box3D::X], Box3D::Y);
285     pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::XYZ], Box3D::Z);
286     box->corners[id ^ Box3D::XY] = pl_one.meet(pl_two);
288     pl_one = Box3D::PerspectiveLine (box->corners[id], Box3D::Y);
289     pl_two = Box3D::PerspectiveLine (box->corners[id ^ Box3D::YZ], Box3D::Z);
290     box->corners[id ^ Box3D::Y] = pl_one.meet(pl_two);
294 /*
295   Local Variables:
296   mode:c++
297   c-file-style:"stroustrup"
298   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
299   indent-tabs-mode:nil
300   fill-column:99
301   End:
302 */
303 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :