STLImport.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Include own headers
  2. #include "STLImport.hpp"
  3. // Include dependencies
  4. #include "stl_reader.h"
  5. void STLImport::readSTL(std::string file, std::vector<Lib3MF::sPosition>* verticesBuffer, std::vector<Lib3MF::sTriangle>* triangleBuffer) {
  6. stl_reader::StlMesh<float, unsigned int> mesh(file);
  7. unsigned int vertexId = 0;
  8. for (size_t i = 0; i < mesh.num_tris(); i++) {
  9. Lib3MF_uint32 indices[3] = {0, 0, 0};
  10. for (size_t corner = 0; corner < 3; corner++) {
  11. const float* coordinates = mesh.tri_corner_coords(i, corner);
  12. int searchIndex = STLImport::findVertex(coordinates[0], coordinates[1], coordinates[2], verticesBuffer);
  13. if (searchIndex >= 0) {
  14. indices[corner] = searchIndex;
  15. } else {
  16. Lib3MF::sPosition vertex = {coordinates[0], coordinates[1], coordinates[2]};
  17. verticesBuffer->push_back(vertex);
  18. indices[corner] = vertexId;
  19. vertexId++;
  20. }
  21. }
  22. Lib3MF::sTriangle triangle = {indices[0], indices[1], indices[2]};
  23. triangleBuffer->push_back(triangle);
  24. }
  25. }
  26. int STLImport::findVertex(float x, float y, float z, std::vector<Lib3MF::sPosition>* verticesBuffer) {
  27. int index = 0;
  28. for (Lib3MF::sPosition vertex: *verticesBuffer) {
  29. if (x == vertex.m_Coordinates[0] && y == vertex.m_Coordinates[1] && z == vertex.m_Coordinates[2]) {
  30. return index;
  31. }
  32. index++;
  33. }
  34. return -1;
  35. }