Browse Source

Upload current status

Johannes Kreutz 2 years ago
parent
commit
a19063cd3d

BIN
testdata/cube_gears.3mf


BIN
testdata/cylinder.3mf


BIN
testdata/zEdgeLift.3mf


+ 2 - 2
trackpoint-app/CMakeLists.txt

@@ -6,10 +6,10 @@ INCLUDE(FetchContent)
 
 # Build TrackpointApp
 # Set C++ mode
-SET(CMAKE_CXX_STANDARD 20)
+SET(CMAKE_CXX_STANDARD 11)
 SET(CMAKE_CXX_STANDARD_REQUIRED True)
 
-SET(CMAKE_CXX_FLAGS_DEBUG "-O0")
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wunreachable-code")
 SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
 
 SET(CMAKE_AUTOUIC ON)

+ 3 - 2
trackpoint-app/include/MainWindow.hpp

@@ -20,17 +20,18 @@ class MainWindow: public QMainWindow {
   Q_OBJECT
 
 public:
+  static MainWindow* getInstance();
   MainWindow(QWidget* parent = nullptr);
   ~MainWindow();
   void renderView(GuiView view);
-  ProjectStore* getProjectStorePointer();
   OSGWidget* getOsgWidget();
+  ProjectStore* getStore();
 
 private:
   void openFile();
   Ui::MainWindow* ui;
-  ProjectStore* projectStore;
   OSGWidget* osgWidget;
   NoMeshWidget* noMeshWidget;
   EditWidget* editWidget;
+  ProjectStore projectStore;
 };

+ 13 - 11
trackpoint-app/include/OSGWidget.hpp

