Code

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