PrusaSlicer-NonPlainar/xs/xsp/TriangleMesh.xsp
Vojtech Bubnik 8a2a9dba2f Eradicated admesh from TriangleMesh:
TriangleMesh newly only holds indexed_triangle_set and
TriangleMeshStats. TriangleMeshStats contains an excerpt of stl_stats.
TriangleMeshStats are updated when initializing with indexed_triangle_set.

Admesh triangle mesh fixing is newly only used when loading an STL.
AMF / 3MF / OBJ file formats are already indexed triangle sets, thus
they are no more converted to admesh stl_file format, nor fixed
through admesh repair machinery. When importing AMF / 3MF / OBJ files,
volume is calculated and if negative, all faces are flipped. Also
a bounding box and number of open edges is calculated.

Implemented its_number_of_patches(), its_num_open_edges()
Optimized its_split(), its_is_splittable() using a visitor pattern.

Reworked QHull integration into TriangleMesh:
    1) Face normals were not right.
    2) Indexed triangle set is newly emitted instead of duplicating
       vertices for each face.

Fixed cut_mesh(): Orient the triangulated faces correctly.
2021-09-20 17:12:22 +02:00

206 lines
6.6 KiB
Plaintext

%module{Slic3r::XS};
%{
#include <xsinit.h>
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/TriangleMeshSlicer.hpp"
%}
%name{Slic3r::TriangleMesh} class TriangleMesh {
TriangleMesh();
~TriangleMesh();
Clone<TriangleMesh> clone()
%code{% RETVAL = THIS; %};
void write_ascii(char* output_file);
void write_binary(char* output_file);
void scale(float factor);
void scale_xyz(Vec3d* versor)
%code{% THIS->scale(versor->cast<float>()); %};
void translate(float x, float y, float z);
void rotate_x(float angle);
void rotate_y(float angle);
void rotate_z(float angle);
void mirror_x();
void mirror_y();
void mirror_z();
void align_to_origin();
void rotate(double angle, Point* center);
void merge(TriangleMesh* mesh)
%code{% THIS->merge(*mesh); %};
Clone<Polygon> convex_hull();
Clone<BoundingBoxf3> bounding_box();
Clone<Vec3d> center()
%code{% RETVAL = THIS->bounding_box().center(); %};
int facets_count();
%{
void
TriangleMesh::ReadFromPerl(vertices, facets)
SV* vertices
SV* facets
CODE:
std::vector<Slic3r::Vec3f> out_vertices;
{
AV* vertices_av = (AV*)SvRV(vertices);
int number_of_vertices = av_len(vertices_av) + 1;
out_vertices.reserve(number_of_vertices);
for (int i = 0; i < number_of_vertices; ++ i) {
AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, i, 0));
out_vertices.push_back(Slic3r::Vec3f(SvNV(*av_fetch(vertex_av, 0, 0)), SvNV(*av_fetch(vertex_av, 1, 0)), SvNV(*av_fetch(vertex_av, 2, 0))));
}
}
std::vector<Slic3r::Vec3i> out_indices;
{
AV* facets_av = (AV*)SvRV(facets);
int number_of_facets = av_len(facets_av) + 1;
out_indices.reserve(number_of_facets);
for (int i = 0; i < number_of_facets; ++ i) {
AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0));
out_indices.push_back(Slic3r::Vec3i(SvIV(*av_fetch(facet_av, 0, 0)), SvIV(*av_fetch(facet_av, 1, 0)), SvIV(*av_fetch(facet_av, 2, 0))));
}
}
*THIS = TriangleMesh(std::move(out_vertices), std::move(out_indices));
SV*
TriangleMesh::stats()
CODE:
HV* hv = newHV();
(void)hv_stores( hv, "number_of_facets", newSViv(THIS->facets_count()) );
(void)hv_stores( hv, "number_of_parts", newSViv(THIS->stats().number_of_parts) );
(void)hv_stores( hv, "volume", newSVnv(THIS->stats().volume) );
(void)hv_stores( hv, "degenerate_facets", newSViv(THIS->stats().degenerate_facets) );
(void)hv_stores( hv, "edges_fixed", newSViv(THIS->stats().edges_fixed) );
(void)hv_stores( hv, "facets_removed", newSViv(THIS->stats().facets_removed) );
(void)hv_stores( hv, "facets_reversed", newSViv(THIS->stats().facets_reversed) );
(void)hv_stores( hv, "backwards_edges", newSViv(THIS->stats().backwards_edges) );
RETVAL = (SV*)newRV_noinc((SV*)hv);
OUTPUT:
RETVAL
SV*
TriangleMesh::vertices()
CODE:
// vertices
AV* vertices = newAV();
av_extend(vertices, THIS->its.vertices.size());
for (size_t i = 0; i < THIS->its.vertices.size(); i++) {
AV* vertex = newAV();
av_store(vertices, i, newRV_noinc((SV*)vertex));
av_extend(vertex, 2);
av_store(vertex, 0, newSVnv(THIS->its.vertices[i](0)));
av_store(vertex, 1, newSVnv(THIS->its.vertices[i](1)));
av_store(vertex, 2, newSVnv(THIS->its.vertices[i](2)));
}
RETVAL = newRV_noinc((SV*)vertices);
OUTPUT:
RETVAL
SV*
TriangleMesh::facets()
CODE:
// facets
AV* facets = newAV();
av_extend(facets, THIS->facets_count());
for (int i = 0; i < THIS->facets_count(); i++) {
AV* facet = newAV();
av_store(facets, i, newRV_noinc((SV*)facet));
av_extend(facet, 2);
av_store(facet, 0, newSVnv(THIS->its.indices[i][0]));
av_store(facet, 1, newSVnv(THIS->its.indices[i][1]));
av_store(facet, 2, newSVnv(THIS->its.indices[i][2]));
}
RETVAL = newRV_noinc((SV*)facets);
OUTPUT:
RETVAL
SV*
TriangleMesh::size()
CODE:
AV* size = newAV();
av_extend(size, 2);
av_store(size, 0, newSVnv(THIS->stats().size(0)));
av_store(size, 1, newSVnv(THIS->stats().size(1)));
av_store(size, 2, newSVnv(THIS->stats().size(2)));
RETVAL = newRV_noinc((SV*)size);
OUTPUT:
RETVAL
SV*
TriangleMesh::slice(z)
std::vector<double> z
CODE:
// convert doubles to floats
std::vector<float> z_f = cast<float>(z);
std::vector<ExPolygons> layers = slice_mesh_ex(THIS->its, z_f, 0.049f);
AV* layers_av = newAV();
size_t len = layers.size();
if (len > 0) av_extend(layers_av, len-1);
for (unsigned int i = 0; i < layers.size(); i++) {
AV* expolygons_av = newAV();
len = layers[i].size();
if (len > 0) av_extend(expolygons_av, len-1);
unsigned int j = 0;
for (ExPolygons::iterator it = layers[i].begin(); it != layers[i].end(); ++it) {
av_store(expolygons_av, j++, perl_to_SV_clone_ref(*it));
}
av_store(layers_av, i, newRV_noinc((SV*)expolygons_av));
}
RETVAL = (SV*)newRV_noinc((SV*)layers_av);
OUTPUT:
RETVAL
void
TriangleMesh::cut(z, upper_mesh, lower_mesh)
float z;
TriangleMesh* upper_mesh;
TriangleMesh* lower_mesh;
CODE:
indexed_triangle_set upper, lower;
cut_mesh(THIS->its, z, upper_mesh ? &upper : nullptr, lower_mesh ? &lower : nullptr);
if (upper_mesh)
*upper_mesh = TriangleMesh(upper);
if (lower_mesh)
*lower_mesh = TriangleMesh(lower);
std::vector<double>
TriangleMesh::bb3()
CODE:
RETVAL.push_back(THIS->stats().min(0));
RETVAL.push_back(THIS->stats().min(1));
RETVAL.push_back(THIS->stats().max(0));
RETVAL.push_back(THIS->stats().max(1));
RETVAL.push_back(THIS->stats().min(2));
RETVAL.push_back(THIS->stats().max(2));
OUTPUT:
RETVAL
Clone<TriangleMesh>
cube(double x, double y, double z)
CODE:
RETVAL = make_cube(x,y,z);
OUTPUT:
RETVAL
Clone<TriangleMesh>
cylinder(double r, double h)
CODE:
RETVAL = make_cylinder(r, h);
OUTPUT:
RETVAL
Clone<TriangleMesh>
sphere(double rho)
CODE:
RETVAL = make_sphere(rho);
OUTPUT:
RETVAL
%}
};