Skip to content

Commit

Permalink
add: adding a test for STL mesh reader, including two stl meshes in b…
Browse files Browse the repository at this point in the history
…inary and ascii format
  • Loading branch information
fabricix committed Jan 20, 2025
1 parent 22634ad commit 63661ce
Show file tree
Hide file tree
Showing 8 changed files with 1,473 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build/msbuild/MPM-Geomechanics.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\tests\stl-mesh-reader\stl-mesh-reader-test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\inc\Body\Body.h" />
Expand Down
3 changes: 3 additions & 0 deletions build/msbuild/MPM-Geomechanics.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
</ClCompile>
<ClCompile Include="..\..\src\BodySphere.cpp" />
<ClCompile Include="..\..\src\STLReader.cpp" />
<ClCompile Include="..\..\tests\stl-mesh-reader\stl-mesh-reader-test.cpp">
<Filter>tests</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\inc\Body\Body.h" />
Expand Down
9 changes: 7 additions & 2 deletions build/msbuild/MPM-Geomechanics.vcxproj.user
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>
27 changes: 19 additions & 8 deletions src/STLReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include <fstream>
#include <sstream>

bool STLReader::read(const std::string& filename) {
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 format
// read header to determine the format
char header[80];
file.read(header, 80);

Expand All @@ -25,14 +27,20 @@ bool STLReader::read(const std::string& filename) {
}
}

bool STLReader::readASCII(std::ifstream& file) {
bool STLReader::readASCII(std::ifstream& file)
{
// clear triangles
triangles.clear();

std::string line;
while (std::getline(file, line)) {
while (std::getline(file, line))
{
std::istringstream ss(line);
std::string word;
ss >> word;

if (word == "facet") {
if (word == "facet")
{
Triangle triangle;
ss >> word >> triangle.normal.x() >> triangle.normal.y() >> triangle.normal.z();

Expand All @@ -49,15 +57,18 @@ bool STLReader::readASCII(std::ifstream& file) {

triangles.push_back(triangle);

std::getline(file, line); // endloop
std::getline(file, line); // endfacet
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));
Expand Down
Loading

0 comments on commit 63661ce

Please sign in to comment.