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 
158 MACFieldIO::read(const OgIGroup & /* layerGroup */,
159  const std::string & /* filename */,
160  const std::string & /* layerPath */,
161  OgDataType /* typeEnum */)
162 {
163  return FieldBase::Ptr();
164 }
165 
166 //----------------------------------------------------------------------------//
167 
168 bool
169 MACFieldIO::write(hid_t layerGroup, FieldBase::Ptr field)
170 {
171  if (layerGroup == -1) {
172  throw BadHdf5IdException("Bad layer group in MACFieldIO::write");
173  }
174 
175  // Add version attribute
176  if (!writeAttribute(layerGroup, k_versionAttrName,
177  1, k_versionNumber)) {
178  throw WriteAttributeException("Couldn't write attribute " +
179  k_versionAttrName);
180  }
181 
182  MACField<V3h>::Ptr vecHalfField =
183  field_dynamic_cast<MACField<V3h> >(field);
184  MACField<V3f>::Ptr vecFloatField =
185  field_dynamic_cast<MACField<V3f> >(field);
186  MACField<V3d>::Ptr vecDoubleField =
187  field_dynamic_cast<MACField<V3d> >(field);
188 
189  bool success = true;
190  if (vecFloatField) {
191  success = writeInternal<V3f>(layerGroup, vecFloatField);
192  } else if (vecHalfField) {
193  success = writeInternal<V3h>(layerGroup, vecHalfField);
194  } else if (vecDoubleField) {
195  success = writeInternal<V3d>(layerGroup, vecDoubleField);
196  } else {
197  throw WriteLayerException("MACFieldIO does not support the given "
198  "MACField template parameter");
199  }
200 
201  return success;
202 }
203 
204 //----------------------------------------------------------------------------//
205 
206 bool
207 MACFieldIO::write(OgOGroup & /* layerGroup */ , FieldBase::Ptr /* field */)
208 {
209  return true;
210 }
211 
212 //----------------------------------------------------------------------------//
213 
215 
216 //----------------------------------------------------------------------------//
Hdf5Util::writeAttribute
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
DataTypeEnum
DataTypeEnum
Definition: Traits.h:108
FieldBase::Ptr
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
DataTypeVecHalf
@ DataTypeVecHalf
Definition: Traits.h:114
MACField::Ptr
boost::intrusive_ptr< MACField > Ptr
Definition: MACField.h:101
DataTypeVecDouble
@ DataTypeVecDouble
Definition: Traits.h:116
MACField
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Definition: MACField.h:96
FIELD3D_NAMESPACE_SOURCE_CLOSE
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
DataTypeVecFloat
@ DataTypeVecFloat
Definition: Traits.h:115
Exc
Namespace for Exception objects.
Definition: Exception.h:57
Hdf5Util::readAttribute
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
Hdf5Util
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
Box3i
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
OgDataType
OgDataType
Enumerates the various uses for Ogawa-level groups.
Definition: Traits.h:125