Code

Changed preference to use file chooser button
[inkscape.git] / src / box3d-face.cpp
1 #define __SP_3DBOX_FACE_C__
3 /*
4  * Face of a 3D box ('perspectivic rectangle')
5  *
6  * Authors:
7  *   Maximilian Albert <Anhalter42@gmx.de>
8  *
9  * Copyright (C) 2007  authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "svg/svg.h"
15 #include "box3d-face.h"
16 #include "prefs-utils.h"
18 // FIXME: It's quite redundant to pass the box plus the corners plus the axes. At least the corners can
19 //        theoretically be reconstructed from the box and the axes, but in order to do this we need
20 //        access to box->corners, which is not possible if we only have a forward declaration of SP3DBox
21 //        in box3d-face.h. (But we can't include box3d.h itself because the latter already includes
22 //        box3d-face.h).
23 Box3DFace::Box3DFace(SP3DBox *box, NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D,
24                      Box3D::Axis plane, Box3D::FrontOrRear rel_pos)
25     : front_or_rear (rel_pos), path (NULL), parent_box3d (box)
26  {
27     dir1 = extract_first_axis_direction (plane);
28     dir2 = extract_second_axis_direction (plane);
29     /*
30     Box3D::Axis axis = (rel_pos == Box3D::FRONT ? Box3D::NONE : Box3D::third_axis_direction (plane));
31     set_corners (box->corners[axis],
32                  box->corners[axis ^ dir1],
33                  box->corners[axis ^ dir1 ^ dir2],
34                  box->corners[axis ^ dir2]);
35     */
36     set_corners (A, B, C, D);
37 }
39 Box3DFace::~Box3DFace()
40 {
41     for (int i = 0; i < 4; ++i) {
42         if (this->corners[i]) {
43             //delete this->corners[i];
44             this->corners[i] = NULL;
45         }
46     }
47
49 void Box3DFace::set_corners(NR::Point &A, NR::Point &B, NR::Point &C, NR::Point &D)
50 {
51     corners[0] = &A;
52     corners[1] = &B;
53     corners[2] = &C;
54     corners[3] = &D;
55 }
57 /***
58 void Box3DFace::set_shape(NR::Point const ul, NR::Point const lr,
59                      Box3D::Axis const dir1, Box3D::Axis const dir2,
60                      unsigned int shift_count, NR::Maybe<NR::Point> pt_align, bool align_along_PL)
61 {
62     corners[0] = ul;
63     if (!pt_align) {
64         corners[2] = lr;
65     } else {
66         if (align_along_PL) {
67             Box3D::Axis dir3 = Box3D::third_axis_direction (dir1, dir2);
68             Box3D::Line line1(*Box3D::Perspective3D::current_perspective->get_vanishing_point(dir1), lr);
69             Box3D::Line line2(*pt_align, *Box3D::Perspective3D::current_perspective->get_vanishing_point(dir3));
70             corners[2] = *line1.intersect(line2);
71         } else {
72             corners[2] = Box3D::Line(*pt_align, *Box3D::Perspective3D::current_perspective->get_vanishing_point(dir1)).closest_to(lr);
73         }
74     }
76     Box3D::PerspectiveLine first_line  (corners[0], dir1);
77     Box3D::PerspectiveLine second_line (corners[2], dir2);
78     NR::Maybe<NR::Point> ur = first_line.intersect(second_line);
80     Box3D::PerspectiveLine third_line  (corners[0], dir2);
81     Box3D::PerspectiveLine fourth_line (corners[2], dir1);
82     NR::Maybe<NR::Point> ll = third_line.intersect(fourth_line);
84     // FIXME: How to handle the case if one of the intersections doesn't exist?
85     //        Maybe set them equal to the corresponding VPs? 
86     if (!ur) ur = NR::Point(0.0, 0.0);    
87     if (!ll) ll = NR::Point(0.0, 0.0);
89     corners[1] = *ll;
90     corners[3] = *ur;
92     this->dir1 = dir1;
93     this->dir2 = dir2;
95     // FIXME: Can be made more concise
96     NR::Point tmp_pt;
97     for (unsigned int i=0; i < shift_count; i++) {
98         tmp_pt = corners[3];
99         corners[1] = corners[0];
100         corners[2] = corners[1];
101         corners[3] = corners[2];
102         corners[0] = tmp_pt;
103     }
105 ***/
107 Box3DFace::Box3DFace(Box3DFace const &box3dface)
109     for (int i = 0; i < 4; ++i) {
110         this->corners[i] = box3dface.corners[i];
111     }
112     this->dir1 = box3dface.dir1;
113     this->dir2 = box3dface.dir2;
116 /**
117  * Construct a 3D box face with opposite corners A and C whose sides are directed
118  * along axis1 and axis2. The corners have the following order:
119  *
120  * A = corners[0]  --> along axis1 --> B = corners[1] --> along axis2 --> C = corners[2]
121  *                 --> along axis1 --> D = corners[3] --> along axis2 --> D = corners[0].
122  * 
123  * Note that several other functions rely on this precise order.
124  */
125 /***
126 void
127 Box3DFace::set_face (NR::Point const A, NR::Point const C, Box3D::Axis const axis1, Box3D::Axis const axis2)
129     *corners[0] = A;
130     *corners[2] = C;
131     if (!SP_IS_3DBOX_CONTEXT(inkscape_active_event_context()))
132         return;
133     SP3DBoxContext *bc = SP_3DBOX_CONTEXT(inkscape_active_event_context());
134     
135     Box3D::PerspectiveLine line1 (A, axis1, Box3D::Perspective3D::current_perspective);
136     Box3D::PerspectiveLine line2 (C, axis2, Box3D::Perspective3D::current_perspective);
137     NR::Maybe<NR::Point> B = line1.intersect(line2);
139     Box3D::PerspectiveLine line3 (*corners[0], axis2, Box3D::Perspective3D::current_perspective);
140     Box3D::PerspectiveLine line4 (*corners[2], axis1, Box3D::Perspective3D::current_perspective);
141     NR::Maybe<NR::Point> D = line3.intersect(line4);
143     // FIXME: How to handle the case if one of the intersections doesn't exist?
144     //        Maybe set them equal to the corresponding VPs? 
145     if (!D) D = NR::Point(0.0, 0.0);    
146     if (!B) B = NR::Point(0.0, 0.0);
148     *corners[1] = *B;
149     *corners[3] = *D;
151     this->dir1 = axis1;
152     this->dir2 = axis2;
154 ***/
156 NR::Point Box3DFace::operator[](unsigned int i)
158     return *corners[i % 4];
163 /**
164  * Append the curve's path as a child to the given 3D box (since SP3DBox
165  * is derived from SPGroup, so we can append children to its svg representation)
166  */
167 void Box3DFace::hook_path_to_3dbox(SPPath * existing_path)
169     if (this->path) {
170         //g_print ("Path already exists. Returning ...\n");
171         return;
172     }
174     if (existing_path != NULL) {
175         // no need to create a new path
176         this->path = existing_path;
177         return;
178     }
180     /* create new path for face */
181     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(SP_OBJECT(parent_box3d)));
183     Inkscape::XML::Node *repr_face = xml_doc->createElement("svg:path");
184     repr_face->setAttribute("inkscape:box3dface", this->axes_string());
185     this->path = SP_PATH(SP_OBJECT(parent_box3d)->appendChildRepr(repr_face));
186     Inkscape::GC::release(repr_face);
188     /* set the correct style */
189     this->set_style (repr_face);
192 void Box3DFace::set_style(Inkscape::XML::Node *repr_face, bool extruded)
194     if (repr_face == NULL) {
195         repr_face = SP_OBJECT_REPR (this->path);
196     }
198     if (!extruded && !strcmp (axes_string (), "XYrear")) {
199         // to avoid "flashing" during the initial dragging process, we make the rear face invisible in this case
200         repr_face->setAttribute("style", "fill:none");
201         return;
202     }
204     gchar *descr = g_strconcat ("desktop.", axes_string (), NULL);
205     const gchar * cur_style = prefs_get_string_attribute(descr, "style");
206     g_free (descr);    
207     
208     SPDesktop *desktop = inkscape_active_desktop();
209     bool use_current = prefs_get_int_attribute("tools.shapes.3dbox", "usecurrent", 0);
210     if (use_current && cur_style !=NULL) {
211         /* use last used style */
212         repr_face->setAttribute("style", cur_style);
213     } else {
214         /* use default style */
215         GString *pstring = g_string_new("");
216         g_string_printf (pstring, "tools.shapes.3dbox.%s", axes_string());
217         sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
218     }
221 /**
222  * Write the path's "d" attribute to the SVG representation.
223  */
224 void Box3DFace::set_path_repr()
226     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (this->parent_box3d)));
227     SPCurve * curve = sp_curve_new();
228     sp_curve_moveto (curve, ((*corners[0]) * i2d)[NR::X], ((*corners[0]) * i2d)[NR::Y]);
229     sp_curve_lineto (curve, ((*corners[1]) * i2d)[NR::X], ((*corners[1]) * i2d)[NR::Y]);
230     sp_curve_lineto (curve, ((*corners[2]) * i2d)[NR::X], ((*corners[2]) * i2d)[NR::Y]);
231     sp_curve_lineto (curve, ((*corners[3]) * i2d)[NR::X], ((*corners[3]) * i2d)[NR::Y]);
232     sp_curve_closepath (curve);
233     SP_OBJECT(this->path)->repr->setAttribute("d", sp_svg_write_path (SP_CURVE_BPATH(curve)));
236 void Box3DFace::set_curve()
238     if (this->path == NULL) {
239         return;
240     }
241     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (this->parent_box3d)));
242     SPCurve *curve = sp_curve_new();
243     sp_curve_moveto(curve, (*corners[0]) * i2d);
244     sp_curve_lineto(curve, (*corners[1]) * i2d);
245     sp_curve_lineto(curve, (*corners[2]) * i2d);
246     sp_curve_lineto(curve, (*corners[3]) * i2d);
247     sp_curve_closepath(curve);
248     sp_shape_set_curve(SP_SHAPE(this->path), curve, true);
249     sp_curve_unref(curve);
252 gchar * Box3DFace::axes_string()
254     GString *pstring = g_string_new("");
255     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (dir1 ^ dir2)));
256     switch ((Box3D::Axis) (dir1 ^ dir2)) {
257         case Box3D::XY:
258             g_string_append_printf (pstring, (front_or_rear == Box3D::FRONT) ? "front" : "rear");
259             break;
260         case Box3D::XZ:
261             g_string_append_printf (pstring, (front_or_rear == Box3D::FRONT) ? "top" : "bottom");
262             break;
263         case Box3D::YZ:
264             g_string_append_printf (pstring, (front_or_rear == Box3D::FRONT) ? "right" : "left");
265             break;
266         default:
267             break;
268     }
269     return pstring->str;
272 gint Box3DFace::descr_to_id (gchar const *descr)
274     if (!strcmp (descr, "XYrear")) { return 5; }
275     if (!strcmp (descr, "XYfront")) { return 4; }
276     if (!strcmp (descr, "XZbottom")) { return 3; }
277     if (!strcmp (descr, "XZtop")) { return 2; }
278     if (!strcmp (descr, "YZleft")) { return 1; }
279     if (!strcmp (descr, "YZright")) { return 0; }
281     g_warning ("Invalid description of 3D box face.\n");
282     return -1;
285 /*
286   Local Variables:
287   mode:c++
288   c-file-style:"stroustrup"
289   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
290   indent-tabs-mode:nil
291   fill-column:99
292   End:
293 */
294 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :