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
 

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...
 
 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 const char * classType ()
 
static FieldIO::Ptr create ()
 
- Static Public Member Functions inherited from FieldIO
static const char * classType ()
 
- Static Public Member Functions inherited from RefBase
static const char * classType ()
 

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_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

◆ Ptr

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

Definition at line 85 of file DenseFieldIO.h.

◆ class_type

Definition at line 89 of file DenseFieldIO.h.

◆ base

typedef FieldIO DenseFieldIO::base
private

Convenience typedef for referring to base class.

Definition at line 160 of file DenseFieldIO.h.

Constructor & Destructor Documentation

◆ DenseFieldIO()

DenseFieldIO::DenseFieldIO ( )
inline

Ctor.

Definition at line 100 of file DenseFieldIO.h.

101  : FieldIO()
102  { }
FieldIO()
Ctor.
Definition: FieldIO.h:105

◆ ~DenseFieldIO()

virtual DenseFieldIO::~DenseFieldIO ( )
inlinevirtual

Dtor.

Definition at line 105 of file DenseFieldIO.h.

106  { /* Empty */ }

Member Function Documentation

◆ classType()

static const char* DenseFieldIO::classType ( )
inlinestatic

Definition at line 92 of file DenseFieldIO.h.

93  {
94  return "DenseFieldIO";
95  }

◆ create()

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

◆ read()

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 76 of file DenseFieldIO.cpp.

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

Referenced by create().

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

◆ write()

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 161 of file DenseFieldIO.cpp.

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

Referenced by create().

162 {
163  if (layerGroup == -1)
164  throw BadHdf5IdException("Bad layer group in DenseFieldIO::write");
165 
166  // Add version attribute
167  if (!writeAttribute(layerGroup, k_versionAttrName,
168  1, k_versionNumber))
169  throw WriteAttributeException("Couldn't write attribute " +
171 
172  DenseField<half>::Ptr halfField =
174  DenseField<float>::Ptr floatField =
176  DenseField<double>::Ptr doubleField =
178  DenseField<V3h>::Ptr vecHalfField =
180  DenseField<V3f>::Ptr vecFloatField =
182  DenseField<V3d>::Ptr vecDoubleField =
184 
185  bool success = true;
186 
187  if (floatField) {
188  success = writeInternal<float>(layerGroup, floatField);
189  }
190  else if (halfField) {
191  success = writeInternal<half>(layerGroup, halfField);
192  }
193  else if (doubleField) {
194  success = writeInternal<double>(layerGroup, doubleField);
195  }
196  else if (vecFloatField) {
197  success = writeInternal<V3f>(layerGroup, vecFloatField);
198  }
199  else if (vecHalfField) {
200  success = writeInternal<V3h>(layerGroup, vecHalfField);
201  }
202  else if (vecDoubleField) {
203  success = writeInternal<V3d>(layerGroup, vecDoubleField);
204  }
205  else {
206  throw WriteLayerException("DenseFieldIO does not support the given "
207  "DenseField template parameter");
208  }
209 
210  return success;
211 }
This subclass of Field stores data in a contiguous std::vector.
Definition: DenseField.h:82
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:224
static const std::string k_versionAttrName
Definition: DenseFieldIO.h:151
static const int k_versionNumber
Definition: DenseFieldIO.h:150
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
boost::intrusive_ptr< DenseField > Ptr
Definition: DenseField.h:89

◆ className()

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"; }

◆ writeInternal()

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 170 of file DenseFieldIO.h.

References Hdf5Util::checkHdf5Gzip(), FieldTraits< Data_T >::dataDims(), FieldRes::dataWindow(), FieldRes::extents(), Hdf5Util::H5Base::id(), DenseField< Data_T >::internalMemSize(), k_componentsStr, k_dataStr, k_dataWindowStr, k_extentsStr, and Hdf5Util::writeAttribute().

Referenced by className().

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

◆ writeData()

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 266 of file DenseFieldIO.h.

References DenseField< Data_T >::begin().

Referenced by className().

269 {
270  using namespace Hdf5Util;
271 
272  hid_t err = H5Dwrite(dataSet,
274  H5S_ALL, H5S_ALL,
275  H5P_DEFAULT, &(*field->begin()));
276 
277  if (err < 0) {
278  throw Exc::WriteLayerException("Error writing layer in "
279  "DenseFieldIO::writeData");
280  }
281 
282  return true;
283 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76

◆ readData()

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 289 of file DenseFieldIO.h.

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

Referenced by className().

290 {
291  typename DenseField<Data_T>::Ptr field(new DenseField<Data_T>);
292  field->setSize(extents, dataW);
293 
294  if (H5Dread(dataSet, DataTypeTraits<Data_T>::h5type(),
295  H5S_ALL, H5S_ALL, H5P_DEFAULT, &(*field->begin())) < 0)
296  {
297  std::string typeName = "DenseField<" +
299  throw Exc::Hdf5DataReadException("Couldn't read " + typeName + " data");
300  }
301 
302  return field;
303 }
This subclass of Field stores data in a contiguous std::vector.
Definition: DenseField.h:82
static std::string name()
Definition: Traits.h:101
boost::intrusive_ptr< DenseField > Ptr
Definition: DenseField.h:89

Member Data Documentation

◆ DEFINE_FIELD_RTTI_CONCRETE_CLASS

DenseFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 90 of file DenseFieldIO.h.

◆ k_versionNumber

const int DenseFieldIO::k_versionNumber
staticprivate

Definition at line 150 of file DenseFieldIO.h.

◆ k_versionAttrName

const std::string DenseFieldIO::k_versionAttrName
staticprivate

Definition at line 151 of file DenseFieldIO.h.

◆ k_extentsStr

const std::string DenseFieldIO::k_extentsStr
staticprivate

Definition at line 152 of file DenseFieldIO.h.

Referenced by writeInternal().

◆ k_dataWindowStr

const std::string DenseFieldIO::k_dataWindowStr
staticprivate

Definition at line 153 of file DenseFieldIO.h.

Referenced by writeInternal().

◆ k_componentsStr

const std::string DenseFieldIO::k_componentsStr
staticprivate

Definition at line 154 of file DenseFieldIO.h.

Referenced by writeInternal().

◆ k_dataStr

const std::string DenseFieldIO::k_dataStr
staticprivate

Definition at line 155 of file DenseFieldIO.h.

Referenced by writeInternal().


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