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

Contour3D.cpp « SLA « libslic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e39672b8bdd39f70775c24a7dd93af308f535bd9 (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
#include <libslic3r/SLA/Contour3D.hpp>
#include <libslic3r/SLA/EigenMesh3D.hpp>

#include <libslic3r/Format/objparser.hpp>

namespace Slic3r { namespace sla {

Contour3D::Contour3D(const TriangleMesh &trmesh)
{
    points.reserve(trmesh.its.vertices.size());
    faces3.reserve(trmesh.its.indices.size());
    
    for (auto &v : trmesh.its.vertices)
        points.emplace_back(v.cast<double>());
    
    std::copy(trmesh.its.indices.begin(), trmesh.its.indices.end(),
              std::back_inserter(faces3));
}

Contour3D::Contour3D(TriangleMesh &&trmesh)
{
    points.reserve(trmesh.its.vertices.size());
    
    for (auto &v : trmesh.its.vertices)
        points.emplace_back(v.cast<double>());
    
    faces3.swap(trmesh.its.indices);
}

Contour3D::Contour3D(const EigenMesh3D &emesh) {
    points.reserve(size_t(emesh.V().rows()));
    faces3.reserve(size_t(emesh.F().rows()));
    
    for (int r = 0; r < emesh.V().rows(); r++)
        points.emplace_back(emesh.V().row(r).cast<double>());
    
    for (int i = 0; i < emesh.F().rows(); i++)
        faces3.emplace_back(emesh.F().row(i));
}

Contour3D &Contour3D::merge(const Contour3D &ctr)
{
    auto N = coord_t(points.size());
    auto N_f3 = faces3.size();
    auto N_f4 = faces4.size();
    
    points.insert(points.end(), ctr.points.begin(), ctr.points.end());
    faces3.insert(faces3.end(), ctr.faces3.begin(), ctr.faces3.end());
    faces4.insert(faces4.end(), ctr.faces4.begin(), ctr.faces4.end());
    
    for(size_t n = N_f3; n < faces3.size(); n++) {
        auto& idx = faces3[n]; idx.x() += N; idx.y() += N; idx.z() += N;
    }
    
    for(size_t n = N_f4; n < faces4.size(); n++) {
        auto& idx = faces4[n]; for (int k = 0; k < 4; k++) idx(k) += N;
    }        
    
    return *this;
}

Contour3D &Contour3D::merge(const Pointf3s &triangles)
{
    const size_t offs = points.size();
    points.insert(points.end(), triangles.begin(), triangles.end());
    faces3.reserve(faces3.size() + points.size() / 3);
    
    for(int i = int(offs); i < int(points.size()); i += 3)
        faces3.emplace_back(i, i + 1, i + 2);
    
    return *this;
}

void Contour3D::to_obj(std::ostream &stream)
{
    for(auto& p : points)
        stream << "v " << p.transpose() << "\n";
    
    for(auto& f : faces3) 
        stream << "f " << (f + Vec3i(1, 1, 1)).transpose() << "\n";
    
    for(auto& f : faces4)
        stream << "f " << (f + Vec4i(1, 1, 1, 1)).transpose() << "\n";
}

void Contour3D::from_obj(std::istream &stream)
{
    ObjParser::ObjData data;
    ObjParser::objparse(stream, data);
    
    points.reserve(data.coordinates.size() / 4 + 1);
    auto &coords = data.coordinates;
    for (size_t i = 0; i < coords.size(); i += 4)
        points.emplace_back(coords[i], coords[i + 1], coords[i + 2]);
    
    Vec3i triangle;
    Vec4i quad;
    size_t v = 0;
    while(v < data.vertices.size()) {
        size_t N = 0;
        size_t i = v;
        while (data.vertices[v++].coordIdx != -1) ++N;
        
        std::function<void(int, int)> setfn;
        if (N < 3 || N > 4) continue;
        else if (N == 3) setfn = [&triangle](int k, int f) { triangle(k) = f; };
        else setfn = [&quad](int k, int f) { quad(k) = f; };
        
        for (size_t j = 0; j < N; ++j)
            setfn(int(j), data.vertices[i + j].coordIdx);
    }
}

TriangleMesh to_triangle_mesh(const Contour3D &ctour) {
    if (ctour.faces4.empty()) return {ctour.points, ctour.faces3};
    
    std::vector<Vec3i> triangles;
    
    triangles.reserve(ctour.faces3.size() + 2 * ctour.faces4.size());
    std::copy(ctour.faces3.begin(), ctour.faces3.end(),
              std::back_inserter(triangles));
    
    for (auto &quad : ctour.faces4) {
        triangles.emplace_back(quad(0), quad(1), quad(2));
        triangles.emplace_back(quad(2), quad(3), quad(0));
    }
    
    return {ctour.points, std::move(triangles)};
}

TriangleMesh to_triangle_mesh(Contour3D &&ctour) {
    if (ctour.faces4.empty())
        return {std::move(ctour.points), std::move(ctour.faces3)};
    
    std::vector<Vec3i> triangles;
    
    triangles.reserve(ctour.faces3.size() + 2 * ctour.faces4.size());
    std::copy(ctour.faces3.begin(), ctour.faces3.end(),
              std::back_inserter(triangles));
    
    for (auto &quad : ctour.faces4) {
        triangles.emplace_back(quad(0), quad(1), quad(2));
        triangles.emplace_back(quad(2), quad(3), quad(0));
    }
    
    return {std::move(ctour.points), std::move(triangles)};
}

}} // namespace Slic3r::sla