1 #define __PERSPECTIVE3D_C__
3 /*
4 * Class modelling a 3D perspective
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 "box3d.h"
15 #include "box3d-context.h"
16 #include "perspective-line.h"
17 #include <iostream>
18 #include "perspective3d.h"
19 #include "desktop-handles.h"
21 // can probably be removed later
22 #include "inkscape.h"
24 namespace Box3D {
26 gint Perspective3D::counter = 0;
28 /**
29 * Computes the intersection of the two perspective lines from pt1 and pt2 to the respective
30 * vanishing points in the given directions.
31 */
32 // FIXME: This has been moved to a virtual method inside PerspectiveLine; can probably be purged
33 NR::Point
34 perspective_intersection (NR::Point pt1, Box3D::Axis dir1, NR::Point pt2, Box3D::Axis dir2, Perspective3D *persp)
35 {
36 VanishingPoint const *vp1 = persp->get_vanishing_point(dir1);
37 VanishingPoint const *vp2 = persp->get_vanishing_point(dir2);
38 NR::Maybe<NR::Point> meet = Line(pt1, *vp1).intersect(Line(pt2, *vp2));
39 // FIXME: How to handle parallel lines (also depends on the type of the VPs)?
40 if (!meet) { meet = NR::Point (0.0, 0.0); }
41 return *meet;
42 }
44 /**
45 * Find the point on the perspective line from line_pt to the
46 * vanishing point in direction dir that is closest to ext_pt.
47 */
48 NR::Point
49 perspective_line_snap (NR::Point line_pt, Box3D::Axis dir, NR::Point ext_pt, Perspective3D *persp)
50 {
51 return PerspectiveLine(line_pt, dir, persp).closest_to(ext_pt);
52 }
54 Perspective3D::Perspective3D (VanishingPoint const &pt_x, VanishingPoint const &pt_y, VanishingPoint const &pt_z, SPDocument *doc)
55 : boxes (NULL),
56 document (doc)
57 {
58 vp_x = new VanishingPoint (pt_x);
59 vp_y = new VanishingPoint (pt_y);
60 vp_z = new VanishingPoint (pt_z);
62 my_counter = Perspective3D::counter++;
64 if (document == NULL) {
65 g_warning ("What to do now?\n");
66 }
67 }
69 Perspective3D::Perspective3D (Perspective3D &other)
70 : boxes (NULL) // Should we add an option to copy the list of boxes?
71 {
72 vp_x = new VanishingPoint (*other.vp_x);
73 vp_y = new VanishingPoint (*other.vp_y);
74 vp_z = new VanishingPoint (*other.vp_z);
76 my_counter = Perspective3D::counter++;
78 document = other.document;
79 }
81 Perspective3D::~Perspective3D ()
82 {
83 if (document) {
84 document->remove_perspective (this);
85 } else {
86 g_warning ("No document found!\n");
87 }
89 // Remove the VPs from their draggers
90 SPEventContext *ec = inkscape_active_event_context();
91 if (SP_IS_3DBOX_CONTEXT (ec)) {
92 SP3DBoxContext *bc = SP_3DBOX_CONTEXT (ec);
93 // we need to check if there are any draggers because the selection
94 // is temporarily empty during duplication of boxes, e.g.
95 if (bc->_vpdrag->draggers != NULL) {
96 /***
97 g_assert (bc->_vpdrag->getDraggerFor (*vp_x) != NULL);
98 g_assert (bc->_vpdrag->getDraggerFor (*vp_y) != NULL);
99 g_assert (bc->_vpdrag->getDraggerFor (*vp_z) != NULL);
100 bc->_vpdrag->getDraggerFor (*vp_x)->removeVP (vp_x);
101 bc->_vpdrag->getDraggerFor (*vp_y)->removeVP (vp_y);
102 bc->_vpdrag->getDraggerFor (*vp_z)->removeVP (vp_z);
103 ***/
104 // TODO: the temporary perspective created when building boxes is not linked to any dragger, hence
105 // we need to do the following checks. Maybe it would be better to not create a temporary
106 // perspective at all but simply compare the VPs manually in sp_3dbox_build.
107 VPDragger * dragger;
108 dragger = bc->_vpdrag->getDraggerFor (*vp_x);
109 if (dragger)
110 dragger->removeVP (vp_x);
111 dragger = bc->_vpdrag->getDraggerFor (*vp_y);
112 if (dragger)
113 dragger->removeVP (vp_y);
114 dragger = bc->_vpdrag->getDraggerFor (*vp_z);
115 if (dragger)
116 dragger->removeVP (vp_z);
117 }
118 }
120 delete vp_x;
121 delete vp_y;
122 delete vp_z;
124 g_slist_free (boxes);
125 }
127 bool
128 Perspective3D::operator==(Perspective3D const &other) const
129 {
130 // Two perspectives are equal iff their vanishing points coincide and have identical states
131 return (*vp_x == *other.vp_x && *vp_y == *other.vp_y && *vp_z == *other.vp_z);
132 }
134 bool
135 Perspective3D::has_vanishing_point (VanishingPoint *vp)
136 {
137 return (vp == vp_x || vp == vp_y || vp == vp_z);
138 }
140 VanishingPoint *
141 Perspective3D::get_vanishing_point (Box3D::Axis const dir)
142 {
143 switch (dir) {
144 case X:
145 return vp_x;
146 break;
147 case Y:
148 return vp_y;
149 break;
150 case Z:
151 return vp_z;
152 break;
153 case NONE:
154 g_warning ("Axis direction must be specified. As a workaround we return the VP in X direction.\n");
155 return vp_x;
156 break;
157 default:
158 g_warning ("Single axis direction needed to determine corresponding vanishing point.\n");
159 return get_vanishing_point (extract_first_axis_direction(dir));
160 break;
161 }
162 }
164 void
165 Perspective3D::set_vanishing_point (Box3D::Axis const dir, VanishingPoint const &pt)
166 {
167 switch (dir) {
168 case X:
169 (*vp_x) = pt;
170 break;
171 case Y:
172 (*vp_y) = pt;
173 break;
174 case Z:
175 (*vp_z) = pt;
176 break;
177 default:
178 // no vanishing point to set
179 break;
180 }
181 }
183 Axis
184 Perspective3D::get_axis_of_VP (VanishingPoint *vp)
185 {
186 if (vp == vp_x) return X;
187 if (vp == vp_y) return Y;
188 if (vp == vp_z) return Z;
190 g_warning ("Vanishing point not present in the perspective.\n");
191 return NONE;
192 }
194 void
195 Perspective3D::set_vanishing_point (Box3D::Axis const dir, gdouble pt_x, gdouble pt_y, gdouble dir_x, gdouble dir_y, VPState st)
196 {
197 VanishingPoint *vp;
198 switch (dir) {
199 case X:
200 vp = vp_x;
201 break;
202 case Y:
203 vp = vp_y;
204 break;
205 case Z:
206 vp = vp_z;
207 break;
208 default:
209 // no vanishing point to set
210 return;
211 }
213 vp->set_pos (pt_x, pt_y);
214 vp->v_dir = NR::Point (dir_x, dir_y);
215 vp->state = st;
216 }
218 void
219 Perspective3D::add_box (SP3DBox *box)
220 {
221 if (g_slist_find (this->boxes, box) != NULL) {
222 // Don't add the same box twice
223 g_warning ("Box already uses the current perspective. We don't add it again.\n");
224 return;
225 }
226 this->boxes = g_slist_append (this->boxes, box);
227 }
229 void
230 Perspective3D::remove_box (const SP3DBox *box)
231 {
232 if (!g_slist_find (this->boxes, box)) {
233 g_warning ("Could not find box that is to be removed in the current perspective.\n");
234 }
235 this->boxes = g_slist_remove (this->boxes, box);
236 }
238 bool
239 Perspective3D::has_box (const SP3DBox *box) const
240 {
241 return (g_slist_find (this->boxes, box) != NULL);
242 }
244 bool
245 Perspective3D::all_boxes_occur_in_list (GSList *boxes_to_do)
246 {
247 for (GSList *i = boxes; i != NULL; i = i->next) {
248 if (!g_slist_find (boxes_to_do, i->data)) {
249 return false;
250 }
251 }
252 return true;
253 }
255 GSList *
256 Perspective3D::boxes_occurring_in_list (GSList * list_of_boxes)
257 {
258 GSList * result = NULL;
259 for (GSList *i = list_of_boxes; i != NULL; i = i->next) {
260 if (this->has_box (SP_3DBOX (i->data))) {
261 result = g_slist_prepend (result, i->data);
262 }
263 }
264 // we reverse so as to retain the same order as in list_of_boxes
265 return g_slist_reverse (result);
266 }
268 /**
269 * Update the shape of a box after a handle was dragged or a VP was changed, according to the stored ratios.
270 */
271 void
272 Perspective3D::reshape_boxes (Box3D::Axis axes)
273 {
274 // TODO: Leave the "correct" corner fixed according to which face is supposed to be on front.
275 NR::Point new_pt;
276 VanishingPoint *vp;
277 for (const GSList *i = this->boxes; i != NULL; i = i->next) {
278 SP3DBox *box = SP_3DBOX (i->data);
279 if (axes & Box3D::X) {
280 vp = this->get_vanishing_point (Box3D::X);
281 if (vp->is_finite()) {
282 new_pt = vp->get_pos() + box->ratio_x * (box->corners[3] - vp->get_pos());
283 sp_3dbox_move_corner_in_XY_plane (box, 2, new_pt);
284 }
285 }
286 if (axes & Box3D::Y) {
287 vp = this->get_vanishing_point (Box3D::Y);
288 if (vp->is_finite()) {
289 new_pt = vp->get_pos() + box->ratio_y * (box->corners[0] - vp->get_pos());
290 sp_3dbox_move_corner_in_XY_plane (box, 2, new_pt);
291 }
292 }
293 if (axes & Box3D::Z) {
294 vp = this->get_vanishing_point (Box3D::Z);
295 if (vp->is_finite()) {
296 new_pt = vp->get_pos() + box->ratio_z * (box->corners[0] - vp->get_pos());
297 sp_3dbox_move_corner_in_Z_direction (box, 4, new_pt);
298 }
299 }
301 sp_3dbox_set_shape (box, true);
302 }
303 }
305 void
306 Perspective3D::toggle_boxes (Box3D::Axis axis)
307 {
308 get_vanishing_point (axis)->toggle_parallel();
309 for (GSList *i = this->boxes; i != NULL; i = i->next) {
310 sp_3dbox_reshape_after_VP_toggling (SP_3DBOX (i->data), axis);
311 }
312 update_box_reprs();
314 SP3DBoxContext *bc = SP_3DBOX_CONTEXT (inkscape_active_event_context());
315 bc->_vpdrag->updateDraggers ();
316 }
318 void
319 Perspective3D::update_box_reprs ()
320 {
321 for (GSList *i = this->boxes; i != NULL; i = i->next) {
322 SP_OBJECT(SP_3DBOX (i->data))->updateRepr(SP_OBJECT_WRITE_EXT);
323 }
324 }
326 void
327 Perspective3D::update_z_orders ()
328 {
329 for (GSList *i = this->boxes; i != NULL; i = i->next) {
330 sp_3dbox_set_z_orders_later_on (SP_3DBOX (i->data));
331 }
332 }
334 // swallow the list of boxes from the other perspective and delete it
335 void
336 Perspective3D::absorb (Perspective3D *other)
337 {
338 g_return_if_fail (*this == *other);
340 // FIXME: Is copying necessary? Is other->boxes invalidated when other is deleted below?
341 this->boxes = g_slist_concat (this->boxes, g_slist_copy (other->boxes));
343 // Should we delete the other perspective here or at the place from where absorb() is called?
344 delete other;
345 other = NULL;
346 }
348 // FIXME: We get compiler errors when we try to move the code from sp_3dbox_get_perspective_string to this function
349 /***
350 gchar *
351 Perspective3D::svg_string ()
352 {
353 }
354 ***/
356 void
357 Perspective3D::print_debugging_info ()
358 {
359 g_print ("====================================================\n");
360 for (GSList *i = sp_desktop_document (inkscape_active_desktop())->perspectives; i != NULL; i = i->next) {
361 Perspective3D *persp = (Perspective3D *) i->data;
362 g_print ("Perspective %d:\n", persp->my_counter);
364 VanishingPoint * vp = persp->get_vanishing_point(Box3D::X);
365 g_print (" VP X: (%f,%f) ", (*vp)[NR::X], (*vp)[NR::Y]);
366 g_print ((vp->is_finite()) ? "(finite)\n" : "(infinite)\n");
368 vp = persp->get_vanishing_point(Box3D::Y);
369 g_print (" VP Y: (%f,%f) ", (*vp)[NR::X], (*vp)[NR::Y]);
370 g_print ((vp->is_finite()) ? "(finite)\n" : "(infinite)\n");
372 vp = persp->get_vanishing_point(Box3D::Z);
373 g_print (" VP Z: (%f,%f) ", (*vp)[NR::X], (*vp)[NR::Y]);
374 g_print ((vp->is_finite()) ? "(finite)\n" : "(infinite)\n");
376 g_print ("\nBoxes: ");
377 if (persp->boxes == NULL) {
378 g_print ("none");
379 } else {
380 GSList *j;
381 for (j = persp->boxes; j != NULL; j = j->next) {
382 if (j->next == NULL) break;
383 g_print ("%d, ", SP_3DBOX (j->data)->my_counter);
384 }
385 if (j != NULL) {
386 g_print ("%d", SP_3DBOX (j->data)->my_counter);
387 }
388 }
389 }
390 g_print ("\n====================================================\n");
391 }
393 } // namespace Box3D
395 /*
396 Local Variables:
397 mode:c++
398 c-file-style:"stroustrup"
399 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
400 indent-tabs-mode:nil
401 fill-column:99
402 End:
403 */
404 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :