summaryrefslogtreecommitdiffstats
path: root/src/helper/geom-pathinfo.cpp
blob: 9e5e409c320da8a6bfdcba652fb3f523998470a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * \file
 * \brief Pathinfo store the _data of a pathvector and allow get info about it
 */ /*
    * Authors:
    * 2015 Jabier Arraiza Cenoz<jabier.arraiza@marker.es>
    *
    * This code is in public domain
    */

#include <helper/geom-pathinfo.h>
#include <2geom/sbasis-to-bezier.h>

/**
 * @brief Pathinfo store the _data of a pathvector and allow get info about it
 *
 */
Pathinfo::Pathinfo(Piecewise<D2<SBasis> > pwd2)
{
    set(pwd2);
}

Pathinfo::Pathinfo(Geom::PathVector path_vector, bool skip_degenerate)
{
    set(path_vector, skip_degenerate);
}


Pathinfo::~Pathinfo() {}


void Pathinfo::set(Piecewise<D2<SBasis> > pwd2)
{
    set(path_from_piecewise(remove_short_cuts(pwd2, 0.1), 0.001));
}
/** Store the base path _data
 */
void Pathinfo::set(Geom::PathVector path_vector, bool skip_degenerate)
{
    _data.clear();
    size_t counter = 0;
    for (PathVector::const_iterator path_it = path_vector.begin();
            path_it != path_vector.end(); ++path_it) 
    {
        if (path_it->empty()) {
            continue;
        }
        Geom::Path::const_iterator curve_it1 = path_it->begin();
        Geom::Path::const_iterator curve_endit = path_it->end_default();
        if (path_it->closed()) {
            const Curve &closingline = path_it->back_closed();
            if (are_near(closingline.initialPoint(), closingline.finalPoint())) {
                curve_endit = path_it->end_open();
            }
        }
        while (curve_it1 != curve_endit) {
            if(curve_it1->isDegenerate() && skip_degenerate ){
                ++curve_it1;
                continue;
            }
            ++curve_it1;
            counter++;
        }
        if (path_it->closed()) {
            _data.push_back(std::make_pair(counter - 1, true));
        } else {
            _data.push_back(std::make_pair(counter - 1, false));
        }
    }
}

size_t Pathinfo::subPathCounter() const
{
    return _data.back().first + 1;
}

size_t Pathinfo::subPathSize(size_t index) const
{
    size_t size = 0;
    if( _data.size() > index){
        size = _data[index].first + 1;
    }
    return size;
}

size_t Pathinfo::subPathIndex(size_t index) const
{
    for (size_t i = 0; i < _data.size(); i++) {
        if (index <= _data[i].first) {
            return i;
        }
    }
    return 0;
}

size_t Pathinfo::last(size_t index) const
{
    for (size_t i = 0; i < _data.size(); i++) {
        if (index <= _data[i].first) {
            return _data[i].first;
        }
    }
    return 0;
}

size_t Pathinfo::first(size_t index) const
{
    for (size_t i = 0; i < _data.size(); i++) {
        if (index <= _data[i].first) {
            if (i == 0) {
                return 0;
            } else {
                return _data[i - 1].first + 1;
            }
        }
    }
    return 0;
}

boost::optional<size_t> Pathinfo::previous(size_t index) const
{
    if (first(index) == index && closed(index)) {
        return last(index);
    }
    if (first(index) == index && !closed(index)) {
        return boost::none;
    }
    return index - 1;
}

boost::optional<size_t> Pathinfo::next(size_t index) const
{
    if (last(index) == index && closed(index)) {
        return first(index);
    }
    if (last(index) == index && !closed(index)) {
        return boost::none;
    }
    return index + 1;
}

bool Pathinfo::closed(size_t index) const
{
    for (size_t i = 0; i < _data.size(); i++) {
        if (index <= _data[i].first) {
            return _data[i].second;
        }
    }
    return false;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim:
// filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99
// :