Code

Filters. Custom predefined filters update and new ABC filters.
[inkscape.git] / src / snapper.h
1 #ifndef SEEN_SNAPPER_H
2 #define SEEN_SNAPPER_H
4 /**
5  *    \file src/snapper.h
6  *    \brief Snapper class.
7  *
8  *    Authors:
9  *      Carl Hetherington <inkscape@carlh.net>
10  *      Diederik van Lierop <mail@diedenrezi.nl>
11  *
12  *    Released under GNU GPL, read the file 'COPYING' for more information.
13  */
15 #include <map>
16 #include <list>
17 #include <boost/optional.hpp>
18 #include <glib.h> // for g_assert
20 #include "snapped-point.h"
21 #include "snapped-line.h"
22 #include "snapped-curve.h"
23 #include "snap-preferences.h"
24 #include "snap-candidate.h"
26 struct SnappedConstraints {
27     std::list<Inkscape::SnappedPoint> points;
28     std::list<Inkscape::SnappedLineSegment> lines;
29     std::list<Inkscape::SnappedLine> grid_lines;
30     std::list<Inkscape::SnappedLine> guide_lines;
31     std::list<Inkscape::SnappedCurve> curves;
32 };
34 class SnapManager;
35 struct SPItem;
37 namespace Inkscape
38 {
39 /// Parent for classes that can snap points to something
40 class Snapper
41 {
42 public:
43     Snapper() {}
44     Snapper(SnapManager *sm, ::Geom::Coord const t);
45     virtual ~Snapper() {}
47     virtual Geom::Coord getSnapperTolerance() const = 0; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
48     virtual bool getSnapperAlwaysSnap() const = 0; //if true, then the snapper will always snap, regardless of its tolerance
50     /**
51     *  \return true if this Snapper will snap at least one kind of point.
52     */
53     virtual bool ThisSnapperMightSnap() const {return _snap_enabled;} // will likely be overridden by derived classes
55     // These four methods are only used for grids, for which snapping can be enabled individually
56     void setEnabled(bool s);
57     void setSnapVisibleOnly(bool s);
58     bool getEnabled() const {return _snap_enabled;}
59     bool getSnapVisibleOnly() const {return _snap_visible_only;}
61     virtual void freeSnap(SnappedConstraints &/*sc*/,
62                           Inkscape::SnapCandidatePoint const &/*p*/,
63                           Geom::OptRect const &/*bbox_to_snap*/,
64                           std::vector<SPItem const *> const */*it*/,
65                           std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
67     // Class for storing the constraint for constrained snapping; can be
68     // - a line (infinite line with origin, running through _point pointing in _direction)
69     // - a direction (infinite line without origin, i.e. only a direction vector, stored in _direction)
70     // - a circle (_point denotes the center, _radius doesn't need an explanation, _direction contains
71     //      the vector from the origin to the original untransformed point);
72     class SnapConstraint
73     {
74     private:
75         enum SnapConstraintType {LINE, DIRECTION, CIRCLE, UNDEFINED};
77     public:
78         // Constructs a direction constraint, e.g. horizontal or vertical but without a specified point
79         SnapConstraint(Geom::Point const &d) : _direction(d), _type(DIRECTION) {}
80         // Constructs a linear constraint
81         SnapConstraint(Geom::Point const &p, Geom::Point const &d) : _point(p), _direction(d), _type(LINE) {}
82         SnapConstraint(Geom::Line const &l) : _point(l.origin()), _direction(l.versor()), _type(LINE) {}
83         // Constructs a circular constraint
84         SnapConstraint(Geom::Point const &p, Geom::Point const &d, Geom::Coord const &r) : _point(p), _direction(d), _radius(r), _type(CIRCLE) {}
85         // Undefined, or empty constraint
86         SnapConstraint() : _type(UNDEFINED) {}
88         bool hasPoint() const {return _type != DIRECTION && _type != UNDEFINED;}
90         Geom::Point getPoint() const {
91             g_assert(_type != DIRECTION && _type != UNDEFINED);
92             return _point;
93         }
95         Geom::Point getDirection() const {
96             return _direction;
97         }
99         Geom::Coord getRadius() const {
100             g_assert(_type == CIRCLE);
101             return _radius;
102         }
104         bool isCircular() const { return _type == CIRCLE; }
105         bool isLinear() const { return _type == LINE; }
106         bool isDirection() const { return _type == DIRECTION; }
107         bool isUndefined() const { return _type == UNDEFINED; }
109         Geom::Point projection(Geom::Point const &p) const { // returns the projection of p on this constraint
110             if (_type == CIRCLE) {
111                 // project on to a circular constraint
112                 Geom::Point v_orig = p - _point;
113                 Geom::Coord l = Geom::L2(v_orig);
114                 if (l > 0) {
115                     return _point + _radius * v_orig/l; // Length of _direction is equal to the radius
116                 } else {
117                     // point to be projected is exactly at the center of the circle, so any point on the circle is a projection
118                     return _point + Geom::Point(_radius, 0);
119                 }
120             } else if (_type != UNDEFINED){
121                 // project on to a linear constraint
122                 Geom::Point const p1_on_cl = (_type == LINE) ? _point : p;
123                 Geom::Point const p2_on_cl = p1_on_cl + _direction;
124                 return Geom::projection(p, Geom::Line(p1_on_cl, p2_on_cl));
125             } else {
126                 g_warning("Bug: trying to find the projection onto an undefined constraint");
127                 return Geom::Point();
128             }
129         }
131     private:
132         Geom::Point _point;
133         Geom::Point _direction;
134         Geom::Coord _radius;
135         SnapConstraintType _type;
136     };
138     virtual void constrainedSnap(SnappedConstraints &/*sc*/,
139                                  Inkscape::SnapCandidatePoint const &/*p*/,
140                                  Geom::OptRect const &/*bbox_to_snap*/,
141                                  SnapConstraint const &/*c*/,
142                                  std::vector<SPItem const *> const */*it*/,
143                                  std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
145 protected:
146     SnapManager *_snapmanager;
148     // This is only used for grids, for which snapping can be enabled individually
149     bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
150     bool _snap_visible_only;
151 };
155 #endif /* !SEEN_SNAPPER_H */
157 /*
158   Local Variables:
159   mode:c++
160   c-file-style:"stroustrup"
161   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
162   indent-tabs-mode:nil
163   fill-column:99
164   End:
165 */
166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :