PrusaSlicer-NonPlainar/xs/xsp/TriangleMesh.xsp

244 lines
7.9 KiB
Plaintext

%module{Slic3r::XS};
%{
#include <xsinit.h>
#include "libslic3r/TriangleMesh.hpp"
%}
%name{Slic3r::TriangleMesh} class TriangleMesh {
TriangleMesh();
~TriangleMesh();
Clone<TriangleMesh> clone()
%code{% RETVAL = THIS; %};
void ReadSTLFile(char* input_file);
void write_ascii(char* output_file);
void write_binary(char* output_file);
void repair();
void WriteOBJFile(char* output_file);
void scale(float factor);
void scale_xyz(Pointf3* versor)
%code{% THIS->scale(*versor); %};
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);
TriangleMeshPtrs split();
void merge(TriangleMesh* mesh)
%code{% THIS->merge(*mesh); %};
ExPolygons horizontal_projection();
Clone<Polygon> convex_hull();
Clone<BoundingBoxf3> bounding_box();
Clone<Pointf3> center()
%code{% RETVAL = THIS->bounding_box().center(); %};
int facets_count();
void reset_repair_stats();
Clone<TriangleMesh> cube(double x, double y, double z)
%code{% RETVAL = make_cube(x, y, z); %};
%{
void
TriangleMesh::ReadFromPerl(vertices, facets)
SV* vertices
SV* facets
CODE:
stl_file &stl = THIS->stl;
stl.error = 0;
stl.stats.type = inmemory;
// count facets and allocate memory
AV* facets_av = (AV*)SvRV(facets);
stl.stats.number_of_facets = av_len(facets_av)+1;
stl.stats.original_num_facets = stl.stats.number_of_facets;
stl_allocate(&stl);
// read geometry
AV* vertices_av = (AV*)SvRV(vertices);
for (int i = 0; i < stl.stats.number_of_facets; i++) {
AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0));
stl_facet facet;
facet.normal.x = 0;
facet.normal.y = 0;
facet.normal.z = 0;
for (unsigned int v = 0; v <= 2; v++) {
AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, SvIV(*av_fetch(facet_av, v, 0)), 0));
facet.vertex[v].x = SvNV(*av_fetch(vertex_av, 0, 0));
facet.vertex[v].y = SvNV(*av_fetch(vertex_av, 1, 0));
facet.vertex[v].z = SvNV(*av_fetch(vertex_av, 2, 0));
}
facet.extra[0] = 0;
facet.extra[1] = 0;
stl.facet_start[i] = facet;
}
stl_get_size(&stl);
SV*
TriangleMesh::stats()
CODE:
HV* hv = newHV();
(void)hv_stores( hv, "number_of_facets", newSViv(THIS->stl.stats.number_of_facets) );
(void)hv_stores( hv, "number_of_parts", newSViv(THIS->stl.stats.number_of_parts) );
(void)hv_stores( hv, "volume", newSVnv(THIS->stl.stats.volume) );
(void)hv_stores( hv, "degenerate_facets", newSViv(THIS->stl.stats.degenerate_facets) );
(void)hv_stores( hv, "edges_fixed", newSViv(THIS->stl.stats.edges_fixed) );
(void)hv_stores( hv, "facets_removed", newSViv(THIS->stl.stats.facets_removed) );
(void)hv_stores( hv, "facets_added", newSViv(THIS->stl.stats.facets_added) );
(void)hv_stores( hv, "facets_reversed", newSViv(THIS->stl.stats.facets_reversed) );
(void)hv_stores( hv, "backwards_edges", newSViv(THIS->stl.stats.backwards_edges) );
(void)hv_stores( hv, "normals_fixed", newSViv(THIS->stl.stats.normals_fixed) );
RETVAL = (SV*)newRV_noinc((SV*)hv);
OUTPUT:
RETVAL
SV*
TriangleMesh::vertices()
CODE:
if (!THIS->repaired) CONFESS("vertices() requires repair()");
if (THIS->stl.v_shared == NULL)
stl_generate_shared_vertices(&(THIS->stl));
// vertices
AV* vertices = newAV();
av_extend(vertices, THIS->stl.stats.shared_vertices);
for (int i = 0; i < THIS->stl.stats.shared_vertices; i++) {
AV* vertex = newAV();
av_store(vertices, i, newRV_noinc((SV*)vertex));
av_extend(vertex, 2);
av_store(vertex, 0, newSVnv(THIS->stl.v_shared[i].x));
av_store(vertex, 1, newSVnv(THIS->stl.v_shared[i].y));
av_store(vertex, 2, newSVnv(THIS->stl.v_shared[i].z));
}
RETVAL = newRV_noinc((SV*)vertices);
OUTPUT:
RETVAL
SV*
TriangleMesh::facets()
CODE:
if (!THIS->repaired) CONFESS("facets() requires repair()");
if (THIS->stl.v_shared == NULL)
stl_generate_shared_vertices(&(THIS->stl));
// facets
AV* facets = newAV();
av_extend(facets, THIS->stl.stats.number_of_facets);
for (int i = 0; i < THIS->stl.stats.number_of_facets; i++) {
AV* facet = newAV();
av_store(facets, i, newRV_noinc((SV*)facet));
av_extend(facet, 2);
av_store(facet, 0, newSVnv(THIS->stl.v_indices[i].vertex[0]));
av_store(facet, 1, newSVnv(THIS->stl.v_indices[i].vertex[1]));
av_store(facet, 2, newSVnv(THIS->stl.v_indices[i].vertex[2]));
}
RETVAL = newRV_noinc((SV*)facets);
OUTPUT:
RETVAL
SV*
TriangleMesh::normals()
CODE:
if (!THIS->repaired) CONFESS("normals() requires repair()");
// normals
AV* normals = newAV();
av_extend(normals, THIS->stl.stats.number_of_facets);
for (int i = 0; i < THIS->stl.stats.number_of_facets; i++) {
AV* facet = newAV();
av_store(normals, i, newRV_noinc((SV*)facet));
av_extend(facet, 2);
av_store(facet, 0, newSVnv(THIS->stl.facet_start[i].normal.x));
av_store(facet, 1, newSVnv(THIS->stl.facet_start[i].normal.y));
av_store(facet, 2, newSVnv(THIS->stl.facet_start[i].normal.z));
}
RETVAL = newRV_noinc((SV*)normals);
OUTPUT:
RETVAL
SV*
TriangleMesh::size()
CODE:
AV* size = newAV();
av_extend(size, 2);
av_store(size, 0, newSVnv(THIS->stl.stats.size.x));
av_store(size, 1, newSVnv(THIS->stl.stats.size.y));
av_store(size, 2, newSVnv(THIS->stl.stats.size.z));
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(z.begin(), z.end());
std::vector<ExPolygons> layers;
TriangleMeshSlicer mslicer(THIS);
mslicer.slice(z_f, &layers);
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, lower)
float z;
TriangleMesh* upper;
TriangleMesh* lower;
CODE:
TriangleMeshSlicer mslicer(THIS);
mslicer.cut(z, upper, lower);
std::vector<double>
TriangleMesh::bb3()
CODE:
RETVAL.push_back(THIS->stl.stats.min.x);
RETVAL.push_back(THIS->stl.stats.min.y);
RETVAL.push_back(THIS->stl.stats.max.x);
RETVAL.push_back(THIS->stl.stats.max.y);
RETVAL.push_back(THIS->stl.stats.min.z);
RETVAL.push_back(THIS->stl.stats.max.z);
OUTPUT:
RETVAL
%}
};
%package{Slic3r::TriangleMesh};
%{
PROTOTYPES: DISABLE
std::string
hello_world()
CODE:
RETVAL = "Hello world!";
OUTPUT:
RETVAL
%}