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"
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,
182 NR::Maybe<NR::Point> point_not_to_snap_to) const
184 {
185 std::list<SPItem const *> lit;
186 lit.push_back(it);
188 std::vector<NR::Point> points_to_snap;
189 points_to_snap.push_back(p);
191 return freeSnap(t, p, true, points_to_snap, lit, NULL);
192 }
194 /**
195 * Try to snap a point to any interested snappers.
196 *
197 * \param t Type of point.
198 * \param p Point.
199 * \param it Item to ignore when snapping.
200 * \return Snapped point.
201 */
203 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
204 NR::Point const &p,
205 SPItem const *it,
206 std::vector<NR::Point> *unselected_nodes) const
208 {
209 std::list<SPItem const *> lit;
210 lit.push_back(it);
212 std::vector<NR::Point> points_to_snap;
213 points_to_snap.push_back(p);
215 return freeSnap(t, p, true, points_to_snap, lit, unselected_nodes);
216 }
219 /**
220 * Try to snap a point to any of the specified snappers.
221 *
222 * \param t Type of point.
223 * \param p Point.
224 * \param first_point If true then this point is the first one from a whole bunch of points
225 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
226 * \param it List of items to ignore when snapping.
227 * \param snappers List of snappers to try to snap to
228 * \return Snapped point.
229 */
231 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
232 NR::Point const &p,
233 bool const &first_point,
234 std::vector<NR::Point> &points_to_snap,
235 std::list<SPItem const *> const &it,
236 std::vector<NR::Point> *unselected_nodes) const
237 {
238 if (!SomeSnapperMightSnap()) {
239 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
240 }
242 SnappedConstraints sc;
244 SnapperList const snappers = getSnappers();
246 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
247 (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it, unselected_nodes);
248 }
250 return findBestSnap(p, sc, false);
251 }
253 /**
254 * Try to snap a point to any interested snappers. A snap will only occur along
255 * a line described by a Inkscape::Snapper::ConstraintLine.
256 *
257 * \param t Type of point.
258 * \param p Point.
259 * \param c Constraint line.
260 * \param it Item to ignore when snapping.
261 * \return Snapped point.
262 */
264 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
265 NR::Point const &p,
266 Inkscape::Snapper::ConstraintLine const &c,
267 SPItem const *it) const
268 {
269 std::list<SPItem const *> lit;
270 lit.push_back(it);
272 std::vector<NR::Point> points_to_snap;
273 points_to_snap.push_back(p);
275 return constrainedSnap(t, p, true, points_to_snap, c, lit);
276 }
280 /**
281 * Try to snap a point to any interested snappers. A snap will only occur along
282 * a line described by a Inkscape::Snapper::ConstraintLine.
283 *
284 * \param t Type of point.
285 * \param p Point.
286 * \param first_point If true then this point is the first one from a whole bunch of points
287 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
288 * \param c Constraint line.
289 * \param it List of items to ignore when snapping.
290 * \return Snapped point.
291 */
293 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
294 NR::Point const &p,
295 bool const &first_point,
296 std::vector<NR::Point> &points_to_snap,
297 Inkscape::Snapper::ConstraintLine const &c,
298 std::list<SPItem const *> const &it) const
299 {
300 if (!SomeSnapperMightSnap()) {
301 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
302 }
304 SnappedConstraints sc;
306 SnapperList const snappers = getSnappers();
307 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
308 (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
309 }
311 return findBestSnap(p, sc, true);
312 }
314 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
315 NR::Point const &guide_normal) const
316 {
317 // This method is used to snap a guide to nodes, while dragging the guide around
319 if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
320 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
321 }
323 SnappedConstraints sc;
324 object.guideSnap(sc, p, guide_normal);
326 return findBestSnap(p, sc, false);
327 }
330 /**
331 * Main internal snapping method, which is called by the other, friendlier, public
332 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
333 * of duplicated code.
334 *
335 * \param type Type of points being snapped.
336 * \param points List of points to snap.
337 * \param ignore List of items to ignore while snapping.
338 * \param constrained true if the snap is constrained.
339 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
340 * \param transformation_type Type of transformation to apply to points before trying to snap them.
341 * \param transformation Description of the transformation; details depend on the type.
342 * \param origin Origin of the transformation, if applicable.
343 * \param dim Dimension of the transformation, if applicable.
344 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
345 */
347 Inkscape::SnappedPoint SnapManager::_snapTransformed(
348 Inkscape::Snapper::PointType type,
349 std::vector<NR::Point> const &points,
350 std::list<SPItem const *> const &ignore,
351 bool constrained,
352 Inkscape::Snapper::ConstraintLine const &constraint,
353 Transformation transformation_type,
354 NR::Point const &transformation,
355 NR::Point const &origin,
356 NR::Dim2 dim,
357 bool uniform) const
358 {
359 /* We have a list of points, which we are proposing to transform in some way. We need to see
360 ** if any of these points, when transformed, snap to anything. If they do, we return the
361 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
362 */
364 /* Quick check to see if we have any snappers that are enabled
365 ** Also used to globally disable all snapping
366 */
367 if (SomeSnapperMightSnap() == false) {
368 return Inkscape::SnappedPoint();
369 }
371 std::vector<NR::Point> transformed_points;
373 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
375 /* Work out the transformed version of this point */
376 NR::Point transformed;
377 switch (transformation_type) {
378 case TRANSLATION:
379 transformed = *i + transformation;
380 break;
381 case SCALE:
382 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
383 break;
384 case STRETCH:
385 {
386 NR::scale s(1, 1);
387 if (uniform)
388 s[NR::X] = s[NR::Y] = transformation[dim];
389 else {
390 s[dim] = transformation[dim];
391 s[1 - dim] = 1;
392 }
393 transformed = ((*i - origin) * s) + origin;
394 break;
395 }
396 case SKEW:
397 transformed = *i;
398 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
399 break;
400 default:
401 g_assert_not_reached();
402 }
404 // add the current transformed point to the box hulling all transformed points
405 transformed_points.push_back(transformed);
406 }
408 /* The current best transformation */
409 NR::Point best_transformation = transformation;
411 /* The current best metric for the best transformation; lower is better, NR_HUGE
412 ** means that we haven't snapped anything.
413 */
414 NR::Coord best_metric = NR_HUGE;
415 NR::Coord best_second_metric = NR_HUGE;
416 NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
417 Inkscape::SnappedPoint best_snapped_point;
418 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
419 g_assert(best_snapped_point.getAtIntersection() == false);
421 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
423 // std::cout << std::endl;
424 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
426 /* Snap it */
427 Inkscape::SnappedPoint snapped_point;
429 if (constrained) {
430 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
431 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
432 // When uniformly scaling, each point will have its own unique constraint line,
433 // running from the scaling origin to the original untransformed point. We will
434 // calculate that line here
435 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
436 } else if (transformation_type == STRETCH || transformation_type == SKEW) { // when skewing or non-uniform stretching {
437 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
438 } // else: leave the original constraint, e.g. for constrained translation
439 if (transformation_type == SCALE && !uniform) {
440 g_warning("Non-uniform constrained scaling is not supported!");
441 }
442 snapped_point = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
443 } else {
444 snapped_point = freeSnap(type, *j, i == points.begin(), transformed_points, ignore, NULL);
445 }
447 NR::Point result;
448 NR::Coord metric = NR_HUGE;
449 NR::Coord second_metric = NR_HUGE;
450 NR::Point scale_metric(NR_HUGE, NR_HUGE);
452 if (snapped_point.getSnapped()) {
453 /* We snapped. Find the transformation that describes where the snapped point has
454 ** ended up, and also the metric for this transformation.
455 */
456 NR::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
457 NR::Point const b = (*i - origin); // vector to original point
459 switch (transformation_type) {
460 case TRANSLATION:
461 result = snapped_point.getPoint() - *i;
462 /* Consider the case in which a box is almost aligned with a grid in both
463 * horizontal and vertical directions. The distance to the intersection of
464 * the grid lines will always be larger then the distance to a single grid
465 * line. If we prefer snapping to an intersection instead of to a single
466 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
467 * snapped distance will be used as a metric. Please note that the snapped
468 * distance is defined as the distance to the nearest line of the intersection,
469 * and not to the intersection itself!
470 */
471 metric = snapped_point.getDistance(); //used to be: metric = NR::L2(result);
472 second_metric = snapped_point.getSecondDistance();
473 break;
474 case SCALE:
475 {
476 result = NR::Point(NR_HUGE, NR_HUGE);
477 // If this point *i is horizontally or vertically aligned with
478 // the origin of the scaling, then it will scale purely in X or Y
479 // We can therefore only calculate the scaling in this direction
480 // and the scaling factor for the other direction should remain
481 // untouched (unless scaling is uniform ofcourse)
482 for (int index = 0; index < 2; index++) {
483 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
484 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
485 result[index] = a[index] / b[index]; // then calculate it!
486 }
487 // we might leave result[1-index] = NR_HUGE
488 // if scaling didn't occur in the other direction
489 }
490 }
491 // Compare the resulting scaling with the desired scaling
492 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
493 break;
494 }
495 case STRETCH:
496 result = NR::Point(NR_HUGE, NR_HUGE);
497 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
498 result[dim] = a[dim] / b[dim];
499 result[1-dim] = uniform ? result[dim] : 1;
500 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
501 if (uniform && fabs(b[1-dim]) > 1e-6) {
502 result[1-dim] = a[1-dim] / b[1-dim];
503 result[dim] = result[1-dim];
504 }
505 }
506 metric = std::abs(result[dim] - transformation[dim]);
507 break;
508 case SKEW:
509 result[dim] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
510 metric = std::abs(result[dim] - transformation[dim]);
511 break;
512 default:
513 g_assert_not_reached();
514 }
516 /* Note it if it's the best so far */
517 if (transformation_type == SCALE) {
518 for (int index = 0; index < 2; index++) {
519 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
520 best_transformation[index] = result[index];
521 best_scale_metric[index] = fabs(scale_metric[index]);
522 // When scaling, we're considering the best transformation in each direction separately
523 // Therefore two different snapped points might together make a single best transformation
524 // We will however return only a single snapped point (e.g. to display the snapping indicator)
525 best_snapped_point = snapped_point;
526 // std::cout << "SEL ";
527 } // else { std::cout << " ";}
528 }
529 if (uniform) {
530 if (best_scale_metric[0] < best_scale_metric[1]) {
531 best_transformation[1] = best_transformation[0];
532 best_scale_metric[1] = best_scale_metric[0];
533 } else {
534 best_transformation[0] = best_transformation[1];
535 best_scale_metric[0] = best_scale_metric[1];
536 }
537 }
538 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
539 // std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped_point.getDistance() << " | P_snap = " << snapped_point.getPoint() << std::endl;
540 } else {
541 bool const c1 = metric < best_metric;
542 bool const c2 = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == false;
543 bool const c3a = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == true;
544 bool const c3b = second_metric < best_second_metric;
545 bool const c4 = snapped_point.getAlwaysSnap() == true && best_snapped_point.getAlwaysSnap() == false;
546 bool const c4n = snapped_point.getAlwaysSnap() == false && best_snapped_point.getAlwaysSnap() == true;
548 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
549 best_transformation = result;
550 best_metric = metric;
551 best_second_metric = second_metric;
552 best_snapped_point = snapped_point;
553 // std::cout << "SEL ";
554 } // else { std::cout << " ";}
555 // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped_point.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped_point.getPoint() << std::endl;
556 }
557 }
559 j++;
560 }
562 if (transformation_type == SCALE) {
563 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
564 for (int index = 0; index < 2; index++) {
565 if (best_transformation[index] == NR_HUGE) {
566 if (uniform && best_transformation[1-index] < NR_HUGE) {
567 best_transformation[index] = best_transformation[1-index];
568 } else {
569 best_transformation[index] = transformation[index];
570 }
571 }
572 }
573 }
575 best_snapped_point.setTransformation(best_transformation);
576 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
577 // These rounding errors might be caused by NRRects, see bug #1584301
578 best_snapped_point.setDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
579 return best_snapped_point;
580 }
583 /**
584 * Try to snap a list of points to any interested snappers after they have undergone
585 * a translation.
586 *
587 * \param t Type of points.
588 * \param p Points.
589 * \param it List of items to ignore when snapping.
590 * \param tr Proposed translation.
591 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
592 */
594 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
595 std::vector<NR::Point> const &p,
596 std::list<SPItem const *> const &it,
597 NR::Point const &tr) const
598 {
599 return _snapTransformed(t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false);
600 }
603 /**
604 * Try to snap a list of points to any interested snappers after they have undergone a
605 * translation. A snap will only occur along a line described by a
606 * Inkscape::Snapper::ConstraintLine.
607 *
608 * \param t Type of points.
609 * \param p Points.
610 * \param it List of items to ignore when snapping.
611 * \param c Constraint line.
612 * \param tr Proposed translation.
613 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
614 */
616 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
617 std::vector<NR::Point> const &p,
618 std::list<SPItem const *> const &it,
619 Inkscape::Snapper::ConstraintLine const &c,
620 NR::Point const &tr) const
621 {
622 return _snapTransformed(t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false);
623 }
626 /**
627 * Try to snap a list of points to any interested snappers after they have undergone
628 * a scale.
629 *
630 * \param t Type of points.
631 * \param p Points.
632 * \param it List of items to ignore when snapping.
633 * \param s Proposed scale.
634 * \param o Origin of proposed scale.
635 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
636 */
638 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
639 std::vector<NR::Point> const &p,
640 std::list<SPItem const *> const &it,
641 NR::scale const &s,
642 NR::Point const &o) const
643 {
644 return _snapTransformed(t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false);
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 Inkscape::SnappedPoint 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(t, p, it, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true);
669 }
672 /**
673 * Try to snap a list of points to any interested snappers after they have undergone
674 * a stretch.
675 *
676 * \param t Type of points.
677 * \param p Points.
678 * \param it List of items to ignore when snapping.
679 * \param s Proposed stretch.
680 * \param o Origin of proposed stretch.
681 * \param d Dimension in which to apply proposed stretch.
682 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
683 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
684 */
686 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType t,
687 std::vector<NR::Point> const &p,
688 std::list<SPItem const *> const &it,
689 NR::Coord const &s,
690 NR::Point const &o,
691 NR::Dim2 d,
692 bool u) const
693 {
694 return _snapTransformed(t, p, it, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u);
695 }
698 /**
699 * Try to snap a list of points to any interested snappers after they have undergone
700 * a skew.
701 *
702 * \param t Type of points.
703 * \param p Points.
704 * \param it List of items to ignore when snapping.
705 * \param s Proposed skew.
706 * \param o Origin of proposed skew.
707 * \param d Dimension in which to apply proposed skew.
708 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
709 */
711 Inkscape::SnappedPoint SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
712 std::vector<NR::Point> const &p,
713 std::list<SPItem const *> const &it,
714 NR::Coord const &s,
715 NR::Point const &o,
716 NR::Dim2 d) const
717 {
718 return _snapTransformed(t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false);
719 }
721 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
722 {
723 /*
724 std::cout << "Type and number of snapped constraints: " << std::endl;
725 std::cout << " Points : " << sc.points.size() << std::endl;
726 std::cout << " Lines : " << sc.lines.size() << std::endl;
727 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
728 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
729 */
731 // Store all snappoints
732 std::list<Inkscape::SnappedPoint> sp_list;
734 // search for the closest snapped point
735 Inkscape::SnappedPoint closestPoint;
736 if (getClosestSP(sc.points, closestPoint)) {
737 sp_list.push_back(closestPoint);
738 }
740 // search for the closest snapped line segment
741 Inkscape::SnappedLineSegment closestLineSegment;
742 if (getClosestSLS(sc.lines, closestLineSegment)) {
743 sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
744 }
746 if (_intersectionLS) {
747 // search for the closest snapped intersection of line segments
748 Inkscape::SnappedPoint closestLineSegmentIntersection;
749 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
750 sp_list.push_back(closestLineSegmentIntersection);
751 }
752 }
754 // search for the closest snapped grid line
755 Inkscape::SnappedLine closestGridLine;
756 if (getClosestSL(sc.grid_lines, closestGridLine)) {
757 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
758 }
760 // search for the closest snapped guide line
761 Inkscape::SnappedLine closestGuideLine;
762 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
763 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
764 }
766 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
767 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
769 // When doing a constrained snap however, we're already at an intersection of the constrained line and
770 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
771 // no need to look for additional intersections
772 if (!constrained) {
773 // search for the closest snapped intersection of grid lines
774 Inkscape::SnappedPoint closestGridPoint;
775 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
776 sp_list.push_back(closestGridPoint);
777 }
779 // search for the closest snapped intersection of guide lines
780 Inkscape::SnappedPoint closestGuidePoint;
781 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
782 sp_list.push_back(closestGuidePoint);
783 }
785 // search for the closest snapped intersection of grid with guide lines
786 if (_intersectionGG) {
787 Inkscape::SnappedPoint closestGridGuidePoint;
788 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
789 sp_list.push_back(closestGridGuidePoint);
790 }
791 }
792 }
794 // now let's see which snapped point gets a thumbs up
795 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
796 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
797 // first find out if this snapped point is within snapping range
798 if ((*i).getDistance() <= (*i).getTolerance()) {
799 // if it's the first point
800 bool c1 = (i == sp_list.begin());
801 // or, if it's closer
802 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
803 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
804 bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
805 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
806 bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
807 // or, if it's just as close then consider the second distance
808 // (which is only relevant for points at an intersection)
809 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance());
810 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
811 // then prefer this point over the previous one
812 if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
813 bestSnappedPoint = *i;
814 }
815 }
816 }
818 return bestSnappedPoint;
819 }
821 /*
822 Local Variables:
823 mode:c++
824 c-file-style:"stroustrup"
825 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
826 indent-tabs-mode:nil
827 fill-column:99
828 End:
829 */
830 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :