This is an example of how to use the netdem library.
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
using namespace std;
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;
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 TestDatasetEllipsoidPlaneDetection() {
cout << "particle created ... " << endl;
Plane plane(0, 0, 0, 0, 0, 1);
cout << "wall created ... " << endl;
string root_dir = "local/examples/netdem/ann_models/ellipsoid_plane/";
LoadDatasetEllipsoidPlane(root_dir + "dataset_detection.txt");
int num_samples = dataset.size();
double(*ds_inputs)[4] = new double[num_samples][4];
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 < 4; i_inputs++) {
ds_inputs[i][i_inputs] = dataset[i][i_inputs];
}
ds_cnt_flags[i] = (bool)dataset[i][4];
}
for (int i = 0; i < num_samples; i++) {
double dist_pc_to_plane;
dist_pc_to_plane = ds_inputs[i][0];
dir_n[0] = ds_inputs[i][1];
dir_n[1] = ds_inputs[i][2];
dir_n[2] = ds_inputs[i][3];
Vec3d dir_n_ref{0, 0, 1};
auto rot_axis = Math::Cross(dir_n_ref, dir_n);
quat[0] = 1 + Math::Dot(dir_n, dir_n_ref);
quat[1] = rot_axis[0];
quat[2] = rot_axis[1];
quat[3] = rot_axis[2];
Math::Quaternion::Normalize(&quat);
-dist_pc_to_plane * dir_n[1],
-dist_pc_to_plane * dir_n[2]);
cnt_solver.
Init(&obj_p, &obj_w);
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 TestDatasetEllipsoidPlaneResolution() {
cout << "particle created ... " << endl;
Plane plane(0, 0, 0, 0, 0, 1);
cout << "wall created ... " << endl;
string root_dir = "local/examples/netdem/ann_models/ellipsoid_plane/";
LoadDatasetEllipsoidPlane(root_dir + "dataset_resolution.txt");
int num_samples = dataset.size();
double(*ds_inputs)[4] = new double[num_samples][4];
bool *ds_cnt_flags = new bool[num_samples];
double(*ds_cnt_feats)[4] = new double[num_samples][4];
for (int i = 0, skip = 1; i < num_samples; i += skip) {
for (int i_inputs = 0; i_inputs < 4; i_inputs++) {
ds_inputs[i][i_inputs] = dataset[i][i_inputs];
}
for (int i_outputs = 0; i_outputs < 4; i_outputs++) {
ds_cnt_feats[i][i_outputs] = dataset[i][4 + i_outputs];
}
}
for (int i = 0; i < num_samples; i++) {
double dist_pc_to_plane;
dist_pc_to_plane = ds_inputs[i][0];
dir_n[0] = ds_inputs[i][1];
dir_n[1] = ds_inputs[i][2];
dir_n[2] = ds_inputs[i][3];
Vec3d dir_n_ref{0, 0, 1};
auto rot_axis = Math::Cross(dir_n_ref, dir_n);
Math::Normalize(&rot_axis);
auto rot_angle = acos(Math::Dot(dir_n, dir_n_ref));
auto quat = Math::Quaternion::FromRodrigues(rot_angle, rot_axis);
-dist_pc_to_plane * dir_n[1],
-dist_pc_to_plane * dir_n[2]);
cnt_solver.
Init(&obj_p, &obj_w);
cout << ">> true: " << cnt_geoms.len_n << ", " << 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] << endl;
}
}
delete[] ds_inputs;
delete[] ds_cnt_feats;
}
void TestDatasetEllipsoidPlane() {
TestDatasetEllipsoidPlaneDetection();
TestDatasetEllipsoidPlaneResolution();
}
A class for representing an ellipsoid shape.
Definition shape_ellipsoid.hpp:15
void SetSize(double d) override
Set the size of the Ellipsoid object.
Definition shape_ellipsoid.cpp:51
Contact model that uses linear spring elements to evaluate contact forces and moments.
Definition model_linear_spring.hpp:16
Definition particle.hpp:26
A class for representing a plane with a center point and normal vector.
Definition shape_plane.hpp:22
GJK solver for convex geometries.
Definition solver_gjk_pw.hpp:20
bool Detect() override
Detects collisions between a particle and a wall.
Definition solver_gjk_pw.cpp:36
void Init(Particle *const p, Wall *const w) override
Initializes the collision solver with a particle and a wall.
Definition solver_gjk_pw.cpp:18
void ResolveInit(ContactPW *const cnt, double timestep) override
Initializes the contact resolution for a contact point.
Definition solver_gjk_pw.cpp:60
A class representing a wall object in a physics simulation.
Definition wall.hpp:32
void SetQuaternion(double q_0, double q_1, double q_2, double q_3)
Sets the orientation of the wall using a quaternion.
Definition wall.cpp:61
void SetPosition(double pos_x, double pos_y, double pos_z)
Sets the position of the wall.
Definition wall.cpp:41
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
std::array< double, 3 > Vec3d
Definition utils_macros.hpp:18
std::array< double, 4 > Vec4d
Definition utils_macros.hpp:19
dir_n
Definition json_serilization.hpp:19