Skip to content

Commit

Permalink
WIP: Mesh saving and Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Apr 5, 2023
1 parent 4c20f62 commit c458d3c
Show file tree
Hide file tree
Showing 21 changed files with 943 additions and 49 deletions.
325 changes: 325 additions & 0 deletions CMakeLists.txt.user

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "-DQt5_DIR=\"C:/Qt/5.15.2/msvc2019_64/lib/cmake/Qt5\" -DAutoscoper_RENDERING_BACKEND=\"OpenCL\"",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}
4 changes: 4 additions & 0 deletions autoscoper/src/ui/AutoscoperMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,10 @@ void AutoscoperMainWindow::on_toolButtonTrackCurrent_clicked() {
tracking_dialog->retrack();
}*/

void AutoscoperMainWindow::on_toolButtonToggleMeshes_clicked() {
tracker->trial()->meshes[0].WriteSTL("C:\\Users\\anthony.lombardi\\Desktop\\mesh.stl");
}

void AutoscoperMainWindow::on_actionExport_NCC_as_csv_triggered(bool checked) {
QString filename = get_filename(true, "*.ncc");
if (filename.compare("") != 0) {
Expand Down
5 changes: 5 additions & 0 deletions autoscoper/src/ui/AutoscoperMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class AutoscoperMainWindow : public QMainWindow{
// Backup
bool backup_on;

// Meshes
bool meshesVisible = false;

//Tracker
Tracker * tracker;
GLTracker * gltracker;
Expand Down Expand Up @@ -277,6 +280,8 @@ class AutoscoperMainWindow : public QMainWindow{
void on_toolButtonTrackCurrent_clicked();
//void on_toolButtonRetrack_clicked();

// Meshes
void on_toolButtonToggleMeshes_clicked();

//Shortcuts
void key_w_pressed();
Expand Down
Binary file added autoscoper/src/ui/resource-files/icons/mesh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion autoscoper/src/ui/ui-files/AutoscoperMainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,24 @@ Dialog</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonToggleMeshes">
<property name="text">
<string>Toggle
Meshes</string>
</property>
<property name="icon">
<iconset>
<normaloff>../resource-files/icons/mesh.png</normaloff>../resource-files/icons/mesh.png</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -411,7 +429,7 @@ Dialog</string>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand Down
155 changes: 145 additions & 10 deletions libautoscoper/src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,128 @@
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <algorithm>

Mesh::Mesh(const std::string&& filename){
// check the file extension
std::string extension = filename.substr(filename.find_last_of(".") + 1);
if (extension == "stl") {
ReadSTL(filename);
} else if (extension == "wrl" || extension == "iv") {
ReadVRML(filename);
} else {
throw std::runtime_error("Mesh: File extension, " + extension + ", is not supported");
Mesh::Mesh(const std::string& filename) {
// check the file extension
std::string extension = filename.substr(filename.find_last_of(".") + 1);
if (extension == "stl") {
ReadSTL(filename);
}
else if (extension == "wrl" || extension == "iv") {
ReadVRML(filename);
}
else {
throw std::runtime_error("Mesh: File extension, " + extension + ", is not supported");
}
}

Mesh::Mesh(const Mesh& mesh) {
facets_ = mesh.facets_;
numfacets_ = mesh.numfacets_;
}

void Mesh::TransformMesh(double xyzypr[]) {
// xyzypr is a 6 element array of doubles that contains the x, y, z, yaw, pitch, and roll movements of the mesh

for (Facet& facet : facets_) {
// apply the x, y, and z movements
for (int i = 0; i < 3; i++) {
facet.v1[i] += xyzypr[i];
facet.v2[i] += xyzypr[i];
facet.v3[i] += xyzypr[i];
}

// apply the yaw, pitch, and roll movements
// these are rotations about the x, y, and z axes, respectively
// first, rotate about the z axis
double x = facet.v1[0];
double y = facet.v1[1];
facet.v1[0] = x * cos(xyzypr[5]) - y * sin(xyzypr[5]);
facet.v1[1] = x * sin(xyzypr[5]) + y * cos(xyzypr[5]);

x = facet.v2[0];
y = facet.v2[1];
facet.v2[0] = x * cos(xyzypr[5]) - y * sin(xyzypr[5]);
facet.v2[1] = x * sin(xyzypr[5]) + y * cos(xyzypr[5]);

x = facet.v3[0];
y = facet.v3[1];
facet.v3[0] = x * cos(xyzypr[5]) - y * sin(xyzypr[5]);
facet.v3[1] = x * sin(xyzypr[5]) + y * cos(xyzypr[5]);

// next, rotate about the y axis
double z = facet.v1[2];
x = facet.v1[0];
facet.v1[2] = z * cos(xyzypr[4]) - x * sin(xyzypr[4]);
facet.v1[0] = z * sin(xyzypr[4]) + x * cos(xyzypr[4]);

z = facet.v2[2];
x = facet.v2[0];
facet.v2[2] = z * cos(xyzypr[4]) - x * sin(xyzypr[4]);
facet.v2[0] = z * sin(xyzypr[4]) + x * cos(xyzypr[4]);

z = facet.v3[2];
x = facet.v3[0];
facet.v3[2] = z * cos(xyzypr[4]) - x * sin(xyzypr[4]);
facet.v3[0] = z * sin(xyzypr[4]) + x * cos(xyzypr[4]);

// finally, rotate about the x axis
y = facet.v1[1];
z = facet.v1[2];
facet.v1[1] = y * cos(xyzypr[3]) - z * sin(xyzypr[3]);
facet.v1[2] = y * sin(xyzypr[3]) + z * cos(xyzypr[3]);

y = facet.v2[1];
z = facet.v2[2];
facet.v2[1] = y * cos(xyzypr[3]) - z * sin(xyzypr[3]);
facet.v2[2] = y * sin(xyzypr[3]) + z * cos(xyzypr[3]);

y = facet.v3[1];
z = facet.v3[2];
facet.v3[1] = y * cos(xyzypr[3]) - z * sin(xyzypr[3]);
facet.v3[2] = y * sin(xyzypr[3]) + z * cos(xyzypr[3]);
}

}

void Mesh::WriteSTL(const std::string& filename) {
// Open the file in write mode
std::ofstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Mesh: Could not open file");
}
// write the STL in Binary
// write the header -> use 80 blank chars since the header is generally ignored
char header[81];
for (int i = 0; i < 80; i++) {
header[i] = ' ';
}
header[80] = '\0';
file.write(header, 80);
// write the number of facets
file.write((char*)&numfacets_, 4);
// write the facets
for (Facet& facet : facets_) {
// write the normal
file.write((char*)&facet.normal[0], 4);
file.write((char*)&facet.normal[1], 4);
file.write((char*)&facet.normal[2], 4);
// write the vertices
file.write((char*)&facet.v1[0], 4);
file.write((char*)&facet.v1[1], 4);
file.write((char*)&facet.v1[2], 4);
file.write((char*)&facet.v2[0], 4);
file.write((char*)&facet.v2[1], 4);
file.write((char*)&facet.v2[2], 4);
file.write((char*)&facet.v3[0], 4);
file.write((char*)&facet.v3[1], 4);
file.write((char*)&facet.v3[2], 4);
// write the attribute byte count
unsigned short attribute = 0;
file.write((char*)&attribute, 2);
}
file.close();
}


Expand All @@ -34,6 +145,7 @@ void Mesh::ReadSTL(const std::string& filename) {
ReadSTLBinary(filename);
}

CalculateAABB();
}

void Mesh::ReadSTLASCII(const std::string& filename) {
Expand Down Expand Up @@ -61,6 +173,7 @@ void Mesh::ReadSTLASCII(const std::string& filename) {
StringToVector(line.substr(7), f.v2);
std::getline(file, line);
StringToVector(line.substr(7), f.v3);
f.centroid = { (f.v1[0] + f.v2[0] + f.v3[0]) / 3.0f, (f.v1[1] + f.v2[1] + f.v3[1]) / 3.0f, (f.v1[2] + f.v2[2] + f.v3[2]) / 3.0f };
facets_.push_back(f);
}
}
Expand Down Expand Up @@ -109,6 +222,7 @@ void Mesh::ReadSTLBinary(const std::string& filename) {
f.v1 = { v1[0],v1[1],v1[2] };
f.v2 = { v2[0],v2[1],v2[2] };
f.v3 = { v3[0],v3[1],v3[2] };
f.centroid = { (f.v1[0] + f.v2[0] + f.v3[0]) / 3.0f, (f.v1[1] + f.v2[1] + f.v3[1]) / 3.0f, (f.v1[2] + f.v2[2] + f.v3[2]) / 3.0f };
// add the facet to the mesh
facets_.push_back(f);
}
Expand Down Expand Up @@ -187,6 +301,7 @@ void Mesh::ReadVRML(const std::string& filename) {
f.v1 = points[indices[i][0]];
f.v2 = points[indices[i][1]];
f.v3 = points[indices[i][2]];
f.centroid = { (f.v1[0] + f.v2[0] + f.v3[0]) / 3.0f, (f.v1[1] + f.v2[1] + f.v3[1]) / 3.0f, (f.v1[2] + f.v2[2] + f.v3[2]) / 3.0f };
// calculate the normal
std::vector<float> U = { (f.v2[0] - f.v1[0]), (f.v2[1] - f.v1[1]), (f.v2[2] - f.v1[2] )};
std::vector<float> V = { (f.v3[0] - f.v1[0]), (f.v3[1] - f.v1[1]), (f.v3[2] - f.v1[2] )};
Expand All @@ -200,6 +315,27 @@ void Mesh::ReadVRML(const std::string& filename) {
numfacets_ = facets_.size();
}

void Mesh::CalculateAABB() {
// find the min and max for all xyz values of the mesh
float minx, miny, minz, maxx, maxy, maxz;
minx = miny = minz = std::numeric_limits<float>::max();
maxx = maxy = maxz = std::numeric_limits<float>::min();
for (Facet f : facets_) {
minx = std::min(minx, std::min(f.v1[0], std::min(f.v2[0], f.v3[0])));
miny = std::min(miny, std::min(f.v1[1], std::min(f.v2[1], f.v3[1])));
minz = std::min(minz, std::min(f.v1[2], std::min(f.v2[2], f.v3[2])));
maxx = std::max(maxx, std::max(f.v1[0], std::max(f.v2[0], f.v3[0])));
maxy = std::max(maxy, std::max(f.v1[1], std::max(f.v2[1], f.v3[1])));
maxz = std::max(maxz, std::max(f.v1[2], std::max(f.v2[2], f.v3[2])));
}
aabb_[0] = minx;
aabb_[1] = miny;
aabb_[2] = minz;
aabb_[3] = maxx;
aabb_[4] = maxy;
aabb_[5] = maxz;
}

void Mesh::StringToVector(std::string& str, std::vector<float>& v) {
size_t spacePos;
float v1 = std::stof(str, &spacePos);
Expand All @@ -223,4 +359,3 @@ void Mesh::ReadCoordIndex(std::string& str, std::vector<int>& i) {
}
i = { x,y,std::stoi(str) };
}

14 changes: 13 additions & 1 deletion libautoscoper/src/Mesh.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <string>
#include <vector>

Expand All @@ -6,20 +7,31 @@ struct Facet {
std::vector<float> v2;
std::vector<float> v3;
std::vector<float> normal;
std::vector<float> centroid;
};

class Mesh {
public:
Mesh(const std::string&& filename);
Mesh(const std::string& filename);
Mesh(const Mesh&);

void TransformMesh(double xyzypr[]);
void WriteSTL(const std::string& filename);

unsigned int GetNumFacets() const { return numfacets_; }
const Facet& GetFacet(unsigned int i) const { return facets_[i]; }
const float* GetAABB() const { return aabb_; }
private:
std::vector<Facet> facets_;
unsigned int numfacets_;
float aabb_[6];

void ReadSTL(const std::string& filename);
void ReadSTLASCII(const std::string& filename);
void ReadSTLBinary(const std::string& filename);
void ReadVRML(const std::string& filename);

void CalculateAABB();

void StringToVector(std::string& str, std::vector<float>& v);
void ReadCoordIndex(std::string& str, std::vector<int>& i);
Expand Down
Loading

0 comments on commit c458d3c

Please sign in to comment.