This is an example of how to use the netdem library.
#include <iostream>
#include <unordered_map>
using namespace std;
void TestCodedShape() {
vector<string> template_names;
ifstream file("template_dict.txt");
if (file.is_open()) {
string line;
while (getline(file, line)) {
template_names.push_back(line);
}
file.close();
} else {
cout << "file template_dict.txt not exist" << endl;
return;
}
int num_templates = template_names.size();
arma::mat latent_code;
mlpack::data::Load("latent_code.txt", latent_code, false, false);
int num_latent_code = latent_code.n_cols;
model.
Load(
"ann_SDF.xml",
"SDF");
cout << "finished loading ann model ..." << endl;
auto searching_points = GoldenSpiralSampler::GetInstance().Get(2000);
IGLWrapper::ConvexHull(searching_points, &(stl_model.
vertices),
int num_samples = searching_points.size();
VecXd sdf_list(num_samples);
for (int ii = 0; ii < num_templates; ii++) {
for (int jj = 0; jj < num_samples; jj++) {
auto search_point = searching_points[jj] * 0.5;
auto surf_point = stl_model.
vertices[jj] * 0.5;
for (int iter = 0; iter < 100; iter++) {
arma::mat test_x_coded(num_latent_code + surf_point.size(), 1);
for (int i = 0; i < num_latent_code; i++) {
test_x_coded(i, 0) = latent_code(ii, i);
}
for (int i = num_latent_code; i < test_x_coded.n_rows; i++) {
test_x_coded(i, 0) = surf_point[i - num_latent_code];
}
auto sdf = model.
Predict(test_x_coded);
sdf_list[jj] = sdf(0, 0);
if (abs(sdf(0, 0)) > 0.001) {
surf_point[0] += sdf(0, 0) * search_point[0];
surf_point[1] += sdf(0, 0) * search_point[1];
surf_point[2] += sdf(0, 0) * search_point[2];
} else {
break;
}
}
}
stl_model.
SaveAsSTL(template_names[ii] +
"/shape_netsdf.stl");
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 << stl_model.
vertices[i][i_inputs] <<
", ";
}
os.width(os_width);
os << sdf_list[i] << endl;
}
string filename = template_names[ii] + "/surface_points.txt";
ofstream outfile;
outfile.open(filename);
if (!outfile) {
cout << "cannot open file: " << filename << endl;
}
outfile << buf.str();
outfile.close();
cout << "data saved to: " << filename << endl;
}
}
A class that represents a feedforward neural network for regression.
Definition regression_net.hpp:21
void Load(std::string const &filename, std::string const &label)
Loads the neural network model from disk.
Definition regression_net.cpp:95
arma::mat Predict(const arma::mat &data_x)
Predicts with the neural network model using input data.
Definition regression_net.cpp:62
Class for working with STL models.
Definition stl_model.hpp:17
VecXT< Vec3i > facets
A M by 3 matrix. Each row defines a facet, with the row elements being the indices of the vertices.
Definition stl_model.hpp:28
VecXT< Vec3d > vertices
A N by 3 matrix that defines the points on the 3D model surface.
Definition stl_model.hpp:22
void SaveAsSTL(std::string const &file) const
Save the model as an STL file.
Definition stl_model.cpp:205
Definition bond_entry.hpp:7
std::vector< double > VecXd
Definition utils_macros.hpp:29