Code

Add a constrained snap method that takes multiple constraints. This reduces the code...
[inkscape.git] / src / snap.cpp
index 265b7c19af8d17e135bd7fe991f7c5973a8608f7..bcacb81e2328416447589344e395ee48408b52d5 100644 (file)
@@ -174,7 +174,6 @@ void SnapManager::freeSnapReturnByRef(Geom::Point &p,
                                       Inkscape::SnapSourceType const source_type,
                                       Geom::OptRect const &bbox_to_snap) const
 {
-    //TODO: SnapCandidatePoint and point_type are somewhat redundant; can't we get rid of the point_type parameter?
     Inkscape::SnappedPoint const s = freeSnap(Inkscape::SnapCandidatePoint(p, source_type), bbox_to_snap);
     s.getPoint(p);
 }
@@ -366,12 +365,6 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint
     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);
-    if (constraint.isCircular()) {
-        Geom::Point v_orig = constraint.getDirection(); // vector from the origin to the original (untransformed) point
-        Geom::Point v_proj = pp - constraint.getPoint(); // vector from the origin to the projected point
-        Geom::Coord angle = atan2(Geom::dot(Geom::rot90(v_orig), v_proj), Geom::dot(v_orig, v_proj));
-        no_snap.setTransformation(Geom::Point(angle, angle)); // Store the rotation (in radians), needed in case of snapping while rotating
-    }
 
     if (!someSnapperMightSnap()) {
         // Always return point on constraint
@@ -396,6 +389,68 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint
     return no_snap;
 }
 
+/* See the documentation for constrainedSnap() directly above for more details.
+ * The difference is that multipleConstrainedSnaps() will take a list of constraints instead of a single one,
+ * and will try to snap the SnapCandidatePoint to all of the provided constraints and see which one fits best
+ *  \param p Source point to be snapped
+ *  \param constraints List of directions or lines 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::multipleConstrainedSnaps(Inkscape::SnapCandidatePoint const &p,
+                                                    std::vector<Inkscape::Snapper::SnapConstraint> const &constraints,
+                                                    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);
+    if (constraints.size() == 0) {
+        return no_snap;
+    }
+
+    SnappedConstraints sc;
+    SnapperList const snappers = getSnappers();
+    std::vector<Geom::Point> projections;
+    bool snapping_is_futile = !someSnapperMightSnap();
+
+    // Iterate over the constraints
+    for (std::vector<Inkscape::Snapper::SnapConstraint>::const_iterator c = constraints.begin(); c != constraints.end(); c++) {
+        // Project the mouse pointer onto the constraint; In case we don't snap then we will
+        // return the projection onto the constraint, such that the constraint is always enforced
+        Geom::Point pp = (*c).projection(p.getPoint());
+        projections.push_back(pp);
+        // 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);
+            }
+        }
+    }
+
+    Inkscape::SnappedPoint result = findBestSnap(p, sc, true);
+
+    if (result.getSnapped()) {
+        // only change the snap indicator if we really snapped to something
+        if (_snapindicator) {
+            _desktop->snapindicator->set_new_snaptarget(result);
+        }
+        return result;
+    }
+
+    // So we didn't snap, but we still need to return a point on one of the constraints
+    // Find out which of the constraints yielded the closest projection of point p
+    no_snap.setPoint(projections.front());
+    for (std::vector<Geom::Point>::iterator pp = projections.begin(); pp != projections.end(); pp++) {
+        if (pp != projections.begin()) {
+            if (Geom::L2(*pp - p.getPoint()) < Geom::L2(no_snap.getPoint() - p.getPoint())) {
+                no_snap.setPoint(*pp);
+            }
+        }
+    }
+
+    return no_snap;
+}
+
 /**
  *  \brief Try to snap a point of a guide to another guide or to a node
  *
@@ -414,7 +469,7 @@ void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal,
         return;
     }
 
-    if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
+    if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
         return;
     }
 
@@ -425,7 +480,7 @@ void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal,
 
     // Snap to nodes
     SnappedConstraints sc;
-    if (object.GuidesMightSnap()) {
+    if (object.ThisSnapperMightSnap()) {
         object.guideFreeSnap(sc, p, guide_normal);
     }
 
@@ -436,8 +491,7 @@ void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal,
         (*i)->freeSnap(sc, candidate, Geom::OptRect(), NULL, NULL);
     }
 
-    // Snap to intersections of curves, but not to the curves themselves! (see _snapTranslatingGuideToNodes in object-snapper.cpp)
-    Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, true);
+    Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, false);
 
     s.getPoint(p);
 }
@@ -593,8 +647,18 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
                 // calculate that line here
                 dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, b);
             } else if (transformation_type == ROTATE) {
-                // Geom::L2(b) is the radius of the circular constraint
-                dedicated_constraint = Inkscape::Snapper::SnapConstraint(origin, b, Geom::L2(b));
+                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]);
             } else if (transformation_type == TRANSLATE) {
@@ -639,7 +703,7 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
             /* We snapped.  Find the transformation that describes where the snapped point has
             ** ended up, and also the metric for this transformation.
             */
-            Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
+            Geom::Point const a = snapped_point.getPoint() - origin; // vector to snapped point
             //Geom::Point const b = (*i - origin); // vector to original point
 
             switch (transformation_type) {
@@ -703,14 +767,16 @@ Inkscape::SnappedPoint SnapManager::_snapTransformed(
                     snapped_point.setSecondSnapDistance(NR_HUGE);
                     break;
                 case SKEW:
-                    result[0] = (snapped_point.getPoint()[dim] - ((*i).getPoint())[dim]) / (((*i).getPoint())[1 - dim] - origin[1 - dim]); // skew factor
+                    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);
                     break;
                 case ROTATE:
-                    result = snapped_point.getTransformation();
+                    // 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);
@@ -769,6 +835,8 @@ Inkscape::SnappedPoint SnapManager::freeSnapTranslate(std::vector<Inkscape::Snap
         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
     }
 
+
+
     return _snapTransformed(p, pointer, false, Geom::Point(0,0), TRANSLATE, tr, Geom::Point(0,0), Geom::X, false);
 }
 
@@ -1085,6 +1153,7 @@ void SnapManager::setup(SPDesktop const *desktop,
     _snapindicator = snapindicator;
     _unselected_nodes = unselected_nodes;
     _guide_to_ignore = guide_to_ignore;
+    _rotation_center_source_item = NULL;
 }
 
 /**
@@ -1115,6 +1184,7 @@ void SnapManager::setup(SPDesktop const *desktop,
     _snapindicator = snapindicator;
     _unselected_nodes = unselected_nodes;
     _guide_to_ignore = guide_to_ignore;
+    _rotation_center_source_item = NULL;
 }
 
 /// Setup, taking the list of items to ignore from the desktop's selection.
@@ -1127,6 +1197,7 @@ void SnapManager::setupIgnoreSelection(SPDesktop const *desktop,
     _snapindicator = snapindicator;
     _unselected_nodes = unselected_nodes;
     _guide_to_ignore = guide_to_ignore;
+    _rotation_center_source_item = NULL;
     _items_to_ignore.clear();
 
     Inkscape::Selection *sel = _desktop->selection;
@@ -1212,8 +1283,9 @@ void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) cons
     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;
 
-        if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
+        if (snapprefs.getSnapEnabledGlobally() && (p_is_other || (p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
             _desktop->snapindicator->set_new_snapsource(p);
         } else {
             _desktop->snapindicator->remove_snapsource();