Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
TrivialVectorialRepresenter.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 #ifndef TRIVIALVECTORIALREPRESENTER_H
40 #define TRIVIALVECTORIALREPRESENTER_H
41 
42 #include <memory>
43 #include <H5Cpp.h>
44 
45 #include "CommonTypes.h"
46 #include "Domain.h"
47 #include "HDF5Utils.h"
48 #include "Representer.h"
49 
50 namespace statismo {
51 
52 // A pointId is actually just an unsigned. However, we need to create a distinct type, to disambiguate some of
53 // the methods.
54 struct PointIdType {
55  PointIdType(unsigned ptId_) : ptId(ptId_) {}
56  PointIdType() : ptId(0) {}
57 
58  unsigned ptId;
59 
60 };
61 
62 
63 template <>
64 struct RepresenterTraits<statismo::VectorType> {
65  typedef statismo::VectorType DatasetPointerType;
66  typedef statismo::VectorType DatasetConstPointerType;
67 
68  typedef PointIdType PointType;
69  typedef statismo::ScalarType ValueType;
71 
72 
73 };
74 
75 
76 
77 
83 class TrivialVectorialRepresenter : public Representer<statismo::VectorType> {
84  public:
85 
86  typedef statismo::ScalarType ValueType;
88  typedef Representer<statismo::VectorType> RepresenterBaseType;
89 
90  static TrivialVectorialRepresenter* Create() {
91  return new TrivialVectorialRepresenter();
92  }
93 
94  static TrivialVectorialRepresenter* Create(unsigned numberOfPoints) {
95  return new TrivialVectorialRepresenter(numberOfPoints);
96  }
97 
98  void Load(const H5::Group& fg) {
99  unsigned numPoints = static_cast<unsigned>(statismo::HDF5Utils::readInt(fg, "numberOfPoints"));
100  initializeObject(numPoints);
101  }
102 
103  TrivialVectorialRepresenter* Clone() const {
104  return TrivialVectorialRepresenter::Create(m_domain.GetNumberOfPoints());
105  }
106  void Delete() const {
107  delete this;
108  }
109 
110  virtual ~TrivialVectorialRepresenter() {}
111 
112 
113  std::string GetName() const {
114  return "TrivialVectorialRepresenter";
115  }
116  unsigned GetDimensions() const {
117  return 1;
118  }
119  std::string GetVersion() const {
120  return "0.1";
121  }
122  RepresenterBaseType::RepresenterDataType GetType() const {
123  return RepresenterBaseType::VECTOR;
124  }
125 
126 
127  void DeleteDataset(DatasetPointerType d) const { };
128  DatasetPointerType CloneDataset(DatasetConstPointerType d) const {
129  return d;
130  }
131 
132 
133  const DomainType& GetDomain() const {
134  return m_domain;
135  }
136  DatasetConstPointerType GetReference() const {
137  return VectorType::Zero(m_domain.GetNumberOfPoints());
138  }
139 
140  VectorType PointToVector(const PointType& pt) const {
141  // here, the pt type is simply an id (the index into the vector).
142  VectorType v(1);
143  v(0) = pt.ptId;
144  return v;
145  }
146  DatasetPointerType DatasetToSample(DatasetConstPointerType ds) const {
147  return ds;
148  }
149  VectorType SampleToSampleVector(DatasetConstPointerType sample) const {
150  return sample;
151  }
152  DatasetPointerType SampleVectorToSample(const statismo::VectorType& sample) const {
153  return sample;
154  }
155 
156  VectorType PointSampleToPointSampleVector(const ValueType& v) const {
157  VectorType vec = VectorType::Zero(1);
158  vec(0) = v;
159  return vec;
160 
161  }
162 
163  ValueType PointSampleFromSample(DatasetConstPointerType sample, unsigned ptid) const {
164  return sample[ptid];
165  }
166  ValueType PointSampleVectorToPointSample(const VectorType& pointSample) const {
167  return pointSample(0);
168  }
169 
170 
171  void Save(const H5::Group& fg) const {
172  HDF5Utils::writeInt(fg, "numberOfPoints", static_cast<int>(m_domain.GetNumberOfPoints()));
173  }
174 
175  unsigned GetPointIdForPoint(const PointType& point) const {
176  return point.ptId;
177  }
178 
179 
180  private:
182 
183  TrivialVectorialRepresenter(unsigned numberOfPoints) {
184  initializeObject(numberOfPoints);
185  }
186 
187  void initializeObject(unsigned numberOfPoints) {
188 
189  // the domain for vectors correspond to the valid indices.
190  DomainType::DomainPointsListType domainPoints;
191  for (unsigned i = 0; i < numberOfPoints; i++) {
192  domainPoints.push_back(PointIdType(i));
193  }
194  m_domain = DomainType(domainPoints);
195  }
196 
197  DomainType m_domain;
198 
201 };
202 
203 }
204 
205 #endif
const unsigned GetNumberOfPoints() const
Definition: Domain.h:72
A trivial representer, that does no representation at all, but works directly with vectorial data...
Definition: TrivialVectorialRepresenter.h:83
static H5::DataSet writeInt(const H5::CommonFG &fg, const char *name, int value)
Definition: HDF5Utils.hxx:426
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60
static int readInt(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:434
Definition: Domain.h:51