Skip to content

Commit

Permalink
Added binary only PLY support
Browse files Browse the repository at this point in the history
  • Loading branch information
hochshi committed Jun 17, 2024
1 parent 7311e17 commit d304dab
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ if(ENABLE_PLY)
${tinyply_SOURCE_DIR}/source/tinyply.cpp
${tinyply_SOURCE_DIR}/source/tinyply.h
)
include_directories(${tinyply_SOURCE_DIR}/source)
endif()

add_definitions(-DPLY_ENABLED)
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct Configuration {
bool vaFlag = false;
bool computeNormals = false;
bool saveMSMS = false;
bool savePLY = false;
double sternLayer = -1.;
int Max_Atoms_Multi_Grid = 100;
std::string surfName;
Expand All @@ -110,7 +111,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
maxMeshDim2D, maxMeshPatches2D, NumMSMSfiles, skin_s, maxSkinDim,
maxSkinPatches, maxSkinDim2D, maxSkinPatches2D, useFastProjection,
savePovRay, checkDuplicatedVertices, wellShaped, probeRadius, lb, vaFlag,
computeNormals, saveMSMS, sternLayer, Max_Atoms_Multi_Grid, surfName)
computeNormals, saveMSMS, savePLY, sternLayer, Max_Atoms_Multi_Grid, surfName)
#endif

using ConfigurationOP = std::shared_ptr<Configuration>;
Expand Down
103 changes: 91 additions & 12 deletions src/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
#include <Surface.h>
#include <logging.h>
#include <array>
#include <cstdint>
#include <ostream>
#include <stdexcept>
#include <streambuf>
#include <tuple>
#include <vector>

#ifdef PLY_ENABLED
#include <tinyply.h>
#endif // PLY_ENABLED

