Code

add geom-nodetype. helper function to determine the nodetype between two curves
[inkscape.git] / src / helper / geom-nodetype.cpp
1 #define INKSCAPE_HELPER_GEOM_NODETYPE_CPP
3 /**
4  * Specific nodetype geometry functions for Inkscape, not provided my lib2geom.
5  *
6  * Author:
7  *   Johan Engelen <goejendaagh@zonnet.nl>
8  *
9  * Copyright (C) 2008 Johan Engelen
10  *
11  * Released under GNU GPL
12  */
14 #include "helper/geom-nodetype.h"
16 #include <2geom/curve.h>
17 #include <2geom/point.h>
18 #include <vector>
20 namespace Geom {
22 /*
23  * Returns the nodetype between c_incoming and c_outgoing. Location of the node is
24  * at c_incoming.pointAt(1) == c_outgoing.pointAt(0). If these two are unequal, 
25  * the returned type is NODE_NONE.
26  * This method uses exact floating point comparison, so the final and initial points of
27  * the two input curves should match exactly!
28  */
29 NodeType get_nodetype(Curve const &c_incoming, Curve const &c_outgoing)
30 {
31     // FIXME: this should be exact floating point match, not are_near!
32     if ( !are_near(c_incoming.pointAt(1), c_outgoing.pointAt(0)) )
33         return NODE_NONE;
35     Curve * c1_reverse = c_incoming.reverse();
36     std::vector<Point> deriv1 = c1_reverse->pointAndDerivatives(0, 3);
37     delete c1_reverse;
38     std::vector<Point> deriv2 = c_outgoing.pointAndDerivatives(0, 3);
40     // Determine lowest derivative that is non-zero
41     int n1 = 1;
42     while ( (deriv1[n1] == Point(0,0)) && (n1 < 3) ) {
43         n1++;
44     }
45     int n2 = 1;
46     while ( (deriv2[n2] == Point(0,0)) && (n2 < 3) ) {
47         n2++;
48     }
50     double const angle1 = Geom::atan2(-deriv1[n1]);
51     double const angle2 = Geom::atan2(deriv2[n2]);
53     if ( !are_near(angle1, angle2) )
54         return NODE_CUSP;   // derivatives are not colinear
56     // Apparently, the derivatives are colinear but does the order of the derivatives match?
57     if (n1 != n2)
58         return NODE_SMOOTH;
59     else
60         return NODE_SYMM;
61 }
63 }
65 /*
66   Local Variables:
67   mode:c++
68   c-file-style:"stroustrup"
69   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
70   indent-tabs-mode:nil
71   fill-column:99
72   End:
73 */
74 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :