Code

1) Improve the way the distance to the pointer is taken into account when finding...
[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));
61      
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 (std::vector<Geom::Crossing>::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         }
74         
75         // Now we've found the closests 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);
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(from_2geom(best_p), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryC->getSnapDistance(), primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, true,
87                                           secondaryC->getSnapDistance(), secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
88     }
89     
90     // No intersection
91     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), 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;
98     
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     }
105     
106     return success; 
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)
112     bool success = false;
113     
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);
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 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     }
135     
136     return success; 
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 :