0b4140a8bc95a37dab705832202209a3a5d12aec
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 ();
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 = 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 Perspective3D::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 ();
343 }
344 }
346 // TODO: Update the tips
347 }
349 static void
350 vp_knot_ungrabbed_handler (SPKnot *knot, guint state, gpointer data)
351 {
352 VPDragger *dragger = (VPDragger *) data;
354 //sp_canvas_end_forced_full_redraws(dragger->parent->desktop->canvas);
356 dragger->point_original = dragger->point = knot->pos;
358 /***
359 VanishingPoint *vp;
360 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
361 vp = (VanishingPoint *) i->data;
362 vp->set_pos (knot->pos);
363 }
364 ***/
366 dragger->parent->updateDraggers ();
367 dragger->updateBoxReprs ();
369 // TODO: Update box's paths and svg representation
371 // TODO: Undo machinery!!
372 }
374 VPDragger::VPDragger(VPDrag *parent, NR::Point p, VanishingPoint *vp)
375 {
376 this->vps = NULL;
378 this->parent = parent;
380 this->point = p;
381 this->point_original = p;
383 // create the knot
384 this->knot = sp_knot_new (parent->desktop, NULL);
385 this->knot->setMode(SP_KNOT_MODE_XOR);
386 this->knot->setFill(VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL, VP_KNOT_COLOR_NORMAL);
387 this->knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
388 sp_knot_update_ctrl(this->knot);
390 // move knot to the given point
391 sp_knot_set_position (this->knot, &this->point, SP_KNOT_STATE_NORMAL);
392 sp_knot_show (this->knot);
394 // connect knot's signals
395 g_signal_connect (G_OBJECT (this->knot), "moved", G_CALLBACK (vp_knot_moved_handler), this);
396 /***
397 g_signal_connect (G_OBJECT (this->knot), "clicked", G_CALLBACK (vp_knot_clicked_handler), this);
398 ***/
399 g_signal_connect (G_OBJECT (this->knot), "grabbed", G_CALLBACK (vp_knot_grabbed_handler), this);
400 g_signal_connect (G_OBJECT (this->knot), "ungrabbed", G_CALLBACK (vp_knot_ungrabbed_handler), this);
401 /***
402 g_signal_connect (G_OBJECT (this->knot), "doubleclicked", G_CALLBACK (vp_knot_doubleclicked_handler), this);
403 ***/
405 // add the initial VP (which may be NULL!)
406 this->addVP (vp);
407 //updateKnotShape();
408 }
410 VPDragger::~VPDragger()
411 {
412 // unselect if it was selected
413 //this->parent->setDeselected(this);
415 // disconnect signals
416 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_moved_handler), this);
417 /***
418 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_clicked_handler), this);
419 ***/
420 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_grabbed_handler), this);
421 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_ungrabbed_handler), this);
422 /***
423 g_signal_handlers_disconnect_by_func(G_OBJECT(this->knot), (gpointer) G_CALLBACK (vp_knot_doubleclicked_handler), this);
424 ***/
426 /* unref should call destroy */
427 g_object_unref (G_OBJECT (this->knot));
429 g_slist_free (this->vps);
430 this->vps = NULL;
431 }
433 /**
434 * Adds a vanishing point to the dragger (also updates the position)
435 */
436 void
437 VPDragger::addVP (VanishingPoint *vp)
438 {
439 if (vp == NULL) {
440 return;
441 }
442 if (g_slist_find (this->vps, vp)) {
443 // don't add the same VP twice
444 return;
445 }
447 vp->set_pos (this->point);
448 this->vps = g_slist_prepend (this->vps, vp);
450 //this->updateTip();
451 }
453 void
454 VPDragger::removeVP (VanishingPoint *vp)
455 {
456 if (vp == NULL) {
457 g_print ("NULL vanishing point will not be removed.\n");
458 return;
459 }
460 g_assert (this->vps != NULL);
461 this->vps = g_slist_remove (this->vps, vp);
463 //this->updateTip();
464 }
466 // returns the VP contained in the dragger that belongs to persp
467 VanishingPoint *
468 VPDragger::getVPofPerspective (Perspective3D *persp)
469 {
470 for (GSList *i = vps; i != NULL; i = i->next) {
471 if (persp->has_vanishing_point ((VanishingPoint *) i->data)) {
472 return ((VanishingPoint *) i->data);
473 }
474 }
475 return NULL;
476 }
478 bool
479 VPDragger::hasBox(const SP3DBox *box)
480 {
481 for (GSList *i = this->vps; i != NULL; i = i->next) {
482 if (get_persp_of_VP ((VanishingPoint *) i->data)->has_box (box)) return true;
483 }
484 return false;
485 }
487 guint
488 VPDragger::numberOfBoxes ()
489 {
490 guint num = 0;
491 for (GSList *i = this->vps; i != NULL; i = i->next) {
492 num += get_persp_of_VP ((VanishingPoint *) i->data)->number_of_boxes ();
493 }
494 return num;
495 }
497 bool
498 VPDragger::hasPerspective (const Perspective3D *persp)
499 {
500 for (GSList *i = this->vps; i != NULL; i = i->next) {
501 if (*persp == *get_persp_of_VP ((VanishingPoint *) i->data)) {
502 return true;
503 }
504 }
505 return false;
506 }
508 void
509 VPDragger::mergePerspectives ()
510 {
511 Perspective3D *persp1, *persp2;
512 GSList * successor = NULL;
513 for (GSList *i = this->vps; i != NULL; i = i->next) {
514 persp1 = get_persp_of_VP ((VanishingPoint *) i->data);
515 for (GSList *j = i->next; j != NULL; j = successor) {
516 // if the perspective is deleted, the VP is invalidated, too, so we must store its successor beforehand
517 successor = j->next;
518 persp2 = get_persp_of_VP ((VanishingPoint *) j->data);
519 if (*persp1 == *persp2) {
520 persp1->absorb (persp2); // persp2 is deleted; hopefully this doesn't screw up the list of vanishing points and thus the loops
521 }
522 }
523 }
524 }
526 void
527 VPDragger::reshapeBoxes (NR::Point const &p, Box3D::Axis axes)
528 {
529 Perspective3D *persp;
530 for (GSList const* i = this->vps; i != NULL; i = i->next) {
531 VanishingPoint *vp = (VanishingPoint *) i->data;
532 // TODO: We can extract the VP directly from the box's perspective. Is that vanishing point identical to 'vp'?
533 // Or is there duplicated information? If so, remove it and simplify the whole construction!
534 vp->set_pos(p);
535 persp = get_persp_of_VP (vp);
536 Box3D::Axis axis = persp->get_axis_of_VP (vp);
537 get_persp_of_VP (vp)->reshape_boxes (axis); // FIXME: we should only update the direction of the VP
538 }
539 parent->updateBoxHandles();
540 }
542 void
543 VPDragger::updateBoxReprs ()
544 {
545 for (GSList *i = this->vps; i != NULL; i = i->next) {
546 Box3D::get_persp_of_VP ((VanishingPoint *) i->data)->update_box_reprs ();
547 }
548 }
550 void
551 VPDragger::updateZOrders ()
552 {
553 for (GSList *i = this->vps; i != NULL; i = i->next) {
554 Box3D::get_persp_of_VP ((VanishingPoint *) i->data)->update_z_orders ();
555 }
556 }
558 VPDrag::VPDrag (SPDesktop *desktop)
559 {
560 this->desktop = desktop;
561 this->selection = sp_desktop_selection(desktop);
563 this->draggers = NULL;
564 this->lines = NULL;
565 this->show_lines = true;
566 this->front_or_rear_lines = 0x1;
568 //this->selected = NULL;
569 this->local_change = false;
571 this->sel_changed_connection = this->selection->connectChanged(
572 sigc::bind (
573 sigc::ptr_fun(&vp_drag_sel_changed),
574 (gpointer)this )
576 );
577 this->sel_modified_connection = this->selection->connectModified(
578 sigc::bind(
579 sigc::ptr_fun(&vp_drag_sel_modified),
580 (gpointer)this )
581 );
583 this->updateDraggers ();
584 this->updateLines ();
585 }
587 VPDrag::~VPDrag()
588 {
589 this->sel_changed_connection.disconnect();
590 this->sel_modified_connection.disconnect();
592 for (GList *l = this->draggers; l != NULL; l = l->next) {
593 delete ((VPDragger *) l->data);
594 }
595 g_list_free (this->draggers);
596 this->draggers = NULL;
598 for (GSList const *i = this->lines; i != NULL; i = i->next) {
599 gtk_object_destroy( GTK_OBJECT (i->data));
600 }
601 g_slist_free (this->lines);
602 this->lines = NULL;
603 }
605 /**
606 * Select the dragger that has the given VP.
607 */
608 VPDragger *
609 VPDrag::getDraggerFor (VanishingPoint const &vp)
610 {
611 for (GList const* i = this->draggers; i != NULL; i = i->next) {
612 VPDragger *dragger = (VPDragger *) i->data;
613 for (GSList const* j = dragger->vps; j != NULL; j = j->next) {
614 VanishingPoint *vp2 = (VanishingPoint *) j->data;
615 g_assert (vp2 != NULL);
617 // TODO: Should we compare the pointers or the VPs themselves!?!?!?!
618 //if ((*vp2) == vp) {
619 if (vp2 == &vp) {
620 return (dragger);
621 }
622 }
623 }
624 return NULL;
625 }
627 /**
628 * Regenerates the draggers list from the current selection; is called when selection is changed or modified
629 */
630 void
631 VPDrag::updateDraggers ()
632 {
633 /***
634 while (selected) {
635 selected = g_list_remove(selected, selected->data);
636 }
637 ***/
638 // delete old draggers
639 for (GList const* i = this->draggers; i != NULL; i = i->next) {
640 delete ((VPDragger *) i->data);
641 }
642 g_list_free (this->draggers);
643 this->draggers = NULL;
645 g_return_if_fail (this->selection != NULL);
647 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
648 SPItem *item = SP_ITEM(i->data);
649 //SPStyle *style = SP_OBJECT_STYLE (item);
651 if (!SP_IS_3DBOX (item)) continue;
652 SP3DBox *box = SP_3DBOX (item);
654 // FIXME: Get the VPs from the selection!!!!
655 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::X));
656 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::Y));
657 //addDragger (Box3D::Perspective3D::current_perspective->get_vanishing_point(Box3D::Z));
659 //Box3D::Perspective3D *persp = box->perspective;
660 Box3D::Perspective3D *persp = Box3D::get_persp_of_box (box);
661 addDragger (persp->get_vanishing_point(Box3D::X));
662 addDragger (persp->get_vanishing_point(Box3D::Y));
663 addDragger (persp->get_vanishing_point(Box3D::Z));
664 }
665 }
667 /**
668 Regenerates the lines list from the current selection; is called on each move
669 of a dragger, so that lines are always in sync with the actual perspective
670 */
671 void
672 VPDrag::updateLines ()
673 {
674 // delete old lines
675 for (GSList const *i = this->lines; i != NULL; i = i->next) {
676 gtk_object_destroy( GTK_OBJECT (i->data));
677 }
678 g_slist_free (this->lines);
679 this->lines = NULL;
681 // do nothing if perspective lines are currently disabled
682 if (this->show_lines == 0) return;
684 g_return_if_fail (this->selection != NULL);
686 for (GSList const* i = this->selection->itemList(); i != NULL; i = i->next) {
687 if (!SP_IS_3DBOX(i->data)) continue;
688 SP3DBox *box = SP_3DBOX (i->data);
690 this->drawLinesForFace (box, Box3D::X);
691 this->drawLinesForFace (box, Box3D::Y);
692 this->drawLinesForFace (box, Box3D::Z);
693 }
694 }
696 void
697 VPDrag::updateBoxHandles ()
698 {
699 // FIXME: Is there a way to update the knots without accessing the
700 // statically linked function knotholder_update_knots?
702 GSList *sel = (GSList *) selection->itemList();
703 if (g_slist_length (sel) > 1) {
704 // Currently we only show handles if a single box is selected
705 return;
706 }
708 if (!SP_IS_3DBOX (sel->data))
709 return;
711 SPEventContext *ec = inkscape_active_event_context();
712 g_assert (ec != NULL);
713 if (ec->shape_knot_holder != NULL) {
714 knotholder_update_knots(ec->shape_knot_holder, (SPItem *) sel->data);
715 }
716 }
718 /**
719 * Depending on the value of all_lines, draw the front and/or rear perspective lines starting from the given corners.
720 */
721 void
722 VPDrag::drawLinesForFace (const SP3DBox *box, Box3D::Axis axis) //, guint corner1, guint corner2, guint corner3, guint corner4)
723 {
724 guint color;
725 switch (axis) {
726 // TODO: Make color selectable by user
727 case Box3D::X: color = VP_LINE_COLOR_STROKE_X; break;
728 case Box3D::Y: color = VP_LINE_COLOR_STROKE_Y; break;
729 case Box3D::Z: color = VP_LINE_COLOR_STROKE_Z; break;
730 default: g_assert_not_reached();
731 }
733 NR::Point corner1, corner2, corner3, corner4;
734 sp_3dbox_corners_for_perspective_lines (box, axis, corner1, corner2, corner3, corner4);
736 //VanishingPoint *vp = box->perspective->get_vanishing_point (axis);
737 VanishingPoint *vp = Box3D::get_persp_of_box (box)->get_vanishing_point (axis);
738 if (vp->is_finite()) {
739 NR::Point pt = vp->get_pos();
740 if (this->front_or_rear_lines & 0x1) {
741 // draw 'front' perspective lines
742 this->addLine (corner1, pt, color);
743 this->addLine (corner2, pt, color);
744 }
745 if (this->front_or_rear_lines & 0x2) {
746 // draw 'rear' perspective lines
747 this->addLine (corner3, pt, color);
748 this->addLine (corner4, pt, color);
749 }
750 } else {
751 // TODO: Draw infinite PLs
752 g_warning ("Perspective lines for infinite vanishing points are not supported yet.\n");
753 }
755 }
757 /**
758 * Returns true if all boxes that are linked to a VP in the dragger are selected
759 */
760 bool
761 VPDrag::allBoxesAreSelected (VPDragger *dragger) {
762 GSList *selected_boxes = (GSList *) dragger->parent->selection->itemList();
763 for (GSList *i = dragger->vps; i != NULL; i = i->next) {
764 if (!get_persp_of_VP ((VanishingPoint *) i->data)->all_boxes_occur_in_list (selected_boxes)) {
765 return false;
766 }
767 }
768 return true;
769 }
771 GSList *
772 VPDrag::selectedBoxesWithVPinDragger (VPDragger *dragger)
773 {
774 GSList *sel_boxes = g_slist_copy ((GSList *) dragger->parent->selection->itemList());
775 for (GSList const *i = sel_boxes; i != NULL; i = i->next) {
776 SP3DBox *box = SP_3DBOX (i->data);
777 if (!dragger->hasBox (box)) {
778 sel_boxes = g_slist_remove (sel_boxes, box);
779 }
780 }
781 return sel_boxes;
782 }
785 /**
786 * If there already exists a dragger within MERGE_DIST of p, add the VP to it;
787 * otherwise create new dragger and add it to draggers list
788 */
789 void
790 VPDrag::addDragger (VanishingPoint *vp)
791 {
792 if (vp == NULL) {
793 g_print ("Warning: The VP in addDragger is already NULL. Aborting.\n)");
794 g_assert (vp != NULL);
795 }
796 NR::Point p = vp->get_pos();
798 for (GList *i = this->draggers; i != NULL; i = i->next) {
799 VPDragger *dragger = (VPDragger *) i->data;
800 if (NR::L2 (dragger->point - p) < MERGE_DIST) {
801 // distance is small, merge this draggable into dragger, no need to create new dragger
802 dragger->addVP (vp);
803 //dragger->updateKnotShape();
804 return;
805 }
806 }
808 VPDragger *new_dragger = new VPDragger(this, p, vp);
809 // fixme: draggers should be added AFTER the last one: this way tabbing through them will be from begin to end.
810 this->draggers = g_list_append (this->draggers, new_dragger);
811 }
813 /**
814 Create a line from p1 to p2 and add it to the lines list
815 */
816 void
817 VPDrag::addLine (NR::Point p1, NR::Point p2, guint32 rgba)
818 {
819 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(this->desktop), SP_TYPE_CTRLLINE, NULL);
820 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
821 if (rgba != VP_LINE_COLOR_FILL) // fill is the default, so don't set color for it to speed up redraw
822 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
823 sp_canvas_item_show (line);
824 this->lines = g_slist_append (this->lines, line);
825 }
827 } // namespace Box3D
829 /*
830 Local Variables:
831 mode:c++
832 c-file-style:"stroustrup"
833 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
834 indent-tabs-mode:nil
835 fill-column:99
836 End:
837 */
838 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :