Welcome to mirror list, hosted at ThFree Co, Russian Federation.

boost_alg.hpp « libnest2d « libnest2d « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67e19fcbdd2b66cf22b77fe6a9e03ae35fe73149 (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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#ifndef BOOST_ALG_HPP
#define BOOST_ALG_HPP

#ifndef DISABLE_BOOST_SERIALIZE
    #include <sstream>
#endif

#ifdef __clang__
#undef _MSC_EXTENSIONS
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4244)
#pragma warning(disable: 4267)
#endif
#include <boost/geometry.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
// this should be removed to not confuse the compiler
// #include <libnest2d.h>

namespace bp2d {

using libnest2d::TCoord;
using libnest2d::PointImpl;
using Coord = TCoord<PointImpl>;
using libnest2d::PolygonImpl;
using libnest2d::PathImpl;
using libnest2d::Orientation;
using libnest2d::OrientationType;
using libnest2d::getX;
using libnest2d::getY;
using libnest2d::setX;
using libnest2d::setY;
using Box = libnest2d::_Box<PointImpl>;
using Segment = libnest2d::_Segment<PointImpl>;
using Shapes = libnest2d::Nfp::Shapes<PolygonImpl>;

}

/**
 * We have to make all the libnest2d geometry types available to boost. The real
 * models of the geometries remain the same if a conforming model for libnest2d
 * was defined by the library client. Boost is used only as an optional
 * implementer of some algorithms that can be implemented by the model itself
 * if a faster alternative exists.
 *
 * However, boost has its own type traits and we have to define the needed
 * specializations to be able to use boost::geometry. This can be done with the
 * already provided model.
 */
namespace boost {
namespace geometry {
namespace traits {

/* ************************************************************************** */
/* Point concept adaptaion ************************************************** */
/* ************************************************************************** */

template<> struct tag<bp2d::PointImpl> {
    using type = point_tag;
};

template<> struct coordinate_type<bp2d::PointImpl> {
    using type = bp2d::Coord;
};

template<> struct coordinate_system<bp2d::PointImpl> {
    using type = cs::cartesian;
};

template<> struct dimension<bp2d::PointImpl>: boost::mpl::int_<2> {};

template<>
struct access<bp2d::PointImpl, 0 > {
    static inline bp2d::Coord get(bp2d::PointImpl const& a) {
        return libnest2d::getX(a);
    }

    static inline void set(bp2d::PointImpl& a,
                           bp2d::Coord const& value) {
        libnest2d::setX(a, value);
    }
};

template<>
struct access<bp2d::PointImpl, 1 > {
    static inline bp2d::Coord get(bp2d::PointImpl const& a) {
        return libnest2d::getY(a);
    }

