This is an example of how to use the netdem library.
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
using namespace std;
void SaveDatasetEllipsoidDetection(int num_samples, double (*ds_inputs)[7],
bool *ds_cnt_flags, string filename) {
stringbuf buf;
ostream os(&buf);
int os_width = 24;
os.setf(ios::scientific);
os.precision(15);
for (int i = 0; i < num_samples; i++) {
for (int i_inputs = 0; i_inputs < 7; i_inputs++) {
os.width(os_width);
os << ds_inputs[i][i_inputs] << ", ";
}
os.width(3);
os << (ds_cnt_flags[i] ? 1 : 0) << endl;
}
ofstream outfile;
outfile.open(filename);
if (!outfile) {
cout << "cannot open file: " << filename << endl;
}
outfile << buf.str();
outfile.close();
cout << "data saved to: " << filename << endl;
}
void SaveDatasetEllipsoidResolution(int num_samples, double (*ds_inputs)[7],
double (*ds_cnt_feats)[7],
string filename) {
stringbuf buf;
ostream os(&buf);
int os_width = 24;
os.setf(ios::scientific);
os.precision(15);
for (int i = 0; i < num_samples; i++) {
if (ds_cnt_feats[i][0] < 0)
continue;
for (int i_inputs = 0; i_inputs < 7; i_inputs++) {
os.width(os_width);
os << ds_inputs[i][i_inputs] << ", ";
}
for (int i_outputs = 0; i_outputs < 6; i_outputs++) {
os.width(os_width);
os << ds_cnt_feats[i][i_outputs] << ", ";
}
os.width(os_width);
os << ds_cnt_feats[i][6] << endl;
}
ofstream outfile;
outfile.open(filename);
if (!outfile) {
cout << "cannot open file: " << filename << endl;
}
outfile << buf.str();
outfile.close();
cout << "data saved to: " << filename << endl;
}
void GenDatasetEllipsoid(int num_samples = 100) {
cout << "particle created ... " << endl;
double(*ds_inputs)[7] = new double[num_samples][7];
bool *ds_cnt_flags = new bool[num_samples];
double(*ds_cnt_feats)[7] = new double[num_samples][7];
VecXT<Vec3d> vertices = SphericalVoronoi::Solve(1000, 10000, 1.0e-4);
IGLWrapper::ConvexHull(vertices, &vertices, &facets);
for (int trial = 0, i = 0; trial < num_samples * 100; trial++) {
int id_facet = floor(uniform_dist.Get() * facets.size());
auto vert_0 = vertices[facets[id_facet][0]];
auto vert_1 = vertices[facets[id_facet][1]];
auto vert_2 = vertices[facets[id_facet][2]];
double u_vert = uniform_dist.Get();
double v_vert = uniform_dist.Get() * (1 - u_vert);
double w_vert = 1 - u_vert - v_vert;
dir_n[0] = u_vert * vert_0[0] + v_vert * vert_1[0] + w_vert * vert_2[0];
dir_n[1] = u_vert * vert_0[1] + v_vert * vert_1[1] + w_vert * vert_2[1];
dir_n[2] = u_vert * vert_0[2] + v_vert * vert_1[2] + w_vert * vert_2[2];
Math::Normalize(&dir_n);
double cnt_prob = uniform_dist.Get();
double len_max = cnt_prob > 0.6 ? 0.1 : 0;
double len_n = len_min + uniform_dist.Get() * (len_max - len_min);
quat[0] = uniform_dist.Get() * 2.0 - 1.0;
quat[1] = uniform_dist.Get() * 2.0 - 1.0;
quat[2] = uniform_dist.Get() * 2.0 - 1.0;
quat[3] = uniform_dist.Get() * 2.0 - 1.0;
Math::Quaternion::Normalize(&quat);
Vec3d sup_pos_p2, sup_pos_p2_ref, sup_pos_p2_o;
Vec4d quat_conj = Math::Quaternion::Conjugate(quat);
Vec3d sup_dir_p2_ref = Math::Rotate(dir_n, quat_conj);
sup_pos_p2_o = Math::Rotate(sup_pos_p2_ref, quat);
if (len_n < 0) {
continue;
ds_inputs[i][0] = obj_p2.
pos[0];
ds_inputs[i][1] = obj_p2.
pos[1];
ds_inputs[i][2] = obj_p2.
pos[2];
ds_cnt_flags[i] = 0;
ds_cnt_feats[i][0] = -1;
ds_cnt_feats[i][1] = -1;
ds_cnt_feats[i][2] = -1;
ds_cnt_feats[i][3] = -1;
ds_cnt_feats[i][4] = -1;
ds_cnt_feats[i][5] = -1;
ds_cnt_feats[i][6] = -1;
i++;
} else {
ds_inputs[i][0] = obj_p2.
pos[0];
ds_inputs[i][1] = obj_p2.
pos[1];
ds_inputs[i][2] = obj_p2.
pos[2];
ds_cnt_flags[i] = 1;
ds_cnt_feats[i][0] =
len_n;
ds_cnt_feats[i][1] =
dir_n[0];
ds_cnt_feats[i][2] =
dir_n[1];
ds_cnt_feats[i][3] =
dir_n[2];
ds_cnt_feats[i][4] = sup_pos_p1[0] + 0.5 *
len_n *
dir_n[0];
ds_cnt_feats[i][5] = sup_pos_p1[1] + 0.5 *
len_n *
dir_n[1];
ds_cnt_feats[i][6] = sup_pos_p1[2] + 0.5 *
len_n *
dir_n[2];
i++;
}
if (((i + 1) % 100) == 0) {
cout << "number of samples: " << i + 1 << " ..." << endl;
}
if (i >= num_samples) {
break;
}
}
string root_dir = "local/examples/netdem/ann_models/ellipsoid_ellipsoid/";
SaveDatasetEllipsoidDetection(num_samples, ds_inputs, ds_cnt_flags,
root_dir + "dataset_detection.txt");
SaveDatasetEllipsoidResolution(num_samples, ds_inputs, ds_cnt_feats,
root_dir + "dataset_resolution.txt");
delete[] ds_inputs;
delete[] ds_cnt_flags;
delete[] ds_cnt_feats;
}
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
Vec3d SupportPoint(Vec3d const &dir) override
Compute the support point in a given direction for the Ellipsoid object.
Definition shape_ellipsoid.cpp:168
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
Vec4d quaternion
The quaternion of the particle.
Definition particle.hpp:108
Vec3d pos
The position of the particle.
Definition particle.hpp:103
virtual double GetBoundSphereRadius() const
Return the inertia of the shape.
Definition shape.cpp:126
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
len_n
Definition json_serilization.hpp:20
std::array< double, 4 > Vec4d
Definition utils_macros.hpp:19
dir_n
Definition json_serilization.hpp:19