snapped-line.cpp revision 71c8857afc501c5e737bce6022fd9ac810d94c05
163N/A/**
163N/A * \file src/snapped-line.cpp
163N/A * \brief SnappedLine class.
163N/A *
163N/A * Authors:
163N/A * Diederik van Lierop <mail@diedenrezi.nl>
163N/A *
163N/A * Released under GNU GPL, read the file 'COPYING' for more information.
163N/A */
163N/A
163N/A#include "snapped-line.h"
163N/A#include <2geom/geom.h>
163N/A#include "libnr/nr-values.h"
163N/A
163N/AInkscape::SnappedLineSegment::SnappedLineSegment(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Point const &start_point_of_line, Geom::Point const &end_point_of_line)
163N/A : _start_point_of_line(start_point_of_line), _end_point_of_line(end_point_of_line)
163N/A{
163N/A _point = snapped_point;
163N/A _distance = snapped_distance;
163N/A _tolerance = std::max(snapped_tolerance, 1.0);
163N/A _always_snap = always_snap;
163N/A _at_intersection = false;
5452N/A _second_distance = NR_HUGE;
163N/A _second_tolerance = 1;
5680N/A _second_always_snap = false;
5680N/A}
163N/A
163N/AInkscape::SnappedLineSegment::SnappedLineSegment()
5680N/A{
163N/A _start_point_of_line = Geom::Point(0,0);
211N/A _end_point_of_line = Geom::Point(0,0);
5965N/A _point = Geom::Point(0,0);
636N/A _distance = NR_HUGE;
1689N/A _tolerance = 1;
844N/A _always_snap = false;
5965N/A _at_intersection = false;
4537N/A _second_distance = NR_HUGE;
163N/A _second_tolerance = 1;
5965N/A _second_always_snap = false;
2899N/A}
7243N/A
7243N/A
7243N/AInkscape::SnappedLineSegment::~SnappedLineSegment()
7243N/A{
5758N/A}
5680N/A
163N/AInkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegment const &line) const
7310N/A{
7310N/A Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
7310N/A Geom::IntersectorKind result = segment_intersect(_start_point_of_line, _end_point_of_line,
7310N/A line._start_point_of_line, line._end_point_of_line,
5680N/A intersection_2geom);
163N/A Geom::Point intersection(intersection_2geom);
5999N/A
5999N/A if (result == Geom::intersects) {
5999N/A /* If a snapper has been told to "always snap", then this one should be preferred
5999N/A * over the other, if that other one has not been told so. (The preferred snapper
163N/A * will be labelled "primary" below)
2283N/A */
2283N/A bool const c1 = this->getAlwaysSnap() && !line.getAlwaysSnap(); //do not use _tolerance directly!
163N/A /* If neither or both have been told to "always snap", then cast a vote based on
5680N/A * the snapped distance. For this we should consider the distance to the snapped
163N/A * line, not the distance to the intersection.
163N/A * See the comment in Inkscape::SnappedLine::intersect
5680N/A */
5680N/A bool const c2 = _distance < line.getSnapDistance();
163N/A bool const use_this_as_primary = c1 || c2;
163N/A Inkscape::SnappedLineSegment const *primarySLS = use_this_as_primary ? this : &line;
2283N/A Inkscape::SnappedLineSegment const *secondarySLS = use_this_as_primary ? &line : this;
163N/A Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - this->getPoint()) : Geom::L2(intersection_2geom - line.getPoint());
4870N/A Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - line.getPoint()) : Geom::L2(intersection_2geom - this->getPoint());
163N/A return SnappedPoint(intersection, SNAPTARGET_PATH_INTERSECTION, primaryDist, primarySLS->getTolerance(), primarySLS->getAlwaysSnap(), true, true,
163N/A secondaryDist, secondarySLS->getTolerance(), secondarySLS->getAlwaysSnap());
163N/A }
163N/A
4870N/A // No intersection
4537N/A return SnappedPoint(intersection, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
163N/A};
163N/A
163N/A
163N/A
163N/AInkscape::SnappedLine::SnappedLine(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Point const &normal_to_line, Geom::Point const &point_on_line)
163N/A : _normal_to_line(normal_to_line), _point_on_line(point_on_line)
163N/A{
4537N/A _distance = snapped_distance;
4537N/A _tolerance = std::max(snapped_tolerance, 1.0);
163N/A _always_snap = always_snap;
163N/A _second_distance = NR_HUGE;
163N/A _second_tolerance = 1;
211N/A _second_always_snap = false;
163N/A _point = snapped_point;
163N/A _at_intersection = false;
163N/A}
7310N/A
7310N/AInkscape::SnappedLine::SnappedLine()
7310N/A{
7310N/A _normal_to_line = Geom::Point(0,0);
7310N/A _point_on_line = Geom::Point(0,0);
7310N/A _distance = NR_HUGE;
163N/A _tolerance = 1;
5120N/A _always_snap = false;
5795N/A _second_distance = NR_HUGE;
163N/A _second_tolerance = 1;
4560N/A _second_always_snap = false;
4560N/A _point = Geom::Point(0,0);
4560N/A _at_intersection = false;
4560N/A}
4560N/A
4560N/AInkscape::SnappedLine::~SnappedLine()
4560N/A{
5795N/A}
5795N/A
5795N/AInkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) const
1689N/A{
1689N/A // Calculate the intersection of two lines, which are both within snapping range
1689N/A // One could be a grid line, whereas the other could be a guide line
1689N/A // The point of intersection should be considered for snapping, but might be outside the snapping range
1689N/A
1689N/A Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
1689N/A Geom::IntersectorKind result = Geom::line_intersection(getNormal(), getConstTerm(),
1689N/A line.getNormal(), line.getConstTerm(), intersection_2geom);
4537N/A Geom::Point intersection(intersection_2geom);
4537N/A
4537N/A if (result == Geom::intersects) {
4537N/A /* If a snapper has been told to "always snap", then this one should be preferred
1689N/A * over the other, if that other one has not been told so. (The preferred snapper
1689N/A * will be labelled "primary" below)
1689N/A */
1689N/A bool const c1 = this->getAlwaysSnap() && !line.getAlwaysSnap();
1689N/A /* If neither or both have been told to "always snap", then cast a vote based on
1689N/A * the snapped distance. For this we should consider the distance to the snapped
1689N/A * line or to the intersection
1689N/A */
1689N/A bool const c2 = _distance < line.getSnapDistance();
1689N/A bool const use_this_as_primary = c1 || c2;
1689N/A Inkscape::SnappedLine const *primarySL = use_this_as_primary ? this : &line;
1689N/A Inkscape::SnappedLine const *secondarySL = use_this_as_primary ? &line : this;
1689N/A Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - this->getPoint()) : Geom::L2(intersection_2geom - line.getPoint());
1689N/A Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - line.getPoint()) : Geom::L2(intersection_2geom - this->getPoint());
5807N/A return SnappedPoint(intersection, Inkscape::SNAPTARGET_UNDEFINED, primaryDist, primarySL->getTolerance(), primarySL->getAlwaysSnap(), true, true,
5807N/A secondaryDist, secondarySL->getTolerance(), secondarySL->getAlwaysSnap());
5807N/A // The type of the snap target is yet undefined, as we cannot tell whether
5758N/A // we're snapping to grid or the guide lines; must be set by on a higher level
5758N/A }
5758N/A
5758N/A // No intersection
5758N/A return SnappedPoint(intersection, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
5758N/A}
5758N/A
5758N/A// search for the closest snapped line segment
5758N/Abool getClosestSLS(std::list<Inkscape::SnappedLineSegment> const &list, Inkscape::SnappedLineSegment &result)
5758N/A{
7310N/A bool success = false;
3817N/A
7310N/A for (std::list<Inkscape::SnappedLineSegment>::const_iterator i = list.begin(); i != list.end(); i++) {
5795N/A if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
6066N/A result = *i;
7277N/A success = true;
7277N/A }
7277N/A }
3817N/A
3817N/A return success;
3817N/A}
3817N/A
3817N/A// search for the closest intersection of two snapped line segments, which are both member of the same collection
3817N/Abool getClosestIntersectionSLS(std::list<Inkscape::SnappedLineSegment> const &list, Inkscape::SnappedPoint &result)
7277N/A{
7277N/A bool success = false;
7277N/A
3817N/A for (std::list<Inkscape::SnappedLineSegment>::const_iterator i = list.begin(); i != list.end(); i++) {
5452N/A std::list<Inkscape::SnappedLineSegment>::const_iterator j = i;
3817N/A j++;
6123N/A for (; j != list.end(); j++) {
Inkscape::SnappedPoint sp = (*i).intersect(*j);
if (sp.getAtIntersection()) {
// if it's the first point
bool const c1 = !success;
// or, if it's closer
bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
// or, if it's just then look at the other distance
// (only relevant for snapped points which are at an intersection
bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
// then prefer this point over the previous one
if (c1 || c2 || c3) {
result = sp;
success = true;
}
}
}
}
return success;
}
// search for the closest snapped line
bool getClosestSL(std::list<Inkscape::SnappedLine> const &list, Inkscape::SnappedLine &result)
{
bool success = false;
for (std::list<Inkscape::SnappedLine>::const_iterator i = list.begin(); i != list.end(); i++) {
if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
result = *i;
success = true;
}
}
return success;
}
// search for the closest intersection of two snapped lines, which are both member of the same collection
bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> const &list, Inkscape::SnappedPoint &result)
{
bool success = false;
for (std::list<Inkscape::SnappedLine>::const_iterator i = list.begin(); i != list.end(); i++) {
std::list<Inkscape::SnappedLine>::const_iterator j = i;
j++;
for (; j != list.end(); j++) {
Inkscape::SnappedPoint sp = (*i).intersect(*j);
if (sp.getAtIntersection()) {
// if it's the first point
bool const c1 = !success;
// or, if it's closer
bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
// or, if it's just then look at the other distance
// (only relevant for snapped points which are at an intersection
bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
// then prefer this point over the previous one
if (c1 || c2 || c3) {
result = sp;
success = true;
}
}
}
}
return success;
}
// search for the closest intersection of two snapped lines, which are in two different collections
bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> const &list1, std::list<Inkscape::SnappedLine> const &list2, Inkscape::SnappedPoint &result)
{
bool success = false;
for (std::list<Inkscape::SnappedLine>::const_iterator i = list1.begin(); i != list1.end(); i++) {
for (std::list<Inkscape::SnappedLine>::const_iterator j = list2.begin(); j != list2.end(); j++) {
Inkscape::SnappedPoint sp = (*i).intersect(*j);
if (sp.getAtIntersection()) {
// if it's the first point
bool const c1 = !success;
// or, if it's closer
bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
// or, if it's just then look at the other distance
// (only relevant for snapped points which are at an intersection
bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
// then prefer this point over the previous one
if (c1 || c2 || c3) {
result = sp;
success = true;
}
}
}
}
return success;
}
/*
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 :