    static inline void set(bp2d::PointImpl& a,
                           bp2d::Coord const& value) {
        libnest2d::setY(a, value);
    }
};


/* ************************************************************************** */
/* Box concept adaptaion **************************************************** */
/* ************************************************************************** */

template<> struct tag<bp2d::Box> {
    using type = box_tag;
};

template<> struct point_type<bp2d::Box> {
    using type = bp2d::PointImpl;
};

template<> struct indexed_access<bp2d::Box, min_corner, 0> {
    static inline bp2d::Coord get(bp2d::Box const& box) {
        return bp2d::getX(box.minCorner());
    }
    static inline void set(bp2d::Box &box, bp2d::Coord const& coord) {
        bp2d::setX(box.minCorner(), coord);
    }
};

template<> struct indexed_access<bp2d::Box, min_corner, 1> {
    static inline bp2d::Coord get(bp2d::Box const& box) {
        return bp2d::getY(box.minCorner());
    }
    static inline void set(bp2d::Box &box, bp2d::Coord const& coord) {
        bp2d::setY(box.minCorner(), coord);
    }
};

template<> struct indexed_access<bp2d::Box, max_corner, 0> {
    static inline bp2d::Coord get(bp2d::Box const& box) {
        return bp2d::getX(box.maxCorner());
    }
    static inline void set(bp2d::Box &box, bp2d::Coord const& coord) {
        bp2d::setX(box.maxCorner(), coord);
    }
};

template<> struct indexed_access<bp2d::Box, max_corner, 1> {
    static inline bp2d::Coord get(bp2d::Box const& box) {
        return bp2d::getY(box.maxCorner());
    }
    static inline void set(bp2d::Box &box, bp2d::Coord const& coord) {
        bp2d::setY(box.maxCorner(), coord);
    }
};

/* ************************************************************************** */
/* Segment concept adaptaion ************************************************ */
/* ************************************************************************** */

template<> struct tag<bp2d::Segment> {
    using type = segment_tag;
};

template<> struct point_type<bp2d::Segment> {
    using type = bp2d::PointImpl;
};

template<> struct indexed_access<bp2d::Segment, 0, 0> {
    static inline bp2d::Coord get(bp2d::Segment const& seg) {
        return bp2d::getX(seg.first());
    }
    static inline void set(bp2d::Segment &seg, bp2d::Coord const& coord) {
        auto p = seg.first(); bp2d::setX(p, coord); seg.first(p);
    }
};

template<> struct indexed_access<bp2d::Segment, 0, 1> {
    static inline bp2d::Coord get(bp2d::Segment const& seg) {
        return bp2d::getY(seg.first());
    }
    static inline void set(bp2d::Segment &seg, bp2d::Coord const& coord) {
        auto p = seg.first(); bp2d::setY(p, coord); seg.first(p);
    }
};

template<> struct indexed_access<bp2d::Segment, 1, 0> {
    static inline bp2d::Coord get(bp2d::Segment const& seg) {
        return bp2d::getX(seg.second());
    }
    static inline void set(bp2d::Segment &seg, bp2d::Coord const& coord) {
        auto p = seg.second();  bp2d::setX(p, coord); seg.second(p);
    }
};

template<> struct indexed_access<bp2d::Segment, 1, 1> {
    static inline bp2d::Coord get(bp2d::Segment const& seg) {
        return bp2d::getY(seg.second());
    }
    static inline void set(bp2d::Segment &seg, bp2d::Coord const& coord) {
        auto p = seg.second(); bp2d::setY(p, coord); seg.second(p);
    }
};


/* ************************************************************************** */
/* Polygon concept adaptation *********************************************** */
/* ************************************************************************** */

// Connversion between libnest2d::Orientation and order_selector ///////////////

template<bp2d::Orientation> struct ToBoostOrienation {};

template<>
struct ToBoostOrienation<bp2d::Orientation::CLOCKWISE> {
    static const order_selector Value = clockwise;
};

template<>
struct ToBoostOrienation<bp2d::Orientation::COUNTER_CLOCKWISE> {
    static const order_selector Value = counterclockwise;
};

static const bp2d::Orientation RealOrientation =
        bp2d::OrientationType<bp2d::PolygonImpl>::Value;

// Ring implementation /////////////////////////////////////////////////////////

// Boost would refer to ClipperLib::Path (alias bp2d::PolygonImpl) as a ring
template<> struct tag<bp2d::PathImpl> {
    using type = ring_tag;
};

template<> struct point_order<bp2d::PathImpl> {
    static const order_selector value =
            ToBoostOrienation<RealOrientation>::Value;
};

// All our Paths should be closed for the bin packing application
template<> struct closure<bp2d::PathImpl> {
    static const closure_selector value = closed;
};

// Polygon implementation //////////////////////////////////////////////////////

template<> struct tag<bp2d::PolygonImpl> {
    using type = polygon_tag;
};

template<> struct exterior_ring<bp2d::PolygonImpl> {
    static inline bp2d::PathImpl& get(bp2d::PolygonImpl& p) {
        return libnest2d::ShapeLike::getContour(p);
    }

    static inline bp2d::PathImpl const& get(bp2d::PolygonImpl const& p) {
        return libnest2d::ShapeLike::getContour(p);
    }
};

template<> struct ring_const_type<bp2d::PolygonImpl> {
    using type = const bp2d::PathImpl&;
};

template<> struct ring_mutable_type<bp2d::PolygonImpl> {
    using type = bp2d::PathImpl&;
};

template<> struct interior_const_type<bp2d::PolygonImpl> {
   using type = const libnest2d::THolesContainer<bp2d::PolygonImpl>&;
};

template<> struct interior_mutable_type<bp2d::PolygonImpl> {
   using type = libnest2d::THolesContainer<bp2d::PolygonImpl>&;
};

template<>
struct interior_rings<bp2d::PolygonImpl> {