namespace nanoshaper {

void Surface::init() {
Expand Down Expand Up @@ -53,6 +60,7 @@ void Surface::init() {
vertexAtomsMapFlag = false;
computeNormals = false;
saveMSMS = false;
savePLY = false;
providesAnalyticalNormals = false;
activeCubes = NULL;
MAX_ATOMS_MULTI_GRID = 100;
Expand All @@ -68,6 +76,7 @@ void Surface::init(ConfigurationOP cf) {
bool vaFlag = cf->vaFlag;
bool computeNormals = cf->computeNormals;
bool saveMSMS = cf->saveMSMS;
bool savePLY = cf->savePLY;
double sternLayer = cf->sternLayer;
MAX_ATOMS_MULTI_GRID = cf->Max_Atoms_Multi_Grid;

Expand All @@ -80,6 +89,7 @@ void Surface::init(ConfigurationOP cf) {
setVertexAtomsMap(vaFlag);
setComputeNormals(computeNormals);
setSaveMSMS(saveMSMS);
setSavePLY(savePLY);

// if >0 enable stern layer, else disabled by default
if (sternLayer > 0)
Expand Down Expand Up @@ -4405,13 +4415,78 @@ void Surface::approximateNormals(vector<int>& appNormals, bool doOnlyList) {

deleteMatrix2D<double>(nt, planes);
}

bool Surface::savePLYMesh(int format, bool revert, const char* fileName,
vector<double*>& vertList, vector<int*>& triList,
vector<double*>& normalsList) {
int numVertexes = (int)vertList.size();
int numTriangles = (int)triList.size();
char fullName[100];

snprintf(fullName, sizeof(fullName), "%s.ply", fileName);

std::filebuf fb;
fb.open(fullName, std::ios::out | std::ios::binary);
std::ostream outstream(&fb);
if (outstream.fail()) {
logging::log<logging::level::warn>("Cannot write file {}", fileName);
return false;
}

struct double3 { double x, y, z; };
struct int3 { int32_t x, y, z; };

std::vector<double3> vertStructVec;
for (double* ptr : vertList) {
vertStructVec.push_back(*reinterpret_cast<double3*>(ptr));
}
std::vector<int3> triStructVec;
for (int* ptr : triList) {
triStructVec.push_back(*reinterpret_cast<int3*>(ptr));
}

tinyply::PlyFile mesh_ply;
mesh_ply.add_properties_to_element("vertex", {"x", "y", "z"} ,
tinyply::Type::FLOAT64, numVertexes,
reinterpret_cast<uint8_t *>(vertStructVec.data()),
tinyply::Type::INVALID, 0);
mesh_ply.add_properties_to_element("face", { "vertex_indices" },
tinyply::Type::INT32, numTriangles,
reinterpret_cast<uint8_t *>(triStructVec.data()),
tinyply::Type::UINT8, 3);
if (normalsList.size() != 0) {
std::vector<double3> normalsStructVec;
for (double* ptr : vertList) {
normalsStructVec.push_back(*reinterpret_cast<double3*>(ptr));
}
mesh_ply.add_properties_to_element("vertex", { "nx", "ny", "nz" },
tinyply::Type::FLOAT64, numVertexes,
reinterpret_cast<uint8_t*>(normalsStructVec.data()),
tinyply::Type::INVALID, 0);
}


char comment[100];
time_t pt;
time(&pt);
std::snprintf(comment, sizeof(comment), "# File created by %s version %s date %s", PROGNAME, VERSION, ctime(&pt));
mesh_ply.get_comments().push_back(comment);

mesh_ply.write(outstream, true);
return true;
}

bool Surface::saveMesh(int format, bool revert, const char* fileName,
vector<double*>& vertList, vector<int*>& triList,
vector<double*>& normalsList) {
int numVertexes = (int)vertList.size();
int numTriangles = (int)triList.size();
char fullName[100];

if (format == PLY) {
return savePLYMesh(format, revert, fileName, vertList, triList, normalsList);
}

if (format == OFF || format == OFF_A || format == OFF_N ||
format == OFF_N_A) {
// save all in OFF format
Expand Down Expand Up @@ -5204,22 +5279,26 @@ void Surface::smoothSurface(const char* fn, bool revert) {

int Surface::deduceFormat() {
int format = -1;
if (!saveMSMS) {
if (vertexAtomsMapFlag && computeNormals)
format = OFF_N_A;
else {
if (vertexAtomsMapFlag)
format = OFF_A;
else if (computeNormals)
format = OFF_N;
else
format = OFF;
}
} else {
if (savePLY) {
format = PLY;
return format;
}
if (saveMSMS) {
if (vertexAtomsMapFlag)
format = MSMS;
else
format = MSMS_NO_A;
return format;
}
if (vertexAtomsMapFlag && computeNormals)
format = OFF_N_A;
else {
if (vertexAtomsMapFlag)
format = OFF_A;
else if (computeNormals)
format = OFF_N;
else
format = OFF;
}
return format;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ enum FileFormat : int {
OFF_N_A = 3,
MSMS_NO_A = 4,
MSMS = 5,
PLY = 6,
N_FORMATS
};
// #define DEDUCE -1
Expand Down Expand Up @@ -254,6 +255,7 @@ class Surface {
bool useLoadBalancing;
bool vertexAtomsMapFlag;
bool saveMSMS;
bool savePLY;
//////////////////////////////////////////////////////////////////////////////////////

// current panel under analysis
Expand Down Expand Up @@ -562,6 +564,10 @@ class Surface {
virtual bool saveMesh(const char* filename, bool revert = false,
int format = FileFormat::DEDUCE);

virtual bool savePLYMesh(int format, bool revert, const char* fileName,
vector<double*>& vertList, vector<int*>& triList,
vector<double*>& normalsList);

/** smooth a given mesh and overwrites the given file name.
The input/output mesh is in .off format*/
virtual void smoothSurface(const char* fn = "triangulatedSurf",
Expand Down Expand Up @@ -645,6 +651,10 @@ class Surface {

bool getSaveMSMS() { return saveMSMS; }

void setSavePLY(bool m) { savePLY = m; }

bool getSavePLY() { return savePLY; }

void setTriangulationFlag(bool flag) { accurateTriangulation = flag; }
bool getTriangulationFlag() { return accurateTriangulation; }

Expand Down
1 change: 1 addition & 0 deletions src/main_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ ConfigurationOP parse(ConfigFileOP cf) {
conf->vaFlag = cf->read<bool>("Vertex_Atom_Info", false);
conf->computeNormals = cf->read<bool>("Compute_Vertex_Normals", false);
conf->saveMSMS = cf->read<bool>("Save_Mesh_MSMS_Format", false);
conf->savePLY = cf->read<bool>("Save_Mesh_PLY_Format", false);
conf->sternLayer = cf->read<double>("Stern_layer", -1.);
conf->Max_Atoms_Multi_Grid = cf->read<int>("Max_Atoms_Multi_Grid", 100);
conf->surfName = cf->read<string>("Surface");
Expand Down

0 comments on commit d304dab

Please sign in to comment.