File: main.cpp

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (284 lines) | stat: -rw-r--r-- 12,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/****************************************************************************
* NanoPLY                                                                   *
* NanoPLY is a C++11 header-only library to read and write PLY file         *
*                                                                           *
* Copyright(C) 2014-2015                                                    *
* Visual Computing Lab                                                      *
* ISTI - Italian National Research Council                                  *
*                                                                           *
* This Source Code Form is subject to the terms of the Mozilla Public       *
* License, v. 2.0. If a copy of the MPL was not distributed with this       *
* file, You can obtain one at http://mozilla.org/MPL/2.0/.                  *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <nanoply.hpp>

template<typename T, int N>
struct Container
{

public:
  T data[N];

  Container(){}

  Container(T* temp, int n)
  {
    for (int i = 0; i < std::min(n, N); i++)
      data[i] = temp[i];
  }

  T* V()
  {
    return data;
  }

  bool operator == (Container<T, N> const & m) const
  {
    bool flag = true;
    for (int i = 0; i < N; i++)
      flag = flag && (data[i] == m.data[i]);
    return flag;
  }
};


typedef Container<float, 3> Point3f;
typedef Container<unsigned char, 4> Color4f;
typedef Container<int, 3> VertexIndex;

struct MyVertexInfo
{
  Color4f c;
  float density;
  int materialId;

  bool operator == (MyVertexInfo const & m) const
  {
    return (c == m.c && m.density == density && m.materialId == materialId);
  }

};

struct MyMaterialInfo
{
  Point3f kd;
  Point3f ks;
  float rho;

  bool operator == (MyMaterialInfo const & m) const
  {
    return (kd == m.kd && ks == m.ks && rho == m.rho);
  }
};


class MyMesh
{
public:
  std::vector<Point3f> coordVec;
  std::vector<Point3f> normalVec;
  std::vector<MyVertexInfo> infoVec;
  std::vector<VertexIndex> faceIndex;
  std::vector<MyMaterialInfo> material;

  void FillMesh()
  {
    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 };
    int index[] = { 0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 2, 1 };
    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 };
    unsigned char color[] = { 68, 68, 68, 255, 177, 68, 177, 255, 177, 177, 68, 255, 68, 177, 177 };
    float density[] = { 3.5, 2.0, 4.0, 3.0 };
    int materialId[] = { 1, 0, -1, 1 };
    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 };
    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));
    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));
    MyVertexInfo info1 = { Color4f(color, 4), density[0], materialId[0] }; infoVec.push_back(info1);
    MyVertexInfo info2 = { Color4f(&color[4], 4), density[1], materialId[1] }; infoVec.push_back(info2);
    MyVertexInfo info3 = { Color4f(&color[8], 4), density[2], materialId[2] }; infoVec.push_back(info3);
    MyVertexInfo info4 = { Color4f(&color[12], 4), density[3], materialId[3] }; infoVec.push_back(info4);
    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));
    MyMaterialInfo mat1 = { Point3f(materialValue, 3), Point3f(&materialValue[3], 3), materialValue[6] }; material.push_back(mat1);
    MyMaterialInfo mat2 = { Point3f(&materialValue[7], 3), Point3f(&materialValue[10], 3), materialValue[13] }; material.push_back(mat2);
  }

  bool operator == (MyMesh& m)
  {
    bool flag = (coordVec == m.coordVec);
    flag = flag && (normalVec == m.normalVec);
    flag = flag && (infoVec == m.infoVec);
    flag = flag && (faceIndex == m.faceIndex);
    flag = flag && (material == m.material);
    return flag;
  }
};


bool Load(const char* filename, MyMesh& mesh)
{
  //Get file info
  nanoply::Info info(filename);
  if (info.errInfo != nanoply::NNP_OK)
  {
    std::cout << "Invalid file format" << std::endl;
    return false;
  }

  //Resize the element containers
  int vertCnt = info.GetVertexCount();
  if (vertCnt <= 0)
  {
    std::cout << "The file does't contain any vertex." << std::endl;
    return false;
  }
  mesh.coordVec.resize(vertCnt);
  mesh.normalVec.resize(vertCnt);
  mesh.infoVec.resize(vertCnt);
  int faceCnt = info.GetFaceCount();
  mesh.faceIndex.resize(faceCnt);
  size_t materialCnt = info.GetElementCount(std::string("material"));
  mesh.material.resize(2);

  //Create the vertex properties descriptor (what ply property and where to save its data)
  nanoply::ElementDescriptor vertex(nanoply::NNP_VERTEX_ELEM);
  if (vertCnt > 0)
  {
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_PXYZ, (*mesh.coordVec.begin()).V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_NXYZ, (*mesh.normalVec.begin()).V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 4, unsigned char>(nanoply::NNP_CRGBA, (*mesh.infoVec.begin()).c.V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, float>(nanoply::NNP_DENSITY, &(*mesh.infoVec.begin()).density));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, int>(std::string("materialId"), &(*mesh.infoVec.begin()).materialId));
  }

  //Create the face properties descriptor (what ply property and where the data is stored)
  nanoply::ElementDescriptor face(nanoply::NNP_FACE_ELEM);
  if (mesh.faceIndex.size() > 0)
    face.dataDescriptor.push_back(new nanoply::DataDescriptor<VertexIndex, 3, int>(nanoply::NNP_FACE_VERTEX_LIST, (*mesh.faceIndex.begin()).V()));

  //Create the material properties descriptor (what ply property and where the data is stored)
  nanoply::ElementDescriptor material(std::string("material"));
  if (mesh.material.size() > 0)
  {
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("kd"), (*mesh.material.begin()).kd.V()));
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("ks"), (*mesh.material.begin()).ks.V()));
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 1, float>(std::string("rho"), &(*mesh.material.begin()).rho));
  }

  //Create the mesh descriptor
  std::vector<nanoply::ElementDescriptor*> meshDescr;
  meshDescr.push_back(&vertex);
  meshDescr.push_back(&face);
  meshDescr.push_back(&material);

  //Open the file and save the element data according the relative element descriptor
  OpenModel(info, meshDescr);
  for (int i = 0; i < vertex.dataDescriptor.size(); i++)
    delete vertex.dataDescriptor[i];
  for (int i = 0; i < face.dataDescriptor.size(); i++)
    delete face.dataDescriptor[i];
  for (int i = 0; i < material.dataDescriptor.size(); i++)
    delete material.dataDescriptor[i];
  return (info.errInfo == nanoply::NNP_OK);
}



bool Save(const char* filename, MyMesh& mesh, bool binary)
{
  //Create the vector of vertex properties to save in the file
  std::vector<nanoply::PlyProperty> vertexProp;
  vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_PXYZ));
  vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_NXYZ));
  vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_DENSITY));
  vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, nanoply::NNP_CRGBA));
  vertexProp.push_back(nanoply::PlyProperty(nanoply::NNP_INT32, "materialId"));

  //Create the vector of face properties to save in the file
  std::vector<nanoply::PlyProperty> faceProp;
  faceProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_UINT32, nanoply::NNP_FACE_VERTEX_LIST));

  //Create the vector of material properties to save in the file
  std::vector<nanoply::PlyProperty> materialProp;
  materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_FLOAT32, "kd"));
  materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_LIST_UINT8_FLOAT32, "ks"));
  materialProp.push_back(nanoply::PlyProperty(nanoply::NNP_FLOAT32, "rho"));

  //Create the PlyElement
  nanoply::PlyElement vertexElem(nanoply::NNP_VERTEX_ELEM, vertexProp, mesh.coordVec.size());
  nanoply::PlyElement faceElem(nanoply::NNP_FACE_ELEM, faceProp, mesh.faceIndex.size());
  nanoply::PlyElement materialElem(std::string("material"), materialProp, mesh.material.size());

  //Create the Info object with the data to save in the header
  nanoply::Info infoSave;
  infoSave.filename = filename;
  infoSave.binary = binary;
  infoSave.AddPlyElement(vertexElem);
  infoSave.AddPlyElement(faceElem);
  infoSave.AddPlyElement(materialElem);

  //Create the vertex properties descriptor (what ply property and where the data is stored)
  nanoply::ElementDescriptor vertex(nanoply::NNP_VERTEX_ELEM);
  if (mesh.coordVec.size() > 0)
  {
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_PXYZ, (*mesh.coordVec.begin()).V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<Point3f, 3, float>(nanoply::NNP_NXYZ, (*mesh.normalVec.begin()).V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 4, unsigned char>(nanoply::NNP_CRGBA, (*mesh.infoVec.begin()).c.V()));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, float>(nanoply::NNP_DENSITY, &(*mesh.infoVec.begin()).density));
    vertex.dataDescriptor.push_back(new nanoply::DataDescriptor<MyVertexInfo, 1, int>(std::string("materialId"), &(*mesh.infoVec.begin()).materialId));
  }

  //Create the face properties descriptor (what ply property and where the data is stored)
  nanoply::ElementDescriptor face(nanoply::NNP_FACE_ELEM);
  if (mesh.faceIndex.size() > 0)
    face.dataDescriptor.push_back(new nanoply::DataDescriptor<VertexIndex, 3, int>(nanoply::NNP_FACE_VERTEX_LIST, (*mesh.faceIndex.begin()).V()));

  //Create the material properties descriptor (what ply property and where the data is stored)
  nanoply::ElementDescriptor material(std::string("material"));
  if (mesh.material.size() > 0)
  {
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("kd"), (*mesh.material.begin()).kd.V()));
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 3, float>(std::string("ks"), (*mesh.material.begin()).ks.V()));
    material.dataDescriptor.push_back(new nanoply::DataDescriptor<MyMaterialInfo, 1, float>(std::string("rho"), &(*mesh.material.begin()).rho));
  }

  //Create the mesh descriptor
  std::vector<nanoply::ElementDescriptor*> meshDescr;
  meshDescr.push_back(&vertex);
  meshDescr.push_back(&face);
  meshDescr.push_back(&material);

  //Save the file
  bool result = nanoply::SaveModel(infoSave.filename, meshDescr, infoSave);

  for (int i = 0; i < vertex.dataDescriptor.size(); i++)
    delete vertex.dataDescriptor[i];
  for (int i = 0; i < face.dataDescriptor.size(); i++)
    delete face.dataDescriptor[i];
  for (int i = 0; i < material.dataDescriptor.size(); i++)
    delete material.dataDescriptor[i];
  return result;
}



int main()
{
  MyMesh mesh1;
  mesh1.FillMesh();
  Save("example_ascii.ply", mesh1, false);
  Save("example_binary.ply", mesh1, true);
  MyMesh mesh2, mesh3;
  Load("example_ascii.ply", mesh2);
  Load("example_binary.ply", mesh3);
  if (mesh2 == mesh1)
    std::cout << "Write and read ASCII ply file:  SUCCESS\n";
  else
    std::cout << "Write and read ASCII ply file:  FAIL\n";
  if (mesh3 == mesh1)
    std::cout << "Write and read binary ply file:  SUCCESS\n";
  else
    std::cout << "Write and read binary ply file:  FAIL\n";
  return true;
}