24cfefe0ba53024633648f31a275a5e41d597560
1 #define __SP_DESKTOP_SNAP_C__
3 /**
4 * \file snap.cpp
5 * \brief SnapManager class.
6 *
7 * Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Frank Felfe <innerspace@iname.com>
10 * Nathan Hurst <njh@njhurst.com>
11 * Carl Hetherington <inkscape@carlh.net>
12 * Diederik van Lierop <mail@diedenrezi.nl>
13 *
14 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15 * Copyrigth (C) 2004 Nathan Hurst
16 * Copyright (C) 1999-2008 Authors
17 *
18 * Released under GNU GPL, read the file 'COPYING' for more information
19 */
21 #include <utility>
23 #include "sp-namedview.h"
24 #include "snap.h"
25 #include "snapped-line.h"
26 #include "snapped-curve.h"
28 #include <libnr/nr-point-fns.h>
29 #include <libnr/nr-scale-ops.h>
30 #include <libnr/nr-values.h>
32 #include "display/canvas-grid.h"
33 #include "display/snap-indicator.h"
35 #include "inkscape.h"
36 #include "desktop.h"
37 #include "sp-guide.h"
38 #include "preferences.h"
39 using std::vector;
41 /**
42 * Construct a SnapManager for a SPNamedView.
43 *
44 * \param v `Owning' SPNamedView.
45 */
47 SnapManager::SnapManager(SPNamedView const *v) :
48 guide(this, 0),
49 object(this, 0),
50 snapprefs(),
51 _named_view(v)
52 {
53 }
55 /**
56 * \return List of snappers that we use.
57 */
58 SnapManager::SnapperList
59 SnapManager::getSnappers() const
60 {
61 SnapManager::SnapperList s;
62 s.push_back(&guide);
63 s.push_back(&object);
65 SnapManager::SnapperList gs = getGridSnappers();
66 s.splice(s.begin(), gs);
68 return s;
69 }
71 /**
72 * \return List of gridsnappers that we use.
73 */
74 SnapManager::SnapperList
75 SnapManager::getGridSnappers() const
76 {
77 SnapperList s;
79 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
80 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
81 if (desktop && desktop->gridsEnabled()) {
82 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
83 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
84 s.push_back(grid->snapper);
85 }
86 }
88 return s;
89 }
91 /**
92 * \return true if one of the snappers will try to snap something.
93 */
95 bool SnapManager::someSnapperMightSnap() const
96 {
97 if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
98 return false;
99 }
101 SnapperList const s = getSnappers();
102 SnapperList::const_iterator i = s.begin();
103 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
104 i++;
105 }
107 return (i != s.end());
108 }
110 /**
111 * \return true if one of the snappers will try to snap something.
112 */
114 bool SnapManager::gridSnapperMightSnap() const
115 {
116 if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
117 return false;
118 }
120 SnapperList const s = getGridSnappers();
121 SnapperList::const_iterator i = s.begin();
122 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
123 i++;
124 }
126 return (i != s.end());
127 }
129 /**
130 * Try to snap a point to any of the specified snappers.
131 *
132 * \param point_type Type of point.
133 * \param p Point.
134 * \param first_point If true then this point is the first one from a whole bunch of points
135 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
136 * \param snappers List of snappers to try to snap to
137 * \return Snapped point.
138 */
140 void SnapManager::freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
141 Geom::Point &p,
142 bool first_point,
143 Geom::OptRect const &bbox_to_snap) const
144 {
145 Inkscape::SnappedPoint const s = freeSnap(point_type, p, first_point, bbox_to_snap);
146 s.getPoint(p);
147 }
149 /**
150 * Try to snap a point to any of the specified snappers.
151 *
152 * \param point_type Type of point.
153 * \param p Point.
154 * \param first_point If true then this point is the first one from a whole bunch of points
155 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
156 * \param snappers List of snappers to try to snap to
157 * \return Snapped point.
158 */
160 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapPreferences::PointType point_type,
161 Geom::Point const &p,
162 bool first_point,
163 Geom::OptRect const &bbox_to_snap) const
164 {
165 if (!someSnapperMightSnap()) {
166 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
167 }
169 std::vector<SPItem const *> *items_to_ignore;
170 if (_item_to_ignore) { // If we have only a single item to ignore
171 // then build a list containing this single item;
172 // This single-item list will prevail over any other _items_to_ignore list, should that exist
173 items_to_ignore = new std::vector<SPItem const *>;
174 items_to_ignore->push_back(_item_to_ignore);
175 } else {
176 items_to_ignore = _items_to_ignore;
177 }
179 SnappedConstraints sc;
180 SnapperList const snappers = getSnappers();
182 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
183 (*i)->freeSnap(sc, point_type, p, first_point, bbox_to_snap, items_to_ignore, _unselected_nodes);
184 }
186 if (_item_to_ignore) {
187 delete items_to_ignore;
188 }
190 return findBestSnap(p, sc, false);
191 }
193 // When pasting, we would like to snap to the grid. Problem is that we don't know which nodes were
194 // aligned to the grid at the time of copying, so we don't know which nodes to snap. If we'd snap an
195 // unaligned node to the grid, previously aligned nodes would become unaligned. That's undesirable.
196 // Instead we will make sure that the offset between the source and the copy is a multiple of the grid
197 // pitch. If the source was aligned, then the copy will therefore also be aligned
198 // PS: Wether we really find a multiple also depends on the snapping range!
199 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
200 {
201 if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
202 return t;
204 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
205 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
207 if (desktop && desktop->gridsEnabled()) {
208 bool success = false;
209 Geom::Point nearest_multiple;
210 Geom::Coord nearest_distance = NR_HUGE;
212 // It will snap to the grid for which we find the closest snap. This might be a different
213 // grid than to which the objects were initially aligned. I don't see an easy way to fix
214 // this, so when using multiple grids one can get unexpected results
216 // Cannot use getGridSnappers() because we need both the grids AND their snappers
217 // Therefor we iterate through all grids manually
218 for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
219 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
220 const Inkscape::Snapper* snapper = grid->snapper;
221 if (snapper && snapper->ThisSnapperMightSnap()) {
222 // To find the nearest multiple of the grid pitch for a given translation t, we
223 // will use the grid snapper. Simply snapping the value t to the grid will do, but
224 // only if the origin of the grid is at (0,0). If it's not then compensate for this
225 // in the translation t
226 Geom::Point const t_offset = t + grid->origin;
227 SnappedConstraints sc;
228 // Only the first three parameters are being used for grid snappers
229 snapper->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_NODE, t_offset, TRUE, Geom::OptRect(), NULL, NULL);
230 // Find the best snap for this grid, including intersections of the grid-lines
231 Inkscape::SnappedPoint s = findBestSnap(t_offset, sc, false);
232 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
233 // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
234 // doesn't tell us anything about which node to snap
235 success = true;
236 nearest_multiple = s.getPoint() - to_2geom(grid->origin);
237 nearest_distance = s.getSnapDistance();
238 }
239 }
240 }
242 if (success)
243 return nearest_multiple;
244 }
246 return t;
247 }
249 /**
250 * Try to snap a point to any interested snappers. A snap will only occur along
251 * a line described by a Inkscape::Snapper::ConstraintLine.
252 *
253 * \param point_type Type of point.
254 * \param p Point.
255 * \param first_point If true then this point is the first one from a whole bunch of points
256 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
257 * \param constraint Constraint line.
258 * \return Snapped point.
259 */
261 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
262 Geom::Point &p,
263 Inkscape::Snapper::ConstraintLine const &constraint,
264 bool first_point,
265 Geom::OptRect const &bbox_to_snap) const
266 {
267 Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, constraint, first_point, bbox_to_snap);
268 s.getPoint(p);
269 }
271 /**
272 * Try to snap a point to any interested snappers. A snap will only occur along
273 * a line described by a Inkscape::Snapper::ConstraintLine.
274 *
275 * \param point_type Type of point.
276 * \param p Point.
277 * \param first_point If true then this point is the first one from a whole bunch of points
278 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
279 * \param constraint Constraint line.
280 * \return Snapped point.
281 */
283 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
284 Geom::Point const &p,
285 Inkscape::Snapper::ConstraintLine const &constraint,
286 bool first_point,
287 Geom::OptRect const &bbox_to_snap) const
288 {
289 if (!someSnapperMightSnap()) {
290 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
291 }
293 std::vector<SPItem const *> *items_to_ignore;
294 if (_item_to_ignore) { // If we have only a single item to ignore
295 // then build a list containing this single item;
296 // This single-item list will prevail over any other _items_to_ignore list, should that exist
297 items_to_ignore = new std::vector<SPItem const *>;
298 items_to_ignore->push_back(_item_to_ignore);
299 } else {
300 items_to_ignore = _items_to_ignore;
301 }
303 SnappedConstraints sc;
304 SnapperList const snappers = getSnappers();
305 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
306 (*i)->constrainedSnap(sc, point_type, p, first_point, bbox_to_snap, constraint, items_to_ignore);
307 }
309 if (_item_to_ignore) {
310 delete items_to_ignore;
311 }
313 return findBestSnap(p, sc, true);
314 }
316 void SnapManager::guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const
317 {
318 // This method is used to snap a guide to nodes, while dragging the guide around
320 if ( !(object.GuidesMightSnap() && snapprefs.getSnapEnabledGlobally()) || snapprefs.getSnapPostponedGlobally() ) {
321 return;
322 }
324 SnappedConstraints sc;
325 object.guideSnap(sc, p, guide_normal);
327 Inkscape::SnappedPoint const s = findBestSnap(p, sc, false);
328 s.getPoint(p);
329 }
332 /**
333 * Main internal snapping method, which is called by the other, friendlier, public
334 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
335 * of duplicated code.
336 *
337 * \param type Type of points being snapped.
338 * \param points List of points to snap (i.e. untransformed).
339 * \param pointer Location of the mouse pointer, at the time when dragging started (i.e. "untransformed")
340 * \param constrained true if the snap is constrained.
341 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
342 * \param transformation_type Type of transformation to apply to points before trying to snap them.
343 * \param transformation Description of the transformation; details depend on the type.
344 * \param origin Origin of the transformation, if applicable.
345 * \param dim Dimension of the transformation, if applicable.
346 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
347 */
349 Inkscape::SnappedPoint SnapManager::_snapTransformed(
350 Inkscape::SnapPreferences::PointType type,
351 std::vector<Geom::Point> const &points,
352 Geom::Point const &pointer,
353 bool constrained,
354 Inkscape::Snapper::ConstraintLine const &constraint,
355 Transformation transformation_type,
356 Geom::Point const &transformation,
357 Geom::Point const &origin,
358 Geom::Dim2 dim,
359 bool uniform) const
360 {
361 /* We have a list of points, which we are proposing to transform in some way. We need to see
362 ** if any of these points, when transformed, snap to anything. If they do, we return the
363 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
364 */
366 /* Quick check to see if we have any snappers that are enabled
367 ** Also used to globally disable all snapping
368 */
369 if (someSnapperMightSnap() == false) {
370 return Inkscape::SnappedPoint();
371 }
373 std::vector<Geom::Point> transformed_points;
374 Geom::Rect bbox;
376 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
378 /* Work out the transformed version of this point */
379 Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
381 // add the current transformed point to the box hulling all transformed points
382 if (i == points.begin()) {
383 bbox = Geom::Rect(transformed, transformed);
384 } else {
385 bbox.expandTo(transformed);
386 }
388 transformed_points.push_back(transformed);
389 }
391 /* The current best transformation */
392 Geom::Point best_transformation = transformation;
394 /* The current best metric for the best transformation; lower is better, NR_HUGE
395 ** means that we haven't snapped anything.
396 */
397 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
398 Inkscape::SnappedPoint best_snapped_point;
399 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
400 g_assert(best_snapped_point.getAtIntersection() == false);
402 std::vector<Geom::Point>::const_iterator j = transformed_points.begin();
404 // std::cout << std::endl;
405 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
407 /* Snap it */
408 Inkscape::SnappedPoint snapped_point;
409 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
410 Geom::Point const b = (*i - origin); // vector to original point
412 if (constrained) {
413 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
414 // When uniformly scaling, each point will have its own unique constraint line,
415 // running from the scaling origin to the original untransformed point. We will
416 // calculate that line here
417 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
418 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
419 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
420 } else if (transformation_type == TRANSLATION) {
421 // When doing a constrained translation, all points will move in the same direction, i.e.
422 // either horizontally or vertically. The lines along which they move are therefore all
423 // parallel, but might not be colinear. Therefore we will have to set the point through
424 // which the constraint-line runs here, for each point individually.
425 dedicated_constraint.setPoint(*i);
426 } // else: leave the original constraint, e.g. for skewing
427 if (transformation_type == SCALE && !uniform) {
428 g_warning("Non-uniform constrained scaling is not supported!");
429 }
430 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
431 } else {
432 bool const c1 = fabs(b[Geom::X]) < 1e-6;
433 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
434 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
435 // When scaling, a point aligned either horizontally or vertically with the origin can only
436 // move in that specific direction; therefore it should only snap in that direction, otherwise
437 // we will get snapped points with an invalid transformation
438 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
439 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
440 } else {
441 snapped_point = freeSnap(type, *j, i == points.begin(), bbox);
442 }
443 }
444 // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
445 snapped_point.setPointerDistance(Geom::L2(pointer - *i));
447 Geom::Point result;
448 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
450 if (snapped_point.getSnapped()) {
451 /* We snapped. Find the transformation that describes where the snapped point has
452 ** ended up, and also the metric for this transformation.
453 */
454 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
455 //Geom::Point const b = (*i - origin); // vector to original point
457 switch (transformation_type) {
458 case TRANSLATION:
459 result = snapped_point.getPoint() - *i;
460 /* Consider the case in which a box is almost aligned with a grid in both
461 * horizontal and vertical directions. The distance to the intersection of
462 * the grid lines will always be larger then the distance to a single grid
463 * line. If we prefer snapping to an intersection instead of to a single
464 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
465 * snapped distance will be used as a metric. Please note that the snapped
466 * distance is defined as the distance to the nearest line of the intersection,
467 * and not to the intersection itself!
468 */
469 // Only for translations, the relevant metric will be the real snapped distance,
470 // so we don't have to do anything special here
471 break;
472 case SCALE:
473 {
474 result = Geom::Point(NR_HUGE, NR_HUGE);
475 // If this point *i is horizontally or vertically aligned with
476 // the origin of the scaling, then it will scale purely in X or Y
477 // We can therefore only calculate the scaling in this direction
478 // and the scaling factor for the other direction should remain
479 // untouched (unless scaling is uniform ofcourse)
480 for (int index = 0; index < 2; index++) {
481 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
482 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
483 result[index] = a[index] / b[index]; // then calculate it!
484 }
485 // we might leave result[1-index] = NR_HUGE
486 // if scaling didn't occur in the other direction
487 }
488 }
489 // Compare the resulting scaling with the desired scaling
490 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
491 break;
492 }
493 case STRETCH:
494 result = Geom::Point(NR_HUGE, NR_HUGE);
495 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
496 result[dim] = a[dim] / b[dim];
497 result[1-dim] = uniform ? result[dim] : 1;
498 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
499 if (uniform && fabs(b[1-dim]) > 1e-6) {
500 result[1-dim] = a[1-dim] / b[1-dim];
501 result[dim] = result[1-dim];
502 }
503 }
504 // Store the metric for this transformation as a virtual distance
505 snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
506 snapped_point.setSecondSnapDistance(NR_HUGE);
507 break;
508 case SKEW:
509 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
510 result[1] = transformation[1]; // scale factor
511 // Store the metric for this transformation as a virtual distance
512 snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
513 snapped_point.setSecondSnapDistance(NR_HUGE);
514 break;
515 default:
516 g_assert_not_reached();
517 }
519 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
520 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
521 // the scaling metric differently
522 if (transformation_type == SCALE) {
523 for (int index = 0; index < 2; index++) {
524 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
525 best_transformation[index] = result[index];
526 best_scale_metric[index] = fabs(scale_metric[index]);
527 // When scaling, we're considering the best transformation in each direction separately
528 // Therefore two different snapped points might together make a single best transformation
529 // We will however return only a single snapped point (e.g. to display the snapping indicator)
530 best_snapped_point = snapped_point;
531 // std::cout << "SEL ";
532 } // else { std::cout << " ";}
533 }
534 if (uniform) {
535 if (best_scale_metric[0] < best_scale_metric[1]) {
536 best_transformation[1] = best_transformation[0];
537 best_scale_metric[1] = best_scale_metric[0];
538 } else {
539 best_transformation[0] = best_transformation[1];
540 best_scale_metric[0] = best_scale_metric[1];
541 }
542 }
543 } else { // For all transformations other than scaling
544 if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
545 best_transformation = result;
546 best_snapped_point = snapped_point;
547 }
548 }
549 }
551 j++;
552 }
554 Geom::Coord best_metric;
555 if (transformation_type == SCALE) {
556 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
557 for (int index = 0; index < 2; index++) {
558 if (best_transformation[index] == NR_HUGE) {
559 if (uniform && best_transformation[1-index] < NR_HUGE) {
560 best_transformation[index] = best_transformation[1-index];
561 } else {
562 best_transformation[index] = transformation[index];
563 }
564 }
565 }
566 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
567 } else { // For all transformations other than scaling
568 best_metric = best_snapped_point.getSnapDistance();
569 }
571 best_snapped_point.setTransformation(best_transformation);
572 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
573 // These rounding errors might be caused by NRRects, see bug #1584301
574 best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
575 return best_snapped_point;
576 }
579 /**
580 * Try to snap a list of points to any interested snappers after they have undergone
581 * a translation.
582 *
583 * \param point_type Type of points.
584 * \param p Points.
585 * \param tr Proposed translation.
586 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
587 */
589 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
590 std::vector<Geom::Point> const &p,
591 Geom::Point const &pointer,
592 Geom::Point const &tr) const
593 {
594 if (p.size() == 1) {
595 _displaySnapsource(point_type, _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false));
596 }
598 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
599 }
602 /**
603 * Try to snap a list of points to any interested snappers after they have undergone a
604 * translation. A snap will only occur along a line described by a
605 * Inkscape::Snapper::ConstraintLine.
606 *
607 * \param point_type Type of points.
608 * \param p Points.
609 * \param constraint Constraint line.
610 * \param tr Proposed translation.
611 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
612 */
614 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
615 std::vector<Geom::Point> const &p,
616 Geom::Point const &pointer,
617 Inkscape::Snapper::ConstraintLine const &constraint,
618 Geom::Point const &tr) const
619 {
620 if (p.size() == 1) {
621 _displaySnapsource(point_type, _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false));
622 }
624 return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
625 }
628 /**
629 * Try to snap a list of points to any interested snappers after they have undergone
630 * a scale.
631 *
632 * \param point_type Type of points.
633 * \param p Points.
634 * \param s Proposed scale.
635 * \param o Origin of proposed scale.
636 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
637 */
639 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
640 std::vector<Geom::Point> const &p,
641 Geom::Point const &pointer,
642 Geom::Scale const &s,
643 Geom::Point const &o) const
644 {
645 if (p.size() == 1) {
646 _displaySnapsource(point_type, _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false));
647 }
649 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
650 }
653 /**
654 * Try to snap a list of points to any interested snappers after they have undergone
655 * a scale. A snap will only occur along a line described by a
656 * Inkscape::Snapper::ConstraintLine.
657 *
658 * \param point_type Type of points.
659 * \param p Points.
660 * \param s Proposed scale.
661 * \param o Origin of proposed scale.
662 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
663 */
665 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
666 std::vector<Geom::Point> const &p,
667 Geom::Point const &pointer,
668 Geom::Scale const &s,
669 Geom::Point const &o) const
670 {
671 // When constrained scaling, only uniform scaling is supported.
672 if (p.size() == 1) {
673 _displaySnapsource(point_type, _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true));
674 }
676 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
677 }
680 /**
681 * Try to snap a list of points to any interested snappers after they have undergone
682 * a stretch.
683 *
684 * \param point_type Type of points.
685 * \param p Points.
686 * \param s Proposed stretch.
687 * \param o Origin of proposed stretch.
688 * \param d Dimension in which to apply proposed stretch.
689 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
690 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
691 */
693 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
694 std::vector<Geom::Point> const &p,
695 Geom::Point const &pointer,
696 Geom::Coord const &s,
697 Geom::Point const &o,
698 Geom::Dim2 d,
699 bool u) const
700 {
701 if (p.size() == 1) {
702 _displaySnapsource(point_type, _transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u));
703 }
705 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
706 }
709 /**
710 * Try to snap a list of points to any interested snappers after they have undergone
711 * a skew.
712 *
713 * \param point_type Type of points.
714 * \param p Points.
715 * \param s Proposed skew.
716 * \param o Origin of proposed skew.
717 * \param d Dimension in which to apply proposed skew.
718 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
719 */
721 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
722 std::vector<Geom::Point> const &p,
723 Geom::Point const &pointer,
724 Inkscape::Snapper::ConstraintLine const &constraint,
725 Geom::Point const &s,
726 Geom::Point const &o,
727 Geom::Dim2 d) const
728 {
729 // "s" contains skew factor in s[0], and scale factor in s[1]
731 // Snapping the nodes of the boundingbox of a selection that is being transformed, will only work if
732 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
733 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
734 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
735 // of bounding boxes is not allowed here.
736 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
738 if (p.size() == 1) {
739 _displaySnapsource(point_type, _transformPoint(p.at(0), SKEW, s, o, d, false));
740 }
742 return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
743 }
745 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const
746 {
748 /*
749 std::cout << "Type and number of snapped constraints: " << std::endl;
750 std::cout << " Points : " << sc.points.size() << std::endl;
751 std::cout << " Lines : " << sc.lines.size() << std::endl;
752 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
753 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
754 std::cout << " Curves : " << sc.curves.size()<< std::endl;
755 */
757 // Store all snappoints
758 std::list<Inkscape::SnappedPoint> sp_list;
760 // search for the closest snapped point
761 Inkscape::SnappedPoint closestPoint;
762 if (getClosestSP(sc.points, closestPoint)) {
763 sp_list.push_back(closestPoint);
764 }
766 // search for the closest snapped curve
767 Inkscape::SnappedCurve closestCurve;
768 if (getClosestCurve(sc.curves, closestCurve)) {
769 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
770 }
772 if (snapprefs.getSnapIntersectionCS()) {
773 // search for the closest snapped intersection of curves
774 Inkscape::SnappedPoint closestCurvesIntersection;
775 if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection)) {
776 sp_list.push_back(closestCurvesIntersection);
777 }
778 }
780 // search for the closest snapped grid line
781 Inkscape::SnappedLine closestGridLine;
782 if (getClosestSL(sc.grid_lines, closestGridLine)) {
783 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
784 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
785 }
787 // search for the closest snapped guide line
788 Inkscape::SnappedLine closestGuideLine;
789 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
790 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
791 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
792 }
794 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
795 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
797 // When doing a constrained snap however, we're already at an intersection of the constrained line and
798 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
799 // no need to look for additional intersections
800 if (!constrained) {
801 // search for the closest snapped intersection of grid lines
802 Inkscape::SnappedPoint closestGridPoint;
803 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
804 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
805 sp_list.push_back(closestGridPoint);
806 }
808 // search for the closest snapped intersection of guide lines
809 Inkscape::SnappedPoint closestGuidePoint;
810 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
811 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
812 sp_list.push_back(closestGuidePoint);
813 }
815 // search for the closest snapped intersection of grid with guide lines
816 if (snapprefs.getSnapIntersectionGG()) {
817 Inkscape::SnappedPoint closestGridGuidePoint;
818 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
819 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
820 sp_list.push_back(closestGridGuidePoint);
821 }
822 }
823 }
825 // now let's see which snapped point gets a thumbs up
826 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
827 // std::cout << "Finding the best snap..." << std::endl;
828 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
829 // first find out if this snapped point is within snapping range
830 // std::cout << "sp = " << from_2geom((*i).getPoint());
831 if ((*i).getSnapDistance() <= (*i).getTolerance()) {
832 // if it's the first point, or if it is closer than the best snapped point so far
833 if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
834 // then prefer this point over the previous one
835 bestSnappedPoint = *i;
836 }
837 }
838 // std::cout << std::endl;
839 }
841 // Update the snap indicator, if requested
842 if (_snapindicator) {
843 if (bestSnappedPoint.getSnapped()) {
844 _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
845 } else {
846 _desktop->snapindicator->remove_snaptarget();
847 }
848 }
850 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
851 return bestSnappedPoint;
852 }
854 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, SPItem const *item_to_ignore, std::vector<Geom::Point> *unselected_nodes)
855 {
856 g_assert(desktop != NULL);
857 _item_to_ignore = item_to_ignore;
858 _items_to_ignore = NULL;
859 _desktop = desktop;
860 _snapindicator = snapindicator;
861 _unselected_nodes = unselected_nodes;
862 }
864 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes)
865 {
866 g_assert(desktop != NULL);
867 _item_to_ignore = NULL;
868 _items_to_ignore = &items_to_ignore;
869 _desktop = desktop;
870 _snapindicator = snapindicator;
871 _unselected_nodes = unselected_nodes;
872 }
874 SPDocument *SnapManager::getDocument() const
875 {
876 return _named_view->document;
877 }
879 Geom::Point SnapManager::_transformPoint(Geom::Point const &p,
880 Transformation const transformation_type,
881 Geom::Point const &transformation,
882 Geom::Point const &origin,
883 Geom::Dim2 const dim,
884 bool const uniform) const
885 {
886 /* Work out the transformed version of this point */
887 Geom::Point transformed;
888 switch (transformation_type) {
889 case TRANSLATION:
890 transformed = p + transformation;
891 break;
892 case SCALE:
893 transformed = (p - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
894 break;
895 case STRETCH:
896 {
897 Geom::Scale s(1, 1);
898 if (uniform)
899 s[Geom::X] = s[Geom::Y] = transformation[dim];
900 else {
901 s[dim] = transformation[dim];
902 s[1 - dim] = 1;
903 }
904 transformed = ((p - origin) * s) + origin;
905 break;
906 }
907 case SKEW:
908 // Apply the skew factor
909 transformed[dim] = p[dim] + transformation[0] * (p[1 - dim] - origin[1 - dim]);
910 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
911 // Apply that scale factor here
912 transformed[1-dim] = (p - origin)[1 - dim] * transformation[1] + origin[1 - dim];
913 break;
914 default:
915 g_assert_not_reached();
916 }
918 return transformed;
919 }
921 void SnapManager::_displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Geom::Point const &p) const {
923 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
924 if (prefs->getBool("/options/snapclosestonly/value")) {
925 bool p_is_a_node = point_type & Inkscape::SnapPreferences::SNAPPOINT_NODE;
926 bool p_is_a_bbox = point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
927 if ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox())) {
928 _desktop->snapindicator->set_new_snapsource(p);
929 } else {
930 _desktop->snapindicator->remove_snapsource();
931 }
932 }
933 }
935 /*
936 Local Variables:
937 mode:c++
938 c-file-style:"stroustrup"
939 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
940 indent-tabs-mode:nil
941 fill-column:99
942 End:
943 */
944 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :