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 "libnr/nr-point.h"
16 #include "knot.h"
17 #include "selection.h"
18 #include "axis-manip.h"
20 #include "line-geometry.h" // TODO: Remove this include as soon as we don't need create_canvas_(point|line) any more.
22 class SP3DBox;
24 namespace Box3D {
26 enum VPState {
27 VP_FINITE = 0, // perspective lines meet in the VP
28 VP_INFINITE // perspective lines are parallel
29 };
31 // FIXME: Store the Axis of the VP inside the class
32 class VanishingPoint : public NR::Point {
33 public:
34 inline VanishingPoint() : NR::Point() {};
35 /***
36 inline VanishingPoint(NR::Point const &pt, NR::Point const &ref = NR::Point(0,0))
37 : NR::Point (pt),
38 ref_pt (ref),
39 v_dir (pt[NR::X] - ref[NR::X], pt[NR::Y] - ref[NR::Y]) {}
40 inline VanishingPoint(NR::Coord x, NR::Coord y, NR::Point const &ref = NR::Point(0,0))
41 : NR::Point (x, y),
42 ref_pt (ref),
43 v_dir (x - ref[NR::X], y - ref[NR::Y]) {}
44 ***/
45 VanishingPoint(NR::Point const &pt, NR::Point const &inf_dir, VPState st);
46 VanishingPoint(NR::Point const &pt);
47 VanishingPoint(NR::Point const &dir, VPState const state);
48 VanishingPoint(NR::Point const &pt, NR::Point const &direction);
49 VanishingPoint(NR::Coord x, NR::Coord y);
50 VanishingPoint(NR::Coord x, NR::Coord y, VPState const state);
51 VanishingPoint(NR::Coord x, NR::Coord y, NR::Coord dir_x, NR::Coord dir_y);
52 VanishingPoint(VanishingPoint const &rhs);
53 ~VanishingPoint();
55 bool operator== (VanishingPoint const &other);
57 inline NR::Point get_pos() const { return NR::Point ((*this)[NR::X], (*this)[NR::Y]); }
58 inline double get_angle() const { return NR::atan2 (this->v_dir) * 180/M_PI; } // return angle of infinite direction is in degrees
59 inline void set_pos(NR::Point const &pt) { (*this)[NR::X] = pt[NR::X];
60 (*this)[NR::Y] = pt[NR::Y]; }
61 inline void set_pos(const double pt_x, const double pt_y) { (*this)[NR::X] = pt_x;
62 (*this)[NR::Y] = pt_y; }
63 inline void set_infinite_direction (const NR::Point dir) { v_dir = dir; }
64 inline void set_infinite_direction (const double dir_x, const double dir_y) { v_dir = NR::Point (dir_x, dir_y); }
66 bool is_finite() const;
67 VPState toggle_parallel();
68 void draw(Box3D::Axis const axis); // Draws a point on the canvas if state == VP_FINITE
69 //inline VPState state() { return state; }
71 VPState state;
72 //NR::Point ref_pt; // point of reference to compute the direction of parallel lines
73 NR::Point v_dir; // direction of perslective lines if the VP has state == VP_INFINITE
75 private:
76 };
78 class Perspective3D;
79 class VPDrag;
81 struct VPDragger {
82 public:
83 VPDragger(VPDrag *parent, NR::Point p, VanishingPoint *vp);
84 ~VPDragger();
86 VPDrag *parent;
87 SPKnot *knot;
89 // position of the knot, desktop coords
90 NR::Point point;
91 // position of the knot before it began to drag; updated when released
92 NR::Point point_original;
94 GSList *vps; // the list of vanishing points
96 void addVP(VanishingPoint *vp);
97 void removeVP(VanishingPoint *vp);
98 /* returns the VP of the dragger that belongs to the given perspective */
99 VanishingPoint *getVPofPerspective (Perspective3D *persp);
101 void updateTip();
103 bool hasBox (const SP3DBox *box);
104 guint numberOfBoxes(); // the number of boxes linked to all VPs of the dragger
106 bool hasPerspective (const Perspective3D *perps);
107 void mergePerspectives (); // remove duplicate perspectives
109 void reshapeBoxes(NR::Point const &p, Box3D::Axis axes);
110 void updateBoxReprs();
111 void updateZOrders();
112 };
114 struct VPDrag {
115 public:
116 VPDrag(SPDocument *document);
117 ~VPDrag();
119 VPDragger *getDraggerFor (VanishingPoint const &vp);
121 //void grabKnot (VanishingPoint const &vp, gint x, gint y, guint32 etime);
123 bool local_change;
124 bool dragging;
126 SPDocument *document;
127 GList *draggers;
128 GSList *lines;
130 void updateDraggers ();
131 void updateLines ();
132 void updateBoxHandles ();
133 void drawLinesForFace (const SP3DBox *box, Box3D::Axis axis); //, guint corner1, guint corner2, guint corner3, guint corner4);
134 bool show_lines; /* whether perspective lines are drawn at all */
135 guint front_or_rear_lines; /* whether we draw perspective lines from all corners or only the
136 front/rear corners (indicated by the first/second bit, respectively */
139 inline bool hasEmptySelection() { return this->selection->isEmpty(); }
140 bool allBoxesAreSelected (VPDragger *dragger);
141 GSList * selectedBoxesWithVPinDragger (VPDragger *dragger);
143 // FIXME: Should this be private? (It's the case with the corresponding function in gradient-drag.h)
144 // But vp_knot_grabbed_handler
145 void addDragger (VanishingPoint *vp);
147 private:
148 //void deselect_all();
150 void addLine (NR::Point p1, NR::Point p2, guint32 rgba);
152 Inkscape::Selection *selection;
153 sigc::connection sel_changed_connection;
154 sigc::connection sel_modified_connection;
155 };
157 } // namespace Box3D
160 /** A function to print out the VanishingPoint (prints the coordinates) **/
161 /***
162 inline std::ostream &operator<< (std::ostream &out_file, const VanishingPoint &vp) {
163 out_file << vp;
164 return out_file;
165 }
166 ***/
169 #endif /* !SEEN_VANISHING_POINT_H */
171 /*
172 Local Variables:
173 mode:c++
174 c-file-style:"stroustrup"
175 c-file-offsets:((innamespace . 0)(inline-open . 0))
176 indent-tabs-mode:nil
177 fill-column:99
178 End:
179 */
180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :