Field3D

#include <DenseFieldIO.h>

Inheritance diagram for DenseFieldIO:
FieldIO RefBase

Public Types

typedef DenseFieldIO class_type
 
typedef boost::intrusive_ptr< DenseFieldIOPtr
 
- Public Types inherited from FieldIO
typedef FieldIO class_type
 
typedef boost::intrusive_ptr< FieldIOPtr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr< RefBasePtr
 
typedef boost::weak_ptr< RefBaseWeakPtr
 

Public Member Functions

virtual std::string className () const
 Returns the class name. More...
 
 DenseFieldIO ()
 Ctor. More...
 
virtual FieldBase::Ptr read (hid_t layerGroup, const std::string &filename, const std::string &layerPath, DataTypeEnum typeEnum)
 Reads the field at the given location and tries to create a DenseField object from it. Calls out to readData() for template-specific work. More...
 
virtual bool write (hid_t layerGroup, FieldBase::Ptr field)
 Writes the given field to disk. This function calls out to writeInternal once the template type has been determined. More...
 
virtual ~DenseFieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from FieldIO
 FieldIO ()
 Ctor. More...
 
virtual ~FieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from RefBase
void ref () const
 Used by boost::intrusive_pointer. More...
 
size_t refcnt ()
 Used by boost::intrusive_pointer. More...
 
void unref () const
 Used by boost::intrusive_pointer. More...
 
WeakPtr weakPtr () const
 
 RefBase ()
 
 RefBase (const RefBase &)
 Copy constructor. More...
 
RefBaseoperator= (const RefBase &)
 Assignment operator. More...
 
virtual ~RefBase ()
 Destructor. More...
 
virtual bool checkRTTI (const char *typenameStr)=0
 This function is only implemented by concrete classes and triggers the actual RTTI check through matchRTTI();. More...
 
bool matchRTTI (const char *typenameStr)
 Performs a check to see if the given typename string matches this class' This needs to be implemented in -all- subclasses, even abstract ones. More...
 

Static Public Member Functions

static FieldIO::Ptr create ()
 
static const char * staticClassType ()
 
- Static Public Member Functions inherited from FieldIO
static const char * staticClassType ()
 
- Static Public Member Functions inherited from RefBase
static const char * staticClassType ()
 

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_CLASS
 
- Public Attributes inherited from FieldIO
 DEFINE_FIELD_RTTI_ABSTRACT_CLASS
 

Private Types

typedef FieldIO base
 Convenience typedef for referring to base class. More...
 

Private Member Functions

template<class Data_T >
DenseField< Data_T >::Ptr readData (hid_t dataSet, const Box3i &extents, const Box3i &dataW)
 This call performs the actual reading of data from disk. More...
 
template<class Data_T >
bool writeData (hid_t dataSet, typename DenseField< Data_T >::Ptr field, Data_T dummy)
 This call performs the actual writing of data to disk. More...
 
template<class Data_T >
bool writeInternal (hid_t layerGroup, typename DenseField< Data_T >::Ptr field)
 This call writes all the attributes and sets up the data space. More...
 

Static Private Attributes

static const std::string k_bitsPerComponentStr
 
static const std::string k_componentsStr
 
static const std::string k_dataStr
 
static const std::string k_dataWindowStr
 
static const std::string k_extentsStr
 
static const std::string k_versionAttrName
 
static const int k_versionNumber
 

Detailed Description

Handles IO for a DenseField object

Definition at line 78 of file DenseFieldIO.h.

Member Typedef Documentation

typedef boost::intrusive_ptr<DenseFieldIO> DenseFieldIO::Ptr

Definition at line 85 of file DenseFieldIO.h.

Definition at line 89 of file DenseFieldIO.h.

typedef FieldIO DenseFieldIO::base
private

Convenience typedef for referring to base class.

Definition at line 161 of file DenseFieldIO.h.

Constructor & Destructor Documentation

DenseFieldIO::DenseFieldIO ( )
inline

Ctor.

Definition at line 100 of file DenseFieldIO.h.