@@ -31,25 +31,26 @@ class OSGWidget : public QOpenGLWidget {
 public:
   OSGWidget(QWidget* parent = nullptr);
   virtual ~OSGWidget();
+  void renderBaseMesh(const std::vector<Lib3MF::sPosition> &verticesBuffer, const std::vector<Lib3MF::sTriangle> &triangleBuffer);
 
 protected:
-  virtual void paintEvent( QPaintEvent* paintEvent );
+  virtual void paintEvent(QPaintEvent* paintEvent);
   virtual void paintGL();
-  virtual void resizeGL( int width, int height );
+  virtual void resizeGL(int width, int height);
 
-  virtual void keyPressEvent( QKeyEvent* event );
-  virtual void keyReleaseEvent( QKeyEvent* event );
+  virtual void keyPressEvent(QKeyEvent* event);
+  virtual void keyReleaseEvent(QKeyEvent* event);
 
-  virtual void mouseMoveEvent( QMouseEvent* event );
-  virtual void mousePressEvent( QMouseEvent* event );
-  virtual void mouseReleaseEvent( QMouseEvent* event );
-  virtual void wheelEvent( QWheelEvent* event );
+  virtual void mouseMoveEvent(QMouseEvent* event);
+  virtual void mousePressEvent(QMouseEvent* event);
+  virtual void mouseReleaseEvent(QMouseEvent* event);
+  virtual void wheelEvent(QWheelEvent* event);
 
-  virtual bool event( QEvent* event );
+  virtual bool event(QEvent* event);
 
 private:
   virtual void onHome();
-  virtual void onResize( int width, int height );
+  virtual void onResize(int width, int height);
 
   osgGA::EventQueue* getEventQueue() const;
 
@@ -57,7 +58,8 @@ private:
   osg::ref_ptr<osgWidget::Viewer> _viewer;
   osgViewer::View* _view;
   osg::ref_ptr<osg::Group> _root;
-  osg::ref_ptr<osg::Node> _axesNode;
+  osg::ref_ptr<osg::Geode> _coordinateAxes;
+  osg::ref_ptr<osg::Geode> _axesNode;
 
   QPoint selectionStart_;
   QPoint selectionEnd_;

+ 7 - 6
trackpoint-app/include/ProjectStore.hpp

@@ -1,20 +1,21 @@
 #pragma once
 
-#include <QString>
-#include "lib3mf_implicit.hpp"
+#include <string>
 
 class ProjectStore {
 public:
   // Create an empty project
   ProjectStore();
   // Load an existing project
-  ProjectStore(QString projectFile);
+  ProjectStore(std::string projectFile);
+  // Destructor
+  ~ProjectStore();
   // Load a mesh
-  void loadMesh(QString meshFileString);
+  void loadMesh(std::string meshFile);
 
 private:
   bool projectLoaded;
-  Lib3MF::PWrapper _wrapper;
-  Lib3MF::PModel _project;
+  //Lib3MF::PWrapper _wrapper;
+  //Lib3MF::PModel _project;
   void load3mfLib();
 };

+ 14 - 5
trackpoint-app/src/MainWindow.cpp

@@ -5,15 +5,24 @@
 // Include dependencies
 #include <QFileDialog>
 
+MainWindow* globalPointer;
+
+MainWindow* MainWindow::getInstance() {
+  return globalPointer;
+}
+
 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent), ui(new Ui::MainWindow) {
+  globalPointer = this;
+
   ui->setupUi(this);
 
   osgWidget = new OSGWidget(this);
   ui->sceneWidget->layout()->addWidget(osgWidget);
 
   // TODO: Add option for opening a project via double click
-  projectStore = new ProjectStore();
+  //projectStore = new ProjectStore();
   renderView(NoMesh);
+  projectStore.loadMesh("/home/johannes/Documents/GitLab/bachelorthesis/testdata/testbutton.3mf");
 }
 
 MainWindow::~MainWindow() {
@@ -21,14 +30,14 @@ MainWindow::~MainWindow() {
   delete osgWidget;
 }
 
-ProjectStore* MainWindow::getProjectStorePointer() {
-  return projectStore;
-}
-
 OSGWidget* MainWindow::getOsgWidget() {
   return osgWidget;
 }
 
+ProjectStore* MainWindow::getStore() {
+  return &projectStore;
+}
+
 void MainWindow::renderView(GuiView view) {
   switch(view) {
     case NoMesh: {

+ 2 - 2
trackpoint-app/src/NoMeshWidget.cpp

@@ -19,6 +19,6 @@ NoMeshWidget::~NoMeshWidget() {
 
 void NoMeshWidget::loadMeshFile() {
   QString fileName = QFileDialog::getOpenFileName(this, tr("Open a 3D-Object"), "", tr("3D Objects (*.3mf *.stl)"));
-  MainWindow* parent = ((MainWindow*)this->parentWidget());
-  parent->getProjectStorePointer()->loadMesh(fileName);
+  std::string meshFile = fileName.toUtf8().constData();
+  MainWindow::getInstance()->getStore()->loadMesh(meshFile);
 }

+ 45 - 9
trackpoint-app/src/OSGWidget.cpp

@@ -14,6 +14,7 @@
 #include <osgGA/TrackballManipulator>
 #include <osgUtil/LineSegmentIntersector>
 #include <osgUtil/IntersectionVisitor>
+#include <osgUtil/SmoothingVisitor>
 #include <osg/PolygonMode>
 
 #include <osgDB/WriteFile>
@@ -69,16 +70,15 @@ OSGWidget::OSGWidget(QWidget* parent): QOpenGLWidget(parent),
   _view->setCamera(camera);
   _view->setSceneData(_root);
 
-  // Add axesNode under root
-  _axesNode = osgDB::readNodeFile("../../testdata/testbutton.stl");
-  if (!_axesNode) {
-      printf("Origin node not loaded, model not found\n");
-  }
+  // Create coordinate axes
+  _coordinateAxes = new osg::Geode;
+  osg::ref_ptr<osg::ShapeDrawable> zAxis = new osg::ShapeDrawable();
+  zAxis->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 0.1f, 100.0f));
+  zAxis->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
+  _coordinateAxes->addDrawable(zAxis.get());
 
-  // Set material for basic lighting and enable depth tests. Else, the sphere
-  // will suffer from rendering errors.
   {
-    osg::StateSet* stateSet = _axesNode->getOrCreateStateSet();
+    osg::StateSet* stateSet = _coordinateAxes->getOrCreateStateSet();
     osg::Material* material = new osg::Material;
 
     material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
@@ -87,7 +87,9 @@ OSGWidget::OSGWidget(QWidget* parent): QOpenGLWidget(parent),
     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
   }
 
-  _root->addChild(_axesNode);
+  _root->addChild(_coordinateAxes);
+
+  _axesNode = new osg::Geode;
 
   // Attach a manipulator (it's usually done for us when we use viewer.run())
   osg::ref_ptr<osgGA::TrackballManipulator> tm = new osgGA::TrackballManipulator;
@@ -122,6 +124,40 @@ OSGWidget::OSGWidget(QWidget* parent): QOpenGLWidget(parent),
 OSGWidget::~OSGWidget() {
 }
 
+void OSGWidget::renderBaseMesh(const std::vector<Lib3MF::sPosition> &verticesBuffer, const std::vector<Lib3MF::sTriangle> &triangleBuffer) {
+  _root->removeChild(_axesNode);
+
+  osg::ref_ptr<osg::Geometry> meshGeometry = new osg::Geometry;
+  osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
+  for (const Lib3MF::sPosition vertex: verticesBuffer) {
+    vertices->push_back(osg::Vec3(vertex.m_Coordinates[0], vertex.m_Coordinates[1], vertex.m_Coordinates[2]));
+  }
+  meshGeometry->setVertexArray(vertices.get());
+  osg::ref_ptr<osg::DrawElementsUInt> primitiveSet = new osg::DrawElementsUInt(GL_TRIANGLES);
+  for (const Lib3MF::sTriangle triangle: triangleBuffer) {
+    for (unsigned char i = 0; i < 3; i++) {
+      primitiveSet->push_back(triangle.m_Indices[i]);
+    }
+  }
+  meshGeometry->addPrimitiveSet(primitiveSet);
+  osgUtil::SmoothingVisitor::smooth(*meshGeometry);
+  _axesNode->addDrawable(meshGeometry.get());
+
+  // Set material for basic lighting and enable depth tests. Else, the sphere
+  // will suffer from rendering errors.
+  {
+    osg::StateSet* stateSet = _axesNode->getOrCreateStateSet();
+    osg::Material* material = new osg::Material;
+
+    material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
+
+    stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
+    stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
+  }
+
+  _root->addChild(_axesNode);
+}
+
 void OSGWidget::paintEvent(QPaintEvent*) {
   this->makeCurrent();
 

+ 57 - 7
trackpoint-app/src/ProjectStore.cpp

@@ -2,29 +2,79 @@
 #include "ProjectStore.hpp"
 
 // Include modules
+#include "MainWindow.hpp"
 #include "StringBasics.hpp"
 
+#include "lib3mf_implicit.hpp"
+
+#include <typeinfo>
+#include <iostream>
+
 ProjectStore::ProjectStore() {
   projectLoaded = false;
-  load3mfLib();
+  //load3mfLib();
+}
+
+ProjectStore::ProjectStore(std::string projectFile) {
+
 }
 
-ProjectStore::ProjectStore(QString projectFile) {
+ProjectStore::~ProjectStore() {
 
 }
 
 void ProjectStore::load3mfLib() {
-  _wrapper = Lib3MF::CWrapper::loadLibrary();
+  //_wrapper = Lib3MF::CWrapper::loadLibrary();
 }
 
-void ProjectStore::loadMesh(QString meshFileString) {
-  std::string meshFile = meshFileString.toUtf8().constData();
+void ProjectStore::loadMesh(std::string meshFile) {
   if (StringBasics::endsWithCaseInsensitive(meshFile, ".STL")) {
     printf("Currently unsupported.\n");
   } else if (StringBasics::endsWithCaseInsensitive(meshFile, ".3MF")) {
-    Lib3MF::PModel meshModel = _wrapper->CreateModel();
-    Lib3MF::PReader reader = meshModel->QueryReader("3mf");
+    Lib3MF::PWrapper _wrapper = Lib3MF::CWrapper::loadLibrary();
+    Lib3MF::PModel _project = _wrapper->CreateModel();
+    Lib3MF::PReader reader = _project->QueryReader("3mf");
     reader->ReadFromFile(meshFile);
+    Lib3MF::PMeshObjectIterator meshIterator = _project->GetMeshObjects();
+    if (meshIterator->Count() != 1) {
+      printf("Not 1 mesh: %d\n", meshIterator->Count());
+    }
+    meshIterator->MoveNext();
+    Lib3MF::PMeshObject mesh = meshIterator->GetCurrentMeshObject();
+    std::vector<Lib3MF::sPosition> verticesBuffer;
+    mesh->GetVertices(verticesBuffer);
+
+    Lib3MF_uint64 vertex_count = mesh->GetVertexCount();
+    Lib3MF_uint64 triangle_count = mesh->GetTriangleCount();
+    for (Lib3MF_uint64 idx = 0; idx < triangle_count; ++idx) {
+			Lib3MF::sTriangle triangle = mesh->GetTriangle(idx);
+			Lib3MF::sPosition vertex1, vertex2, vertex3;
+
+			vertex1 = mesh->GetVertex(triangle.m_Indices[0]);
+			vertex2 = mesh->GetVertex(triangle.m_Indices[1]);
+			vertex3 = mesh->GetVertex(triangle.m_Indices[2]);
+
+			//p->append_poly();
+			//p->append_vertex(vertex1.m_Coordinates[0], vertex1.m_Coordinates[1], vertex1.m_Coordinates[2]);
+			//p->append_vertex(vertex2.m_Coordinates[0], vertex2.m_Coordinates[1], vertex2.m_Coordinates[2]);
+			//p->append_vertex(vertex3.m_Coordinates[0], vertex3.m_Coordinates[1], vertex3.m_Coordinates[2]);
+      printf("%f %f %f\n", vertex1.m_Coordinates[0], vertex1.m_Coordinates[1], vertex1.m_Coordinates[2]);
+      printf("%f %f %f\n", vertex2.m_Coordinates[0], vertex2.m_Coordinates[1], vertex2.m_Coordinates[2]);
+      printf("%f %f %f\n", vertex3.m_Coordinates[0], vertex3.m_Coordinates[1], vertex3.m_Coordinates[2]);
+		}
+
+
+    /*for (const Lib3MF::sPosition vertex: verticesBuffer) {
+      std::cout << typeid(vertex).name() << std::endl;
+      std::cout << typeid(vertex.m_Coordinates).name() << std::endl;
+      std::cout << typeid(vertex.m_Coordinates[0]).name() << std::endl;
+      printf("%f %f %f\n", (float)vertex.m_Coordinates[0], (float)vertex.m_Coordinates[1], (float)vertex.m_Coordinates[2]);
+    }*/
+    std::vector<Lib3MF::sTriangle> triangleBuffer;
+    mesh->GetTriangleIndices(triangleBuffer);
+    MainWindow* mainWindow = MainWindow::getInstance();
+    mainWindow->getOsgWidget()->renderBaseMesh(verticesBuffer, triangleBuffer);
+    printf("Done\n");
   } else {
     printf("Unsupported file type.\n"); // TODO: Show popup
   }