Code

Remove some SP_ACTIVE_DESKTOP() calls
[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 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)
18 {
19     _distance = snapped_distance;
20     _tolerance = std::max(snapped_tolerance, 1.0);
21     _always_snap = always_snap;
22     _curve = curve;
23     _second_distance = NR_HUGE;
24     _second_tolerance = 1;
25     _second_always_snap = false;
26     _point = snapped_point;
27     _at_intersection = false;
28     _fully_constrained = fully_constrained;
29 }
31 Inkscape::SnappedCurve::SnappedCurve()
32 {
33     _distance = NR_HUGE;
34     _tolerance = 1;
35     _always_snap = false;
36     _curve = NULL;
37     _second_distance = NR_HUGE;
38     _second_tolerance = 1;
39     _second_always_snap = false;
40     _point = Geom::Point(0,0);
41     _at_intersection = false;
42     _fully_constrained = false;
43 }
45 Inkscape::SnappedCurve::~SnappedCurve()
46 {
47 }
49 Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p, Geom::Matrix dt2doc) const
50 {
51     // Calculate the intersections of two curves, which are both within snapping range, and
52     // return only the closest intersection
53     // The point of intersection should be considered for snapping, but might be outside the snapping range
54     // PS: We need p (the location of the mouse pointer) for find out which intersection is the
55     // closest, as there might be multiple intersections of two curves
56     Geom::Crossings cs = crossings(*(this->_curve), *(curve._curve));
58     if (cs.size() > 0) {
59         // There might be multiple intersections: find the closest
60         Geom::Coord best_dist = NR_HUGE;
61         Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);
62         for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
63             Geom::Point p_ix = this->_curve->pointAt((*i).ta);
64             Geom::Coord dist = Geom::distance(p_ix, p);
65             if (dist < best_dist) {
66                 best_dist = dist;
67                 best_p = p_ix;
68             }
69         }
71         // Now we've found the closest intersection, return it as a SnappedPoint
72         bool const use_this_as_primary = _distance < curve.getSnapDistance();
73         Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;
74         Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;
76         // The intersection should in fact be returned in desktop coordinates
77         best_p = best_p * dt2doc;
79         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
80         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
81         // TODO: Investigate whether it is possible to use document coordinates everywhere
82         // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
83         // All paths are already in document coords and we are certainly not going to change THAT.
84         return SnappedPoint(best_p, Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, true,
85                 secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
86     }
88     // No intersection
89     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
90 }
92 // search for the closest snapped line
93 bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
94 {
95     bool success = false;
97     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
98         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
99             result = *i;
100             success = true;
101         }
102     }
104     return success;
107 // search for the closest intersection of two snapped curves, which are both member of the same collection
108 bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result, Geom::Matrix dt2doc)
110     bool success = false;
112     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
113         std::list<Inkscape::SnappedCurve>::const_iterator j = i;
114         j++;
115         for (; j != list.end(); j++) {
116             Inkscape::SnappedPoint sp = (*i).intersect(*j, p, dt2doc);
117             if (sp.getAtIntersection()) {
118                 // if it's the first point
119                 bool const c1 = !success;
120                 // or, if it's closer
121                 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
122                 // or, if it's just as close then look at the other distance
123                 // (only relevant for snapped points which are at an intersection)
124                 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
125                 // then prefer this point over the previous one
126                 if (c1 || c2 || c3) {
127                     result = sp;
128                     success = true;
129                 }
130             }
131         }
132     }
134     return success;
136 /*
137   Local Variables:
138   mode:c++
139   c-file-style:"stroustrup"
140   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
141   indent-tabs-mode:nil
142   fill-column:99
143   End:
144 */
145 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :