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, long source_num, 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 _source_num = source_num;
29 _target = target;
30 }
32 Inkscape::SnappedCurve::SnappedCurve()
33 {
34 _distance = NR_HUGE;
35 _tolerance = 1;
36 _always_snap = false;
37 _curve = NULL;
38 _second_distance = NR_HUGE;
39 _second_tolerance = 1;
40 _second_always_snap = false;
41 _point = Geom::Point(0,0);
42 _at_intersection = false;
43 _fully_constrained = false;
44 _source = SNAPSOURCE_UNDEFINED;
45 _source_num = 0;
46 _target = SNAPTARGET_UNDEFINED;
47 }
49 Inkscape::SnappedCurve::~SnappedCurve()
50 {
51 }
53 Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p, Geom::Matrix dt2doc) 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;
80 // The intersection should in fact be returned in desktop coordinates
81 best_p = best_p * dt2doc;
83 Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
84 Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
85 // TODO: Investigate whether it is possible to use document coordinates everywhere
86 // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
87 // All paths are already in document coords and we are certainly not going to change THAT.
88 return SnappedPoint(best_p, Inkscape::SNAPSOURCE_UNDEFINED, primaryC->getSourceNum(), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, true,
89 secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
90 }
92 // No intersection
93 return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
94 }
96 // search for the closest snapped line
97 bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
98 {
99 bool success = false;
101 for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
102 if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
103 result = *i;
104 success = true;
105 }
106 }
108 return success;
109 }
111 // search for the closest intersection of two snapped curves, which are both member of the same collection
112 bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result, Geom::Matrix dt2doc)
113 {
114 bool success = false;
116 for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
117 std::list<Inkscape::SnappedCurve>::const_iterator j = i;
118 j++;
119 for (; j != list.end(); j++) {
120 Inkscape::SnappedPoint sp = (*i).intersect(*j, p, dt2doc);
121 if (sp.getAtIntersection()) {
122 // if it's the first point
123 bool const c1 = !success;
124 // or, if it's closer
125 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
126 // or, if it's just as close then look at the other distance
127 // (only relevant for snapped points which are at an intersection)
128 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
129 // then prefer this point over the previous one
130 if (c1 || c2 || c3) {
131 result = sp;
132 success = true;
133 }
134 }
135 }
136 }
138 return success;
139 }
140 /*
141 Local Variables:
142 mode:c++
143 c-file-style:"stroustrup"
144 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
145 indent-tabs-mode:nil
146 fill-column:99
147 End:
148 */
149 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :