Code

Fix change in revision 9947 to be consistent with rest of the codebase.
[inkscape.git] / src / snapped-line.cpp
1 /**
2  *    \file src/snapped-line.cpp
3  *    \brief SnappedLine 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 <2geom/line.h>
14 Inkscape::SnappedLineSegment::SnappedLineSegment(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Point const &start_point_of_line, Geom::Point const &end_point_of_line)
15     : _start_point_of_line(start_point_of_line), _end_point_of_line(end_point_of_line)
16 {
17     _point = snapped_point;
18     _source = source;
19     _source_num = source_num;
20     _target = target;
21     _distance = snapped_distance;
22     _tolerance = std::max(snapped_tolerance, 1.0);
23     _always_snap = always_snap;
24     _at_intersection = false;
25     _second_distance = NR_HUGE;
26     _second_tolerance = 1;
27     _second_always_snap = false;
28 }
30 Inkscape::SnappedLineSegment::SnappedLineSegment()
31 {
32     _start_point_of_line = Geom::Point(0,0);
33     _end_point_of_line = Geom::Point(0,0);
34     _point = Geom::Point(0,0);
35     _source = SNAPSOURCE_UNDEFINED;
36     _source_num = -1;
37     _target = SNAPTARGET_UNDEFINED;
38     _distance = NR_HUGE;
39     _tolerance = 1;
40     _always_snap = false;
41     _at_intersection = false;
42     _second_distance = NR_HUGE;
43     _second_tolerance = 1;
44     _second_always_snap = false;
45 }
48 Inkscape::SnappedLineSegment::~SnappedLineSegment()
49 {
50 }
52 Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegment const &line) const
53 {
54     Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
55     try
56     {
57         inters = Geom::intersection(getLineSegment(), line.getLineSegment());
58     }
59     catch (Geom::InfiniteSolutions e)
60     {
61         // We're probably dealing with parallel lines, so they don't really cross
62         inters = Geom::OptCrossing();
63     }
65     if (inters) {
66         Geom::Point inters_pt = getLineSegment().pointAt((*inters).ta);
67         /* If a snapper has been told to "always snap", then this one should be preferred
68          * over the other, if that other one has not been told so. (The preferred snapper
69          * will be labeled "primary" below)
70         */
71         bool const c1 = this->getAlwaysSnap() && !line.getAlwaysSnap(); //do not use _tolerance directly!
72         /* If neither or both have been told to "always snap", then cast a vote based on
73          * the snapped distance. For this we should consider the distance to the snapped
74          * line, not the distance to the intersection.
75          * See the comment in Inkscape::SnappedLine::intersect
76         */
77         bool const c2 = _distance < line.getSnapDistance();
78         bool const use_this_as_primary = c1 || c2;
79         Inkscape::SnappedLineSegment const *primarySLS = use_this_as_primary ? this : &line;
80         Inkscape::SnappedLineSegment const *secondarySLS = use_this_as_primary ? &line : this;
81         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(inters_pt - this->getPoint()) : Geom::L2(inters_pt - line.getPoint());
82         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(inters_pt - line.getPoint()) : Geom::L2(inters_pt - this->getPoint());
83         return SnappedPoint(inters_pt, SNAPSOURCE_UNDEFINED, primarySLS->getSourceNum(), SNAPTARGET_PATH_INTERSECTION, primaryDist, primarySLS->getTolerance(), primarySLS->getAlwaysSnap(), true, false, true,
84                                           secondaryDist, secondarySLS->getTolerance(), secondarySLS->getAlwaysSnap());
85     }
87     // No intersection
88     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, false, NR_HUGE, 0, false);
89 };
93 Inkscape::SnappedLine::SnappedLine(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Point const &normal_to_line, Geom::Point const &point_on_line)
94     : _normal_to_line(normal_to_line), _point_on_line(point_on_line)
95 {
96     _source = source;
97     _source_num = source_num;
98     _target = target;
99     _distance = snapped_distance;
100     _tolerance = std::max(snapped_tolerance, 1.0);
101     _always_snap = always_snap;
102     _second_distance = NR_HUGE;
103     _second_tolerance = 1;
104     _second_always_snap = false;
105     _point = snapped_point;
106     _at_intersection = false;
109 Inkscape::SnappedLine::SnappedLine()
111     _normal_to_line = Geom::Point(0,0);
112     _point_on_line = Geom::Point(0,0);
113     _source = SNAPSOURCE_UNDEFINED;
114     _source_num = -1;
115     _target = SNAPTARGET_UNDEFINED;
116     _distance = NR_HUGE;
117     _tolerance = 1;
118     _always_snap = false;
119     _second_distance = NR_HUGE;
120     _second_tolerance = 1;
121     _second_always_snap = false;
122     _point = Geom::Point(0,0);
123     _at_intersection = false;
126 Inkscape::SnappedLine::~SnappedLine()
130 Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) const
132     // Calculate the intersection of two lines, which are both within snapping range
133     // One could be a grid line, whereas the other could be a guide line
134     // The point of intersection should be considered for snapping, but might be outside the snapping range
136     Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
137     try
138     {
139         inters = Geom::intersection(getLine(), line.getLine());
140     }
141     catch (Geom::InfiniteSolutions e)
142     {
143         // We're probably dealing with parallel lines, so they don't really cross
144         inters = Geom::OptCrossing();
145     }
147     if (inters) {
148         Geom::Point inters_pt = getLine().pointAt((*inters).ta);
149         /* If a snapper has been told to "always snap", then this one should be preferred
150          * over the other, if that other one has not been told so. (The preferred snapper
151          * will be labelled "primary" below)
152         */
153         bool const c1 = this->getAlwaysSnap() && !line.getAlwaysSnap();
154         /* If neither or both have been told to "always snap", then cast a vote based on
155          * the snapped distance. For this we should consider the distance to the snapped
156          * line or to the intersection
157          */
158         bool const c2 = _distance < line.getSnapDistance();
159         bool const use_this_as_primary = c1 || c2;
160         Inkscape::SnappedLine const *primarySL = use_this_as_primary ? this : &line;
161         Inkscape::SnappedLine const *secondarySL = use_this_as_primary ? &line : this;
162         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(inters_pt - this->getPoint()) : Geom::L2(inters_pt - line.getPoint());
163         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(inters_pt - line.getPoint()) : Geom::L2(inters_pt - this->getPoint());
164         return SnappedPoint(inters_pt, Inkscape::SNAPSOURCE_UNDEFINED, primarySL->getSourceNum(), Inkscape::SNAPTARGET_UNDEFINED, primaryDist, primarySL->getTolerance(), primarySL->getAlwaysSnap(), true, false, true,
165                                           secondaryDist, secondarySL->getTolerance(), secondarySL->getAlwaysSnap());
166         // The type of the snap target is yet undefined, as we cannot tell whether
167         // we're snapping to grid or the guide lines; must be set by on a higher level
168     }
170     // No intersection
171     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, false, NR_HUGE, 0, false);
174 // search for the closest snapped line segment
175 bool getClosestSLS(std::list<Inkscape::SnappedLineSegment> const &list, Inkscape::SnappedLineSegment &result)
177     bool success = false;
179     for (std::list<Inkscape::SnappedLineSegment>::const_iterator i = list.begin(); i != list.end(); i++) {
180         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
181             result = *i;
182             success = true;
183         }
184     }
186     return success;
189 // search for the closest intersection of two snapped line segments, which are both member of the same collection
190 bool getClosestIntersectionSLS(std::list<Inkscape::SnappedLineSegment> const &list, Inkscape::SnappedPoint &result)
192     bool success = false;
194     for (std::list<Inkscape::SnappedLineSegment>::const_iterator i = list.begin(); i != list.end(); i++) {
195         std::list<Inkscape::SnappedLineSegment>::const_iterator j = i;
196         j++;
197         for (; j != list.end(); j++) {
198             Inkscape::SnappedPoint sp = (*i).intersect(*j);
199             if (sp.getAtIntersection()) {
200                 // if it's the first point
201                  bool const c1 = !success;
202                 // or, if it's closer
203                 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
204                 // or, if it's just then look at the other distance
205                 // (only relevant for snapped points which are at an intersection
206                 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
207                 // then prefer this point over the previous one
208                 if (c1 || c2 || c3) {
209                     result = sp;
210                     success = true;
211                 }
212             }
213         }
214     }
216     return success;
219 // search for the closest snapped line
220 bool getClosestSL(std::list<Inkscape::SnappedLine> const &list, Inkscape::SnappedLine &result)
222     bool success = false;
224     for (std::list<Inkscape::SnappedLine>::const_iterator i = list.begin(); i != list.end(); i++) {
225         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
226             result = *i;
227             success = true;
228         }
229     }
231     return success;
234 // search for the closest intersection of two snapped lines, which are both member of the same collection
235 bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> const &list, Inkscape::SnappedPoint &result)
237     bool success = false;
239     for (std::list<Inkscape::SnappedLine>::const_iterator i = list.begin(); i != list.end(); i++) {
240         std::list<Inkscape::SnappedLine>::const_iterator j = i;
241         j++;
242         for (; j != list.end(); j++) {
243             Inkscape::SnappedPoint sp = (*i).intersect(*j);
244             if (sp.getAtIntersection()) {
245                 // if it's the first point
246                  bool const c1 = !success;
247                 // or, if it's closer
248                 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
249                 // or, if it's just then look at the other distance
250                 // (only relevant for snapped points which are at an intersection
251                 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
252                 // then prefer this point over the previous one
253                 if (c1 || c2 || c3) {
254                     result = sp;
255                     success = true;
256                 }
257             }
258         }
259     }
261     return success;
264 // search for the closest intersection of two snapped lines, which are in two different collections
265 bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> const &list1, std::list<Inkscape::SnappedLine> const &list2, Inkscape::SnappedPoint &result)
267     bool success = false;
269     for (std::list<Inkscape::SnappedLine>::const_iterator i = list1.begin(); i != list1.end(); i++) {
270         for (std::list<Inkscape::SnappedLine>::const_iterator j = list2.begin(); j != list2.end(); j++) {
271             Inkscape::SnappedPoint sp = (*i).intersect(*j);
272             if (sp.getAtIntersection()) {
273                 // if it's the first point
274                  bool const c1 = !success;
275                 // or, if it's closer
276                 bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
277                 // or, if it's just then look at the other distance
278                 // (only relevant for snapped points which are at an intersection
279                 bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
280                 // then prefer this point over the previous one
281                 if (c1 || c2 || c3) {
282                     result = sp;
283                     success = true;
284                 }
285             }
286         }
287     }
289     return success;
292 /*
293   Local Variables:
294   mode:c++
295   c-file-style:"stroustrup"
296   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
297   indent-tabs-mode:nil
298   fill-column:99
299   End:
300 */
301 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :