Code

Node tool: snap to paths and their nodes, incl. to the path currently being edited
[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-2007 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "libnr/nr-values.h"
15 #include "libnr/nr-point-fns.h"
16 #include <2geom/geom.h>
17 #include "line-snapper.h"
18 #include "snapped-line.h"
19 #include <gtk/gtk.h>
21 Inkscape::LineSnapper::LineSnapper(SPNamedView const *nv, NR::Coord const d) : Snapper(nv, d)
22 {
24 }
26 void Inkscape::LineSnapper::_doFreeSnap(SnappedConstraints &sc,
27                                                     Inkscape::Snapper::PointType const &t,
28                                                     NR::Point const &p,
29                                                     bool const &f,
30                                                     std::vector<NR::Point> &points_to_snap,
31                                                     std::list<SPItem const *> const &it,
32                                                     std::vector<NR::Point> *unselected_nodes) const
33 {
34     /* Get the lines that we will try to snap to */
35     const LineList lines = _getSnapLines(p);
37     // std::cout << "snap point " << p << " to: " << std::endl;
39     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
40         NR::Point const p1 = i->second; // point at guide/grid line
41         NR::Point const p2 = p1 + NR::rot90(i->first); // 2nd point at guide/grid line
42         // std::cout << "  line through " << i->second << " with normal " << i->first;
43         g_assert(i->first != NR::Point(0,0)); // we cannot project on an linesegment of zero length
44         
45         NR::Point const p_proj = project_on_linesegment(p, p1, p2);
46         NR::Coord const dist = NR::L2(p_proj - p);
47         //Store any line that's within snapping range
48         if (dist < getSnapperTolerance()) {
49             _addSnappedLine(sc, p_proj, dist, i->first, i->second);
50             // std::cout << " -> distance = " << dist; 
51         }     
52         // std::cout << std::endl;
53     }    
54 }
56 void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc,
57                                                Inkscape::Snapper::PointType const &/*t*/,
58                                                NR::Point const &p,
59                                                bool const &/*f*/,
60                                                std::vector<NR::Point> &/*points_to_snap*/,
61                                                ConstraintLine const &c,
62                                                std::list<SPItem const *> const &/*it*/) const
64 {
65     /* Get the lines that we will try to snap to */
66     const LineList lines = _getSnapLines(p);
68     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
69         if (NR::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
70             /* Normal to the line we're trying to snap along */
71             NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
72     
73             NR::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
74     
75             /* Constant term of the line we're trying to snap along */
76             NR::Coord const q0 = dot(n, point_on_line);
77             /* Constant term of the grid or guide line */
78             NR::Coord const q1 = dot(i->first, i->second);        
79     
80             /* Try to intersect this line with the target line */
81             Geom::Point t_2geom(NR_HUGE, NR_HUGE);
82             Geom::IntersectorKind const k = Geom::line_intersection(n.to_2geom(), q0, i->first.to_2geom(), q1, t_2geom);
83             NR::Point t(t_2geom);
84     
85             if (k == Geom::intersects) {
86                 const NR::Coord dist = L2(t - p);
87                 if (dist < getSnapperTolerance()) {
88                                 // When doing a constrained snap, we're already at an intersection.
89                     // This snappoint is therefore fully constrained, so there's no need
90                     // to look for additional intersections; just return the snapped point
91                     // and forget about the line
92                     sc.points.push_back(SnappedPoint(t, dist, getSnapperTolerance(), getSnapperAlwaysSnap())); 
93                 }
94             }
95         }
96     }
97 }
99 /*
100   Local Variables:
101   mode:c++
102   c-file-style:"stroustrup"
103   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
104   indent-tabs-mode:nil
105   fill-column:99
106   End:
107 */
108 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :