Code

Node tool: correctly save node skewing to undo history
[inkscape.git] / src / snapped-curve.cpp
1 /**
2  *    \file src/snapped-curve.cpp
3  *    \brief SnappedCurve 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-curve.h"
12 #include <2geom/crossing.h>
13 #include <2geom/path-intersection.h>
15 Inkscape::SnappedCurve::SnappedCurve(Geom::Point const &snapped_point, int num_path, int num_segm, Geom::Coord const &snapped_distance, Geom::Coord const &snapped_tolerance, bool const &always_snap, bool const &fully_constrained, Geom::Curve const *curve, SnapSourceType source, long source_num, SnapTargetType target, Geom::OptRect target_bbox)
16 {
17     _num_path = num_path;
18     _num_segm = num_segm;
19     _distance = snapped_distance;
20     _tolerance = std::max(snapped_tolerance, 1.0);
21     _always_snap = always_snap;
22     _curve = curve;
23     _second_distance = NR_HUGE;
24     _second_tolerance = 1;
25     _second_always_snap = false;
26     _point = snapped_point;
27     _at_intersection = false;
28     _fully_constrained = fully_constrained;
29     _source = source;
30     _source_num = source_num;
31     _target = target;
32     _target_bbox = target_bbox;
33 }
35 Inkscape::SnappedCurve::SnappedCurve()
36 {
37     _num_path = 0;
38     _num_segm = 0;
39     _distance = NR_HUGE;
40     _tolerance = 1;
41     _always_snap = false;
42     _curve = NULL;
43     _second_distance = NR_HUGE;
44     _second_tolerance = 1;
45     _second_always_snap = false;
46     _point = Geom::Point(0,0);
47     _at_intersection = false;
48     _fully_constrained = false;
49     _source = SNAPSOURCE_UNDEFINED;
50     _source_num = -1;
51     _target = SNAPTARGET_UNDEFINED;
52     _target_bbox = Geom::OptRect();
53 }
55 Inkscape::SnappedCurve::~SnappedCurve()
56 {
57 }
59 Inkscape::SnappedPoint Inkscape::SnappedCurve::intersect(SnappedCurve const &curve, Geom::Point const &p, Geom::Matrix dt2doc) const
60 {
61     // Calculate the intersections of two curves, which are both within snapping range, and
62     // return only the closest intersection
63     // The point of intersection should be considered for snapping, but might be outside the snapping range
64     // PS: We need p (the location of the mouse pointer) for find out which intersection is the
65     // closest, as there might be multiple intersections of two curves
66     Geom::Crossings cs = crossings(*(this->_curve), *(curve._curve));
68     if (cs.size() > 0) {
69         // There might be multiple intersections: find the closest
70         Geom::Coord best_dist = NR_HUGE;
71         Geom::Point best_p = Geom::Point(NR_HUGE, NR_HUGE);
72         for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
73             Geom::Point p_ix = this->_curve->pointAt((*i).ta);
74             Geom::Coord dist = Geom::distance(p_ix, p);
76             // Test if we have two segments (curves) from the same path..
77             if (this->_num_path == curve._num_path) {
78                 // Never try to intersect a segment with itself
79                 if (this->_num_segm == curve._num_segm) continue;
80                 // Two subsequent segments (curves) in a path will have a common node; this node is not considered to be an intersection
81                 if (this->_num_segm == curve._num_segm + 1 && (*i).ta == 0 && (*i).tb == 1) continue;
82                 if (this->_num_segm + 1 == curve._num_segm && (*i).ta == 1 && (*i).tb == 0) continue;
83             }
85             if (dist < best_dist) {
86                 best_dist = dist;
87                 best_p = p_ix;
88             }
89         }
91         // Now we've found the closest intersection, return it as a SnappedPoint
92         bool const use_this_as_primary = _distance < curve.getSnapDistance();
93         Inkscape::SnappedCurve const *primaryC = use_this_as_primary ? this : &curve;
94         Inkscape::SnappedCurve const *secondaryC = use_this_as_primary ? &curve : this;
96         // The intersection should in fact be returned in desktop coordinates
97         best_p = best_p * dt2doc;
99         Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(best_p - this->getPoint()) : Geom::L2(best_p - curve.getPoint());
100         Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(best_p - curve.getPoint()) : Geom::L2(best_p - this->getPoint());
101         // TODO: Investigate whether it is possible to use document coordinates everywhere
102         // in the snapper code. Only the mouse position should be in desktop coordinates, I guess.
103         // All paths are already in document coords and we are certainly not going to change THAT.
104         return SnappedPoint(best_p, Inkscape::SNAPSOURCE_UNDEFINED, primaryC->getSourceNum(), Inkscape::SNAPTARGET_PATH_INTERSECTION, primaryDist, primaryC->getTolerance(), primaryC->getAlwaysSnap(), true, false, true,
105                 secondaryDist, secondaryC->getTolerance(), secondaryC->getAlwaysSnap());
106     }
108     // No intersection
109     return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, 0, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, false, NR_HUGE, 0, false);
112 // search for the closest snapped line
113 bool getClosestCurve(std::list<Inkscape::SnappedCurve> const &list, Inkscape::SnappedCurve &result)
115     bool success = false;
117     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
118         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
119             result = *i;
120             success = true;
121         }
122     }
124     return success;
127 // search for the closest intersection of two snapped curves, which are both member of the same collection
128 bool getClosestIntersectionCS(std::list<Inkscape::SnappedCurve> const &list, Geom::Point const &p, Inkscape::SnappedPoint &result, Geom::Matrix dt2doc)
130     bool success = false;
132     for (std::list<Inkscape::SnappedCurve>::const_iterator i = list.begin(); i != list.end(); i++) {
133         if ((*i).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes,
134             // as this would require two bboxes two be flashed in the snap indicator
135             std::list<Inkscape::SnappedCurve>::const_iterator j = i;
136             j++;
137             for (; j != list.end(); j++) {
138                 if ((*j).getTarget() != Inkscape::SNAPTARGET_BBOX_EDGE) { // We don't support snapping to intersections of bboxes
139                     Inkscape::SnappedPoint sp = (*i).intersect(*j, p, dt2doc);
140                     if (sp.getAtIntersection()) {
141                         // if it's the first point
142                         bool const c1 = !success;
143                         // or, if it's closer
144                         bool const c2 = sp.getSnapDistance() < result.getSnapDistance();
145                         // or, if it's just as close then look at the other distance
146                         // (only relevant for snapped points which are at an intersection)
147                         bool const c3 = (sp.getSnapDistance() == result.getSnapDistance()) && (sp.getSecondSnapDistance() < result.getSecondSnapDistance());
148                         // then prefer this point over the previous one
149                         if (c1 || c2 || c3) {
150                             result = sp;
151                             success = true;
152                         }
153                     }
154                 }
155             }
156         }
157     }
159     return success;
161 /*
162   Local Variables:
163   mode:c++
164   c-file-style:"stroustrup"
165   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166   indent-tabs-mode:nil
167   fill-column:99
168   End:
169 */
170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :