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-2002 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"
27 #include <libnr/nr-point-fns.h>
28 #include <libnr/nr-scale-ops.h>
29 #include <libnr/nr-values.h>
31 #include "display/canvas-grid.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "sp-guide.h"
36 using std::vector;
38 /**
39 * Construct a SnapManager for a SPNamedView.
40 *
41 * \param v `Owning' SPNamedView.
42 */
44 SnapManager::SnapManager(SPNamedView const *v) :
45 guide(v, 0),
46 object(v, 0),
47 _named_view(v),
48 _include_item_center(false),
49 _snap_enabled_globally(true)
50 {
51 }
54 /**
55 * \return List of snappers that we use.
56 */
57 SnapManager::SnapperList
58 SnapManager::getSnappers() const
59 {
60 SnapManager::SnapperList s;
61 s.push_back(&guide);
62 s.push_back(&object);
64 SnapManager::SnapperList gs = getGridSnappers();
65 s.splice(s.begin(), gs);
67 return s;
68 }
70 /**
71 * \return List of gridsnappers that we use.
72 */
73 SnapManager::SnapperList
74 SnapManager::getGridSnappers() const
75 {
76 SnapperList s;
78 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
79 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
80 if (desktop && desktop->gridsEnabled()) {
81 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
82 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
83 s.push_back(grid->snapper);
84 }
85 }
87 return s;
88 }
90 /**
91 * \return true if one of the snappers will try to snap something.
92 */
94 bool SnapManager::SomeSnapperMightSnap() const
95 {
96 if (!_snap_enabled_globally) {
97 return false;
98 }
100 SnapperList const s = getSnappers();
101 SnapperList::const_iterator i = s.begin();
102 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
103 i++;
104 }
106 return (i != s.end());
107 }
109 /*
110 * The snappers have too many parameters to adjust individually. Therefore only
111 * two snapping modes are presented to the user: snapping bounding box corners (to
112 * other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
113 * paths, grids or guides). To select either of these modes (or both), use the
114 * methods defined below: setSnapModeBBox() and setSnapModeNode().
115 *
116 * */
119 void SnapManager::setSnapModeBBox(bool enabled)
120 {
121 //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
122 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
124 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
125 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
126 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
127 }
129 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
130 //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
131 //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
132 object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa
133 }
135 bool SnapManager::getSnapModeBBox() const
136 {
137 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
138 }
140 void SnapManager::setSnapModeNode(bool enabled)
141 {
142 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
144 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
145 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
146 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
147 }
149 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
150 //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
151 //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog
152 object.setStrictSnapping(true);
153 }
155 bool SnapManager::getSnapModeNode() const
156 {
157 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
158 }
160 void SnapManager::setSnapModeGuide(bool enabled)
161 {
162 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
163 }
165 bool SnapManager::getSnapModeGuide() const
166 {
167 return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
168 }
170 /**
171 * Try to snap a point to any interested snappers.
172 *
173 * \param t Type of point.
174 * \param p Point.
175 * \param it Item to ignore when snapping.
176 * \return Snapped point.
177 */
179 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
180 NR::Point const &p,
181 SPItem const *it) const
183 {
184 std::list<SPItem const *> lit;
185 lit.push_back(it);
187 std::vector<NR::Point> points_to_snap;
188 points_to_snap.push_back(p);
190 return freeSnap(t, p, true, points_to_snap, lit, NULL);
191 }
193 /**
194 * Try to snap a point to any interested snappers.
195 *
196 * \param t Type of point.
197 * \param p Point.
198 * \param it Item to ignore when snapping.
199 * \return Snapped point.
200 */
202 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
203 NR::Point const &p,
204 SPItem const *it,
205 std::vector<NR::Point> *unselected_nodes) const
207 {
208 std::list<SPItem const *> lit;
209 lit.push_back(it);
211 std::vector<NR::Point> points_to_snap;
212 points_to_snap.push_back(p);
214 return freeSnap(t, p, true, points_to_snap, lit, unselected_nodes);
215 }
218 /**
219 * Try to snap a point to any of the specified snappers.
220 *
221 * \param t Type of point.
222 * \param p Point.
223 * \param first_point If true then this point is the first one from a whole bunch of points
224 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
225 * \param it List of items to ignore when snapping.
226 * \param snappers List of snappers to try to snap to
227 * \return Snapped point.
228 */
230 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
231 NR::Point const &p,
232 bool const &first_point,
233 std::vector<NR::Point> &points_to_snap,
234 std::list<SPItem const *> const &it,
235 std::vector<NR::Point> *unselected_nodes) const
236 {
237 if (!SomeSnapperMightSnap()) {
238 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
239 }
241 SnappedConstraints sc;
243 SnapperList const snappers = getSnappers();
245 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
246 (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it, unselected_nodes);
247 }
249 return findBestSnap(p, sc, false);
250 }
252 /**
253 * Try to snap a point to any interested snappers. A snap will only occur along
254 * a line described by a Inkscape::Snapper::ConstraintLine.
255 *
256 * \param t Type of point.
257 * \param p Point.
258 * \param c Constraint line.
259 * \param it Item to ignore when snapping.
260 * \return Snapped point.
261 */
263 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
264 NR::Point const &p,
265 Inkscape::Snapper::ConstraintLine const &c,
266 SPItem const *it) const
267 {
268 std::list<SPItem const *> lit;
269 lit.push_back(it);
271 std::vector<NR::Point> points_to_snap;
272 points_to_snap.push_back(p);
274 return constrainedSnap(t, p, true, points_to_snap, c, lit);
275 }
279 /**
280 * Try to snap a point to any interested snappers. A snap will only occur along
281 * a line described by a Inkscape::Snapper::ConstraintLine.
282 *
283 * \param t Type of point.
284 * \param p Point.
285 * \param first_point If true then this point is the first one from a whole bunch of points
286 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
287 * \param c Constraint line.
288 * \param it List of items to ignore when snapping.
289 * \return Snapped point.
290 */
292 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
293 NR::Point const &p,
294 bool const &first_point,
295 std::vector<NR::Point> &points_to_snap,
296 Inkscape::Snapper::ConstraintLine const &c,
297 std::list<SPItem const *> const &it) const
298 {
299 if (!SomeSnapperMightSnap()) {
300 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
301 }
303 SnappedConstraints sc;
305 SnapperList const snappers = getSnappers();
306 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
307 (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
308 }
310 return findBestSnap(p, sc, true);
311 }
313 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
314 NR::Point const &guide_normal) const
315 {
316 // This method is used to snap a guide to nodes, while dragging the guide around
318 if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
319 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
320 }
322 SnappedConstraints sc;
323 object.guideSnap(sc, p, guide_normal);
325 return findBestSnap(p, sc, false);
326 }
329 /**
330 * Main internal snapping method, which is called by the other, friendlier, public
331 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
332 * of duplicated code.
333 *
334 * \param type Type of points being snapped.
335 * \param points List of points to snap.
336 * \param ignore List of items to ignore while snapping.
337 * \param constrained true if the snap is constrained.
338 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
339 * \param transformation_type Type of transformation to apply to points before trying to snap them.
340 * \param transformation Description of the transformation; details depend on the type.
341 * \param origin Origin of the transformation, if applicable.
342 * \param dim Dimension of the transformation, if applicable.
343 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
344 */
346 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
347 Inkscape::Snapper::PointType type,
348 std::vector<NR::Point> const &points,
349 std::list<SPItem const *> const &ignore,
350 bool constrained,
351 Inkscape::Snapper::ConstraintLine const &constraint,
352 Transformation transformation_type,
353 NR::Point const &transformation,
354 NR::Point const &origin,
355 NR::Dim2 dim,
356 bool uniform) const
357 {
358 /* We have a list of points, which we are proposing to transform in some way. We need to see
359 ** if any of these points, when transformed, snap to anything. If they do, we return the
360 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
361 */
363 /* Quick check to see if we have any snappers that are enabled
364 ** Also used to globally disable all snapping
365 */
366 if (SomeSnapperMightSnap() == false) {
367 return std::make_pair(transformation, false);
368 }
370 std::vector<NR::Point> transformed_points;
372 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
374 /* Work out the transformed version of this point */
375 NR::Point transformed;
376 switch (transformation_type) {
377 case TRANSLATION:
378 transformed = *i + transformation;
379 break;
380 case SCALE:
381 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
382 break;
383 case STRETCH:
384 {
385 NR::scale s(1, 1);
386 if (uniform)
387 s[NR::X] = s[NR::Y] = transformation[dim];
388 else {
389 s[dim] = transformation[dim];
390 s[1 - dim] = 1;
391 }
392 transformed = ((*i - origin) * s) + origin;
393 break;
394 }
395 case SKEW:
396 transformed = *i;
397 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
398 break;
399 default:
400 g_assert_not_reached();
401 }
403 // add the current transformed point to the box hulling all transformed points
404 transformed_points.push_back(transformed);
405 }
407 /* The current best transformation */
408 NR::Point best_transformation = transformation;
410 /* The current best metric for the best transformation; lower is better, NR_HUGE
411 ** means that we haven't snapped anything.
412 */
413 NR::Coord best_metric = NR_HUGE;
414 NR::Coord best_second_metric = NR_HUGE;
415 NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
416 bool best_at_intersection = false;
417 bool best_always_snap = false;
419 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
421 //std::cout << std::endl;
423 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
425 /* Snap it */
426 Inkscape::SnappedPoint snapped;
428 if (constrained) {
429 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
430 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
431 // When uniformly scaling, each point will have its own unique constraint line,
432 // running from the scaling origin to the original untransformed point. We will
433 // calculate that line here
434 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
435 } else if (transformation_type == STRETCH || transformation_type == SKEW) { // when skewing or non-uniform stretching {
436 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
437 } // else: leave the original constraint, e.g. for constrained translation
438 if (transformation_type == SCALE && !uniform) {
439 g_warning("Non-uniform constrained scaling is not supported!");
440 }
441 snapped = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
442 } else {
443 snapped = freeSnap(type, *j, i == points.begin(), transformed_points, ignore, NULL);
444 }
446 NR::Point result;
447 NR::Coord metric = NR_HUGE;
448 NR::Coord second_metric = NR_HUGE;
449 NR::Point scale_metric(NR_HUGE, NR_HUGE);
451 if (snapped.getDistance() < NR_HUGE) {
452 /* We snapped. Find the transformation that describes where the snapped point has
453 ** ended up, and also the metric for this transformation.
454 */
455 NR::Point const a = (snapped.getPoint() - origin); // vector to snapped point
456 NR::Point const b = (*i - origin); // vector to original point
458 switch (transformation_type) {
459 case TRANSLATION:
460 result = snapped.getPoint() - *i;
461 /* Consider the case in which a box is almost aligned with a grid in both
462 * horizontal and vertical directions. The distance to the intersection of
463 * the grid lines will always be larger then the distance to a single grid
464 * line. If we prefer snapping to an intersection instead of to a single
465 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
466 * snapped distance will be used as a metric. Please note that the snapped
467 * distance is defined as the distance to the nearest line of the intersection,
468 * and not to the intersection itself!
469 */
470 metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
471 second_metric = snapped.getSecondDistance();
472 break;
473 case SCALE:
474 {
475 result = NR::Point(NR_HUGE, NR_HUGE);
476 // If this point *i is horizontally or vertically aligned with
477 // the origin of the scaling, then it will scale purely in X or Y
478 // We can therefore only calculate the scaling in this direction
479 // and the scaling factor for the other direction should remain
480 // untouched (unless scaling is uniform ofcourse)
481 for (int index = 0; index < 2; index++) {
482 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
483 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
484 result[index] = a[index] / b[index]; // then calculate it!
485 }
486 // we might leave result[1-index] = NR_HUGE
487 // if scaling didn't occur in the other direction
488 }
489 }
490 // Compare the resulting scaling with the desired scaling
491 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
492 break;
493 }
494 case STRETCH:
495 result = NR::Point(NR_HUGE, NR_HUGE);
496 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
497 result[dim] = a[dim] / b[dim];
498 result[1-dim] = uniform ? result[dim] : 1;
499 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
500 if (uniform && fabs(b[1-dim]) > 1e-6) {
501 result[1-dim] = a[1-dim] / b[1-dim];
502 result[dim] = result[1-dim];
503 }
504 }
505 metric = std::abs(result[dim] - transformation[dim]);
506 break;
507 case SKEW:
508 result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
509 metric = std::abs(result[dim] - transformation[dim]);
510 break;
511 default:
512 g_assert_not_reached();
513 }
515 /* Note it if it's the best so far */
516 if (transformation_type == SCALE) {
517 for (int index = 0; index < 2; index++) {
518 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
519 best_transformation[index] = result[index];
520 best_scale_metric[index] = fabs(scale_metric[index]);
521 //std::cout << "SEL ";
522 } //else { std::cout << " ";}
523 }
524 if (uniform) {
525 if (best_scale_metric[0] < best_scale_metric[1]) {
526 best_transformation[1] = best_transformation[0];
527 best_scale_metric[1] = best_scale_metric[0];
528 } else {
529 best_transformation[0] = best_transformation[1];
530 best_scale_metric[0] = best_scale_metric[1];
531 }
532 }
533 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
534 //std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
535 } else {
536 bool const c1 = metric < best_metric;
537 bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
538 bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
539 bool const c3b = second_metric < best_second_metric;
540 bool const c4 = snapped.getAlwaysSnap() == true && best_always_snap == false;
541 bool const c4n = snapped.getAlwaysSnap() == false && best_always_snap == true;
543 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
544 best_transformation = result;
545 best_metric = metric;
546 best_second_metric = second_metric;
547 best_at_intersection = snapped.getAtIntersection();
548 best_always_snap = snapped.getAlwaysSnap();
549 //std::cout << "SEL ";
550 } //else { std::cout << " ";}
551 //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
552 }
553 }
555 j++;
556 }
558 if (transformation_type == SCALE) {
559 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
560 for (int index = 0; index < 2; index++) {
561 if (best_transformation[index] == NR_HUGE) {
562 if (uniform && best_transformation[1-index] < NR_HUGE) {
563 best_transformation[index] = best_transformation[1-index];
564 } else {
565 best_transformation[index] = transformation[index];
566 }
567 }
568 }
569 }
571 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
572 // These rounding errors might be caused by NRRects, see bug #1584301
573 return std::make_pair(best_transformation, best_metric < 1e6);
574 }
577 /**
578 * Try to snap a list of points to any interested snappers after they have undergone
579 * a translation.
580 *
581 * \param t Type of points.
582 * \param p Points.
583 * \param it List of items to ignore when snapping.
584 * \param tr Proposed translation.
585 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
586 */
588 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
589 std::vector<NR::Point> const &p,
590 std::list<SPItem const *> const &it,
591 NR::Point const &tr) const
592 {
593 return _snapTransformed(
594 t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
595 );
596 }
599 /**
600 * Try to snap a list of points to any interested snappers after they have undergone a
601 * translation. A snap will only occur along a line described by a
602 * Inkscape::Snapper::ConstraintLine.
603 *
604 * \param t Type of points.
605 * \param p Points.
606 * \param it List of items to ignore when snapping.
607 * \param c Constraint line.
608 * \param tr Proposed translation.
609 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
610 */
612 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
613 std::vector<NR::Point> const &p,
614 std::list<SPItem const *> const &it,
615 Inkscape::Snapper::ConstraintLine const &c,
616 NR::Point const &tr) const
617 {
618 return _snapTransformed(
619 t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
620 );
621 }
624 /**
625 * Try to snap a list of points to any interested snappers after they have undergone
626 * a scale.
627 *
628 * \param t Type of points.
629 * \param p Points.
630 * \param it List of items to ignore when snapping.
631 * \param s Proposed scale.
632 * \param o Origin of proposed scale.
633 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
634 */
636 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
637 std::vector<NR::Point> const &p,
638 std::list<SPItem const *> const &it,
639 NR::scale const &s,
640 NR::Point const &o) const
641 {
642 return _snapTransformed(
643 t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
644 );
645 }
648 /**
649 * Try to snap a list of points to any interested snappers after they have undergone
650 * a scale. A snap will only occur along a line described by a
651 * Inkscape::Snapper::ConstraintLine.
652 *
653 * \param t Type of points.
654 * \param p Points.
655 * \param it List of items to ignore when snapping.
656 * \param s Proposed scale.
657 * \param o Origin of proposed scale.
658 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
659 */
661 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
662 std::vector<NR::Point> const &p,
663 std::list<SPItem const *> const &it,
664 NR::scale const &s,
665 NR::Point const &o) const
666 {
667 // When constrained scaling, only uniform scaling is supported.
668 return _snapTransformed(
669 t, p, it, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true
670 );
671 }
674 /**
675 * Try to snap a list of points to any interested snappers after they have undergone
676 * a stretch.
677 *
678 * \param t Type of points.
679 * \param p Points.
680 * \param it List of items to ignore when snapping.
681 * \param s Proposed stretch.
682 * \param o Origin of proposed stretch.
683 * \param d Dimension in which to apply proposed stretch.
684 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
685 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
686 */
688 std::pair<NR::Coord, bool> SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType t,
689 std::vector<NR::Point> const &p,
690 std::list<SPItem const *> const &it,
691 NR::Coord const &s,
692 NR::Point const &o,
693 NR::Dim2 d,
694 bool u) const
695 {
696 std::pair<NR::Point, bool> const r = _snapTransformed(
697 t, p, it, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
698 );
700 return std::make_pair(r.first[d], r.second);
701 }
704 /**
705 * Try to snap a list of points to any interested snappers after they have undergone
706 * a skew.
707 *
708 * \param t Type of points.
709 * \param p Points.
710 * \param it List of items to ignore when snapping.
711 * \param s Proposed skew.
712 * \param o Origin of proposed skew.
713 * \param d Dimension in which to apply proposed skew.
714 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
715 */
717 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
718 std::vector<NR::Point> const &p,
719 std::list<SPItem const *> const &it,
720 NR::Coord const &s,
721 NR::Point const &o,
722 NR::Dim2 d) const
723 {
724 std::pair<NR::Point, bool> const r = _snapTransformed(
725 t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
726 );
728 return std::make_pair(r.first[d], r.second);
729 }
731 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
732 {
733 /*
734 std::cout << "Type and number of snapped constraints: " << std::endl;
735 std::cout << " Points : " << sc.points.size() << std::endl;
736 std::cout << " Lines : " << sc.lines.size() << std::endl;
737 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
738 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
739 */
741 // Store all snappoints
742 std::list<Inkscape::SnappedPoint> sp_list;
744 // search for the closest snapped point
745 Inkscape::SnappedPoint closestPoint;
746 if (getClosestSP(sc.points, closestPoint)) {
747 sp_list.push_back(closestPoint);
748 }
750 // search for the closest snapped line segment
751 Inkscape::SnappedLineSegment closestLineSegment;
752 if (getClosestSLS(sc.lines, closestLineSegment)) {
753 sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
754 }
756 if (_intersectionLS) {
757 // search for the closest snapped intersection of line segments
758 Inkscape::SnappedPoint closestLineSegmentIntersection;
759 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
760 sp_list.push_back(closestLineSegmentIntersection);
761 }
762 }
764 // search for the closest snapped grid line
765 Inkscape::SnappedLine closestGridLine;
766 if (getClosestSL(sc.grid_lines, closestGridLine)) {
767 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
768 }
770 // search for the closest snapped guide line
771 Inkscape::SnappedLine closestGuideLine;
772 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
773 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
774 }
776 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
777 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
779 // When doing a constrained snap however, we're already at an intersection of the constrained line and
780 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
781 // no need to look for additional intersections
782 if (!constrained) {
783 // search for the closest snapped intersection of grid lines
784 Inkscape::SnappedPoint closestGridPoint;
785 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
786 sp_list.push_back(closestGridPoint);
787 }
789 // search for the closest snapped intersection of guide lines
790 Inkscape::SnappedPoint closestGuidePoint;
791 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
792 sp_list.push_back(closestGuidePoint);
793 }
795 // search for the closest snapped intersection of grid with guide lines
796 if (_intersectionGG) {
797 Inkscape::SnappedPoint closestGridGuidePoint;
798 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
799 sp_list.push_back(closestGridGuidePoint);
800 }
801 }
802 }
804 // now let's see which snapped point gets a thumbs up
805 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
806 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
807 // first find out if this snapped point is within snapping range
808 if ((*i).getDistance() <= (*i).getTolerance()) {
809 // if it's the first point
810 bool c1 = (i == sp_list.begin());
811 // or, if it's closer
812 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
813 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
814 bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
815 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
816 bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
817 // or, if it's just as close then consider the second distance
818 // (which is only relevant for points at an intersection)
819 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance());
820 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
821 // then prefer this point over the previous one
822 if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
823 bestSnappedPoint = *i;
824 }
825 }
826 }
828 return bestSnappedPoint;
829 }
831 /*
832 Local Variables:
833 mode:c++
834 c-file-style:"stroustrup"
835 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
836 indent-tabs-mode:nil
837 fill-column:99
838 End:
839 */
840 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :