Code

7fc6a32a473db511b5c3e49a2c82a10d628010a2
[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) const
32 {
33     Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
35     /* Get the lines that we will try to snap to */
36     const LineList lines = _getSnapLines(p);
38     // std::cout << "snap point " << p << " to: " << std::endl;
40     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
41         NR::Point const p1 = i->second; // point at guide/grid line
42         NR::Point const p2 = p1 + NR::rot90(i->first); // 2nd point at guide/grid line
43         // std::cout << "  line through " << i->second << " with normal " << i->first;
44         g_assert(i->first != NR::Point(0,0)); // we cannot project on an linesegment of zero length
45         
46         NR::Point const p_proj = project_on_linesegment(p, p1, p2);
47         NR::Coord const dist = NR::L2(p_proj - p);
48         //Store any line that's within snapping range
49         if (dist < getDistance()) {
50             _addSnappedLine(sc, p_proj, dist, i->first, i->second);
51             // std::cout << " -> distance = " << dist; 
52         }     
53         // std::cout << std::endl;
54     }    
55 }
57 void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc,
58                                                Inkscape::Snapper::PointType const &/*t*/,
59                                                NR::Point const &p,
60                                                bool const &/*f*/,
61                                                std::vector<NR::Point> &/*points_to_snap*/,
62                                                ConstraintLine const &c,
63                                                std::list<SPItem const *> const &/*it*/) const
65 {
66     Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
68     /* Get the lines that we will try to snap to */
69     const LineList lines = _getSnapLines(p);
71     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
73         /* Normal to the line we're trying to snap along */
74         NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
76         NR::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
78         /* Constant term of the line we're trying to snap along */
79         NR::Coord const q0 = dot(n, point_on_line);
80         /* Constant term of the grid or guide line */
81         NR::Coord const q1 = dot(i->first, i->second);        
83         /* Try to intersect this line with the target line */
84         Geom::Point t_2geom(NR_HUGE, NR_HUGE);
85         Geom::IntersectorKind const k = Geom::line_intersection(n.to_2geom(), q0, i->first.to_2geom(), q1, t_2geom);
86         NR::Point t(t_2geom);
88         if (k == Geom::intersects) {
89             const NR::Coord dist = L2(t - p);
90             if (dist < getDistance()) {
91                                 // When doing a constrained snap, we're already at an intersection.
92                 // This snappoint is therefore fully constrained, so there's no need
93                 // to look for additional intersections; just return the snapped point
94                 // and forget about the line
95                 sc.points.push_back(SnappedPoint(t, dist)); 
96             }
97         }
98     }
99 }
101 /*
102   Local Variables:
103   mode:c++
104   c-file-style:"stroustrup"
105   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
106   indent-tabs-mode:nil
107   fill-column:99
108   End:
109 */
110 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :