This is an example of how to use the netdem library.
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
using namespace std;
void SaveDatasetSDF(int num_samples, double (*ds_inputs)[3],
double (*ds_SDF)[1], 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 < 3; i_inputs++) {
os.width(os_width);
os << ds_inputs[i][i_inputs] << ", ";
}
os.width(os_width);
os << ds_SDF[i][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 SaveDatasetSPF(int num_samples, double (*ds_inputs)[3],
double (*ds_SPF)[3], 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_SPF[i][0] == 10)
continue;
for (int i_inputs = 0; i_inputs < 3; i_inputs++) {
os.width(os_width);
os << ds_inputs[i][i_inputs] << ", ";
}
for (int i_outputs = 0; i_outputs < 2; i_outputs++) {
os.width(os_width);
os << ds_SPF[i][i_outputs] << ", ";
}
os.width(os_width);
os << ds_SPF[i][2] << 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 GenDataset(int num_samples = 100) {
cout << "particle created ... " << endl;
double(*ds_inputs)[3] = new double[num_samples][3];
double(*ds_SDF)[1] = new double[num_samples][1];
double(*ds_SPF)[3] = new double[num_samples][3];
double dist_min{dist_max};
dist_min = min(Math::NormL2(vert), dist_min);
}
dist_min *= 0.9;
double dist_range = dist_max - dist_min;
VecXT<Vec3d> vertices = SphericalVoronoi::Solve(1000, 10000, 1.0e-4);
IGLWrapper::ConvexHull(vertices, &vertices, &facets);
int count = 0;
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;
tmp_dir[0] = u_vert * vert_0[0] + v_vert * vert_1[0] + w_vert * vert_2[0];
tmp_dir[1] = u_vert * vert_0[1] + v_vert * vert_1[1] + w_vert * vert_2[1];
tmp_dir[2] = u_vert * vert_0[2] + v_vert * vert_1[2] + w_vert * vert_2[2];
Math::Normalize(&tmp_dir);
double tmp_dist = dist_min + uniform_dist.Get() * dist_range;
query_p[0] = tmp_dist * tmp_dir[0];
query_p[1] = tmp_dist * tmp_dir[1];
query_p[2] = tmp_dist * tmp_dir[2];
if (SDF_P < 0 && SDF_P > -0.1) {
ds_inputs[i][0] = query_p[0];
ds_inputs[i][1] = query_p[1];
ds_inputs[i][2] = query_p[2];
ds_SDF[i][0] = SDF_P;
ds_SPF[i][0] = 10;
ds_SPF[i][1] = 10;
ds_SPF[i][2] = 10;
i++;
} else if (SDF_P > 0 && SDF_P < 0.1) {
ds_inputs[i][0] = query_p[0];
ds_inputs[i][1] = query_p[1];
ds_inputs[i][2] = query_p[2];
ds_SDF[i][0] = SDF_P;
ds_SPF[i][0] = peoject_q[0];
ds_SPF[i][1] = peoject_q[1];
ds_SPF[i][2] = peoject_q[2];
i++;
}
count = i;
if (i >= num_samples) {
break;
}
}
cout << "generated " << count << " samples." << endl;
SaveDatasetSDF(count, ds_inputs, ds_SDF, "dataset_SDF.txt");
SaveDatasetSPF(count, ds_inputs, ds_SPF, "dataset_SPF.txt");
delete[] ds_inputs;
delete[] ds_SDF;
delete[] ds_SPF;
}
Definition particle.hpp:26
Shape * shape
The shape of the particle.
Definition particle.hpp:45
bool need_update_stl_model
Flag indicating whether STl model intersection-based contact detection and resolution is needed.
Definition particle.hpp:207
virtual void SaveAsSTL(std::string const &filename)
Saves the shape instance as an STL file.
Definition shape.cpp:353
virtual double GetBoundSphereRadius() const
Return the inertia of the shape.
Definition shape.cpp:126
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
void Decimate(int num_nodes)
Decimate the TriMesh object.
Definition shape_trimesh.cpp:121
Vec3d SurfacePoint(Vec3d const &pos) override
Calculate the surface point on the TriMesh object nearest to a given position.
Definition shape_trimesh.cpp:517
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
VecXT< Vec3d > vertices
The vertices of the triangular mesh.
Definition shape_trimesh.hpp:28
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
std::array< double, 3 > Vec3d
Definition utils_macros.hpp:18