Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
grid-snapper.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2022 Authors
8 * Copyright (C) Johan Engelen 2006-2007 <johan@shouraizou.nl>
9 * Copyright (C) Lauris Kaplinski 2000
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12#include "grid-snapper.h"
13
14#include "desktop.h"
15#include "preferences.h"
16#include "helper/mathfns.h"
17#include "object/sp-grid.h"
18#include "object/sp-namedview.h"
19
20static int calculate_scaling_factor(double length, int major)
21{
22 int multiply = 1;
23 int step = std::max(major, 1);
24 int watchdog = 0;
25
26 while (length * multiply < 8.0 && watchdog < 100) {
27 multiply *= step;
28 // First pass, go up to the major line spacing, then keep increasing by two.
29 step = 2;
30 watchdog++;
31 }
32
33 return multiply;
34}
35
36// Project a vector onto the given axis.
37static auto proj(Geom::Point const &p, int dim)
38{
39 return dim == 0
40 ? Geom::Point(p.x(), 0.0)
41 : Geom::Point(0.0, p.y());
42}
43
44// Return the unit vector along the given axis.
45static auto basis(int dim)
46{
47 return dim == 0
48 ? Geom::Point(1.0, 0.0)
49 : Geom::Point(0.0, 1.0);
50}
51
52namespace Inkscape {
53
55 : LineSnapper(sm, d)
56 , _grid(grid)
57{
58}
59
64{
65 SPDesktop const *dt = _snapmanager->getDesktop();
66 double const zoom = dt ? dt->current_zoom() : 1;
68}
69
71{
72 return Preferences::get()->getBool("/options/snap/grid/always", false);
73}
74
76{
78 return {};
79 }
80
81 switch (_grid->getType()) {
82 case GridType::RECTANGULAR: return get_snap_lines(p, 0);
84 case GridType::MODULAR: return get_snap_lines(p, 4);
85 default: g_assert_not_reached(); return {};
86 }
87}
88
89void GridSnapper::_addSnappedLine(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, Geom::Point const &normal_to_line, Geom::Point const &point_on_line) const
90{
91 isr.grid_lines.emplace_back(snapped_point, snapped_distance, source, source_num, SNAPTARGET_GRID, getSnapperTolerance(), getSnapperAlwaysSnap(source), normal_to_line, point_on_line);
92}
93
94void GridSnapper::_addSnappedPoint(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const
95{
96 isr.points.emplace_back(snapped_point, source, source_num, SNAPTARGET_GRID, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(source), constrained_snap, true);
97}
98
99void GridSnapper::_addSnappedLinePerpendicularly(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const
100{
101 isr.points.emplace_back(snapped_point, source, source_num, SNAPTARGET_GRID_PERPENDICULAR, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(source), constrained_snap, true);
102}
103
111
113 LineList s;
114
115 const auto desktop = _snapmanager->getDesktop();
116 const int start = limit > 0 ? 0 : -1;
117
118 for (int index = start; index < limit; ++index) {
119 const auto [origin, spacing] = _grid->getEffectiveOriginAndSpacing(index);
120 // empty spacing - skip this element
121 if (spacing.isZero()) continue;;
122
123 for (int i = 0; i < 2; ++i) {
124 double scaled_spacing = spacing[i];
125
126 if (getSnapVisibleOnly() && desktop) {
127 // Only snap to visible grid lines.
128 auto const sw = proj(spacing, i) * desktop->d2w().withoutTranslation();
129 int const mult = calculate_scaling_factor(sw.length(), _grid->getMajorLineInterval());
130 scaled_spacing *= mult;
131 }
132
133 s.emplace_back(basis(i), basis(i) * Util::round_to_upper_multiple_plus(p[i], scaled_spacing, origin[i]));
134 s.emplace_back(basis(i), basis(i) * Util::round_to_lower_multiple_plus(p[i], scaled_spacing, origin[i]));
135 }
136 }
137
138 return s;
139}
140
142{
143 LineList s;
144
145 auto const *desktop = _snapmanager->getDesktop();
146 auto const [origin, spacing] = _grid->getEffectiveOriginAndSpacing();
147
148 double ta_x = tan(Geom::rad_from_deg(_grid->getAngleX()));
149 double ta_z = tan(Geom::rad_from_deg(_grid->getAngleZ()));
150
151 if (desktop && desktop->is_yaxisdown()) {
152 std::swap(ta_x, ta_z);
153 }
154
155 double spacing_h = spacing.y() / (ta_x + ta_z);
156 double spacing_v = spacing.y();
157
158 if (getSnapVisibleOnly() && desktop) {
159 // Only snap to visible grid lines.
160 auto const lyw = spacing.y() * desktop->d2w().descrim();
161 int const mult = calculate_scaling_factor(lyw, _grid->getMajorLineInterval());
162 spacing_h *= mult;
163 spacing_v *= mult;
164 }
165
166 // In an axonometric grid, any point will be surrounded by 6 grid lines:
167 // - 2 vertical grid lines, one left and one right from the point
168 // - 2 angled z grid lines, one above and one below the point
169 // - 2 angled x grid lines, one above and one below the point
170
171 // Calculate the x coordinate of the vertical grid lines
174
175 // Calculate the y coordinate of the intersection of the angled grid lines with the y-axis
176 double y_proj_along_z = p[Geom::Y] - ta_z * (p[Geom::X] - origin[Geom::X]);
177 double y_proj_along_x = p[Geom::Y] + ta_x * (p[Geom::X] - origin[Geom::X]);
178 double y_proj_along_z_max = Util::round_to_upper_multiple_plus(y_proj_along_z, spacing_v, origin[Geom::Y]);
179 double y_proj_along_z_min = Util::round_to_lower_multiple_plus(y_proj_along_z, spacing_v, origin[Geom::Y]);
180 double y_proj_along_x_max = Util::round_to_upper_multiple_plus(y_proj_along_x, spacing_v, origin[Geom::Y]);
181 double y_proj_along_x_min = Util::round_to_lower_multiple_plus(y_proj_along_x, spacing_v, origin[Geom::Y]);
182
183 // Calculate the versor for the angled grid lines
184 Geom::Point vers_x = Geom::Point(1, -ta_x);
185 Geom::Point vers_z = Geom::Point(1, ta_z);
186
187 // Calculate the normal for the angled grid lines
188 Geom::Point norm_x = Geom::rot90(vers_x);
189 Geom::Point norm_z = Geom::rot90(vers_z);
190
191 // The four angled grid lines form a parallelogram, enclosing the point
192 // One of the two vertical grid lines divides this parallelogram in two triangles
193 // We will now try to find out in which half (i.e. triangle) our point is, and return
194 // only the three grid lines defining that triangle
195
196 // The vertical grid line is at the intersection of two angled grid lines.
197 // Now go find that intersection!
198 Geom::Point p_x(0, y_proj_along_x_max);
199 Geom::Line line_x(p_x, p_x + vers_x);
200 Geom::Point p_z(0, y_proj_along_z_max);
201 Geom::Line line_z(p_z, p_z + vers_z);
202
203 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
204 try
205 {
206 inters = Geom::intersection(line_x, line_z);
207 }
208 catch (Geom::InfiniteSolutions &e)
209 {
210 // We're probably dealing with parallel lines; this is useless!
211 return s;
212 }
213
214 // Determine which half of the parallelogram to use
215 bool use_left_half = true;
216 bool use_right_half = true;
217
218 if (inters) {
219 Geom::Point inters_pt = line_x.pointAt((*inters).ta);
220 use_left_half = (p[Geom::X] - origin[Geom::X]) < inters_pt[Geom::X];
221 use_right_half = !use_left_half;
222 }
223
224 // Return the three grid lines which define the triangle that encloses our point
225 // If we didn't find an intersection above, all 6 grid lines will be returned
226 if (use_left_half) {
227 s.emplace_back(norm_z, Geom::Point(origin[Geom::X], y_proj_along_z_max));
228 s.emplace_back(norm_x, Geom::Point(origin[Geom::X], y_proj_along_x_min));
229 s.emplace_back(Geom::Point(1, 0), Geom::Point(x_max, 0));
230 }
231
232 if (use_right_half) {
233 s.emplace_back(norm_z, Geom::Point(origin[Geom::X], y_proj_along_z_min));
234 s.emplace_back(norm_x, Geom::Point(origin[Geom::X], y_proj_along_x_max));
235 s.emplace_back(Geom::Point(1, 0), Geom::Point(x_min, 0));
236 }
237
238 return s;
239}
240
241} // namepsace Inkscape
Point origin
Definition aa.cpp:227
static int calculate_scaling_factor(double length, int major)
Coord descrim() const
Calculate the descriminant.
Definition affine.cpp:434
Affine withoutTranslation() const
Definition affine.h:169
Infinite line on a plane.
Definition line.h:53
Point pointAt(Coord t) const
Definition line.h:231
Two-dimensional point that doubles as a vector.
Definition point.h:66
constexpr Coord y() const noexcept
Definition point.h:106
constexpr Coord x() const noexcept
Definition point.h:104
LineList get_snap_lines(const Geom::Point &p, int limit) const
bool getSnapperAlwaysSnap(SnapSourceType const &source) const override
void _addSnappedLinePerpendicularly(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const override
LineList _getSnapLines(Geom::Point const &p) const override
bool ThisSnapperMightSnap() const override
GridSnapper(SPGrid const *grid, SnapManager *sm, Geom::Coord const d)
void _addSnappedPoint(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const override
Geom::Coord getSnapperTolerance() const override
void _addSnappedLine(IntermSnapResults &isr, Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, Geom::Point const &normal_to_line, const Geom::Point &point_on_line) const override
SPGrid const * _grid
LineList getSnapLinesAxonom(Geom::Point const &p) const
Superclass for snappers to horizontal and vertical lines.
std::list< std::pair< Geom::Point, Geom::Point > > LineList
bool getBool(Glib::ustring const &pref_path, bool def=false)
Retrieve a Boolean value.
static Preferences * get()
Access the singleton Preferences object.
bool isTargetSnappable(Inkscape::SnapTargetType const target) const
bool getSnapVisibleOnly() const
Definition snapper.h:57
SnapManager * _snapmanager
Definition snapper.h:146
bool _snap_enabled
true if this snapper is enabled, otherwise false
Definition snapper.h:149
To do: update description of desktop.
Definition desktop.h:149
double current_zoom() const
Definition desktop.h:335
Geom::Affine const & d2w() const
Transformation from desktop to window coordinates.
Definition desktop.h:419
bool is_yaxisdown() const
Definition desktop.h:427
double getAngleX() const
Definition sp-grid.h:88
guint32 getMajorLineInterval() const
Definition sp-grid.h:85
double getAngleZ() const
Definition sp-grid.h:91
std::pair< Geom::Point, Geom::Point > getEffectiveOriginAndSpacing(int index=-1) const
Definition sp-grid.cpp:547
GridType getType() const
Definition sp-grid.h:100
Class to coordinate snapping operations.
Definition snap.h:80
SPNamedView const * getNamedView() const
Definition snap.h:371
Inkscape::SnapPreferences & snapprefs
Definition snap.h:342
SPDesktop const * getDesktop() const
Definition snap.h:370
Editable view implementation.
static auto basis(int dim)
static int calculate_scaling_factor(double length, int major)
static auto proj(Geom::Point const &p, int dim)
Grid Snapper for Rectangular and Axonometric Grids.
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Geom::Point start
std::optional< Crossing > OptCrossing
Definition crossing.h:64
OptCrossing intersection(Ray const &r1, Line const &l2)
Definition line.h:545
D2< T > rot90(D2< T > const &a)
Definition d2.h:397
double round_to_upper_multiple_plus(double x, double const c1, double const c0=0)
Definition mathfns.h:53
double round_to_lower_multiple_plus(double x, double c1, double c0=0.0)
Definition mathfns.h:41
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
@ SNAPTARGET_GRID
Definition snap-enums.h:96
@ SNAPTARGET_GRID_PERPENDICULAR
Definition snap-enums.h:99
Singleton class to access the preferences file in a convenient way.
std::list< Inkscape::SnappedLine > grid_lines
Definition snapper.h:27
std::list< Inkscape::SnappedPoint > points
Definition snapper.h:26
int index
SPDesktop * desktop