-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from fabricix/STL-mesh-reader
STL mesh reader for terrain contact algorithm
- Loading branch information
Showing
10 changed files
with
1,590 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LocalDebuggerCommandArguments>boussinesq-problem.json</LocalDebuggerCommandArguments> | ||
<LocalDebuggerWorkingDirectory>C:\Users\fabri\MPM\MPM-Geomechanics\tests\boussinesq problem</LocalDebuggerWorkingDirectory> | ||
<LocalDebuggerCommandArguments>plane-slope-ASCII.stl</LocalDebuggerCommandArguments> | ||
<LocalDebuggerWorkingDirectory>C:\Users\fabri\MPM\MPM-Geomechanics\tests\stl-mesh-reader\</LocalDebuggerWorkingDirectory> | ||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LocalDebuggerCommandArguments> | ||
</LocalDebuggerCommandArguments> | ||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef STL_READER_H | ||
#define STL_READER_H | ||
|
||
#include <Eigen/Eigenvalues> | ||
using namespace Eigen; | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
/// @brief Struct representing a triangle | ||
struct Triangle { | ||
Vector3f normal; | ||
Vector3f v1, v2, v3; | ||
}; | ||
|
||
/// @brief Class to read STL files | ||
class STLReader { | ||
|
||
public: | ||
/// @brief Read the STL file | ||
bool read(const std::string& filename); | ||
|
||
/// @brief Get the triangles of the STL mesh | ||
/// @return Vector containing the triangles | ||
const std::vector<Triangle>& getTriangles() const; | ||
|
||
private: | ||
|
||
/// @brief Vector containing the triangles | ||
std::vector<Triangle> triangles; | ||
|
||
/// @brief Read the ASCII STL file | ||
bool readASCII(std::ifstream& file); | ||
|
||
/// @brief Read the binary STL file | ||
bool readBinary(std::ifstream& file); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "Mesh/STLReader.h" | ||
#include <iostream> | ||
#include <fstream> | ||
#include <sstream> | ||
|
||
bool STLReader::read(const std::string& filename) | ||
{ | ||
// open the file | ||
std::ifstream file(filename, std::ios::binary); | ||
if (!file.is_open()) { | ||
std::cerr << "Error opening STL file: " << filename << std::endl; | ||
return false; | ||
} | ||
|
||
// read header to determine the format | ||
char header[80]; | ||
file.read(header, 80); | ||
|
||
// determine if it is ASCII or binary | ||
std::string headerStr(header, 80); | ||
if (headerStr.find("solid") == 0) { | ||
file.close(); | ||
file.open(filename); // open it in text mode | ||
return readASCII(file); | ||
} else { | ||
return readBinary(file); | ||
} | ||
} | ||
|
||
bool STLReader::readASCII(std::ifstream& file) | ||
{ | ||
// clear triangles | ||
triangles.clear(); | ||
|
||
std::string line; | ||
while (std::getline(file, line)) | ||
{ | ||
std::istringstream ss(line); | ||
std::string word; | ||
ss >> word; | ||
|
||
if (word == "facet") | ||
{ | ||
Triangle triangle; | ||
ss >> word >> triangle.normal.x() >> triangle.normal.y() >> triangle.normal.z(); | ||
|
||
// read vertex coordinates | ||
std::getline(file, line); // outer loop | ||
std::getline(file, line); | ||
std::sscanf(line.c_str(), " vertex %f %f %f", &triangle.v1.x(), &triangle.v1.y(), &triangle.v1.z()); | ||
|
||
std::getline(file, line); | ||
std::sscanf(line.c_str(), " vertex %f %f %f", &triangle.v2.x(), &triangle.v2.y(), &triangle.v2.z()); | ||
|
||
std::getline(file, line); | ||
std::sscanf(line.c_str(), " vertex %f %f %f", &triangle.v3.x(), &triangle.v3.y(), &triangle.v3.z()); | ||
|
||
triangles.push_back(triangle); | ||
|
||
std::getline(file, line); // end loop | ||
std::getline(file, line); // end facet | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool STLReader::readBinary(std::ifstream& file) { | ||
|
||
// clear triangles | ||
triangles.clear(); | ||
|
||
uint32_t numTriangles; | ||
|
||
file.read(reinterpret_cast<char*>(&numTriangles), sizeof(uint32_t)); | ||
|
||
for (uint32_t i = 0; i < numTriangles; ++i) { | ||
Triangle triangle; | ||
file.read(reinterpret_cast<char*>(&triangle.normal), sizeof(Vector2d)); | ||
file.read(reinterpret_cast<char*>(&triangle.v1), sizeof(Vector2d)); | ||
file.read(reinterpret_cast<char*>(&triangle.v2), sizeof(Vector2d)); | ||
file.read(reinterpret_cast<char*>(&triangle.v3), sizeof(Vector2d)); | ||
file.ignore(2); // Ignore attributes for now | ||
triangles.push_back(triangle); | ||
} | ||
return true; | ||
} | ||
|
||
const std::vector<Triangle>& STLReader::getTriangles() const { | ||
return triangles; | ||
} |
Oops, something went wrong.