Code

Filters. Custom predefined filters update and new ABC filters.
[inkscape.git] / src / snapped-curve.cpp
index 17d417d408314b375f6c6acc90a4b36e9664a259..5deb4c449c214db0f9bd8511e67bc5e4a34bdffa 100644 (file)
-/**\r
- *    \file src/snapped-curve.cpp\r
- *    \brief SnappedCurve class.\r
- *\r
- *    Authors:\r
- *      Diederik van Lierop <mail@diedenrezi.nl>\r
- *\r
- *    Released under GNU GPL, read the file 'COPYING' for more information.\r
- */\r
-\r
-#include "snapped-curve.h"\r
-#include "libnr/nr-values.h"\r
-#include <2geom/crossing.h>\r
-#include <2geom/path-intersection.h>\r
-#include <libnr/nr-convert2geom.h>\r
-\r
-// These two are needed for SP_ACTIVE_DESKTOP; this is a dirty hack\r
-#include "desktop.h"\r
-#include "inkscape.h"\r
-\r
-Inkscape::SnappedCurve::SnappedCurve(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Curve const *curve)\r
-{\r
-       _distance = snapped_distance;\r
-    _tolerance = snapped_tolerance;\r
-    _always_snap = always_snap;\r
-    _curve = curve;\r
-       _second_distance = NR_HUGE;\r
-    _second_tolerance = 0;\r
-    _second_always_snap = false;\r
-       _point = snapped_point;\r
-       _at_intersection = false;\r
-}\r
-\r
-Inkscape::SnappedCurve::SnappedCurve() \r
-{\r
-       _distance = NR_HUGE;\r
-    _tolerance = 0;\r
-    _always_snap = false;\r
-    _curve = NULL;\r
-       _second_distance = NR_HUGE;\r
-    _second_tolerance = 0;\r
-    _second_always_snap = false;\r
-       _point = Geom::Point(0,0);\r
-       _at_intersection = false;\r
-}\r
-\r
-Inkscape::SnappedCurve::~SnappedCurve()\r
-{\r
-}\r
-\r
-Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p) const \r
-{\r
-    // Calculate the intersections of two curves, which are both within snapping range, and\r
-    // return only the closest intersection\r
-    // The point of intersection should be considered for snapping, but might be outside the snapping range\r
-    // PS: We need p (the location of the mouse pointer) for find out which intersection is the\r
-    // closest, as there might be multiple intersections of two curves\r
-    Geom::SimpleCrosser xr;\r
-    Geom::Crossings cs = xr.crossings(*(this->_curve), *(curve._curve));\r
-     \r
-    if (cs.size() > 0) {\r
-        // There might be multiple intersections: find the closest\r
-        Geom::Coord best_dist = NR_HUGE;\r
-        Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);\r
-        for (std::vector<Geom::Crossing>::const_iterator i = cs.begin(); i != cs.end(); i++) {\r
-            Geom::Point p_ix = this->_curve->pointAt((*i).ta);\r
-            Geom::Coord dist = Geom::distance(p_ix, p);\r
-            if (dist < best_dist) {\r
-                best_dist = dist;\r
-                best_p = p_ix;\r
-            }\r
-        }\r
-        \r
-        // Now we've found the closests intersection, return it as a SnappedPoint\r
-        bool const use_this_as_primary = _distance < curve.getDistance();\r
-        Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;\r
-        Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;\r
-        // The intersection should in fact be returned in desktop coordinates, but for this\r
-        // we need a desktop: this is a dirty hack\r
-        SPDesktop const *desktop = SP_ACTIVE_DESKTOP;\r
-        best_p = desktop->dt2doc(best_p);\r
-        // TODO: Investigate whether it is possible to use document coordinates everywhere\r
-        // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.\r
-        // All paths are already in document coords and we are certainly not going to change THAT.\r
-        return SnappedPoint(from_2geom(best_p), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryC->getDistance(), primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, \r
-                                          secondaryC->getDistance(), secondaryC->getTolerance(), secondaryC->getAlwaysSnap());\r
-    }\r
-    \r
-    // No intersection\r
-    return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, NR_HUGE, 0, false);\r
-}\r
-\r
-// search for the closest snapped line\r
-bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result) \r
-{\r
-    bool success = false;\r
-    \r
-    for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {\r
-        if ((i == list.begin()) || (*i).getDistance() < result.getDistance()) {\r
-            result = *i;\r
-            success = true;\r
-        }   \r
-    }\r
-    \r
-    return success; \r
-}\r
-\r
-// search for the closest intersection of two snapped curves, which are both member of the same collection\r
-bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result)\r
-{\r
-    bool success = false;\r
-    \r
-    for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {\r
-        std::list<Inkscape::SnappedCurve>::const_iterator j = i;\r
-        j++;\r
-        for (; j != list.end(); j++) {\r
-            Inkscape::SnappedPoint sp = (*i).intersect(*j, p);\r
-            if (sp.getAtIntersection()) {\r
-                // if it's the first point\r
-                bool const c1 = !success;\r
-                // or, if it's closer             \r
-                bool const c2 = sp.getDistance() < result.getDistance();\r
-                // or, if it's just then look at the other distance \r
-                // (only relevant for snapped points which are at an intersection\r
-                bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); \r
-                // then prefer this point over the previous one\r
-                if (c1 || c2 || c3) {  \r
-                    result = sp;\r
-                    success = true;\r
-                }\r
-            }               \r
-        }\r
-    }\r
-    \r
-    return success; \r
-}\r
-/*\r
-  Local Variables:\r
-  mode:c++\r
-  c-file-style:"stroustrup"\r
-  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
-  indent-tabs-mode:nil\r
-  fill-column:99\r
-  End:\r
-*/\r
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r
+/**
+ *    \file src/snapped-curve.cpp
+ *    \brief SnappedCurve class.
+ *
+ *    Authors:
+ *      Diederik van Lierop <mail@diedenrezi.nl>
+ *
+ *    Released under GNU GPL, read the file 'COPYING' for more information.
+ */
+
+#include "snapped-curve.h"
+#include <2geom/crossing.h>
+#include <2geom/path-intersection.h>
+
+Inkscape::SnappedCurve::SnappedCurve(Geom::Point const &snapped_point, int num_path, int num_segm, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, bool const &fully_constrained, Geom::Curve const *curve, SnapSourceType source, long source_num, SnapTargetType target, Geom::OptRect target_bbox)
+{
+    _num_path = num_path;
+    _num_segm = num_segm;
+    _distance = snapped_distance;
+    _tolerance = std::max(snapped_tolerance, 1.0);
+    _always_snap = always_snap;
+    _curve = curve;
+    _second_distance = NR_HUGE;
+    _second_tolerance = 1;
+    _second_always_snap = false;
+    _point = snapped_point;
+    _at_intersection = false;
+    _fully_constrained = fully_constrained;
+    _source = source;
+    _source_num = source_num;
+    _target = target;
+    _target_bbox = target_bbox;
+}
+
+Inkscape::SnappedCurve::SnappedCurve()
+{
+    _num_path = 0;
+    _num_segm = 0;
+    _distance = NR_HUGE;
+    _tolerance = 1;
+    _always_snap = false;
+    _curve = NULL;
+    _second_distance = NR_HUGE;
+    _second_tolerance = 1;
+    _second_always_snap = false;
+    _point = Geom::Point(0,0);
+    _at_intersection = false;
+    _fully_constrained = false;
+    _source = SNAPSOURCE_UNDEFINED;
+    _source_num = -1;
+    _target = SNAPTARGET_UNDEFINED;
+    _target_bbox = Geom::OptRect();
+}
+
+Inkscape::SnappedCurve::~SnappedCurve()
+{
+}
+
+Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p, Geom::Matrix dt2doc) const
+{
+    // Calculate the intersections of two curves, which are both within snapping range, and
+    // return only the closest intersection
+    // The point of intersection should be considered for snapping, but might be outside the snapping range
+    // PS: We need p (the location of the mouse pointer) for find out which intersection is the
+    // closest, as there might be multiple intersections of two curves
+    Geom::Crossings cs = crossings(*(this->_curve), *(curve._curve));
+
+    if (cs.size() > 0) {
+        // There might be multiple intersections: find the closest
+        Geom::Coord best_dist = NR_HUGE;
+        Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);
+        for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
+            Geom::Point p_ix = this->_curve->pointAt((*i).ta);
+            Geom::Coord dist = Geom::distance(p_ix, p);
+
+            // Test if we have two segments (curves) from the same path..
+            if (this->_num_path == curve._num_path) {
+                // Never try to intersect a segment with itself
+                if (this->_num_segm == curve._num_segm) continue;
+                // Two subsequent segments (curves) in a path will have a common node; this node is not considered to be an intersection
+                if (this->_num_segm == curve._num_segm + 1 && (*i).ta == 0 && (*i).tb == 1) continue;
+                if (this->_num_segm + 1 == curve._num_segm && (*i).ta == 1 && (*i).tb == 0) continue;
+            }
+
+            if (dist < best_dist) {
+                best_dist = dist;
+                best_p = p_ix;
+            }
+        }
+
+        // Now we've found the closest intersection, return it as a SnappedPoint
+        bool const use_this_as_primary = _distance < curve.getSnapDistance();
+        Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;
+        Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;
+
+        // The intersection should in fact be returned in desktop coordinates
+        best_p = best_p * dt2doc;
+
+        Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
+        Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
+        // TODO: Investigate whether it is possible to use document coordinates everywhere
+        // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
+        // All paths are already in document coords and we are certainly not going to change THAT.
+        return SnappedPoint(best_p, Inkscape::SNAPSOURCE_UNDEFINED, primaryC->getSourceNum(), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, false, true,
+                secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
+    }
+
+    // No intersection
+    return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, false, NR_HUGE, 0, false);
+}
+
+// search for the closest snapped line
+bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
+{
+    bool success = false;
+
+    for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
+        if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
+            result = *i;
+            success = true;
+        }
+    }
+
+    return success;
+}
+
+// search for the closest intersection of two snapped curves, which are both member of the same collection
+bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result, Geom::Matrix dt2doc)
+{
+    bool success = false;
+
+    for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
+        if ((*i).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes,
+            // as this would require two bboxes two be flashed in the snap indicator
+            std::list<Inkscape::SnappedCurve>::const_iterator j = i;
+            j++;
+            for (; j != list.end(); j++) {
+                if ((*j).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes
+                    Inkscape::SnappedPoint sp = (*i).intersect(*j, p, dt2doc);
+                    if (sp.getAtIntersection()) {
+                        // if it's the first point
+                        bool const c1 = !success;
+                        // or, if it's closer
+                        bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
+                        // or, if it's just as close then look at the other distance
+                        // (only relevant for snapped points which are at an intersection)
+                        bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
+                        // then prefer this point over the previous one
+                        if (c1 || c2 || c3) {
+                            result = sp;
+                            success = true;
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    return success;
+}
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :