SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Bundle.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 #ifndef SCIRUN_CORE_DATATYPES_BUNDLE_H
30 #define SCIRUN_CORE_DATATYPES_BUNDLE_H 1
31 
34 
35 namespace SCIRun {
36  namespace Core {
37  namespace Datatypes {
38 
39  /// @todo:
40  // 0. Bundle I/O
41  // 1. Store ColorMaps, Nrrds, and Bundles
42  // 2. Matrix <-> Nrrd interop. Should go in separate class.
43 
44 class SCISHARE Bundle : public Datatype
45 {
46  public:
47  Bundle();
48 
49  virtual Bundle* clone() const;
50 
51  bool empty() const;
52  size_t size() const;
53 
54  DatatypeHandle get(const std::string& name) const;
55  void set(const std::string& name, DatatypeHandle data);
56 
57  bool isField(const std::string& name) const;
58  FieldHandle getField(const std::string& name) const;
59  size_t numFields() const;
60  std::vector<FieldHandle> getFields() const;
61  std::vector<std::string> getFieldNames() const;
62 
63  bool isMatrix(const std::string& name) const;
64  MatrixHandle getMatrix(const std::string& name) const;
65  size_t numMatrices() const;
66  std::vector<MatrixHandle> getMatrices() const;
67  std::vector<std::string> getMatrixNames() const;
68 
69  bool isString(const std::string& name) const;
70  StringHandle getString(const std::string& name) const;
71  size_t numStrings() const;
72  std::vector<StringHandle> getStrings() const;
73  std::vector<std::string> getStringNames() const;
74 
75  bool remove(const std::string& name);
76 
77 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
78 
79  /// For writing bundles to file
80  virtual void io(Piostream&);
81  static PersistentTypeID type_id;
82 
83  /// Merge two bundles together
84  void merge(SCIRun::LockingHandle<Bundle> C);
85 
86  /// Transpose when doing a matrix to nrrd conversion
87  void transposeNrrd(bool on);
88 
89  // The basic functions for managing fields
90  /// getfield -> Retrieve a Handle to a field stored in the bundle
91  /// setfield -> Add a field with a name, if it already exists the
92  /// old one is overwritten
93  /// remfield -> Remove a handle from the bundle
94  /// isfield -> Test whether a field is present in the bundle
95  /// numfields -> The number of fields stored in the bundle
96  /// getfieldname -> Get the nth name in the bundle for building a contents
97  /// list
98 
99  std::string getFieldName(int index)
100  { return(getName<Field>(index)); }
101 
102  /// The basic functions for managing matrices
103  /// getmatrix -> Retrieve a Handle to a matrix stored in the bundle
104  /// setmatrix -> Add a matrix with a name, if it already exists the old
105  /// one is overwritten
106  /// remmatrix -> Remove a handle from the bundle
107  /// ismatrix -> Test whether a matrix is present in the bundle
108  /// nummatrices -> The number of matrices stored in the bundle
109  /// getmatrixname -> Get the nth name in the bundle for building a contents
110  /// list
111 
112  std::string getMatrixName(int index);
113  // Implementation in cc file, with NRRD/MATRIX compatibility
114 
115  LockingHandle<NrrdData> getNrrd(const std::string& name);
116  // Implementation in cc file, with NRRD/MATRIX compatibility
117 
118  void setNrrd(const std::string& name, LockingHandle<NrrdData> &nrrd)
119  { set<NrrdData>(name,nrrd); }
120 
121  void remNrrd(const std::string& name)
122  { rem(name); }
123 
124  bool isNrrd(const std::string& name);
125  // Implementation in cc file, with NRRD/MATRIX compatibility
126 
127  int numNrrds();
128  // Implementation in cc file, with NRRD/MATRIX compatibility
129 
130  std::string getNrrdName(int index);
131  // Implementation in cc file, with NRRD/MATRIX compatibility
132 
133  /// The basic functions for managing colormaps
134  /// getcolormap -> Retrieve a Handle to a colormap stored in the bundle
135  /// setcolormap -> Add a colormap with a name, if it already exists the
136  /// old one is overwritten
137  /// remcolormap -> Remove a handle from the bundle
138  /// iscolormap -> Test whether a colormap is present in the bundle
139  /// numcolormaps -> The number of colormaps stored in the bundle
140  /// getcolormapname -> Get the nth name in the bundle for building a
141  /// contents list
142 
143  LockingHandle<ColorMap> getColorMap(const std::string& name)
144  { return(get<ColorMap>(name)); }
145 
146  void setColorMap(const std::string& name, LockingHandle<ColorMap> &colormap)
147  { set<ColorMap>(name,colormap); }
148 
149  void remColorMap(const std::string& name)
150  { rem(name); }
151 
152  bool isColorMap(const std::string& name)
153  { return(is<ColorMap>(name)); }
154 
155  int numColorMaps()
156  { return(num<ColorMap>()); }
157 
158  std::string getColorMapName(int index)
159  { return(getName<ColorMap>(index)); }
160 
161  /// The basic functions for managing bundles
162  /// getbundle -> Retrieve a Handle to a bundle stored in the bundle
163  /// setbundle -> Add a bundle with a name, if it already exists the old
164  /// one is overwritten
165  /// rembundle -> Remove a handle from the bundle
166  /// isbundle -> Test whether a bundle is present in the bundle
167  /// numbundles -> The number of bundles stored in the bundle
168  /// getbundleName -> Get the nth name in the bundle for building a contents
169  /// list
170 
171  LockingHandle<Bundle> getBundle(const std::string& name)
172  { return(get<Bundle>(name)); }
173 
174  void setBundle(const std::string& name, LockingHandle<Bundle> &bundle)
175  { set<Bundle>(name,bundle); }
176 
177  void remBundle(const std::string& name)
178  { rem(name); }
179 
180  bool isBundle(const std::string& name)
181  { return(is<Bundle>(name)); }
182 
183  int numBundles()
184  { return(num<Bundle>()); }
185 
186  std::string getBundleName(int index)
187  { return(getName<Bundle>(index)); }
188 
189  /// Get the name of the handles in the bundle
190  std::string getHandleName(int index)
191  { return bundleName_[static_cast<size_t>(index)]; }
192 
193  /// Get one of the handles in the bundle
194  LockingHandle<PropertyManager> gethandle(int index)
195  { return bundle_[static_cast<size_t>(index)]; }
196 
197  /// Get the type of a handle (for BundleInfo)
198  std::string getHandleType(int index);
199 
200  virtual std::string dynamic_type_name() const { return type_id.type; }
201 
202  private:
203 
204  /// Functions for doing NRRD/MATRIX conversion
205  template<class PTYPE>
206  inline bool NrrdToMatrixHelper(NrrdDataHandle dataH, MatrixHandle& matH);
207 
208  bool NrrdToMatrixConvertible(NrrdDataHandle nrrdH);
209  bool NrrdToMatrix(NrrdDataHandle dataH,MatrixHandle& matH);
210  bool MatrixToNrrdConvertible(MatrixHandle matH);
211  bool MatrixToNrrd(MatrixHandle matH,NrrdDataHandle &nrrdH);
212 
213  /// Setting for the conversion between NRRD/MATRIX
214  bool transposeNrrd_;
215 #endif
216  std::map<std::string, Core::Datatypes::DatatypeHandle> bundle_;
217 };
218 
219 typedef boost::shared_ptr<Bundle> BundleHandle;
220 
221 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
222 inline void Bundle::transposeNrrd(bool transpose)
223 {
224  transposeNrrd_ = transpose;
225 }
226 
227 
228 
229 template<class T> inline std::string Bundle::getName(int index)
230 {
231  int cnt = -1;
232  size_t p;
233  for (p=0;p<bundleName_.size();p++)
234  {
235  if (SCI_DATATYPE_CAST<T*>(bundle_[p].get_rep()) != 0) cnt++;
236  if (index == cnt) break;
237  }
238  if ((p < bundleName_.size())&&(cnt==index)) return bundleName_[p];
239  return(std::string(""));
240 }
241 
242 template<class PTYPE> inline bool Bundle::NrrdToMatrixHelper(
243  NrrdDataHandle dataH, MatrixHandle& matH)
244 {
245  if (dataH->nrrd_->dim == 1)
246  {
247  int cols = dataH->nrrd_->axis[0].size;
248 
249  ColumnMatrix* matrix = new ColumnMatrix(cols);
250 
251  PTYPE *val = reinterpret_cast<PTYPE*>(dataH->nrrd_->data);
252  double *data = matrix->get_data_pointer();
253 
254  for(int c=0; c<cols; c++)
255  {
256  *data = *val;
257  data++;
258  val++;
259  }
260  matH = matrix;
261  return(true);
262  }
263 
264  if (dataH->nrrd_->dim == 2)
265  {
266  if (transposeNrrd_)
267  {
268  int rows = dataH->nrrd_->axis[1].size;
269  int cols = dataH->nrrd_->axis[0].size;
270 
271  DenseMatrix* matrix = new DenseMatrix(rows,cols);
272 
273  PTYPE *val = reinterpret_cast<PTYPE*>(dataH->nrrd_->data);
274  double *data = matrix->get_data_pointer();
275 
276  int i,j;
277  i = 0; j = 0;
278  for(int r=0; r<rows; r++)
279  {
280  for(int c=0; c<cols; c++)
281  {
282  i = c + cols*r;
283  data[j++] = val[i];
284  }
285  }
286  matH = matrix;
287  }
288  else
289  {
290  int cols = dataH->nrrd_->axis[1].size;
291  int rows = dataH->nrrd_->axis[0].size;
292 
293  DenseMatrix* matrix = new DenseMatrix(cols,rows);
294 
295  PTYPE *val = reinterpret_cast<PTYPE*>(dataH->nrrd_->data);
296  double *data = matrix->get_data_pointer();
297 
298  for(int c=0; c<cols; c++)
299  {
300  for(int r=0; r<rows; r++)
301  {
302  *data++ = *val++;
303  }
304  }
305  matH = matrix;
306  }
307  return(true);
308  }
309  // Improper dimensions
310  return(false);
311 }
312 #endif
313 }}}
314 
315 #endif
boost::shared_ptr< Matrix > MatrixHandle
Definition: MatrixFwd.h:44
Definition: Bundle.h:44
Eigen::MatrixXd DenseMatrix
Definition: EigenDenseMatrixTests.cc:33
Definition: Persistent.h:89
#define SCISHARE
Definition: share.h:39
boost::shared_ptr< Bundle > BundleHandle
Definition: Bundle.h:219
Definition: Datatype.h:41
ColumnMatrixGeneric< double > ColumnMatrix
Definition: MatrixFwd.h:48
const char * name[]
Definition: BoostGraphExampleTests.cc:87
boost::shared_ptr< String > StringHandle
Definition: DatatypeFwd.h:58
dictionary data
Definition: eabLatVolData.py:11
LockingHandle< NrrdData > NrrdDataHandle
Definition: NrrdData.h:108
DenseMatrixGeneric< double > DenseMatrix
Definition: MatrixFwd.h:48
boost::shared_ptr< Field > FieldHandle
Definition: DatatypeFwd.h:65
Definition: Persistent.h:64
boost::shared_ptr< Datatype > DatatypeHandle
Definition: DatatypeFwd.h:44
int size
Definition: eabLatVolData.py:2
std::map< std::string, Core::Datatypes::DatatypeHandle > bundle_
Definition: Bundle.h:216