MPM-Geomechanics
Material Point Method for simulating geo-materials under large deformation conditions
Loading...
Searching...
No Matches
STLReader.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2021-2025 MPM-Geomechanics Development Team
3
4#ifndef STL_READER_H
5#define STL_READER_H
6
7#include <Eigen/Eigenvalues>
8using namespace Eigen;
9
10#include <string>
11#include <vector>
12
14struct Triangle {
15
16 Vector3d normal;
17 Vector3d v1, v2, v3;
18
21 Vector3d getVertex1() const {
22 return v1;
23 }
24
27 const Vector3d& getVertex2() const {
28 return v2;
29 }
30
33 const Vector3d& getVertex3() const {
34 return v3;
35 }
36
39 const Vector3d& getNormal() const {
40 return normal;
41 }
42
45 Vector3d getCentroid() const {
46 return (v1 + v2 + v3) / 3.0f;
47 }
48
51 double getArea() const {
52 return 0.5f * ((v2 - v1).cross(v3 - v1)).norm();
53 }
54};
55
57class STLReader {
58
59public:
61 bool read(const std::string& filename);
62
65 const std::vector<Triangle>& getTriangles() const;
66
70 void removeTrianglesOutsideLimits(const Vector3d& min, const Vector3d& max);
71
75
78 bool writeSTL(const std::string& output_filename) const;
79
81 inline void STLResultsFlagSet(bool flag) { write_stl_results = flag; }
82
84 inline bool STLResultsFlagGet() { return write_stl_results; }
85
86 private:
87
89 std::vector<Triangle> triangles;
90
92 bool readASCII(std::ifstream& file);
93
95 bool readBinary(std::ifstream& file);
96
98 bool write_stl_results = false;
99};
100
101#endif
Class to read STL files.
Definition STLReader.h:57
const std::vector< Triangle > & getTriangles() const
Get the triangles of the STL mesh.
bool writeSTL(const std::string &output_filename) const
Write the STL file.
bool readASCII(std::ifstream &file)
Read the ASCII STL file.
void removeTrianglesOutsideLimits(const Vector3d &min, const Vector3d &max)
Remove triangles that have all vertices outside the specified bounding box.
void recalculateNormals()
Recalculate the normals of the triangles.
bool readBinary(std::ifstream &file)
Read the binary STL file.
bool STLResultsFlagGet()
Get flag to write STL results with normals.
Definition STLReader.h:84
bool read(const std::string &filename)
Read the STL file.
void STLResultsFlagSet(bool flag)
Set flag to write STL results with normals.
Definition STLReader.h:81
bool write_stl_results
Flag to write STL results with normals.
Definition STLReader.h:98
std::vector< Triangle > triangles
Vector containing the triangles.
Definition STLReader.h:89
Struct representing a triangle.
Definition STLReader.h:14
Vector3d v1
Definition STLReader.h:17
double getArea() const
Calculate the area of the triangle.
Definition STLReader.h:51
Vector3d getCentroid() const
Calculate the centroid of the triangle.
Definition STLReader.h:45
Vector3d normal
Normal vector of the triangle.
Definition STLReader.h:16
const Vector3d & getVertex2() const
Get the second vertex of the triangle.
Definition STLReader.h:27
const Vector3d & getVertex3() const
Get the third vertex of the triangle.
Definition STLReader.h:33
Vector3d getVertex1() const
Get the first vertex of the triangle.
Definition STLReader.h:21
Vector3d v2
Definition STLReader.h:17
Vector3d v3
Vertices of the triangle.
Definition STLReader.h:17
const Vector3d & getNormal() const
Get the normal of the triangle.
Definition STLReader.h:39