Code

fa2d561cd2e161c299e8f5e9935a6af39d275124
[inkscape.git] / src / snapped-curve.cpp
1 /**
2  *    \file src/snapped-curve.cpp
3  *    \brief SnappedCurve class.
4  *
5  *    Authors:
6  *      Diederik van Lierop <mail@diedenrezi.nl>
7  *
8  *    Released under GNU GPL, read the file 'COPYING' for more information.
9  */
11 #include "snapped-curve.h"
12 #include "libnr/nr-values.h"
13 #include <2geom/crossing.h>
14 #include <2geom/path-intersection.h>
15 #include <libnr/nr-convert2geom.h>
17 // These two are needed for SP_ACTIVE_DESKTOP; this is a dirty hack
18 #include "desktop.h"
19 #include "inkscape.h"
21 Inkscape::SnappedCurve::SnappedCurve(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, bool const &fully_constrained, Geom::Curve const *curve)
22 {
23     _distance = snapped_distance;
24     _tolerance = std::max(snapped_tolerance, 1.0);
25     _always_snap = always_snap;
26     _curve = curve;
27     _second_distance = NR_HUGE;
28     _second_tolerance = 1;
29     _second_always_snap = false;
30     _point = snapped_point;
31     _at_intersection = false;
32     _fully_constrained = fully_constrained;
33 }
35 Inkscape::SnappedCurve::SnappedCurve()
36 {
37     _distance = NR_HUGE;
38     _tolerance = 1;
39     _always_snap = false;
40     _curve = NULL;
41     _second_distance = NR_HUGE;
42     _second_tolerance = 1;
43     _second_always_snap = false;
44     _point = Geom::Point(0,0);
45     _at_intersection = false;
46     _fully_constrained = false;
47 }
49 Inkscape::SnappedCurve::~SnappedCurve()
50 {
51 }
53 Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p) const
54 {
55     // Calculate the intersections of two curves, which are both within snapping range, and
56     // return only the closest intersection
57     // The point of intersection should be considered for snapping, but might be outside the snapping range
58     // PS: We need p (the location of the mouse pointer) for find out which intersection is the
59     // closest, as there might be multiple intersections of two curves
60     Geom::Crossings cs = crossings(*(this->_curve), *(curve._curve));
62     if (cs.size() > 0) {
63         // There might be multiple intersections: find the closest
64         Geom::Coord best_dist = NR_HUGE;
65         Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);
66         for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
67             Geom::Point p_ix = this->_curve->pointAt((*i).ta);
68             Geom::Coord dist = Geom::distance(p_ix, p);
69             if (dist < best_dist) {
70                 best_dist = dist;
71                 best_p = p_ix;
72             }
73         }
75         // Now we've found the closest intersection, return it as a SnappedPoint
76         bool const use_this_as_primary = _distance < curve.getSnapDistance();
77         Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;
78         Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;
79         // The intersection should in fact be returned in desktop coordinates, but for this
80         // we need a desktop: this is a dirty hack
81         SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
82         best_p = desktop->dt2doc(best_p);
84         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
85         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
86         // TODO: Investigate whether it is possible to use document coordinates everywhere
87         // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
88         // All paths are already in document coords and we are certainly not going to change THAT.
89         return SnappedPoint(from_2geom(best_p), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, true,
90                 secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
91     }
93     // No intersection
94     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
95 }
97 // search for the closest snapped line
98 bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
99 {
100     bool success = false;
102     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
103         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
104             result = *i;
105             success = true;
106         }
107     }
109     return success;
112 // search for the closest intersection of two snapped curves, which are both member of the same collection
113 bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result)
115     bool success = false;
117     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
118         std::list<Inkscape::SnappedCurve>::const_iterator j = i;
119         j++;
120         for (; j != list.end(); j++) {
121             Inkscape::SnappedPoint sp = (*i).intersect(*j, p);
122             if (sp.getAtIntersection()) {
123                 // if it's the first point
124                 bool const c1 = !success;
125                 // or, if it's closer
126                 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
127                 // or, if it's just as close then look at the other distance
128                 // (only relevant for snapped points which are at an intersection)
129                 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
130                 // then prefer this point over the previous one
131                 if (c1 || c2 || c3) {
132                     result = sp;
133                     success = true;
134                 }
135             }
136         }
137     }
139     return success;
141 /*
142   Local Variables:
143   mode:c++
144   c-file-style:"stroustrup"
145   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
146   indent-tabs-mode:nil
147   fill-column:99
148   End:
149 */
150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :