TriangleMesh.xsp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. %module{Slic3r::XS};
  2. %{
  3. #include <xsinit.h>
  4. #include "libslic3r/TriangleMesh.hpp"
  5. #include "libslic3r/TriangleMeshSlicer.hpp"
  6. %}
  7. %name{Slic3r::TriangleMesh} class TriangleMesh {
  8. TriangleMesh();
  9. ~TriangleMesh();
  10. Clone<TriangleMesh> clone()
  11. %code{% RETVAL = THIS; %};
  12. void write_ascii(char* output_file);
  13. void write_binary(char* output_file);
  14. void scale(float factor);
  15. void scale_xyz(Vec3d* versor)
  16. %code{% THIS->scale(versor->cast<float>()); %};
  17. void translate(float x, float y, float z);
  18. void rotate_x(float angle);
  19. void rotate_y(float angle);
  20. void rotate_z(float angle);
  21. void mirror_x();
  22. void mirror_y();
  23. void mirror_z();
  24. void align_to_origin();
  25. void rotate(double angle, Point* center);
  26. void merge(TriangleMesh* mesh)
  27. %code{% THIS->merge(*mesh); %};
  28. Clone<Polygon> convex_hull();
  29. Clone<BoundingBoxf3> bounding_box();
  30. Clone<Vec3d> center()
  31. %code{% RETVAL = THIS->bounding_box().center(); %};
  32. int facets_count();
  33. %{
  34. void
  35. TriangleMesh::ReadFromPerl(vertices, facets)
  36. SV* vertices
  37. SV* facets
  38. CODE:
  39. std::vector<Slic3r::Vec3f> out_vertices;
  40. {
  41. AV* vertices_av = (AV*)SvRV(vertices);
  42. int number_of_vertices = av_len(vertices_av) + 1;
  43. out_vertices.reserve(number_of_vertices);
  44. for (int i = 0; i < number_of_vertices; ++ i) {
  45. AV* vertex_av = (AV*)SvRV(*av_fetch(vertices_av, i, 0));
  46. 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))));
  47. }
  48. }
  49. std::vector<Slic3r::Vec3i> out_indices;
  50. {
  51. AV* facets_av = (AV*)SvRV(facets);
  52. int number_of_facets = av_len(facets_av) + 1;
  53. out_indices.reserve(number_of_facets);
  54. for (int i = 0; i < number_of_facets; ++ i) {
  55. AV* facet_av = (AV*)SvRV(*av_fetch(facets_av, i, 0));
  56. 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))));
  57. }
  58. }
  59. *THIS = TriangleMesh(std::move(out_vertices), std::move(out_indices));
  60. SV*
  61. TriangleMesh::stats()
  62. CODE:
  63. HV* hv = newHV();
  64. (void)hv_stores( hv, "number_of_facets", newSViv(THIS->facets_count()) );
  65. (void)hv_stores( hv, "number_of_parts", newSViv(THIS->stats().number_of_parts) );
  66. (void)hv_stores( hv, "volume", newSVnv(THIS->stats().volume) );
  67. (void)hv_stores( hv, "degenerate_facets", newSViv(THIS->stats().repaired_errors.degenerate_facets) );
  68. (void)hv_stores( hv, "edges_fixed", newSViv(THIS->stats().repaired_errors.edges_fixed) );
  69. (void)hv_stores( hv, "facets_removed", newSViv(THIS->stats().repaired_errors.facets_removed) );
  70. (void)hv_stores( hv, "facets_reversed", newSViv(THIS->stats().repaired_errors.facets_reversed) );
  71. (void)hv_stores( hv, "backwards_edges", newSViv(THIS->stats().repaired_errors.backwards_edges) );
  72. RETVAL = (SV*)newRV_noinc((SV*)hv);
  73. OUTPUT:
  74. RETVAL
  75. SV*
  76. TriangleMesh::vertices()
  77. CODE:
  78. // vertices
  79. AV* vertices = newAV();
  80. av_extend(vertices, THIS->its.vertices.size());
  81. for (size_t i = 0; i < THIS->its.vertices.size(); i++) {
  82. AV* vertex = newAV();
  83. av_store(vertices, i, newRV_noinc((SV*)vertex));
  84. av_extend(vertex, 2);
  85. av_store(vertex, 0, newSVnv(THIS->its.vertices[i](0)));
  86. av_store(vertex, 1, newSVnv(THIS->its.vertices[i](1)));
  87. av_store(vertex, 2, newSVnv(THIS->its.vertices[i](2)));
  88. }
  89. RETVAL = newRV_noinc((SV*)vertices);
  90. OUTPUT:
  91. RETVAL
  92. SV*
  93. TriangleMesh::facets()
  94. CODE:
  95. // facets
  96. AV* facets = newAV();
  97. av_extend(facets, THIS->facets_count());
  98. for (int i = 0; i < THIS->facets_count(); i++) {
  99. AV* facet = newAV();
  100. av_store(facets, i, newRV_noinc((SV*)facet));
  101. av_extend(facet, 2);
  102. av_store(facet, 0, newSVnv(THIS->its.indices[i][0]));
  103. av_store(facet, 1, newSVnv(THIS->its.indices[i][1]));
  104. av_store(facet, 2, newSVnv(THIS->its.indices[i][2]));
  105. }
  106. RETVAL = newRV_noinc((SV*)facets);
  107. OUTPUT:
  108. RETVAL
  109. SV*
  110. TriangleMesh::size()
  111. CODE:
  112. AV* size = newAV();
  113. av_extend(size, 2);
  114. av_store(size, 0, newSVnv(THIS->stats().size(0)));
  115. av_store(size, 1, newSVnv(THIS->stats().size(1)));
  116. av_store(size, 2, newSVnv(THIS->stats().size(2)));
  117. RETVAL = newRV_noinc((SV*)size);
  118. OUTPUT:
  119. RETVAL
  120. SV*
  121. TriangleMesh::slice(z)
  122. std::vector<double> z
  123. CODE:
  124. // convert doubles to floats
  125. std::vector<float> z_f = cast<float>(z);
  126. std::vector<ExPolygons> layers = slice_mesh_ex(THIS->its, z_f, 0.049f);
  127. AV* layers_av = newAV();
  128. size_t len = layers.size();
  129. if (len > 0) av_extend(layers_av, len-1);
  130. for (unsigned int i = 0; i < layers.size(); i++) {
  131. AV* expolygons_av = newAV();
  132. len = layers[i].size();
  133. if (len > 0) av_extend(expolygons_av, len-1);
  134. unsigned int j = 0;
  135. for (ExPolygons::iterator it = layers[i].begin(); it != layers[i].end(); ++it) {
  136. av_store(expolygons_av, j++, perl_to_SV_clone_ref(*it));
  137. }
  138. av_store(layers_av, i, newRV_noinc((SV*)expolygons_av));
  139. }
  140. RETVAL = (SV*)newRV_noinc((SV*)layers_av);
  141. OUTPUT:
  142. RETVAL
  143. void
  144. TriangleMesh::cut(z, upper_mesh, lower_mesh)
  145. float z;
  146. TriangleMesh* upper_mesh;
  147. TriangleMesh* lower_mesh;
  148. CODE:
  149. indexed_triangle_set upper, lower;
  150. cut_mesh(THIS->its, z, upper_mesh ? &upper : nullptr, lower_mesh ? &lower : nullptr);
  151. if (upper_mesh)
  152. *upper_mesh = TriangleMesh(upper);
  153. if (lower_mesh)
  154. *lower_mesh = TriangleMesh(lower);
  155. std::vector<double>
  156. TriangleMesh::bb3()
  157. CODE:
  158. RETVAL.push_back(THIS->stats().min(0));
  159. RETVAL.push_back(THIS->stats().min(1));
  160. RETVAL.push_back(THIS->stats().max(0));
  161. RETVAL.push_back(THIS->stats().max(1));
  162. RETVAL.push_back(THIS->stats().min(2));
  163. RETVAL.push_back(THIS->stats().max(2));
  164. OUTPUT:
  165. RETVAL
  166. Clone<TriangleMesh>
  167. cube(double x, double y, double z)
  168. CODE:
  169. RETVAL = make_cube(x,y,z);
  170. OUTPUT:
  171. RETVAL
  172. Clone<TriangleMesh>
  173. cylinder(double r, double h)
  174. CODE:
  175. RETVAL = make_cylinder(r, h);
  176. OUTPUT:
  177. RETVAL
  178. Clone<TriangleMesh>
  179. sphere(double rho)
  180. CODE:
  181. RETVAL = make_sphere(rho);
  182. OUTPUT:
  183. RETVAL
  184. %}
  185. };