Field3D
FieldMappingIO.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 
43 //----------------------------------------------------------------------------//
44 
45 #include "Hdf5Util.h"
46 
47 #include "FieldMappingIO.h"
48 
49 //----------------------------------------------------------------------------//
50 
52 
53 //----------------------------------------------------------------------------//
54 // Field3D namespaces
55 //----------------------------------------------------------------------------//
56 
57 using namespace std;
58 using namespace Exc;
59 using namespace Hdf5Util;
60 
61 //----------------------------------------------------------------------------//
62 
63 namespace {
65  const string k_nullMappingName("NullFieldMapping");
67  const string k_matrixMappingName("MatrixFieldMapping");
68  const string k_frustumMappingName("FrustumFieldMapping");
69 
70  const string k_nullMappingDataName("NullFieldMapping data");
71 
72  const string k_matrixMappingDataName("MatrixFieldMapping data");
73  const string k_matrixMappingNumSamples("num_time_samples");
74  const string k_matrixMappingTime("time_");
75  const string k_matrixMappingMatrix("matrix_");
76 
77  const string k_frustumMappingNumSamples("num_time_samples");
78  const string k_frustumMappingTime("time_");
79  const string k_frustumMappingScreenMatrix("screen_to_world_");
80  const string k_frustumMappingCameraMatrix("camera_to_world_");
81  const string k_frustumMappingZDistribution("z_distribution");
82 }
83 
84 //----------------------------------------------------------------------------//
85 
87 NullFieldMappingIO::read(hid_t mappingGroup)
88 {
89  string nfmData;
90  if (!readAttribute(mappingGroup, k_nullMappingDataName, nfmData)) {
91  Msg::print(Msg::SevWarning, "Couldn't read attribute " + k_nullMappingDataName);
92  return NullFieldMapping::Ptr();
93  }
95 }
96 
97 //----------------------------------------------------------------------------//
98 
99 bool
100 NullFieldMappingIO::write(hid_t mappingGroup, FieldMapping::Ptr /* nm */)
101 {
102  string nfmAttrData("NullFieldMapping has no data");
103  if (!writeAttribute(mappingGroup, k_nullMappingDataName, nfmAttrData)) {
104  Msg::print(Msg::SevWarning, "Couldn't add attribute " + k_nullMappingDataName);
105  return false;
106  }
107  return true;
108 }
109 
110 //----------------------------------------------------------------------------//
111 
112 std::string NullFieldMappingIO::className() const
113 {
114  return k_nullMappingName;
115 }
116 
117 //----------------------------------------------------------------------------//
118 // MatrixFieldMapping
119 //----------------------------------------------------------------------------//
120 
122 MatrixFieldMappingIO::read(hid_t mappingGroup)
123 {
124  M44d mtx;
125  int numSamples=0;
126 
128 
129  // For backward compatibility, we first try to read the non-time-varying
130  // mapping.
131 
132  try {
133  readAttribute(mappingGroup, k_matrixMappingDataName, 16, mtx.x[0][0]);
134  mm->setLocalToWorld(mtx);
135  return mm;
136  }
137  catch (...) {
138  // Do nothing
139  }
140 
141  // If we didn't find the non-time-varying matrix data then we attempt
142  // to read time samples
143 
144  try {
145  if (!readAttribute(mappingGroup, k_matrixMappingNumSamples, 1, numSamples)) {
146  Msg::print(Msg::SevWarning, "Couldn't read attribute " +
147  k_matrixMappingNumSamples);
148  return FieldMapping::Ptr();
149  }
150  } catch (...) {
151  //do nothing
152  }
153 
154  for (int i = 0; i < numSamples; ++i) {
155  float time;
156  string timeAttr = k_matrixMappingTime + boost::lexical_cast<string>(i);
157  string matrixAttr = k_matrixMappingMatrix + boost::lexical_cast<string>(i);
158  if (!readAttribute(mappingGroup, timeAttr, 1, time)) {
159  Msg::print(Msg::SevWarning, "Couldn't read attribute " + timeAttr);
160  return FieldMapping::Ptr();
161  }
162  std::vector<unsigned int> attrSize;
163  attrSize.assign(2,4);
164 
165  if (!readAttribute(mappingGroup, matrixAttr, attrSize, mtx.x[0][0])) {
166  Msg::print(Msg::SevWarning, "Couldn't read attribute " + matrixAttr);
167  return FieldMapping::Ptr();
168  }
169  mm->setLocalToWorld(time, mtx);
170  }
171 
172  return mm;
173 }
174 
175 //----------------------------------------------------------------------------//
176 
177 bool
178 MatrixFieldMappingIO::write(hid_t mappingGroup, FieldMapping::Ptr mapping)
179 {
181 
183  FIELD_DYNAMIC_CAST<MatrixFieldMapping>(mapping);
184 
185  if (!mm) {
186  Msg::print(Msg::SevWarning, "Couldn't get MatrixFieldMapping from pointer");
187  return false;
188  }
189 
190  // First write number of time samples
191 
192  const SampleVec &samples = mm->localToWorldSamples();
193  int numSamples = static_cast<int>(samples.size());
194 
195  if (!writeAttribute(mappingGroup, k_matrixMappingNumSamples, 1, numSamples)) {
196  Msg::print(Msg::SevWarning, "Couldn't add attribute " +
197  k_matrixMappingNumSamples);
198  return false;
199  }
200 
201  // Then write each sample
202 
203  for (int i = 0; i < numSamples; ++i) {
204  string timeAttr = k_matrixMappingTime + boost::lexical_cast<string>(i);
205  string matrixAttr = k_matrixMappingMatrix + boost::lexical_cast<string>(i);
206  if (!writeAttribute(mappingGroup, timeAttr, 1, samples[i].first)) {
207  Msg::print(Msg::SevWarning, "Couldn't add attribute " + timeAttr);
208  return false;
209  }
210  std::vector<unsigned int> attrSize;
211  attrSize.assign(2,4);
212  if (!writeAttribute(mappingGroup, matrixAttr, attrSize,
213  samples[i].second.x[0][0])) {
214  Msg::print(Msg::SevWarning, "Couldn't add attribute " + matrixAttr);
215  return false;
216  }
217  }
218 
219  return true;
220 }
221 
222 //----------------------------------------------------------------------------//
223 
225 {
226  return k_matrixMappingName;
227 }
228 
229 //----------------------------------------------------------------------------//
230 // FrustumFieldMapping
231 //----------------------------------------------------------------------------//
232 
234 FrustumFieldMappingIO::read(hid_t mappingGroup)
235 {
236  float time;
237  M44d ssMtx, csMtx;
238  int numSamples=0;
239 
241 
242  // Read number of time samples
243 
244  try {
245  if (!readAttribute(mappingGroup, k_frustumMappingNumSamples, 1, numSamples)) {
246  Msg::print(Msg::SevWarning, "Couldn't read attribute " +
247  k_frustumMappingNumSamples);
248  return FieldMapping::Ptr();
249  }
250  } catch (...) {
251  //do nothing
252  }
253 
254  // Read each time sample
255 
256  for (int i = 0; i < numSamples; ++i) {
257  string timeAttr = k_frustumMappingTime + boost::lexical_cast<string>(i);
258  string ssAttr = k_frustumMappingScreenMatrix + boost::lexical_cast<string>(i);
259  string csAttr = k_frustumMappingCameraMatrix + boost::lexical_cast<string>(i);
260  if (!readAttribute(mappingGroup, timeAttr, 1, time)) {
261  Msg::print(Msg::SevWarning, "Couldn't read attribute " + timeAttr);
262  return FieldMapping::Ptr();
263  }
264  std::vector<unsigned int> attrSize;
265  attrSize.assign(2,4);
266 
267  if (!readAttribute(mappingGroup, ssAttr, attrSize, ssMtx.x[0][0])) {
268  Msg::print(Msg::SevWarning, "Couldn't read attribute " + ssAttr);
269  return FieldMapping::Ptr();
270  }
271  if (!readAttribute(mappingGroup, csAttr, attrSize, csMtx.x[0][0])) {
272  Msg::print(Msg::SevWarning, "Couldn't read attribute " + csAttr);
273  return FieldMapping::Ptr();
274  }
275 
276  fm->setTransforms(time, ssMtx, csMtx);
277  }
278 
279 
280  // Read Z distribution
281 
282  int distInt;
284 
285  try {
286  if (!readAttribute(mappingGroup, k_frustumMappingZDistribution, 1, distInt)) {
287  Msg::print(Msg::SevWarning, "Couldn't read attribute " +
288  k_frustumMappingZDistribution);
289  return FieldMapping::Ptr();
290  }
291  dist = static_cast<FrustumFieldMapping::ZDistribution>(distInt);
292  } catch (...) {
294  }
295 
296  fm->setZDistribution(dist);
297 
298  return fm;
299 }
300 
301 //----------------------------------------------------------------------------//
302 
303 bool
305 {
307 
309  FIELD_DYNAMIC_CAST<FrustumFieldMapping>(mapping);
310 
311  if (!fm) {
312  Msg::print(Msg::SevWarning, "Couldn't get FrustumFieldMapping from pointer");
313  return false;
314  }
315 
316  // First write number of time samples
317 
318  const SampleVec &ssSamples = fm->screenToWorldSamples();
319  const SampleVec &csSamples = fm->cameraToWorldSamples();
320  int numSamples = static_cast<int>(ssSamples.size());
321 
322  if (!writeAttribute(mappingGroup, k_frustumMappingNumSamples, 1, numSamples)) {
323  Msg::print(Msg::SevWarning, "Couldn't add attribute " +
324  k_frustumMappingNumSamples);
325  return false;
326  }
327 
328  // Then write each sample
329 
330  for (int i = 0; i < numSamples; ++i) {
331  string timeAttr = k_frustumMappingTime + boost::lexical_cast<string>(i);
332  string ssAttr = k_frustumMappingScreenMatrix + boost::lexical_cast<string>(i);
333  string csAttr = k_frustumMappingCameraMatrix + boost::lexical_cast<string>(i);
334  if (!writeAttribute(mappingGroup, timeAttr, 1, ssSamples[i].first)) {
335  Msg::print(Msg::SevWarning, "Couldn't add attribute " + timeAttr);
336  return false;
337  }
338 
339  std::vector<unsigned int> attrSize;
340  attrSize.assign(2,4);
341 
342  if (!writeAttribute(mappingGroup, ssAttr,attrSize,
343  ssSamples[i].second.x[0][0])) {
344  Msg::print(Msg::SevWarning, "Couldn't add attribute " + ssAttr);
345  return false;
346  }
347  if (!writeAttribute(mappingGroup, csAttr, attrSize,
348  csSamples[i].second.x[0][0])) {
349  Msg::print(Msg::SevWarning, "Couldn't add attribute " + csAttr);
350  return false;
351  }
352  }
353 
354  // Write distribution type
355 
356  int dist = static_cast<int>(fm->zDistribution());
357 
358  if (!writeAttribute(mappingGroup, k_frustumMappingZDistribution, 1, dist)) {
359  Msg::print(Msg::SevWarning, "Couldn't add attribute " +
360  k_frustumMappingNumSamples);
361  return false;
362  }
363 
364  return true;
365 }
366 
367 //----------------------------------------------------------------------------//
368 
370 {
371  return k_frustumMappingName;
372 }
373 
374 //----------------------------------------------------------------------------//
375 
377 
378 //----------------------------------------------------------------------------//
Trivial class, world space is equal to local space, i.e. the field is contained in the unit cube [0...
Definition: FieldMapping.h:229
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
Imath::M44d M44d
Definition: SpiMathLib.h:82
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
const string k_matrixMappingName("MatrixFieldMapping")
virtual FieldMapping::Ptr read(hid_t mappingGroup)
Reads the field mapping and tries to create a NullFieldMapping object from it.
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
virtual std::string className() const
Returns the class name.
virtual std::string className() const
Returns the class name.
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
virtual bool write(hid_t mappingGroup, FieldMapping::Ptr mapping)
Writes the given field mapping to disk.
Contains various utility functions for Hdf5.
boost::intrusive_ptr< FieldMapping > Ptr
Definition: FieldMapping.h:92
boost::intrusive_ptr< NullFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:236
virtual bool write(hid_t mappingGroup, FieldMapping::Ptr mapping)
Writes the given field mapping to disk.
Represents the mapping of a field by a perspective transform.
Definition: FieldMapping.h:547
ZDistribution
Enumerates the Z slice distribution. .f3d files will store values as an int, so be very careful not t...
Definition: FieldMapping.h:568
virtual bool write(hid_t mappingGroup, FieldMapping::Ptr mapping)
Writes the given field mapping to disk.
const string k_nullMappingName("NullFieldMapping")
virtual std::string className() const
Returns the class name.
std::vector< Sample > SampleVec
Definition: Curve.h:85
virtual FieldMapping::Ptr read(hid_t mappingGroup)
Reads the field mapping and tries to create a FrustumFieldMapping object from it. ...
virtual FieldMapping::Ptr read(hid_t mappingGroup)
Reads the field mapping and tries to create a MatrixFieldMapping object from it.
Represents the mapping of a field by a matrix transform.
Definition: FieldMapping.h:327
boost::intrusive_ptr< MatrixFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:334
boost::intrusive_ptr< FrustumFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:554
const string k_frustumMappingName("FrustumFieldMapping")
Contains the FieldMappingIO base class and the NullFieldMappingIO and MatrixFieldMappingIO subclasses...