VCG Library
trimesh_harmonic.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2009 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #include<vcg/complex/complex.h>
24 #include<wrap/io_trimesh/import_ply.h>
25 #include<wrap/io_trimesh/export_ply.h>
26 #include<vcg/complex/algorithms/update/color.h>
27 #include<vcg/complex/algorithms/update/quality.h>
28 #include<vcg/complex/algorithms/harmonic.h>
29 
30 
31 using namespace vcg;
32 using namespace std;
33 
34 class MyFace;
35 class MyVertex;
36 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
37  Use<MyFace> ::AsFaceType>{};
38 
39 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj, vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
40 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::Mark, face::BitFlags, face::VFAdj, face::FFAdj > {};
41 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace> > {};
42 
43 
44 
45 
46 int main( int argc, char **argv )
47 {
48  MyMesh m;
49  if(argc < 2 )
50  {
51  printf("Usage: trimesh_harmonic mesh.ply\n");
52  return -1;
53  }
54 
55  int ret= tri::io::ImporterPLY<MyMesh>::Open(m,argv[1]);
56  if(ret!=0)
57  {
58  printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY<MyMesh>::ErrorMsg(ret));
59  return -1;
60  }
61 
63 
64 
65  srand(time(0));
66  int ind0=rand()%m.vn;
67  int ind1=rand()%m.vn;
68  printf("Computing harmonic field from vertex %i to vertex %i\n",ind0,ind1);
69 
70  // Get the two vertices with value set
71  tri::Harmonic<MyMesh, double>::ConstraintVec constraints;
72  constraints.push_back(tri::Harmonic<MyMesh, double>::Constraint(&(m.vert[ind0]), 1.0f));
73  constraints.push_back(tri::Harmonic<MyMesh, double>::Constraint(&(m.vert[ind1]), 2.0f));
74 
75  MyMesh::PerVertexAttributeHandle<double> handle = tri::Allocator<MyMesh>::GetPerVertexAttribute<double>(m, "harmonic");
76  bool ok = tri::Harmonic<MyMesh, double>::ComputeScalarField(m, constraints, handle);
77  assert(ok);
78 
81  tri::io::ExporterPLY<MyMesh>::Save(m,"harmonic.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY);
82 
83  return 0;
84 }