Code

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