101  : FieldIO()
102  { }
FieldIO()
Ctor.
Definition: FieldIO.h:105
virtual DenseFieldIO::~DenseFieldIO ( )
inlinevirtual

Dtor.

Definition at line 105 of file DenseFieldIO.h.

106  { /* Empty */ }

Member Function Documentation

static const char* DenseFieldIO::staticClassType ( )
inlinestatic

Definition at line 92 of file DenseFieldIO.h.

93  {
94  return "DenseFieldIO";
95  }
static FieldIO::Ptr DenseFieldIO::create ( )
inlinestatic

Definition at line 108 of file DenseFieldIO.h.

References read(), and write().

Referenced by initIO().

109  { return Ptr(new DenseFieldIO); }
boost::intrusive_ptr< DenseFieldIO > Ptr
Definition: DenseFieldIO.h:85
FieldBase::Ptr DenseFieldIO::read ( hid_t  layerGroup,
const std::string &  filename,
const std::string &  layerPath,
DataTypeEnum  typeEnum 
)
virtual

Reads the field at the given location and tries to create a DenseField object from it. Calls out to readData() for template-specific work.

Returns
Null if no object was read

Implements FieldIO.

Definition at line 77 of file DenseFieldIO.cpp.

References DataTypeDouble, DataTypeFloat, DataTypeHalf, DataTypeVecDouble, DataTypeVecFloat, DataTypeVecHalf, Hdf5Util::H5Base::id(), and Hdf5Util::readAttribute().

Referenced by create().

