5e31fc1dd147418a197198beca914c946de03fd8
1 /**
2 * \file src/snapped-line.cpp
3 * \brief SnappedInfiniteLine class.
4 *
5 * Authors:
6 * Diederik van Lierop <mail@diedenrezi.nl>
7 *
8 * Released under GNU GPL, read the file 'COPYING' for more information.
9 */
11 #include "snapped-line.h"
12 #include "geom.h"
14 Inkscape::SnappedLine::SnappedLine(NR::Point snapped_point, NR::Coord snapped_distance, NR::Point start_point_of_line, NR::Point end_point_of_line)
15 : _start_point_of_line(start_point_of_line), _end_point_of_line(end_point_of_line)
16 {
17 _distance = snapped_distance;
18 _point = snapped_point;
19 _at_intersection = false;
20 }
22 Inkscape::SnappedLine::SnappedLine()
23 {
24 _start_point_of_line = NR::Point(0,0);
25 _end_point_of_line = NR::Point(0,0);
26 _distance = NR_HUGE;
27 _point = NR::Point(0,0);
28 _at_intersection = false;
29 }
32 Inkscape::SnappedLine::~SnappedLine()
33 {
34 }
36 Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) const
37 {
38 //TODO: Diederik, implement the intersection
39 NR::Point const intersection = NR::Point(NR_HUGE, NR_HUGE);
41 //if (result == INTERSECTS) {
42 /* The relevant snapped distance is the distance to the closest snapped line, not the
43 distance to the intersection. For example, when a box is almost aligned with a grid
44 in both horizontal and vertical directions, the distance to the intersection of the
45 grid lines will always be larger then the distance to a grid line. We will be snapping
46 to the closest snapped point however, so if we ever want to snap to the intersection
47 then the distance to it should at least be equal to the other distance, not greater
48 than it, as that would rule the intersection out
49 */
50 NR::Coord distance = std::min(_distance, line.getDistance());
51 //}
52 return SnappedPoint(intersection, distance);
53 };
57 Inkscape::SnappedInfiniteLine::SnappedInfiniteLine(NR::Point snapped_point, NR::Coord snapped_distance, NR::Point normal_to_line, NR::Point point_on_line)
58 : _normal_to_line(normal_to_line), _point_on_line(point_on_line)
59 {
60 _distance = snapped_distance;
61 _point = snapped_point;
62 _at_intersection = false;
63 }
65 Inkscape::SnappedInfiniteLine::SnappedInfiniteLine()
66 {
67 _normal_to_line = NR::Point(0,0);
68 _point_on_line = NR::Point(0,0);
69 _distance = NR_HUGE;
70 _point = NR::Point(0,0);
71 _at_intersection = false;
72 }
74 Inkscape::SnappedInfiniteLine::~SnappedInfiniteLine()
75 {
76 }
78 Inkscape::SnappedPoint Inkscape::SnappedInfiniteLine::intersect(SnappedInfiniteLine const &line) const
79 {
80 // Calculate the intersection of to infinite lines, which are both within snapping range
81 // The point of intersection should be considered for snapping, but might be outside the snapping range
83 NR::Point intersection = NR::Point(NR_HUGE, NR_HUGE);
84 NR::Coord distance = NR_HUGE;
86 IntersectorKind result = intersector_line_intersection(getNormal(), getConstTerm(),
87 line.getNormal(), line.getConstTerm(), intersection);
89 /*std::cout << "n0 = " << getNormal() << std::endl;
90 std::cout << "n1 = " << line.getNormal() << std::endl;
91 std::cout << "c0 = " << getConstTerm() << std::endl;
92 std::cout << "c1 = " << line.getConstTerm() << std::endl;*/
94 if (result == INTERSECTS) {
95 /* The relevant snapped distance is the distance to the closest snapped line, not the
96 distance to the intersection. For example, when a box is almost aligned with a grid
97 in both horizontal and vertical directions, the distance to the intersection of the
98 grid lines will always be larger then the distance to a grid line. We will be snapping
99 to the closest snapped point however, so if we ever want to snap to the intersection
100 then the distance to it should at least be equal to the other distance, not greater
101 than it, as that would rule the intersection out
102 */
103 distance = std::min(_distance, line.getDistance());
104 //std::cout << "Intersected nicely, now getSIL distance = " << distance << std::endl;
105 }
107 //std::cout << "getSIL distance = " << distance << std::endl;
109 return SnappedPoint(intersection, distance, result == INTERSECTS);
110 }
112 // search for the closest snapped infinite line
113 bool getClosestSIL(std::list<Inkscape::SnappedInfiniteLine> &list, Inkscape::SnappedInfiniteLine &result)
114 {
115 bool success = false;
117 for (std::list<Inkscape::SnappedInfiniteLine>::const_iterator i = list.begin(); i != list.end(); i++) {
118 if ((i == list.begin()) || (*i).getDistance() < result.getDistance()) {
119 result = *i;
120 success = true;
121 }
122 }
124 return success;
125 }
127 // search for the closest intersection of two snapped infinite lines, which are both member of the same collection
128 bool getClosestIntersectionSIL(std::list<Inkscape::SnappedInfiniteLine> &list, Inkscape::SnappedPoint &result)
129 {
130 bool success = false;
132 for (std::list<Inkscape::SnappedInfiniteLine>::const_iterator i = list.begin(); i != list.end(); i++) {
133 std::list<Inkscape::SnappedInfiniteLine>::const_iterator j = i;
134 j++;
135 for (; j != list.end(); j++) {
136 Inkscape::SnappedPoint sp = (*i).intersect(*j);
137 if (sp.getAtIntersection()) {
138 if (!success || sp.getDistance() < result.getDistance()) {
139 // !success because the first intersection cannot be compared to a previous one
140 result = sp;
141 success = true;
142 }
143 }
144 }
145 }
147 return success;
148 }
150 // search for the closest intersection of two snapped infinite lines, which are in two different collections
151 bool getClosestIntersectionSIL(std::list<Inkscape::SnappedInfiniteLine> &list1, std::list<Inkscape::SnappedInfiniteLine> &list2, Inkscape::SnappedPoint &result)
152 {
153 bool success = false;
155 for (std::list<Inkscape::SnappedInfiniteLine>::const_iterator i = list1.begin(); i != list1.end(); i++) {
156 for (std::list<Inkscape::SnappedInfiniteLine>::const_iterator j = list2.begin(); j != list2.end(); j++) {
157 Inkscape::SnappedPoint sp = (*i).intersect(*j);
158 if (sp.getAtIntersection()) {
159 if (!success || sp.getDistance() < result.getDistance()) {
160 // !success because the first intersection cannot be compared to a previous one
161 result = sp;
162 success = true;
163 }
164 }
165 }
166 }
168 return success;
169 }
171 /*
172 Local Variables:
173 mode:c++
174 c-file-style:"stroustrup"
175 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
176 indent-tabs-mode:nil
177 fill-column:99
178 End:
179 */
180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :