VCG Library
trimesh_kdtree.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
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 ****************************************************************************/
34 #include<vcg/complex/complex.h>
35 
36 #include<wrap/io_trimesh/import.h>
37 #include<wrap/io_trimesh/export.h>
38 #include <vcg/space/index/kdtree/kdtree.h>
39 #include<vcg/complex/algorithms/update/normal.h>
40 #include<vcg/complex/algorithms/update/color.h>
41 
42 using namespace vcg;
43 using namespace std;
44 
45 class MyEdge;
46 class MyFace;
47 class MyVertex;
48 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
49  Use<MyEdge> ::AsEdgeType,
50  Use<MyFace> ::AsFaceType>{};
51 
52 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
53 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::BitFlags > {};
54 class MyEdge : public Edge<MyUsedTypes>{};
55 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace> , vector<MyEdge> > {};
56 
57 int main( int argc, char **argv )
58 {
59  if(argc<2) argv[1]=(char *)"../../meshes/torus_irregular.ply";
60 
61  MyMesh m;
62  if(tri::io::Importer<MyMesh>::Open(m,argv[1])!=0)
63  {
64  printf("Error reading file %s\n",argv[1]);
65  exit(0);
66  }
67 
68  VertexConstDataWrapper<MyMesh> ww(m);
69 
70  KdTree<float> tree(ww);
71  KdTree<float>::PriorityQueue queue;
72 
73  for (int j = 0; j < m.VN(); j++) {
74  tree.doQueryK(m.vert[j].cP(), 3, queue);
75  int neighbours = queue.getNofElements();
76  float avgDist=0;
77  for (int i = 0; i < neighbours; i++) {
78  int neightId = queue.getIndex(i);
79  avgDist += Distance(m.vert[j].cP(),m.vert[neightId].cP());
80  }
81  m.vert[j].Q() = avgDist/=neighbours;
82  }
84  tri::io::ExporterPLY<MyMesh>::Save(m,"out.ply",tri::io::Mask::IOM_VERTCOLOR+tri::io::Mask::IOM_VERTQUALITY);
85  return 0;
86 }