00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iostream>
00016 #include <wrap/nanoply/include/nanoplyWrapper.hpp>
00017
00018 #include <vcg/complex/complex.h>
00019 #include <vcg/complex/algorithms/create/platonic.h>
00020 #include <vcg/complex/algorithms/update/normal.h>
00021
00022 struct Material
00023 {
00024 vcg::Point3f kd;
00025 vcg::Point3f ks;
00026 float rho;
00027 };
00028
00029
00030 class MyVertex;
00031 class MyFace;
00032
00033 class MyUsedTypes : public vcg::UsedTypes < vcg::Use< MyVertex >::AsVertexType, vcg::Use< MyFace >::AsFaceType>{};
00034 class MyVertex : public vcg::Vertex < MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::Color4b, vcg::vertex::Radiusf, vcg::vertex::BitFlags>{};
00035 class MyFace : public vcg::Face < MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags>{};
00036
00037 class MyMesh : public vcg::tri::TriMesh < std::vector< MyVertex >, std::vector< MyFace > >
00038 {
00039 public:
00040 MyMesh::PerVertexAttributeHandle<int> vertexMaterial;
00041 MyMesh::PerFaceAttributeHandle<MyVertex::CoordType> faceBarycenter;
00042 MyMesh::PerMeshAttributeHandle<std::vector<Material>> material;
00043
00044 MyMesh()
00045 {
00046 vertexMaterial = vcg::tri::Allocator<MyMesh>::AddPerVertexAttribute<int>(*this, std::string("materialId"));
00047 faceBarycenter = vcg::tri::Allocator<MyMesh>::AddPerFaceAttribute<MyVertex::CoordType>(*this, std::string("barycenter"));
00048 material = vcg::tri::Allocator<MyMesh>::AddPerMeshAttribute<std::vector<Material>>(*this, std::string("material"));
00049 }
00050
00051 void FillMesh()
00052 {
00053 vcg::tri::Icosahedron(*this);
00054 vcg::tri::UpdateNormal<MyMesh>::PerFaceNormalized(*this);
00055 vcg::tri::UpdateNormal<MyMesh>::PerVertexNormalized(*this);
00056
00057 int tempC = 255 / vert.size();
00058 for (int i = 0; i < vert.size(); i++)
00059 {
00060 if (i < 2)
00061 vertexMaterial[i] = -1;
00062 else if (i < vert.size() / 2)
00063 vertexMaterial[i] = 0;
00064 else
00065 vertexMaterial[i] = 1;
00066 vert[i].R() = i*i / 2.0f;
00067 vert[i].C() = vcg::Color4b(tempC*i, tempC*i, tempC*i, 255);
00068 }
00069
00070 for (int i = 0; i < face.size(); i++)
00071 faceBarycenter[i] = vcg::Barycenter(face[i]);
00072
00073 material().resize(2);
00074 material()[0] = { vcg::Point3f(0.1, 0.2, 0.3), vcg::Point3f(0.3, 0.3, 0.3), 5.0 };
00075 material()[1] = { vcg::Point3f(0.1, 0.1, 0.1), vcg::Point3f(0.5, 0.3, 0.4), 50.0 };
00076 }
00077
00078 };
00079
00080
00081 bool Load(const char* filename, MyMesh& mesh)
00082 {
00083
00084 nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib;
00085 customAttrib.GetMeshAttrib(filename);
00086 int count = customAttrib.meshAttribCnt["material"];
00087 mesh.material().resize(count);
00088 customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, NULL);
00089 customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, NULL);
00090 customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_FLOAT32, mesh.material()[0].kd.V());
00091 customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_FLOAT32, mesh.material()[0].ks.V());
00092 customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
00093
00094
00095 unsigned int mask = 0;
00096 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOORD;
00097 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTNORMAL;
00098 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOLOR;
00099 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTRADIUS;
00100 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTATTRIB;
00101 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEINDEX;
00102 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACENORMAL;
00103 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEATTRIB;
00104 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_MESHATTRIB;
00105 return (nanoply::NanoPlyWrapper<MyMesh>::LoadModel(filename, mesh, mask, customAttrib) != 0);
00106 }
00107
00108
00109
00110 bool Save(const char* filename, MyMesh& mesh, bool binary)
00111 {
00112
00113 nanoply::NanoPlyWrapper<MyMesh>::CustomAttributeDescriptor customAttrib;
00114 customAttrib.AddVertexAttribDescriptor<int, int, 1>(std::string("materialId"), nanoply::NNP_INT32, &mesh.vertexMaterial[0]);
00115 customAttrib.AddFaceAttribDescriptor<vcg::Point3f, float, 3>(std::string("barycenter"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.faceBarycenter[0].V());
00116 customAttrib.AddMeshAttrib(std::string("material"), 2);
00117 customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("kd"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].kd.V());
00118 customAttrib.AddMeshAttribDescriptor<Material, float, 3>(std::string("material"), std::string("ks"), nanoply::NNP_LIST_UINT8_FLOAT32, mesh.material()[0].ks.V());
00119 customAttrib.AddMeshAttribDescriptor<Material, float, 1>(std::string("material"), std::string("rho"), nanoply::NNP_FLOAT32, &mesh.material()[0].rho);
00120
00121
00122 unsigned int mask = 0;
00123 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOORD;
00124 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTNORMAL;
00125 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTCOLOR;
00126 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTRADIUS;
00127 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_VERTATTRIB;
00128 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEINDEX;
00129 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACENORMAL;
00130 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_FACEATTRIB;
00131 mask |= nanoply::NanoPlyWrapper<MyMesh>::IO_MESHATTRIB;
00132 return nanoply::NanoPlyWrapper<MyMesh>::SaveModel(filename, mesh, mask, customAttrib, binary);
00133 }
00134
00135
00136
00137 int main()
00138 {
00139 MyMesh mesh1;
00140 mesh1.FillMesh();
00141 Save("example_ascii.ply", mesh1, false);
00142 Save("example_binary.ply", mesh1, true);
00143 MyMesh mesh2, mesh3;
00144 Load("example_ascii.ply", mesh2);
00145 Load("example_binary.ply", mesh3);
00146 return true;
00147 }