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 if (box3d == NULL) { g_warning ("box3d is NULL!\n"); }
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 static void
99 sp_3dbox_release (SPObject *object)
100 {
101 SP3DBox *box3d = SP_3DBOX(object);
102 for (int i = 0; i < 6; ++i) {
103 if (box3d->faces[i]) {
104 delete box3d->faces[i]; // FIXME: Anything else to do? Do we need to clean up the face first?
105 }
106 }
108 if (((SPObjectClass *) parent_class)->release) {
109 ((SPObjectClass *) parent_class)->release (object);
110 }
111 }
114 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
115 {
116 //SP3DBox *box3d = SP_3DBOX(object);
118 switch (key) {
119 /***
120 case SP_ATTR_WIDTH:
121 rect->width.readOrUnset(value);
122 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
123 break;
124 ***/
125 default:
126 if (((SPObjectClass *) (parent_class))->set) {
127 ((SPObjectClass *) (parent_class))->set(object, key, value);
128 }
129 break;
130 }
131 }
134 static void
135 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
136 {
137 //SP3DBox *box3d = SP_3DBOX(object);
139 /* Invoke parent method */
140 if (((SPObjectClass *) (parent_class))->update)
141 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
143 //sp_3dbox_set_shape (box3d);
144 }
148 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
149 {
150 SP3DBox *box3d = SP_3DBOX(object);
151 // FIXME: How to handle other contexts???
152 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
153 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
154 return repr;
155 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
157 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
158 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
159 repr = xml_doc->createElement("svg:g");
160 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
161 }
164 box3d->faces[4]->set_path_repr();
165 if (bc->extruded) {
166 NR::Point corner1, corner2, corner3, corner4;
167 sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4);
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 void
181 sp_3dbox_position_set (SP3DBoxContext &bc)
182 {
183 SP3DBox *box3d = SP_3DBOX(bc.item);
185 sp_3dbox_set_shape(box3d);
187 // FIXME: Why does the following call not automatically update the children
188 // of box3d (which is an SPGroup, which should do this)?
189 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
191 /**
192 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
193 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
194 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
195 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
196 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
197 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
198 ***/
199 }
201 static void
202 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
203 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
204 // called from sp_3dbox_update().
205 //sp_3dbox_set_shape(SPShape *shape)
206 sp_3dbox_set_shape(SP3DBox *box3d)
207 {
208 // FIXME: How to handle other contexts???
209 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
210 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
211 return;
212 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
214 // FIXME: Why must the coordinates be flipped vertically???
215 //SPDocument *doc = SP_OBJECT_DOCUMENT(box3d);
216 //gdouble height = sp_document_height(doc);
218 /* Curve-adaption variant: */
219 NR::Point corner1, corner2, corner3, corner4;
220 corner1 = bc->drag_origin;
222 if (bc->extruded) {
223 sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4);
224 for (int i=0; i < 6; ++i) {
225 if (!box3d->faces[i]) {
226 g_warning ("Face no. %d does not exist!\n", i);
227 return;
228 }
229 }
230 box3d->faces[0]->set_face (bc->drag_origin, corner4, Box3D::Z, Box3D::Y);
231 box3d->faces[0]->set_curve();
232 box3d->faces[5]->set_face (corner3, bc->drag_ptC, Box3D::Y, Box3D::X);
233 box3d->faces[5]->set_curve();
234 box3d->faces[3]->set_face (bc->drag_origin, corner2, Box3D::X, Box3D::Z);
235 box3d->faces[3]->set_curve();
236 box3d->faces[2]->set_face (bc->drag_ptB, corner4, Box3D::X, Box3D::Z);
237 box3d->faces[2]->set_curve();
239 box3d->faces[1]->set_face (corner1, bc->drag_ptC, Box3D::Z, Box3D::Y);
240 box3d->faces[1]->set_curve();
241 }
242 box3d->faces[4]->set_face(bc->drag_origin, bc->drag_ptB, Box3D::Y, Box3D::X);
243 box3d->faces[4]->set_curve();
244 }
247 void
248 sp_3dbox_compute_specific_corners (SP3DBoxContext *box3d_context, NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
249 {
250 // TODO: Check for numerical stability and handle "wrong" cases more gracefully.
251 // (This now mostly applies to the intersection code in the PerspectiveLine class)
252 Box3D::PerspectiveLine pl1 (box3d_context->drag_origin, Box3D::X);
253 Box3D::PerspectiveLine pl2 (box3d_context->drag_ptB, Box3D::Y);
254 corner1 = pl1.meet(pl2);
256 Box3D::PerspectiveLine pl3 (corner1, Box3D::Z);
257 Box3D::PerspectiveLine pl4 (box3d_context->drag_ptC, Box3D::Y);
258 corner2 = pl3.meet(pl4);
260 Box3D::PerspectiveLine pl5 (corner2, Box3D::X);
261 Box3D::PerspectiveLine pl6 (box3d_context->drag_origin, Box3D::Z);
262 corner3 = pl5.meet(pl6);
264 Box3D::PerspectiveLine pl7 (box3d_context->drag_ptC, Box3D::X);
265 Box3D::PerspectiveLine pl8 (corner3, Box3D::Y);
266 corner4 = pl7.meet(pl8);
267 }
271 /*
272 Local Variables:
273 mode:c++
274 c-file-style:"stroustrup"
275 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
276 indent-tabs-mode:nil
277 fill-column:99
278 End:
279 */
280 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :