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::freeSnapReturnByRef(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::constrainedSnapReturnByRef(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 if (transformation_type == TRANSLATION) {
436 // When doing a constrained translation, all points will move in the same direction, i.e.
437 // either horizontally or vertically. The lines along which they move are therefore all
438 // parallel, but might not be colinear. Therefore we will have to set the point through
439 // which the constraint-line runs here, for each point individually.
440 dedicated_constraint.setPoint(*i);
441 } // else: leave the original constraint, e.g. for skewing
442 if (transformation_type == SCALE && !uniform) {
443 g_warning("Non-uniform constrained scaling is not supported!");
444 }
445 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
446 } else {
447 snapped_point = freeSnap(type, *j, i == points.begin(), bbox);
448 }
450 NR::Point result;
451 NR::Coord metric = NR_HUGE;
452 NR::Coord second_metric = NR_HUGE;
453 NR::Point scale_metric(NR_HUGE, NR_HUGE);
455 if (snapped_point.getSnapped()) {
456 /* We snapped. Find the transformation that describes where the snapped point has
457 ** ended up, and also the metric for this transformation.
458 */
459 NR::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
460 NR::Point const b = (*i - origin); // vector to original point
462 switch (transformation_type) {
463 case TRANSLATION:
464 result = snapped_point.getPoint() - *i;
465 /* Consider the case in which a box is almost aligned with a grid in both
466 * horizontal and vertical directions. The distance to the intersection of
467 * the grid lines will always be larger then the distance to a single grid
468 * line. If we prefer snapping to an intersection instead of to a single
469 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
470 * snapped distance will be used as a metric. Please note that the snapped
471 * distance is defined as the distance to the nearest line of the intersection,
472 * and not to the intersection itself!
473 */
474 metric = snapped_point.getDistance(); //used to be: metric = NR::L2(result);
475 second_metric = snapped_point.getSecondDistance();
476 break;
477 case SCALE:
478 {
479 result = NR::Point(NR_HUGE, NR_HUGE);
480 // If this point *i is horizontally or vertically aligned with
481 // the origin of the scaling, then it will scale purely in X or Y
482 // We can therefore only calculate the scaling in this direction
483 // and the scaling factor for the other direction should remain
484 // untouched (unless scaling is uniform ofcourse)
485 for (int index = 0; index < 2; index++) {
486 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
487 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
488 result[index] = a[index] / b[index]; // then calculate it!
489 }
490 // we might leave result[1-index] = NR_HUGE
491 // if scaling didn't occur in the other direction
492 }
493 }
494 // Compare the resulting scaling with the desired scaling
495 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
496 break;
497 }
498 case STRETCH:
499 result = NR::Point(NR_HUGE, NR_HUGE);
500 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
501 result[dim] = a[dim] / b[dim];
502 result[1-dim] = uniform ? result[dim] : 1;
503 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
504 if (uniform && fabs(b[1-dim]) > 1e-6) {
505 result[1-dim] = a[1-dim] / b[1-dim];
506 result[dim] = result[1-dim];
507 }
508 }
509 metric = std::abs(result[dim] - transformation[dim]);
510 break;
511 case SKEW:
512 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
513 result[1] = transformation[1]; // scale factor
514 metric = std::abs(result[0] - transformation[0]);
515 break;
516 default:
517 g_assert_not_reached();
518 }
520 /* Note it if it's the best so far */
521 if (transformation_type == SCALE) {
522 for (int index = 0; index < 2; index++) {
523 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
524 best_transformation[index] = result[index];
525 best_scale_metric[index] = fabs(scale_metric[index]);
526 // When scaling, we're considering the best transformation in each direction separately
527 // Therefore two different snapped points might together make a single best transformation
528 // We will however return only a single snapped point (e.g. to display the snapping indicator)
529 best_snapped_point = snapped_point;
530 // std::cout << "SEL ";
531 } // else { std::cout << " ";}
532 }
533 if (uniform) {
534 if (best_scale_metric[0] < best_scale_metric[1]) {
535 best_transformation[1] = best_transformation[0];
536 best_scale_metric[1] = best_scale_metric[0];
537 } else {
538 best_transformation[0] = best_transformation[1];
539 best_scale_metric[0] = best_scale_metric[1];
540 }
541 }
542 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
543 // std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped_point.getDistance() << " | P_snap = " << snapped_point.getPoint() << std::endl;
544 } else {
545 bool const c1 = metric < best_metric;
546 bool const c2 = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == false;
547 bool const c3a = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == true;
548 bool const c3b = second_metric < best_second_metric;
549 bool const c4 = snapped_point.getAlwaysSnap() == true && best_snapped_point.getAlwaysSnap() == false;
550 bool const c4n = snapped_point.getAlwaysSnap() == false && best_snapped_point.getAlwaysSnap() == true;
552 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
553 best_transformation = result;
554 best_metric = metric;
555 best_second_metric = second_metric;
556 best_snapped_point = snapped_point;
557 // std::cout << "SEL ";
558 } // else { std::cout << " ";}
559 // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped_point.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped_point.getPoint() << std::endl;
560 }
561 }
563 j++;
564 }
566 if (transformation_type == SCALE) {
567 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
568 for (int index = 0; index < 2; index++) {
569 if (best_transformation[index] == NR_HUGE) {
570 if (uniform && best_transformation[1-index] < NR_HUGE) {
571 best_transformation[index] = best_transformation[1-index];
572 } else {
573 best_transformation[index] = transformation[index];
574 }
575 }
576 }
577 }
579 best_snapped_point.setTransformation(best_transformation);
580 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
581 // These rounding errors might be caused by NRRects, see bug #1584301
582 best_snapped_point.setDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
583 return best_snapped_point;
584 }
587 /**
588 * Try to snap a list of points to any interested snappers after they have undergone
589 * a translation.
590 *
591 * \param point_type Type of points.
592 * \param p Points.
593 * \param tr Proposed translation.
594 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
595 */
597 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType point_type,
598 std::vector<NR::Point> const &p,
599 NR::Point const &tr) const
600 {
601 return _snapTransformed(point_type, p, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false);
602 }
605 /**
606 * Try to snap a list of points to any interested snappers after they have undergone a
607 * translation. A snap will only occur along a line described by a
608 * Inkscape::Snapper::ConstraintLine.
609 *
610 * \param point_type Type of points.
611 * \param p Points.
612 * \param constraint Constraint line.
613 * \param tr Proposed translation.
614 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
615 */
617 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
618 std::vector<NR::Point> const &p,
619 Inkscape::Snapper::ConstraintLine const &constraint,
620 NR::Point const &tr) const
621 {
622 return _snapTransformed(point_type, p, true, constraint, 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 point_type Type of points.
631 * \param p Points.
632 * \param s Proposed scale.
633 * \param o Origin of proposed scale.
634 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
635 */
637 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::Snapper::PointType point_type,
638 std::vector<NR::Point> const &p,
639 NR::scale const &s,
640 NR::Point const &o) const
641 {
642 return _snapTransformed(point_type, p, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false);
643 }
646 /**
647 * Try to snap a list of points to any interested snappers after they have undergone
648 * a scale. A snap will only occur along a line described by a
649 * Inkscape::Snapper::ConstraintLine.
650 *
651 * \param point_type Type of points.
652 * \param p Points.
653 * \param s Proposed scale.
654 * \param o Origin of proposed scale.
655 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
656 */
658 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType point_type,
659 std::vector<NR::Point> const &p,
660 NR::scale const &s,
661 NR::Point const &o) const
662 {
663 // When constrained scaling, only uniform scaling is supported.
664 return _snapTransformed(point_type, p, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true);
665 }
668 /**
669 * Try to snap a list of points to any interested snappers after they have undergone
670 * a stretch.
671 *
672 * \param point_type Type of points.
673 * \param p Points.
674 * \param s Proposed stretch.
675 * \param o Origin of proposed stretch.
676 * \param d Dimension in which to apply proposed stretch.
677 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
678 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
679 */
681 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
682 std::vector<NR::Point> const &p,
683 NR::Coord const &s,
684 NR::Point const &o,
685 NR::Dim2 d,
686 bool u) const
687 {
688 return _snapTransformed(point_type, p, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u);
689 }
692 /**
693 * Try to snap a list of points to any interested snappers after they have undergone
694 * a skew.
695 *
696 * \param point_type Type of points.
697 * \param p Points.
698 * \param s Proposed skew.
699 * \param o Origin of proposed skew.
700 * \param d Dimension in which to apply proposed skew.
701 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
702 */
704 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
705 std::vector<NR::Point> const &p,
706 Inkscape::Snapper::ConstraintLine const &constraint,
707 NR::Point const &s,
708 NR::Point const &o,
709 NR::Dim2 d) const
710 {
711 // "s" contains skew factor in s[0], and scale factor in s[1]
712 return _snapTransformed(point_type, p, true, constraint, SKEW, s, o, d, false);
713 }
715 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
716 {
717 /*
718 std::cout << "Type and number of snapped constraints: " << std::endl;
719 std::cout << " Points : " << sc.points.size() << std::endl;
720 std::cout << " Lines : " << sc.lines.size() << std::endl;
721 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
722 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
723 */
725 // Store all snappoints
726 std::list<Inkscape::SnappedPoint> sp_list;
728 // search for the closest snapped point
729 Inkscape::SnappedPoint closestPoint;
730 if (getClosestSP(sc.points, closestPoint)) {
731 sp_list.push_back(closestPoint);
732 }
734 // search for the closest snapped line segment
735 Inkscape::SnappedLineSegment closestLineSegment;
736 if (getClosestSLS(sc.lines, closestLineSegment)) {
737 sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
738 }
740 if (_intersectionLS) {
741 // search for the closest snapped intersection of line segments
742 Inkscape::SnappedPoint closestLineSegmentIntersection;
743 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
744 sp_list.push_back(closestLineSegmentIntersection);
745 }
746 }
748 // search for the closest snapped grid line
749 Inkscape::SnappedLine closestGridLine;
750 if (getClosestSL(sc.grid_lines, closestGridLine)) {
751 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
752 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
753 }
755 // search for the closest snapped guide line
756 Inkscape::SnappedLine closestGuideLine;
757 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
758 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
759 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
760 }
762 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
763 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
765 // When doing a constrained snap however, we're already at an intersection of the constrained line and
766 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
767 // no need to look for additional intersections
768 if (!constrained) {
769 // search for the closest snapped intersection of grid lines
770 Inkscape::SnappedPoint closestGridPoint;
771 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
772 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
773 sp_list.push_back(closestGridPoint);
774 }
776 // search for the closest snapped intersection of guide lines
777 Inkscape::SnappedPoint closestGuidePoint;
778 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
779 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
780 sp_list.push_back(closestGuidePoint);
781 }
783 // search for the closest snapped intersection of grid with guide lines
784 if (_intersectionGG) {
785 Inkscape::SnappedPoint closestGridGuidePoint;
786 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
787 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
788 sp_list.push_back(closestGridGuidePoint);
789 }
790 }
791 }
793 // now let's see which snapped point gets a thumbs up
794 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false);
795 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
796 // first find out if this snapped point is within snapping range
797 if ((*i).getDistance() <= (*i).getTolerance()) {
798 // if it's the first point
799 bool c1 = (i == sp_list.begin());
800 // or, if it's closer
801 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
802 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
803 bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
804 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
805 bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
806 // or, if it's just as close then consider the second distance
807 // (which is only relevant for points at an intersection)
808 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance());
809 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
810 // then prefer this point over the previous one
811 if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
812 bestSnappedPoint = *i;
813 }
814 }
815 }
818 // Update the snap indicator, if requested
819 if (_desktop_for_snapindicator) {
820 if (bestSnappedPoint.getSnapped()) {
821 _desktop_for_snapindicator->snapindicator->set_new_snappoint(bestSnappedPoint);
822 } else {
823 _desktop_for_snapindicator->snapindicator->remove_snappoint();
824 }
825 }
827 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << std::endl;
828 return bestSnappedPoint;
829 }
831 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, SPItem const *item_to_ignore, std::vector<NR::Point> *unselected_nodes)
832 {
833 _item_to_ignore = item_to_ignore;
834 _items_to_ignore = NULL;
835 _desktop_for_snapindicator = desktop_for_snapindicator;
836 _unselected_nodes = unselected_nodes;
837 }
839 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<NR::Point> *unselected_nodes)
840 {
841 _item_to_ignore = NULL;
842 _items_to_ignore = &items_to_ignore;
843 _desktop_for_snapindicator = desktop_for_snapindicator;
844 _unselected_nodes = unselected_nodes;
845 }
847 /*
848 Local Variables:
849 mode:c++
850 c-file-style:"stroustrup"
851 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
852 indent-tabs-mode:nil
853 fill-column:99
854 End:
855 */
856 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :