NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
niftkMeshSmoother.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef niftkMeshSmoother_h
16 #define niftkMeshSmoother_h
17 
18 #include <mitkLogMacros.h>
19 
20 #include <iostream>
21 #include <vector>
22 #include "niftkBasicVertex.h"
23 #include "niftkBasicTriangle.h"
24 #include "niftkCMC33.h"
25 
26 #include "niftkCoreExports.h"
27 
28 #include <iostream>
29 using std::cout;
30 using std::endl;
31 
32 #include <fstream>
33 using std::ifstream;
34 using std::ofstream;
35 
36 #include <iomanip>
37 using std::setiosflags;
38 
39 #include <ios>
40 using std::ios_base;
41 using std::ios;
42 
43 #include <set>
44 #include <vector>
45 
46 #include <limits>
47 using std::numeric_limits;
48 
49 #include <cstring> // for memcpy()
50 #include <cctype>
51 
52 namespace niftk
53 {
54 
61 {
62 public:
63  ordered_size_t_pair(const size_t &a, const size_t &b)
64  {
65  if(a < b)
66  {
67  indices[0] = a;
68  indices[1] = b;
69  }
70  else
71  {
72  indices[0] = b;
73  indices[1] = a;
74  }
75  }
76 
77  bool operator<(const ordered_size_t_pair &right) const
78  {
79  if(indices[0] < right.indices[0])
80  return true;
81  else if(indices[0] > right.indices[0])
82  return false;
83 
84  if(indices[1] < right.indices[1])
85  return true;
86  else if(indices[1] > right.indices[1])
87  return false;
88 
89  return false;
90  }
91 
92  size_t indices[2];
93 };
94 
100 {
101 public:
103  {
104  if(a.GetIndex() < b.GetIndex())
105  {
106  indices[0] = a.GetIndex();
107  indices[1] = b.GetIndex();
108  }
109  else
110  {
111  indices[0] = b.GetIndex();
112  indices[1] = a.GetIndex();
113  }
114 
115  centre_point.SetX((a.GetCoordX() + b.GetCoordX())*0.5f);
116  centre_point.SetY((a.GetCoordY() + b.GetCoordY())*0.5f);
117  centre_point.SetZ((a.GetCoordZ() + b.GetCoordZ())*0.5f);
118  }
119 
120  bool operator<(const ordered_indexed_edge &right) const
121  {
122  if(indices[0] < right.indices[0])
123  return true;
124  else if(indices[0] > right.indices[0])
125  return false;
126 
127  if(indices[1] < right.indices[1])
128  return true;
129  else if(indices[1] > right.indices[1])
130  return false;
131 
132  return false;
133  }
134 
135  size_t indices[2];
137  size_t id;
138 };
139 
140 
147 class NIFTKCORE_EXPORT MeshSmoother
148 {
149 public:
151  MeshSmoother();
153  virtual ~MeshSmoother();
154 
156  void Clear(void);
157 
159  void InitWithExternalData(MeshData * data);
160 
162  void GenerateVertexAndTriangleNormals(void);
164  void ReOrientFaces(void);
165 
167  bool LoadFromBinarySTL(const char *const file_name, const bool generate_normals = true, const size_t buffer_width = 65536);
168 
170  void SetMaxExtent(float max_extent);
171 
173  void RescaleMesh(double scale_value);
174 
176  void TaubinSmooth(const float lambda, const float mu, const size_t steps);
178  void FixCracks(void);
179 
181  inline void SetSmoothingMethod(int val) { m_SmoothingMethod = val;}
182 
184  inline void SetFlipNormals(bool val) { m_FlipNormals = val;}
186  inline bool GetFlipNormals(void) { return m_FlipNormals; }
187 
188 private:
190  void LaplaceSmooth(const float scale);
192  void CurvatureNormalSmooth(const float scale);
194  void InverseEdgeLengthSmooth(const float scale);
195 
197  void GenerateVertexNormals(void);
199  void GenerateTriangleNormals(void);
201  void RegenerateVertexAndTriangleNormalsIfExists(void);
202 
204  template<typename T> void EliminateVectorDuplicates(std::vector<T> &v);
206  bool MergeVertexPair(const size_t keeper, const size_t goner);
207 
208 private:
209  int m_SmoothingMethod; // Stores which smoothing method to use
210  bool m_FlipNormals; // Flag to indicate wether we need to flip the normals
211 
212  std::vector<BasicVec3D> m_VertexNormals; // stores all vertex normals
213  std::vector<BasicVec3D> m_TriangleNormals; // stores all triangle normals
214 
215  MeshData* m_MeshDataExt; // pointer to the externally created container
216 };
217 
218 }
219 
220 #endif
float GetCoordZ()
Get Z coordinate of the vertex.
Definition: niftkBasicVertex.h:135
ordered_indexed_edge(const BasicVertex &a, const BasicVertex &b)
Definition: niftkMeshSmoother.h:102
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:12017
int GetIndex() const
Get index of the current vertex.
Definition: niftkBasicVertex.h:215
float GetCoordX()
Get X coordinate of the vertex.
Definition: niftkBasicVertex.h:131
Utility class for storing an ordered pair of vertex indices.
Definition: niftkMeshSmoother.h:60
BasicVec3D centre_point
Definition: niftkMeshSmoother.h:136
bool operator<(const ordered_indexed_edge &right) const
Definition: niftkMeshSmoother.h:120
GLuint const GLfloat * val
Definition: glew.h:2691
bool GetFlipNormals(void)
Get the flip normals flag.
Definition: niftkMeshSmoother.h:186
Utility class for storing an ordered edge.
Definition: niftkMeshSmoother.h:99
void SetZ(float z)
Sets the Z coordinate of the vector.
Definition: niftkBasicVec3D.h:123
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1363
float GetCoordY()
Get Y coordinate of the vertex.
Definition: niftkBasicVertex.h:133
void SetX(float x)
Sets the X coordinate of the vector.
Definition: niftkBasicVec3D.h:113
GLdouble GLdouble GLdouble b
Definition: glew.h:7885
void SetY(float y)
Sets the Y coordinate of the vector.
Definition: niftkBasicVec3D.h:118
vcl_size_t id
Definition: niftkMeshSmoother.h:137
const GLdouble * v
Definition: glew.h:1375
vcl_size_t indices[2]
Definition: niftkMeshSmoother.h:135
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8272
vcl_size_t indices[2]
Definition: niftkMeshSmoother.h:92
bool operator<(const ordered_size_t_pair &right) const
Definition: niftkMeshSmoother.h:77
ordered_size_t_pair(const vcl_size_t &a, const vcl_size_t &b)
Definition: niftkMeshSmoother.h:63
void SetFlipNormals(bool val)
Set the flip normals flag.
Definition: niftkMeshSmoother.h:184
Simple vertex implementation that is used in the Surface Extraction and surface smoothing and decimat...
Definition: niftkBasicVertex.h:77
void SetSmoothingMethod(int val)
Select smoothing method. 0- Classic Laplacian, 1 - Inverse Edge Length, 2 - Curvature Normal...
Definition: niftkMeshSmoother.h:181
This class implements various mesh smoothing algorithms and it can be used to (re)compute surface and...
Definition: niftkMeshSmoother.h:147
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: glew.h:1237
Container class for passing around data structures containing a mesh.
Definition: niftkCMC33.h:61
Simple 3D Vector implementation that is used in the Surface Extraction and surface smoothing and deci...
Definition: niftkBasicVec3D.h:30
Definition: niftkExceptionObject.h:21
GLclampf f
Definition: glew.h:3136