NetDEM v1.0
Loading...
Searching...
No Matches
01_sdf_error.cpp

This is an example of how to use the netdem library.

#include "stl_model.hpp"
#include "tetmesh.hpp"
#include "utils_math.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace netdem;
using namespace std;
void SDFError(int argc, char **argv) {
string root_dir = "tmp/examples/sdf_dem/sdf_error/";
filesystem::create_directories(root_dir);
PolySuperEllipsoid ellipsoid(0.5, 1, 1.5, 0.5, 1, 1.5, 1.5, 1.5);
auto stl_model = ellipsoid.GetSTLModel(1000);
stl_model.SaveAsVTK(root_dir + "surface.vtk");
TriMesh trimesh;
trimesh.InitFromSTL(stl_model);
double mesh_size =
pow(stl_model.GetVolume() / Math::PI * 6.0, 1.0 / 3.0) / 30.0;
TetMesh tetmesh(stl_model.vertices, stl_model.facets, mesh_size);
// calculate the signed distance
VecXT<Vec3d> dist_list;
for (auto &node : tetmesh.nodes) {
auto dist_ellipsoid = ellipsoid.SignedDistance(node);
auto dist_trimesh = trimesh.SignedDistance(node);
dist_list.push_back(
{dist_ellipsoid, dist_trimesh, abs(dist_ellipsoid - dist_trimesh)});
}
// save the results
tetmesh.SaveAsVTK(root_dir + "tetmesh.vtk");
stringbuf buf;
ostream os(&buf);
os << "POINT_DATA " << dist_list.size() << endl;
os << "SCALARS SDF float 3" << endl;
os << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < dist_list.size(); i++) {
os << dist_list[i][0] << " " << dist_list[i][1] << " " << dist_list[i][2]
<< endl;
}
ofstream outfile;
outfile.open(root_dir + "tetmesh.vtk", ios::app);
if (!outfile) {
cout << "cannot open file: " << root_dir + "tetmesh.vtk" << endl;
}
outfile << buf.str();
outfile.close();
}
A class representing a poly superellipsoid with two different orders and three different axes.
Definition shape_poly_super_ellipsoid.hpp:27
double SignedDistance(Vec3d const &pos) const override
Compute the signed distance from a given position to the surface of the PolySuperEllipsoid object.
Definition shape_poly_super_ellipsoid.cpp:329
STLModel GetSTLModel(int num_nodes=200) override
Generate an STL model of the PolySuperEllipsoid object.
Definition shape_poly_super_ellipsoid.cpp:250
void SaveAsVTK(std::string const &file) const
Save the model as a VTK file.
Definition stl_model.cpp:170
A class that represents a tetrahedral mesh.
Definition tetmesh.hpp:17
void SaveAsVTK(std::string const &file)
Saves the tetrahedral mesh as a VTK file.
Definition tetmesh.cpp:49
VecXT< Vec3d > nodes
The nodes in the tetrahedral mesh.
Definition tetmesh.hpp:20
A class representing a triangular mesh in 3D space.
Definition shape_trimesh.hpp:23
void InitFromSTL(std::string const &file)
Initialize the TriMesh object from an STL file.
double SignedDistance(Vec3d const &pos) const override
Calculate the signed distance from a point to the TriMesh object.
Definition shape_trimesh.cpp:513
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31