1 #define __VANISHING_POINT_C__
3 /*
4 * Vanishing point for 3D perspectives
5 *
6 * Authors:
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 * Maximilian Albert <Anhalter42@gmx.de>
10 *
11 * Copyright (C) 2005-2007 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <glibmm/i18n.h>
18 #include "vanishing-point.h"
19 #include "desktop-handles.h"
20 #include "box3d.h"
22 #include "knotholder.h" // FIXME: can we avoid direct access to knotholder_update_knots?
24 namespace Box3D {
26 #define VP_KNOT_COLOR_NORMAL 0xffffff00
27 #define VP_KNOT_COLOR_SELECTED 0x0000ff00
29 #define VP_LINE_COLOR_FILL 0x0000ff7f
30 #define VP_LINE_COLOR_STROKE_X 0xff00007f
31 #define VP_LINE_COLOR_STROKE_Y 0x0000ff7f
32 #define VP_LINE_COLOR_STROKE_Z 0xffff007f
34 // screen pixels between knots when they snap:
35 #define SNAP_DIST 5
37 // absolute distance between gradient points for them to become a single dragger when the drag is created:
38 #define MERGE_DIST 0.1
40 // knot shapes corresponding to GrPointType enum
41 SPKnotShapeType vp_knot_shapes [] = {
42 SP_KNOT_SHAPE_SQUARE, // VP_FINITE
43 SP_KNOT_SHAPE_CIRCLE //VP_INFINITE
44 };
46 // FIXME: We should always require to have both the point (for finite VPs)
47 // and the direction (for infinite VPs) set. Otherwise toggling
48 // shows very unexpected behaviour.
49 // Later on we can maybe infer the infinite direction from the finite point
50 // and a suitable center of the scene. How to go in the other direction?
51 VanishingPoint::VanishingPoint(NR::Point const &pt, NR::Point const &inf_dir, VPState st)
52 : NR::Point (pt), state (st), v_dir (inf_dir) {}
54 VanishingPoint::VanishingPoint(NR::Point const &pt)
55 : NR::Point (pt), state (VP_FINITE), v_dir (0.0, 0.0) {}
57 VanishingPoint::VanishingPoint(NR::Point const &pt, NR::Point const &direction)
58 : NR::Point (pt), state (VP_INFINITE), v_dir (direction) {}
60 VanishingPoint::VanishingPoint(NR::Coord x, NR::Coord y)
61 : NR::Point(x, y), state(VP_FINITE), v_dir(0.0, 0.0) {}
63 VanishingPoint::VanishingPoint(NR::Coord dir_x, NR::Coord dir_y, VPState st)
64 : NR::Point(0.0, 0.0), state(st), v_dir(dir_x, dir_y) {}
66 VanishingPoint::VanishingPoint(NR::Coord x, NR::Coord y, NR::Coord dir_x, NR::Coord dir_y)
67 : NR::Point(x, y), state(VP_INFINITE), v_dir(dir_x, dir_y) {}
69 VanishingPoint::VanishingPoint(VanishingPoint const &rhs) : NR::Point (rhs)
70 {
71 this->state = rhs.state;
72 //this->ref_pt = rhs.ref_pt;
73 this->v_dir = rhs.v_dir;
74 }
76 VanishingPoint::~VanishingPoint () {}
78 bool VanishingPoint::operator== (VanishingPoint const &other)
79 {
80 // Should we compare the parent perspectives, too? Probably not.
81 if ((*this)[NR::X] == other[NR::X] && (*this)[NR::Y] == other[NR::Y]
82 && this->state == other.state && this->v_dir == other.v_dir) {
83 return true;
84 }
85 return false;
86 }
88 bool VanishingPoint::is_finite() const
89 {
90 return this->state == VP_FINITE;
91 }
93 VPState VanishingPoint::toggle_parallel()
94 {
95 if (this->state == VP_FINITE) {
96 this->state = VP_INFINITE;
97 } else {
98 this->state = VP_FINITE;
99 }
101 return this->state;
102 }
104 void VanishingPoint::draw(Box3D::Axis const axis)
105 {
106 switch (axis) {
107 case X:
108 if (state == VP_FINITE)
109 create_canvas_point(*this, 6.0, 0xff000000);
110 else
111 create_canvas_point(*this, 6.0, 0xffffff00);
112 break;
113 case Y:
114 if (state == VP_FINITE)
115 create_canvas_point(*this, 6.0, 0x0000ff00);
116 else
117 create_canvas_point(*this, 6.0, 0xffffff00);
118 break;
119 case Z:
120 if (state == VP_FINITE)
121 create_canvas_point(*this, 6.0, 0x00770000);
122 else
123 create_canvas_point(*this, 6.0, 0xffffff00);
124 break;
125 default:
126 g_assert_not_reached();
127 break;
128 }
129 }
131 static void
132 vp_drag_sel_changed(Inkscape::Selection *selection, gpointer data)
133 {
134 VPDrag *drag = (VPDrag *) data;
135 drag->updateDraggers ();
136 drag->updateLines ();
137 }
139 static void
140 vp_drag_sel_modified (Inkscape::Selection *selection, guint flags, gpointer data)
141 {
142 VPDrag *drag = (VPDrag *) data;
143 /***
144 if (drag->local_change) {
145 drag->local_change = false;
146 } else {
147 drag->updateDraggers ();
148 }
149 ***/
150 drag->updateLines ();
151 }
153 // auxiliary function
154 static GSList *
155 eliminate_remaining_boxes_of_persp_starting_from_list_position (GSList *boxes_to_do, const SP3DBox *start_box, const Perspective3D *persp)
156 {
157 GSList *i = g_slist_find (boxes_to_do, start_box);
158 g_return_val_if_fail (i != NULL, boxes_to_do);
160 SP3DBox *box;
161 GSList *successor;
163 i = i->next;
164 while (i != NULL) {
165 successor = i->next;
166 box = SP_3DBOX (i->data);
167 if (persp->has_box (box)) {
168 boxes_to_do = g_slist_remove (boxes_to_do, box);
169 }
170 i = successor;
171 }
173 return boxes_to_do;
174 }
176 static bool
177 have_VPs_of_same_perspective (VPDragger *dr1, VPDragger *dr2)
178 {
179 Perspective3D *persp;
180 for (GSList *i = dr1->vps; i != NULL; i = i->next) {
181 persp = dr1->parent->document->get_persp_of_VP ((VanishingPoint *) i->data);
182 if (dr2->hasPerspective (persp)) {
183 return true;
184 }
185 }
186 return false;
187 }
189 static void
190 vp_knot_moved_handler (SPKnot *knot, NR::Point const *ppointer, guint state, gpointer data)
191 {
192 VPDragger *dragger = (VPDragger *) data;
193 VPDrag *drag = dragger->parent;
195 NR::Point p = *ppointer;
197 // FIXME: take from prefs
198 double snap_dist = SNAP_DIST / inkscape_active_desktop()->current_zoom();
200 if (!(state & GDK_SHIFT_MASK)) {
201 // without Shift; see if we need to snap to another dragger
202 for (GList *di = dragger->parent->draggers; di != NULL; di = di->next) {
203 VPDragger *d_new = (VPDragger *) di->data;
204 if ((d_new != dragger) && (NR::L2 (d_new->point - p) < snap_dist)) {
205 if (have_VPs_of_same_perspective (dragger, d_new)) {
206 // this would result in degenerate boxes, which we disallow for the time being
207 continue;
208 }
210 // update positions ...
211 for (GSList *j = dragger->vps; j != NULL; j = j->next) {
212 ((VanishingPoint *) j->data)->set_pos (d_new->point);
213 }
214 // ... join lists of VPs ...
215 // FIXME: Do we have to copy the second list (i.e, is it invalidated when dragger is deleted below)?
216 d_new->vps = g_slist_concat (d_new->vps, g_slist_copy (dragger->vps));
218 // ... delete old dragger ...
219 drag->draggers = g_list_remove (drag->draggers, dragger);
220 delete dragger;
221 dragger = NULL;
223 // ... and merge any duplicate perspectives
224 d_new->mergePerspectives();
226 // TODO: Update the new merged dragger
227 //d_new->updateKnotShape ();
228 d_new->updateTip ();
230 d_new->reshapeBoxes (d_new->point, Box3D::XYZ);
231 d_new->updateBoxReprs ();
232 d_new->updateZOrders ();
234 drag->updateLines ();
236 // TODO: Undo machinery; this doesn't work yet because perspectives must be created and
237 // deleted according to changes in the svg representation, not based on any user input
238 // as is currently the case.
240 //sp_document_done (sp_desktop_document (drag->desktop), SP_VERB_CONTEXT_3DBOX,
241 // _("Merge vanishing points"));
243 return;
244 }
245 }
246 }
248 dragger->point = p;
250 dragger->reshapeBoxes (p, Box3D::XYZ);
251 dragger->updateBoxReprs ();
252 dragger->updateZOrders ();
254 drag->updateLines ();
256 //drag->local_change = false;
257 }
259 /***
260 static void
261 vp_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
262 {
263 VPDragger *dragger = (VPDragger *) data;
264 }
265 ***/
267 void
268 vp_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
269 {
270 VPDragger *dragger = (VPDragger *) data;
271 VPDrag *drag = dragger->parent;
273 //sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
275 if ((state & GDK_SHIFT_MASK) && !drag->hasEmptySelection()) { // FIXME: Is the second check necessary?
277 if (drag->allBoxesAreSelected (dragger)) {
278 // if all of the boxes linked to dragger are selected, we don't need to split it
279 return;
280 }
282 // we are Shift-dragging; unsnap if we carry more than one VP
284 // FIXME: Should we distinguish between the following cases:
285 // 1) there are several VPs in a dragger
286 // 2) there is only a single VP but several boxes linked to it
287 // ?
288 // Or should we simply unlink all selected boxes? Currently we do the latter.
289 if (dragger->numberOfBoxes() > 1) {
290 // create a new dragger
291 VPDragger *dr_new = new VPDragger (drag, dragger->point, NULL);
292 drag->draggers = g_list_prepend (drag->draggers, dr_new);
294 // move all the VPs from dragger to dr_new
295 dr_new->vps = dragger->vps;
296 dragger->vps = NULL;
298 /* now we move all selected boxes back to the current dragger (splitting perspectives
299 if they also have unselected boxes) so that they are further reshaped during dragging */
301 GSList *boxes_to_do = drag->selectedBoxesWithVPinDragger (dr_new);
303 for (GSList *i = boxes_to_do; i != NULL; i = i->next) {
304 SP3DBox *box = SP_3DBOX (i->data);
305 Perspective3D *persp = drag->document->get_persp_of_box (box);
306 VanishingPoint *vp = dr_new->getVPofPerspective (persp);
307 if (vp == NULL) {
308 g_warning ("VP is NULL. We should be okay, though.\n");
309 }
310 if (persp->all_boxes_occur_in_list (boxes_to_do)) {
311 // if all boxes of persp are selected, we can simply move the VP from dr_new back to dragger
312 dr_new->removeVP (vp);
313 dragger->addVP (vp);
315 // some cleaning up for efficiency
316 boxes_to_do = eliminate_remaining_boxes_of_persp_starting_from_list_position (boxes_to_do, box, persp);
317 } else {
318 /* otherwise the unselected boxes need to stay linked to dr_new; thus we
319 create a new perspective and link the VPs to the correct draggers */
320 Perspective3D *persp_new = new Perspective3D (*persp);
321 drag->document->add_perspective (persp_new);
323 Axis vp_axis = persp->get_axis_of_VP (vp);
324 dragger->addVP (persp_new->get_vanishing_point (vp_axis));
325 std::pair<Axis, Axis> rem_axes = get_remaining_axes (vp_axis);
326 drag->addDragger (persp->get_vanishing_point (rem_axes.first));
327 drag->addDragger (persp->get_vanishing_point (rem_axes.second));
329 // now we move the selected boxes from persp to persp_new
330 GSList * selected_boxes_of_perspective = persp->boxes_occurring_in_list (boxes_to_do);
331 for (GSList *j = selected_boxes_of_perspective; j != NULL; j = j->next) {
332 persp->remove_box (SP_3DBOX (j->data));
333 persp_new->add_box (SP_3DBOX (j->data));
334 }
336 // cleaning up
337 boxes_to_do = eliminate_remaining_boxes_of_persp_starting_from_list_position (boxes_to_do, box, persp);
338 }
339 }
341 // TODO: Something is still wrong with updating the boxes' representations after snapping
342 //dr_new->updateBoxReprs ();
344 dragger->updateTip();
345 dr_new->updateTip();
346 }
347 }
348 }
350 static void
351 vp_knot_ungrabbed_handler (SPKnot *knot, guint state, gpointer data)
352 {
353 VPDragger *dragger = (VPDragger *) data;
355 //sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
357 dragger->point_original = dragger->point = knot->pos;
359 /***
360 VanishingPoint *vp;
361 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
362 vp = (VanishingPoint *) i->data;
363 vp->set_pos (knot->pos);
364 }
365 ***/
367 dragger->parent->updateDraggers ();
368 dragger->updateBoxReprs ();
370 // TODO: Update box's paths and svg representation
372 // TODO: Undo machinery!!
373 }
375 VPDragger::VPDragger(VPDrag *parent, NR::Point p, VanishingPoint *vp)
376 {
377 this->vps = NULL;
379 this->parent = parent;
381 this->point = p;
382 this->point_original = p;
384 if (vp->is_finite()) {
385 // create the knot
386 this->knot = sp_knot_new (inkscape_active_desktop(), NULL);
387 this->knot->setMode(SP_KNOT_MODE_XOR);
388 this->knot->setFill(VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL);
389 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
390 sp_knot_update_ctrl(this->knot);
392 // move knot to the given point
393 sp_knot_set_position (this->knot, &this->point, SP_KNOT_STATE_NORMAL);
394 sp_knot_show (this->knot);
396 // connect knot's signals
397 g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (vp_knot_moved_handler), this);
398 /***
399 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (vp_knot_clicked_handler), this);
400 ***/
401 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (vp_knot_grabbed_handler), this);
402 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (vp_knot_ungrabbed_handler), this);
403 /***
404 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (vp_knot_doubleclicked_handler), this);
405 ***/
407 // add the initial VP (which may be NULL!)
408 this->addVP (vp);
409 //updateKnotShape();
410 }
411 }
413 VPDragger::~VPDragger()
414 {
415 // unselect if it was selected
416 //this->parent->setDeselected(this);
418 // disconnect signals
419 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_moved_handler), this);
420 /***
421 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_clicked_handler), this);
422 ***/
423 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_grabbed_handler), this);
424 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_ungrabbed_handler), this);
425 /***
426 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_doubleclicked_handler), this);
427 ***/
429 /* unref should call destroy */
430 g_object_unref (G_OBJECT (this->knot));
432 g_slist_free (this->vps);
433 this->vps = NULL;
434 }
436 /**
437 Updates the statusbar tip of the dragger knot, based on its draggables
438 */
439 void
440 VPDragger::updateTip ()
441 {
442 if (this->knot && this->knot->tip) {
443 g_free (this->knot->tip);
444 this->knot->tip = NULL;
445 }
447 guint num = this->numberOfBoxes();
448 if (g_slist_length (this->vps) == 1) {
449 VanishingPoint *vp = (VanishingPoint *) this->vps->data;
450 switch (vp->state) {
451 case VP_FINITE:
452 this->knot->tip = g_strdup_printf (ngettext("<b>Finite</b> vanishing point shared by <b>%d</b> box",
453 "<b>Finite</b> vanishing point shared by <b>%d</b> boxes; drag with <b>Shift</b> to separate selected box(es)",
454 num),
455 num);
456 break;
457 case VP_INFINITE:
458 // This won't make sense any more when infinite VPs are not shown on the canvas,
459 // but currently we update the status message anyway
460 this->knot->tip = g_strdup_printf (ngettext("<b>Infinite</b> vanishing point shared by <b>%d</b> box",
461 "<b>Infinite</b> vanishing point shared by <b>%d</b> boxes; drag with <b>Shift</b> to separate selected box(es)",
462 num),
463 num);
464 break;
465 }
466 } else {
467 int length = g_slist_length (this->vps);
468 char *desc1 = g_strdup_printf ("Collection of <b>%d</b> vanishing points ", length);
469 char *desc2 = g_strdup_printf (ngettext("shared by <b>%d</b> box; drag with <b>Shift</b> to separate selected box(es)",
470 "shared by <b>%d</b> boxes; drag with <b>Shift</b> to separate selected box(es)",
471 num),
472 num);
473 this->knot->tip = g_strconcat(desc1, desc2, NULL);
474 g_free (desc1);
475 g_free (desc2);
476 }
477 }
479 /**
480 * Adds a vanishing point to the dragger (also updates the position)
481 */
482 void
483 VPDragger::addVP (VanishingPoint *vp)
484 {
485 if (vp == NULL) {
486 return;
487 }
488 if (!vp->is_finite() || g_slist_find (this->vps, vp)) {
489 // don't add infinite VPs, and don't add the same VP twice
490 return;
491 }
493 vp->set_pos (this->point);
494 this->vps = g_slist_prepend (this->vps, vp);
496 this->updateTip();
497 }
499 void
500 VPDragger::removeVP (VanishingPoint *vp)
501 {
502 if (vp == NULL) {
503 g_print ("NULL vanishing point will not be removed.\n");
504 return;
505 }
506 g_assert (this->vps != NULL);
507 this->vps = g_slist_remove (this->vps, vp);
509 this->updateTip();
510 }
512 // returns the VP contained in the dragger that belongs to persp
513 VanishingPoint *
514 VPDragger::getVPofPerspective (Perspective3D *persp)
515 {
516 for (GSList *i = vps; i != NULL; i = i->next) {
517 if (persp->has_vanishing_point ((VanishingPoint *) i->data)) {
518 return ((VanishingPoint *) i->data);
519 }
520 }
521 return NULL;
522 }
524 bool
525 VPDragger::hasBox(const SP3DBox *box)
526 {
527 for (GSList *i = this->vps; i != NULL; i = i->next) {
528 if (parent->document->get_persp_of_VP ((VanishingPoint *) i->data)->has_box (box)) return true;
529 }
530 return false;
531 }
533 guint
534 VPDragger::numberOfBoxes ()
535 {
536 guint num = 0;
537 for (GSList *i = this->vps; i != NULL; i = i->next) {
538 num += parent->document->get_persp_of_VP ((VanishingPoint *) i->data)->number_of_boxes ();
539 }
540 return num;
541 }
543 bool
544 VPDragger::hasPerspective (const Perspective3D *persp)
545 {
546 for (GSList *i = this->vps; i != NULL; i = i->next) {
547 if (*persp == *parent->document->get_persp_of_VP ((VanishingPoint *) i->data)) {
548 return true;
549 }
550 }
551 return false;
552 }
554 void
555 VPDragger::mergePerspectives ()
556 {
557 Perspective3D *persp1, *persp2;
558 GSList * successor = NULL;
559 for (GSList *i = this->vps; i != NULL; i = i->next) {
560 persp1 = parent->document->get_persp_of_VP ((VanishingPoint *) i->data);
561 for (GSList *j = i->next; j != NULL; j = successor) {
562 // if the perspective is deleted, the VP is invalidated, too, so we must store its successor beforehand
563 successor = j->next;
564 persp2 = parent->document->get_persp_of_VP ((VanishingPoint *) j->data);
565 if (*persp1 == *persp2) {
566 persp1->absorb (persp2); // persp2 is deleted; hopefully this doesn't screw up the list of vanishing points and thus the loops
567 }
568 }
569 }
570 }
572 void
573 VPDragger::reshapeBoxes (NR::Point const &p, Box3D::Axis axes)
574 {
575 Perspective3D *persp;
576 for (GSList const* i = this->vps; i != NULL; i = i->next) {
577 VanishingPoint *vp = (VanishingPoint *) i->data;
578 // TODO: We can extract the VP directly from the box's perspective. Is that vanishing point identical to 'vp'?
579 // Or is there duplicated information? If so, remove it and simplify the whole construction!
580 vp->set_pos(p);
581 persp = parent->document->get_persp_of_VP (vp);
582 Box3D::Axis axis = persp->get_axis_of_VP (vp);
583 parent->document->get_persp_of_VP (vp)->reshape_boxes (axis); // FIXME: we should only update the direction of the VP
584 }
585 parent->updateBoxHandles();
586 }
588 void
589 VPDragger::updateBoxReprs ()
590 {
591 for (GSList *i = this->vps; i != NULL; i = i->next) {
592 parent->document->get_persp_of_VP ((VanishingPoint *) i->data)->update_box_reprs ();
593 }
594 }
596 void
597 VPDragger::updateZOrders ()
598 {
599 for (GSList *i = this->vps; i != NULL; i = i->next) {
600 parent->document->get_persp_of_VP ((VanishingPoint *) i->data)->update_z_orders ();
601 }
602 }
604 VPDrag::VPDrag (SPDocument *document)
605 {
606 this->document = document;
607 this->selection = sp_desktop_selection(inkscape_active_desktop());
609 this->draggers = NULL;
610 this->lines = NULL;
611 this->show_lines = true;
612 this->front_or_rear_lines = 0x1;
614 //this->selected = NULL;
615 this->local_change = false;
617 this->sel_changed_connection = this->selection->connectChanged(
618 sigc::bind (
619 sigc::ptr_fun(&vp_drag_sel_changed),
620 (gpointer)this )
622 );
623 this->sel_modified_connection = this->selection->connectModified(
624 sigc::bind(
625 sigc::ptr_fun(&vp_drag_sel_modified),
626 (gpointer)this )
627 );
629 this->updateDraggers ();
630 this->updateLines ();
631 }
633 VPDrag::~VPDrag()
634 {
635 this->sel_changed_connection.disconnect();
636 this->sel_modified_connection.disconnect();
638 for (GList *l = this->draggers; l != NULL; l = l->next) {
639 delete ((VPDragger *) l->data);
640 }
641 g_list_free (this->draggers);
642 this->draggers = NULL;
644 for (GSList const *i = this->lines; i != NULL; i = i->next) {
645 gtk_object_destroy( GTK_OBJECT (i->data));
646 }
647 g_slist_free (this->lines);
648 this->lines = NULL;
649 }
651 /**
652 * Select the dragger that has the given VP.
653 */
654 VPDragger *
655 VPDrag::getDraggerFor (VanishingPoint const &vp)
656 {
657 for (GList const* i = this->draggers; i != NULL; i = i->next) {
658 VPDragger *dragger = (VPDragger *) i->data;
659 for (GSList const* j = dragger->vps; j != NULL; j = j->next) {
660 VanishingPoint *vp2 = (VanishingPoint *) j->data;
661 g_assert (vp2 != NULL);
663 // TODO: Should we compare the pointers or the VPs themselves!?!?!?!
664 //if ((*vp2) == vp) {
665 if (vp2 == &vp) {
666 return (dragger);
667 }
668 }
669 }
670 return NULL;
671 }
673 /**
674 * Regenerates the draggers list from the current selection; is called when selection is changed or modified
675 */
676 void
677 VPDrag::updateDraggers ()
678 {
679 /***
680 while (selected) {
681 selected = g_list_remove(selected, selected->data);
682 }
683 ***/
684 // delete old draggers
685 for (GList const* i = this->draggers; i != NULL; i = i->next) {
686 delete ((VPDragger *) i->data);
687 }
688 g_list_free (this->draggers);
689 this->draggers = NULL;
691 g_return_if_fail (this->selection != NULL);
693 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
694 SPItem *item = SP_ITEM(i->data);
695 //SPStyle *style = SP_OBJECT_STYLE (item);
697 if (!SP_IS_3DBOX (item)) continue;
698 SP3DBox *box = SP_3DBOX (item);
700 Box3D::Perspective3D *persp = document->get_persp_of_box (box);
701 addDragger (persp->get_vanishing_point(Box3D::X));
702 addDragger (persp->get_vanishing_point(Box3D::Y));
703 addDragger (persp->get_vanishing_point(Box3D::Z));
704 }
705 }
707 /**
708 Regenerates the lines list from the current selection; is called on each move
709 of a dragger, so that lines are always in sync with the actual perspective
710 */
711 void
712 VPDrag::updateLines ()
713 {
714 // delete old lines
715 for (GSList const *i = this->lines; i != NULL; i = i->next) {
716 gtk_object_destroy( GTK_OBJECT (i->data));
717 }
718 g_slist_free (this->lines);
719 this->lines = NULL;
721 // do nothing if perspective lines are currently disabled
722 if (this->show_lines == 0) return;
724 g_return_if_fail (this->selection != NULL);
726 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
727 if (!SP_IS_3DBOX(i->data)) continue;
728 SP3DBox *box = SP_3DBOX (i->data);
730 this->drawLinesForFace (box, Box3D::X);
731 this->drawLinesForFace (box, Box3D::Y);
732 this->drawLinesForFace (box, Box3D::Z);
733 }
734 }
736 void
737 VPDrag::updateBoxHandles ()
738 {
739 // FIXME: Is there a way to update the knots without accessing the
740 // statically linked function knotholder_update_knots?
742 GSList *sel = (GSList *) selection->itemList();
743 if (g_slist_length (sel) > 1) {
744 // Currently we only show handles if a single box is selected
745 return;
746 }
748 if (!SP_IS_3DBOX (sel->data))
749 return;
751 SPEventContext *ec = inkscape_active_event_context();
752 g_assert (ec != NULL);
753 if (ec->shape_knot_holder != NULL) {
754 knotholder_update_knots(ec->shape_knot_holder, (SPItem *) sel->data);
755 }
756 }
758 /**
759 * Depending on the value of all_lines, draw the front and/or rear perspective lines starting from the given corners.
760 */
761 void
762 VPDrag::drawLinesForFace (const SP3DBox *box, Box3D::Axis axis) //, guint corner1, guint corner2, guint corner3, guint corner4)
763 {
764 guint color;
765 switch (axis) {
766 // TODO: Make color selectable by user
767 case Box3D::X: color = VP_LINE_COLOR_STROKE_X; break;
768 case Box3D::Y: color = VP_LINE_COLOR_STROKE_Y; break;
769 case Box3D::Z: color = VP_LINE_COLOR_STROKE_Z; break;
770 default: g_assert_not_reached();
771 }
773 NR::Point corner1, corner2, corner3, corner4;
774 sp_3dbox_corners_for_perspective_lines (box, axis, corner1, corner2, corner3, corner4);
776 VanishingPoint *vp = document->get_persp_of_box (box)->get_vanishing_point (axis);
777 if (vp->is_finite()) {
778 // draw perspective lines for finite VPs
779 NR::Point pt = vp->get_pos();
780 if (this->front_or_rear_lines & 0x1) {
781 // draw 'front' perspective lines
782 this->addLine (corner1, pt, color);
783 this->addLine (corner2, pt, color);
784 }
785 if (this->front_or_rear_lines & 0x2) {
786 // draw 'rear' perspective lines
787 this->addLine (corner3, pt, color);
788 this->addLine (corner4, pt, color);
789 }
790 } else {
791 // draw perspective lines for infinite VPs
792 NR::Maybe<NR::Point> pt1, pt2, pt3, pt4;
793 Box3D::Perspective3D *persp = this->document->get_persp_of_box (box);
794 SPDesktop *desktop = inkscape_active_desktop (); // FIXME: Store the desktop in VPDrag
795 Box3D::PerspectiveLine pl (corner1, axis, persp);
796 pt1 = pl.intersection_with_viewbox(desktop);
798 pl = Box3D::PerspectiveLine (corner2, axis, persp);
799 pt2 = pl.intersection_with_viewbox(desktop);
801 pl = Box3D::PerspectiveLine (corner3, axis, persp);
802 pt3 = pl.intersection_with_viewbox(desktop);
804 pl = Box3D::PerspectiveLine (corner4, axis, persp);
805 pt4 = pl.intersection_with_viewbox(desktop);
807 if (!pt1 || !pt2 || !pt3 || !pt4) {
808 // some perspective lines s are outside the canvas; currently we don't draw any of them
809 return;
810 }
811 if (this->front_or_rear_lines & 0x1) {
812 // draw 'front' perspective lines
813 this->addLine (corner1, *pt1, color);
814 this->addLine (corner2, *pt2, color);
815 }
816 if (this->front_or_rear_lines & 0x2) {
817 // draw 'rear' perspective lines
818 this->addLine (corner3, *pt3, color);
819 this->addLine (corner4, *pt4, color);
820 }
821 }
823 }
825 /**
826 * Returns true if all boxes that are linked to a VP in the dragger are selected
827 */
828 bool
829 VPDrag::allBoxesAreSelected (VPDragger *dragger) {
830 GSList *selected_boxes = (GSList *) dragger->parent->selection->itemList();
831 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
832 if (!document->get_persp_of_VP ((VanishingPoint *) i->data)->all_boxes_occur_in_list (selected_boxes)) {
833 return false;
834 }
835 }
836 return true;
837 }
839 GSList *
840 VPDrag::selectedBoxesWithVPinDragger (VPDragger *dragger)
841 {
842 GSList *sel_boxes = g_slist_copy ((GSList *) dragger->parent->selection->itemList());
843 for (GSList const *i = sel_boxes; i != NULL; i = i->next) {
844 SP3DBox *box = SP_3DBOX (i->data);
845 if (!dragger->hasBox (box)) {
846 sel_boxes = g_slist_remove (sel_boxes, box);
847 }
848 }
849 return sel_boxes;
850 }
853 /**
854 * If there already exists a dragger within MERGE_DIST of p, add the VP to it;
855 * otherwise create new dragger and add it to draggers list
856 */
857 void
858 VPDrag::addDragger (VanishingPoint *vp)
859 {
860 if (vp == NULL) {
861 g_print ("Warning: The VP in addDragger is already NULL. Aborting.\n)");
862 g_assert (vp != NULL);
863 }
864 if (!vp->is_finite()) {
865 // don't create draggers for infinite vanishing points
866 return;
867 }
868 NR::Point p = vp->get_pos();
870 for (GList *i = this->draggers; i != NULL; i = i->next) {
871 VPDragger *dragger = (VPDragger *) i->data;
872 if (NR::L2 (dragger->point - p) < MERGE_DIST) {
873 // distance is small, merge this draggable into dragger, no need to create new dragger
874 dragger->addVP (vp);
875 //dragger->updateKnotShape();
876 return;
877 }
878 }
880 VPDragger *new_dragger = new VPDragger(this, p, vp);
881 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
882 this->draggers = g_list_append (this->draggers, new_dragger);
883 }
885 /**
886 Create a line from p1 to p2 and add it to the lines list
887 */
888 void
889 VPDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
890 {
891 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(inkscape_active_desktop()), SP_TYPE_CTRLLINE, NULL);
892 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
893 if (rgba != VP_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
894 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
895 sp_canvas_item_show (line);
896 this->lines = g_slist_append (this->lines, line);
897 }
899 } // namespace Box3D
901 /*
902 Local Variables:
903 mode:c++
904 c-file-style:"stroustrup"
905 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
906 indent-tabs-mode:nil
907 fill-column:99
908 End:
909 */
910 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :