| 1 |
/**************************************************************************** |
| 2 |
* NanoPLY * |
| 3 |
* NanoPLY is a C++11 header-only library to read and write PLY file * |
| 4 |
* * |
| 5 |
* Copyright(C) 2014-2015 * |
| 6 |
* Visual Computing Lab * |
| 7 |
* ISTI - Italian National Research Council * |
| 8 |
* * |
| 9 |
* This Source Code Form is subject to the terms of the Mozilla Public * |
| 10 |
* License, v. 2.0. If a copy of the MPL was not distributed with this * |
| 11 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * |
| 12 |
* * |
| 13 |
****************************************************************************/ |
| 14 |
|
| 15 |
#include <iostream> |
| 16 |
#include <wrap/nanoply/include/nanoply.hpp> |
| 17 |
|
| 18 |
template<typename T, int N> |
| 19 |
struct Container |
| 20 |
{ |
| 21 |
|
| 22 |
public: |
| 23 |
T data[N]; |
| 24 |
|
| 25 |
Container(){} |
| 26 |
|
| 27 |
Container(T* temp, int n) |
| 28 |
{ |
| 29 |
for (int i = 0; i < std::min(n, N); i++) |
| 30 |
data[i] = temp[i]; |
| 31 |
} |
| 32 |
|
| 33 |
T* V() |
| 34 |
{ |
| 35 |
return data; |
| 36 |
} |
| 37 |
|
| 38 |
bool operator == (Container<T, N> const & m) const |
| 39 |
{ |
| 40 |
bool flag = true; |
| 41 |
for (int i = 0; i < N; i++) |
| 42 |
flag = flag && (data[i] == m.data[i]); |
| 43 |
return flag; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
typedef Container<float, 3> Point3f; |
| 49 |
typedef Container<unsigned char, 4> Color4f; |
| 50 |
typedef Container<int, 3> VertexIndex; |
| 51 |
|
| 52 |
struct MyVertexInfo |
| 53 |
{ |
| 54 |
Color4f c; |
| 55 |
float density; |
| 56 |
int materialId; |
| 57 |
|
| 58 |
bool operator == (MyVertexInfo const & m) const |
| 59 |
{ |
| 60 |
return (c == m.c && m.density == density && m.materialId == materialId); |
| 61 |
} |
| 62 |
|
| 63 |
}; |
| 64 |
|
| 65 |
struct MyMaterialInfo |
| 66 |
{ |
| 67 |
Point3f kd; |
| 68 |
Point3f ks; |
| 69 |
float rho; |
| 70 |
|
| 71 |
bool operator == (MyMaterialInfo const & m) const |
| 72 |
{ |
| 73 |
return (kd == m.kd && ks == m.ks && rho == m.rho); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
class MyMesh |
| 79 |
{ |
| 80 |
public: |
| 81 |
std::vector<Point3f> coordVec; |
| 82 |
std::vector<Point3f> normalVec; |
| 83 |
std::vector<MyVertexInfo> infoVec; |
| 84 |
std::vector<VertexIndex> faceIndex; |
| 85 |
std::vector<MyMaterialInfo> material; |
| 86 |
|
| 87 |
void FillMesh() |
| 88 |
{ |
| 89 |
float pos[] = { 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0 }; |
| 90 |
int index[] = { 0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 2, 1 }; |
| 91 |
float norm[] = { 0.57735, 0.57735, 0.57735, -0.57735, 0.57735, -0.57735, -0.57735, -0.57735, 0.57735, 0.57735, -0.57735, -0.57735 }; |
| 92 |
unsigned char color[] = { 68, 68, 68, 255, 177, 68, 177, 255, 177, 177, 68, 255, 68, 177, 177 }; |
| 93 |
float density[] = { 3.5, 2.0, 4.0, 3.0 }; |
| 94 |
int materialId[] = { 1, 0, -1, 1 }; |
| 95 |
float materialValue[] = { 0.2, 0.3, 0.2, 0.5, 0.5, 0.6, 20.0, 0.1, 0.1, 0.1, 0.7, 0.5, 0.4, 1.0 }; |
| 96 |
coordVec.push_back(Point3f(pos, 3)); coordVec.push_back(Point3f(&pos[3], 3)); coordVec.push_back(Point3f(&pos[6], 3)); coordVec.push_back(Point3f(&pos[9], 3)); |
| 97 |
normalVec.push_back(Point3f(norm, 3)); normalVec.push_back(Point3f(&norm[3], 3)); normalVec.push_back(Point3f(&norm[6], 3)); normalVec.push_back(Point3f(&norm[9], 3)); |
| 98 |
MyVertexInfo info1 = { Color4f(color, 4), density[0], materialId[0] }; infoVec.push_back(info1); |
| 99 |
MyVertexInfo info2 = { Color4f(&color[4], 4), density[1], materialId[1] }; infoVec.push_back(info2); |
| 100 |
MyVertexInfo info3 = { Color4f(&color[8], 4), density[2], materialId[2] }; infoVec.push_back(info3); |
| 101 |
MyVertexInfo info4 = { Color4f(&color[12], 4), density[3], materialId[3] }; infoVec.push_back(info4); |
| 102 |
faceIndex.push_back(VertexIndex(index, 3)); faceIndex.push_back(VertexIndex(&index[3], 3)); faceIndex.push_back(VertexIndex(&index[6], 3)); faceIndex.push_back(VertexIndex(&index[9], 3)); |
| 103 |
MyMaterialInfo mat1 = { Point3f(materialValue, 3), Point3f(&materialValue[3], 3), materialValue[6] }; material.push_back(mat1); |
| 104 |
MyMaterialInfo mat2 = { Point3f(&materialValue[7], 3), Point3f(&materialValue[10], 3), materialValue[13] }; material.push_back(mat2); |
| 105 |
} |
| 106 |
|
| 107 |
bool operator == (MyMesh& m) |
| 108 |
{ |
| 109 |
bool flag = (coordVec == m.coordVec); |
| 110 |
flag = flag && (normalVec == m.normalVec); |
| 111 |
flag = flag && (infoVec == m.infoVec); |
| 112 |
flag = flag && (faceIndex == m.faceIndex); |
| 113 |
flag = flag && (material == m.material); |
| 114 |
return flag; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
bool Load(const char* filename, MyMesh& mesh) |
| 120 |
{ |
| 121 |
//Get file info |
| 122 |
nanoply::Info info(filename); |
| 123 |
if (info.errInfo != nanoply::NNP_OK) |
| 124 |
{ |
| 125 |
std::cout << "Invalid file format" << std::endl; |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
//Resize the element containers |
| 130 |
int vertCnt = info.GetVertexCount(); |
| 131 |
if (vertCnt <= 0) |
| 132 |
{ |
| 133 |
std::cout << "The file does't contain any vertex." << std::endl; |
| 134 |
return false; |
| 135 |
} |
| 136 |
mesh.coordVec.resize(vertCnt); |
| 137 |
mesh.normalVec.resize(vertCnt); |
| 138 |
mesh.infoVec.resize(vertCnt); |
| 139 |
int faceCnt = info.GetFaceCount(); |
| 140 |
mesh.faceIndex.resize(faceCnt); |
| 141 |
size_t materialCnt = info.GetElementCount(std::string("material")); |
| 142 |
mesh.material.resize(2); |
| 143 |
|
| 144 |
//Create the vertex properties descriptor (what ply property and where to save its data) |
| 145 |
nanoply::ElementDescriptor vertex(nanoply::NNP_VERTEX_ELEM); |
| 146 |
if (vertCnt > 0) |
| 147 |
{ |
| 148 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_PXYZ, (*mesh.coordVec.begin()).V())); |
| 149 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_NXYZ, (*mesh.normalVec.begin()).V())); |
| 150 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 4, unsigned char>(nanoply::NNP_CRGBA, (*mesh.infoVec.begin()).c.V())); |
| 151 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, float>(nanoply::NNP_DENSITY, &(*mesh.infoVec.begin()).density)); |
| 152 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, int>(std::string("materialId"), &(*mesh.infoVec.begin()).materialId)); |
| 153 |
} |
| 154 |
|
| 155 |
//Create the face properties descriptor (what ply property and where the data is stored) |
| 156 |
nanoply::ElementDescriptor face(nanoply::NNP_FACE_ELEM); |
| 157 |
if (mesh.faceIndex.size() > 0) |
| 158 |
face.dataDescriptor.push_back(new nanoply::DataDescriptor<VertexIndex, 3, int>(nanoply::NNP_FACE_VERTEX_LIST, (*mesh.faceIndex.begin()).V())); |
| 159 |
|
| 160 |
//Create the material properties descriptor (what ply property and where the data is stored) |
| 161 |
nanoply::ElementDescriptor material(std::string("material")); |
| 162 |
if (mesh.material.size() > 0) |
| 163 |
{ |
| 164 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("kd"), (*mesh.material.begin()).kd.V())); |
| 165 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("ks"), (*mesh.material.begin()).ks.V())); |
| 166 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 1, float>(std::string("rho"), &(*mesh.material.begin()).rho)); |
| 167 |
} |
| 168 |
|
| 169 |
//Create the mesh descriptor |
| 170 |
std::vector<nanoply::ElementDescriptor*> meshDescr; |
| 171 |
meshDescr.push_back(&vertex); |
| 172 |
meshDescr.push_back(&face); |
| 173 |
meshDescr.push_back(&material); |
| 174 |
|
| 175 |
//Open the file and save the element data according the relative element descriptor |
| 176 |
OpenModel(info, meshDescr); |
| 177 |
for (int i = 0; i < vertex.dataDescriptor.size(); i++) |
| 178 |
delete vertex.dataDescriptor[i]; |
| 179 |
for (int i = 0; i < face.dataDescriptor.size(); i++) |
| 180 |
delete face.dataDescriptor[i]; |
| 181 |
for (int i = 0; i < material.dataDescriptor.size(); i++) |
| 182 |
delete material.dataDescriptor[i]; |
| 183 |
return (info.errInfo == nanoply::NNP_OK); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
bool Save(const char* filename, MyMesh& mesh, bool binary) |
| 189 |
{ |
| 190 |
//Create the vector of vertex properties to save in the file |
| 191 |
std::vector<nanoply::PlyProperty> vertexProp; |
| 192 |
vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_PXYZ)); |
| 193 |
vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_NXYZ)); |
| 194 |
vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_DENSITY)); |
| 195 |
vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_CRGBA)); |
| 196 |
vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_INT32, "materialId")); |
| 197 |
|
| 198 |
//Create the vector of face properties to save in the file |
| 199 |
std::vector<nanoply::PlyProperty> faceProp; |
| 200 |
faceProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_UINT32, nanoply::NNP_FACE_VERTEX_LIST)); |
| 201 |
|
| 202 |
//Create the vector of material properties to save in the file |
| 203 |
std::vector<nanoply::PlyProperty> materialProp; |
| 204 |
materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_FLOAT32, "kd")); |
| 205 |
materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_FLOAT32, "ks")); |
| 206 |
materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, "rho")); |
| 207 |
|
| 208 |
//Create the PlyElement |
| 209 |
nanoply::PlyElement vertexElem(nanoply::NNP_VERTEX_ELEM, vertexProp, mesh.coordVec.size()); |
| 210 |
nanoply::PlyElement faceElem(nanoply::NNP_FACE_ELEM, faceProp, mesh.faceIndex.size()); |
| 211 |
nanoply::PlyElement materialElem(std::string("material"), materialProp, mesh.material.size()); |
| 212 |
|
| 213 |
//Create the Info object with the data to save in the header |
| 214 |
nanoply::Info infoSave; |
| 215 |
infoSave.filename = filename; |
| 216 |
infoSave.binary = binary; |
| 217 |
infoSave.AddPlyElement(vertexElem); |
| 218 |
infoSave.AddPlyElement(faceElem); |
| 219 |
infoSave.AddPlyElement(materialElem); |
| 220 |
|
| 221 |
//Create the vertex properties descriptor (what ply property and where the data is stored) |
| 222 |
nanoply::ElementDescriptor vertex(nanoply::NNP_VERTEX_ELEM); |
| 223 |
if (mesh.coordVec.size() > 0) |
| 224 |
{ |
| 225 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_PXYZ, (*mesh.coordVec.begin()).V())); |
| 226 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_NXYZ, (*mesh.normalVec.begin()).V())); |
| 227 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 4, unsigned char>(nanoply::NNP_CRGBA, (*mesh.infoVec.begin()).c.V())); |
| 228 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, float>(nanoply::NNP_DENSITY, &(*mesh.infoVec.begin()).density)); |
| 229 |
vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, int>(std::string("materialId"), &(*mesh.infoVec.begin()).materialId)); |
| 230 |
} |
| 231 |
|
| 232 |
//Create the face properties descriptor (what ply property and where the data is stored) |
| 233 |
nanoply::ElementDescriptor face(nanoply::NNP_FACE_ELEM); |
| 234 |
if (mesh.faceIndex.size() > 0) |
| 235 |
face.dataDescriptor.push_back(new nanoply::DataDescriptor<VertexIndex, 3, int>(nanoply::NNP_FACE_VERTEX_LIST, (*mesh.faceIndex.begin()).V())); |
| 236 |
|
| 237 |
//Create the material properties descriptor (what ply property and where the data is stored) |
| 238 |
nanoply::ElementDescriptor material(std::string("material")); |
| 239 |
if (mesh.material.size() > 0) |
| 240 |
{ |
| 241 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("kd"), (*mesh.material.begin()).kd.V())); |
| 242 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("ks"), (*mesh.material.begin()).ks.V())); |
| 243 |
material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 1, float>(std::string("rho"), &(*mesh.material.begin()).rho)); |
| 244 |
} |
| 245 |
|
| 246 |
//Create the mesh descriptor |
| 247 |
std::vector<nanoply::ElementDescriptor*> meshDescr; |
| 248 |
meshDescr.push_back(&vertex); |
| 249 |
meshDescr.push_back(&face); |
| 250 |
meshDescr.push_back(&material); |
| 251 |
|
| 252 |
//Save the file |
| 253 |
bool result = nanoply::SaveModel(infoSave.filename, meshDescr, infoSave); |
| 254 |
|
| 255 |
for (int i = 0; i < vertex.dataDescriptor.size(); i++) |
| 256 |
delete vertex.dataDescriptor[i]; |
| 257 |
for (int i = 0; i < face.dataDescriptor.size(); i++) |
| 258 |
delete face.dataDescriptor[i]; |
| 259 |
for (int i = 0; i < material.dataDescriptor.size(); i++) |
| 260 |
delete material.dataDescriptor[i]; |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
int main() |
| 267 |
{ |
| 268 |
MyMesh mesh1; |
| 269 |
mesh1.FillMesh(); |
| 270 |
Save("example_ascii.ply", mesh1, false); |
| 271 |
Save("example_binary.ply", mesh1, true); |
| 272 |
MyMesh mesh2, mesh3; |
| 273 |
Load("example_ascii.ply", mesh2); |
| 274 |
Load("example_binary.ply", mesh3); |
| 275 |
if (mesh2 == mesh1) |
| 276 |
std::cout << "Write and read ASCII ply file: SUCCESS\n"; |
| 277 |
else |
| 278 |
std::cout << "Write and read ASCII ply file: FAIL\n"; |
| 279 |
if (mesh3 == mesh1) |
| 280 |
std::cout << "Write and read binary ply file: SUCCESS\n"; |
| 281 |
else |
| 282 |
std::cout << "Write and read binary ply file: FAIL\n"; |
| 283 |
return 0; |
| 284 |
} |