Code

Merge from trunk
[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 <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, Geom::OptRect target_bbox)
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     _target_bbox = target_bbox;
31 }
33 Inkscape::SnappedCurve::SnappedCurve()
34 {
35     _distance = NR_HUGE;
36     _tolerance = 1;
37     _always_snap = false;
38     _curve = NULL;
39     _second_distance = NR_HUGE;
40     _second_tolerance = 1;
41     _second_always_snap = false;
42     _point = Geom::Point(0,0);
43     _at_intersection = false;
44     _fully_constrained = false;
45     _source = SNAPSOURCE_UNDEFINED;
46     _source_num = 0;
47     _target = SNAPTARGET_UNDEFINED;
48     _target_bbox = Geom::OptRect();
49 }
51 Inkscape::SnappedCurve::~SnappedCurve()
52 {
53 }
55 Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p, Geom::Matrix dt2doc) const
56 {
57     // Calculate the intersections of two curves, which are both within snapping range, and
58     // return only the closest intersection
59     // The point of intersection should be considered for snapping, but might be outside the snapping range
60     // PS: We need p (the location of the mouse pointer) for find out which intersection is the
61     // closest, as there might be multiple intersections of two curves
62     Geom::Crossings cs = crossings(*(this->_curve), *(curve._curve));
64     if (cs.size() > 0) {
65         // There might be multiple intersections: find the closest
66         Geom::Coord best_dist = NR_HUGE;
67         Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);
68         for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
69             Geom::Point p_ix = this->_curve->pointAt((*i).ta);
70             Geom::Coord dist = Geom::distance(p_ix, p);
71             if (dist < best_dist) {
72                 best_dist = dist;
73                 best_p = p_ix;
74             }
75         }
77         // Now we've found the closest intersection, return it as a SnappedPoint
78         bool const use_this_as_primary = _distance < curve.getSnapDistance();
79         Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;
80         Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;
82         // The intersection should in fact be returned in desktop coordinates
83         best_p = best_p * dt2doc;
85         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
86         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
87         // TODO: Investigate whether it is possible to use document coordinates everywhere
88         // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
89         // All paths are already in document coords and we are certainly not going to change THAT.
90         return SnappedPoint(best_p, Inkscape::SNAPSOURCE_UNDEFINED, primaryC->getSourceNum(), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, true,
91                 secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
92     }
94     // No intersection
95     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
96 }
98 // search for the closest snapped line
99 bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
101     bool success = false;
103     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
104         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
105             result = *i;
106             success = true;
107         }
108     }
110     return success;
113 // search for the closest intersection of two snapped curves, which are both member of the same collection
114 bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result, Geom::Matrix dt2doc)
116     bool success = false;
118     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
119         if ((*i).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes,
120             // as this would require two bboxes two be flashed in the snap indicator
121             std::list<Inkscape::SnappedCurve>::const_iterator j = i;
122             j++;
123             for (; j != list.end(); j++) {
124                 if ((*j).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes
125                     Inkscape::SnappedPoint sp = (*i).intersect(*j, p, dt2doc);
126                     if (sp.getAtIntersection()) {
127                         // if it's the first point
128                         bool const c1 = !success;
129                         // or, if it's closer
130                         bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
131                         // or, if it's just as close then look at the other distance
132                         // (only relevant for snapped points which are at an intersection)
133                         bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
134                         // then prefer this point over the previous one
135                         if (c1 || c2 || c3) {
136                             result = sp;
137                             success = true;
138                         }
139                     }
140                 }
141             }
142         }
143     }
145     return success;
147 /*
148   Local Variables:
149   mode:c++
150   c-file-style:"stroustrup"
151   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
152   indent-tabs-mode:nil
153   fill-column:99
154   End:
155 */
156 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :