VCG Library
trimesh_fitting.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 #include<vcg/complex/algorithms/create/platonic.h>
36 #include<vcg/complex/algorithms/point_sampling.h>
37 #include<wrap/io_trimesh/import_off.h>
38 #include<vcg/space/fitting3.h>
39 
40 
41 class MyEdge;
42 class MyFace;
43 class MyVertex;
44 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex> ::AsVertexType,
45  vcg::Use<MyEdge> ::AsEdgeType,
46  vcg::Use<MyFace> ::AsFaceType>{};
47 
48 class MyVertex : public vcg::Vertex<MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
49 class MyFace : public vcg::Face< MyUsedTypes, vcg::face::FFAdj, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags > {};
50 class MyEdge : public vcg::Edge<MyUsedTypes>{};
51 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> , std::vector<MyEdge> > {};
52 
53 float EvalPlane(vcg::Plane3f &pl, std::vector<vcg::Point3f> posVec)
54 {
55  float off=0;
56  for(size_t i=0;i<posVec.size();++i)
57  off += fabs(vcg::SignedDistancePlanePoint(pl,posVec[i]));
58 
59  off/=float(posVec.size());
60  return off;
61 }
62 
63 
64 int main( )
65 {
66  MyMesh m;
67  vcg::tri::Icosahedron(m);
70 
71  // As a simple test just run over all the faces of a mesh
72  // get a few random points over it, perturb them and fit a plane on them.
73  vcg::Plane3f ple,plf,plw;
74  int cnt=0;
75  float scaleFac = m.bbox.Diag()/10.0f;
76  printf("ScaleFac %f\n\n",scaleFac);
77  vcg::math::MarsenneTwisterRNG rnd;
78  for(int i=0;i<m.FN();++i)
79  {
80  std::vector<vcg::Point3f> ExactVec;
81  std::vector<vcg::Point3f> PerturbVec;
82  std::vector<float> WeightVec;
83  vcg::Plane3f pl;
84  pl.Init(vcg::Barycenter(m.face[i]),m.face[i].N());
85  for(int j=0;j<200;++j)
86  {
88  ExactVec.push_back(p);
89  vcg::Point3f off = vcg::math::GeneratePointInUnitBallUniform<float>(rnd);
90  p+=off*scaleFac;
91  float w = std::max(0.0f, 1.0f-fabs(vcg::SignedDistancePlanePoint(pl,p))/scaleFac);
92  PerturbVec.push_back(p);
93  WeightVec.push_back(w*w); // as weight we use the square of (1-distance)
94  }
95 
96  vcg::FitPlaneToPointSet(ExactVec,ple);
97  float err=EvalPlane(ple,ExactVec);
98 
99  vcg::FitPlaneToPointSet(PerturbVec,plf);
100  float err0=EvalPlane(plf,ExactVec);
101 
102  vcg::WeightedFitPlaneToPointSet(PerturbVec,WeightVec,plw);
103  float err1=EvalPlane(plw,ExactVec);
104  printf("Exact %5.3f Fit to Perturbed %5.3f Weighted fit to perturbed %5.3f\n",err,err0,err1);
105  if(err0>err1) cnt++;
106  }
107 
108  printf("\nWeighted Fitting was better %i on %i\n",cnt,m.FN());
109  return 0;
110 }