1 #define __SP_DESKTOP_SNAP_C__
3 /**
4 * \file snap.cpp
5 * \brief SnapManager class.
6 *
7 * Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Frank Felfe <innerspace@iname.com>
10 * Nathan Hurst <njh@njhurst.com>
11 * Carl Hetherington <inkscape@carlh.net>
12 * Diederik van Lierop <mail@diedenrezi.nl>
13 *
14 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15 * Copyrigth (C) 2004 Nathan Hurst
16 * Copyright (C) 1999-2008 Authors
17 *
18 * Released under GNU GPL, read the file 'COPYING' for more information
19 */
21 #include <utility>
23 #include "sp-namedview.h"
24 #include "snap.h"
25 #include "snapped-line.h"
26 #include "snapped-curve.h"
28 #include <libnr/nr-point-fns.h>
29 #include <libnr/nr-scale-ops.h>
30 #include <libnr/nr-values.h>
32 #include "display/canvas-grid.h"
33 #include "display/snap-indicator.h"
35 #include "inkscape.h"
36 #include "desktop.h"
37 #include "sp-guide.h"
38 using std::vector;
40 /**
41 * Construct a SnapManager for a SPNamedView.
42 *
43 * \param v `Owning' SPNamedView.
44 */
46 SnapManager::SnapManager(SPNamedView const *v) :
47 guide(this, 0),
48 object(this, 0),
49 snapprefs(),
50 _named_view(v)
51 {
52 }
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 ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
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 * Try to snap a point to any of the specified snappers.
111 *
112 * \param point_type Type of point.
113 * \param p Point.
114 * \param first_point If true then this point is the first one from a whole bunch of points
115 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
116 * \param snappers List of snappers to try to snap to
117 * \return Snapped point.
118 */
120 void SnapManager::freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
121 Geom::Point &p,
122 bool first_point,
123 boost::optional<Geom::Rect> const &bbox_to_snap) const
124 {
125 Inkscape::SnappedPoint const s = freeSnap(point_type, p, first_point, bbox_to_snap);
126 s.getPoint(p);
127 }
129 /**
130 * Try to snap a point to any of the specified snappers.
131 *
132 * \param point_type Type of point.
133 * \param p Point.
134 * \param first_point If true then this point is the first one from a whole bunch of points
135 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
136 * \param snappers List of snappers to try to snap to
137 * \return Snapped point.
138 */
140 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapPreferences::PointType point_type,
141 Geom::Point const &p,
142 bool first_point,
143 boost::optional<Geom::Rect> const &bbox_to_snap) const
144 {
145 if (!someSnapperMightSnap()) {
146 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
147 }
149 std::vector<SPItem const *> *items_to_ignore;
150 if (_item_to_ignore) { // If we have only a single item to ignore
151 // then build a list containing this single item;
152 // This single-item list will prevail over any other _items_to_ignore list, should that exist
153 items_to_ignore = new std::vector<SPItem const *>;
154 items_to_ignore->push_back(_item_to_ignore);
155 } else {
156 items_to_ignore = _items_to_ignore;
157 }
159 SnappedConstraints sc;
160 SnapperList const snappers = getSnappers();
162 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
163 (*i)->freeSnap(sc, point_type, p, first_point, bbox_to_snap, items_to_ignore, _unselected_nodes);
164 }
166 if (_item_to_ignore) {
167 delete items_to_ignore;
168 }
170 return findBestSnap(p, sc, false);
171 }
173 // When pasting, we would like to snap to the grid. Problem is that we don't know which nodes were
174 // aligned to the grid at the time of copying, so we don't know which nodes to snap. If we'd snap an
175 // unaligned node to the grid, previously aligned nodes would become unaligned. That's undesirable.
176 // Instead we will make sure that the offset between the source and the copy is a multiple of the grid
177 // pitch. If the source was aligned, then the copy will therefore also be aligned
178 // PS: Wether we really find a multiple also depends on the snapping range!
179 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
180 {
181 if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
182 return t;
184 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
185 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
187 if (desktop && desktop->gridsEnabled()) {
188 bool success = false;
189 Geom::Point nearest_multiple;
190 Geom::Coord nearest_distance = NR_HUGE;
192 // It will snap to the grid for which we find the closest snap. This might be a different
193 // grid than to which the objects were initially aligned. I don't see an easy way to fix
194 // this, so when using multiple grids one can get unexpected results
196 // Cannot use getGridSnappers() because we need both the grids AND their snappers
197 // Therefor we iterate through all grids manually
198 for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
199 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
200 const Inkscape::Snapper* snapper = grid->snapper;
201 if (snapper && snapper->ThisSnapperMightSnap()) {
202 // To find the nearest multiple of the grid pitch for a given translation t, we
203 // will use the grid snapper. Simply snapping the value t to the grid will do, but
204 // only if the origin of the grid is at (0,0). If it's not then compensate for this
205 // in the translation t
206 Geom::Point const t_offset = from_2geom(t) + grid->origin;
207 SnappedConstraints sc;
208 // Only the first three parameters are being used for grid snappers
209 snapper->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_NODE, t_offset, TRUE, boost::optional<Geom::Rect>(), NULL, NULL);
210 // Find the best snap for this grid, including intersections of the grid-lines
211 Inkscape::SnappedPoint s = findBestSnap(t_offset, sc, false);
212 if (s.getSnapped() && (s.getDistance() < nearest_distance)) {
213 success = true;
214 nearest_multiple = s.getPoint() - to_2geom(grid->origin);
215 nearest_distance = s.getDistance();
216 }
217 }
218 }
220 if (success)
221 return nearest_multiple;
222 }
224 return t;
225 }
227 /**
228 * Try to snap a point to any interested snappers. A snap will only occur along
229 * a line described by a Inkscape::Snapper::ConstraintLine.
230 *
231 * \param point_type Type of point.
232 * \param p Point.
233 * \param first_point If true then this point is the first one from a whole bunch of points
234 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
235 * \param constraint Constraint line.
236 * \return Snapped point.
237 */
239 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
240 Geom::Point &p,
241 Inkscape::Snapper::ConstraintLine const &constraint,
242 bool first_point,
243 boost::optional<Geom::Rect> const &bbox_to_snap) const
244 {
245 Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, constraint, first_point, bbox_to_snap);
246 s.getPoint(p);
247 }
249 /**
250 * Try to snap a point to any interested snappers. A snap will only occur along
251 * a line described by a Inkscape::Snapper::ConstraintLine.
252 *
253 * \param point_type Type of point.
254 * \param p Point.
255 * \param first_point If true then this point is the first one from a whole bunch of points
256 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
257 * \param constraint Constraint line.
258 * \return Snapped point.
259 */
261 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
262 Geom::Point const &p,
263 Inkscape::Snapper::ConstraintLine const &constraint,
264 bool first_point,
265 boost::optional<Geom::Rect> const &bbox_to_snap) const
266 {
267 if (!someSnapperMightSnap()) {
268 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
269 }
271 std::vector<SPItem const *> *items_to_ignore;
272 if (_item_to_ignore) { // If we have only a single item to ignore
273 // then build a list containing this single item;
274 // This single-item list will prevail over any other _items_to_ignore list, should that exist
275 items_to_ignore = new std::vector<SPItem const *>;
276 items_to_ignore->push_back(_item_to_ignore);
277 } else {
278 items_to_ignore = _items_to_ignore;
279 }
281 SnappedConstraints sc;
282 SnapperList const snappers = getSnappers();
283 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
284 (*i)->constrainedSnap(sc, point_type, p, first_point, bbox_to_snap, constraint, items_to_ignore);
285 }
287 if (_item_to_ignore) {
288 delete items_to_ignore;
289 }
291 return findBestSnap(p, sc, true);
292 }
294 void SnapManager::guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const
295 {
296 // This method is used to snap a guide to nodes, while dragging the guide around
298 if ( !(object.GuidesMightSnap() && snapprefs.getSnapEnabledGlobally()) || snapprefs.getSnapPostponedGlobally() ) {
299 return;
300 }
302 SnappedConstraints sc;
303 object.guideSnap(sc, p, guide_normal);
305 Inkscape::SnappedPoint const s = findBestSnap(p, sc, false);
306 s.getPoint(p);
307 }
310 /**
311 * Main internal snapping method, which is called by the other, friendlier, public
312 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
313 * of duplicated code.
314 *
315 * \param type Type of points being snapped.
316 * \param points List of points to snap.
317 * \param constrained true if the snap is constrained.
318 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
319 * \param transformation_type Type of transformation to apply to points before trying to snap them.
320 * \param transformation Description of the transformation; details depend on the type.
321 * \param origin Origin of the transformation, if applicable.
322 * \param dim Dimension of the transformation, if applicable.
323 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
324 */
326 Inkscape::SnappedPoint SnapManager::_snapTransformed(
327 Inkscape::SnapPreferences::PointType type,
328 std::vector<Geom::Point> const &points,
329 bool constrained,
330 Inkscape::Snapper::ConstraintLine const &constraint,
331 Transformation transformation_type,
332 Geom::Point const &transformation,
333 Geom::Point const &origin,
334 Geom::Dim2 dim,
335 bool uniform) const
336 {
337 /* We have a list of points, which we are proposing to transform in some way. We need to see
338 ** if any of these points, when transformed, snap to anything. If they do, we return the
339 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
340 */
342 /* Quick check to see if we have any snappers that are enabled
343 ** Also used to globally disable all snapping
344 */
345 if (someSnapperMightSnap() == false) {
346 g_assert(points.size() > 0);
347 return Inkscape::SnappedPoint();
348 }
350 std::vector<Geom::Point> transformed_points;
351 Geom::Rect bbox;
353 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
355 /* Work out the transformed version of this point */
356 Geom::Point transformed;
357 switch (transformation_type) {
358 case TRANSLATION:
359 transformed = *i + transformation;
360 break;
361 case SCALE:
362 transformed = (*i - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
363 break;
364 case STRETCH:
365 {
366 Geom::Scale s(1, 1);
367 if (uniform)
368 s[Geom::X] = s[Geom::Y] = transformation[dim];
369 else {
370 s[dim] = transformation[dim];
371 s[1 - dim] = 1;
372 }
373 transformed = ((*i - origin) * s) + origin;
374 break;
375 }
376 case SKEW:
377 // Apply the skew factor
378 transformed[dim] = (*i)[dim] + transformation[0] * ((*i)[1 - dim] - origin[1 - dim]);
379 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
380 // Apply that scale factor here
381 transformed[1-dim] = (*i - origin)[1 - dim] * transformation[1] + origin[1 - dim];
382 break;
383 default:
384 g_assert_not_reached();
385 }
387 // add the current transformed point to the box hulling all transformed points
388 if (i == points.begin()) {
389 bbox = Geom::Rect(transformed, transformed);
390 } else {
391 bbox.expandTo(transformed);
392 }
394 transformed_points.push_back(transformed);
395 }
397 /* The current best transformation */
398 Geom::Point best_transformation = transformation;
400 /* The current best metric for the best transformation; lower is better, NR_HUGE
401 ** means that we haven't snapped anything.
402 */
403 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
404 Inkscape::SnappedPoint best_snapped_point;
405 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
406 g_assert(best_snapped_point.getAtIntersection() == false);
408 std::vector<Geom::Point>::const_iterator j = transformed_points.begin();
410 // std::cout << std::endl;
411 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
413 /* Snap it */
414 Inkscape::SnappedPoint snapped_point;
415 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
416 Geom::Point const b = (*i - origin); // vector to original point
418 if (constrained) {
419 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
420 // When uniformly scaling, each point will have its own unique constraint line,
421 // running from the scaling origin to the original untransformed point. We will
422 // calculate that line here
423 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
424 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
425 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
426 } else if (transformation_type == TRANSLATION) {
427 // When doing a constrained translation, all points will move in the same direction, i.e.
428 // either horizontally or vertically. The lines along which they move are therefore all
429 // parallel, but might not be colinear. Therefore we will have to set the point through
430 // which the constraint-line runs here, for each point individually.
431 dedicated_constraint.setPoint(*i);
432 } // else: leave the original constraint, e.g. for skewing
433 if (transformation_type == SCALE && !uniform) {
434 g_warning("Non-uniform constrained scaling is not supported!");
435 }
436 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
437 } else {
438 bool const c1 = fabs(b[Geom::X]) < 1e-6;
439 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
440 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
441 // When scaling, a point aligned either horizontally or vertically with the origin can only
442 // move in that specific direction; therefore it should only snap in that direction, otherwise
443 // we will get snapped points with an invalid transformation
444 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
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 }
449 }
451 Geom::Point result;
452 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
454 if (snapped_point.getSnapped()) {
455 /* We snapped. Find the transformation that describes where the snapped point has
456 ** ended up, and also the metric for this transformation.
457 */
458 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
459 //Geom::Point const b = (*i - origin); // vector to original point
461 switch (transformation_type) {
462 case TRANSLATION:
463 result = snapped_point.getPoint() - *i;
464 /* Consider the case in which a box is almost aligned with a grid in both
465 * horizontal and vertical directions. The distance to the intersection of
466 * the grid lines will always be larger then the distance to a single grid
467 * line. If we prefer snapping to an intersection instead of to a single
468 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
469 * snapped distance will be used as a metric. Please note that the snapped
470 * distance is defined as the distance to the nearest line of the intersection,
471 * and not to the intersection itself!
472 */
473 // Only for translations, the relevant metric will be the real snapped distance,
474 // so we don't have to do anything special here
475 break;
476 case SCALE:
477 {
478 result = Geom::Point(NR_HUGE, NR_HUGE);
479 // If this point *i is horizontally or vertically aligned with
480 // the origin of the scaling, then it will scale purely in X or Y
481 // We can therefore only calculate the scaling in this direction
482 // and the scaling factor for the other direction should remain
483 // untouched (unless scaling is uniform ofcourse)
484 for (int index = 0; index < 2; index++) {
485 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
486 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
487 result[index] = a[index] / b[index]; // then calculate it!
488 }
489 // we might leave result[1-index] = NR_HUGE
490 // if scaling didn't occur in the other direction
491 }
492 }
493 // Compare the resulting scaling with the desired scaling
494 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
495 break;
496 }
497 case STRETCH:
498 result = Geom::Point(NR_HUGE, NR_HUGE);
499 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
500 result[dim] = a[dim] / b[dim];
501 result[1-dim] = uniform ? result[dim] : 1;
502 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
503 if (uniform && fabs(b[1-dim]) > 1e-6) {
504 result[1-dim] = a[1-dim] / b[1-dim];
505 result[dim] = result[1-dim];
506 }
507 }
508 // Store the metric for this transformation as a virtual distance
509 snapped_point.setDistance(std::abs(result[dim] - transformation[dim]));
510 snapped_point.setSecondDistance(NR_HUGE);
511 break;
512 case SKEW:
513 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
514 result[1] = transformation[1]; // scale factor
515 // Store the metric for this transformation as a virtual distance
516 snapped_point.setDistance(std::abs(result[0] - transformation[0]));
517 snapped_point.setSecondDistance(NR_HUGE);
518 break;
519 default:
520 g_assert_not_reached();
521 }
523 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
524 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
525 // the scaling metric differently
526 if (transformation_type == SCALE) {
527 for (int index = 0; index < 2; index++) {
528 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
529 best_transformation[index] = result[index];
530 best_scale_metric[index] = fabs(scale_metric[index]);
531 // When scaling, we're considering the best transformation in each direction separately
532 // Therefore two different snapped points might together make a single best transformation
533 // We will however return only a single snapped point (e.g. to display the snapping indicator)
534 best_snapped_point = snapped_point;
535 // std::cout << "SEL ";
536 } // else { std::cout << " ";}
537 }
538 if (uniform) {
539 if (best_scale_metric[0] < best_scale_metric[1]) {
540 best_transformation[1] = best_transformation[0];
541 best_scale_metric[1] = best_scale_metric[0];
542 } else {
543 best_transformation[0] = best_transformation[1];
544 best_scale_metric[0] = best_scale_metric[1];
545 }
546 }
547 } else { // For all transformations other than scaling
548 if (best_snapped_point.isOtherOneBetter(snapped_point)) {
549 best_transformation = result;
550 best_snapped_point = snapped_point;
551 }
552 }
553 }
555 j++;
556 }
558 Geom::Coord best_metric;
559 if (transformation_type == SCALE) {
560 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
561 for (int index = 0; index < 2; index++) {
562 if (best_transformation[index] == NR_HUGE) {
563 if (uniform && best_transformation[1-index] < NR_HUGE) {
564 best_transformation[index] = best_transformation[1-index];
565 } else {
566 best_transformation[index] = transformation[index];
567 }
568 }
569 }
570 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
571 } else { // For all transformations other than scaling
572 best_metric = best_snapped_point.getDistance();
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 point_type Type of points.
588 * \param p Points.
589 * \param tr Proposed translation.
590 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
591 */
593 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
594 std::vector<Geom::Point> const &p,
595 Geom::Point const &tr) const
596 {
597 return _snapTransformed(point_type, p, false, Geom::Point(), TRANSLATION, tr, Geom::Point(), Geom::X, false);
598 }
601 /**
602 * Try to snap a list of points to any interested snappers after they have undergone a
603 * translation. A snap will only occur along a line described by a
604 * Inkscape::Snapper::ConstraintLine.
605 *
606 * \param point_type Type of points.
607 * \param p Points.
608 * \param constraint Constraint line.
609 * \param tr Proposed translation.
610 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
611 */
613 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
614 std::vector<Geom::Point> const &p,
615 Inkscape::Snapper::ConstraintLine const &constraint,
616 Geom::Point const &tr) const
617 {
618 return _snapTransformed(point_type, p, true, constraint, TRANSLATION, tr, Geom::Point(), Geom::X, false);
619 }
622 /**
623 * Try to snap a list of points to any interested snappers after they have undergone
624 * a scale.
625 *
626 * \param point_type Type of points.
627 * \param p Points.
628 * \param s Proposed scale.
629 * \param o Origin of proposed scale.
630 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
631 */
633 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
634 std::vector<Geom::Point> const &p,
635 Geom::Scale const &s,
636 Geom::Point const &o) const
637 {
638 return _snapTransformed(point_type, p, false, Geom::Point(), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
639 }
642 /**
643 * Try to snap a list of points to any interested snappers after they have undergone
644 * a scale. A snap will only occur along a line described by a
645 * Inkscape::Snapper::ConstraintLine.
646 *
647 * \param point_type Type of points.
648 * \param p Points.
649 * \param s Proposed scale.
650 * \param o Origin of proposed scale.
651 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
652 */
654 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
655 std::vector<Geom::Point> const &p,
656 Geom::Scale const &s,
657 Geom::Point const &o) const
658 {
659 // When constrained scaling, only uniform scaling is supported.
660 return _snapTransformed(point_type, p, true, Geom::Point(), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
661 }
664 /**
665 * Try to snap a list of points to any interested snappers after they have undergone
666 * a stretch.
667 *
668 * \param point_type Type of points.
669 * \param p Points.
670 * \param s Proposed stretch.
671 * \param o Origin of proposed stretch.
672 * \param d Dimension in which to apply proposed stretch.
673 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
674 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
675 */
677 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
678 std::vector<Geom::Point> const &p,
679 Geom::Coord const &s,
680 Geom::Point const &o,
681 Geom::Dim2 d,
682 bool u) const
683 {
684 return _snapTransformed(point_type, p, true, Geom::Point(), STRETCH, Geom::Point(s, s), o, d, u);
685 }
688 /**
689 * Try to snap a list of points to any interested snappers after they have undergone
690 * a skew.
691 *
692 * \param point_type Type of points.
693 * \param p Points.
694 * \param s Proposed skew.
695 * \param o Origin of proposed skew.
696 * \param d Dimension in which to apply proposed skew.
697 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
698 */
700 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
701 std::vector<Geom::Point> const &p,
702 Inkscape::Snapper::ConstraintLine const &constraint,
703 Geom::Point const &s,
704 Geom::Point const &o,
705 Geom::Dim2 d) const
706 {
707 // "s" contains skew factor in s[0], and scale factor in s[1]
709 // Snapping the nodes of the boundingbox of a selection that is being transformed, will only work if
710 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
711 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
712 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
713 // of bounding boxes is not allowed here.
714 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
715 return _snapTransformed(point_type, p, true, constraint, SKEW, s, o, d, false);
716 }
718 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const
719 {
721 /*
722 std::cout << "Type and number of snapped constraints: " << std::endl;
723 std::cout << " Points : " << sc.points.size() << std::endl;
724 std::cout << " Lines : " << sc.lines.size() << std::endl;
725 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
726 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
727 std::cout << " Curves : " << sc.curves.size()<< std::endl;
728 */
730 // Store all snappoints
731 std::list<Inkscape::SnappedPoint> sp_list;
733 // search for the closest snapped point
734 Inkscape::SnappedPoint closestPoint;
735 if (getClosestSP(sc.points, closestPoint)) {
736 sp_list.push_back(closestPoint);
737 }
739 // search for the closest snapped curve
740 Inkscape::SnappedCurve closestCurve;
741 if (getClosestCurve(sc.curves, closestCurve)) {
742 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
743 }
745 if (snapprefs.getSnapIntersectionCS()) {
746 // search for the closest snapped intersection of curves
747 Inkscape::SnappedPoint closestCurvesIntersection;
748 if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection)) {
749 sp_list.push_back(closestCurvesIntersection);
750 }
751 }
753 // search for the closest snapped grid line
754 Inkscape::SnappedLine closestGridLine;
755 if (getClosestSL(sc.grid_lines, closestGridLine)) {
756 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
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 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
764 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
765 }
767 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
768 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
770 // When doing a constrained snap however, we're already at an intersection of the constrained line and
771 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
772 // no need to look for additional intersections
773 if (!constrained) {
774 // search for the closest snapped intersection of grid lines
775 Inkscape::SnappedPoint closestGridPoint;
776 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
777 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
778 sp_list.push_back(closestGridPoint);
779 }
781 // search for the closest snapped intersection of guide lines
782 Inkscape::SnappedPoint closestGuidePoint;
783 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
784 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
785 sp_list.push_back(closestGuidePoint);
786 }
788 // search for the closest snapped intersection of grid with guide lines
789 if (snapprefs.getSnapIntersectionGG()) {
790 Inkscape::SnappedPoint closestGridGuidePoint;
791 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
792 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
793 sp_list.push_back(closestGridGuidePoint);
794 }
795 }
796 }
798 // now let's see which snapped point gets a thumbs up
799 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
800 // std::cout << "Finding the best snap..." << std::endl;
801 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
802 // first find out if this snapped point is within snapping range
803 // std::cout << "sp = " << from_2geom((*i).getPoint());
804 if ((*i).getDistance() <= (*i).getTolerance()) {
805 // if it's the first point, or if it is closer than the best snapped point so far
806 if (i == sp_list.begin() || bestSnappedPoint.isOtherOneBetter(*i)) {
807 // then prefer this point over the previous one
808 bestSnappedPoint = *i;
809 }
810 }
811 // std::cout << std::endl;
812 }
814 // Update the snap indicator, if requested
815 if (_snapindicator) {
816 if (bestSnappedPoint.getSnapped()) {
817 _desktop->snapindicator->set_new_snappoint(bestSnappedPoint);
818 } else {
819 _desktop->snapindicator->remove_snappoint();
820 }
821 }
823 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << std::endl;
824 return bestSnappedPoint;
825 }
827 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, SPItem const *item_to_ignore, std::vector<Geom::Point> *unselected_nodes)
828 {
829 g_assert(desktop != NULL);
830 _item_to_ignore = item_to_ignore;
831 _items_to_ignore = NULL;
832 _desktop = desktop;
833 _snapindicator = snapindicator;
834 _unselected_nodes = unselected_nodes;
835 }
837 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes)
838 {
839 g_assert(desktop != NULL);
840 _item_to_ignore = NULL;
841 _items_to_ignore = &items_to_ignore;
842 _desktop = desktop;
843 _snapindicator = snapindicator;
844 _unselected_nodes = unselected_nodes;
845 }
847 SPDocument *SnapManager::getDocument() const
848 {
849 return _named_view->document;
850 }
852 /*
853 Local Variables:
854 mode:c++
855 c-file-style:"stroustrup"
856 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
857 indent-tabs-mode:nil
858 fill-column:99
859 End:
860 */
861 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :