~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/snapped-point.cpp

  • Committer: Diederik van Lierop
  • Date: 2009-12-24 20:10:43 UTC
  • Revision ID: diederik_van_lierop_mail_at-sign_diedenrezi_dot_nl-20091224201043-txzqbq2uvs8334ei
refactoring the snapping code (laying the groundwork for my next commit which reduces snap jitter)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#include "preferences.h"
15
15
 
16
16
// overloaded constructor
17
 
Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &fully_constrained)
18
 
    : _point(p), _source(source), _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a)
 
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
19
{
20
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
21
    _at_intersection = false;
27
27
    _pointer_distance = NR_HUGE;
28
28
}
29
29
 
30
 
Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, 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), _target(target), _at_intersection(at_intersection), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
 
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
32
    _second_distance(d2), _second_tolerance(std::max(t2,1.0)), _second_always_snap(a2)
33
33
{
34
34
    // tolerance should never be smaller than 1 px, as it is used for normalization in
41
41
{
42
42
    _point = Geom::Point(0,0);
43
43
    _source = SNAPSOURCE_UNDEFINED,
 
44
    _source_num = 0,
44
45
    _target = SNAPTARGET_UNDEFINED,
45
46
    _at_intersection = false;
46
47
    _fully_constrained = false;
73
74
    bool success = false;
74
75
 
75
76
    for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); i++) {
76
 
        if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
 
77
        if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
77
78
            result = *i;
78
79
            success = true;
79
80
        }
93
94
    // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
94
95
    if (weighted) {
95
96
 
96
 
        Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
97
 
        Geom::Coord const dist_pointer_this = getPointerDistance();
98
 
        // Weight factor: controls which node should be preferred for snapping, which is either
 
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
99
100
        // the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
100
101
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
101
102
        double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
103
104
            w = 1;
104
105
        }
105
106
        if (w > 0) {
106
 
                if (!(w == 1 && dist_pointer_this == dist_pointer_other)) {
107
 
                        // When accounting for the distance to the mouse pointer, then at least one of the snapped points should
108
 
                                // have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
109
 
                                // mouse pointer distance must be set.
110
 
                                g_assert(dist_pointer_this != NR_HUGE || dist_pointer_other != NR_HUGE);
111
 
                                // The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
112
 
                                // however be very large. To compare these in a fair way, we will have to normalize these metrics first
113
 
                                // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
114
 
                                // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
115
 
                                double const norm_p = std::min(dist_pointer_this, dist_pointer_other);
116
 
                                double const norm_t_other = std::min(50.0, other_one.getTolerance());
117
 
                                double const norm_t_this = std::min(50.0, getTolerance());
118
 
                                dist_other = w * dist_pointer_other / norm_p + (1-w) * dist_other / norm_t_other;
119
 
                                dist_this = w * dist_pointer_this / norm_p + (1-w) * dist_this / norm_t_this;
120
 
                }
 
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
            }
121
122
        }
122
123
    }
123
124