Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / snapped-point.cpp
1 /**
2  *  \file src/snapped-point.cpp
3  *  \brief SnappedPoint class.
4  *
5  *  Authors:
6  *    Mathieu Dimanche <mdimanche@free.fr>
7  *    Diederik van Lierop <mail@diedenrezi.nl>
8  *
9  *  Released under GNU GPL, read the file 'COPYING' for more information.
10  */
12 #include <gtk/gtk.h>
13 #include "snapped-point.h"
14 #include "preferences.h"
16 // overloaded constructor
17 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &fully_constrained)
18     : _point(p), _source(source), _source_num(source_num), _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a)
19 {
20     // tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
21     _at_intersection = false;
22     _fully_constrained = fully_constrained;
23     _second_distance = NR_HUGE;
24     _second_tolerance = 1;
25     _second_always_snap = false;
26     _transformation = Geom::Point(1,1);
27     _pointer_distance = NR_HUGE;
28 }
30 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &at_intersection, bool const &fully_constrained, Geom::Coord const &d2, Geom::Coord const &t2, bool const &a2)
31     : _point(p), _source(source), _source_num(source_num), _target(target), _at_intersection(at_intersection), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
32     _second_distance(d2), _second_tolerance(std::max(t2,1.0)), _second_always_snap(a2)
33 {
34     // tolerance should never be smaller than 1 px, as it is used for normalization in
35     // isOtherSnapBetter. We don't want a division by zero.
36     _transformation = Geom::Point(1,1);
37     _pointer_distance = NR_HUGE;
38 }
40 Inkscape::SnappedPoint::SnappedPoint()
41 {
42     _point = Geom::Point(0,0);
43     _source = SNAPSOURCE_UNDEFINED,
44     _source_num = 0,
45     _target = SNAPTARGET_UNDEFINED,
46     _at_intersection = false;
47     _fully_constrained = false;
48     _distance = NR_HUGE;
49     _tolerance = 1;
50     _always_snap = false;
51     _second_distance = NR_HUGE;
52     _second_tolerance = 1;
53     _second_always_snap = false;
54     _transformation = Geom::Point(1,1);
55     _pointer_distance = NR_HUGE;
56 }
58 Inkscape::SnappedPoint::~SnappedPoint()
59 {
60 }
62 void Inkscape::SnappedPoint::getPoint(Geom::Point &p) const
63 {
64     // When we have snapped
65     if (getSnapped()) {
66         // then return the snapped point by overwriting p
67         p = _point;
68     } //otherwise p will be left untouched; this way the caller doesn't have to check wether we've snapped
69 }
71 // search for the closest snapped point
72 bool getClosestSP(std::list<Inkscape::SnappedPoint> &list, Inkscape::SnappedPoint &result)
73 {
74     bool success = false;
76     for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); i++) {
77         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
78             result = *i;
79             success = true;
80         }
81     }
83     return success;
84 }
86 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
87 {
89     double dist_other = other_one.getSnapDistance();
90     double dist_this = getSnapDistance();
92     // The distance to the pointer should only be taken into account when finding the best snapped source node (when
93     // there's more than one). It is not useful when trying to find the best snapped target point.
94     // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
95     if (weighted) {
97         Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
98         Geom::Coord const dist_pointer_this = getPointerDistance();
99         // Weight factor: controls which node should be preferred for snapping, which is either
100         // the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
101         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
102         double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
103         if (prefs->getBool("/options/snapclosestonly/value", false)) {
104             w = 1;
105         }
106         if (w > 0) {
107             if (!(w == 1 && dist_pointer_this == dist_pointer_other)) {
108                 // When accounting for the distance to the mouse pointer, then at least one of the snapped points should
109                 // have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
110                 // mouse pointer distance must be set.
111                 g_assert(dist_pointer_this != NR_HUGE || dist_pointer_other != NR_HUGE);
112                 // The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
113                 // however be very large. To compare these in a fair way, we will have to normalize these metrics first
114                 // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
115                 // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
116                 double const norm_p = std::min(dist_pointer_this, dist_pointer_other);
117                 double const norm_t_other = std::min(50.0, other_one.getTolerance());
118                 double const norm_t_this = std::min(50.0, getTolerance());
119                 dist_other = w * dist_pointer_other / norm_p + (1-w) * dist_other / norm_t_other;
120                 dist_this = w * dist_pointer_this / norm_p + (1-w) * dist_this / norm_t_this;
121             }
122         }
123     }
125     // If it's closer
126     bool c1 = dist_other < dist_this;
127     // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
128     bool c2 = other_one.getAlwaysSnap() && !getAlwaysSnap();
129     // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
130     bool c2n = !other_one.getAlwaysSnap() && getAlwaysSnap();
131     // or, if we have a fully constrained snappoint (e.g. to a node or an intersection), while the previous one was only partly constrained (e.g. to a line)
132     bool c3 = other_one.getFullyConstrained() && !getFullyConstrained();
133     // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
134     bool c3n = !other_one.getFullyConstrained() && getFullyConstrained();
136     // When both are fully constrained AND coincident, then prefer nodes over intersections
137     bool d = other_one.getFullyConstrained() && getFullyConstrained() && (Geom::L2(other_one.getPoint() - getPoint()) < 1e-9);
138     bool c4 = d && !other_one.getAtIntersection() && getAtIntersection();
139     // But don't fall back...
140     bool c4n = d && other_one.getAtIntersection() && !getAtIntersection();
142     // or, if it's just as close then consider the second distance
143     bool c5a = (dist_other == dist_this);
144     bool c5b = other_one.getSecondSnapDistance() < getSecondSnapDistance();
146     // std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
147     // std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << std::endl;
148     return (c1 || c2 || c3 || c4 || (c5a && c5b)) && !c2n && (!c3n || c2) && !c4n;
151 /*
152   Local Variables:
153   mode:c++
154   c-file-style:"stroustrup"
155   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156   indent-tabs-mode:nil
157   fill-column:99
158   End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :