Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
itkStandardMeshRepresenter.h
1 /*
2  * This file is part of the statismo library.
3  *
4  * Author: Marcel Luethi (marcel.luethi@unibas.ch)
5  *
6  * Copyright (c) 2011 University of Basel
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * Neither the name of the project's author nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 
39 
40 #ifndef ITK_STANDARD_MESH_REPRESENTER_H
41 #define ITK_STANDARD_MESH_REPRESENTER_H
42 
43 #include <boost/unordered_map.hpp>
44 
45 #include <itkMesh.h>
46 #include <itkObject.h>
47 
48 #include "statismoITKConfig.h" // this needs to be the first include file
49 
50 #include "CommonTypes.h"
51 #include "Exceptions.h"
52 #include "Representer.h"
53 
54 #include "itkPixelConversionTraits.h"
55 
56 namespace statismo {
57 template <>
58 struct RepresenterTraits<itk::Mesh<float, 3u> > {
59 
60  typedef itk::Mesh<float, 3u> MeshType;
61 
62  typedef MeshType::Pointer DatasetPointerType;
63  typedef MeshType::Pointer DatasetConstPointerType;
64 
65  typedef MeshType::PointType PointType;
66  typedef MeshType::PointType ValueType;
67 };
68 
69 }
70 
71 namespace itk {
72 
73 // helper function to compute the hash value of an itk point (needed by unorderd_map)
74 template <typename PointType>
75 size_t hash_value(const PointType& pt) {
76  size_t hash_val = 0;
77  for (unsigned i = 0; i < pt.GetPointDimension(); i++) {
78  boost::hash_combine( hash_val, pt[i] );
79  }
80  return hash_val;
81 }
82 
83 
89 template <class TPixel, unsigned MeshDimension>
90 class StandardMeshRepresenter : public statismo::Representer<itk::Mesh<TPixel, MeshDimension> >, public Object {
91  public:
92 
93  /* Standard class typedefs. */
95  typedef Object Superclass;
96  typedef SmartPointer<Self> Pointer;
97  typedef SmartPointer<const Self> ConstPointer;
98 
99 
100  typedef itk::Mesh<TPixel, MeshDimension> MeshType;
101  typedef typename statismo::Representer<MeshType > RepresenterBaseType;
103  typedef typename RepresenterBaseType::PointType PointType;
104  typedef typename RepresenterBaseType::ValueType ValueType;
105  typedef typename RepresenterBaseType::DatasetPointerType DatasetPointerType;
106  typedef typename RepresenterBaseType::DatasetConstPointerType DatasetConstPointerType;
107  typedef typename MeshType::PointsContainer PointsContainerType;
108 
111 
114 
115 
116  static StandardMeshRepresenter* Create() {
117  return new StandardMeshRepresenter();
118  }
119 
120  void Load(const H5::Group& fg);
121  StandardMeshRepresenter* Clone() const;
122 
124  typedef MeshType DatasetType;
125 
126  // An unordered map is used to cache pointid for corresonding points
127  typedef boost::unordered_map<PointType, unsigned> PointCacheType;
128 
130  virtual ~StandardMeshRepresenter();
131 
132  unsigned GetDimensions() const {
133  return MeshDimension;
134  }
135  std::string GetName() const {
136  return "itkStandardMeshRepresenter";
137  }
138  typename RepresenterBaseType::RepresenterDataType GetType() const {
139  return RepresenterBaseType::POLYGON_MESH;
140  }
141  std::string GetVersion() const {
142  return "0.1";
143  }
144 
145  const DomainType& GetDomain() const {
146  return m_domain;
147  }
148 
150  void SetReference(DatasetPointerType ds);
151 
152  statismo::VectorType PointToVector(const PointType& pt) const;
153 
154 
158  DatasetPointerType DatasetToSample(DatasetConstPointerType ds) const;
159 
163  statismo::VectorType SampleToSampleVector(DatasetConstPointerType sample) const;
164 
168  DatasetPointerType SampleVectorToSample(const statismo::VectorType& sample) const;
169 
173  ValueType PointSampleFromSample(DatasetConstPointerType sample, unsigned ptid) const;
174 
178  ValueType PointSampleVectorToPointSample(const statismo::VectorType& pointSample) const;
179 
183  statismo::VectorType PointSampleToPointSampleVector(const ValueType& v) const;
184 
188  void Save(const H5::Group& fg) const;
189 
191  virtual unsigned GetNumberOfPoints() const;
192 
195  virtual unsigned GetPointIdForPoint(const PointType& point) const;
196 
198  DatasetConstPointerType GetReference() const {
199  return m_reference;
200  }
201 
202  void Delete() const {
203  this->UnRegister();
204  }
205 
206 
207  void DeleteDataset(DatasetPointerType d) const { };
208  DatasetPointerType CloneDataset(DatasetConstPointerType mesh) const;
209 
210  private:
211 
212  typename MeshType::Pointer LoadRef(const H5::Group& fg) const;
213  typename MeshType::Pointer LoadRefLegacy(const H5::Group& fg) const;
214 
215  // returns the closest point for the given mesh
216  unsigned FindClosestPoint(const MeshType* mesh, const PointType pt) const ;
217 
218  DatasetConstPointerType m_reference;
219  DomainType m_domain;
220  mutable PointCacheType m_pointCache;
221 };
222 
223 
224 } // namespace itk
225 
226 
227 
228 #include "itkStandardMeshRepresenter.hxx"
229 
230 #endif /* ITK_STANDARD_MESH_REPRESENTER */
MeshType DatasetType
The type of the data set to be used.
Definition: itkStandardMeshRepresenter.h:124
statismo::VectorType SampleToSampleVector(DatasetConstPointerType sample) const
Definition: itkStandardMeshRepresenter.hxx:235
A representer for scalar valued itk Meshs.
Definition: itkStandardMeshRepresenter.h:90
ValueType PointSampleFromSample(DatasetConstPointerType sample, unsigned ptid) const
Definition: itkStandardMeshRepresenter.hxx:279
virtual unsigned GetPointIdForPoint(const PointType &point) const
Definition: itkStandardMeshRepresenter.hxx:374
statismo::VectorType PointSampleToPointSampleVector(const ValueType &v) const
Definition: itkStandardMeshRepresenter.hxx:299
void Save(const H5::Group &fg) const
Definition: itkStandardMeshRepresenter.hxx:310
DatasetPointerType SampleVectorToSample(const statismo::VectorType &sample) const
Definition: itkStandardMeshRepresenter.hxx:257
DatasetConstPointerType GetReference() const
return the reference used in the representer
Definition: itkStandardMeshRepresenter.h:198
itkTypeMacro(StandardMeshRepresenter, Object)
void SetReference(DatasetPointerType ds)
Definition: itkStandardMeshRepresenter.hxx:194
ValueType PointSampleVectorToPointSample(const statismo::VectorType &pointSample) const
Definition: itkStandardMeshRepresenter.hxx:290
DatasetPointerType DatasetToSample(DatasetConstPointerType ds) const
Definition: itkStandardMeshRepresenter.hxx:228
Definition: Domain.h:51
virtual unsigned GetNumberOfPoints() const
return the number of points of the reference
Definition: itkStandardMeshRepresenter.hxx:367