| 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 <wrap/nanoply/include/nanoplyWrapper.hpp> |
| 16 |
|
| 17 |
#include <vcg/complex/complex.h> |
| 18 |
#include <vcg/complex/algorithms/create/platonic.h> |
| 19 |
#include <vcg/complex/algorithms/update/normal.h> |
| 20 |
|
| 21 |
struct Material |
| 22 |
{ |
| 23 |
vcg::Point3f kd; |
| 24 |
vcg::Point3f ks; |
| 25 |
float rho; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
class MyVertex; |
| 30 |
class MyFace; |
| 31 |
|
| 32 |
class MyUsedTypes : public vcg::UsedTypes < vcg::Use< MyVertex >::AsVertexType, vcg::Use< MyFace >::AsFaceType>{}; |
| 33 |
class MyVertex : public vcg::Vertex < MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::Color4b, vcg::vertex::Radiusf, vcg::vertex::BitFlags>{}; |
| 34 |
class MyFace : public vcg::Face < MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags>{}; |
| 35 |
|
| 36 |
class MyMesh : public vcg::tri::TriMesh < std::vector< MyVertex >, std::vector< MyFace > > |
| 37 |
{ |
| 38 |
public: |
| 39 |
MyMesh::PerVertexAttributeHandle<int> vertexMaterial; |
| 40 |
MyMesh::PerFaceAttributeHandle<MyVertex::CoordType> faceBarycenter; |
| 41 |
MyMesh::PerMeshAttributeHandle<std::vector<Material>> material; |
| 42 |
|
| 43 |
MyMesh() |
| 44 |
{ |
| 45 |
vertexMaterial = vcg::tri::Allocator<MyMesh>::AddPerVertexAttribute<int>(*this, std::string("materialId")); |
| 46 |
faceBarycenter = vcg::tri::Allocator<MyMesh>::AddPerFaceAttribute<MyVertex::CoordType>(*this, std::string("barycenter")); |
| 47 |
material = vcg::tri::Allocator<MyMesh>::AddPerMeshAttribute<std::vector<Material> >(*this, std::string("material")); |
| 48 |
} |
| 49 |
|
| 50 |
void FillMesh() |
| 51 |
{ |
| 52 |
vcg::tri::Icosahedron(*this); |
| 53 |
vcg::tri::UpdateNormal<MyMesh>::PerFaceNormalized(*this); |
| 54 |
vcg::tri::UpdateNormal<MyMesh>::PerVertexNormalized(*this); |
| 55 |
|
| 56 |
int tempC = 255 / vert.size(); |
| 57 |
for (size_t i = 0; i < vert.size(); i++) |
| 58 |
{ |
| 59 |
if (i < 2) |
| 60 |
vertexMaterial[i] = -1; |
| 61 |
else if (i < vert.size() / 2) |
| 62 |
vertexMaterial[i] = 0; |
| 63 |
else |
| 64 |
vertexMaterial[i] = 1; |
| 65 |
vert[i].R() = i*i / 2.0f; |
| 66 |
vert[i].C() = vcg::Color4b(tempC*i, tempC*i, tempC*i, 255); |
| 67 |
} |
| 68 |
|
| 69 |
for (size_t i = 0; i < face.size(); i++) |
| 70 |
faceBarycenter[i] = vcg::Barycenter(face[i]); |
| 71 |
|
| 72 |
material().resize(2); |
| 73 |
material()[0] = { vcg::Point3f(0.1f, 0.2f, 0.3f), vcg::Point3f(0.3f, 0.3f, 0.3f), 5.0f }; |
| 74 |
material()[1] = { vcg::Point3f(0.1f, 0.1f, 0.1f), vcg::Point3f(0.5f, 0.3f, 0.4f), 50.0f }; |
| 75 |
} |
| 76 |
|
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
bool Load(const char* filename, MyMesh& mesh) |
| 81 |
{ |
| 82 |
//Create the data descriport for the custom attributes |
| 83 |
nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib; |
| 84 |
customAttrib.GetMeshAttrib(filename); |
| 85 |
int count = customAttrib.meshAttribCnt["material"]; |
| 86 |
mesh.material().resize(count); |
| 87 |
customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, NULL); |
| 88 |
customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, NULL); |
| 89 |
if (count > 0) |
| 90 |
{ |
| 91 |
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V()); |
| 92 |
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V()); |
| 93 |
customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); |
| 94 |
} |
| 95 |
|
| 96 |
//Load the ply file |
| 97 |
unsigned int mask = 0; |
| 98 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOORD; |
| 99 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTNORMAL; |
| 100 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOLOR; |
| 101 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTRADIUS; |
| 102 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTATTRIB; |
| 103 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEINDEX; |
| 104 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACENORMAL; |
| 105 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEATTRIB; |
| 106 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_MESHATTRIB; |
| 107 |
return (nanoply::NanoPlyWrapper<MyMesh>::LoadModel(filename, mesh, mask, customAttrib) != 0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
bool Save(const char* filename, MyMesh& mesh, bool binary) |
| 113 |
{ |
| 114 |
//Create the data descriport for the custom attributes |
| 115 |
nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib; |
| 116 |
customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, &mesh.vertexMaterial[0]); |
| 117 |
customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.faceBarycenter[0].V()); |
| 118 |
if (mesh.material().size() > 0) |
| 119 |
{ |
| 120 |
customAttrib.AddMeshAttrib(std::string("material"), mesh.material().size()); |
| 121 |
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V()); |
| 122 |
customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V()); |
| 123 |
customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho); |
| 124 |
} |
| 125 |
|
| 126 |
//Save the ply file |
| 127 |
unsigned int mask = 0; |
| 128 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOORD; |
| 129 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTNORMAL; |
| 130 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOLOR; |
| 131 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTRADIUS; |
| 132 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTATTRIB; |
| 133 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEINDEX; |
| 134 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACENORMAL; |
| 135 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEATTRIB; |
| 136 |
mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_MESHATTRIB; |
| 137 |
return nanoply::NanoPlyWrapper<MyMesh>::SaveModel(filename, mesh, mask, customAttrib, binary); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
int main() |
| 143 |
{ |
| 144 |
MyMesh mesh1; |
| 145 |
mesh1.FillMesh(); |
| 146 |
Save("example_ascii.ply", mesh1, false); |
| 147 |
Save("example_binary.ply", mesh1, true); |
| 148 |
MyMesh mesh2, mesh3; |
| 149 |
Load("example_ascii.ply", mesh2); |
| 150 |
Load("example_binary.ply", mesh3); |
| 151 |
Save("example_ascii_1.ply", mesh2, false); |
| 152 |
Save("example_binary_1.ply", mesh3, true); |
| 153 |
return 0; |
| 154 |
} |