snapped-point.cpp revision f528f43c592144e0ac7a2c4dcf99ab0a82e65335
/**
* \file src/snapped-point.cpp
* \brief SnappedPoint class.
*
* Authors:
* Mathieu Dimanche <mdimanche@free.fr>
* Diederik van Lierop <mail@diedenrezi.nl>
*
* Released under GNU GPL, read the file 'COPYING' for more information.
*/
#include "snapped-point.h"
#include "preferences.h"
// overloaded constructor
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 &constrained_snap, bool const &fully_constrained, Geom::OptRect target_bbox)
: _point(p), _source(source), _source_num(source_num), _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a), _target_bbox(target_bbox)
{
// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
_at_intersection = false;
_second_tolerance = 1;
_second_always_snap = false;
}
Inkscape::SnappedPoint::SnappedPoint(Inkscape::SnapCandidatePoint const &p, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &constrained_snap, bool const &fully_constrained)
{
_source = p.getSourceType();
_source_num = p.getSourceNum();
_at_intersection = false;
_second_tolerance = 1;
_second_always_snap = false;
_target_bbox = p.getTargetBBox();
}
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 &constrained_snap, bool const &fully_constrained, Geom::Coord const &d2, Geom::Coord const &t2, bool const &a2)
: _point(p), _source(source), _source_num(source_num), _target(target), _at_intersection(at_intersection), _constrained_snap(constrained_snap), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
{
// tolerance should never be smaller than 1 px, as it is used for normalization in
// isOtherSnapBetter. We don't want a division by zero.
}
{
_source_num = 0,
_at_intersection = false;
_constrained_snap = false;
_fully_constrained = false;
_tolerance = 1;
_always_snap = false;
_second_tolerance = 1;
_second_always_snap = false;
}
{
_point = p;
_source_num = 0,
_at_intersection = false;
_fully_constrained = false;
_tolerance = 1;
_always_snap = false;
_second_tolerance = 1;
_second_always_snap = false;
}
{
}
{
// When we have snapped
if (getSnapped()) {
// then return the snapped point by overwriting p
p = _point;
} //otherwise p will be left untouched; this way the caller doesn't have to check whether we've snapped
}
// search for the closest snapped point
{
bool success = false;
result = *i;
success = true;
}
}
return success;
}
bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
{
if (!other_one.getSnapped()) {
return false;
}
double dist_this = getSnapDistance();
// The distance to the pointer should only be taken into account when finding the best snapped source node (when
// there's more than one). It is not useful when trying to find the best snapped target point.
// (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
if (weighted) {
// Weight factor: controls which node should be preferred for snapping, which is either
// the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
w = 1;
}
if (w > 0) {
// When accounting for the distance to the mouse pointer, then at least one of the snapped points should
// have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
// mouse pointer distance must be set.
// The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
// however be very large. To compare these in a fair way, we will have to normalize these metrics first
// The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
// The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
// make sure norm_p is never too close to zero (e.g. when snapping the bbox-corner that was grabbed), by incr. with 1
}
}
}
// When snapping to a constraint line only, which is not really a snap but merely a projection
// to the constraint line, then give this snap a very low priority. Basically, any other snap will do
}
if (getTarget() == SNAPTARGET_CONSTRAINT) {
}
// If it's closer
// or, if it's for a snapper with "always snap" turned on, and the previous wasn't
// But in no case fall back from a snapper with "always snap" on to one with "always snap" off
// 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)
bool c3 = (other_one.getFullyConstrained() && !other_one.getConstrainedSnap()) && !getFullyConstrained(); // Do not consider constrained snaps here, because these will always be fully constrained anyway
// But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
// When both are fully constrained AND coincident, then prefer nodes over intersections
bool d = other_one.getFullyConstrained() && getFullyConstrained() && (Geom::L2(other_one.getPoint() - getPoint()) < 1e-9);
// But don't fall back...
// or, if it's just as close then consider the second distance ...
bool c5b = (other_one.getSecondSnapDistance() < getSecondSnapDistance()) && (getSecondSnapDistance() < NR_HUGE);
// ... or prefer free snaps over constrained snaps
bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
/*
std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << " | c5c = " << c5c << std::endl;
std::cout << "Other one provides a better snap: " << other_is_better << std::endl;
*/
return other_is_better;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :