NetDEM v1.0
Loading...
Searching...
No Matches
11_test_dataset_trimesh.cpp

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

#include "particle.hpp"
#include "utils_math.hpp"
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
using namespace netdem;
using namespace std;
VecXT<VecXT<double>> LoadDatasetTrimesh(string filename) {
ifstream file_handle;
file_handle.open(filename, ios::binary);
if (!file_handle) {
cout << "Fail to read file: " << filename << endl;
}
file_handle.seekg(0, ios::end);
long context_size = file_handle.tellg();
char *buffer = new char[context_size];
file_handle.seekg(0);
file_handle.read(buffer, context_size);
file_handle.close();
stringstream ss;
ss.str("");
ss.clear();
ss.str(buffer);
string str_line, str_num;
VecXT<double> data_line;
getline(ss, str_line);
istringstream readstr(str_line);
while (getline(readstr, str_num, ',')) {
data_line.push_back(atof(str_num.c_str()));
}
getline(readstr, str_num);
data_line.push_back(atof(str_num.c_str()));
dataset.push_back(data_line);
int num_data_per_line = data_line.size();
while (getline(ss, str_line)) {
if (str_line.find(",") == string::npos)
continue;
data_line.resize(num_data_per_line);
istringstream readstr(str_line);
for (int i = 0; i < num_data_per_line - 1; i++) {
getline(readstr, str_num, ',');
data_line[i] = atof(str_num.c_str());
}
getline(readstr, str_num);
data_line[num_data_per_line - 1] = atof(str_num.c_str());
dataset.push_back(data_line);
}
dataset.shrink_to_fit();
delete[] buffer;
return dataset;
}
void TestDatasetTrimeshDetection() {
// load particle
TriMesh tri_mesh_1;
tri_mesh_1.InitFromSTL("data/particle_template.stl");
tri_mesh_1.Decimate(200);
tri_mesh_1.AlignAxes();
tri_mesh_1.SetSize(1.0);
Particle obj_p1 = Particle(&tri_mesh_1);
obj_p1.need_update_stl_model = true;
Particle obj_p2 = Particle(&tri_mesh_1);
obj_p2.need_update_stl_model = true;
cout << "particle created ... " << endl;
string root_dir = "local/examples/netdem/ann_models/trimesh_trimesh/";
LoadDatasetTrimesh(root_dir + "dataset_detection.txt");
int num_samples = dataset.size();
double(*ds_inputs)[7] = new double[num_samples][7];
bool *ds_cnt_flags = new bool[num_samples];
for (int i = 0, skip = 1; i < num_samples; i += skip) {
for (int i_inputs = 0; i_inputs < 7; i_inputs++) {
ds_inputs[i][i_inputs] = dataset[i][i_inputs];
}
ds_cnt_flags[i] = (bool)dataset[i][7];
}
SolverBooleanPP cnt_solver;
VolumeBased cnt_model;
for (int i = 0; i < num_samples; i++) {
Vec4d quat;
// unpack the inputs
pos[0] = ds_inputs[i][0];
pos[1] = ds_inputs[i][1];
pos[2] = ds_inputs[i][2];
quat[0] = ds_inputs[i][3];
quat[1] = ds_inputs[i][4];
quat[2] = ds_inputs[i][5];
quat[3] = ds_inputs[i][6];
obj_p2.SetPosition(pos[0], pos[1], pos[2]);
obj_p2.SetQuaternion(quat[0], quat[1], quat[2], quat[3]);
cnt_solver.Init(&obj_p1, &obj_p2);
bool cnt_flag = cnt_solver.Detect();
cout << "true vs data: " << cnt_flag << ", " << ds_cnt_flags[i] << endl;
}
delete[] ds_inputs;
delete[] ds_cnt_flags;
}
void TestDatasetTrimeshResolution() {
// load particle
TriMesh tri_mesh_1;
tri_mesh_1.InitFromSTL("data/particle_template.stl");
tri_mesh_1.Decimate(200);
tri_mesh_1.AlignAxes();
tri_mesh_1.SetSize(1.0);
Particle obj_p1 = Particle(&tri_mesh_1);
obj_p1.need_update_stl_model = true;
Particle obj_p2 = Particle(&tri_mesh_1);
obj_p2.need_update_stl_model = true;
cout << "particle created ... " << endl;
string root_dir = "local/examples/netdem/ann_models/trimesh_trimesh/";
LoadDatasetTrimesh(root_dir + "dataset_resolution.txt");
int num_samples = dataset.size();
double(*ds_inputs)[7] = new double[num_samples][7];
double(*ds_cnt_feats)[8] = new double[num_samples][8];
for (int i = 0, skip = 1; i < num_samples; i += skip) {
for (int i_inputs = 0; i_inputs < 7; i_inputs++) {
ds_inputs[i][i_inputs] = dataset[i][i_inputs];
}
for (int i_outputs = 0; i_outputs < 8; i_outputs++) {
ds_cnt_feats[i][i_outputs] = dataset[i][7 + i_outputs];
}
}
SolverBooleanPP cnt_solver;
VolumeBased cnt_model;
for (int i = 0; i < num_samples; i++) {
Vec4d quat;
// unpack the inputs
pos[0] = ds_inputs[i][0];
pos[1] = ds_inputs[i][1];
pos[2] = ds_inputs[i][2];
quat[0] = ds_inputs[i][3];
quat[1] = ds_inputs[i][4];
quat[2] = ds_inputs[i][5];
quat[3] = ds_inputs[i][6];
obj_p2.SetPosition(pos[0], pos[1], pos[2]);
obj_p2.SetQuaternion(quat[0], quat[1], quat[2], quat[3]);
cnt_solver.Init(&obj_p1, &obj_p2);
// contact detection and resolution
if (cnt_solver.Detect()) {
auto cnt = ContactPP(&obj_p1, &obj_p2);
cnt.SetCollisionModel(&cnt_model);
cnt_solver.ResolveInit(&cnt, 1.0e-4);
auto &cnt_geoms = cnt.collision_entries[0].cnt_geoms;
cout << ">> true: " << cnt_geoms.vol << ", " << cnt_geoms.sn << ", "
<< cnt_geoms.dir_n[0] << ", " << cnt_geoms.dir_n[1] << ", "
<< cnt_geoms.dir_n[2] << ", " << cnt_geoms.pos[0] << ", "
<< cnt_geoms.pos[1] << ", " << cnt_geoms.pos[2] << endl;
cout << ">> data: " << ds_cnt_feats[i][0] << ", " << ds_cnt_feats[i][1]
<< ", " << ds_cnt_feats[i][2] << ", " << ds_cnt_feats[i][3] << ", "
<< ds_cnt_feats[i][4] << ", " << ds_cnt_feats[i][5] << ", "
<< ds_cnt_feats[i][6] << ", " << ds_cnt_feats[i][7] << endl;
}
}
delete[] ds_inputs;
delete[] ds_cnt_feats;
}
void TestDatasetTrimesh() {
TestDatasetTrimeshDetection();
TestDatasetTrimeshResolution();
}
A class representing a contact between two particles.
Definition contact_pp.hpp:20
Definition particle.hpp:26
virtual void SetQuaternion(double q_0, double q_1, double q_2, double q_3)
Sets the orientation of the particle using a quaternion.
Definition particle.cpp:103
virtual void SetPosition(double pos_x, double pos_y, double pos_z)
Sets the position of the particle.
Definition particle.cpp:83
bool need_update_stl_model
Flag indicating whether STl model intersection-based contact detection and resolution is needed.
Definition particle.hpp:207
Solver for triangle mesh contacts between two particles using boolean operations.
Definition solver_boolean_pp.hpp:18
bool Detect() override
Detects collisions between two particles using boolean operations on their triangle meshes.
Definition solver_boolean_pp.cpp:44
void ResolveInit(ContactPP *const cnt, double timestep) override
Initializes the contact point between two particles at time t = 0.
Definition solver_boolean_pp.cpp:74
void Init(Particle *const p1, Particle *const p2) override
Initializes the collision solver with two particles.
Definition solver_boolean_pp.cpp:22
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.
void Decimate(int num_nodes)
Decimate the TriMesh object.
Definition shape_trimesh.cpp:121
void AlignAxes()
Align the axes of the TriMesh object.
Definition shape_trimesh.cpp:107
void SetSize(double d) override
Set the size of the TriMesh object.
Definition shape_trimesh.cpp:207
Contact model that evaluates forces and moments based on volume overlap and relative velocity.
Definition model_volume_based.hpp:13
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
pos
Definition json_serilization.hpp:19
std::array< double, 3 > Vec3d
Definition utils_macros.hpp:18
std::array< double, 4 > Vec4d
Definition utils_macros.hpp:19