80 {
81  Box3i extents, dataW;
82  int components;
83  hsize_t dims[1];
84 
85  if (layerGroup == -1)
86  throw BadHdf5IdException("Bad layer group in DenseFieldIO::read");
87 
88  int version;
89  if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
90  throw MissingAttributeException("Couldn't find attribute " +
92 
93  if (version != k_versionNumber)
94  throw UnsupportedVersionException("DenseField version not supported: " +
95  lexical_cast<std::string>(version));
96 
97  if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x))
98  throw MissingAttributeException("Couldn't find attribute " +
99  k_extentsStr);
100 
101  if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x))
102  throw MissingAttributeException("Couldn't find attribute " +
104 
105  if (!readAttribute(layerGroup, k_componentsStr, 1, components))
106  throw MissingAttributeException("Couldn't find attribute " +
108 
109  H5ScopedDopen dataSet(layerGroup, k_dataStr, H5P_DEFAULT);
110 
111  if (dataSet.id() < 0)
112  throw OpenDataSetException("Couldn't open data set: " + k_dataStr);
113 
114  H5ScopedDget_space dataSpace(dataSet.id());
115  H5ScopedDget_type dataType(dataSet.id());
116  H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
117 
118  if (dataSpace.id() < 0)
119  throw GetDataSpaceException("Couldn't get data space");
120 
121  if (dataType.id() < 0)
122  throw GetDataTypeException("Couldn't get data type");
123 
124  // Double-check that the sizes match ---
125 
126  V3i size(dataW.size() + V3i(1));
127  int calculatedTotal = size.x * size.y * size.z;
128  int reportedSize = dims[0] / components;
129 
130  if (calculatedTotal != reportedSize)
131  throw FileIntegrityException("Data size doesn't match number of voxels");
132 
133  // Build a DenseField to store everything in
134  FieldBase::Ptr result;
135 
136  // Read the data ---
137 
138  bool isHalf, isFloat, isDouble;
139  isHalf = H5Tequal(dataType, H5T_NATIVE_SHORT);
140  isFloat = H5Tequal(dataType, H5T_NATIVE_FLOAT);
141  isDouble = H5Tequal(dataType, H5T_NATIVE_DOUBLE);
142 
143  if (isHalf && components == 1 && typeEnum == DataTypeHalf)
144  result = readData<half>(dataSet.id(), extents, dataW);
145  if (isFloat && components == 1 && typeEnum == DataTypeFloat)
146  result = readData<float>(dataSet.id(), extents, dataW);
147  if (isDouble && components == 1 && typeEnum == DataTypeDouble)
148  result = readData<double>(dataSet.id(), extents, dataW);
149  if (isHalf && components == 3 && typeEnum == DataTypeVecHalf)
150  result = readData<V3h>(dataSet.id(), extents, dataW);
151  if (isFloat && components == 3 && typeEnum == DataTypeVecFloat)
152  result = readData<V3f>(dataSet.id(), extents, dataW);
153  if (isDouble && components == 3 && typeEnum == DataTypeVecDouble)
154  result = readData<V3d>(dataSet.id(), extents, dataW);
155 
156  return result;
157 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:416
static const std::string k_versionAttrName
Definition: DenseFieldIO.h:151
static const std::string k_dataWindowStr
Definition: DenseFieldIO.h:153
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:356
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
static const int k_versionNumber
Definition: DenseFieldIO.h:150
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
static const std::string k_extentsStr
Definition: DenseFieldIO.h:152
Imath::V3i V3i
Definition: SpiMathLib.h:71
static const std::string k_dataStr
Definition: DenseFieldIO.h:156
static const std::string k_componentsStr
Definition: DenseFieldIO.h:154
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:386
bool DenseFieldIO::write ( hid_t  layerGroup,
FieldBase::Ptr  field 
)
virtual

Writes the given field to disk. This function calls out to writeInternal once the template type has been determined.

Returns
true if successful, otherwise false

Implements FieldIO.

Definition at line 162 of file DenseFieldIO.cpp.

References FIELD3D_NAMESPACE_SOURCE_CLOSE, field_dynamic_cast(), and Hdf5Util::writeAttribute().

Referenced by create().

163 {
164  if (layerGroup == -1)
165  throw BadHdf5IdException("Bad layer group in DenseFieldIO::write");
166 
167  // Add version attribute
168  if (!writeAttribute(layerGroup, k_versionAttrName,
169  1, k_versionNumber))
170  throw WriteAttributeException("Couldn't write attribute " +
172 
173  DenseField<half>::Ptr halfField =
175  DenseField<float>::Ptr floatField =
177  DenseField<double>::Ptr doubleField =
179  DenseField<V3h>::Ptr vecHalfField =
181  DenseField<V3f>::Ptr vecFloatField =
183  DenseField<V3d>::Ptr vecDoubleField =
185 
186  bool success = true;
187 
188  if (floatField) {
189  success = writeInternal<float>(layerGroup, floatField);
190  }
191  else if (halfField) {
192  success = writeInternal<half>(layerGroup, halfField);
193  }
194  else if (doubleField) {
195  success = writeInternal<double>(layerGroup, doubleField);
196  }
197  else if (vecFloatField) {
198  success = writeInternal<V3f>(layerGroup, vecFloatField);
199  }
200  else if (vecHalfField) {
201  success = writeInternal<V3h>(layerGroup, vecHalfField);
202  }
203  else if (vecDoubleField) {
204  success = writeInternal<V3d>(layerGroup, vecDoubleField);
205  }
206  else {
207  throw WriteLayerException("DenseFieldIO does not support the given "
208  "DenseField template parameter");
209  }
210 
211  return success;
212 }
This subclass of Field stores data in a contiguous std::vector.
Definition: DenseField.h:85
Field_T::Ptr field_dynamic_cast(RefBase::Ptr field)
Dynamic cast that uses string-comparison in order to be safe even after an object crosses a shared li...
Definition: RefCount.h:256
static const std::string k_versionAttrName
Definition: DenseFieldIO.h:151
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static const int k_versionNumber
Definition: DenseFieldIO.h:150
boost::intrusive_ptr< DenseField > Ptr
Definition: DenseField.h:92
virtual std::string DenseFieldIO::className ( ) const
inlinevirtual

Returns the class name.

Implements FieldIO.

Definition at line 126 of file DenseFieldIO.h.

References readData(), writeData(), and writeInternal().

127  { return "DenseField"; }
template<class Data_T >
bool DenseFieldIO::writeInternal ( hid_t  layerGroup,
typename DenseField< Data_T >::Ptr  field 
)
private

This call writes all the attributes and sets up the data space.

Todo:
Tune the chunk size of the gzip call

Definition at line 171 of file DenseFieldIO.h.

References Hdf5Util::checkHdf5Gzip(), FieldTraits< Data_T >::dataDims(), FieldRes::dataWindow(), FieldRes::extents(), DataTypeTraits< T >::h5bits(), Hdf5Util::H5Base::id(), DenseField< Data_T >::internalMemSize(), k_bitsPerComponentStr, k_componentsStr, k_dataStr, k_dataWindowStr, k_extentsStr, Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

Referenced by className().

173 {
174  using namespace Exc;
175  using namespace Hdf5Util;
176 
177  const V3i& memSize = field->internalMemSize();
178  int size[3];
179  size[0] = memSize.x;
180  size[1] = memSize.y;
181  size[2] = memSize.z;
182 
183  int components = FieldTraits<Data_T>::dataDims();
184 
185  hsize_t totalSize[1];
186  totalSize[0] = size[0] * size[1] * size[2] * components;
187 
188  // Make sure chunk size isn't too big.
189  hsize_t preferredChunkSize = 4096 * 16;
190  const hsize_t chunkSize = std::min(preferredChunkSize, totalSize[0] / 2);
191 
192  Box3i ext(field->extents()), dw(field->dataWindow());
193 
194  // Add extents attribute ---
195 
196  int extents[6] =
197  { ext.min.x, ext.min.y, ext.min.z, ext.max.x, ext.max.y, ext.max.z };
198 
199  if (!writeAttribute(layerGroup, k_extentsStr, 6, extents[0])) {
200  throw WriteAttributeException("Couldn't write attribute " + k_extentsStr);
201  }
202 
203  // Add data window attribute ---
204 
205  int dataWindow[6] =
206  { dw.min.x, dw.min.y, dw.min.z, dw.max.x, dw.max.y, dw.max.z };
207 
208  if (!writeAttribute(layerGroup, k_dataWindowStr, 6, dataWindow[0])) {
209  throw WriteAttributeException("Couldn't write attribute " + k_dataWindowStr);
210  }
211 
212  // Add components attribute ---
213 
214  if (!writeAttribute(layerGroup, k_componentsStr, 1, components)) {
215  throw WriteAttributeException("Couldn't write attribute " + k_componentsStr);
216  }
217 
218  // Add the bits per component attribute ---
219 
220  int bits = DataTypeTraits<Data_T>::h5bits();
221  if (!writeAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) {
222  Msg::print(Msg::SevWarning, "Error adding bits per component attribute.");
223  return false;
224  }
225 
226  // Add data to file ---
227 
228  H5ScopedScreate dataSpace(H5S_SIMPLE);
229 
230  if (dataSpace.id() < 0) {
231  throw CreateDataSpaceException("Couldn't create data space in "
232  "DenseFieldIO::writeInternal");
233  }
234 
235  // Create a "simple" data structure ---
236 
237  H5Sset_extent_simple(dataSpace.id(), 1, totalSize, NULL);
238 
239  // Set up gzip property list
240  bool gzipAvailable = checkHdf5Gzip();
241  hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
242  if (gzipAvailable) {
243  herr_t status = H5Pset_deflate(dcpl, 9);
244  if (status < 0) {
245  return false;
246  }
247  status = H5Pset_chunk(dcpl, 1, &chunkSize);
248  if (status < 0) {
249  return false;
250  }
251  }
252 
253  H5ScopedDcreate dataSet(layerGroup, k_dataStr,
255  dataSpace.id(),
256  H5P_DEFAULT, dcpl, H5P_DEFAULT);
257 
258  if (dataSet.id() < 0) {
259  throw CreateDataSetException("Couldn't create data set in "
260  "DenseFieldIO::writeInternal");
261  }
262 
263  // Call out to the templated function, it will figure out how to get
264  // the data into the file in the appropriate fashion.
265  if (!writeData<Data_T>(dataSet.id(), field, Data_T(0.0f))) {
266  throw WriteLayerException("Error writing layer");
267  }
268 
269  return true;
270 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static int dataDims()
Dimensions of the given data type. i.e. 3 for V3f, 1 for float.
Namespace for Exception objects.
Definition: Exception.h:57
static const std::string k_dataWindowStr
Definition: DenseFieldIO.h:153
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
static const std::string k_extentsStr
Definition: DenseFieldIO.h:152
Imath::V3i V3i
Definition: SpiMathLib.h:71
static const std::string k_bitsPerComponentStr
Definition: DenseFieldIO.h:155
static const std::string k_dataStr
Definition: DenseFieldIO.h:156
FIELD3D_API bool checkHdf5Gzip()
Checks whether gzip is available in the current hdf5 library.
Definition: Hdf5Util.cpp:722
static const std::string k_componentsStr
Definition: DenseFieldIO.h:154
static int h5bits()
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:234
Scoped object - creates a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:263
template<class Data_T >
bool DenseFieldIO::writeData ( hid_t  dataSet,
typename DenseField< Data_T >::Ptr  field,
Data_T  dummy 
)
private

This call performs the actual writing of data to disk.

Definition at line 275 of file DenseFieldIO.h.

References DenseField< Data_T >::begin().

Referenced by className().

278 {
279  using namespace Hdf5Util;
280 
281  hid_t err = H5Dwrite(dataSet,
283  H5S_ALL, H5S_ALL,
284  H5P_DEFAULT, &(*field->begin()));
285 
286  if (err < 0) {
287  throw Exc::WriteLayerException("Error writing layer in "
288  "DenseFieldIO::writeData");
289  }
290 
291  return true;
292 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
template<class Data_T >
DenseField< Data_T >::Ptr DenseFieldIO::readData ( hid_t  dataSet,
const Box3i extents,
const Box3i dataW 
)
private

This call performs the actual reading of data from disk.

Definition at line 298 of file DenseFieldIO.h.

References DenseField< Data_T >::begin(), FIELD3D_NAMESPACE_HEADER_CLOSE, DataTypeTraits< T >::name(), and ResizableField< Data_T >::setSize().

Referenced by className().

299 {
300  typename DenseField<Data_T>::Ptr field(new DenseField<Data_T>);
301  field->setSize(extents, dataW);
302 
303  if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
304  H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin())) < 0)
305  {
306  std::string typeName = "DenseField<" +
308  throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
309  }
310 
311  return field;
312 }
This subclass of Field stores data in a contiguous std::vector.
Definition: DenseField.h:85
static std::string name()
Definition: Traits.h:101
boost::intrusive_ptr< DenseField > Ptr
Definition: DenseField.h:92

Member Data Documentation

DenseFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 90 of file DenseFieldIO.h.

const int DenseFieldIO::k_versionNumber
staticprivate

Definition at line 150 of file DenseFieldIO.h.

const std::string DenseFieldIO::k_versionAttrName
staticprivate

Definition at line 151 of file DenseFieldIO.h.

const std::string DenseFieldIO::k_extentsStr
staticprivate

Definition at line 152 of file DenseFieldIO.h.

Referenced by writeInternal().

const std::string DenseFieldIO::k_dataWindowStr
staticprivate

Definition at line 153 of file DenseFieldIO.h.

Referenced by writeInternal().

const std::string DenseFieldIO::k_componentsStr
staticprivate

Definition at line 154 of file DenseFieldIO.h.

Referenced by writeInternal().

const std::string DenseFieldIO::k_bitsPerComponentStr
staticprivate

Definition at line 155 of file DenseFieldIO.h.

Referenced by writeInternal().

const std::string DenseFieldIO::k_dataStr
staticprivate

Definition at line 156 of file DenseFieldIO.h.

Referenced by writeInternal().


The documentation for this class was generated from the following files: