1 /*
2 * Vanishing point for 3D perspectives
3 *
4 * Authors:
5 * Maximilian Albert <Anhalter42@gmx.de>
6 *
7 * Copyright (C) 2007 authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #ifndef SEEN_VANISHING_POINT_H
13 #define SEEN_VANISHING_POINT_H
15 #include <set>
16 #include "libnr/nr-point.h"
17 #include "knot.h"
18 #include "selection.h"
19 #include "axis-manip.h"
20 #include "inkscape.h"
21 #include "persp3d.h"
22 #include "box3d.h"
23 #include "persp3d-reference.h"
25 #include "line-geometry.h" // TODO: Remove this include as soon as we don't need create_canvas_(point|line) any more.
27 class SPBox3D;
29 namespace Box3D {
31 enum VPState {
32 VP_FINITE = 0, // perspective lines meet in the VP
33 VP_INFINITE // perspective lines are parallel
34 };
36 /* VanishingPoint is a simple wrapper class to easily extract VP data from perspectives.
37 * A VanishingPoint represents a VP in a certain direction (X, Y, Z) of a single perspective.
38 * In particular, it can potentially have more than one box linked to it (although in facth they
39 * are rather linked to the parent perspective).
40 */
41 // FIXME: Don't store the box in the VP but rather the perspective (and link the box to it)!!
42 class VanishingPoint {
43 public:
44 VanishingPoint() : my_counter(VanishingPoint::global_counter++), _persp(NULL), _axis(Proj::NONE) {}
45 VanishingPoint(Persp3D *persp, Proj::Axis axis) : my_counter(VanishingPoint::global_counter++), _persp(persp), _axis(axis) {}
46 VanishingPoint(const VanishingPoint &other) : my_counter(VanishingPoint::global_counter++), _persp(other._persp), _axis(other._axis) {}
48 inline VanishingPoint &operator=(VanishingPoint const &rhs) {
49 _persp = rhs._persp;
50 _axis = rhs._axis;
51 return *this;
52 }
53 inline bool operator==(VanishingPoint const &rhs) const {
54 /* vanishing points coincide if they belong to the same perspective */
55 return (_persp == rhs._persp && _axis == rhs._axis);
56 }
58 inline bool operator<(VanishingPoint const &rhs) const {
59 return my_counter < rhs.my_counter;
60 }
62 inline void set(Persp3D *persp, Proj::Axis axis) {
63 _persp = persp;
64 _axis = axis;
65 }
66 void set_pos(Proj::Pt2 const &pt);
67 inline bool is_finite() const {
68 g_return_val_if_fail (_persp, false);
69 return persp3d_get_VP (_persp, _axis).is_finite();
70 }
71 inline NR::Point get_pos() const {
72 g_return_val_if_fail (_persp, NR::Point (NR_HUGE, NR_HUGE));
73 return persp3d_get_VP (_persp,_axis).affine();
74 }
75 inline Persp3D * get_perspective() const {
76 return _persp;
77 }
78 inline Persp3D * set_perspective(Persp3D *persp) {
79 return _persp = persp;
80 }
82 inline bool hasBox (SPBox3D *box) {
83 return persp3d_has_box(_persp, box);
84 }
85 inline unsigned int numberOfBoxes() const {
86 return persp3d_num_boxes(_persp);
87 }
89 /* returns all selected boxes sharing this perspective */
90 std::list<SPBox3D *> selectedBoxes(Inkscape::Selection *sel);
92 inline void updateBoxDisplays() const {
93 g_return_if_fail (_persp);
94 persp3d_update_box_displays(_persp);
95 }
96 inline void updateBoxReprs() const {
97 g_return_if_fail (_persp);
98 persp3d_update_box_reprs(_persp);
99 }
100 inline void updatePerspRepr() const {
101 g_return_if_fail (_persp);
102 SP_OBJECT(_persp)->updateRepr(SP_OBJECT_WRITE_EXT);
103 }
104 inline void printPt() const {
105 g_return_if_fail (_persp);
106 persp3d_get_VP (_persp, _axis).print("");
107 }
108 inline gchar const *axisString () { return Proj::string_from_axis(_axis); }
110 unsigned int my_counter;
111 static unsigned int global_counter; // FIXME: Only to implement operator< so that we can merge lists. Do this in a better way!!
112 private:
113 Persp3D *_persp;
114 Proj::Axis _axis;
115 };
117 class VPDrag;
119 struct less_ptr : public std::binary_function<VanishingPoint *, VanishingPoint *, bool> {
120 bool operator()(VanishingPoint *vp1, VanishingPoint *vp2) {
121 return GPOINTER_TO_INT(vp1) < GPOINTER_TO_INT(vp2);
122 }
123 };
125 struct VPDragger {
126 public:
127 VPDragger(VPDrag *parent, NR::Point p, VanishingPoint &vp);
128 ~VPDragger();
130 VPDrag *parent;
131 SPKnot *knot;
133 // position of the knot, desktop coords
134 NR::Point point;
135 // position of the knot before it began to drag; updated when released
136 NR::Point point_original;
138 bool dragging_started;
140 std::list<VanishingPoint> vps;
142 void addVP(VanishingPoint &vp, bool update_pos = false);
143 void removeVP(const VanishingPoint &vp);
145 void updateTip();
147 guint numberOfBoxes(); // the number of boxes linked to all VPs of the dragger
148 VanishingPoint *findVPWithBox(SPBox3D *box);
149 std::set<VanishingPoint*, less_ptr> VPsOfSelectedBoxes();
151 bool hasPerspective(const Persp3D *persp);
152 void mergePerspectives(); // remove duplicate perspectives
154 void updateBoxDisplays();
155 void updateVPs(NR::Point const &pt);
156 void updateZOrders();
158 void printVPs();
159 };
161 struct VPDrag {
162 public:
163 VPDrag(SPDocument *document);
164 ~VPDrag();
166 VPDragger *getDraggerFor (VanishingPoint const &vp);
168 bool dragging;
170 SPDocument *document;
171 GList *draggers;
172 GSList *lines;
174 void printDraggers(); // convenience for debugging
175 /*
176 * FIXME: Should the following functions be merged?
177 * Also, they should make use of the info in a VanishingPoint structure (regarding boxes
178 * and perspectives) rather than each time iterating over the whole list of selected items?
179 */
180 void updateDraggers ();
181 void updateLines ();
182 void updateBoxHandles ();
183 void updateBoxReprs ();
184 void updateBoxDisplays ();
185 void drawLinesForFace (const SPBox3D *box, Proj::Axis axis); //, guint corner1, guint corner2, guint corner3, guint corner4);
186 bool show_lines; /* whether perspective lines are drawn at all */
187 guint front_or_rear_lines; /* whether we draw perspective lines from all corners or only the
188 front/rear corners (indicated by the first/second bit, respectively */
191 inline bool hasEmptySelection() { return this->selection->isEmpty(); }
192 bool allBoxesAreSelected (VPDragger *dragger);
193 GSList * selectedBoxesWithVPinDragger (VPDragger *dragger);
195 // FIXME: Should this be private? (It's the case with the corresponding function in gradient-drag.h)
196 // But vp_knot_grabbed_handler
197 void addDragger (VanishingPoint &vp);
199 void swap_perspectives_of_VPs(Persp3D *persp2, Persp3D *persp1);
201 private:
202 //void deselect_all();
204 void addLine (NR::Point p1, NR::Point p2, guint32 rgba);
206 Inkscape::Selection *selection;
207 sigc::connection sel_changed_connection;
208 sigc::connection sel_modified_connection;
209 };
211 } // namespace Box3D
214 #endif /* !SEEN_VANISHING_POINT_H */
216 /*
217 Local Variables:
218 mode:c++
219 c-file-style:"stroustrup"
220 c-file-offsets:((innamespace . 0)(inline-open . 0))
221 indent-tabs-mode:nil
222 fill-column:99
223 End:
224 */
225 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :