File: skeleton.h

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 (132 lines) | stat: -rw-r--r-- 5,667 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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef VCG__SKELETON_H
#define VCG__SKELETON_H
#include<vcg/complex/algorithms/voronoi_volume_sampling.h>

namespace vcg
{
namespace tri
{

template <class MeshType> 
class SampledSkeleton
{
public:
  typedef typename MeshType::ScalarType ScalarType;
  typedef typename MeshType::BoxType BoxType;
  typedef typename MeshType::VertexIterator VertexIterator;
  typedef typename MeshType::VertexPointer VertexPointer;
  typedef typename MeshType::CoordType CoordType;
  typedef typename MeshType::FacePointer FacePointer;
  typedef typename MeshType::FaceType FaceType;
  typedef VoronoiVolumeSampling<MeshType> VoronoiVolumeSamplingType;
  SampledSkeleton(VoronoiVolumeSamplingType &_vvs):vvs(_vvs){}
  
  VoronoiVolumeSamplingType &vvs;
  

/**
 * @brief Compute an evaulation of the thickness as distance from the medial axis.
 * It starts from a montecarlo volume sampling and try to search for the samples that can be part of the medial axis.
 * It use a sampled representation of the surface. A volume sample is considered part 
 * of the medial axis if there are at least two points that are (almost) the same minimal distance to that point.
 *
 * 
 */
void ThicknessEvaluator(float distThr, int smoothSize, int smoothIter, MeshType *skelM=0)
{
  tri::UpdateQuality<MeshType>::VertexConstant(vvs.psd.poissonSurfaceMesh,0);
  std::vector<VertexPointer> medialSrc(vvs.psd.poissonSurfaceMesh.vert.size(),0);
  for(VertexIterator vi=vvs.montecarloVolumeMesh.vert.begin(); vi!=vvs.montecarloVolumeMesh.vert.end(); ++vi)
   {
    unsigned int ind;
    ScalarType sqdist;
    this->vvs.psd.surfTree->doQueryClosest(vi->P(),ind,sqdist);
    VertexPointer vp = &vvs.psd.poissonSurfaceMesh.vert[ind];
    ScalarType minDist = math::Sqrt(sqdist);
    if(vp->Q() < minDist) 
    {
      std::vector<unsigned int> indVec;
      std::vector<ScalarType> sqDistVec;
      
      this->vvs.psd.surfTree->doQueryDist( vi->P(), minDist*distThr,indVec,sqDistVec);
      if(indVec.size()>1)
      {
        for(size_t i=0;i<indVec.size();++i)
        {
          VertexPointer vp = &vvs.psd.poissonSurfaceMesh.vert[indVec[i]];
          //ScalarType dist = math::Sqrt(sqDistVec[i]);
          if(vp->Q() < minDist) {
            vp->Q()=minDist;
            medialSrc[indVec[i]]=&*vi;
          }             
        }
      }       
    }
  }
  // Now collect the vertexes of the volume mesh that are on the medial surface 
  if(skelM)
  {
    tri::UpdateFlags<MeshType>::VertexClearV(vvs.montecarloVolumeMesh);
    for(size_t i=0;i<medialSrc.size();++i)
      medialSrc[i]->SetV();
    for(VertexIterator vi=vvs.montecarloVolumeMesh.vert.begin(); vi!=vvs.montecarloVolumeMesh.vert.end(); ++vi)
      if(vi->IsV()) tri::Allocator<MeshType>::AddVertex(*skelM,vi->P());
    printf("Generated a medial surf of %i vertexes\n",skelM->vn);
  }
  
  
  tri::Smooth<MeshType>::PointCloudQualityMedian(vvs.psd.poissonSurfaceMesh);
  tri::Smooth<MeshType>::PointCloudQualityAverage(vvs.psd.poissonSurfaceMesh,smoothSize,smoothIter);
  tri::UpdateColor<MeshType>::PerVertexQualityRamp(vvs.psd.poissonSurfaceMesh);
  tri::RedetailSampler<MeshType> rs;
  rs.init(&vvs.psd.poissonSurfaceMesh);
  rs.dist_upper_bound = vvs.psd.poissonSurfaceMesh.bbox.Diag()*0.05 ;
  rs.qualityFlag = true;
  tri::SurfaceSampling<MeshType, RedetailSampler<MeshType> >::VertexUniform(vvs.baseMesh, rs, vvs.baseMesh.vn, false);
}

void RefineSkeletonVolume(MeshType &skelMesh)
{
  CoordType closestP;
  int trialNum=0;
  for(int i=0;i<skelMesh.vn;++i)
   {
       CoordType point = math::GeneratePointInBox3Uniform(vvs.rng,vvs.baseMesh.bbox);
       trialNum++;
       ScalarType d = this->DistanceFromSurface(point, closestP);
       if(d<0){
         vcg::tri::Allocator<MeshType>::AddVertex(vvs.montecarloVolumeMesh,point);
         vvs.montecarloVolumeMesh.vert.back().Q() = fabs(d);
       }
   }
}


}; // end class


} // end namespace vcg
} // end namespace vcg
#endif // VCG__SKELETON_H