TriangleMesh.xsp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. %module{Slic3r::XS};
  2. %{
  3. #include <xsinit.h>
  4. #include "libslic3r/TriangleMesh.hpp"
  5. %}
  6. %name{Slic3r::TriangleMesh} class TriangleMesh {
  7. TriangleMesh();
  8. ~TriangleMesh();
  9. Clone<TriangleMesh> clone()
  10. %code{% RETVAL = THIS; %};
  11. void ReadSTLFile(char* input_file);
  12. void write_ascii(char* output_file);
  13. void write_binary(char* output_file);
  14. void repair();
  15. void WriteOBJFile(char* output_file);
  16. void scale(float factor);
  17. void scale_xyz(Pointf3* versor)
  18. %code{% THIS->scale(*versor); %};
  19. void translate(float x, float y, float z);
  20. void rotate_x(float angle);
  21. void rotate_y(float angle);
  22. void rotate_z(float angle);
  23. void mirror_x();
  24. void mirror_y();
  25. void mirror_z();
  26. void align_to_origin();
  27. void rotate(double angle, Point* center);
  28. TriangleMeshPtrs split();
  29. void merge(TriangleMesh* mesh)
  30. %code{% THIS->merge(*mesh); %};
  31. ExPolygons horizontal_projection();
  32. Clone<Polygon> convex_hull();
  33. Clone<BoundingBoxf3> bounding_box();
  34. Clone<Pointf3> center()
  35. %code{% RETVAL = THIS->bounding_box().center(); %};
  36. int facets_count();
  37. void reset_repair_stats();
  38. %{
  39. void
  40. TriangleMesh::ReadFromPerl(vertices, facets)
  41. SV* vertices
  42. SV* facets
  43. CODE:
  44. stl_file &stl = THIS->stl;
  45. stl.error = 0;
  46. stl.stats.type = inmemory;
  47. // count facets and allocate memory
  48. AV* facets_av = (AV*)SvRV(facets);
  49. stl.stats.number_of_facets = av_len(facets_av)+1;
  50. stl.stats.original_num_facets = stl.stats.number_of_facets;
  51. stl_allocate(&stl);
  52. // read geometry
  53. AV* vertices_av = (AV*)SvRV(vertices);
  54. for (int i = 0; i < stl.stats.number_of_facets; i++) {
  55. AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0));
  56. stl_facet facet;
  57. facet.normal.x = 0;
  58. facet.normal.y = 0;
  59. facet.normal.z = 0;
  60. for (unsigned int v = 0; v <= 2; v++) {
  61. AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, SvIV(*av_fetch(facet_av, v, 0)), 0));
  62. facet.vertex[v].x = SvNV(*av_fetch(vertex_av, 0, 0));
  63. facet.vertex[v].y = SvNV(*av_fetch(vertex_av, 1, 0));
  64. facet.vertex[v].z = SvNV(*av_fetch(vertex_av, 2, 0));
  65. }
  66. facet.extra[0] = 0;
  67. facet.extra[1] = 0;
  68. stl.facet_start[i] = facet;
  69. }
  70. stl_get_size(&stl);
  71. SV*
  72. TriangleMesh::stats()
  73. CODE:
  74. HV* hv = newHV();
  75. (void)hv_stores( hv, "number_of_facets", newSViv(THIS->stl.stats.number_of_facets) );
  76. (void)hv_stores( hv, "number_of_parts", newSViv(THIS->stl.stats.number_of_parts) );
  77. (void)hv_stores( hv, "volume", newSVnv(THIS->stl.stats.volume) );
  78. (void)hv_stores( hv, "degenerate_facets", newSViv(THIS->stl.stats.degenerate_facets) );
  79. (void)hv_stores( hv, "edges_fixed", newSViv(THIS->stl.stats.edges_fixed) );
  80. (void)hv_stores( hv, "facets_removed", newSViv(THIS->stl.stats.facets_removed) );
  81. (void)hv_stores( hv, "facets_added", newSViv(THIS->stl.stats.facets_added) );
  82. (void)hv_stores( hv, "facets_reversed", newSViv(THIS->stl.stats.facets_reversed) );
  83. (void)hv_stores( hv, "backwards_edges", newSViv(THIS->stl.stats.backwards_edges) );
  84. (void)hv_stores( hv, "normals_fixed", newSViv(THIS->stl.stats.normals_fixed) );
  85. RETVAL = (SV*)newRV_noinc((SV*)hv);
  86. OUTPUT:
  87. RETVAL
  88. SV*
  89. TriangleMesh::vertices()
  90. CODE:
  91. if (!THIS->repaired) CONFESS("vertices() requires repair()");
  92. if (THIS->stl.v_shared == NULL)
  93. stl_generate_shared_vertices(&(THIS->stl));
  94. // vertices
  95. AV* vertices = newAV();
  96. av_extend(vertices, THIS->stl.stats.shared_vertices);
  97. for (int i = 0; i < THIS->stl.stats.shared_vertices; i++) {
  98. AV* vertex = newAV();
  99. av_store(vertices, i, newRV_noinc((SV*)vertex));
  100. av_extend(vertex, 2);
  101. av_store(vertex, 0, newSVnv(THIS->stl.v_shared[i].x));
  102. av_store(vertex, 1, newSVnv(THIS->stl.v_shared[i].y));
  103. av_store(vertex, 2, newSVnv(THIS->stl.v_shared[i].z));
  104. }
  105. RETVAL = newRV_noinc((SV*)vertices);
  106. OUTPUT:
  107. RETVAL
  108. SV*
  109. TriangleMesh::facets()
  110. CODE:
  111. if (!THIS->repaired) CONFESS("facets() requires repair()");
  112. if (THIS->stl.v_shared == NULL)
  113. stl_generate_shared_vertices(&(THIS->stl));
  114. // facets
  115. AV* facets = newAV();
  116. av_extend(facets, THIS->stl.stats.number_of_facets);
  117. for (int i = 0; i < THIS->stl.stats.number_of_facets; i++) {
  118. AV* facet = newAV();
  119. av_store(facets, i, newRV_noinc((SV*)facet));
  120. av_extend(facet, 2);
  121. av_store(facet, 0, newSVnv(THIS->stl.v_indices[i].vertex[0]));
  122. av_store(facet, 1, newSVnv(THIS->stl.v_indices[i].vertex[1]));
  123. av_store(facet, 2, newSVnv(THIS->stl.v_indices[i].vertex[2]));
  124. }
  125. RETVAL = newRV_noinc((SV*)facets);
  126. OUTPUT:
  127. RETVAL
  128. SV*
  129. TriangleMesh::normals()
  130. CODE:
  131. if (!THIS->repaired) CONFESS("normals() requires repair()");
  132. // normals
  133. AV* normals = newAV();
  134. av_extend(normals, THIS->stl.stats.number_of_facets);
  135. for (int i = 0; i < THIS->stl.stats.number_of_facets; i++) {
  136. AV* facet = newAV();
  137. av_store(normals, i, newRV_noinc((SV*)facet));
  138. av_extend(facet, 2);
  139. av_store(facet, 0, newSVnv(THIS->stl.facet_start[i].normal.x));
  140. av_store(facet, 1, newSVnv(THIS->stl.facet_start[i].normal.y));
  141. av_store(facet, 2, newSVnv(THIS->stl.facet_start[i].normal.z));
  142. }
  143. RETVAL = newRV_noinc((SV*)normals);
  144. OUTPUT:
  145. RETVAL
  146. SV*
  147. TriangleMesh::size()
  148. CODE:
  149. AV* size = newAV();
  150. av_extend(size, 2);
  151. av_store(size, 0, newSVnv(THIS->stl.stats.size.x));
  152. av_store(size, 1, newSVnv(THIS->stl.stats.size.y));
  153. av_store(size, 2, newSVnv(THIS->stl.stats.size.z));
  154. RETVAL = newRV_noinc((SV*)size);
  155. OUTPUT:
  156. RETVAL
  157. SV*
  158. TriangleMesh::slice(z)
  159. std::vector<double> z
  160. CODE:
  161. // convert doubles to floats
  162. std::vector<float> z_f(z.begin(), z.end());
  163. std::vector<ExPolygons> layers;
  164. TriangleMeshSlicer mslicer(THIS);
  165. mslicer.slice(z_f, &layers);
  166. AV* layers_av = newAV();
  167. size_t len = layers.size();
  168. if (len > 0) av_extend(layers_av, len-1);
  169. for (unsigned int i = 0; i < layers.size(); i++) {
  170. AV* expolygons_av = newAV();
  171. len = layers[i].size();
  172. if (len > 0) av_extend(expolygons_av, len-1);
  173. unsigned int j = 0;
  174. for (ExPolygons::iterator it = layers[i].begin(); it != layers[i].end(); ++it) {
  175. av_store(expolygons_av, j++, perl_to_SV_clone_ref(*it));
  176. }
  177. av_store(layers_av, i, newRV_noinc((SV*)expolygons_av));
  178. }
  179. RETVAL = (SV*)newRV_noinc((SV*)layers_av);
  180. OUTPUT:
  181. RETVAL
  182. void
  183. TriangleMesh::cut(z, upper, lower)
  184. float z;
  185. TriangleMesh* upper;
  186. TriangleMesh* lower;
  187. CODE:
  188. TriangleMeshSlicer mslicer(THIS);
  189. mslicer.cut(z, upper, lower);
  190. std::vector<double>
  191. TriangleMesh::bb3()
  192. CODE:
  193. RETVAL.push_back(THIS->stl.stats.min.x);
  194. RETVAL.push_back(THIS->stl.stats.min.y);
  195. RETVAL.push_back(THIS->stl.stats.max.x);
  196. RETVAL.push_back(THIS->stl.stats.max.y);
  197. RETVAL.push_back(THIS->stl.stats.min.z);
  198. RETVAL.push_back(THIS->stl.stats.max.z);
  199. OUTPUT:
  200. RETVAL
  201. Clone<TriangleMesh>
  202. cube(double x, double y, double z)
  203. CODE:
  204. RETVAL = make_cube(x,y,z);
  205. OUTPUT:
  206. RETVAL
  207. Clone<TriangleMesh>
  208. cylinder(double r, double h)
  209. CODE:
  210. RETVAL = make_cylinder(r, h);
  211. OUTPUT:
  212. RETVAL
  213. Clone<TriangleMesh>
  214. sphere(double rho)
  215. CODE:
  216. RETVAL = make_sphere(rho);
  217. OUTPUT:
  218. RETVAL
  219. %}
  220. };
  221. %package{Slic3r::TriangleMesh};
  222. %{
  223. PROTOTYPES: DISABLE
  224. std::string
  225. hello_world()
  226. CODE:
  227. RETVAL = "Hello world!";
  228. OUTPUT:
  229. RETVAL
  230. %}