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

SlicesToTriangleMesh.cpp « libslic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6a54696195c742f40ed626ef48dd8797adf1f5e (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

#include "SlicesToTriangleMesh.hpp"

#include "libslic3r/MTUtils.hpp"
#include "libslic3r/SLA/Contour3D.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include "libslic3r/Tesselate.hpp"

#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>

namespace Slic3r {

inline sla::Contour3D wall_strip(const Polygon &poly,
                                 double         lower_z_mm,
                                 double         upper_z_mm)
{
    sla::Contour3D ret;
    
    size_t startidx = ret.points.size();
    size_t offs     = poly.points.size();
    
    ret.points.reserve(ret.points.size() + 2 *offs);
    
    for (const Point &p : poly.points)
        ret.points.emplace_back(to_3d(unscaled(p), lower_z_mm));
    
    for (const Point &p : poly.points)
        ret.points.emplace_back(to_3d(unscaled(p), upper_z_mm));
    
    for (size_t i = startidx + 1; i < startidx + offs; ++i) {
        ret.faces3.emplace_back(i - 1, i, i + offs - 1);
        ret.faces3.emplace_back(i, i + offs, i + offs - 1);
    }
    
    ret.faces3.emplace_back(startidx + offs - 1, startidx, startidx + 2 * offs - 1);
    ret.faces3.emplace_back(startidx, startidx + offs, startidx + 2 * offs - 1);
    
    return ret;
}

// Same as walls() but with identical higher and lower polygons.
sla::Contour3D inline straight_walls(const Polygon &plate,
                                     double         lo_z,
                                     double         hi_z)
{
    return wall_strip(plate, lo_z, hi_z);
}

sla::Contour3D inline straight_walls(const ExPolygon &plate,
                                     double           lo_z,
                                     double           hi_z)
{
    sla::Contour3D ret;
    ret.merge(straight_walls(plate.contour, lo_z, hi_z));
    for (auto &h : plate.holes) ret.merge(straight_walls(h, lo_z, hi_z));
    return ret;
}

sla::Contour3D inline straight_walls(const ExPolygons &slice,
                                     double            lo_z,
                                     double            hi_z)
{   
    sla::Contour3D ret;
    for (const ExPolygon &poly : slice)
        ret.merge(straight_walls(poly, lo_z, hi_z));
    
    return ret;
}

sla::Contour3D slices_to_triangle_mesh(const std::vector<ExPolygons> &slices,
                                       double zmin,
                                       const std::vector<float> &    grid)
{
    assert(slices.size() == grid.size());

    using Layers = std::vector<sla::Contour3D>;
    std::vector<sla::Contour3D> layers(slices.size());
    size_t len = slices.size() - 1;

    tbb::parallel_for(size_t(0), len, [&slices, &layers, &grid](size_t i) {
        const ExPolygons &upper = slices[i + 1];
        const ExPolygons &lower = slices[i];

        ExPolygons dff1 = diff_ex(lower, upper);
        ExPolygons dff2 = diff_ex(upper, lower);
        layers[i].merge(triangulate_expolygons_3d(dff1, grid[i], NORMALS_UP));
        layers[i].merge(triangulate_expolygons_3d(dff2, grid[i], NORMALS_DOWN));
        layers[i].merge(straight_walls(upper, grid[i], grid[i + 1]));
        
    });
    
    sla::Contour3D ret = tbb::parallel_reduce(
        tbb::blocked_range(layers.begin(), layers.end()),
        sla::Contour3D{},
        [](const tbb::blocked_range<Layers::iterator>& r, sla::Contour3D init) {
            for(auto it = r.begin(); it != r.end(); ++it ) init.merge(*it);
            return init;
        },
        []( const sla::Contour3D &a, const sla::Contour3D &b ) {
            sla::Contour3D res{a}; res.merge(b); return res;
        });
    
    ret.merge(triangulate_expolygons_3d(slices.front(), zmin, NORMALS_DOWN));
    ret.merge(straight_walls(slices.front(), zmin, grid.front()));
    ret.merge(triangulate_expolygons_3d(slices.back(), grid.back(), NORMALS_UP));
        
    return ret;
}

void slices_to_triangle_mesh(TriangleMesh &                 mesh,
                             const std::vector<ExPolygons> &slices,
                             double                         zmin,
                             double                         lh,
                             double                         ilh)
{
    std::vector<sla::Contour3D> wall_meshes(slices.size());
    std::vector<float> grid(slices.size(), zmin + ilh);
    
    for (size_t i = 1; i < grid.size(); ++i) grid[i] = grid[i - 1] + lh;
    
    sla::Contour3D cntr = slices_to_triangle_mesh(slices, zmin, grid);
    mesh.merge(sla::to_triangle_mesh(cntr));
    mesh.repaired = true;
    mesh.require_shared_vertices();
}

} // namespace Slic3r