VCG Library
trimesh_split_vertex.cpp
1 #include <vcg/complex/complex.h>
2 #include <vcg/complex/algorithms/attribute_seam.h>
3 
4 #include <wrap/io_trimesh/import_ply.h>
5 #include <wrap/io_trimesh/export_ply.h>
6 
7 /*
8  this sample shows how to transfer per wedge attributes from wedges to vertices.
9  during the process new vertices could be created.
10 */
11 using namespace vcg;
12 
13 #define TEST_IN_PLACE_SPLIT
14 
15 #ifdef TEST_IN_PLACE_SPLIT
16 
17 class SrcVertex;
18 class SrcFace;
19 
20 struct ScrUsedTypes : public UsedTypes< Use<SrcVertex>::AsVertexType,
21  Use<SrcFace>::AsFaceType>{};
22 
23 class SrcVertex : public vcg::Vertex
24 < ScrUsedTypes,
25  vcg::vertex::InfoOcf,
26  vcg::vertex::Coord3f,
27  vcg::vertex::TexCoordfOcf,
28  vcg::vertex::BitFlags
29 > { };
30 
31 class SrcFace : public vcg::Face
32 < ScrUsedTypes,
33  vcg::face::InfoOcf,
34  vcg::face::VertexRef,
35  vcg::face::WedgeTexCoordfOcf
36 > { };
37 
38 class SrcMesh : public vcg::tri::TriMesh <vcg::vertex::vector_ocf<SrcVertex>, vcg::face::vector_ocf<SrcFace> > { };
39 
40 typedef SrcVertex DstVertex;
41 typedef SrcFace DstFace;
42 typedef SrcMesh DstMesh;
43 
44 #else
45 
46 // source mesh type: per-wedge texture coordinates
47 class SrcVertex;
48 class SrcFace;
49 
50 
51 struct SrcUsedTypes : public UsedTypes< Use<SrcVertex>::AsVertexType,
52  Use<SrcFace>::AsFaceType>{};
53 
54 class SrcVertex : public vcg::Vertex <SrcUsedTypes, vcg::vertex::Coord3f, vcg::vertex::TexCoord2f, vcg::vertex::BitFlags> { };
55 class SrcFace : public vcg::Face <SrcUsedTypes, vcg::face::VertexRef, vcg::face::WedgeTexCoord2f> { };
56 class SrcMesh : public vcg::tri::TriMesh <std::vector<SrcVertex>, std::vector<SrcFace> > { };
57 
58 
59 // destination mesh type: per-vertex texture coordinates
60 class DstVertex;
61 class DstFace;
62 
63 struct DstUsedTypes : public UsedTypes< Use<SrcVertex>::AsVertexType,
64  Use<SrcFace>::AsFaceType>{};
65 
66 class DstVertex : public vcg::Vertex <DstUsedTypes, vcg::vertex::Coord3f, vcg::vertex::TexCoord2f, vcg::vertex::BitFlags> { };
67 class DstFace : public vcg::Face <DstUsedTypes, vcg::face::VertexRef> { };
68 class DstMesh : public vcg::tri::TriMesh <std::vector<DstVertex>, std::vector<DstFace> > { };
69 
70 #endif
71 
72 // extract wedge attributes functor.
73 // given a source face and a wedge index, this functor extracts all the relevant attributes from the wedge
74 // and transfer them to the destination vertex.
75 // source and destination meshes are provided to allow for attribute presence checking (.Is*Enabled()).
76 inline void ExtractVertex(const SrcMesh & srcMesh, const SrcFace & f, int whichWedge, const DstMesh & dstMesh, DstVertex & v)
77 {
78  (void)srcMesh;
79  (void)dstMesh;
80 
81  v.P() = f.cP(whichWedge);
82  v.T() = f.cWT(whichWedge);
83 }
84 
85 // sample compare functor.
86 // given two destination vertices, this functor tells if they are identical in all relevan attributes.
87 // source and destination meshes are provided to allow for attribute presence checking (.Is*Enabled()).
88 inline bool CompareVertex(const DstMesh & m, const DstVertex & vA, const DstVertex & vB)
89 {
90  (void)m;
91 
92  return (vA.cT() == vB.cT());
93 }
94 
95 // sample copy functor.
96 // given two destination vertices, this functor is asked to copy all relevan attributes.
97 // source and destination meshes are provided to allow for attribute presence checking (.Is*Enabled()).
98 inline void CopyVertex(const DstMesh & m, const DstVertex & vSrc, DstVertex & vDst)
99 {
100  (void)m;
101 
102  vDst.P() = vSrc.cP();
103  vDst.T() = vSrc.cT();
104 }
105 
106 void usage(void)
107 {
108  printf("usage : trimesh_split_vertex <src_ply_file_name> <dst_ply_file_name>\n");
109  printf("where : <src_ply_file_name> : source PLY trimesh file name with texture coordinates per wedge\n");
110  printf(" <dst_ply_file_name> : destination PLY trimesh file name with texture coordinates per vertex\n");
111  printf("exit.\n");
112 }
113 
114 int main(int argc, char ** argv)
115 {
116  if (argc != 3)
117  {
118  usage();
119  return -1;
120  }
121 
122  SrcMesh srcMesh;
123 #ifdef TEST_IN_PLACE_SPLIT
124  srcMesh.face.EnableWedgeTexCoord();
125 #endif
126  vcg::tri::io::ImporterPLY<SrcMesh>::Open(srcMesh, argv[1]);
127  if ((srcMesh.VN() <= 0) || (srcMesh.FN() <= 0))
128  {
129  printf("invalid source mesh file.\n");
130  return -1;
131  }
132  const int srcVN = srcMesh.VN();
133  const int srcFN = srcMesh.FN();
134  printf("source mesh succesfully loaded.\n");
135 
136 #ifdef TEST_IN_PLACE_SPLIT
137  DstMesh & dstMesh = srcMesh;
138  dstMesh.vert.EnableTexCoord();
139  vcg::tri::AttributeSeam::SplitVertex(dstMesh, ExtractVertex, CompareVertex);
140 #else
141  DstMesh dstMesh;
142  vcg::tri::AttributeSeam::SplitVertex(srcMesh, dstMesh, ExtractVertex, CompareVertex, CopyVertex);
143  dstMesh.textures = srcMesh.textures;
144 #endif
145  if (vcg::tri::io::ExporterPLY<DstMesh>::Save(dstMesh, argv[2], vcg::tri::io::Mask::IOM_VERTCOORD | vcg::tri::io::Mask::IOM_VERTTEXCOORD) != 0)
146  {
147  printf("cannot save destination mesh file.\n");
148  return -1;
149  }
150  printf("destination mesh succesfully saved.\n");
151  const int dstVN = dstMesh.VN();
152  const int dstFN = dstMesh.FN();
153 
154  printf("\n");
155  printf("statistics:\n");
156  printf(" input mesh vertices count : %d\n", srcVN);
157  printf(" input mesh faces count : %d\n", srcFN);
158  printf(" splitted mesh vertices count : %d\n", dstVN);
159  printf(" splitted mesh faces count : %d\n", dstFN);
160  printf("\n");
161 
162  return 0;
163 }