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