Code

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