Browse Source

Add STL import (Closes #1)

Johannes Kreutz 2 years ago
parent
commit
76d016e4ce

+ 2 - 0
trackpoint-app/CMakeLists.txt

@@ -74,6 +74,7 @@ QT_ADD_EXECUTABLE(TrackpointApp
   src/TrackPointRenderer.cpp
   src/PointShape.cpp
   src/HudCallback.cpp
+  src/STLImport.cpp
 )
 
 if(NOT BUILD_STATIC_RELEASE)
@@ -103,6 +104,7 @@ ENDIF()
 
 INCLUDE_DIRECTORIES(
   ${${JSON_PREFIX}_SOURCE_DIR}/include
+  ${CMAKE_CURRENT_LIST_DIR}/thirdparty/stl_reader/install
   include
   gui
 )

+ 1 - 0
trackpoint-app/build-dependencies-first.sh

@@ -14,6 +14,7 @@ do
 done
 
 thirdparty/qt.sh $OPTIONS
+thirdparty/stl_reader.sh $OPTIONS
 
 if [ $OPTIONS == "release" ]; then
   thirdparty/openscenegraph.sh release

+ 13 - 0
trackpoint-app/include/STLImport.hpp

@@ -0,0 +1,13 @@
+#pragma once
+
+// Include dependencies
+#include <string>
+#include "lib3mf_implicit.hpp"
+
+class STLImport {
+public:
+  static void readSTL(std::string file, std::vector<Lib3MF::sPosition>* verticesBuffer, std::vector<Lib3MF::sTriangle>* triangleBuffer);
+
+private:
+  static int findVertex(float x, float y, float z, std::vector<Lib3MF::sPosition>* verticesBuffer);
+};

+ 10 - 1
trackpoint-app/src/ProjectStore.cpp

@@ -6,6 +6,7 @@
 #include "StringBasics.hpp"
 #include "TrackPointRenderer.hpp"
 #include "PlatformSupport.hpp"
+#include "STLImport.hpp"
 
 // Include dependencies
 #include <typeinfo>
@@ -25,7 +26,15 @@ ProjectStore::~ProjectStore() {
 
 void ProjectStore::loadMesh(std::string meshFile) {
   if (StringBasics::endsWithCaseInsensitive(meshFile, ".STL")) {
-    printf("Currently unsupported.\n");
+    _projectLoaded = true;
+    // Read STL file
+    std::vector<Lib3MF::sPosition> verticesBuffer;
+    std::vector<Lib3MF::sTriangle> triangleBuffer;
+    STLImport::readSTL(meshFile, &verticesBuffer, &triangleBuffer);
+    Lib3MF::PMeshObject baseMesh = _project->AddMeshObject();
+    baseMesh->SetGeometry(verticesBuffer, triangleBuffer);
+    render3MFMesh();
+    MainWindow::getInstance()->renderView(Edit);
   } else if (StringBasics::endsWithCaseInsensitive(meshFile, ".3MF")) {
     _projectLoaded = true;
     // Read 3MF file

+ 38 - 0
trackpoint-app/src/STLImport.cpp

@@ -0,0 +1,38 @@
+// Include own headers
+#include "STLImport.hpp"
+
+// Include dependencies
+#include "stl_reader.h"
+
+void STLImport::readSTL(std::string file, std::vector<Lib3MF::sPosition>* verticesBuffer, std::vector<Lib3MF::sTriangle>* triangleBuffer) {
+  stl_reader::StlMesh<float, unsigned int> mesh(file);
+  unsigned int vertexId = 0;
+  for (size_t i = 0; i < mesh.num_tris(); i++) {
+    Lib3MF_uint32 indices[3] = {0, 0, 0};
+    for (size_t corner = 0; corner < 3; corner++) {
+      const float* coordinates = mesh.tri_corner_coords(i, corner);
+      int searchIndex = STLImport::findVertex(coordinates[0], coordinates[1], coordinates[2], verticesBuffer);
+      if (searchIndex >= 0) {
+        indices[corner] = searchIndex;
+      } else {
+        Lib3MF::sPosition vertex = {coordinates[0], coordinates[1], coordinates[2]};
+        verticesBuffer->push_back(vertex);
+        indices[corner] = vertexId;
+        vertexId++;
+      }
+    }
+    Lib3MF::sTriangle triangle = {indices[0], indices[1], indices[2]};
+    triangleBuffer->push_back(triangle);
+  }
+}
+
+int STLImport::findVertex(float x, float y, float z, std::vector<Lib3MF::sPosition>* verticesBuffer) {
+  int index = 0;
+  for (Lib3MF::sPosition vertex: *verticesBuffer) {
+    if (x == vertex.m_Coordinates[0] && y == vertex.m_Coordinates[1] && z == vertex.m_Coordinates[2]) {
+      return index;
+    }
+    index++;
+  }
+  return -1;
+}

+ 1 - 0
trackpoint-app/thirdparty/.gitignore

@@ -2,3 +2,4 @@ qt/
 lib3mf/
 openscenegraph/
 json/
+stl_reader/

+ 28 - 0
trackpoint-app/thirdparty/stl_reader.sh

@@ -0,0 +1,28 @@
+#!/bin/bash
+# Build stl_reader
+
+set -e
+
+BASEDIR=$PWD/thirdparty/stl_reader
+DEPLOYDIR=$BASEDIR/install
+
+STL_READER_REPO="https://github.com/sreiter/stl_reader"
+
+mkdir -p "$BASEDIR"
+
+if [ -d "$DEPLOYDIR" ]; then
+  rm -rf "$DEPLOYDIR"
+fi
+mkdir -p "$DEPLOYDIR"
+
+pushd "$BASEDIR"
+
+if [ -d "stl_reader" ]; then
+  rm -rf "stl_reader"
+fi
+
+git clone --depth 1 $STL_READER_REPO
+
+cp stl_reader/stl_reader.h "$DEPLOYDIR"
+
+popd