Code

Store cached icons to disk between runs, and invalidate/purge as needed.
[inkscape.git] / src / vanishing-point.h
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 <2geom/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 class SPBox3D;
27 namespace Box3D {
29 enum VPState {
30     VP_FINITE = 0, // perspective lines meet in the VP
31     VP_INFINITE    // perspective lines are parallel
32 };
34 /* VanishingPoint is a simple wrapper class to easily extract VP data from perspectives.
35  * A VanishingPoint represents a VP in a certain direction (X, Y, Z) of a single perspective.
36  * In particular, it can potentially have more than one box linked to it (although in facth they
37  * are rather linked to the parent perspective).
38  */
39 // FIXME: Don't store the box in the VP but rather the perspective (and link the box to it)!!
40 class VanishingPoint {
41 public:
42     VanishingPoint() : my_counter(VanishingPoint::global_counter++), _persp(NULL), _axis(Proj::NONE) {}
43     VanishingPoint(Persp3D *persp, Proj::Axis axis) : my_counter(VanishingPoint::global_counter++), _persp(persp), _axis(axis) {}
44     VanishingPoint(const VanishingPoint &other) : my_counter(VanishingPoint::global_counter++), _persp(other._persp), _axis(other._axis) {}
46     inline VanishingPoint &operator=(VanishingPoint const &rhs) {
47         _persp = rhs._persp;
48         _axis = rhs._axis;
49         return *this;
50     }
51     inline bool operator==(VanishingPoint const &rhs) const {
52         /* vanishing points coincide if they belong to the same perspective */
53         return (_persp == rhs._persp && _axis == rhs._axis);
54     }
56     inline bool operator<(VanishingPoint const &rhs) const {
57         return my_counter < rhs.my_counter;
58     }
60     inline void set(Persp3D *persp, Proj::Axis axis) {
61         _persp = persp;
62         _axis = axis;
63     }
64     void set_pos(Proj::Pt2 const &pt);
65     inline bool is_finite() const {
66         g_return_val_if_fail (_persp, false);
67         return persp3d_get_VP (_persp, _axis).is_finite();
68     }
69     inline Geom::Point get_pos() const {
70         g_return_val_if_fail (_persp, Geom::Point (NR_HUGE, NR_HUGE));
71         return persp3d_get_VP (_persp,_axis).affine();
72     }
73     inline Persp3D * get_perspective() const {
74         return _persp;
75     }
76     inline Persp3D * set_perspective(Persp3D *persp) {
77         return _persp = persp;
78     }
80     inline bool hasBox (SPBox3D *box) {
81         return persp3d_has_box(_persp, box);
82     }
83     inline unsigned int numberOfBoxes() const {
84         return persp3d_num_boxes(_persp);
85     }
87     /* returns all selected boxes sharing this perspective */
88     std::list<SPBox3D *> selectedBoxes(Inkscape::Selection *sel);
90     inline void updateBoxDisplays() const {
91         g_return_if_fail (_persp);
92         persp3d_update_box_displays(_persp);
93     }
94     inline void updateBoxReprs() const {
95         g_return_if_fail (_persp);
96         persp3d_update_box_reprs(_persp);
97     }
98     inline void updatePerspRepr() const {
99         g_return_if_fail (_persp);
100         SP_OBJECT(_persp)->updateRepr(SP_OBJECT_WRITE_EXT);
101     }
102     inline void printPt() const {
103         g_return_if_fail (_persp);
104         persp3d_get_VP (_persp, _axis).print("");
105     }
106     inline gchar const *axisString () { return Proj::string_from_axis(_axis); }
108     unsigned int my_counter;
109     static unsigned int global_counter; // FIXME: Only to implement operator< so that we can merge lists. Do this in a better way!!
110 private:
111     Persp3D *_persp;
112     Proj::Axis _axis;
113 };
115 class VPDrag;
117 struct less_ptr : public std::binary_function<VanishingPoint *, VanishingPoint *, bool> {
118     bool operator()(VanishingPoint *vp1, VanishingPoint *vp2) {
119         return GPOINTER_TO_INT(vp1) < GPOINTER_TO_INT(vp2);
120     }
121 };
123 struct VPDragger {
124 public:
125     VPDragger(VPDrag *parent, Geom::Point p, VanishingPoint &vp);
126     ~VPDragger();
128     VPDrag *parent;
129     SPKnot *knot;
131     // position of the knot, desktop coords
132     Geom::Point point;
133     // position of the knot before it began to drag; updated when released
134     Geom::Point point_original;
136     bool dragging_started;
138     std::list<VanishingPoint> vps;
140     void addVP(VanishingPoint &vp, bool update_pos = false);
141     void removeVP(const VanishingPoint &vp);
143     void updateTip();
145     guint numberOfBoxes(); // the number of boxes linked to all VPs of the dragger
146     VanishingPoint *findVPWithBox(SPBox3D *box);
147     std::set<VanishingPoint*, less_ptr> VPsOfSelectedBoxes();
149     bool hasPerspective(const Persp3D *persp);
150     void mergePerspectives(); // remove duplicate perspectives
152     void updateBoxDisplays();
153     void updateVPs(Geom::Point const &pt);
154     void updateZOrders();
156     void printVPs();
157 };
159 struct VPDrag {
160 public:
161     VPDrag(SPDocument *document);
162     ~VPDrag();
164     VPDragger *getDraggerFor (VanishingPoint const &vp);
166     bool dragging;
168     SPDocument *document;
169     GList *draggers;
170     GSList *lines;
172     void printDraggers(); // convenience for debugging
173     /* 
174      * FIXME: Should the following functions be merged?
175      *        Also, they should make use of the info in a VanishingPoint structure (regarding boxes
176      *        and perspectives) rather than each time iterating over the whole list of selected items?
177      */
178     void updateDraggers ();
179     void updateLines ();
180     void updateBoxHandles ();
181     void updateBoxReprs ();
182     void updateBoxDisplays ();
183     void drawLinesForFace (const SPBox3D *box, Proj::Axis axis); //, guint corner1, guint corner2, guint corner3, guint corner4);
184     bool show_lines; /* whether perspective lines are drawn at all */
185     guint front_or_rear_lines; /* whether we draw perspective lines from all corners or only the
186                                   front/rear corners (indicated by the first/second bit, respectively  */
189     inline bool hasEmptySelection() { return this->selection->isEmpty(); }
190     bool allBoxesAreSelected (VPDragger *dragger);
191     GSList * selectedBoxesWithVPinDragger (VPDragger *dragger);
193     // FIXME: Should this be private? (It's the case with the corresponding function in gradient-drag.h)
194     //        But vp_knot_grabbed_handler
195     void addDragger (VanishingPoint &vp);
197     void swap_perspectives_of_VPs(Persp3D *persp2, Persp3D *persp1);
199 private:
200     //void deselect_all();
202     void addLine (Geom::Point p1, Geom::Point p2, guint32 rgba);
204     Inkscape::Selection *selection;
205     sigc::connection sel_changed_connection;
206     sigc::connection sel_modified_connection;
207 };
209 } // namespace Box3D
212 #endif /* !SEEN_VANISHING_POINT_H */
214 /*
215   Local Variables:
216   mode:c++
217   c-file-style:"stroustrup"
218   c-file-offsets:((innamespace . 0)(inline-open . 0))
219   indent-tabs-mode:nil
220   fill-column:99
221   End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :