From bbbe9eebaeb94bab65295acc609a7c98544c4f4c Mon Sep 17 00:00:00 2001 From: dvlierop2 Date: Sun, 18 Nov 2007 19:14:14 +0000 Subject: [PATCH] 1) Improving snapping logic 2) When skewing, don't snap to selection itself --- src/line-snapper.cpp | 2 +- src/seltrans.cpp | 12 +++++++++-- src/snap.cpp | 47 ++++++++++++++++++++----------------------- src/snapped-line.cpp | 45 +++++++++++++++++++++++++++++++++-------- src/snapped-point.cpp | 13 +++++++++--- src/snapped-point.h | 21 +++++++++++++++---- 6 files changed, 97 insertions(+), 43 deletions(-) diff --git a/src/line-snapper.cpp b/src/line-snapper.cpp index 3b5bd047c..be35ec2db 100644 --- a/src/line-snapper.cpp +++ b/src/line-snapper.cpp @@ -56,7 +56,7 @@ void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc, const NR::Coord dist = L2(t - p); //Store any line that's within snapping range if (dist < getDistance()) { - _addSnappedLine(sc, t, dist, c.getDirection(), t); + _addSnappedLine(sc, t, dist, c.getDirection(), t); //SnappedLine dummy = SnappedLine(t, dist, c.getDirection(), t); //sc.infinite_lines.push_back(dummy); } diff --git a/src/seltrans.cpp b/src/seltrans.cpp index 3650b18b6..512d2c0ca 100644 --- a/src/seltrans.cpp +++ b/src/seltrans.cpp @@ -1151,18 +1151,26 @@ gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Poi } skew[dim_a] = tan(radians) * s[dim_a]; } else { + /* Get a STL list of the selected items. + ** FIXME: this should probably be done by Inkscape::Selection. + */ + std::list it; + for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) { + it.push_back(reinterpret_cast(i->data)); + } + SnapManager const &m = _desktop->namedview->snap_manager; std::pair bb = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_BBOX, _bbox_points, - std::list(), + it, skew[dim_a], _origin_for_bboxpoints, dim_b); std::pair sn = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE, _snap_points, - std::list(), + it, skew[dim_a], _origin_for_specpoints, dim_b); diff --git a/src/snap.cpp b/src/snap.cpp index 28b0664c7..1ff5d3d05 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -258,7 +258,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t, NR::Point const &p, bool const &first_point, - std::vector &points_to_snap, + std::vector &points_to_snap, Inkscape::Snapper::ConstraintLine const &c, std::list const &it) const { @@ -390,8 +390,8 @@ std::pair SnapManager::_snapTransformed( constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore); NR::Point result; - NR::Coord metric; - NR::Coord second_metric; + NR::Coord metric = NR_HUGE; + NR::Coord second_metric = NR_HUGE; if (snapped.getDistance() < NR_HUGE) { /* We snapped. Find the transformation that describes where the snapped point has @@ -410,7 +410,7 @@ std::pair SnapManager::_snapTransformed( * and not to the intersection itself! */ metric = snapped.getDistance(); //used to be: metric = NR::L2(result); - second_metric = NR::L2(*j - snapped.getPoint()); + second_metric = snapped.getSecondDistance(); break; case SCALE: { @@ -439,8 +439,6 @@ std::pair SnapManager::_snapTransformed( default: g_assert_not_reached(); } - - /* Note it if it's the best so far */ bool const c1 = metric < best_metric; @@ -450,9 +448,6 @@ std::pair SnapManager::_snapTransformed( if (c1 || c2 || c3a && c3b) { best_transformation = result; - //if (c1) {std::cout << "c1 ";} - //if (c2) {std::cout << "c2 ";} - //if (c3a && c3b) {std::cout << "c3 ";} best_metric = metric; best_second_metric = second_metric; best_at_intersection = snapped.getAtIntersection(); @@ -464,7 +459,7 @@ std::pair SnapManager::_snapTransformed( j++; } - // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors + // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors // These rounding errors might be caused by NRRects, see bug #1584301 return std::make_pair(best_transformation, best_metric < 1e6); } @@ -699,21 +694,23 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedCons // now let's see which snapped point gets a thumbs up Inkscape::SnappedPoint bestPoint(p, NR_HUGE); for (std::list >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) { - // first find out if this snapped point is within snapping range - if ((*i).first.getDistance() <= (*i).second) { - // if it's the first point - bool c1 = (i == sp_list.begin()); - // or, if it's closer - bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); - // or, if it's just as close but at an intersection - bool c3 = ((*i).first.getDistance() == bestPoint.getDistance()) && (*i).first.getAtIntersection(); - // then prefer this point over the previous one - if (c1 || c2 || c3) { - bestPoint = (*i).first; - } - } - } - return bestPoint; + // first find out if this snapped point is within snapping range + if ((*i).first.getDistance() <= (*i).second) { + // if it's the first point + bool c1 = (i == sp_list.begin()); + // or, if it's closer + bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); + // or, if it's just as close then consider the second distance + // (which is only relevant for points at an intersection) + bool c3a = ((*i).first.getDistance() == bestPoint.getDistance()); + bool c3b = (*i).first.getSecondDistance() < bestPoint.getSecondDistance(); + // then prefer this point over the previous one + if (c1 || c2 || c3a && c3b) { + bestPoint = (*i).first; + } + } + } + return bestPoint; } /* diff --git a/src/snapped-line.cpp b/src/snapped-line.cpp index 7ae94e4f4..67fec627a 100644 --- a/src/snapped-line.cpp +++ b/src/snapped-line.cpp @@ -18,6 +18,7 @@ Inkscape::SnappedLineSegment::SnappedLineSegment(NR::Point snapped_point, NR::Co _distance = snapped_distance; _point = snapped_point; _at_intersection = false; + _second_distance = NR_HUGE; } Inkscape::SnappedLineSegment::SnappedLineSegment() @@ -27,6 +28,7 @@ Inkscape::SnappedLineSegment::SnappedLineSegment() _distance = NR_HUGE; _point = NR::Point(0,0); _at_intersection = false; + _second_distance = NR_HUGE; } @@ -38,6 +40,7 @@ Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegmen { Geom::Point intersection_2geom(NR_HUGE, NR_HUGE); NR::Coord distance = NR_HUGE; + NR::Coord second_distance = NR_HUGE; Geom::IntersectorKind result = segment_intersect(_start_point_of_line.to_2geom(), _end_point_of_line.to_2geom(), line._start_point_of_line.to_2geom(), line._end_point_of_line.to_2geom(), @@ -49,8 +52,9 @@ Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegmen distance to the intersection. See the comment in Inkscape::SnappedLine::intersect */ distance = std::min(_distance, line.getDistance()); + second_distance = std::max(_distance, line.getDistance()); } - return SnappedPoint(intersection, distance, result == Geom::intersects); + return SnappedPoint(intersection, distance, result == Geom::intersects, second_distance); }; @@ -59,6 +63,7 @@ Inkscape::SnappedLine::SnappedLine(NR::Point snapped_point, NR::Coord snapped_di : _normal_to_line(normal_to_line), _point_on_line(point_on_line) { _distance = snapped_distance; + _second_distance = NR_HUGE; _point = snapped_point; _at_intersection = false; } @@ -68,6 +73,7 @@ Inkscape::SnappedLine::SnappedLine() _normal_to_line = NR::Point(0,0); _point_on_line = NR::Point(0,0); _distance = NR_HUGE; + _second_distance = NR_HUGE; _point = NR::Point(0,0); _at_intersection = false; } @@ -83,6 +89,7 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) Geom::Point intersection_2geom(NR_HUGE, NR_HUGE); NR::Coord distance = NR_HUGE; + NR::Coord second_distance = NR_HUGE; Geom::IntersectorKind result = Geom::line_intersection(getNormal().to_2geom(), getConstTerm(), line.getNormal().to_2geom(), line.getConstTerm(), intersection_2geom); @@ -98,9 +105,10 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) than it, as that would rule the intersection out */ distance = std::min(_distance, line.getDistance()); + second_distance = std::max(_distance, line.getDistance()); } - return SnappedPoint(intersection, distance, result == Geom::intersects); + return SnappedPoint(intersection, distance, result == Geom::intersects, second_distance); } // search for the closest snapped line segment @@ -129,8 +137,15 @@ bool getClosestIntersectionSLS(std::list &list, In for (; j != list.end(); j++) { Inkscape::SnappedPoint sp = (*i).intersect(*j); if (sp.getAtIntersection()) { - if (!success || sp.getDistance() < result.getDistance()) { - // !success because the first intersection cannot be compared to a previous one + // if it's the first point + bool const c1 = !success; + // or, if it's closer + bool const c2 = sp.getDistance() < result.getDistance(); + // or, if it's just then look at the other distance + // (only relevant for snapped points which are at an intersection + bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); + // then prefer this point over the previous one + if (c1 || c2 || c3) { result = sp; success = true; } @@ -167,8 +182,15 @@ bool getClosestIntersectionSL(std::list &list, Inkscape:: for (; j != list.end(); j++) { Inkscape::SnappedPoint sp = (*i).intersect(*j); if (sp.getAtIntersection()) { - if (!success || sp.getDistance() < result.getDistance()) { - // !success because the first intersection cannot be compared to a previous one + // if it's the first point + bool const c1 = !success; + // or, if it's closer + bool const c2 = sp.getDistance() < result.getDistance(); + // or, if it's just then look at the other distance + // (only relevant for snapped points which are at an intersection + bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); + // then prefer this point over the previous one + if (c1 || c2 || c3) { result = sp; success = true; } @@ -188,8 +210,15 @@ bool getClosestIntersectionSL(std::list &list1, std::list for (std::list::const_iterator j = list2.begin(); j != list2.end(); j++) { Inkscape::SnappedPoint sp = (*i).intersect(*j); if (sp.getAtIntersection()) { - if (!success || sp.getDistance() < result.getDistance()) { - // !success because the first intersection cannot be compared to a previous one + // if it's the first point + bool const c1 = !success; + // or, if it's closer + bool const c2 = sp.getDistance() < result.getDistance(); + // or, if it's just then look at the other distance + // (only relevant for snapped points which are at an intersection + bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); + // then prefer this point over the previous one + if (c1 || c2 || c3) { result = sp; success = true; } diff --git a/src/snapped-point.cpp b/src/snapped-point.cpp index ed82c47c0..d97abda1d 100644 --- a/src/snapped-point.cpp +++ b/src/snapped-point.cpp @@ -4,15 +4,15 @@ * * Authors: * Mathieu Dimanche + * Diederik van Lierop * * Released under GNU GPL, read the file 'COPYING' for more information. */ #include "snapped-point.h" -#include -Inkscape::SnappedPoint::SnappedPoint(NR::Point p, NR::Coord d, bool at_intersection) - : _distance(d), _point(p), _at_intersection(at_intersection) +Inkscape::SnappedPoint::SnappedPoint(NR::Point p, NR::Coord d, bool at_intersection, NR::Coord d2) + : _distance(d), _point(p), _at_intersection(at_intersection), _second_distance(d2) { } @@ -21,6 +21,7 @@ Inkscape::SnappedPoint::SnappedPoint() _distance = NR_HUGE; _point = NR::Point(0,0); _at_intersection = false; + _second_distance = NR_HUGE; } @@ -34,6 +35,12 @@ NR::Coord Inkscape::SnappedPoint::getDistance() const return _distance; } +NR::Coord Inkscape::SnappedPoint::getSecondDistance() const +{ + return _second_distance; +} + + NR::Point Inkscape::SnappedPoint::getPoint() const { return _point; diff --git a/src/snapped-point.h b/src/snapped-point.h index 4d8751f5f..e4b08a302 100644 --- a/src/snapped-point.h +++ b/src/snapped-point.h @@ -7,6 +7,7 @@ * * Authors: * Mathieu Dimanche + * Diederik van Lierop * * Released under GNU GPL, read the file 'COPYING' for more information. */ @@ -15,6 +16,7 @@ #include #include "libnr/nr-coord.h" #include "libnr/nr-point.h" +#include namespace Inkscape { @@ -24,17 +26,28 @@ class SnappedPoint { public: SnappedPoint(); - SnappedPoint(::NR::Point p, ::NR::Coord d, bool at_intersection = false); + SnappedPoint(::NR::Point p, ::NR::Coord d, bool at_intersection = false, NR::Coord d2 = NR_HUGE); ~SnappedPoint(); NR::Coord getDistance() const; + NR::Coord getSecondDistance() const; NR::Point getPoint() const; bool getAtIntersection() const {return _at_intersection;} protected: - NR::Coord _distance; - NR::Point _point; - bool _at_intersection; + NR::Point _point; // Location of the snapped point + bool _at_intersection; // If true, the snapped point is at an intersection + + /* Distance from original point to snapped point. If the snapped point is at + an intersection of e.g. two lines, then this is the distance to the closest + line */ + NR::Coord _distance; + + /* If the snapped point is at an intersection of e.g. two lines, then this is + the distance to the fartest line */ + NR::Coord _second_distance; + + }; } -- 2.30.2