diff options
| author | Andrew Higginson <at.higginson@gmail.com> | 2011-12-27 21:04:47 +0000 |
|---|---|---|
| committer | Andrew <at.higginson@gmail.com> | 2011-12-27 21:04:47 +0000 |
| commit | 80960b623a99aae1402ab651b2974ef544ed3b03 (patch) | |
| tree | ba49d42c2789e9e11f805e2d5263e10f9fedeef8 /src/snap.cpp | |
| parent | try to fix bug (diff) | |
| parent | GDL: Cherry-pick upstream patch 73852 (2011-03-23) - Add missing return value. (diff) | |
| download | inkscape-80960b623a99aae1402ab651b2974ef544ed3b03.tar.gz inkscape-80960b623a99aae1402ab651b2974ef544ed3b03.zip | |
merged with trunk so I can build again...
(bzr r10092.1.36)
Diffstat (limited to 'src/snap.cpp')
| -rw-r--r-- | src/snap.cpp | 637 |
1 files changed, 148 insertions, 489 deletions
diff --git a/src/snap.cpp b/src/snap.cpp index f13b02b46..36d17102d 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -1,8 +1,5 @@ -#define __SP_DESKTOP_SNAP_C__ - -/** - * \file snap.cpp - * \brief SnapManager class. +/* + * SnapManager class. * * Authors: * Lauris Kaplinski <lauris@kaplinski.com> @@ -19,6 +16,7 @@ */ #include <utility> +#include <2geom/transforms.h> #include "sp-namedview.h" #include "snap.h" @@ -38,12 +36,6 @@ #include "util/mathfns.h" using std::vector; -/** - * Construct a SnapManager for a SPNamedView. - * - * \param v `Owning' SPNamedView. - */ - SnapManager::SnapManager(SPNamedView const *v) : guide(this, 0), object(this, 0), @@ -56,20 +48,7 @@ SnapManager::SnapManager(SPNamedView const *v) : { } -/** - * \brief Return a list of snappers - * - * Inkscape snaps to objects, grids, and guides. For each of these snap targets a - * separate class is used, which has been derived from the base Snapper class. The - * getSnappers() method returns a list of pointers to instances of this class. This - * list contains exactly one instance of the guide snapper and of the object snapper - * class, but any number of grid snappers (because each grid has its own snapper - * instance) - * - * \return List of snappers that we use. - */ -SnapManager::SnapperList -SnapManager::getSnappers() const +SnapManager::SnapperList SnapManager::getSnappers() const { SnapManager::SnapperList s; s.push_back(&guide); @@ -81,22 +60,11 @@ SnapManager::getSnappers() const return s; } -/** - * \brief Return a list of gridsnappers - * - * Each grid has its own instance of the snapper class. This way snapping can - * be enabled per grid individually. A list will be returned containing the - * pointers to these instances, but only for grids that are being displayed - * and for which snapping is enabled. - * - * \return List of gridsnappers that we use. - */ -SnapManager::SnapperList -SnapManager::getGridSnappers() const +SnapManager::SnapperList SnapManager::getGridSnappers() const { SnapperList s; - if (_desktop && _desktop->gridsEnabled() && snapprefs.getSnapToGrids()) { + if (_desktop && _desktop->gridsEnabled() && snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_GRID)) { for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) { Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data; s.push_back(grid->snapper); @@ -106,17 +74,6 @@ SnapManager::getGridSnappers() const return s; } -/** - * \brief Return true if any snapping might occur, whether its to grids, guides or objects - * - * Each snapper instance handles its own snapping target, e.g. grids, guides or - * objects. This method iterates through all these snapper instances and returns - * true if any of the snappers might possible snap, considering only the relevant - * snapping preferences. - * - * \return true if one of the snappers will try to snap to something. - */ - bool SnapManager::someSnapperMightSnap() const { if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) { @@ -132,10 +89,6 @@ bool SnapManager::someSnapperMightSnap() const return (i != s.end()); } -/** - * \return true if one of the grids might be snapped to. - */ - bool SnapManager::gridSnapperMightSnap() const { if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) { @@ -151,31 +104,6 @@ bool SnapManager::gridSnapperMightSnap() const return (i != s.end()); } -/** - * \brief Try to snap a point to grids, guides or objects. - * - * Try to snap a point to grids, guides or objects, in two degrees-of-freedom, - * i.e. snap in any direction on the two dimensional canvas to the nearest - * snap target. freeSnapReturnByRef() is equal in snapping behavior to - * freeSnap(), but the former returns the snapped point trough the referenced - * parameter p. This parameter p initially contains the position of the snap - * source and will we overwritten by the target position if snapping has occurred. - * This makes snapping transparent to the calling code. If this is not desired - * because either the calling code must know whether snapping has occurred, or - * because the original position should not be touched, then freeSnap() should be - * called instead. - * - * PS: - * 1) SnapManager::setup() must have been called before calling this method, - * but only once for a set of points - * 2) Only to be used when a single source point is to be snapped; it assumes - * that source_num = 0, which is inefficient when snapping sets our source points - * - * \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred - * \param source_type Detailed description of the source type, will be used by the snap indicator - * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation - */ - void SnapManager::freeSnapReturnByRef(Geom::Point &p, Inkscape::SnapSourceType const source_type, Geom::OptRect const &bbox_to_snap) const @@ -184,39 +112,29 @@ void SnapManager::freeSnapReturnByRef(Geom::Point &p, s.getPointIfSnapped(p); } - -/** - * \brief Try to snap a point to grids, guides or objects. - * - * Try to snap a point to grids, guides or objects, in two degrees-of-freedom, - * i.e. snap in any direction on the two dimensional canvas to the nearest - * snap target. freeSnap() is equal in snapping behavior to - * freeSnapReturnByRef(). Please read the comments of the latter for more details - * - * PS: SnapManager::setup() must have been called before calling this method, - * but only once for a set of points - * - * \param p Source point to be snapped - * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics - */ - +void SnapManager::freeSnapReturnByRef(Geom::Point &p, + Inkscape::SnapSourceType const source_type, + boost::optional<Geom::Point> &starting_point) const +{ + Inkscape::SnappedPoint const s = freeSnap(Inkscape::SnapCandidatePoint(p, source_type, starting_point), Geom::OptRect()); + s.getPointIfSnapped(p); +} Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapCandidatePoint const &p, Geom::OptRect const &bbox_to_snap) const { if (!someSnapperMightSnap()) { - return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false); + return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, Geom::infinity(), 0, false, false, false); } - SnappedConstraints sc; + IntermSnapResults isr; SnapperList const snappers = getSnappers(); for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - (*i)->freeSnap(sc, p, bbox_to_snap, &_items_to_ignore, _unselected_nodes); + (*i)->freeSnap(isr, p, bbox_to_snap, &_items_to_ignore, _unselected_nodes); } - return findBestSnap(p, sc, false); + return findBestSnap(p, isr, false); } void SnapManager::preSnap(Inkscape::SnapCandidatePoint const &p) @@ -236,25 +154,6 @@ void SnapManager::preSnap(Inkscape::SnapCandidatePoint const &p) } } -/** - * \brief Snap to the closest multiple of a grid pitch - * - * When pasting, we would like to snap to the grid. Problem is that we don't know which - * nodes were aligned to the grid at the time of copying, so we don't know which nodes - * to snap. If we'd snap an unaligned node to the grid, previously aligned nodes would - * become unaligned. That's undesirable. Instead we will make sure that the offset - * between the source and its pasted copy is a multiple of the grid pitch. If the source - * was aligned, then the copy will therefore also be aligned. - * - * PS: Whether we really find a multiple also depends on the snapping range! Most users - * will have "always snap" enabled though, in which case a multiple will always be found. - * PS2: When multiple grids are present then the result will become ambiguous. There is no - * way to control to which grid this method will snap. - * - * \param t Vector that represents the offset of the pasted copy with respect to the original - * \return Offset vector after snapping to the closest multiple of a grid pitch - */ - Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point const &origin) { if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) @@ -263,7 +162,7 @@ Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point c if (_desktop && _desktop->gridsEnabled()) { bool success = false; Geom::Point nearest_multiple; - Geom::Coord nearest_distance = NR_HUGE; + Geom::Coord nearest_distance = Geom::infinity(); Inkscape::SnappedPoint bestSnappedPoint(t); // It will snap to the grid for which we find the closest snap. This might be a different @@ -281,19 +180,19 @@ Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point c // only if the origin of the grid is at (0,0). If it's not then compensate for this // in the translation t Geom::Point const t_offset = t + grid->origin; - SnappedConstraints sc; + IntermSnapResults isr; // Only the first three parameters are being used for grid snappers - snapper->freeSnap(sc, Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH),Geom::OptRect(), NULL, NULL); + snapper->freeSnap(isr, Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH),Geom::OptRect(), NULL, NULL); // Find the best snap for this grid, including intersections of the grid-lines bool old_val = _snapindicator; _snapindicator = false; - Inkscape::SnappedPoint s = findBestSnap(Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH), sc, false, false, true); + Inkscape::SnappedPoint s = findBestSnap(Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH), isr, false, true); _snapindicator = old_val; if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) { // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position // doesn't tell us anything about which node to snap success = true; - nearest_multiple = s.getPoint() - to_2geom(grid->origin); + nearest_multiple = s.getPoint() - grid->origin; nearest_distance = s.getSnapDistance(); bestSnappedPoint = s; } @@ -310,36 +209,6 @@ Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point c return t; } -/** - * \brief Try to snap a point along a constraint line to grids, guides or objects. - * - * Try to snap a point to grids, guides or objects, in only one degree-of-freedom, - * i.e. snap in a specific direction on the two dimensional canvas to the nearest - * snap target. - * - * constrainedSnapReturnByRef() is equal in snapping behavior to - * constrainedSnap(), but the former returns the snapped point trough the referenced - * parameter p. This parameter p initially contains the position of the snap - * source and will be overwritten by the target position if snapping has occurred. - * This makes snapping transparent to the calling code. If this is not desired - * because either the calling code must know whether snapping has occurred, or - * because the original position should not be touched, then constrainedSnap() should - * be called instead. If there's nothing to snap to or if snapping has been disabled, - * then this method will still apply the constraint (but without snapping) - * - * PS: - * 1) SnapManager::setup() must have been called before calling this method, - * but only once for a set of points - * 2) Only to be used when a single source point is to be snapped; it assumes - * that source_num = 0, which is inefficient when snapping sets our source points - - * - * \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred - * \param source_type Detailed description of the source type, will be used by the snap indicator - * \param constraint The direction or line along which snapping must occur - * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation - */ - void SnapManager::constrainedSnapReturnByRef(Geom::Point &p, Inkscape::SnapSourceType const source_type, Inkscape::Snapper::SnapConstraint const &constraint, @@ -349,24 +218,6 @@ void SnapManager::constrainedSnapReturnByRef(Geom::Point &p, p = s.getPoint(); // If we didn't snap, then we will return the point projected onto the constraint } -/** - * \brief Try to snap a point along a constraint line to grids, guides or objects. - * - * Try to snap a point to grids, guides or objects, in only one degree-of-freedom, - * i.e. snap in a specific direction on the two dimensional canvas to the nearest - * snap target. constrainedSnap is equal in snapping behavior to - * constrainedSnapReturnByRef(). Please read the comments of the latter for more details. - * - * PS: SnapManager::setup() must have been called before calling this method, - * but only once for a set of points - * PS: If there's nothing to snap to or if snapping has been disabled, then this - * method will still apply the constraint (but without snapping) - * - * \param p Source point to be snapped - * \param constraint The direction or line along which snapping must occur - * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation - */ - Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint const &p, Inkscape::Snapper::SnapConstraint const &constraint, Geom::OptRect const &bbox_to_snap) const @@ -374,7 +225,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint // First project the mouse pointer onto the constraint Geom::Point pp = constraint.projection(p.getPoint()); - Inkscape::SnappedPoint no_snap = Inkscape::SnappedPoint(pp, p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_CONSTRAINT, NR_HUGE, 0, false, true, false); + Inkscape::SnappedPoint no_snap = Inkscape::SnappedPoint(pp, p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_CONSTRAINT, Geom::infinity(), 0, false, true, false); if (!someSnapperMightSnap()) { // Always return point on constraint @@ -402,13 +253,14 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint return no_snap; } - SnappedConstraints sc; + IntermSnapResults isr; SnapperList const snappers = getSnappers(); for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - (*i)->constrainedSnap(sc, p, bbox_to_snap, constraint, &_items_to_ignore, _unselected_nodes); + (*i)->constrainedSnap(isr, p, bbox_to_snap, constraint, &_items_to_ignore, _unselected_nodes); } - result = findBestSnap(p, sc, true); + result = findBestSnap(p, isr, true); + if (result.getSnapped()) { // only change the snap indicator if we really snapped to something @@ -436,12 +288,12 @@ Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandi Geom::OptRect const &bbox_to_snap) const { - Inkscape::SnappedPoint no_snap = Inkscape::SnappedPoint(p.getPoint(), p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_CONSTRAINT, NR_HUGE, 0, false, true, false); + Inkscape::SnappedPoint no_snap = Inkscape::SnappedPoint(p.getPoint(), p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_CONSTRAINT, Geom::infinity(), 0, false, true, false); if (constraints.size() == 0) { return no_snap; } - SnappedConstraints sc; + IntermSnapResults isr; SnapperList const snappers = getSnappers(); std::vector<Geom::Point> projections; bool snapping_is_futile = !someSnapperMightSnap() || dont_snap; @@ -470,11 +322,11 @@ Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandi // Try to snap to the constraint if (!snapping_is_futile) { for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - (*i)->constrainedSnap(sc, p, bbox_to_snap, *c, &_items_to_ignore,_unselected_nodes); + (*i)->constrainedSnap(isr, p, bbox_to_snap, *c, &_items_to_ignore,_unselected_nodes); } } } - result = findBestSnap(p, sc, true); + result = findBestSnap(p, isr, true); } if (result.getSnapped()) { @@ -509,19 +361,6 @@ Inkscape::SnappedPoint SnapManager::multipleConstrainedSnaps(Inkscape::SnapCandi return no_snap; } -/** - * \brief Try to snap a point to something at a specific angle - * - * When drawing a straight line or modifying a gradient, it will snap to specific angle increments - * if CTRL is being pressed. This method will enforce this angular constraint (even if there is nothing - * to snap to) - * - * \param p Source point to be snapped - * \param p_ref Optional original point, relative to which the angle should be calculated. If empty then - * the angle will be calculated relative to the y-axis - * \param snaps Number of angular increments per PI radians; E.g. if snaps = 2 then we will snap every PI/2 = 90 degrees - */ - Inkscape::SnappedPoint SnapManager::constrainedAngularSnap(Inkscape::SnapCandidatePoint const &p, boost::optional<Geom::Point> const &p_ref, Geom::Point const &o, @@ -558,25 +397,9 @@ Inkscape::SnappedPoint SnapManager::constrainedAngularSnap(Inkscape::SnapCandida return sp; } -/** - * \brief Try to snap a point of a guide to another guide or to a node - * - * Try to snap a point of a guide to another guide or to a node in two degrees- - * of-freedom, i.e. snap in any direction on the two dimensional canvas to the - * nearest snap target. This method is used when dragging or rotating a guide - * - * PS: SnapManager::setup() must have been called before calling this method, - * - * \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred - * \param guide_normal Vector normal to the guide line - */ -void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const +void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &/*guide_normal*/, SPGuideDragType drag_type) const { - if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) { - return; - } - - if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) { + if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() || !snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_GUIDE)) { return; } @@ -585,95 +408,37 @@ void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, candidate = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_GUIDE); } - // Snap to nodes - SnappedConstraints sc; - if (object.ThisSnapperMightSnap()) { - object.guideFreeSnap(sc, p, guide_normal); - } - - // Snap to guides & grid lines - SnapperList snappers = getGridSnappers(); - snappers.push_back(&guide); + IntermSnapResults isr; + SnapperList snappers = getSnappers(); for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - (*i)->freeSnap(sc, candidate, Geom::OptRect(), NULL, NULL); + (*i)->freeSnap(isr, candidate, Geom::OptRect(), NULL, NULL); } - Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, false); + Inkscape::SnappedPoint const s = findBestSnap(candidate, isr, false); s.getPointIfSnapped(p); } -/** - * \brief Try to snap a point on a guide to the intersection with another guide or a path - * - * Try to snap a point on a guide to the intersection of that guide with another - * guide or with a path. The snapped point will lie somewhere on the guide-line, - * making this is a constrained snap, i.e. in only one degree-of-freedom. - * This method is used when dragging the origin of the guide along the guide itself. - * - * PS: SnapManager::setup() must have been called before calling this method, - * - * \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred - * \param guide_normal Vector normal to the guide line - */ - void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const { - if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) { - return; - } - - if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) { + if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() || !snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_GUIDE)) { return; } Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, Inkscape::SNAPTARGET_UNDEFINED); - // Snap to nodes or paths - SnappedConstraints sc; + IntermSnapResults isr; Inkscape::Snapper::SnapConstraint cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line)); - if (object.ThisSnapperMightSnap()) { - object.constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL, NULL); - } - // Snap to guides & grid lines - SnapperList snappers = getGridSnappers(); - snappers.push_back(&guide); + SnapperList snappers = getSnappers(); for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - (*i)->constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL, NULL); + (*i)->constrainedSnap(isr, candidate, Geom::OptRect(), cl, NULL, NULL); } - Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false); + Inkscape::SnappedPoint const s = findBestSnap(candidate, isr, false); s.getPointIfSnapped(p); } -/** - * \brief Method for snapping sets of points while they are being transformed - * - * Method for snapping sets of points while they are being transformed, when using - * for example the selector tool. This method is for internal use only, and should - * not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(), - * etc. instead. - * - * This is what is being done in this method: transform each point, find out whether - * a free snap or constrained snap is more appropriate, do the snapping, calculate - * some metrics to quantify the snap "distance", and see if it's better than the - * previous snap. Finally, the best ("nearest") snap from all these points is returned. - * If no snap has occurred and we're asked for a constrained snap then the constraint - * will be applied nevertheless - * - * \param points Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation. - * \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined. - * \param transformation_type Type of transformation to apply to points before trying to snap them. - * \param transformation Description of the transformation; details depend on the type. - * \param origin Origin of the transformation, if applicable. - * \param dim Dimension to which the transformation applies, if applicable. - * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling. - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::_snapTransformed( std::vector<Inkscape::SnapCandidatePoint> const &points, Geom::Point const &pointer, @@ -717,10 +482,10 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( /* The current best transformation */ Geom::Point best_transformation = transformation; - /* The current best metric for the best transformation; lower is better, NR_HUGE + /* The current best metric for the best transformation; lower is better, Geom::infinity() ** means that we haven't snapped anything. */ - Geom::Point best_scale_metric(NR_HUGE, NR_HUGE); + Geom::Point best_scale_metric(Geom::infinity(), Geom::infinity()); Inkscape::SnappedPoint best_snapped_point; g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point g_assert(best_snapped_point.getAtIntersection() == false); @@ -745,6 +510,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( // std::cout << std::endl; bool first_free_snap = true; + for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) { /* Snap it */ @@ -760,19 +526,10 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, b); } else if (transformation_type == ROTATE) { Geom::Coord r = Geom::L2(b); // the radius of the circular constraint - if (r < 1e-9) { // points too close to the rotation center will not move. Don't try to snap these - // as they will always yield a perfect snap result if they're already snapped beforehand (e.g. - // when the transformation center has been snapped to a grid intersection in the selector tool) - continue; // skip this SnapCandidate and continue with the next one - // PS1: Apparently we don't have to do this for skewing, but why? - // PS2: We cannot easily filter these points upstream, e.g. in the grab() method (seltrans.cpp) - // because the rotation center will change when pressing shift, and grab() won't be recalled. - // Filtering could be done in handleRequest() (again in seltrans.cpp), by iterating through - // the snap candidates. But hey, we're iterating here anyway. - } dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, b, r); } else if (transformation_type == STRETCH) { // when non-uniform stretching { - dedicated_constraint = Inkscape::Snapper::SnapConstraint((*i).getPoint(), component_vectors[dim]); + Geom::Point cvec; cvec[dim] = 1.; + dedicated_constraint = Inkscape::Snapper::SnapConstraint((*i).getPoint(), cvec); } else if (transformation_type == TRANSLATE) { // When doing a constrained translation, all points will move in the same direction, i.e. // either horizontally or vertically. The lines along which they move are therefore all @@ -789,7 +546,8 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( // When scaling, a point aligned either horizontally or vertically with the origin can only // move in that specific direction; therefore it should only snap in that direction, otherwise // we will get snapped points with an invalid transformation - dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, component_vectors[c1]); + Geom::Point cvec; cvec[c1] = 1.; + dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, cvec); snapped_point = constrainedSnap(*j, dedicated_constraint, bbox); } else { // If we have a collection of SnapCandidatePoints, with mixed constrained snapping and free snapping @@ -837,7 +595,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( break; case SCALE: { - result = Geom::Point(NR_HUGE, NR_HUGE); + result = Geom::Point(Geom::infinity(), Geom::infinity()); // If this point *i is horizontally or vertically aligned with // the origin of the scaling, then it will scale purely in X or Y // We can therefore only calculate the scaling in this direction @@ -848,7 +606,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction result[index] = a[index] / b[index]; // then calculate it! } - // we might have left result[1-index] = NR_HUGE + // we might have left result[1-index] = Geom::infinity() // if scaling didn't occur in the other direction } } @@ -859,18 +617,21 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( result[0] = result[1]; } } + // Compare the resulting scaling with the desired scaling - Geom::Point scale_metric = Geom::abs(result - transformation); // One or both of its components might be NR_HUGE - if (scale_metric[0] == NR_HUGE || scale_metric[1] == NR_HUGE) { + Geom::Point scale_metric = result - transformation; // One or both of its components might be Geom::infinity() + scale_metric[0] = fabs(scale_metric[0]); + scale_metric[1] = fabs(scale_metric[1]); + if (scale_metric[0] == Geom::infinity() || scale_metric[1] == Geom::infinity()) { snapped_point.setSnapDistance(std::min(scale_metric[0], scale_metric[1])); } else { snapped_point.setSnapDistance(Geom::L2(scale_metric)); } - snapped_point.setSecondSnapDistance(NR_HUGE); + snapped_point.setSecondSnapDistance(Geom::infinity()); break; } case STRETCH: - result = Geom::Point(NR_HUGE, NR_HUGE); + result = Geom::Point(Geom::infinity(), Geom::infinity()); if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point result[dim] = a[dim] / b[dim]; result[1-dim] = uniform ? result[dim] : 1; @@ -882,22 +643,32 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( } // Store the metric for this transformation as a virtual distance snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim])); - snapped_point.setSecondSnapDistance(NR_HUGE); + snapped_point.setSecondSnapDistance(Geom::infinity()); break; case SKEW: result[0] = (snapped_point.getPoint()[dim] - ((*i).getPoint())[dim]) / b[1 - dim]; // skew factor result[1] = transformation[1]; // scale factor // Store the metric for this transformation as a virtual distance snapped_point.setSnapDistance(std::abs(result[0] - transformation[0])); - snapped_point.setSecondSnapDistance(NR_HUGE); + snapped_point.setSecondSnapDistance(Geom::infinity()); break; case ROTATE: // a is vector to snapped point; b is vector to original point; now lets calculate angle between a and b result[0] = atan2(Geom::dot(Geom::rot90(b), a), Geom::dot(b, a)); result[1] = result[1]; // how else should we store an angle in a point ;-) - // Store the metric for this transformation as a virtual distance (we're storing an angle) - snapped_point.setSnapDistance(std::abs(result[0] - transformation[0])); - snapped_point.setSecondSnapDistance(NR_HUGE); + if (Geom::L2(b) < 1e-9) { // points too close to the rotation center will not move. Don't try to snap these + // as they will always yield a perfect snap result if they're already snapped beforehand (e.g. + // when the transformation center has been snapped to a grid intersection in the selector tool) + snapped_point.setSnapDistance(Geom::infinity()); + // PS1: Apparently we don't have to do this for skewing, but why? + // PS2: We cannot easily filter these points upstream, e.g. in the grab() method (seltrans.cpp) + // because the rotation center will change when pressing shift, and grab() won't be recalled. + // Filtering could be done in handleRequest() (again in seltrans.cpp), by iterating through + // the snap candidates. But hey, we're iterating here anyway. + } else { + snapped_point.setSnapDistance(std::abs(result[0] - transformation[0])); + } + snapped_point.setSecondSnapDistance(Geom::infinity()); break; default: g_assert_not_reached(); @@ -916,7 +687,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( // We might still need to apply a constraint though, if we tried a constrained snap. And // in case of a free snap we might have use for the transformed point, so let's return that // point, whether it's constrained or not - if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) { + if (best_snapped_point.isOtherSnapBetter(snapped_point, true) || points.size() == 1) { // .. so we must keep track of the best non-snapped constrained point best_transformation = result; best_snapped_point = snapped_point; @@ -929,10 +700,10 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( Geom::Coord best_metric; if (transformation_type == SCALE) { - // When scaling, don't ever exit with one of scaling components set to NR_HUGE + // When scaling, don't ever exit with one of scaling components uninitialized for (int index = 0; index < 2; index++) { - if (best_transformation[index] == NR_HUGE) { - if (uniform && best_transformation[1-index] < NR_HUGE) { + if (fabs(best_transformation[index]) == Geom::infinity()) { + if (uniform && fabs(best_transformation[1-index]) < Geom::infinity()) { best_transformation[index] = best_transformation[1-index]; } else { best_transformation[index] = transformation[index]; @@ -943,9 +714,9 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( best_metric = best_snapped_point.getSnapDistance(); best_snapped_point.setTransformation(best_transformation); - // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors + // Using " < 1e6" instead of " < Geom::infinity()" for catching some rounding errors // These rounding errors might be caused by NRRects, see bug #1584301 - best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE); + best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : Geom::infinity()); if (_snapindicator) { if (best_snapped_point.getSnapped()) { @@ -954,20 +725,10 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed( _desktop->snapindicator->remove_snaptarget(); } } - return best_snapped_point; } -/** - * \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::freeSnapTranslate(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Geom::Point const &tr) @@ -975,22 +736,12 @@ Inkscape::SnappedPoint SnapManager::freeSnapTranslate(std::vector<Inkscape::Snap Inkscape::SnappedPoint result = _snapTransformed(p, pointer, false, Geom::Point(0,0), TRANSLATE, tr, Geom::Point(0,0), Geom::X, false); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a translation to a set of points and try to snap along a constraint - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param constraint The direction or line along which snapping must occur. - * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred. - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::constrainedSnapTranslate(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Inkscape::Snapper::SnapConstraint const &constraint, @@ -999,23 +750,13 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapTranslate(std::vector<Inkscap Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, constraint, TRANSLATE, tr, Geom::Point(0,0), Geom::X, false); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred - * \param o Origin of the scaling - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Geom::Scale const &s, @@ -1024,23 +765,13 @@ Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCand Inkscape::SnappedPoint result = _snapTransformed(p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred - * \param o Origin of the scaling - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::constrainedSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Geom::Scale const &s, @@ -1050,24 +781,12 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapScale(std::vector<Inkscape::S Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred - * \param o Origin of the stretching - * \param d Dimension in which to apply proposed stretch. - * \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions) - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Geom::Coord const &s, @@ -1078,24 +797,12 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(std::vector<Inkscape: Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param constraint The direction or line along which snapping must occur. - * \param s Proposed skew; the final skew can only be calculated after snapping has occurred - * \param o Origin of the proposed skew - * \param d Dimension in which to apply proposed skew. - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Inkscape::Snapper::SnapConstraint const &constraint, @@ -1117,22 +824,12 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::Sn Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, constraint, SKEW, s, o, d, false); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Apply a rotation to a set of points and snap, without scaling - * - * \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source. - * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed). - * \param angle Proposed rotation (in radians); the final rotation can only be calculated after snapping has occurred - * \param o Origin of the rotation - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics. - */ - Inkscape::SnappedPoint SnapManager::constrainedSnapRotate(std::vector<Inkscape::SnapCandidatePoint> const &p, Geom::Point const &pointer, Geom::Coord const &angle, @@ -1147,40 +844,43 @@ Inkscape::SnappedPoint SnapManager::constrainedSnapRotate(std::vector<Inkscape:: Inkscape::SnappedPoint result = _snapTransformed(p, pointer, true, Geom::Point(0,0), ROTATE, Geom::Point(angle, angle), o, Geom::X, false); if (p.size() == 1) { - _displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); + displaySnapsource(Inkscape::SnapCandidatePoint(result.getPoint(), p.at(0).getSourceType())); } return result; } -/** - * \brief Given a set of possible snap targets, find the best target (which is not necessarily - * also the nearest target), and show the snap indicator if requested - * - * \param p Source point to be snapped - * \param sc A structure holding all snap targets that have been found so far - * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation. - * \param noCurves If true, then do consider snapping to intersections of curves, but not to the curves themselves - * \param allowOffScreen If true, then snapping to points which are off the screen is allowed (needed for example when pasting to the grid) - * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics - */ - Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint const &p, - SnappedConstraints const &sc, + IntermSnapResults const &isr, bool constrained, - bool noCurves, bool allowOffScreen) const { g_assert(_desktop != NULL); /* std::cout << "Type and number of snapped constraints: " << std::endl; - std::cout << " Points : " << sc.points.size() << std::endl; - std::cout << " Lines : " << sc.lines.size() << std::endl; - std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl; - std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl; - std::cout << " Curves : " << sc.curves.size()<< std::endl; + std::cout << " Points : " << isr.points.size() << std::endl; + std::cout << " Grid lines : " << isr.grid_lines.size()<< std::endl; + std::cout << " Guide lines : " << isr.guide_lines.size()<< std::endl; + std::cout << " Curves : " << isr.curves.size()<< std::endl; + */ + + /* + // Display all snap candidates on the canvas + _desktop->snapindicator->remove_debugging_points(); + for (std::list<Inkscape::SnappedPoint>::const_iterator i = isr.points.begin(); i != isr.points.end(); i++) { + _desktop->snapindicator->set_new_debugging_point((*i).getPoint()); + } + for (std::list<Inkscape::SnappedCurve>::const_iterator i = isr.curves.begin(); i != isr.curves.end(); i++) { + _desktop->snapindicator->set_new_debugging_point((*i).getPoint()); + } + for (std::list<Inkscape::SnappedLine>::const_iterator i = isr.grid_lines.begin(); i != isr.grid_lines.end(); i++) { + _desktop->snapindicator->set_new_debugging_point((*i).getPoint()); + } + for (std::list<Inkscape::SnappedLine>::const_iterator i = isr.guide_lines.begin(); i != isr.guide_lines.end(); i++) { + _desktop->snapindicator->set_new_debugging_point((*i).getPoint()); + } */ // Store all snappoints @@ -1188,36 +888,28 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint co // search for the closest snapped point Inkscape::SnappedPoint closestPoint; - if (getClosestSP(sc.points, closestPoint)) { + if (getClosestSP(isr.points, closestPoint)) { sp_list.push_back(closestPoint); } // search for the closest snapped curve - if (!noCurves) { - Inkscape::SnappedCurve closestCurve; - if (getClosestCurve(sc.curves, closestCurve)) { - sp_list.push_back(Inkscape::SnappedPoint(closestCurve)); - } - } - - if (snapprefs.getSnapIntersectionCS()) { - // search for the closest snapped intersection of curves - Inkscape::SnappedPoint closestCurvesIntersection; - if (getClosestIntersectionCS(sc.curves, p.getPoint(), closestCurvesIntersection, _desktop->dt2doc())) { - closestCurvesIntersection.setSource(p.getSourceType()); - sp_list.push_back(closestCurvesIntersection); - } + Inkscape::SnappedCurve closestCurve; + // We might have collected the paths only to snap to their intersection, without the intention to snap to the paths themselves + // Therefore we explicitly check whether the paths should be considered as snap targets themselves + bool exclude_paths = !snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_PATH); + if (getClosestCurve(isr.curves, closestCurve, exclude_paths)) { + sp_list.push_back(Inkscape::SnappedPoint(closestCurve)); } // search for the closest snapped grid line Inkscape::SnappedLine closestGridLine; - if (getClosestSL(sc.grid_lines, closestGridLine)) { + if (getClosestSL(isr.grid_lines, closestGridLine)) { sp_list.push_back(Inkscape::SnappedPoint(closestGridLine)); } // search for the closest snapped guide line Inkscape::SnappedLine closestGuideLine; - if (getClosestSL(sc.guide_lines, closestGuideLine)) { + if (getClosestSL(isr.guide_lines, closestGuideLine)) { sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine)); } @@ -1228,9 +920,27 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint co // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's // no need to look for additional intersections if (!constrained) { + if (snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_PATH_INTERSECTION)) { + // search for the closest snapped intersection of curves + Inkscape::SnappedPoint closestCurvesIntersection; + if (getClosestIntersectionCS(isr.curves, p.getPoint(), closestCurvesIntersection, _desktop->dt2doc())) { + closestCurvesIntersection.setSource(p.getSourceType()); + sp_list.push_back(closestCurvesIntersection); + } + } + + if (snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_PATH_GUIDE_INTERSECTION)) { + // search for the closest snapped intersection of a guide with a curve + Inkscape::SnappedPoint closestCurveGuideIntersection; + if (getClosestIntersectionCL(isr.curves, isr.guide_lines, p.getPoint(), closestCurveGuideIntersection, _desktop->dt2doc())) { + closestCurveGuideIntersection.setSource(p.getSourceType()); + sp_list.push_back(closestCurveGuideIntersection); + } + } + // search for the closest snapped intersection of grid lines Inkscape::SnappedPoint closestGridPoint; - if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) { + if (getClosestIntersectionSL(isr.grid_lines, closestGridPoint)) { closestGridPoint.setSource(p.getSourceType()); closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION); sp_list.push_back(closestGridPoint); @@ -1238,16 +948,16 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint co // search for the closest snapped intersection of guide lines Inkscape::SnappedPoint closestGuidePoint; - if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) { + if (getClosestIntersectionSL(isr.guide_lines, closestGuidePoint)) { closestGuidePoint.setSource(p.getSourceType()); closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION); sp_list.push_back(closestGuidePoint); } // search for the closest snapped intersection of grid with guide lines - if (snapprefs.getSnapIntersectionGG()) { + if (snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION)) { Inkscape::SnappedPoint closestGridGuidePoint; - if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) { + if (getClosestIntersectionSL(isr.grid_lines, isr.guide_lines, closestGridGuidePoint)) { closestGridGuidePoint.setSource(p.getSourceType()); closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION); sp_list.push_back(closestGridGuidePoint); @@ -1286,7 +996,6 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint co return bestSnappedPoint; } -/// Convenience shortcut when there is only one item to ignore void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, SPItem const *item_to_ignore, @@ -1306,22 +1015,6 @@ void SnapManager::setup(SPDesktop const *desktop, _rotation_center_source_items = NULL; } -/** - * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets - * to ignore and toggling the snap indicator - * - * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored - * whereas this one will take a list of items to ignore - * - * \param desktop Reference to the desktop to which this snap manager is attached - * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences) - * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping" - * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and - * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each - * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored - * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to - */ - void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, @@ -1370,18 +1063,6 @@ SPDocument *SnapManager::getDocument() const return _named_view->document; } -/** - * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code - * - * \param p The untransformed position of the point, paired with an identifier of the type of the snap source. - * \param transformation_type Type of transformation to apply. - * \param transformation Mathematical description of the transformation; details depend on the type. - * \param origin Origin of the transformation, if applicable. - * \param dim Dimension to which the transformation applies, if applicable. - * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling. - * \return The position of the point after transformation - */ - Geom::Point SnapManager::_transformPoint(Inkscape::SnapCandidatePoint const &p, Transformation const transformation_type, Geom::Point const &transformation, @@ -1429,49 +1110,27 @@ Geom::Point SnapManager::_transformPoint(Inkscape::SnapCandidatePoint const &p, } /** - * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol + * Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol. * - * \param point_type Category of points to which the source point belongs: node, guide or bounding box - * \param p The transformed position of the source point, paired with an identifier of the type of the snap source. + * @param point_type Category of points to which the source point belongs: node, guide or bounding box + * @param p The transformed position of the source point, paired with an identifier of the type of the snap source. */ - -void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) const { - +void SnapManager::displaySnapsource(Inkscape::SnapCandidatePoint const &p) const { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); if (prefs->getBool("/options/snapclosestonly/value")) { - bool p_is_a_node = p.getSourceType() & Inkscape::SNAPSOURCE_NODE_CATEGORY; - bool p_is_a_bbox = p.getSourceType() & Inkscape::SNAPSOURCE_BBOX_CATEGORY; - bool p_is_other = p.getSourceType() & Inkscape::SNAPSOURCE_OTHER_CATEGORY; + Inkscape::SnapSourceType t = p.getSourceType(); + bool p_is_a_node = t & Inkscape::SNAPSOURCE_NODE_CATEGORY; + bool p_is_a_bbox = t & Inkscape::SNAPSOURCE_BBOX_CATEGORY; + bool p_is_other = (t & Inkscape::SNAPSOURCE_OTHERS_CATEGORY) || (t & Inkscape::SNAPSOURCE_DATUMS_CATEGORY); g_assert(_desktop != NULL); - if (snapprefs.getSnapEnabledGlobally() && (p_is_other || (p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) { + if (snapprefs.getSnapEnabledGlobally() && (p_is_other || (p_is_a_node && snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_NODE_CATEGORY)) || (p_is_a_bbox && snapprefs.isTargetSnappable(Inkscape::SNAPTARGET_BBOX_CATEGORY)))) { _desktop->snapindicator->set_new_snapsource(p); } else { _desktop->snapindicator->remove_snapsource(); } } } - -void SnapManager::keepClosestPointOnly(std::vector<Inkscape::SnapCandidatePoint> &points, const Geom::Point &reference) const -{ - if (points.size() < 2) return; - - Inkscape::SnapCandidatePoint closest_point = Inkscape::SnapCandidatePoint(Geom::Point(NR_HUGE, NR_HUGE), Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED); - Geom::Coord closest_dist = NR_HUGE; - - for(std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) { - Geom::Coord dist = Geom::L2((*i).getPoint() - reference); - if (i == points.begin() || dist < closest_dist) { - closest_point = *i; - closest_dist = dist; - } - } - - closest_point.setSourceNum(-1); - points.clear(); - points.push_back(closest_point); -} - /* Local Variables: mode:c++ |
