Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
itkDataManager.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 ITK_DATAMANAGER_H_
40 #define ITK_DATAMANAGER_H_
41 
42 #include <boost/bind.hpp>
43 #include <boost/utility/result_of.hpp>
44 
45 #include <itkObject.h>
46 #include <itkObjectFactory.h>
47 
48 #include "DataManager.h"
49 #include "statismoITKConfig.h"
50 
51 namespace itk {
52 
53 
58 template <class T>
59 class DataManager : public Object {
60  public:
61 
62 
63  typedef DataManager Self;
64  typedef Object Superclass;
65  typedef SmartPointer<Self> Pointer;
66  typedef SmartPointer<const Self> ConstPointer;
67 
68  itkNewMacro( Self );
69  itkTypeMacro( DataManager, Object );
70 
71 
73  typedef typename statismo::DataManager<T>::DataItemType DataItemType;
74  typedef typename statismo::DataManager<T>::DataItemListType DataItemListType;
75  typedef statismo::Representer<T> RepresenterType;
76 
77  template <class F>
78  typename boost::result_of<F()>::type callstatismoImpl(F f) const {
79  if (m_impl == 0) {
80  itkExceptionMacro(<< "Model not properly initialized. Maybe you forgot to call SetRepresenter");
81  }
82  try {
83  return f();
85  itkExceptionMacro(<< s.what());
86  }
87  }
88 
89 
90  DataManager() : m_impl(0) {}
91 
92  virtual ~DataManager() {
93  if (m_impl) {
94  delete m_impl;
95  m_impl = 0;
96  }
97  }
98 
99  ImplType* GetstatismoImplObj() const {
100  return m_impl;
101  }
102 
103 
104  void SetstatismoImplObj(ImplType* impl) {
105  if (m_impl) {
106  delete m_impl;
107  }
108  m_impl = impl;
109  }
110 
111  void SetRepresenter(const RepresenterType* representer) {
112  SetstatismoImplObj(ImplType::Create(representer));
113  }
114 
115  void AddDataset(typename RepresenterType::DatasetType* ds, const char* filename) {
116  callstatismoImpl(boost::bind(&ImplType::AddDataset, this->m_impl, ds, filename));
117  }
118 
119  void Load(const char* filename) {
120  try {
121  SetstatismoImplObj(ImplType::Load(filename));
123  itkExceptionMacro(<< s.what());
124  }
125  }
126 
127  void Save(const char* filename) {
128  callstatismoImpl(boost::bind(&ImplType::Save, this->m_impl, filename));
129  }
130 
131  typename statismo::DataManager<T>::DataItemListType GetData() const {
132  return callstatismoImpl(boost::bind(&ImplType::GetData, this->m_impl));
133  }
134 
135 
136  private:
137  DataManager(const DataManager& orig);
138  DataManager& operator=(const DataManager& rhs);
139 
140  ImplType* m_impl;
141 };
142 
143 
144 }
145 
146 #endif /* ITK_DATAMANAGER_H_ */
ITK Wrapper for the statismo::DataManager class.
Definition: itkDataManager.h:59
Manages Training and Test Data for building Statistical Models and provides functionality for Crossva...
Definition: DataManager.h:114
static DataManager< T > * Load(Representer< T > *representer, const std::string &filename)
Definition: DataManager.hxx:75
static DataManager< T > * Create(const RepresenterType *representer)
Definition: DataManager.h:132
DataItemListType GetData() const
Definition: DataManager.hxx:227
virtual void AddDataset(DatasetConstPointerType dataset, const std::string &URI)
Definition: DataManager.hxx:214
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
virtual void Save(const std::string &filename) const
Definition: DataManager.hxx:156