    static inline libnest2d::THolesContainer<bp2d::PolygonImpl>& get(
            bp2d::PolygonImpl& p)
    {
        return libnest2d::ShapeLike::holes(p);
    }

    static inline const libnest2d::THolesContainer<bp2d::PolygonImpl>& get(
            bp2d::PolygonImpl const& p)
    {
        return libnest2d::ShapeLike::holes(p);
    }
};

/* ************************************************************************** */
/* MultiPolygon concept adaptation ****************************************** */
/* ************************************************************************** */

template<> struct tag<bp2d::Shapes> {
    using type = multi_polygon_tag;
};

}   // traits
}   // geometry

// This is an addition to the ring implementation of Polygon concept
template<>
struct range_value<bp2d::PathImpl> {
    using type = bp2d::PointImpl;
};

template<>
struct range_value<bp2d::Shapes> {
    using type = bp2d::PolygonImpl;
};

}   // boost

/* ************************************************************************** */
/* Algorithms *************************************************************** */
/* ************************************************************************** */

namespace libnest2d { // Now the algorithms that boost can provide...

template<>
inline double PointLike::distance(const PointImpl& p1,
                                  const PointImpl& p2 )
{
    return boost::geometry::distance(p1, p2);
}

template<>
inline double PointLike::distance(const PointImpl& p,
                                  const bp2d::Segment& seg )
{
    return boost::geometry::distance(p, seg);
}

// Tell libnest2d how to make string out of a ClipperPolygon object
template<>
inline bool ShapeLike::intersects(const PathImpl& sh1,
                                  const PathImpl& sh2)
{
    return boost::geometry::intersects(sh1, sh2);
}

// Tell libnest2d how to make string out of a ClipperPolygon object
template<>
inline bool ShapeLike::intersects(const PolygonImpl& sh1,
                                  const PolygonImpl& sh2)
{
    return boost::geometry::intersects(sh1, sh2);
}

// Tell libnest2d how to make string out of a ClipperPolygon object
template<>
inline bool ShapeLike::intersects(const bp2d::Segment& s1,
                                  const bp2d::Segment& s2)
{
    return boost::geometry::intersects(s1, s2);
}

#ifndef DISABLE_BOOST_AREA
template<>
inline double ShapeLike::area(const PolygonImpl& shape)
{
    return boost::geometry::area(shape);
}
#endif

template<>
inline bool ShapeLike::isInside<PolygonImpl>(const PointImpl& point,
                                const PolygonImpl& shape)
{
    return boost::geometry::within(point, shape);
}

template<>
inline bool ShapeLike::isInside(const PolygonImpl& sh1,
                                const PolygonImpl& sh2)
{
    return boost::geometry::within(sh1, sh2);
}

template<>
inline bool ShapeLike::touches( const PolygonImpl& sh1,
                                const PolygonImpl& sh2)
{
    return boost::geometry::touches(sh1, sh2);
}

template<>
inline bool ShapeLike::touches( const PointImpl& point,
                                const PolygonImpl& shape)
{
    return boost::geometry::touches(point, shape);
}

#ifndef DISABLE_BOOST_BOUNDING_BOX
template<>
inline bp2d::Box ShapeLike::boundingBox(const PolygonImpl& sh)
{
    bp2d::Box b;
    boost::geometry::envelope(sh, b);
    return b;
}

template<>
inline bp2d::Box ShapeLike::boundingBox<PolygonImpl>(const bp2d::Shapes& shapes)
{
    bp2d::Box b;
    boost::geometry::envelope(shapes, b);
    return b;
}
#endif

#ifndef DISABLE_BOOST_CONVEX_HULL
template<>
inline PolygonImpl ShapeLike::convexHull(const PolygonImpl& sh)
{
    PolygonImpl ret;
    boost::geometry::convex_hull(sh, ret);
    return ret;
}

template<>
inline PolygonImpl ShapeLike::convexHull(const bp2d::Shapes& shapes)
{
    PolygonImpl ret;
    boost::geometry::convex_hull(shapes, ret);
    return ret;
}
#endif

#ifndef DISABLE_BOOST_ROTATE
template<>
inline void ShapeLike::rotate(PolygonImpl& sh, const Radians& rads)
{
    namespace trans = boost::geometry::strategy::transform;

    PolygonImpl cpy = sh;
    trans::rotate_transformer<boost::geometry::radian, Radians, 2, 2>
            rotate(rads);

    boost::geometry::transform(cpy, sh, rotate);
}
#endif

#ifndef DISABLE_BOOST_TRANSLATE
template<>
inline void ShapeLike::translate(PolygonImpl& sh, const PointImpl& offs)
{
    namespace trans = boost::geometry::strategy::transform;

    PolygonImpl cpy = sh;
    trans::translate_transformer<bp2d::Coord, 2, 2> translate(
                bp2d::getX(offs), bp2d::getY(offs));

    boost::geometry::transform(cpy, sh, translate);
}
#endif

#ifndef DISABLE_BOOST_OFFSET
template<>
inline void ShapeLike::offset(PolygonImpl& sh, bp2d::Coord distance)
{
    PolygonImpl cpy = sh;
    boost::geometry::buffer(cpy, sh, distance);
}
#endif

#ifndef DISABLE_BOOST_NFP_MERGE
template<>
inline bp2d::Shapes Nfp::merge(const bp2d::Shapes& shapes,
                               const PolygonImpl& sh)
{
    bp2d::Shapes retv;
    boost::geometry::union_(shapes, sh, retv);
    return retv;
}
#endif

#ifndef DISABLE_BOOST_SERIALIZE
template<> inline std::string ShapeLike::serialize<libnest2d::Formats::SVG>(
        const PolygonImpl& sh, double scale)
{
    std::stringstream ss;
    std::string style = "fill: none; stroke: black; stroke-width: 1px;";

    using namespace boost::geometry;
    using Pointf = model::point<double, 2, cs::cartesian>;
    using Polygonf = model::polygon<Pointf>;

    Polygonf::ring_type ring;
    Polygonf::inner_container_type holes;
    ring.reserve(ShapeLike::contourVertexCount(sh));

    for(auto it = ShapeLike::cbegin(sh); it != ShapeLike::cend(sh); it++) {
        auto& v = *it;
        ring.emplace_back(getX(v)*scale, getY(v)*scale);
    };

    auto H = ShapeLike::holes(sh);
    for(PathImpl& h : H ) {
        Polygonf::ring_type hf;
        for(auto it = h.begin(); it != h.end(); it++) {
            auto& v = *it;
            hf.emplace_back(getX(v)*scale, getY(v)*scale);
        };
        holes.push_back(hf);
    }

    Polygonf poly;
    poly.outer() = ring;
    poly.inners() = holes;
    auto svg_data = boost::geometry::svg(poly, style);

    ss << svg_data << std::endl;

    return ss.str();
}
#endif

#ifndef DISABLE_BOOST_UNSERIALIZE
template<>
inline void ShapeLike::unserialize<libnest2d::Formats::SVG>(
        PolygonImpl& sh,
        const std::string& str)
{
}
#endif

template<> inline std::pair<bool, std::string>
ShapeLike::isValid(const PolygonImpl& sh)
{
    std::string message;
    bool ret = boost::geometry::is_valid(sh, message);

    return {ret, message};
}

}



#endif // BOOST_ALG_HPP