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 = 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 / drag->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 ();
233 drag->updateLines ();
235 // TODO: Undo machinery; this doesn't work yet because perspectives must be created and
236 // deleted according to changes in the svg representation, not based on any user input
237 // as is currently the case.
239 //sp_document_done (sp_desktop_document (drag->desktop), SP_VERB_CONTEXT_3DBOX,
240 // _("Merge vanishing points"));
242 return;
243 }
244 }
245 }
247 dragger->point = p;
249 dragger->reshapeBoxes (p, Box3D::XYZ);
250 dragger->updateBoxReprs ();
252 drag->updateLines ();
254 //drag->local_change = false;
255 }
257 /***
258 static void
259 vp_knot_clicked_handler(SPKnot *knot, guint state, gpointer data)
260 {
261 VPDragger *dragger = (VPDragger *) data;
262 }
263 ***/
265 void
266 vp_knot_grabbed_handler (SPKnot *knot, unsigned int state, gpointer data)
267 {
268 VPDragger *dragger = (VPDragger *) data;
269 VPDrag *drag = dragger->parent;
271 //sp_canvas_force_full_redraw_after_interruptions(dragger->parent->desktop->canvas, 5);
273 if ((state & GDK_SHIFT_MASK) && !drag->hasEmptySelection()) { // FIXME: Is the second check necessary?
275 if (drag->allBoxesAreSelected (dragger)) {
276 // if all of the boxes linked to dragger are selected, we don't need to split it
277 return;
278 }
280 // we are Shift-dragging; unsnap if we carry more than one VP
282 // FIXME: Should we distinguish between the following cases:
283 // 1) there are several VPs in a dragger
284 // 2) there is only a single VP but several boxes linked to it
285 // ?
286 // Or should we simply unlink all selected boxes? Currently we do the latter.
287 if (dragger->numberOfBoxes() > 1) {
288 // create a new dragger
289 VPDragger *dr_new = new VPDragger (drag, dragger->point, NULL);
290 drag->draggers = g_list_prepend (drag->draggers, dr_new);
292 // move all the VPs from dragger to dr_new
293 dr_new->vps = dragger->vps;
294 dragger->vps = NULL;
296 /* now we move all selected boxes back to the current dragger (splitting perspectives
297 if they also have unselected boxes) so that they are further reshaped during dragging */
299 GSList *boxes_to_do = drag->selectedBoxesWithVPinDragger (dr_new);
301 for (GSList *i = boxes_to_do; i != NULL; i = i->next) {
302 SP3DBox *box = SP_3DBOX (i->data);
303 Perspective3D *persp = get_persp_of_box (box);
304 VanishingPoint *vp = dr_new->getVPofPerspective (persp);
305 if (vp == NULL) {
306 g_warning ("VP is NULL. We should be okay, though.\n");
307 }
308 if (persp->all_boxes_occur_in_list (boxes_to_do)) {
309 // if all boxes of persp are selected, we can simply move the VP from dr_new back to dragger
310 dr_new->removeVP (vp);
311 dragger->addVP (vp);
313 // some cleaning up for efficiency
314 boxes_to_do = eliminate_remaining_boxes_of_persp_starting_from_list_position (boxes_to_do, box, persp);
315 } else {
316 /* otherwise the unselected boxes need to stay linked to dr_new; thus we
317 create a new perspective and link the VPs to the correct draggers */
318 Perspective3D *persp_new = new Perspective3D (*persp);
319 Perspective3D::add_perspective (persp_new);
321 Axis vp_axis = persp->get_axis_of_VP (vp);
322 dragger->addVP (persp_new->get_vanishing_point (vp_axis));
323 std::pair<Axis, Axis> rem_axes = get_remaining_axes (vp_axis);
324 drag->addDragger (persp->get_vanishing_point (rem_axes.first));
325 drag->addDragger (persp->get_vanishing_point (rem_axes.second));
327 // now we move the selected boxes from persp to persp_new
328 GSList * selected_boxes_of_perspective = persp->boxes_occurring_in_list (boxes_to_do);
329 for (GSList *j = selected_boxes_of_perspective; j != NULL; j = j->next) {
330 persp->remove_box (SP_3DBOX (j->data));
331 persp_new->add_box (SP_3DBOX (j->data));
332 }
334 // cleaning up
335 boxes_to_do = eliminate_remaining_boxes_of_persp_starting_from_list_position (boxes_to_do, box, persp);
336 }
337 }
339 // TODO: Something is still wrong with updating the boxes' representations after snapping
340 //dr_new->updateBoxReprs ();
341 }
342 }
344 // TODO: Update the tips
345 }
347 static void
348 vp_knot_ungrabbed_handler (SPKnot *knot, guint state, gpointer data)
349 {
350 VPDragger *dragger = (VPDragger *) data;
352 //sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
354 dragger->point_original = dragger->point = knot->pos;
356 /***
357 VanishingPoint *vp;
358 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
359 vp = (VanishingPoint *) i->data;
360 vp->set_pos (knot->pos);
361 }
362 ***/
364 dragger->parent->updateDraggers ();
365 dragger->updateBoxReprs ();
367 // TODO: Update box's paths and svg representation
369 // TODO: Undo machinery!!
370 }
372 VPDragger::VPDragger(VPDrag *parent, NR::Point p, VanishingPoint *vp)
373 {
374 this->vps = NULL;
376 this->parent = parent;
378 this->point = p;
379 this->point_original = p;
381 // create the knot
382 this->knot = sp_knot_new (parent->desktop, NULL);
383 this->knot->setMode(SP_KNOT_MODE_XOR);
384 this->knot->setFill(VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL);
385 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
386 sp_knot_update_ctrl(this->knot);
388 // move knot to the given point
389 sp_knot_set_position (this->knot, &this->point, SP_KNOT_STATE_NORMAL);
390 sp_knot_show (this->knot);
392 // connect knot's signals
393 g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (vp_knot_moved_handler), this);
394 /***
395 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (vp_knot_clicked_handler), this);
396 ***/
397 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (vp_knot_grabbed_handler), this);
398 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (vp_knot_ungrabbed_handler), this);
399 /***
400 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (vp_knot_doubleclicked_handler), this);
401 ***/
403 // add the initial VP (which may be NULL!)
404 this->addVP (vp);
405 //updateKnotShape();
406 }
408 VPDragger::~VPDragger()
409 {
410 // unselect if it was selected
411 //this->parent->setDeselected(this);
413 // disconnect signals
414 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_moved_handler), this);
415 /***
416 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_clicked_handler), this);
417 ***/
418 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_grabbed_handler), this);
419 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_ungrabbed_handler), this);
420 /***
421 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_doubleclicked_handler), this);
422 ***/
424 /* unref should call destroy */
425 g_object_unref (G_OBJECT (this->knot));
427 g_slist_free (this->vps);
428 this->vps = NULL;
429 }
431 /**
432 * Adds a vanishing point to the dragger (also updates the position)
433 */
434 void
435 VPDragger::addVP (VanishingPoint *vp)
436 {
437 if (vp == NULL) {
438 return;
439 }
440 if (g_slist_find (this->vps, vp)) {
441 // don't add the same VP twice
442 return;
443 }
445 vp->set_pos (this->point);
446 this->vps = g_slist_prepend (this->vps, vp);
448 //this->updateTip();
449 }
451 void
452 VPDragger::removeVP (VanishingPoint *vp)
453 {
454 if (vp == NULL) {
455 g_print ("NULL vanishing point will not be removed.\n");
456 return;
457 }
458 g_assert (this->vps != NULL);
459 this->vps = g_slist_remove (this->vps, vp);
461 //this->updateTip();
462 }
464 // returns the VP contained in the dragger that belongs to persp
465 VanishingPoint *
466 VPDragger::getVPofPerspective (Perspective3D *persp)
467 {
468 for (GSList *i = vps; i != NULL; i = i->next) {
469 if (persp->has_vanishing_point ((VanishingPoint *) i->data)) {
470 return ((VanishingPoint *) i->data);
471 }
472 }
473 return NULL;
474 }
476 bool
477 VPDragger::hasBox(const SP3DBox *box)
478 {
479 for (GSList *i = this->vps; i != NULL; i = i->next) {
480 if (get_persp_of_VP ((VanishingPoint *) i->data)->has_box (box)) return true;
481 }
482 return false;
483 }
485 guint
486 VPDragger::numberOfBoxes ()
487 {
488 guint num = 0;
489 for (GSList *i = this->vps; i != NULL; i = i->next) {
490 num += get_persp_of_VP ((VanishingPoint *) i->data)->number_of_boxes ();
491 }
492 return num;
493 }
495 bool
496 VPDragger::hasPerspective (const Perspective3D *persp)
497 {
498 for (GSList *i = this->vps; i != NULL; i = i->next) {
499 if (*persp == *get_persp_of_VP ((VanishingPoint *) i->data)) {
500 return true;
501 }
502 }
503 return false;
504 }
506 void
507 VPDragger::mergePerspectives ()
508 {
509 Perspective3D *persp1, *persp2;
510 GSList * successor = NULL;
511 for (GSList *i = this->vps; i != NULL; i = i->next) {
512 persp1 = get_persp_of_VP ((VanishingPoint *) i->data);
513 for (GSList *j = i->next; j != NULL; j = successor) {
514 // if the perspective is deleted, the VP is invalidated, too, so we must store its successor beforehand
515 successor = j->next;
516 persp2 = get_persp_of_VP ((VanishingPoint *) j->data);
517 if (*persp1 == *persp2) {
518 persp1->absorb (persp2); // persp2 is deleted; hopefully this doesn't screw up the list of vanishing points and thus the loops
519 }
520 }
521 }
522 }
524 void
525 VPDragger::reshapeBoxes (NR::Point const &p, Box3D::Axis axes)
526 {
527 Perspective3D *persp;
528 for (GSList const* i = this->vps; i != NULL; i = i->next) {
529 VanishingPoint *vp = (VanishingPoint *) i->data;
530 // TODO: We can extract the VP directly from the box's perspective. Is that vanishing point identical to 'vp'?
531 // Or is there duplicated information? If so, remove it and simplify the whole construction!
532 vp->set_pos(p);
533 persp = get_persp_of_VP (vp);
534 Box3D::Axis axis = persp->get_axis_of_VP (vp);
535 get_persp_of_VP (vp)->reshape_boxes (axis); // FIXME: we should only update the direction of the VP
536 }
537 parent->updateBoxHandles();
538 }
540 void
541 VPDragger::updateBoxReprs ()
542 {
543 for (GSList *i = this->vps; i != NULL; i = i->next) {
544 Box3D::get_persp_of_VP ((VanishingPoint *) i->data)->update_box_reprs ();
545 }
546 }
548 VPDrag::VPDrag (SPDesktop *desktop)
549 {
550 this->desktop = desktop;
551 this->selection = sp_desktop_selection(desktop);
553 this->draggers = NULL;
554 this->lines = NULL;
555 this->show_lines = true;
556 this->front_or_rear_lines = 0x1;
558 //this->selected = NULL;
559 this->local_change = false;
561 this->sel_changed_connection = this->selection->connectChanged(
562 sigc::bind (
563 sigc::ptr_fun(&vp_drag_sel_changed),
564 (gpointer)this )
566 );
567 this->sel_modified_connection = this->selection->connectModified(
568 sigc::bind(
569 sigc::ptr_fun(&vp_drag_sel_modified),
570 (gpointer)this )
571 );
573 this->updateDraggers ();
574 this->updateLines ();
575 }
577 VPDrag::~VPDrag()
578 {
579 this->sel_changed_connection.disconnect();
580 this->sel_modified_connection.disconnect();
582 for (GList *l = this->draggers; l != NULL; l = l->next) {
583 delete ((VPDragger *) l->data);
584 }
585 g_list_free (this->draggers);
586 this->draggers = NULL;
588 for (GSList const *i = this->lines; i != NULL; i = i->next) {
589 gtk_object_destroy( GTK_OBJECT (i->data));
590 }
591 g_slist_free (this->lines);
592 this->lines = NULL;
593 }
595 /**
596 * Select the dragger that has the given VP.
597 */
598 VPDragger *
599 VPDrag::getDraggerFor (VanishingPoint const &vp)
600 {
601 for (GList const* i = this->draggers; i != NULL; i = i->next) {
602 VPDragger *dragger = (VPDragger *) i->data;
603 for (GSList const* j = dragger->vps; j != NULL; j = j->next) {
604 VanishingPoint *vp2 = (VanishingPoint *) j->data;
605 g_assert (vp2 != NULL);
607 // TODO: Should we compare the pointers or the VPs themselves!?!?!?!
608 //if ((*vp2) == vp) {
609 if (vp2 == &vp) {
610 return (dragger);
611 }
612 }
613 }
614 return NULL;
615 }
617 /**
618 * Regenerates the draggers list from the current selection; is called when selection is changed or modified
619 */
620 void
621 VPDrag::updateDraggers ()
622 {
623 /***
624 while (selected) {
625 selected = g_list_remove(selected, selected->data);
626 }
627 ***/
628 // delete old draggers
629 for (GList const* i = this->draggers; i != NULL; i = i->next) {
630 delete ((VPDragger *) i->data);
631 }
632 g_list_free (this->draggers);
633 this->draggers = NULL;
635 g_return_if_fail (this->selection != NULL);
637 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
638 SPItem *item = SP_ITEM(i->data);
639 //SPStyle *style = SP_OBJECT_STYLE (item);
641 if (!SP_IS_3DBOX (item)) continue;
642 SP3DBox *box = SP_3DBOX (item);
644 // FIXME: Get the VPs from the selection!!!!
645 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::X));
646 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::Y));
647 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::Z));
649 //Box3D::Perspective3D *persp = box->perspective;
650 Box3D::Perspective3D *persp = Box3D::get_persp_of_box (box);
651 addDragger (persp->get_vanishing_point(Box3D::X));
652 addDragger (persp->get_vanishing_point(Box3D::Y));
653 addDragger (persp->get_vanishing_point(Box3D::Z));
654 }
655 }
657 /**
658 Regenerates the lines list from the current selection; is called on each move
659 of a dragger, so that lines are always in sync with the actual perspective
660 */
661 void
662 VPDrag::updateLines ()
663 {
664 // delete old lines
665 for (GSList const *i = this->lines; i != NULL; i = i->next) {
666 gtk_object_destroy( GTK_OBJECT (i->data));
667 }
668 g_slist_free (this->lines);
669 this->lines = NULL;
671 // do nothing if perspective lines are currently disabled
672 if (this->show_lines == 0) return;
674 g_return_if_fail (this->selection != NULL);
676 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
677 if (!SP_IS_3DBOX(i->data)) continue;
678 SP3DBox *box = SP_3DBOX (i->data);
680 this->drawLinesForFace (box, Box3D::X);
681 this->drawLinesForFace (box, Box3D::Y);
682 this->drawLinesForFace (box, Box3D::Z);
683 }
684 }
686 void
687 VPDrag::updateBoxHandles ()
688 {
689 // FIXME: Is there a way to update the knots without accessing the
690 // statically linked function knotholder_update_knots?
692 GSList *sel = (GSList *) selection->itemList();
693 if (g_slist_length (sel) > 1) {
694 // Currently we only show handles if a single box is selected
695 return;
696 }
698 if (!SP_IS_3DBOX (sel->data))
699 return;
701 SPEventContext *ec = inkscape_active_event_context();
702 g_assert (ec != NULL);
703 if (ec->shape_knot_holder != NULL) {
704 knotholder_update_knots(ec->shape_knot_holder, (SPItem *) sel->data);
705 }
706 }
708 /**
709 * Depending on the value of all_lines, draw the front and/or rear perspective lines starting from the given corners.
710 */
711 void
712 VPDrag::drawLinesForFace (const SP3DBox *box, Box3D::Axis axis) //, guint corner1, guint corner2, guint corner3, guint corner4)
713 {
714 guint color;
715 switch (axis) {
716 // TODO: Make color selectable by user
717 case Box3D::X: color = VP_LINE_COLOR_STROKE_X; break;
718 case Box3D::Y: color = VP_LINE_COLOR_STROKE_Y; break;
719 case Box3D::Z: color = VP_LINE_COLOR_STROKE_Z; break;
720 default: g_assert_not_reached();
721 }
723 NR::Point corner1, corner2, corner3, corner4;
724 sp_3dbox_corners_for_perspective_lines (box, axis, corner1, corner2, corner3, corner4);
726 //VanishingPoint *vp = box->perspective->get_vanishing_point (axis);
727 VanishingPoint *vp = Box3D::get_persp_of_box (box)->get_vanishing_point (axis);
728 if (vp->is_finite()) {
729 NR::Point pt = vp->get_pos();
730 if (this->front_or_rear_lines & 0x1) {
731 // draw 'front' perspective lines
732 this->addLine (corner1, pt, color);
733 this->addLine (corner2, pt, color);
734 }
735 if (this->front_or_rear_lines & 0x2) {
736 // draw 'rear' perspective lines
737 this->addLine (corner3, pt, color);
738 this->addLine (corner4, pt, color);
739 }
740 } else {
741 // TODO: Draw infinite PLs
742 g_warning ("Perspective lines for infinite vanishing points are not supported yet.\n");
743 }
745 }
747 /**
748 * Returns true if all boxes that are linked to a VP in the dragger are selected
749 */
750 bool
751 VPDrag::allBoxesAreSelected (VPDragger *dragger) {
752 GSList *selected_boxes = (GSList *) dragger->parent->selection->itemList();
753 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
754 if (!get_persp_of_VP ((VanishingPoint *) i->data)->all_boxes_occur_in_list (selected_boxes)) {
755 return false;
756 }
757 }
758 return true;
759 }
761 GSList *
762 VPDrag::selectedBoxesWithVPinDragger (VPDragger *dragger)
763 {
764 GSList *sel_boxes = g_slist_copy ((GSList *) dragger->parent->selection->itemList());
765 for (GSList const *i = sel_boxes; i != NULL; i = i->next) {
766 SP3DBox *box = SP_3DBOX (i->data);
767 if (!dragger->hasBox (box)) {
768 sel_boxes = g_slist_remove (sel_boxes, box);
769 }
770 }
771 return sel_boxes;
772 }
775 /**
776 * If there already exists a dragger within MERGE_DIST of p, add the VP to it;
777 * otherwise create new dragger and add it to draggers list
778 */
779 void
780 VPDrag::addDragger (VanishingPoint *vp)
781 {
782 if (vp == NULL) {
783 g_print ("Warning: The VP in addDragger is already NULL. Aborting.\n)");
784 g_assert (vp != NULL);
785 }
786 NR::Point p = vp->get_pos();
788 for (GList *i = this->draggers; i != NULL; i = i->next) {
789 VPDragger *dragger = (VPDragger *) i->data;
790 if (NR::L2 (dragger->point - p) < MERGE_DIST) {
791 // distance is small, merge this draggable into dragger, no need to create new dragger
792 dragger->addVP (vp);
793 //dragger->updateKnotShape();
794 return;
795 }
796 }
798 VPDragger *new_dragger = new VPDragger(this, p, vp);
799 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
800 this->draggers = g_list_append (this->draggers, new_dragger);
801 }
803 /**
804 Create a line from p1 to p2 and add it to the lines list
805 */
806 void
807 VPDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
808 {
809 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop), SP_TYPE_CTRLLINE, NULL);
810 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
811 if (rgba != VP_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
812 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
813 sp_canvas_item_show (line);
814 this->lines = g_slist_append (this->lines, line);
815 }
817 } // namespace Box3D
819 /*
820 Local Variables:
821 mode:c++
822 c-file-style:"stroustrup"
823 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
824 indent-tabs-mode:nil
825 fill-column:99
826 End:
827 */
828 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :