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_set(SPObject *object, unsigned int key, const gchar *value);
25 static void sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags);
26 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
28 //static gchar *sp_3dbox_description(SPItem *item);
30 //static void sp_3dbox_set_shape(SPShape *shape);
31 static void sp_3dbox_set_shape(SP3DBox *box3d);
33 static SPGroupClass *parent_class;
35 GType
36 sp_3dbox_get_type(void)
37 {
38 static GType type = 0;
40 if (!type) {
41 GTypeInfo info = {
42 sizeof(SP3DBoxClass),
43 NULL, /* base_init */
44 NULL, /* base_finalize */
45 (GClassInitFunc) sp_3dbox_class_init,
46 NULL, /* class_finalize */
47 NULL, /* class_data */
48 sizeof(SP3DBox),
49 16, /* n_preallocs */
50 (GInstanceInitFunc) sp_3dbox_init,
51 NULL, /* value_table */
52 };
53 type = g_type_register_static(SP_TYPE_GROUP, "SP3DBox", &info, (GTypeFlags) 0);
54 }
56 return type;
57 }
59 static void
60 sp_3dbox_class_init(SP3DBoxClass *klass)
61 {
62 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
63 SPItemClass *item_class = (SPItemClass *) klass;
65 parent_class = (SPGroupClass *) g_type_class_ref(SP_TYPE_GROUP);
67 sp_object_class->build = sp_3dbox_build;
68 sp_object_class->set = sp_3dbox_set;
69 sp_object_class->write = sp_3dbox_write;
70 sp_object_class->update = sp_3dbox_update;
72 //item_class->description = sp_3dbox_description;
73 }
75 static void
76 sp_3dbox_init(SP3DBox *box3d)
77 {
78 for (int i = 0; i < 6; ++i) {
79 box3d->faces[i] = Box3DFace (box3d);
80 }
81 }
83 static void
84 sp_3dbox_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
85 {
86 if (((SPObjectClass *) (parent_class))->build) {
87 ((SPObjectClass *) (parent_class))->build(object, document, repr);
88 }
90 //sp_object_read_attr(object, "width");
91 }
93 static void sp_3dbox_set(SPObject *object, unsigned int key, const gchar *value)
94 {
95 //SP3DBox *box3d = SP_3DBOX(object);
97 switch (key) {
98 /***
99 case SP_ATTR_WIDTH:
100 rect->width.readOrUnset(value);
101 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
102 break;
103 ***/
104 default:
105 if (((SPObjectClass *) (parent_class))->set) {
106 ((SPObjectClass *) (parent_class))->set(object, key, value);
107 }
108 break;
109 }
110 }
113 static void
114 sp_3dbox_update(SPObject *object, SPCtx *ctx, guint flags)
115 {
116 //SP3DBox *box3d = SP_3DBOX(object);
118 /* Invoke parent method */
119 if (((SPObjectClass *) (parent_class))->update)
120 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
122 //sp_3dbox_set_shape (box3d);
123 }
127 static Inkscape::XML::Node *sp_3dbox_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
128 {
129 SP3DBox *box3d = SP_3DBOX(object);
130 // FIXME: How to handle other contexts???
131 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
132 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
133 return repr;
134 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
136 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
137 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
138 repr = xml_doc->createElement("svg:g");
139 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
140 }
143 box3d->faces[0].set_path_repr();
144 if (bc->extruded) {
145 NR::Point corner1, corner2, corner3, corner4;
146 sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4);
147 for (int i = 0; i < 6; ++i) {
148 box3d->faces[i].set_path_repr();
149 }
150 }
151 if (((SPObjectClass *) (parent_class))->write) {
152 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
153 }
155 return repr;
156 }
158 void
159 sp_3dbox_position_set (SP3DBoxContext &bc)
160 {
161 SP3DBox *box3d = SP_3DBOX(bc.item);
163 sp_3dbox_set_shape(box3d);
165 // FIXME: Why does the following call not automatically update the children
166 // of box3d (which is an SPGroup, which should do this)?
167 //SP_OBJECT(box3d)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
169 /**
170 SP_OBJECT(box3d->path_face1)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
171 SP_OBJECT(box3d->path_face2)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
172 SP_OBJECT(box3d->path_face3)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
173 SP_OBJECT(box3d->path_face4)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
174 SP_OBJECT(box3d->path_face5)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
175 SP_OBJECT(box3d->path_face6)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
176 ***/
177 }
179 static void
180 // FIXME: Note that this is _not_ the virtual set_shape() method inherited from SPShape,
181 // since SP3DBox is inherited from SPGroup. The following method is "artificially"
182 // called from sp_3dbox_update().
183 //sp_3dbox_set_shape(SPShape *shape)
184 sp_3dbox_set_shape(SP3DBox *box3d)
185 {
186 // FIXME: How to handle other contexts???
187 // FIXME: Is tools_isactive(..) more recommended to check for the current context/tool?
188 if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
189 return;
190 SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
192 // FIXME: Why must the coordinates be flipped vertically???
193 //SPDocument *doc = SP_OBJECT_DOCUMENT(box3d);
194 //gdouble height = sp_document_height(doc);
196 /* Curve-adaption variant: */
197 NR::Point corner1, corner2, corner3, corner4;
198 corner1 = bc->drag_origin;
200 if (bc->extruded) {
201 sp_3dbox_compute_specific_corners (bc, corner1, corner2, corner3, corner4);
202 box3d->faces[2].set_shape(bc->drag_origin, corner4, Box3D::Z, Box3D::Y);
203 box3d->faces[2].set_curve();
204 box3d->faces[3].set_shape(corner2, corner4, Box3D::X, Box3D::Y);
205 box3d->faces[3].set_curve();
206 box3d->faces[4].set_shape(bc->drag_origin, corner2, Box3D::X, Box3D::Z);
207 box3d->faces[4].set_curve();
208 box3d->faces[5].set_shape(bc->drag_ptB, corner4, Box3D::X, Box3D::Z);
209 box3d->faces[5].set_curve();
211 box3d->faces[1].set_shape(corner1, bc->drag_ptC, Box3D::Z, Box3D::Y);
212 box3d->faces[1].set_curve();
213 }
214 box3d->faces[0].set_shape(bc->drag_origin, bc->drag_ptB, Box3D::X, Box3D::Y);
215 box3d->faces[0].set_curve();
216 }
219 void
220 sp_3dbox_compute_specific_corners (SP3DBoxContext *box3d_context, NR::Point &corner1, NR::Point &corner2, NR::Point &corner3, NR::Point &corner4)
221 {
222 // TODO: Check for numerical stability and handle "wrong" cases more gracefully.
223 // (This now mostly applies to the intersection code in the PerspectiveLine class)
224 Box3D::PerspectiveLine pl1 (box3d_context->drag_origin, Box3D::X);
225 Box3D::PerspectiveLine pl2 (box3d_context->drag_ptB, Box3D::Y);
226 corner1 = pl1.meet(pl2);
228 Box3D::PerspectiveLine pl3 (corner1, Box3D::Z);
229 Box3D::PerspectiveLine pl4 (box3d_context->drag_ptC, Box3D::Y);
230 corner2 = pl3.meet(pl4);
232 Box3D::PerspectiveLine pl5 (corner2, Box3D::X);
233 Box3D::PerspectiveLine pl6 (box3d_context->drag_origin, Box3D::Z);
234 corner3 = pl5.meet(pl6);
236 Box3D::PerspectiveLine pl7 (box3d_context->drag_ptC, Box3D::X);
237 Box3D::PerspectiveLine pl8 (corner3, Box3D::Y);
238 corner4 = pl7.meet(pl8);
239 }
243 /*
244 Local Variables:
245 mode:c++
246 c-file-style:"stroustrup"
247 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
248 indent-tabs-mode:nil
249 fill-column:99
250 End:
251 */
252 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :