Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
snapper.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef SEEN_SNAPPER_H
3#define SEEN_SNAPPER_H
4
16#include <optional>
17#include <cstdio>
18#include <list>
19
20#include "snap-candidate.h"
21#include "snapped-point.h"
22#include "snapped-line.h"
23#include "snapped-curve.h"
24
26 std::list<Inkscape::SnappedPoint> points;
27 std::list<Inkscape::SnappedLine> grid_lines;
28 std::list<Inkscape::SnappedLine> guide_lines;
29 std::list<Inkscape::SnappedCurve> curves;
30};
31
32class SnapManager;
33class SPObject;
34
35namespace Inkscape
36{
39{
40public:
41 //Snapper() {} //does not seem to be used somewhere
42 Snapper(SnapManager *sm, ::Geom::Coord const t);
43 virtual ~Snapper() = default;
44
45 virtual Geom::Coord getSnapperTolerance() const = 0; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
46 virtual bool getSnapperAlwaysSnap(SnapSourceType const &source) const = 0; //if true, then the snapper will always snap, regardless of its tolerance
47
51 virtual bool ThisSnapperMightSnap() const {return _snap_enabled;} // will likely be overridden by derived classes
52
53 // These four methods are only used for grids, for which snapping can be enabled individually
54 void setEnabled(bool s);
55 void setSnapVisibleOnly(bool s);
56 bool getEnabled() const {return _snap_enabled;}
58
59 virtual void freeSnap(IntermSnapResults &/*isr*/,
61 Geom::OptRect const &/*bbox_to_snap*/,
62 std::vector<SPObject const *> const */*it*/,
63 std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
64
65 // Class for storing the constraint for constrained snapping; can be
66 // - a line (infinite line with origin, running through _point pointing in _direction)
67 // - a direction (infinite line without origin, i.e. only a direction vector, stored in _direction)
68 // - a circle (_point denotes the center, _radius doesn't need an explanation, _direction contains
69 // the vector from the origin to the original untransformed point);
71 {
72 private:
74
75 public:
76 // Constructs a direction constraint, e.g. horizontal or vertical but without a specified point
78 // Constructs a linear constraint
80 // Orthogonal version
81 SnapConstraint(Geom::Point const &p, Geom::Dim2 const &d) : _point(p), _direction(), _radius(0), _type(LINE) {_direction[d] = 1.;}
82 SnapConstraint(Geom::Line const &l) : _point(l.origin()), _direction(l.versor()), _radius(0), _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
87
88 bool hasPoint() const {return _type != DIRECTION && _type != UNDEFINED;}
89
91 assert(_type != DIRECTION && _type != UNDEFINED);
92 return _point;
93 }
94
96 return _direction;
97 }
98
100 assert(_type == CIRCLE);
101 return _radius;
102 }
103
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; }
108
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 printf("WARNING: Bug: trying to find the projection onto an undefined constraint");
127 return Geom::Point();
128 }
129 }
130
131 private:
136 };
137
138 virtual void constrainedSnap(IntermSnapResults &/*isr*/,
139 Inkscape::SnapCandidatePoint const &/*p*/,
140 Geom::OptRect const &/*bbox_to_snap*/,
141 SnapConstraint const &/*c*/,
142 std::vector<SPObject const *> const */*it*/,
143 std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
144
145protected:
147
148 // This is only used for grids, for which snapping can be enabled individually
151};
152
153}
154
155#endif /* !SEEN_SNAPPER_H */
156
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 :
Point origin
Definition aa.cpp:227
Infinite line on a plane.
Definition line.h:53
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Two-dimensional point that doubles as a vector.
Definition point.h:66
Class to store data for points which are snap candidates, either as a source or as a target.
Geom::Coord getRadius() const
Definition snapper.h:99
SnapConstraint(Geom::Point const &p, Geom::Point const &d)
Definition snapper.h:79
Geom::Point projection(Geom::Point const &p) const
Definition snapper.h:109
SnapConstraint(Geom::Point const &p, Geom::Dim2 const &d)
Definition snapper.h:81
SnapConstraint(Geom::Point const &p, Geom::Point const &d, Geom::Coord const &r)
Definition snapper.h:84
Geom::Point getDirection() const
Definition snapper.h:95
Geom::Point getPoint() const
Definition snapper.h:90
SnapConstraint(Geom::Point const &d)
Definition snapper.h:77
SnapConstraint(Geom::Line const &l)
Definition snapper.h:82
Parent for classes that can snap points to something.
Definition snapper.h:39
virtual Geom::Coord getSnapperTolerance() const =0
void setEnabled(bool s)
Definition snapper.cpp:34
virtual bool ThisSnapperMightSnap() const
Definition snapper.h:51
virtual bool getSnapperAlwaysSnap(SnapSourceType const &source) const =0
virtual void freeSnap(IntermSnapResults &, Inkscape::SnapCandidatePoint const &, Geom::OptRect const &, std::vector< SPObject const * > const *, std::vector< SnapCandidatePoint > *) const
Definition snapper.h:59
bool _snap_visible_only
Definition snapper.h:150
void setSnapVisibleOnly(bool s)
Definition snapper.cpp:39
virtual ~Snapper()=default
bool getSnapVisibleOnly() const
Definition snapper.h:57
virtual void constrainedSnap(IntermSnapResults &, Inkscape::SnapCandidatePoint const &, Geom::OptRect const &, SnapConstraint const &, std::vector< SPObject const * > const *, std::vector< SnapCandidatePoint > *) const
Definition snapper.h:138
SnapManager * _snapmanager
Definition snapper.h:146
bool getEnabled() const
Definition snapper.h:56
bool _snap_enabled
true if this snapper is enabled, otherwise false
Definition snapper.h:149
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
Class to coordinate snapping operations.
Definition snap.h:80
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Point projection(Point const &p, Line const &line)
Definition line.h:513
SBasis L2(D2< SBasis > const &a, unsigned k)
Definition d2-sbasis.cpp:42
Helper class to stream background task notifications as a series of messages.
SnapSourceType
enumerations of snap source types and snap target types.
Definition snap-enums.h:18
Some utility classes to store various kinds of snap candidates.
SnappedCurve class.
SnappedLine class.
SnappedPoint class.
std::list< Inkscape::SnappedCurve > curves
Definition snapper.h:29
std::list< Inkscape::SnappedLine > grid_lines
Definition snapper.h:27
std::list< Inkscape::SnappedPoint > points
Definition snapper.h:26
std::list< Inkscape::SnappedLine > guide_lines
Definition snapper.h:28