Skip to content

Commit

Permalink
Merge pull request #15 from fabricix/STL-mesh-reader
Browse files Browse the repository at this point in the history
STL mesh reader for terrain contact algorithm
  • Loading branch information
fabricix authored Jan 20, 2025
2 parents 9c175b5 + 63661ce commit 7cbce4c
Show file tree
Hide file tree
Showing 10 changed files with 1,590 additions and 5 deletions.
10 changes: 8 additions & 2 deletions build/msbuild/MPM-Geomechanics.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)../../inc</AdditionalIncludeDirectories>
</ClCompile>
Expand All @@ -120,7 +120,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions); _CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)../../inc</AdditionalIncludeDirectories>
<OpenMPSupport>true</OpenMPSupport>
Expand Down Expand Up @@ -164,6 +164,7 @@
<ClCompile Include="..\..\src\SolverExplicitTwoPhaseUSL.cpp" />
<ClCompile Include="..\..\src\SolverExplicitUSL.cpp" />
<ClCompile Include="..\..\src\States.cpp" />
<ClCompile Include="..\..\src\STLReader.cpp" />
<ClCompile Include="..\..\src\Update.cpp" />
<ClCompile Include="..\..\src\Warning.cpp" />
<ClCompile Include="..\..\tests\deformation-gradient\testDeformationGradient.cpp">
Expand All @@ -178,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 All @@ -201,6 +206,7 @@
<ClInclude Include="..\..\inc\Mesh\Mesh.h" />
<ClInclude Include="..\..\inc\Mesh\Node.h" />
<ClInclude Include="..\..\inc\Mesh\NodeMixture.h" />
<ClInclude Include="..\..\inc\Mesh\STLReader.h" />
<ClInclude Include="..\..\inc\Model.h" />
<ClInclude Include="..\..\inc\MPM.h" />
<ClInclude Include="..\..\inc\Output.h" />
Expand Down
5 changes: 5 additions & 0 deletions build/msbuild/MPM-Geomechanics.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<Filter>tests</Filter>
</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 Expand Up @@ -77,6 +81,7 @@
<ClInclude Include="..\..\inc\Update.h" />
<ClInclude Include="..\..\inc\Warning.h" />
<ClInclude Include="..\..\inc\Body\BodySphere.h" />
<ClInclude Include="..\..\inc\Mesh\STLReader.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="tests">
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>
2 changes: 1 addition & 1 deletion inc/Mesh/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Eigen::Vector3d;

/// \class Node
/// \brief Represents a mesh node.
/// \brief Represents a mesh node
class Node {

public:
Expand Down
39 changes: 39 additions & 0 deletions inc/Mesh/STLReader.h
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
90 changes: 90 additions & 0 deletions src/STLReader.cpp
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;
}
Loading

0 comments on commit 7cbce4c

Please sign in to comment.