summaryrefslogtreecommitdiffstats
path: root/src/helper/geom-pathinfo.cpp
blob: d73f8f707f84ace8c330a7bbc8e2c193bf316085 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * \file
 * \brief Pathinfo iterate a Geom::PathVector and allow get info about it.
 * \Usualy need a curve index to get the results
 * \TODO: migrate more Inkscape loops to use 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 Geom::PathVector and allow get info about it
 *
 */
Pathinfo::Pathinfo() {}

Pathinfo::~Pathinfo() {}


void Pathinfo::set(Geom::Piecewise<Geom::D2<Geom::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 (Geom::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()) {
            Geom::Curve const &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 of pathvector
 */
size_t Pathinfo::size() const
{
    return _data.back().first + 1;
}

/** Size of subpath
 */
size_t Pathinfo::subPathSize(size_t subpath_index) const
{
    size_t size = 0;
    if( _data.size() > subpath_index){
        double prev = 0;
        if(subpath_index != 0){
            prev = _data[subpath_index - 1].first;
        }
        size = prev - _data[subpath_index].first + 1;
    }
    return size;
}

/** Get subpath index from a curve index
 */
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;
}

/** Get subpath last index given a curve index
 */
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;
}

/** Get subpath first index given a curve index
 */
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;
}

/** Get previous index given a curve index
 */
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;
}

/** Get next index given a curve index
 */
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;
}

/** Get if subpath is closed given a curve index
 */
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
// :