Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / line-snapper.cpp
1 /**
2  * \file line-snapper.cpp
3  * \brief LineSnapper class.
4  *
5  * Authors:
6  *   Diederik van Lierop <mail@diedenrezi.nl>
7  *   And others...
8  *
9  * Copyright (C) 1999-2008 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include <2geom/line.h>
15 #include "line-snapper.h"
16 #include "snapped-line.h"
17 //#include <gtk/gtk.h>
18 #include "snap.h"
20 Inkscape::LineSnapper::LineSnapper(SnapManager *sm, Geom::Coord const d) : Snapper(sm, d)
21 {
22 }
24 void Inkscape::LineSnapper::freeSnap(SnappedConstraints &sc,
25                                                     Inkscape::SnapPreferences::PointType const &t,
26                                                     Geom::Point const &p,
27                                                     SnapSourceType const &source_type,
28                                                     long source_num,
29                                                     Geom::OptRect const &/*bbox_to_snap*/,
30                                                     std::vector<SPItem const *> const */*it*/,
31                                                     std::vector<std::pair<Geom::Point, int> > */*unselected_nodes*/) const
32 {
33     if (!(_snap_enabled && _snapmanager->snapprefs.getSnapFrom(t)) ) {
34         return;
35     }
37     /* Get the lines that we will try to snap to */
38     const LineList lines = _getSnapLines(p);
40     // std::cout << "snap point " << p << " to: " << std::endl;
42     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
43         Geom::Point const p1 = i->second; // point at guide/grid line
44         Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
45         // std::cout << "  line through " << i->second << " with normal " << i->first;
46         g_assert(i->first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
48         Geom::Point const p_proj = Geom::projection(p, Geom::Line(p1, p2));
49         Geom::Coord const dist = Geom::L2(p_proj - p);
50         //Store any line that's within snapping range
51         if (dist < getSnapperTolerance()) {
52             _addSnappedLine(sc, p_proj, dist, source_type, source_num, i->first, i->second);
53             // For any line that's within range, we will also look at it's "point on line" p1. For guides
54             // this point coincides with its origin; for grids this is of no use, but we cannot
55             // discern between grids and guides here
56             Geom::Coord const dist_p1 = Geom::L2(p1 - p);
57             if (dist_p1 < getSnapperTolerance()) {
58                 _addSnappedLinesOrigin(sc, p1, dist_p1, source_type, source_num);
59                 // Only relevant for guides; grids don't have an origin per line
60                 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
61             }
62             // std::cout << " -> distance = " << dist;
63         }
64         // std::cout << std::endl;
65     }
66 }
68 void Inkscape::LineSnapper::constrainedSnap(SnappedConstraints &sc,
69                                                Inkscape::SnapPreferences::PointType const &t,
70                                                Geom::Point const &p,
71                                                SnapSourceType const &source_type,
72                                                long source_num,
73                                                Geom::OptRect const &/*bbox_to_snap*/,
74                                                ConstraintLine const &c,
75                                                std::vector<SPItem const *> const */*it*/) const
77 {
78     if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
79         return;
80     }
82     /* Get the lines that we will try to snap to */
83     const LineList lines = _getSnapLines(p);
85     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
86         if (Geom::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
87             // constraint line
88             Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
89             Geom::Line line1(point_on_line, point_on_line + c.getDirection());
91             // grid/guide line
92             Geom::Point const p1 = i->second; // point at guide/grid line
93             Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
94             Geom::Line line2(p1, p2);
96             Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
97             try
98             {
99                 inters = Geom::intersection(line1, line2);
100             }
101             catch (Geom::InfiniteSolutions e)
102             {
103                 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
104                 continue; // jump to the next iterator in the for-loop
105             }
107             if (inters) {
108                 Geom::Point t = line1.pointAt((*inters).ta);
109                 const Geom::Coord dist = Geom::L2(t - p);
110                 if (dist < getSnapperTolerance()) {
111                     // When doing a constrained snap, we're already at an intersection.
112                     // This snappoint is therefore fully constrained, so there's no need
113                     // to look for additional intersections; just return the snapped point
114                     // and forget about the line
115                     _addSnappedPoint(sc, t, dist, source_type, source_num);
116                     // For any line that's within range, we will also look at it's "point on line" p1. For guides
117                     // this point coincides with its origin; for grids this is of no use, but we cannot
118                     // discern between grids and guides here
119                     Geom::Coord const dist_p1 = Geom::L2(p1 - p);
120                     if (dist_p1 < getSnapperTolerance()) {
121                         _addSnappedLinesOrigin(sc, p1, dist_p1, source_type, source_num);
122                         // Only relevant for guides; grids don't have an origin per line
123                         // Therefore _addSnappedLinesOrigin() will only be implemented for guides
124                     }
125                 }
126             }
127         }
128     }
131 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
132 // grid-snapper classes will use this default empty method
133 void Inkscape::LineSnapper::_addSnappedLinesOrigin(SnappedConstraints &/*sc*/, Geom::Point const /*origin*/, Geom::Coord const /*snapped_distance*/, SnapSourceType const &/*source_type*/, long /*source_num*/) const
137 /*
138   Local Variables:
139   mode:c++
140   c-file-style:"stroustrup"
141   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
142   indent-tabs-mode:nil
143   fill-column:99
144   End:
145 */
146 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :