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
48using namespace boost;
49using namespace std;
50
51//----------------------------------------------------------------------------//
52
54
55//----------------------------------------------------------------------------//
56// Field3D namespaces
57//----------------------------------------------------------------------------//
58
59using namespace Exc;
60using namespace Hdf5Util;
61
62//----------------------------------------------------------------------------//
63// Static members
64//----------------------------------------------------------------------------//
65
66const int MACFieldIO::k_versionNumber(1);
67const std::string MACFieldIO::k_versionAttrName("version");
68const std::string MACFieldIO::k_extentsStr("extents");
69const std::string MACFieldIO::k_dataWindowStr("data_window");
70const std::string MACFieldIO::k_componentsStr("components");
71const std::string MACFieldIO::k_bitsPerComponentStr("bits_per_component");
72const std::string MACFieldIO::k_uDataStr("u_data");
73const std::string MACFieldIO::k_vDataStr("v_data");
74const std::string MACFieldIO::k_wDataStr("w_data");
75
76//----------------------------------------------------------------------------//
77
79MACFieldIO::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
158MACFieldIO::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
168bool
169MACFieldIO::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 =
184 MACField<V3f>::Ptr vecFloatField =
186 MACField<V3d>::Ptr vecDoubleField =
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
206bool
207MACFieldIO::write(OgOGroup & /* layerGroup */ , FieldBase::Ptr /* field */)
208{
209 return true;
210}
211
212//----------------------------------------------------------------------------//
213
215
216//----------------------------------------------------------------------------//
Imath::Box3i Box3i
Definition SpiMathLib.h:77
OgDataType
Enumerates the various uses for Ogawa-level groups.
Definition Traits.h:125
DataTypeEnum
Definition Traits.h:108
@ DataTypeVecHalf
Definition Traits.h:114
@ DataTypeVecDouble
Definition Traits.h:116
@ DataTypeVecFloat
Definition Traits.h:115
boost::intrusive_ptr< FieldBase > Ptr
Definition Field.h:97
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Definition MACField.h:96
boost::intrusive_ptr< MACField > Ptr
Definition MACField.h:101
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
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
Namespace for Exception objects.
Definition Exception.h:57
Contains utility functions and classes for Hdf5 files.
Definition Hdf5Util.h:86
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition ns.h:60