Code

fc6e5e87f7d912599f7fbefefbbf1ba56d6270d2
[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, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &fully_constrained)
18     : _point(p), _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     _fully_constrained = fully_constrained;
22     _second_distance = NR_HUGE;
23     _second_tolerance = 1;
24     _second_always_snap = false;
25     _transformation = Geom::Point(1,1);
26     _pointer_distance = NR_HUGE;
27 }
29 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, 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)
30     : _point(p), _target(target), _at_intersection(at_intersection), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
31     _second_distance(d2), _second_tolerance(std::max(t2,1.0)), _second_always_snap(a2)
32 {
33     // tolerance should never be smaller than 1 px, as it is used for normalization in
34     // isOtherSnapBetter. We don't want a division by zero.
35     _transformation = Geom::Point(1,1);
36     _pointer_distance = NR_HUGE;
37 }
39 Inkscape::SnappedPoint::SnappedPoint()
40 {
41     _point = Geom::Point(0,0);
42     _target = SNAPTARGET_UNDEFINED,
43     _distance = NR_HUGE;
44     _tolerance = 1;
45     _always_snap = false;
46     _at_intersection = false;
47     _second_distance = NR_HUGE;
48     _second_tolerance = 1;
49     _second_always_snap = false;
50     _transformation = Geom::Point(1,1);
51     _pointer_distance = NR_HUGE;
52 }
54 Inkscape::SnappedPoint::~SnappedPoint()
55 {
56 }
58 void Inkscape::SnappedPoint::getPoint(Geom::Point &p) const
59 {
60     // When we have snapped
61     if (getSnapped()) {
62         // then return the snapped point by overwriting p
63         p = _point;
64     } //otherwise p will be left untouched; this way the caller doesn't have to check wether we've snapped
65 }
67 // search for the closest snapped point
68 bool getClosestSP(std::list<Inkscape::SnappedPoint> &list, Inkscape::SnappedPoint &result)
69 {
70     bool success = false;
72     for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); i++) {
73         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
74             result = *i;
75             success = true;
76         }
77     }
79     return success;
80 }
82 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
83 {
85     double dist_other = other_one.getSnapDistance();
86     double dist_this = getSnapDistance();
88     // The distance to the pointer should only be taken into account when finding the best snapped source node (when
89     // there's more than one). It is not useful when trying to find the best snapped target point.
90     // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
91     if (weighted) {
92         // weigth factor: controls which node should be preferrerd for snapping, which is either
93         // the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
94         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
95         double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
96         if (prefs->getBool("/options/snapclosestonly/value", false)) {
97             w = 1;
98         }
99         if (w > 0) {
100             // When accounting for the distance to the mouse pointer, then at least one of the snapped points should
101             // have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
102             // mouse pointer distance must be set.
103             g_assert(getPointerDistance() != NR_HUGE || other_one.getPointerDistance() != NR_HUGE);
104             // The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
105             // however be very large. To compare these in a fair way, we will have to normalize these metrics first
106             // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
107             // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
108             double const norm_p = std::min(getPointerDistance(), other_one.getPointerDistance());
109             double const norm_t_other = std::min(50.0, other_one.getTolerance());
110             double const norm_t_this = std::min(50.0, getTolerance());
111             dist_other = w * other_one.getPointerDistance() / norm_p + (1-w) * dist_other / norm_t_other;
112             dist_this = w * getPointerDistance() / norm_p + (1-w) * dist_this / norm_t_this;
113         }
114     }
116     // If it's closer
117     bool c1 =  dist_other < dist_this;
118     // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
119     bool c2 = other_one.getAlwaysSnap() && !getAlwaysSnap();
120     // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
121     bool c2n = !other_one.getAlwaysSnap() && getAlwaysSnap();
122     // 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)
123     bool c3 = other_one.getFullyConstrained() && !getFullyConstrained();
124     // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
125     bool c3n = !other_one.getFullyConstrained() && getFullyConstrained();
126     // or, if it's just as close then consider the second distance
127     bool c4a = (dist_other == dist_this);
128     bool c4b = other_one.getSecondSnapDistance() < getSecondSnapDistance();
130     // std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4a = " << c4a << " | c4b = " << c4b << std::endl;
131     return (c1 || c2 || c3 || (c4a && c4b)) && !c2n && (!c3n || c2);
134 /*
135   Local Variables:
136   mode:c++
137   c-file-style:"stroustrup"
138   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
139   indent-tabs-mode:nil
140   fill-column:99
141   End:
142 */
143 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :