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"
32 #include "display/snap-indicator.h"
34 #include "inkscape.h"
35 #include "desktop.h"
36 #include "sp-guide.h"
37 using std::vector;
39 /**
40 * Construct a SnapManager for a SPNamedView.
41 *
42 * \param v `Owning' SPNamedView.
43 */
45 SnapManager::SnapManager(SPNamedView const *v) :
46 guide(v, 0),
47 object(v, 0),
48 _named_view(v),
49 _include_item_center(false),
50 _snap_enabled_globally(true)
51 {
52 }
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 (!_snap_enabled_globally) {
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 * The snappers have too many parameters to adjust individually. Therefore only
112 * two snapping modes are presented to the user: snapping bounding box corners (to
113 * other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
114 * paths, grids or guides). To select either of these modes (or both), use the
115 * methods defined below: setSnapModeBBox() and setSnapModeNode().
116 *
117 * */
120 void SnapManager::setSnapModeBBox(bool enabled)
121 {
122 //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
123 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
125 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
126 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
127 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
128 }
130 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
131 //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
132 //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
133 object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa
134 }
136 bool SnapManager::getSnapModeBBox() const
137 {
138 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
139 }
141 void SnapManager::setSnapModeNode(bool enabled)
142 {
143 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
145 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
146 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
147 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
148 }
150 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
151 //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
152 //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog
153 object.setStrictSnapping(true);
154 }
156 bool SnapManager::getSnapModeNode() const
157 {
158 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
159 }
161 void SnapManager::setSnapModeGuide(bool enabled)
162 {
163 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
164 }
166 bool SnapManager::getSnapModeGuide() const
167 {
168 return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
169 }
171 /**
172 * Try to snap a point to any of the specified snappers.
173 *
174 * \param point_type Type of point.
175 * \param p Point.
176 * \param first_point If true then this point is the first one from a whole bunch of points
177 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
178 * \param snappers List of snappers to try to snap to
179 * \return Snapped point.
180 */
182 void SnapManager::freeSnapVoid(Inkscape::Snapper::PointType point_type,
183 NR::Point &p,
184 bool first_point,
185 NR::Maybe<NR::Rect> const &bbox_to_snap) const
186 {
187 Inkscape::SnappedPoint const s = freeSnap(point_type, p, first_point, bbox_to_snap);
188 s.getPoint(p);
189 }
191 /**
192 * Try to snap a point to any of the specified snappers.
193 *
194 * \param point_type Type of point.
195 * \param p Point.
196 * \param first_point If true then this point is the first one from a whole bunch of points
197 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
198 * \param snappers List of snappers to try to snap to
199 * \return Snapped point.
200 */
202 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType point_type,
203 NR::Point const &p,
204 bool first_point,
205 NR::Maybe<NR::Rect> const &bbox_to_snap) const
206 {
207 if (!SomeSnapperMightSnap()) {
208 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false);
209 }
211 std::vector<SPItem const *> *items_to_ignore;
212 if (_item_to_ignore) { // If we have only a single item to ignore
213 // then build a list containing this single item;
214 // This single-item list will prevail over any other _items_to_ignore list, should that exist
215 items_to_ignore = new std::vector<SPItem const *>;
216 items_to_ignore->push_back(_item_to_ignore);
217 } else {
218 items_to_ignore = _items_to_ignore;
219 }
221 SnappedConstraints sc;
222 SnapperList const snappers = getSnappers();
224 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
225 (*i)->freeSnap(sc, point_type, p, first_point, bbox_to_snap, items_to_ignore, _unselected_nodes);
226 }
228 if (_item_to_ignore) {
229 delete items_to_ignore;
230 }
232 return findBestSnap(p, sc, false);
233 }
235 /**
236 * Try to snap a point to any interested snappers. A snap will only occur along
237 * a line described by a Inkscape::Snapper::ConstraintLine.
238 *
239 * \param point_type Type of point.
240 * \param p Point.
241 * \param first_point If true then this point is the first one from a whole bunch of points
242 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
243 * \param constraint Constraint line.
244 * \return Snapped point.
245 */
247 void SnapManager::constrainedSnapVoid(Inkscape::Snapper::PointType point_type,
248 NR::Point &p,
249 Inkscape::Snapper::ConstraintLine const &constraint,
250 bool first_point,
251 NR::Maybe<NR::Rect> const &bbox_to_snap) const
252 {
253 Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, constraint, first_point, bbox_to_snap);
254 s.getPoint(p);
255 }
257 /**
258 * Try to snap a point to any interested snappers. A snap will only occur along
259 * a line described by a Inkscape::Snapper::ConstraintLine.
260 *
261 * \param point_type Type of point.
262 * \param p Point.
263 * \param first_point If true then this point is the first one from a whole bunch of points
264 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
265 * \param constraint Constraint line.
266 * \return Snapped point.
267 */
269 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType point_type,
270 NR::Point const &p,
271 Inkscape::Snapper::ConstraintLine const &constraint,
272 bool first_point,
273 NR::Maybe<NR::Rect> const &bbox_to_snap) const
274 {
275 if (!SomeSnapperMightSnap()) {
276 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false);
277 }
279 std::vector<SPItem const *> *items_to_ignore;
280 if (_item_to_ignore) { // If we have only a single item to ignore
281 // then build a list containing this single item;
282 // This single-item list will prevail over any other _items_to_ignore list, should that exist
283 items_to_ignore = new std::vector<SPItem const *>;
284 items_to_ignore->push_back(_item_to_ignore);
285 } else {
286 items_to_ignore = _items_to_ignore;
287 }
289 SnappedConstraints sc;
290 SnapperList const snappers = getSnappers();
291 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
292 (*i)->constrainedSnap(sc, point_type, p, first_point, bbox_to_snap, constraint, items_to_ignore);
293 }
295 if (_item_to_ignore) {
296 delete items_to_ignore;
297 }
299 return findBestSnap(p, sc, true);
300 }
302 void SnapManager::guideSnap(NR::Point &p, NR::Point const &guide_normal) const
303 {
304 // This method is used to snap a guide to nodes, while dragging the guide around
306 if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
307 return;
308 }
310 SnappedConstraints sc;
311 object.guideSnap(sc, p, guide_normal);
313 Inkscape::SnappedPoint const s = findBestSnap(p, sc, false);
314 s.getPoint(p);
315 }
318 /**
319 * Main internal snapping method, which is called by the other, friendlier, public
320 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
321 * of duplicated code.
322 *
323 * \param type Type of points being snapped.
324 * \param points List of points to snap.
325 * \param constrained true if the snap is constrained.
326 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
327 * \param transformation_type Type of transformation to apply to points before trying to snap them.
328 * \param transformation Description of the transformation; details depend on the type.
329 * \param origin Origin of the transformation, if applicable.
330 * \param dim Dimension of the transformation, if applicable.
331 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
332 */
334 Inkscape::SnappedPoint SnapManager::_snapTransformed(
335 Inkscape::Snapper::PointType type,
336 std::vector<NR::Point> const &points,
337 bool constrained,
338 Inkscape::Snapper::ConstraintLine const &constraint,
339 Transformation transformation_type,
340 NR::Point const &transformation,
341 NR::Point const &origin,
342 NR::Dim2 dim,
343 bool uniform) const
344 {
345 /* We have a list of points, which we are proposing to transform in some way. We need to see
346 ** if any of these points, when transformed, snap to anything. If they do, we return the
347 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
348 */
350 /* Quick check to see if we have any snappers that are enabled
351 ** Also used to globally disable all snapping
352 */
353 if (SomeSnapperMightSnap() == false) {
354 g_assert(points.size() > 0);
355 return Inkscape::SnappedPoint();
356 }
358 std::vector<NR::Point> transformed_points;
359 NR::Rect bbox;
361 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
363 /* Work out the transformed version of this point */
364 NR::Point transformed;
365 switch (transformation_type) {
366 case TRANSLATION:
367 transformed = *i + transformation;
368 break;
369 case SCALE:
370 transformed = (*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y]) + origin;
371 break;
372 case STRETCH:
373 {
374 NR::scale s(1, 1);
375 if (uniform)
376 s[NR::X] = s[NR::Y] = transformation[dim];
377 else {
378 s[dim] = transformation[dim];
379 s[1 - dim] = 1;
380 }
381 transformed = ((*i - origin) * s) + origin;
382 break;
383 }
384 case SKEW:
385 // Apply the skew factor
386 transformed[dim] = (*i)[dim] + transformation[0] * ((*i)[1 - dim] - origin[1 - dim]);
387 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
388 // Apply that scale factor here
389 transformed[1-dim] = (*i - origin)[1 - dim] * transformation[1] + origin[1 - dim];
390 break;
391 default:
392 g_assert_not_reached();
393 }
395 // add the current transformed point to the box hulling all transformed points
396 if (i == points.begin()) {
397 bbox = NR::Rect(transformed, transformed);
398 } else {
399 bbox.expandTo(transformed);
400 }
402 transformed_points.push_back(transformed);
403 }
405 /* The current best transformation */
406 NR::Point best_transformation = transformation;
408 /* The current best metric for the best transformation; lower is better, NR_HUGE
409 ** means that we haven't snapped anything.
410 */
411 NR::Coord best_metric = NR_HUGE;
412 NR::Coord best_second_metric = NR_HUGE;
413 NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
414 Inkscape::SnappedPoint best_snapped_point;
415 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
416 g_assert(best_snapped_point.getAtIntersection() == false);
418 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
420 // std::cout << std::endl;
421 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
423 /* Snap it */
424 Inkscape::SnappedPoint snapped_point;
426 if (constrained) {
427 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
428 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
429 // When uniformly scaling, each point will have its own unique constraint line,
430 // running from the scaling origin to the original untransformed point. We will
431 // calculate that line here
432 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
433 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
434 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
435 } // else: leave the original constraint, e.g. for constrained translation and skewing
436 if (transformation_type == SCALE && !uniform) {
437 g_warning("Non-uniform constrained scaling is not supported!");
438 }
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 }
444 NR::Point result;
445 NR::Coord metric = NR_HUGE;
446 NR::Coord second_metric = NR_HUGE;
447 NR::Point scale_metric(NR_HUGE, NR_HUGE);
449 if (snapped_point.getSnapped()) {
450 /* We snapped. Find the transformation that describes where the snapped point has
451 ** ended up, and also the metric for this transformation.
452 */
453 NR::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
454 NR::Point const b = (*i - origin); // vector to original point
456 switch (transformation_type) {
457 case TRANSLATION:
458 result = snapped_point.getPoint() - *i;
459 /* Consider the case in which a box is almost aligned with a grid in both
460 * horizontal and vertical directions. The distance to the intersection of
461 * the grid lines will always be larger then the distance to a single grid
462 * line. If we prefer snapping to an intersection instead of to a single
463 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
464 * snapped distance will be used as a metric. Please note that the snapped
465 * distance is defined as the distance to the nearest line of the intersection,
466 * and not to the intersection itself!
467 */
468 metric = snapped_point.getDistance(); //used to be: metric = NR::L2(result);
469 second_metric = snapped_point.getSecondDistance();
470 break;
471 case SCALE:
472 {
473 result = NR::Point(NR_HUGE, NR_HUGE);
474 // If this point *i is horizontally or vertically aligned with
475 // the origin of the scaling, then it will scale purely in X or Y
476 // We can therefore only calculate the scaling in this direction
477 // and the scaling factor for the other direction should remain
478 // untouched (unless scaling is uniform ofcourse)
479 for (int index = 0; index < 2; index++) {
480 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
481 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
482 result[index] = a[index] / b[index]; // then calculate it!
483 }
484 // we might leave result[1-index] = NR_HUGE
485 // if scaling didn't occur in the other direction
486 }
487 }
488 // Compare the resulting scaling with the desired scaling
489 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
490 break;
491 }
492 case STRETCH:
493 result = NR::Point(NR_HUGE, NR_HUGE);
494 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
495 result[dim] = a[dim] / b[dim];
496 result[1-dim] = uniform ? result[dim] : 1;
497 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
498 if (uniform && fabs(b[1-dim]) > 1e-6) {
499 result[1-dim] = a[1-dim] / b[1-dim];
500 result[dim] = result[1-dim];
501 }
502 }
503 metric = std::abs(result[dim] - transformation[dim]);
504 break;
505 case SKEW:
506 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
507 result[1] = transformation[1]; // scale factor
508 metric = std::abs(result[0] - transformation[0]);
509 break;
510 default:
511 g_assert_not_reached();
512 }
514 /* Note it if it's the best so far */
515 if (transformation_type == SCALE) {
516 for (int index = 0; index < 2; index++) {
517 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
518 best_transformation[index] = result[index];
519 best_scale_metric[index] = fabs(scale_metric[index]);
520 // When scaling, we're considering the best transformation in each direction separately
521 // Therefore two different snapped points might together make a single best transformation
522 // We will however return only a single snapped point (e.g. to display the snapping indicator)
523 best_snapped_point = snapped_point;
524 // std::cout << "SEL ";
525 } // else { std::cout << " ";}
526 }
527 if (uniform) {
528 if (best_scale_metric[0] < best_scale_metric[1]) {
529 best_transformation[1] = best_transformation[0];
530 best_scale_metric[1] = best_scale_metric[0];
531 } else {
532 best_transformation[0] = best_transformation[1];
533 best_scale_metric[0] = best_scale_metric[1];
534 }
535 }
536 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
537 // std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped_point.getDistance() << " | P_snap = " << snapped_point.getPoint() << std::endl;
538 } else {
539 bool const c1 = metric < best_metric;
540 bool const c2 = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == false;
541 bool const c3a = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == true;
542 bool const c3b = second_metric < best_second_metric;
543 bool const c4 = snapped_point.getAlwaysSnap() == true && best_snapped_point.getAlwaysSnap() == false;
544 bool const c4n = snapped_point.getAlwaysSnap() == false && best_snapped_point.getAlwaysSnap() == true;
546 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
547 best_transformation = result;
548 best_metric = metric;
549 best_second_metric = second_metric;
550 best_snapped_point = snapped_point;
551 // std::cout << "SEL ";
552 } // else { std::cout << " ";}
553 // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped_point.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped_point.getPoint() << std::endl;
554 }
555 }
557 j++;
558 }
560 if (transformation_type == SCALE) {
561 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
562 for (int index = 0; index < 2; index++) {
563 if (best_transformation[index] == NR_HUGE) {
564 if (uniform && best_transformation[1-index] < NR_HUGE) {
565 best_transformation[index] = best_transformation[1-index];
566 } else {
567 best_transformation[index] = transformation[index];
568 }
569 }
570 }
571 }
573 best_snapped_point.setTransformation(best_transformation);
574 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
575 // These rounding errors might be caused by NRRects, see bug #1584301
576 best_snapped_point.setDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
577 return best_snapped_point;
578 }
581 /**
582 * Try to snap a list of points to any interested snappers after they have undergone
583 * a translation.
584 *
585 * \param point_type Type of points.
586 * \param p Points.
587 * \param tr Proposed translation.
588 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
589 */
591 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType point_type,
592 std::vector<NR::Point> const &p,
593 NR::Point const &tr) const
594 {
595 return _snapTransformed(point_type, p, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false);
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 point_type Type of points.
605 * \param p Points.
606 * \param constraint Constraint line.
607 * \param tr Proposed translation.
608 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
609 */
611 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
612 std::vector<NR::Point> const &p,
613 Inkscape::Snapper::ConstraintLine const &constraint,
614 NR::Point const &tr) const
615 {
616 return _snapTransformed(point_type, p, true, constraint, TRANSLATION, tr, NR::Point(), NR::X, false);
617 }
620 /**
621 * Try to snap a list of points to any interested snappers after they have undergone
622 * a scale.
623 *
624 * \param point_type Type of points.
625 * \param p Points.
626 * \param s Proposed scale.
627 * \param o Origin of proposed scale.
628 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
629 */
631 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::Snapper::PointType point_type,
632 std::vector<NR::Point> const &p,
633 NR::scale const &s,
634 NR::Point const &o) const
635 {
636 return _snapTransformed(point_type, p, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false);
637 }
640 /**
641 * Try to snap a list of points to any interested snappers after they have undergone
642 * a scale. A snap will only occur along a line described by a
643 * Inkscape::Snapper::ConstraintLine.
644 *
645 * \param point_type Type of points.
646 * \param p Points.
647 * \param s Proposed scale.
648 * \param o Origin of proposed scale.
649 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
650 */
652 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType point_type,
653 std::vector<NR::Point> const &p,
654 NR::scale const &s,
655 NR::Point const &o) const
656 {
657 // When constrained scaling, only uniform scaling is supported.
658 return _snapTransformed(point_type, p, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true);
659 }
662 /**
663 * Try to snap a list of points to any interested snappers after they have undergone
664 * a stretch.
665 *
666 * \param point_type Type of points.
667 * \param p Points.
668 * \param s Proposed stretch.
669 * \param o Origin of proposed stretch.
670 * \param d Dimension in which to apply proposed stretch.
671 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
672 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
673 */
675 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
676 std::vector<NR::Point> const &p,
677 NR::Coord const &s,
678 NR::Point const &o,
679 NR::Dim2 d,
680 bool u) const
681 {
682 return _snapTransformed(point_type, p, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u);
683 }
686 /**
687 * Try to snap a list of points to any interested snappers after they have undergone
688 * a skew.
689 *
690 * \param point_type Type of points.
691 * \param p Points.
692 * \param s Proposed skew.
693 * \param o Origin of proposed skew.
694 * \param d Dimension in which to apply proposed skew.
695 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
696 */
698 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
699 std::vector<NR::Point> const &p,
700 Inkscape::Snapper::ConstraintLine const &constraint,
701 NR::Point const &s,
702 NR::Point const &o,
703 NR::Dim2 d) const
704 {
705 // "s" contains skew factor in s[0], and scale factor in s[1]
706 return _snapTransformed(point_type, p, true, constraint, SKEW, s, o, d, false);
707 }
709 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
710 {
711 /*
712 std::cout << "Type and number of snapped constraints: " << std::endl;
713 std::cout << " Points : " << sc.points.size() << std::endl;
714 std::cout << " Lines : " << sc.lines.size() << std::endl;
715 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
716 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
717 */
719 // Store all snappoints
720 std::list<Inkscape::SnappedPoint> sp_list;
722 // search for the closest snapped point
723 Inkscape::SnappedPoint closestPoint;
724 if (getClosestSP(sc.points, closestPoint)) {
725 sp_list.push_back(closestPoint);
726 }
728 // search for the closest snapped line segment
729 Inkscape::SnappedLineSegment closestLineSegment;
730 if (getClosestSLS(sc.lines, closestLineSegment)) {
731 sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
732 }
734 if (_intersectionLS) {
735 // search for the closest snapped intersection of line segments
736 Inkscape::SnappedPoint closestLineSegmentIntersection;
737 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
738 sp_list.push_back(closestLineSegmentIntersection);
739 }
740 }
742 // search for the closest snapped grid line
743 Inkscape::SnappedLine closestGridLine;
744 if (getClosestSL(sc.grid_lines, closestGridLine)) {
745 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
746 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
747 }
749 // search for the closest snapped guide line
750 Inkscape::SnappedLine closestGuideLine;
751 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
752 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
753 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
754 }
756 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
757 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
759 // When doing a constrained snap however, we're already at an intersection of the constrained line and
760 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
761 // no need to look for additional intersections
762 if (!constrained) {
763 // search for the closest snapped intersection of grid lines
764 Inkscape::SnappedPoint closestGridPoint;
765 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
766 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
767 sp_list.push_back(closestGridPoint);
768 }
770 // search for the closest snapped intersection of guide lines
771 Inkscape::SnappedPoint closestGuidePoint;
772 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
773 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
774 sp_list.push_back(closestGuidePoint);
775 }
777 // search for the closest snapped intersection of grid with guide lines
778 if (_intersectionGG) {
779 Inkscape::SnappedPoint closestGridGuidePoint;
780 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
781 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
782 sp_list.push_back(closestGridGuidePoint);
783 }
784 }
785 }
787 // now let's see which snapped point gets a thumbs up
788 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false);
789 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
790 // first find out if this snapped point is within snapping range
791 if ((*i).getDistance() <= (*i).getTolerance()) {
792 // if it's the first point
793 bool c1 = (i == sp_list.begin());
794 // or, if it's closer
795 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
796 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
797 bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
798 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
799 bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
800 // or, if it's just as close then consider the second distance
801 // (which is only relevant for points at an intersection)
802 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance());
803 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
804 // then prefer this point over the previous one
805 if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
806 bestSnappedPoint = *i;
807 }
808 }
809 }
812 // Update the snap indicator, if requested
813 if (_desktop_for_snapindicator) {
814 if (bestSnappedPoint.getSnapped()) {
815 _desktop_for_snapindicator->snapindicator->set_new_snappoint(bestSnappedPoint);
816 } else {
817 _desktop_for_snapindicator->snapindicator->remove_snappoint();
818 }
819 }
821 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << std::endl;
822 return bestSnappedPoint;
823 }
825 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, SPItem const *item_to_ignore, std::vector<NR::Point> *unselected_nodes)
826 {
827 _item_to_ignore = item_to_ignore;
828 _items_to_ignore = NULL;
829 _desktop_for_snapindicator = desktop_for_snapindicator;
830 _unselected_nodes = unselected_nodes;
831 }
833 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<NR::Point> *unselected_nodes)
834 {
835 _item_to_ignore = NULL;
836 _items_to_ignore = &items_to_ignore;
837 _desktop_for_snapindicator = desktop_for_snapindicator;
838 _unselected_nodes = unselected_nodes;
839 }
841 /*
842 Local Variables:
843 mode:c++
844 c-file-style:"stroustrup"
845 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
846 indent-tabs-mode:nil
847 fill-column:99
848 End:
849 */
850 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :