Field3D
MACFieldIO.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2009 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
42 //----------------------------------------------------------------------------//
43 
44 #include "MACFieldIO.h"
45 
46 //----------------------------------------------------------------------------//
47 
48 using namespace boost;
49 using namespace std;
50 
51 //----------------------------------------------------------------------------//
52 
54 
55 //----------------------------------------------------------------------------//
56 // Field3D namespaces
57 //----------------------------------------------------------------------------//
58 
59 using namespace Exc;
60 using namespace Hdf5Util;
61 
62 //----------------------------------------------------------------------------//
63 // Static members
64 //----------------------------------------------------------------------------//
65 
66 const int MACFieldIO::k_versionNumber(1);
67 const std::string MACFieldIO::k_versionAttrName("version");
68 const std::string MACFieldIO::k_extentsStr("extents");
69 const std::string MACFieldIO::k_dataWindowStr("data_window");
70 const std::string MACFieldIO::k_componentsStr("components");
71 const std::string MACFieldIO::k_bitsPerComponentStr("bits_per_component");
72 const std::string MACFieldIO::k_uDataStr("u_data");
73 const std::string MACFieldIO::k_vDataStr("v_data");
74 const std::string MACFieldIO::k_wDataStr("w_data");
75 
76 //----------------------------------------------------------------------------//
77 
79 MACFieldIO::read(hid_t layerGroup, const std::string & /* filename */,
80  const std::string & /* layerPath */,
81  DataTypeEnum typeEnum)
82 {
83  Box3i extents, dataW;
84  int components;
85 
86  //hsize_t dims[1];
87 
88  if (layerGroup == -1)
89  throw BadHdf5IdException("Bad layer group in MACFieldIO::read");
90 
91  int version;
92  if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
93  throw MissingAttributeException("Couldn't find attribute " +
94  k_versionAttrName);
95 
96  if (version != k_versionNumber)
97  throw UnsupportedVersionException("MACField version not supported: " +
98  lexical_cast<std::string>(version));
99 
100  if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x))
101  throw MissingAttributeException("Couldn't find attribute " +
102  k_extentsStr);
103 
104  if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x))
105  throw MissingAttributeException("Couldn't find attribute " +
106  k_dataWindowStr);
107 
108  if (!readAttribute(layerGroup, k_componentsStr, 1, components))
109  throw MissingAttributeException("Couldn't find attribute " +
110  k_componentsStr);
111  // Check the data type ---
112  int bits;
113  if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits))
114  throw MissingAttributeException("Couldn't find attribute: " +
115  k_bitsPerComponentStr);
116 
117  // Build a MACField to store everything in
118  FieldBase::Ptr result;
119  switch (bits) {
120  case 16:
121  {
122  if (typeEnum != DataTypeVecHalf) break;
124  field->setSize(extents, dataW);
125  readData<V3h>(layerGroup, field);
126 
127  result = field;
128  }
129  break;
130  case 64:
131  {
132  if (typeEnum != DataTypeVecDouble) break;
134  field->setSize(extents, dataW);
135  readData<V3d>(layerGroup, field);
136 
137  result = field;
138  }
139  break;
140  case 32:
141  default:
142  {
143  if (typeEnum != DataTypeVecFloat) break;
145  field->setSize(extents, dataW);
146  readData<V3f>(layerGroup, field);
147 
148  result = field;
149  }
150  }
151 
152  return result;
153 }
154 
155 //----------------------------------------------------------------------------//
156 
157 bool
158 MACFieldIO::write(hid_t layerGroup, FieldBase::Ptr field)
159 {
160  if (layerGroup == -1) {
161  throw BadHdf5IdException("Bad layer group in MACFieldIO::write");
162  }
163 
164  // Add version attribute
165  if (!writeAttribute(layerGroup, k_versionAttrName,
166  1, k_versionNumber)) {
167  throw WriteAttributeException("Couldn't write attribute " +
168  k_versionAttrName);
169  }
170 
171  MACField<V3h>::Ptr vecHalfField =
173  MACField<V3f>::Ptr vecFloatField =
175  MACField<V3d>::Ptr vecDoubleField =
177 
178  bool success = true;
179  if (vecFloatField) {
180  success = writeInternal<V3f>(layerGroup, vecFloatField);
181  } else if (vecHalfField) {
182  success = writeInternal<V3h>(layerGroup, vecHalfField);
183  } else if (vecDoubleField) {
184  success = writeInternal<V3d>(layerGroup, vecDoubleField);
185  } else {
186  throw WriteLayerException("MACFieldIO does not support the given "
187  "MACField template parameter");
188  }
189 
190  return success;
191 }
192 
193 //----------------------------------------------------------------------------//
194 
196 
197 //----------------------------------------------------------------------------//
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
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
static const std::string k_vDataStr
Definition: MACFieldIO.h:154
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
Namespace for Exception objects.
Definition: Exception.h:57
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Definition: MACField.h:95
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
static const std::string k_componentsStr
Definition: MACFieldIO.h:151
static const std::string k_uDataStr
Definition: MACFieldIO.h:153
static const int k_versionNumber
Definition: MACFieldIO.h:147
void setSize(const V3i &size)
Resizes the object.
Definition: Field.h:918
boost::intrusive_ptr< MACField > Ptr
Definition: MACField.h:101
static const std::string k_wDataStr
Definition: MACFieldIO.h:155
Contains the MACFieldIO class.
static const std::string k_extentsStr
Definition: MACFieldIO.h:149
static const std::string k_versionAttrName
Definition: MACFieldIO.h:148
static const std::string k_dataWindowStr
Definition: MACFieldIO.h:150
static const std::string k_bitsPerComponentStr
Definition: MACFieldIO.h:152
virtual bool write(hid_t layerGroup, FieldBase::Ptr field)
Writes the given field to disk.
Definition: MACFieldIO.cpp:158
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 MACField object from it.
Definition: MACFieldIO.cpp:79
DataTypeEnum
Definition: Traits.h:66