From f750db62ff32f0048fdc228874e70a1ad9af79ae Mon Sep 17 00:00:00 2001 From: cth103 Date: Mon, 8 May 2006 15:07:23 +0000 Subject: [PATCH] More snapping cleanups. --- src/seltrans.cpp | 25 ++++++++-- src/snap.cpp | 123 +++++++---------------------------------------- src/snap.h | 28 ++++------- 3 files changed, 49 insertions(+), 127 deletions(-) diff --git a/src/seltrans.cpp b/src/seltrans.cpp index bbaaf2f9d..a05cac181 100644 --- a/src/seltrans.cpp +++ b/src/seltrans.cpp @@ -1022,9 +1022,28 @@ gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Poi } skew[dim_a] = tan(radians) * s[dim_a]; } else { - skew[dim_a] = namedview_dim_snap_list_skew(_desktop->namedview, - Snapper::SNAP_POINT, _snap_points, - _origin, skew[dim_a], dim_b); + SnapManager const &m = _desktop->namedview->snap_manager; + + std::pair bb = m.freeSnapSkew(Inkscape::Snapper::BBOX_POINT, + _bbox_points, + std::list(), + skew[dim_a], + _origin, + dim_a); + + std::pair sn = m.freeSnapSkew(Inkscape::Snapper::SNAP_POINT, + _snap_points, + std::list(), + skew[dim_a], + _origin, + dim_a); + + if (bb.second || sn.second) { + /* We snapped something, so change the skew to reflect it */ + NR::Coord const bd = bb.second ? bb.first : NR_HUGE; + NR::Coord const sd = sn.second ? sn.first : NR_HUGE; + skew[dim_a] = std::min(bd, sd); + } } pt[dim_b] = ( _point[dim_a] - _origin[dim_a] ) * skew[dim_a] + _point[dim_b]; diff --git a/src/snap.cpp b/src/snap.cpp index 4971bada4..3d4a48005 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -161,6 +161,10 @@ std::pair SnapManager::_snapTransformed( transformed = ((*i - origin) * s) + origin; break; } + case SKEW: + transformed = *i; + transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]); + break; default: g_assert_not_reached(); } @@ -200,6 +204,10 @@ std::pair SnapManager::_snapTransformed( metric = std::abs(result[dim] - transformation[dim]); break; } + case SKEW: + result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); + metric = std::abs(result[dim] - transformation[dim]); + break; default: g_assert_not_reached(); } @@ -280,115 +288,20 @@ std::pair SnapManager::freeSnapStretch(Inkscape::Snapper::Point } - -/// Minimal distance to norm before point is considered for snap. -static const double MIN_DIST_NORM = 1.0; - -/** - * Try to snap \a req in one dimension. - * - * \param nv NamedView to use. - * \param req Point to snap; updated to the snapped point if a snap occurred. - * \param dim Dimension to snap in. - * \return Distance to the snap point along the \a dim axis, or \c NR_HUGE - * if no snap occurred. - */ -NR::Coord namedview_dim_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point &req, - NR::Dim2 const dim, SPItem const *it) -{ - return namedview_vector_snap(nv, t, req, component_vectors[dim], it); -} - -NR::Coord namedview_dim_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point &req, - NR::Dim2 const dim, std::list const &it) -{ - return namedview_vector_snap(nv, t, req, component_vectors[dim], it); -} - - -NR::Coord namedview_vector_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, - NR::Point &req, NR::Point const &d, - SPItem const *it) -{ - std::list lit; - lit.push_back(it); - return namedview_vector_snap(nv, t, req, d, lit); -} - -/** - * Look for snap point along the line described by the point \a req - * and the direction vector \a d. - * Modifies req to the snap point, if one is found. - * \return The distance from \a req to the snap point along the vector \a d, - * or \c NR_HUGE if no snap point was found. - * - * \pre d â‰  (0, 0). - */ -NR::Coord namedview_vector_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, - NR::Point &req, NR::Point const &d, - std::list const &it) -{ - g_assert(nv != NULL); - g_assert(SP_IS_NAMEDVIEW(nv)); - - SnapManager::SnapperList const snappers = nv->snap_manager.getSnappers(); - - NR::Coord best = NR_HUGE; - for (SnapManager::SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) { - Inkscape::SnappedPoint const s = (*i)->constrainedSnap(t, req, d, it); - if (s.getDistance() < best) { - req = s.getPoint(); - best = s.getDistance(); - } - } - - return best; -} - - -/* - * functions for lists of points - * - * All functions take a list of NR::Point and parameter indicating the proposed transformation. - * They return the updated transformation parameter. - */ - -/** - * Try to snap points after they have been skewed. - */ -double namedview_dim_snap_list_skew(SPNamedView const *nv, Inkscape::Snapper::PointType t, - const std::vector &p, NR::Point const &norm, - double const sx, NR::Dim2 const dim) +std::pair SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t, + std::vector const &p, + std::list const &it, + NR::Coord const &s, + NR::Point const &o, + NR::Dim2 d) const { - SnapManager const &m = nv->snap_manager; - - if (m.willSnapSomething() == false) { - return sx; - } - - g_assert(dim < 2); - - gdouble dist = NR_HUGE; - gdouble skew = sx; - - for (std::vector::const_iterator i = p.begin(); i != p.end(); i++) { - NR::Point q = *i; - NR::Point check = q; - // apply shear - check[dim] += sx * (q[!dim] - norm[!dim]); - if (fabs (q[!dim] - norm[!dim]) > MIN_DIST_NORM) { - const gdouble d = namedview_dim_snap (nv, t, check, dim, NULL); - if (d < fabs (dist)) { - dist = d; - skew = (check[dim] - q[dim]) / (q[!dim] - norm[!dim]); - } - } - } + std::pair const r = _snapTransformed( + t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false + ); - return skew; + return std::make_pair(r.first[d], r.second); } - /* Local Variables: mode:c++ diff --git a/src/snap.h b/src/snap.h index ad5c68857..12996f04d 100644 --- a/src/snap.h +++ b/src/snap.h @@ -84,6 +84,13 @@ public: NR::Dim2 d, bool uniform) const; + std::pair freeSnapSkew(Inkscape::Snapper::PointType t, + std::vector const &p, + std::list const &it, + NR::Coord const &s, + NR::Point const &o, + NR::Dim2 d) const; + Inkscape::GridSnapper grid; Inkscape::GuideSnapper guide; Inkscape::ObjectSnapper object; @@ -96,7 +103,8 @@ private: enum Transformation { TRANSLATION, SCALE, - STRETCH + STRETCH, + SKEW }; std::pair _snapTransformed(Inkscape::Snapper::PointType type, @@ -111,24 +119,6 @@ private: bool uniform) const; }; - -/* Single point methods */ -NR::Coord namedview_vector_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point &req, - NR::Point const &d, std::list const &it); -NR::Coord namedview_vector_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point &req, - NR::Point const &d, SPItem const *it); -NR::Coord namedview_dim_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point& req, - NR::Dim2 const dim, SPItem const *it); -NR::Coord namedview_dim_snap(SPNamedView const *nv, Inkscape::Snapper::PointType t, NR::Point& req, - NR::Dim2 const dim, std::list const &it); - -/* List of points methods */ - -NR::Coord namedview_dim_snap_list_skew(SPNamedView const *nv, Inkscape::Snapper::PointType t, - const std::vector &p, - NR::Point const &norm, double const sx, NR::Dim2 const dim); - - #endif /* !SEEN_SNAP_H */ /* -- 2.39.5