7404ed12b8eaea62a60476e40fec1e3837a01f06
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-2009 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 #include "preferences.h"
39 using std::vector;
41 /**
42 * Construct a SnapManager for a SPNamedView.
43 *
44 * \param v `Owning' SPNamedView.
45 */
47 SnapManager::SnapManager(SPNamedView const *v) :
48 guide(this, 0),
49 object(this, 0),
50 snapprefs(),
51 _named_view(v)
52 {
53 }
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 ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
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 * \return true if one of the snappers will try to snap something.
112 */
114 bool SnapManager::gridSnapperMightSnap() const
115 {
116 if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
117 return false;
118 }
120 SnapperList const s = getGridSnappers();
121 SnapperList::const_iterator i = s.begin();
122 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
123 i++;
124 }
126 return (i != s.end());
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 void SnapManager::freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
141 Geom::Point &p,
142 bool first_point,
143 Geom::OptRect const &bbox_to_snap) const
144 {
145 Inkscape::SnappedPoint const s = freeSnap(point_type, p, first_point, bbox_to_snap);
146 s.getPoint(p);
147 }
149 /**
150 * Try to snap a point to any of the specified snappers.
151 *
152 * \param point_type Type of point.
153 * \param p Point.
154 * \param first_point If true then this point is the first one from a whole bunch of points
155 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
156 * \param snappers List of snappers to try to snap to
157 * \return Snapped point.
158 */
160 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapPreferences::PointType point_type,
161 Geom::Point const &p,
162 bool first_point,
163 Geom::OptRect const &bbox_to_snap) const
164 {
165 if (_desktop->canvas->context_snap_delay_active == false) {
166 g_warning("context_snap_delay_active has not been set to true by the current context. Please report this!");
167 // When the context goes into dragging-mode, then Inkscape should call this: sp_canvas_set_snap_delay_active(desktop->canvas, true);
168 }
170 if (!someSnapperMightSnap()) {
171 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
172 }
174 std::vector<SPItem const *> *items_to_ignore;
175 if (_item_to_ignore) { // If we have only a single item to ignore
176 // then build a list containing this single item;
177 // This single-item list will prevail over any other _items_to_ignore list, should that exist
178 items_to_ignore = new std::vector<SPItem const *>;
179 items_to_ignore->push_back(_item_to_ignore);
180 } else {
181 items_to_ignore = _items_to_ignore;
182 }
184 SnappedConstraints sc;
185 SnapperList const snappers = getSnappers();
187 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
188 (*i)->freeSnap(sc, point_type, p, first_point, bbox_to_snap, items_to_ignore, _unselected_nodes);
189 }
191 if (_item_to_ignore) {
192 delete items_to_ignore;
193 }
195 return findBestSnap(p, sc, false);
196 }
198 // When pasting, we would like to snap to the grid. Problem is that we don't know which nodes were
199 // aligned to the grid at the time of copying, so we don't know which nodes to snap. If we'd snap an
200 // unaligned node to the grid, previously aligned nodes would become unaligned. That's undesirable.
201 // Instead we will make sure that the offset between the source and the copy is a multiple of the grid
202 // pitch. If the source was aligned, then the copy will therefore also be aligned
203 // PS: Wether we really find a multiple also depends on the snapping range!
204 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
205 {
206 if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
207 return t;
209 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
210 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
212 if (desktop && desktop->gridsEnabled()) {
213 bool success = false;
214 Geom::Point nearest_multiple;
215 Geom::Coord nearest_distance = NR_HUGE;
217 // It will snap to the grid for which we find the closest snap. This might be a different
218 // grid than to which the objects were initially aligned. I don't see an easy way to fix
219 // this, so when using multiple grids one can get unexpected results
221 // Cannot use getGridSnappers() because we need both the grids AND their snappers
222 // Therefor we iterate through all grids manually
223 for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
224 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
225 const Inkscape::Snapper* snapper = grid->snapper;
226 if (snapper && snapper->ThisSnapperMightSnap()) {
227 // To find the nearest multiple of the grid pitch for a given translation t, we
228 // will use the grid snapper. Simply snapping the value t to the grid will do, but
229 // only if the origin of the grid is at (0,0). If it's not then compensate for this
230 // in the translation t
231 Geom::Point const t_offset = t + grid->origin;
232 SnappedConstraints sc;
233 // Only the first three parameters are being used for grid snappers
234 snapper->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_NODE, t_offset, TRUE, Geom::OptRect(), NULL, NULL);
235 // Find the best snap for this grid, including intersections of the grid-lines
236 Inkscape::SnappedPoint s = findBestSnap(t_offset, sc, false);
237 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
238 // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
239 // doesn't tell us anything about which node to snap
240 success = true;
241 nearest_multiple = s.getPoint() - to_2geom(grid->origin);
242 nearest_distance = s.getSnapDistance();
243 }
244 }
245 }
247 if (success)
248 return nearest_multiple;
249 }
251 return t;
252 }
254 /**
255 * Try to snap a point to any interested snappers. A snap will only occur along
256 * a line described by a Inkscape::Snapper::ConstraintLine.
257 *
258 * \param point_type Type of point.
259 * \param p Point.
260 * \param first_point If true then this point is the first one from a whole bunch of points
261 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
262 * \param constraint Constraint line.
263 * \return Snapped point.
264 */
266 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
267 Geom::Point &p,
268 Inkscape::Snapper::ConstraintLine const &constraint,
269 bool first_point,
270 Geom::OptRect const &bbox_to_snap) const
271 {
272 Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, constraint, first_point, bbox_to_snap);
273 s.getPoint(p);
274 }
276 /**
277 * Try to snap a point to any interested snappers. A snap will only occur along
278 * a line described by a Inkscape::Snapper::ConstraintLine.
279 *
280 * \param point_type Type of point.
281 * \param p Point.
282 * \param first_point If true then this point is the first one from a whole bunch of points
283 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
284 * \param constraint Constraint line.
285 * \return Snapped point.
286 */
288 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
289 Geom::Point const &p,
290 Inkscape::Snapper::ConstraintLine const &constraint,
291 bool first_point,
292 Geom::OptRect const &bbox_to_snap) const
293 {
294 if (_desktop->canvas->context_snap_delay_active == false) {
295 g_warning("context_snap_delay_active has not been set to true by the current context. Please report this!");
296 // When the context goes into dragging-mode, then Inkscape should call this: sp_canvas_set_snap_delay_active(desktop->canvas, true);
297 }
299 if (!someSnapperMightSnap()) {
300 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
301 }
303 std::vector<SPItem const *> *items_to_ignore;
304 if (_item_to_ignore) { // If we have only a single item to ignore
305 // then build a list containing this single item;
306 // This single-item list will prevail over any other _items_to_ignore list, should that exist
307 items_to_ignore = new std::vector<SPItem const *>;
308 items_to_ignore->push_back(_item_to_ignore);
309 } else {
310 items_to_ignore = _items_to_ignore;
311 }
313 SnappedConstraints sc;
314 SnapperList const snappers = getSnappers();
315 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
316 (*i)->constrainedSnap(sc, point_type, p, first_point, bbox_to_snap, constraint, items_to_ignore);
317 }
319 if (_item_to_ignore) {
320 delete items_to_ignore;
321 }
323 return findBestSnap(p, sc, true);
324 }
326 void SnapManager::guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const
327 {
328 // This method is used to snap a guide to nodes, while dragging the guide around
330 if (_desktop->canvas->context_snap_delay_active == false) {
331 g_warning("context_snap_delay_active has not been set to true by the current context. Please report this!");
332 // When the context goes into dragging-mode, then Inkscape should call this: sp_canvas_set_snap_delay_active(desktop->canvas, true);
333 }
335 if ( !(object.GuidesMightSnap() && snapprefs.getSnapEnabledGlobally()) || snapprefs.getSnapPostponedGlobally() ) {
336 return;
337 }
339 SnappedConstraints sc;
340 object.guideSnap(sc, p, guide_normal);
342 Inkscape::SnappedPoint const s = findBestSnap(p, sc, false);
343 s.getPoint(p);
344 }
347 /**
348 * Main internal snapping method, which is called by the other, friendlier, public
349 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
350 * of duplicated code.
351 *
352 * \param type Type of points being snapped.
353 * \param points List of points to snap (i.e. untransformed).
354 * \param pointer Location of the mouse pointer, at the time when dragging started (i.e. "untransformed")
355 * \param constrained true if the snap is constrained.
356 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
357 * \param transformation_type Type of transformation to apply to points before trying to snap them.
358 * \param transformation Description of the transformation; details depend on the type.
359 * \param origin Origin of the transformation, if applicable.
360 * \param dim Dimension of the transformation, if applicable.
361 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
362 */
364 Inkscape::SnappedPoint SnapManager::_snapTransformed(
365 Inkscape::SnapPreferences::PointType type,
366 std::vector<Geom::Point> const &points,
367 Geom::Point const &pointer,
368 bool constrained,
369 Inkscape::Snapper::ConstraintLine const &constraint,
370 Transformation transformation_type,
371 Geom::Point const &transformation,
372 Geom::Point const &origin,
373 Geom::Dim2 dim,
374 bool uniform) const
375 {
376 /* We have a list of points, which we are proposing to transform in some way. We need to see
377 ** if any of these points, when transformed, snap to anything. If they do, we return the
378 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
379 */
381 /* Quick check to see if we have any snappers that are enabled
382 ** Also used to globally disable all snapping
383 */
384 if (someSnapperMightSnap() == false) {
385 return Inkscape::SnappedPoint();
386 }
388 std::vector<Geom::Point> transformed_points;
389 Geom::Rect bbox;
391 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
393 /* Work out the transformed version of this point */
394 Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
396 // add the current transformed point to the box hulling all transformed points
397 if (i == points.begin()) {
398 bbox = Geom::Rect(transformed, transformed);
399 } else {
400 bbox.expandTo(transformed);
401 }
403 transformed_points.push_back(transformed);
404 }
406 /* The current best transformation */
407 Geom::Point best_transformation = transformation;
409 /* The current best metric for the best transformation; lower is better, NR_HUGE
410 ** means that we haven't snapped anything.
411 */
412 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
413 Inkscape::SnappedPoint best_snapped_point;
414 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
415 g_assert(best_snapped_point.getAtIntersection() == false);
417 std::vector<Geom::Point>::const_iterator j = transformed_points.begin();
419 // std::cout << std::endl;
420 for (std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
422 /* Snap it */
423 Inkscape::SnappedPoint snapped_point;
424 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
425 Geom::Point const b = (*i - origin); // vector to original point
427 if (constrained) {
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, b);
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 bool const c1 = fabs(b[Geom::X]) < 1e-6;
448 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
449 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
450 // When scaling, a point aligned either horizontally or vertically with the origin can only
451 // move in that specific direction; therefore it should only snap in that direction, otherwise
452 // we will get snapped points with an invalid transformation
453 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
454 snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
455 } else {
456 snapped_point = freeSnap(type, *j, i == points.begin(), bbox);
457 }
458 }
459 // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
460 snapped_point.setPointerDistance(Geom::L2(pointer - *i));
462 Geom::Point result;
463 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
465 if (snapped_point.getSnapped()) {
466 /* We snapped. Find the transformation that describes where the snapped point has
467 ** ended up, and also the metric for this transformation.
468 */
469 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
470 //Geom::Point const b = (*i - origin); // vector to original point
472 switch (transformation_type) {
473 case TRANSLATION:
474 result = snapped_point.getPoint() - *i;
475 /* Consider the case in which a box is almost aligned with a grid in both
476 * horizontal and vertical directions. The distance to the intersection of
477 * the grid lines will always be larger then the distance to a single grid
478 * line. If we prefer snapping to an intersection instead of to a single
479 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
480 * snapped distance will be used as a metric. Please note that the snapped
481 * distance is defined as the distance to the nearest line of the intersection,
482 * and not to the intersection itself!
483 */
484 // Only for translations, the relevant metric will be the real snapped distance,
485 // so we don't have to do anything special here
486 break;
487 case SCALE:
488 {
489 result = Geom::Point(NR_HUGE, NR_HUGE);
490 // If this point *i is horizontally or vertically aligned with
491 // the origin of the scaling, then it will scale purely in X or Y
492 // We can therefore only calculate the scaling in this direction
493 // and the scaling factor for the other direction should remain
494 // untouched (unless scaling is uniform ofcourse)
495 for (int index = 0; index < 2; index++) {
496 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
497 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
498 result[index] = a[index] / b[index]; // then calculate it!
499 }
500 // we might leave result[1-index] = NR_HUGE
501 // if scaling didn't occur in the other direction
502 }
503 }
504 // Compare the resulting scaling with the desired scaling
505 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
506 break;
507 }
508 case STRETCH:
509 result = Geom::Point(NR_HUGE, NR_HUGE);
510 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
511 result[dim] = a[dim] / b[dim];
512 result[1-dim] = uniform ? result[dim] : 1;
513 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
514 if (uniform && fabs(b[1-dim]) > 1e-6) {
515 result[1-dim] = a[1-dim] / b[1-dim];
516 result[dim] = result[1-dim];
517 }
518 }
519 // Store the metric for this transformation as a virtual distance
520 snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
521 snapped_point.setSecondSnapDistance(NR_HUGE);
522 break;
523 case SKEW:
524 result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
525 result[1] = transformation[1]; // scale factor
526 // Store the metric for this transformation as a virtual distance
527 snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
528 snapped_point.setSecondSnapDistance(NR_HUGE);
529 break;
530 default:
531 g_assert_not_reached();
532 }
534 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
535 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
536 // the scaling metric differently
537 if (transformation_type == SCALE) {
538 for (int index = 0; index < 2; index++) {
539 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
540 best_transformation[index] = result[index];
541 best_scale_metric[index] = fabs(scale_metric[index]);
542 // When scaling, we're considering the best transformation in each direction separately
543 // Therefore two different snapped points might together make a single best transformation
544 // We will however return only a single snapped point (e.g. to display the snapping indicator)
545 best_snapped_point = snapped_point;
546 // std::cout << "SEL ";
547 } // else { std::cout << " ";}
548 }
549 if (uniform) {
550 if (best_scale_metric[0] < best_scale_metric[1]) {
551 best_transformation[1] = best_transformation[0];
552 best_scale_metric[1] = best_scale_metric[0];
553 } else {
554 best_transformation[0] = best_transformation[1];
555 best_scale_metric[0] = best_scale_metric[1];
556 }
557 }
558 } else { // For all transformations other than scaling
559 if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
560 best_transformation = result;
561 best_snapped_point = snapped_point;
562 }
563 }
564 }
566 j++;
567 }
569 Geom::Coord best_metric;
570 if (transformation_type == SCALE) {
571 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
572 for (int index = 0; index < 2; index++) {
573 if (best_transformation[index] == NR_HUGE) {
574 if (uniform && best_transformation[1-index] < NR_HUGE) {
575 best_transformation[index] = best_transformation[1-index];
576 } else {
577 best_transformation[index] = transformation[index];
578 }
579 }
580 }
581 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
582 } else { // For all transformations other than scaling
583 best_metric = best_snapped_point.getSnapDistance();
584 }
586 best_snapped_point.setTransformation(best_transformation);
587 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
588 // These rounding errors might be caused by NRRects, see bug #1584301
589 best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
590 return best_snapped_point;
591 }
594 /**
595 * Try to snap a list of points to any interested snappers after they have undergone
596 * a translation.
597 *
598 * \param point_type Type of points.
599 * \param p Points.
600 * \param tr Proposed translation.
601 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
602 */
604 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
605 std::vector<Geom::Point> const &p,
606 Geom::Point const &pointer,
607 Geom::Point const &tr) const
608 {
609 if (p.size() == 1) {
610 _displaySnapsource(point_type, _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false));
611 }
613 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
614 }
617 /**
618 * Try to snap a list of points to any interested snappers after they have undergone a
619 * translation. A snap will only occur along a line described by a
620 * Inkscape::Snapper::ConstraintLine.
621 *
622 * \param point_type Type of points.
623 * \param p Points.
624 * \param constraint Constraint line.
625 * \param tr Proposed translation.
626 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
627 */
629 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
630 std::vector<Geom::Point> const &p,
631 Geom::Point const &pointer,
632 Inkscape::Snapper::ConstraintLine const &constraint,
633 Geom::Point const &tr) const
634 {
635 if (p.size() == 1) {
636 _displaySnapsource(point_type, _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false));
637 }
639 return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
640 }
643 /**
644 * Try to snap a list of points to any interested snappers after they have undergone
645 * a scale.
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::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
655 std::vector<Geom::Point> const &p,
656 Geom::Point const &pointer,
657 Geom::Scale const &s,
658 Geom::Point const &o) const
659 {
660 if (p.size() == 1) {
661 _displaySnapsource(point_type, _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false));
662 }
664 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
665 }
668 /**
669 * Try to snap a list of points to any interested snappers after they have undergone
670 * a scale. A snap will only occur along a line described by a
671 * Inkscape::Snapper::ConstraintLine.
672 *
673 * \param point_type Type of points.
674 * \param p Points.
675 * \param s Proposed scale.
676 * \param o Origin of proposed scale.
677 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
678 */
680 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
681 std::vector<Geom::Point> const &p,
682 Geom::Point const &pointer,
683 Geom::Scale const &s,
684 Geom::Point const &o) const
685 {
686 // When constrained scaling, only uniform scaling is supported.
687 if (p.size() == 1) {
688 _displaySnapsource(point_type, _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true));
689 }
691 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
692 }
695 /**
696 * Try to snap a list of points to any interested snappers after they have undergone
697 * a stretch.
698 *
699 * \param point_type Type of points.
700 * \param p Points.
701 * \param s Proposed stretch.
702 * \param o Origin of proposed stretch.
703 * \param d Dimension in which to apply proposed stretch.
704 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
705 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
706 */
708 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
709 std::vector<Geom::Point> const &p,
710 Geom::Point const &pointer,
711 Geom::Coord const &s,
712 Geom::Point const &o,
713 Geom::Dim2 d,
714 bool u) const
715 {
716 if (p.size() == 1) {
717 _displaySnapsource(point_type, _transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u));
718 }
720 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
721 }
724 /**
725 * Try to snap a list of points to any interested snappers after they have undergone
726 * a skew.
727 *
728 * \param point_type Type of points.
729 * \param p Points.
730 * \param s Proposed skew.
731 * \param o Origin of proposed skew.
732 * \param d Dimension in which to apply proposed skew.
733 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
734 */
736 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
737 std::vector<Geom::Point> const &p,
738 Geom::Point const &pointer,
739 Inkscape::Snapper::ConstraintLine const &constraint,
740 Geom::Point const &s,
741 Geom::Point const &o,
742 Geom::Dim2 d) const
743 {
744 // "s" contains skew factor in s[0], and scale factor in s[1]
746 // Snapping the nodes of the boundingbox of a selection that is being transformed, will only work if
747 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
748 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
749 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
750 // of bounding boxes is not allowed here.
751 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
753 if (p.size() == 1) {
754 _displaySnapsource(point_type, _transformPoint(p.at(0), SKEW, s, o, d, false));
755 }
757 return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
758 }
760 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const
761 {
763 /*
764 std::cout << "Type and number of snapped constraints: " << std::endl;
765 std::cout << " Points : " << sc.points.size() << std::endl;
766 std::cout << " Lines : " << sc.lines.size() << std::endl;
767 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
768 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
769 std::cout << " Curves : " << sc.curves.size()<< std::endl;
770 */
772 // Store all snappoints
773 std::list<Inkscape::SnappedPoint> sp_list;
775 // search for the closest snapped point
776 Inkscape::SnappedPoint closestPoint;
777 if (getClosestSP(sc.points, closestPoint)) {
778 sp_list.push_back(closestPoint);
779 }
781 // search for the closest snapped curve
782 Inkscape::SnappedCurve closestCurve;
783 if (getClosestCurve(sc.curves, closestCurve)) {
784 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
785 }
787 if (snapprefs.getSnapIntersectionCS()) {
788 // search for the closest snapped intersection of curves
789 Inkscape::SnappedPoint closestCurvesIntersection;
790 if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection)) {
791 sp_list.push_back(closestCurvesIntersection);
792 }
793 }
795 // search for the closest snapped grid line
796 Inkscape::SnappedLine closestGridLine;
797 if (getClosestSL(sc.grid_lines, closestGridLine)) {
798 closestGridLine.setTarget(Inkscape::SNAPTARGET_GRID);
799 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
800 }
802 // search for the closest snapped guide line
803 Inkscape::SnappedLine closestGuideLine;
804 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
805 closestGuideLine.setTarget(Inkscape::SNAPTARGET_GUIDE);
806 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
807 }
809 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
810 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
812 // When doing a constrained snap however, we're already at an intersection of the constrained line and
813 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
814 // no need to look for additional intersections
815 if (!constrained) {
816 // search for the closest snapped intersection of grid lines
817 Inkscape::SnappedPoint closestGridPoint;
818 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
819 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
820 sp_list.push_back(closestGridPoint);
821 }
823 // search for the closest snapped intersection of guide lines
824 Inkscape::SnappedPoint closestGuidePoint;
825 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
826 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
827 sp_list.push_back(closestGuidePoint);
828 }
830 // search for the closest snapped intersection of grid with guide lines
831 if (snapprefs.getSnapIntersectionGG()) {
832 Inkscape::SnappedPoint closestGridGuidePoint;
833 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
834 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
835 sp_list.push_back(closestGridGuidePoint);
836 }
837 }
838 }
840 // now let's see which snapped point gets a thumbs up
841 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
842 // std::cout << "Finding the best snap..." << std::endl;
843 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
844 // first find out if this snapped point is within snapping range
845 // std::cout << "sp = " << from_2geom((*i).getPoint());
846 if ((*i).getSnapDistance() <= (*i).getTolerance()) {
847 // if it's the first point, or if it is closer than the best snapped point so far
848 if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
849 // then prefer this point over the previous one
850 bestSnappedPoint = *i;
851 }
852 }
853 // std::cout << std::endl;
854 }
856 // Update the snap indicator, if requested
857 if (_snapindicator) {
858 if (bestSnappedPoint.getSnapped()) {
859 _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
860 } else {
861 _desktop->snapindicator->remove_snaptarget();
862 }
863 }
865 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
866 return bestSnappedPoint;
867 }
869 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, SPItem const *item_to_ignore, std::vector<Geom::Point> *unselected_nodes)
870 {
871 g_assert(desktop != NULL);
872 _item_to_ignore = item_to_ignore;
873 _items_to_ignore = NULL;
874 _desktop = desktop;
875 _snapindicator = snapindicator;
876 _unselected_nodes = unselected_nodes;
877 }
879 void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes)
880 {
881 g_assert(desktop != NULL);
882 _item_to_ignore = NULL;
883 _items_to_ignore = &items_to_ignore;
884 _desktop = desktop;
885 _snapindicator = snapindicator;
886 _unselected_nodes = unselected_nodes;
887 }
889 SPDocument *SnapManager::getDocument() const
890 {
891 return _named_view->document;
892 }
894 Geom::Point SnapManager::_transformPoint(Geom::Point const &p,
895 Transformation const transformation_type,
896 Geom::Point const &transformation,
897 Geom::Point const &origin,
898 Geom::Dim2 const dim,
899 bool const uniform) const
900 {
901 /* Work out the transformed version of this point */
902 Geom::Point transformed;
903 switch (transformation_type) {
904 case TRANSLATION:
905 transformed = p + transformation;
906 break;
907 case SCALE:
908 transformed = (p - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
909 break;
910 case STRETCH:
911 {
912 Geom::Scale s(1, 1);
913 if (uniform)
914 s[Geom::X] = s[Geom::Y] = transformation[dim];
915 else {
916 s[dim] = transformation[dim];
917 s[1 - dim] = 1;
918 }
919 transformed = ((p - origin) * s) + origin;
920 break;
921 }
922 case SKEW:
923 // Apply the skew factor
924 transformed[dim] = p[dim] + transformation[0] * (p[1 - dim] - origin[1 - dim]);
925 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
926 // Apply that scale factor here
927 transformed[1-dim] = (p - origin)[1 - dim] * transformation[1] + origin[1 - dim];
928 break;
929 default:
930 g_assert_not_reached();
931 }
933 return transformed;
934 }
936 void SnapManager::_displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Geom::Point const &p) const {
938 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
939 if (prefs->getBool("/options/snapclosestonly/value")) {
940 bool p_is_a_node = point_type & Inkscape::SnapPreferences::SNAPPOINT_NODE;
941 bool p_is_a_bbox = point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
942 if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
943 _desktop->snapindicator->set_new_snapsource(p);
944 } else {
945 _desktop->snapindicator->remove_snapsource();
946 }
947 }
948 }
950 /*
951 Local Variables:
952 mode:c++
953 c-file-style:"stroustrup"
954 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
955 indent-tabs-mode:nil
956 fill-column:99
957 End:
958 */
959 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :