Code

4176d4a3d40bade1cce8e966deb78259f0c62c47
[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 "snapped-point.h"
14 // overloaded constructor
15 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)
16     : _point(p), _target(target), _distance(d), _tolerance(t), _always_snap(a)
17 {
18     _at_intersection = false;
19     _fully_constrained = fully_constrained;
20     _second_distance = NR_HUGE;
21     _second_tolerance = 0;
22     _second_always_snap = false;
23     _transformation = Geom::Point(1,1);
24 }
26 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)
27     : _point(p), _target(target), _at_intersection(at_intersection), _fully_constrained(fully_constrained), _distance(d), _tolerance(t), _always_snap(a),
28     _second_distance(d2), _second_tolerance(t2), _second_always_snap(a2)
29 {
30     _transformation = Geom::Point(1,1);
31 }
33 Inkscape::SnappedPoint::SnappedPoint()
34 {
35     _point = Geom::Point(0,0);
36     _target = SNAPTARGET_UNDEFINED, 
37     _distance = NR_HUGE;
38     _tolerance = 0;
39     _always_snap = false;
40     _at_intersection = false;
41     _second_distance = NR_HUGE;
42     _second_tolerance = 0;
43     _second_always_snap = false;
44     _transformation = Geom::Point(1,1);
45 }
49 Inkscape::SnappedPoint::~SnappedPoint()
50 {
51 }
53 Geom::Coord Inkscape::SnappedPoint::getDistance() const
54 {
55     return _distance;
56 }
58 Geom::Coord Inkscape::SnappedPoint::getTolerance() const
59 {
60     return _tolerance;
61 }
63 bool Inkscape::SnappedPoint::getAlwaysSnap() const
64 {
65     return _always_snap;
66 }
68 Geom::Coord Inkscape::SnappedPoint::getSecondDistance() const
69 {
70     return _second_distance;
71 }
73 Geom::Coord Inkscape::SnappedPoint::getSecondTolerance() const
74 {
75     return _second_tolerance;
76 }
78 bool Inkscape::SnappedPoint::getSecondAlwaysSnap() const
79 {
80     return _second_always_snap;
81 }
84 void Inkscape::SnappedPoint::getPoint(Geom::Point &p) const
85 {
86     // When we have snapped
87     if (getSnapped()) { 
88         // then return the snapped point by overwriting p
89         p = _point;
90     } //otherwise p will be left untouched; this way the caller doesn't have to check wether we've snapped
91 }
93 // search for the closest snapped point
94 bool getClosestSP(std::list<Inkscape::SnappedPoint> &list, Inkscape::SnappedPoint &result)
95 {
96     bool success = false;
98     for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); i++) {
99         if ((i == list.begin()) || (*i).getDistance() < result.getDistance()) {
100             result = *i;
101             success = true;
102         }
103     }
105     return success;
108 bool Inkscape::SnappedPoint::isOtherOneBetter(Inkscape::SnappedPoint const &other_one) const
110     // If it's closer
111     bool c2 = other_one.getDistance() < getDistance();
112     // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
113     bool c3 = other_one.getAlwaysSnap() && !getAlwaysSnap();
114     // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
115     bool c3n = !other_one.getAlwaysSnap() && getAlwaysSnap();
116     // or, if we have a fully constrained snappoint (e.g. to a node), while the previous one was only partly constrained (e.g. to a line)
117     bool c4 = other_one.getFullyConstrained() && !getFullyConstrained();
118     // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)       
119     bool c4n = !other_one.getFullyConstrained() && getFullyConstrained(); 
120     // or, if it's just as close then consider the second distance
121     // (which is only relevant for points at an intersection)
122     bool c5a = (other_one.getDistance() == getDistance()); 
123     bool c5b = other_one.getSecondDistance() < getSecondDistance();
124     
125     // std::cout << "c2 = " << c2 << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b;
126     return (c2 || c3 || c4 || (c5a && c5b)) && !c3n && (!c4n || c3);       
129 /*
130   Local Variables:
131   mode:c++
132   c-file-style:"stroustrup"
133   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
134   indent-tabs-mode:nil
135   fill-column:99
136   End:
137 */
138 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :