e0c83ad36363c56780b61cf25014104063baac06
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 (i.e. untransformed).
317 * \param pointer Location of the mouse pointer, at the time when dragging started (i.e. "untransformed")
318 * \param constrained true if the snap is constrained.
319 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
320 * \param transformation_type Type of transformation to apply to points before trying to snap them.
321 * \param transformation Description of the transformation; details depend on the type.
322 * \param origin Origin of the transformation, if applicable.
323 * \param dim Dimension of the transformation, if applicable.
324 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
325 */
327 Inkscape::SnappedPoint SnapManager::_snapTransformed(
328 Inkscape::SnapPreferences::PointType type,
329 std::vector<Geom::Point> const &points,
330 Geom::Point const &pointer,
331 bool constrained,
332 Inkscape::Snapper::ConstraintLine const &constraint,
333 Transformation transformation_type,
334 Geom::Point const &transformation,
335 Geom::Point const &origin,
336 Geom::Dim2 dim,
337 bool uniform) const
338 {
339 /* We have a list of points, which we are proposing to transform in some way. We need to see
340 ** if any of these points, when transformed, snap to anything. If they do, we return the
341 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
342 */
344 /* Quick check to see if we have any snappers that are enabled
345 ** Also used to globally disable all snapping
346 */
347 if (someSnapperMightSnap() == false) {
348 g_assert(points.size() > 0);
349 return Inkscape::SnappedPoint();
350 }
352 std::vector<Geom::Point> transformed_points;
353 Geom::Rect bbox;
355 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
357 /* Work out the transformed version of this point */
358 Geom::Point transformed;
359 switch (transformation_type) {
360 case TRANSLATION:
361 transformed = *i + transformation;
362 break;
363 case SCALE:
364 transformed = (*i - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
365 break;
366 case STRETCH:
367 {
368 Geom::Scale s(1, 1);
369 if (uniform)
370 s[Geom::X] = s[Geom::Y] = transformation[dim];
371 else {
372 s[dim] = transformation[dim];
373 s[1 - dim] = 1;
374 }
375 transformed = ((*i - origin) * s) + origin;
376 break;
377 }
378 case SKEW:
379 // Apply the skew factor
380 transformed[dim] = (*i)[dim] + transformation[0] * ((*i)[1 - dim] - origin[1 - dim]);
381 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
382 // Apply that scale factor here
383 transformed[1-dim] = (*i - origin)[1 - dim] * transformation[1] + origin[1 - dim];
384 break;
385 default:
386 g_assert_not_reached();
387 }
389 // add the current transformed point to the box hulling all transformed points
390 if (i == points.begin()) {
391 bbox = Geom::Rect(transformed, transformed);
392 } else {
393 bbox.expandTo(transformed);
394 }
396 transformed_points.push_back(transformed);
397 }
399 /* The current best transformation */
400 Geom::Point best_transformation = transformation;
402 /* The current best metric for the best transformation; lower is better, NR_HUGE
403 ** means that we haven't snapped anything.
404 */
405 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
406 Inkscape::SnappedPoint best_snapped_point;
407 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
408 g_assert(best_snapped_point.getAtIntersection() == false);
410 std::vector<Geom::Point>::const_iterator j = transformed_points.begin();
412 // std::cout << std::endl;
413 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
415 /* Snap it */
416 Inkscape::SnappedPoint snapped_point;
417 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
418 Geom::Point const b = (*i - origin); // vector to original point
420 if (constrained) {
421 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
422 // When uniformly scaling, each point will have its own unique constraint line,
423 // running from the scaling origin to the original untransformed point. We will
424 // calculate that line here
425 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
426 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
427 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
428 } else if (transformation_type == TRANSLATION) {
429 // When doing a constrained translation, all points will move in the same direction, i.e.
430 // either horizontally or vertically. The lines along which they move are therefore all
431 // parallel, but might not be colinear. Therefore we will have to set the point through
432 // which the constraint-line runs here, for each point individually.
433 dedicated_constraint.setPoint(*i);
434 } // else: leave the original constraint, e.g. for skewing
435 if (transformation_type == SCALE && !uniform) {
436 g_warning("Non-uniform constrained scaling is not supported!");
437 }
438 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
439 } else {
440 bool const c1 = fabs(b[Geom::X]) < 1e-6;
441 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
442 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
443 // When scaling, a point aligned either horizontally or vertically with the origin can only
444 // move in that specific direction; therefore it should only snap in that direction, otherwise
445 // we will get snapped points with an invalid transformation
446 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
447 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
448 } else {
449 snapped_point = freeSnap(type, *j, i == points.begin(), bbox);
450 }
451 snapped_point.setPointerDistance(Geom::L2(pointer - *i));
452 }
454 Geom::Point result;
455 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
457 if (snapped_point.getSnapped()) {
458 /* We snapped. Find the transformation that describes where the snapped point has
459 ** ended up, and also the metric for this transformation.
460 */
461 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
462 //Geom::Point const b = (*i - origin); // vector to original point
464 switch (transformation_type) {
465 case TRANSLATION:
466 result = snapped_point.getPoint() - *i;
467 /* Consider the case in which a box is almost aligned with a grid in both
468 * horizontal and vertical directions. The distance to the intersection of
469 * the grid lines will always be larger then the distance to a single grid
470 * line. If we prefer snapping to an intersection instead of to a single
471 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
472 * snapped distance will be used as a metric. Please note that the snapped
473 * distance is defined as the distance to the nearest line of the intersection,
474 * and not to the intersection itself!
475 */
476 // Only for translations, the relevant metric will be the real snapped distance,
477 // so we don't have to do anything special here
478 break;
479 case SCALE:
480 {
481 result = Geom::Point(NR_HUGE, NR_HUGE);
482 // If this point *i is horizontally or vertically aligned with
483 // the origin of the scaling, then it will scale purely in X or Y
484 // We can therefore only calculate the scaling in this direction
485 // and the scaling factor for the other direction should remain
486 // untouched (unless scaling is uniform ofcourse)
487 for (int index = 0; index < 2; index++) {
488 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
489 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
490 result[index] = a[index] / b[index]; // then calculate it!
491 }
492 // we might leave result[1-index] = NR_HUGE
493 // if scaling didn't occur in the other direction
494 }
495 }
496 // Compare the resulting scaling with the desired scaling
497 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
498 break;
499 }
500 case STRETCH:
501 result = Geom::Point(NR_HUGE, NR_HUGE);
502 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
503 result[dim] = a[dim] / b[dim];
504 result[1-dim] = uniform ? result[dim] : 1;
505 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
506 if (uniform && fabs(b[1-dim]) > 1e-6) {
507 result[1-dim] = a[1-dim] / b[1-dim];
508 result[dim] = result[1-dim];
509 }
510 }
511 // Store the metric for this transformation as a virtual distance
512 snapped_point.setDistance(std::abs(result[dim] - transformation[dim]));
513 snapped_point.setSecondDistance(NR_HUGE);
514 break;
515 case SKEW:
516 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
517 result[1] = transformation[1]; // scale factor
518 // Store the metric for this transformation as a virtual distance
519 snapped_point.setDistance(std::abs(result[0] - transformation[0]));
520 snapped_point.setSecondDistance(NR_HUGE);
521 break;
522 default:
523 g_assert_not_reached();
524 }
526 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
527 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
528 // the scaling metric differently
529 if (transformation_type == SCALE) {
530 for (int index = 0; index < 2; index++) {
531 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
532 best_transformation[index] = result[index];
533 best_scale_metric[index] = fabs(scale_metric[index]);
534 // When scaling, we're considering the best transformation in each direction separately
535 // Therefore two different snapped points might together make a single best transformation
536 // We will however return only a single snapped point (e.g. to display the snapping indicator)
537 best_snapped_point = snapped_point;
538 // std::cout << "SEL ";
539 } // else { std::cout << " ";}
540 }
541 if (uniform) {
542 if (best_scale_metric[0] < best_scale_metric[1]) {
543 best_transformation[1] = best_transformation[0];
544 best_scale_metric[1] = best_scale_metric[0];
545 } else {
546 best_transformation[0] = best_transformation[1];
547 best_scale_metric[0] = best_scale_metric[1];
548 }
549 }
550 } else { // For all transformations other than scaling
551 if (best_snapped_point.isOtherOneBetter(snapped_point)) {
552 best_transformation = result;
553 best_snapped_point = snapped_point;
554 }
555 }
556 }
558 j++;
559 }
561 Geom::Coord best_metric;
562 if (transformation_type == SCALE) {
563 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
564 for (int index = 0; index < 2; index++) {
565 if (best_transformation[index] == NR_HUGE) {
566 if (uniform && best_transformation[1-index] < NR_HUGE) {
567 best_transformation[index] = best_transformation[1-index];
568 } else {
569 best_transformation[index] = transformation[index];
570 }
571 }
572 }
573 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
574 } else { // For all transformations other than scaling
575 best_metric = best_snapped_point.getDistance();
576 }
578 best_snapped_point.setTransformation(best_transformation);
579 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
580 // These rounding errors might be caused by NRRects, see bug #1584301
581 best_snapped_point.setDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
582 return best_snapped_point;
583 }
586 /**
587 * Try to snap a list of points to any interested snappers after they have undergone
588 * a translation.
589 *
590 * \param point_type Type of points.
591 * \param p Points.
592 * \param tr Proposed translation.
593 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
594 */
596 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
597 std::vector<Geom::Point> const &p,
598 Geom::Point const &pointer,
599 Geom::Point const &tr) const
600 {
601 return _snapTransformed(point_type, p, pointer, false, Geom::Point(), TRANSLATION, tr, Geom::Point(), Geom::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::SnapPreferences::PointType point_type,
618 std::vector<Geom::Point> const &p,
619 Geom::Point const &pointer,
620 Inkscape::Snapper::ConstraintLine const &constraint,
621 Geom::Point const &tr) const
622 {
623 return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(), Geom::X, false);
624 }
627 /**
628 * Try to snap a list of points to any interested snappers after they have undergone
629 * a scale.
630 *
631 * \param point_type Type of points.
632 * \param p Points.
633 * \param s Proposed scale.
634 * \param o Origin of proposed scale.
635 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
636 */
638 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
639 std::vector<Geom::Point> const &p,
640 Geom::Point const &pointer,
641 Geom::Scale const &s,
642 Geom::Point const &o) const
643 {
644 return _snapTransformed(point_type, p, pointer, false, Geom::Point(), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
645 }
648 /**
649 * Try to snap a list of points to any interested snappers after they have undergone
650 * a scale. A snap will only occur along a line described by a
651 * Inkscape::Snapper::ConstraintLine.
652 *
653 * \param point_type Type of points.
654 * \param p Points.
655 * \param s Proposed scale.
656 * \param o Origin of proposed scale.
657 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
658 */
660 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
661 std::vector<Geom::Point> const &p,
662 Geom::Point const &pointer,
663 Geom::Scale const &s,
664 Geom::Point const &o) const
665 {
666 // When constrained scaling, only uniform scaling is supported.
667 return _snapTransformed(point_type, p, pointer, true, Geom::Point(), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
668 }
671 /**
672 * Try to snap a list of points to any interested snappers after they have undergone
673 * a stretch.
674 *
675 * \param point_type Type of points.
676 * \param p Points.
677 * \param s Proposed stretch.
678 * \param o Origin of proposed stretch.
679 * \param d Dimension in which to apply proposed stretch.
680 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
681 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
682 */
684 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
685 std::vector<Geom::Point> const &p,
686 Geom::Point const &pointer,
687 Geom::Coord const &s,
688 Geom::Point const &o,
689 Geom::Dim2 d,
690 bool u) const
691 {
692 return _snapTransformed(point_type, p, pointer, true, Geom::Point(), STRETCH, Geom::Point(s, s), o, d, u);
693 }
696 /**
697 * Try to snap a list of points to any interested snappers after they have undergone
698 * a skew.
699 *
700 * \param point_type Type of points.
701 * \param p Points.
702 * \param s Proposed skew.
703 * \param o Origin of proposed skew.
704 * \param d Dimension in which to apply proposed skew.
705 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
706 */
708 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
709 std::vector<Geom::Point> const &p,
710 Geom::Point const &pointer,
711 Inkscape::Snapper::ConstraintLine const &constraint,
712 Geom::Point const &s,
713 Geom::Point const &o,
714 Geom::Dim2 d) const
715 {
716 // "s" contains skew factor in s[0], and scale factor in s[1]
718 // Snapping the nodes of the boundingbox of a selection that is being transformed, will only work if
719 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
720 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
721 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
722 // of bounding boxes is not allowed here.
723 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
724 return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
725 }
727 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const
728 {
730 /*
731 std::cout << "Type and number of snapped constraints: " << std::endl;
732 std::cout << " Points : " << sc.points.size() << std::endl;
733 std::cout << " Lines : " << sc.lines.size() << std::endl;
734 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
735 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
736 std::cout << " Curves : " << sc.curves.size()<< std::endl;
737 */
739 // Store all snappoints
740 std::list<Inkscape::SnappedPoint> sp_list;
742 // search for the closest snapped point
743 Inkscape::SnappedPoint closestPoint;
744 if (getClosestSP(sc.points, closestPoint)) {
745 sp_list.push_back(closestPoint);
746 }
748 // search for the closest snapped curve
749 Inkscape::SnappedCurve closestCurve;
750 if (getClosestCurve(sc.curves, closestCurve)) {
751 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
752 }
754 if (snapprefs.getSnapIntersectionCS()) {
755 // search for the closest snapped intersection of curves
756 Inkscape::SnappedPoint closestCurvesIntersection;
757 if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection)) {
758 sp_list.push_back(closestCurvesIntersection);
759 }
760 }
762 // search for the closest snapped grid line
763 Inkscape::SnappedLine closestGridLine;
764 if (getClosestSL(sc.grid_lines, closestGridLine)) {
765 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
766 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
767 }
769 // search for the closest snapped guide line
770 Inkscape::SnappedLine closestGuideLine;
771 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
772 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
773 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
774 }
776 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
777 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
779 // When doing a constrained snap however, we're already at an intersection of the constrained line and
780 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
781 // no need to look for additional intersections
782 if (!constrained) {
783 // search for the closest snapped intersection of grid lines
784 Inkscape::SnappedPoint closestGridPoint;
785 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
786 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
787 sp_list.push_back(closestGridPoint);
788 }
790 // search for the closest snapped intersection of guide lines
791 Inkscape::SnappedPoint closestGuidePoint;
792 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
793 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
794 sp_list.push_back(closestGuidePoint);
795 }
797 // search for the closest snapped intersection of grid with guide lines
798 if (snapprefs.getSnapIntersectionGG()) {
799 Inkscape::SnappedPoint closestGridGuidePoint;
800 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
801 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
802 sp_list.push_back(closestGridGuidePoint);
803 }
804 }
805 }
807 // now let's see which snapped point gets a thumbs up
808 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
809 // std::cout << "Finding the best snap..." << std::endl;
810 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
811 // first find out if this snapped point is within snapping range
812 // std::cout << "sp = " << from_2geom((*i).getPoint());
813 if ((*i).getDistance() <= (*i).getTolerance()) {
814 // if it's the first point, or if it is closer than the best snapped point so far
815 if (i == sp_list.begin() || bestSnappedPoint.isOtherOneBetter(*i)) {
816 // then prefer this point over the previous one
817 bestSnappedPoint = *i;
818 }
819 }
820 // std::cout << std::endl;
821 }
823 // Update the snap indicator, if requested
824 if (_snapindicator) {
825 if (bestSnappedPoint.getSnapped()) {
826 _desktop->snapindicator->set_new_snappoint(bestSnappedPoint);
827 } else {
828 _desktop->snapindicator->remove_snappoint();
829 }
830 }
832 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << std::endl;
833 return bestSnappedPoint;
834 }
836 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, SPItem const *item_to_ignore, std::vector<Geom::Point> *unselected_nodes)
837 {
838 g_assert(desktop != NULL);
839 _item_to_ignore = item_to_ignore;
840 _items_to_ignore = NULL;
841 _desktop = desktop;
842 _snapindicator = snapindicator;
843 _unselected_nodes = unselected_nodes;
844 }
846 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes)
847 {
848 g_assert(desktop != NULL);
849 _item_to_ignore = NULL;
850 _items_to_ignore = &items_to_ignore;
851 _desktop = desktop;
852 _snapindicator = snapindicator;
853 _unselected_nodes = unselected_nodes;
854 }
856 SPDocument *SnapManager::getDocument() const
857 {
858 return _named_view->document;
859 }
861 /*
862 Local Variables:
863 mode:c++
864 c-file-style:"stroustrup"
865 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
866 indent-tabs-mode:nil
867 fill-column:99
868 End:
869 */
870 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :