File size: 9,760 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
 *   Copyright (c) 2020 sliptonic <shopinthewoods@gmail.com>               *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <Base/Vector3D.h>
#include <Base/Tools.h>

#include "Voronoi.h"


using namespace Base;
using namespace Path;

TYPESYSTEM_SOURCE(Path::Voronoi, Base::BaseClass)

// Helpers

// Voronoi::diagram_type

Voronoi::diagram_type::diagram_type()
    : scale(1000)
{}

double Voronoi::diagram_type::getScale() const
{
    return scale;
}

void Voronoi::diagram_type::setScale(double s)
{
    scale = s;
}

Base::Vector3d Voronoi::diagram_type::scaledVector(double x, double y, double z) const
{
    return Base::Vector3d(x / scale, y / scale, z);
}

Base::Vector3d Voronoi::diagram_type::scaledVector(const point_type& p, double z) const
{
    return scaledVector(p.x(), p.y(), z);
}

Base::Vector3d Voronoi::diagram_type::scaledVector(const vertex_type& v, double z) const
{
    return scaledVector(v.x(), v.y(), z);
}


int Voronoi::diagram_type::index(const Voronoi::diagram_type::cell_type* cell) const
{
    auto it = cell_index.find(intptr_t(cell));
    if (it == cell_index.end()) {
        return Voronoi::InvalidIndex;
    }
    return it->second;
}
int Voronoi::diagram_type::index(const Voronoi::diagram_type::edge_type* edge) const
{
    auto it = edge_index.find(intptr_t(edge));
    if (it == edge_index.end()) {
        return Voronoi::InvalidIndex;
    }
    return it->second;
}
int Voronoi::diagram_type::index(const Voronoi::diagram_type::vertex_type* vertex) const
{
    auto it = vertex_index.find(intptr_t(vertex));
    if (it == vertex_index.end()) {
        return Voronoi::InvalidIndex;
    }
    return it->second;
}

void Voronoi::diagram_type::reIndex()
{
    int idx = 0;
    cell_index.clear();
    edge_index.clear();
    vertex_index.clear();

    idx = 0;
    for (auto it = cells().begin(); it != cells().end(); ++it, ++idx) {
        cell_index[intptr_t(&(*it))] = idx;
    }
    idx = 0;
    for (auto it = edges().begin(); it != edges().end(); ++it, ++idx) {
        edge_index[intptr_t(&(*it))] = idx;
    }
    idx = 0;
    for (auto it = vertices().begin(); it != vertices().end(); ++it, ++idx) {
        vertex_index[intptr_t(&(*it))] = idx;
    }
}

Voronoi::point_type Voronoi::diagram_type::retrievePoint(
    const Voronoi::diagram_type::cell_type* cell
) const
{
    Voronoi::diagram_type::cell_type::source_index_type index = cell->source_index();
    Voronoi::diagram_type::cell_type::source_category_type category = cell->source_category();
    if (category == boost::polygon::SOURCE_CATEGORY_SINGLE_POINT) {
        return points[index];
    }
    index -= points.size();
    if (category == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) {
        return low(segments[index]);
    }
    else {
        return high(segments[index]);
    }
}

Voronoi::segment_type Voronoi::diagram_type::retrieveSegment(
    const Voronoi::diagram_type::cell_type* cell
) const
{
    Voronoi::diagram_type::cell_type::source_index_type index = cell->source_index() - points.size();
    return segments[index];
}


// Voronoi

Voronoi::Voronoi()
    : vd(new diagram_type)
{}

Voronoi::~Voronoi()
{}


void Voronoi::addPoint(const Voronoi::point_type& p)
{
    Voronoi::point_type pi;
    pi.x(p.x() * vd->getScale());
    pi.y(p.y() * vd->getScale());
    vd->points.push_back(pi);
}

void Voronoi::addSegment(const Voronoi::segment_type& s)
{
    Voronoi::point_type pil, pih;
    pil.x(low(s).x() * vd->getScale());
    pil.y(low(s).y() * vd->getScale());
    pih.x(high(s).x() * vd->getScale());
    pih.y(high(s).y() * vd->getScale());
    vd->segments.emplace_back(pil, pih);
}

long Voronoi::numPoints() const
{
    return vd->points.size();
}

long Voronoi::numSegments() const
{
    return vd->segments.size();
}


long Voronoi::numCells() const
{
    return vd->num_cells();
}

long Voronoi::numEdges() const
{
    return vd->num_edges();
}

long Voronoi::numVertices() const
{
    return vd->num_vertices();
}

void Voronoi::construct()
{
    vd->clear();
    construct_voronoi(
        vd->points.begin(),
        vd->points.end(),
        vd->segments.begin(),
        vd->segments.end(),
        static_cast<voronoi_diagram_type*>(vd)
    );
    vd->reIndex();
}

void Voronoi::colorExterior(const Voronoi::diagram_type::edge_type* edge, std::size_t colorValue)
{
    if (edge->color()) {
        // end recursion
        return;
    }
    edge->color(colorValue);
    edge->twin()->color(colorValue);
    auto v = edge->vertex1();
    if (!v || !edge->is_primary()) {
        return;
    }
    v->color(colorValue);
    auto e = v->incident_edge();
    do {
        colorExterior(e, colorValue);
        e = e->rot_next();
    } while (e != v->incident_edge());
}

void Voronoi::colorExterior(Voronoi::color_type color)
{
    for (diagram_type::const_edge_iterator it = vd->edges().begin(); it != vd->edges().end(); ++it) {
        if (it->is_infinite()) {
            colorExterior(&(*it), color);
        }
    }
}

void Voronoi::colorTwins(Voronoi::color_type color)
{
    for (diagram_type::const_edge_iterator it = vd->edges().begin(); it != vd->edges().end(); ++it) {
        if (!it->color()) {
            auto twin = it->twin();
            if (!twin->color()) {
                twin->color(color);
            }
        }
    }
}

double Voronoi::diagram_type::angleOfSegment(int i, Voronoi::diagram_type::angle_map_t* angle) const
{
    Voronoi::diagram_type::angle_map_t::const_iterator a = angle
        ? angle->find(i)
        : Voronoi::diagram_type::angle_map_t::const_iterator();
    if (!angle || a == angle->end()) {
        Voronoi::point_type p0 = low(segments[i]);
        Voronoi::point_type p1 = high(segments[i]);
        double ang = 0;
        if (p0.x() == p1.x()) {
            if (p0.y() < p1.y()) {
                ang = std::numbers::pi / 2;
            }
            else {
                ang = -std::numbers::pi / 2;
            }
        }
        else {
            ang = atan((p0.y() - p1.y()) / (p0.x() - p1.x()));
        }
        if (angle) {
            angle->insert(angle_map_t::value_type(i, ang));
        }
        return ang;
    }
    return a->second;
}

static bool pointsMatch(const Voronoi::point_type& p0, const Voronoi::point_type& p1)
{
    return long(p0.x()) == long(p1.x()) && long(p0.y()) == long(p1.y());
}

bool Voronoi::diagram_type::segmentsAreConnected(int i, int j) const
{
    return pointsMatch(low(segments[i]), low(segments[j]))
        || pointsMatch(low(segments[i]), high(segments[j]))
        || pointsMatch(high(segments[i]), low(segments[j]))
        || pointsMatch(high(segments[i]), high(segments[j]));
}

void Voronoi::colorColinear(Voronoi::color_type color, double degree)
{
    using std::numbers::pi;
    double rad = Base::toRadians(degree);

    Voronoi::diagram_type::angle_map_t angle;
    int psize = vd->points.size();

    for (diagram_type::const_edge_iterator it = vd->edges().begin(); it != vd->edges().end(); ++it) {
        int i0 = it->cell()->source_index() - psize;
        int i1 = it->twin()->cell()->source_index() - psize;
        if (it->color() == 0 && it->cell()->contains_segment()
            && it->twin()->cell()->contains_segment() && vd->segmentsAreConnected(i0, i1)) {
            double a0 = vd->angleOfSegment(i0, &angle);
            double a1 = vd->angleOfSegment(i1, &angle);
            double a = a0 - a1;
            if (a > pi / 2) {
                a -= pi;
            }
            else if (a < -pi / 2) {
                a += pi;
            }
            if (fabs(a) < rad) {
                it->color(color);
                it->twin()->color(color);
            }
        }
    }
}

void Voronoi::resetColor(Voronoi::color_type color)
{
    for (auto it = vd->cells().begin(); it != vd->cells().end(); ++it) {
        if (color == 0 || it->color() == color) {
            it->color(0);
        }
    }
    for (auto it = vd->edges().begin(); it != vd->edges().end(); ++it) {
        if (it->color() == color) {
            it->color(0);
        }
    }
    for (auto it = vd->vertices().begin(); it != vd->vertices().end(); ++it) {
        if (it->color() == color) {
            it->color(0);
        }
    }
}