Field3D
Field3DFile.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------//
2
3/*
4 * Copyright (c) 2014 Sony Pictures Imageworks Inc.,
5 * Pixar Animation Studios Inc.
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution. Neither the name of Sony Pictures Imageworks nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37//----------------------------------------------------------------------------//
38
46//----------------------------------------------------------------------------//
47
48#ifndef _INCLUDED_Field3D_Field3DFile_H_
49#define _INCLUDED_Field3D_Field3DFile_H_
50
51//----------------------------------------------------------------------------//
52
53#include <list>
54#include <string>
55#include <vector>
56
57#include <boost/shared_ptr.hpp>
58
59#include "EmptyField.h"
60#include "Field.h"
61#include "Field3DFileHDF5.h"
62#include "FieldMetadata.h"
63#include "ClassFactory.h"
64#include "OgawaFwd.h"
65
66//----------------------------------------------------------------------------//
67
68#include "ns.h"
69
70//----------------------------------------------------------------------------//
71
73
74//----------------------------------------------------------------------------//
75// Forward declarations
76//----------------------------------------------------------------------------//
77
80
81//----------------------------------------------------------------------------//
82// Layer
83//----------------------------------------------------------------------------//
84
87namespace File {
88
96class Layer
97{
98public:
100 std::string name;
103 std::string parent;
104};
105
106} // namespace File
107
108//----------------------------------------------------------------------------//
109// Partition
110//----------------------------------------------------------------------------//
111
112namespace File {
113
122{
123public:
124
125 typedef std::vector<Layer> LayerList;
126
127 typedef boost::intrusive_ptr<Partition> Ptr;
128 typedef boost::intrusive_ptr<const Partition> CPtr;
129
130 // RTTI replacement ----------------------------------------------------------
131
134
135 static const char *staticClassType()
136 {
137 return "Partition";
138 }
139
140 // Ctors, dtor ---------------------------------------------------------------
141
144 : RefBase()
145 { }
146
147 // From RefBase --------------------------------------------------------------
148
151
152 virtual std::string className() const;
153
155
156 // Main methods --------------------------------------------------------------
157
159 void addLayer(const File::Layer &layer);
160
162 const File::Layer* layer(const std::string &name) const;
163
165 void getLayerNames(std::vector<std::string> &names) const;
166
168 OgOGroup& group() const;
170 void setGroup(boost::shared_ptr<OgOGroup> ptr);
171
172 // Public data members -------------------------------------------------------
173
175 std::string name;
178
179private:
180
181 // Private data members ------------------------------------------------------
182
186 boost::shared_ptr<OgOGroup> m_group;
187
188 // Typedefs ------------------------------------------------------------------
189
191 typedef RefBase base;
192
193};
194
195} // namespace File
196
197//----------------------------------------------------------------------------//
198// Field3DFileBase
199//----------------------------------------------------------------------------//
200
208//----------------------------------------------------------------------------//
209
211{
212public:
213
214 // Structs -------------------------------------------------------------------
215
216 struct LayerInfo
217 {
218 std::string name;
219 std::string parentName;
221 LayerInfo(std::string par, std::string nm, int cpt)
222 : name(nm), parentName(par), components(cpt)
223 { /* Empty */ }
224 };
225
226 // Typedefs ------------------------------------------------------------------
227
228 typedef std::map<std::string, std::string> GroupMembershipMap;
229
230 // Ctor, dtor ----------------------------------------------------------------
231
234
237 virtual ~Field3DFileBase() = 0;
238
240
241 // Main methods --------------------------------------------------------------
242
244 void clear();
245
249 bool close();
250
253
255 void getPartitionNames(std::vector<std::string> &names) const;
257 void getScalarLayerNames(std::vector<std::string> &names,
258 const std::string &partitionName) const;
260 void getVectorLayerNames(std::vector<std::string> &names,
261 const std::string &partitionName) const;
262
264
267
269 void addGroupMembership(const GroupMembershipMap &groupMembers);
270
272
273 // Access to metadata --------------------------------------------------------
274
277 {
278 if (m_hdf5Base) {
279 return m_hdf5Base->metadata();
280 }
281 return m_metadata;
282 }
283
285 const FieldMetadata& metadata() const
286 {
287 if (m_hdf5Base) {
288 return m_hdf5Base->metadata();
289 }
290 return m_metadata;
291 }
292
295 virtual void metadataHasChanged(const std::string &/* name */)
296 { /* Empty */ }
297
298 // Debug ---------------------------------------------------------------------
299
302
303 void printHierarchy() const;
304
306
307protected:
308
309 // Internal typedefs ---------------------------------------------------------
310
311 typedef std::vector<File::Partition::Ptr> PartitionList;
312 typedef std::map<std::string, int> PartitionCountMap;
313
314 // Convenience methods -------------------------------------------------------
315
318
321 File::Partition::Ptr getPartition(const std::string &partitionName) const
322 { return partition(partitionName); }
323
325 virtual void closeInternal() = 0;
328 File::Partition::Ptr partition(const std::string &partitionName);
331 File::Partition::Ptr partition(const std::string &partitionName) const;
332
334 void getIntPartitionNames(std::vector<std::string> &names) const;
337 void getIntScalarLayerNames(std::vector<std::string> &names,
338 const std::string &intPartitionName) const;
341 void getIntVectorLayerNames(std::vector<std::string> &names,
342 const std::string &intPartitionName) const;
343
345 int numIntPartitions(const std::string &partitionName) const;
346
349 std::string makeIntPartitionName(const std::string &partitionsName,
350 int i) const;
351
355 std::string intPartitionName(const std::string &partitionName,
356 const std::string &layerName,
357 FieldRes::Ptr field);
358
361 std::string removeUniqueId(const std::string &partitionName) const;
362
364
365 // Data members --------------------------------------------------------------
366
368 std::vector<LayerInfo> m_layerInfo;
369
373 std::vector<std::string> m_partitionNames;
374
378
383
386
388 boost::shared_ptr<Field3DFileHDF5Base> m_hdf5Base;
389
390private:
391
392 // Private member functions --------------------------------------------------
393
396
397};
398
399//----------------------------------------------------------------------------//
400// Field3DInputFile
401//----------------------------------------------------------------------------//
402
411//----------------------------------------------------------------------------//
412
414{
415public:
416
417 // Ctors, dtor ---------------------------------------------------------------
418
421
423 virtual ~Field3DInputFile();
424
426
427 // Main interface ------------------------------------------------------------
428
431 bool open(const std::string &filename);
432
434 const std::string &encoding() const
435 {
436 const static std::string encodings[2] = { "Ogawa", "HDF5" };
437 return encodings[m_hdf5 ? 1 : 0];
438 }
439
442
443 template <class Data_T>
444 typename Field<Data_T>::Vec
445 readLayers(const std::string &layerName = std::string("")) const;
446
447 template <class Data_T>
448 typename Field<Data_T>::Vec
449 readLayers(const std::string &partitionName,
450 const std::string &layerName) const;
451
454
459 template <class Data_T>
460 typename Field<Data_T>::Vec
461 readScalarLayers(const std::string &layerName = std::string("")) const
462 {
463 if (m_hdf5) {
464 return m_hdf5->readScalarLayers<Data_T>(layerName);
465 }
466 return readLayers<Data_T>(layerName);
467 }
468
470 template <class Data_T>
471 typename Field<Data_T>::Vec
472 readScalarLayers(const std::string &partitionName,
473 const std::string &layerName) const
474 {
475 if (m_hdf5) {
476 return m_hdf5->readScalarLayers<Data_T>(partitionName, layerName);
477 }
478 return readLayers<Data_T>(partitionName, layerName);
479 }
480
485 template <class Data_T>
486 typename Field<FIELD3D_VEC3_T<Data_T> >::Vec
487 readVectorLayers(const std::string &layerName = std::string("")) const
488 {
489 if (m_hdf5) {
490 return m_hdf5->readVectorLayers<Data_T>(layerName);
491 }
492 return readLayers<FIELD3D_VEC3_T<Data_T> >(layerName);
493 }
494
496 template <class Data_T>
497 typename Field<FIELD3D_VEC3_T<Data_T> >::Vec
498 readVectorLayers(const std::string &partitionName,
499 const std::string &layerName) const
500 {
501 if (m_hdf5) {
502 return m_hdf5->readVectorLayers<Data_T>(partitionName, layerName);
503 }
504 return readLayers<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName);
505 }
506
508
511
517 template <class Data_T>
519 readProxyLayer(const std::string &partitionName,
520 const std::string &layerName,
521 bool isVectorLayer) const;
522
528 template <class Data_T>
530 readProxyScalarLayers(const std::string &name = std::string("")) const;
531
537 template <class Data_T>
539 readProxyVectorLayers(const std::string &name = std::string("")) const;
540
542
543private:
544
545 // From Field3DFileBase ------------------------------------------------------
546
547 virtual void closeInternal()
548 {
549 if (m_hdf5) {
550 m_hdf5->closeInternal();
551 return;
552 }
553
554 cleanup();
555 }
556
557 void cleanup()
558 {
559 // The destruction of the various Ogawa components must happen in the
560 // right order
561
562 // First, the partition groups
563 m_partitions.clear();
564 // Then the root group
565 m_root.reset();
566 // Finally, the archive
567 m_archive.reset();
568 }
569
570 // Convenience methods -------------------------------------------------------
571
574 template <class Data_T>
575 typename Field<Data_T>::Ptr
576 readLayer(const std::string &intPartitionName,
577 const std::string &layerName) const;
578
582 template <class Data_T>
584 readProxyLayer(OgIGroup &location, const std::string &name,
585 const std::string &attribute,
586 FieldMapping::Ptr mapping) const;
587
589 bool readPartitionAndLayerInfo();
590
592 bool readMetadata(const OgIGroup &metadataGroup, FieldBase::Ptr field) const;
593
595 bool readMetadata(const OgIGroup &metadataGroup);
596
597 // Data members --------------------------------------------------------------
598
600 std::string m_filename;
602 boost::shared_ptr<Alembic::Ogawa::IArchive> m_archive;
604 boost::shared_ptr<OgIGroup> m_root;
605
607 boost::shared_ptr<Field3DInputFileHDF5> m_hdf5;
608
609};
610
611//----------------------------------------------------------------------------//
612// Utility functions
613//----------------------------------------------------------------------------//
614
620bool fileExists(const std::string &filename);
621
622//----------------------------------------------------------------------------//
623// Field3DOutputFile
624//----------------------------------------------------------------------------//
625
634//----------------------------------------------------------------------------//
635
637{
638public:
639
640 // Enums ---------------------------------------------------------------------
641
644 FailOnExisting
645 };
646
647 // Ctors, dtor ---------------------------------------------------------------
648
651
653 virtual ~Field3DOutputFile();
654
656
657 // Main interface ------------------------------------------------------------
658
660 bool create(const std::string &filename, CreateMode cm = OverwriteMode);
661
663 static void useOgawa(const bool enabled)
664 {
665 // simple temporary endian check
666 union {
667 uint32_t l;
668 char c[4];
669 } u;
670
671 u.l = 0x01234567;
672
673 if (u.c[0] == 0x67) {
674 ms_doOgawa = enabled;
675 } else {
676 std::cerr << "WARNING: Field3D only supports Ogawa-backed files "
677 << "on little-endian systems." << std::endl;
678 ms_doOgawa = false;
679 }
680 }
681
684
686 template <class Data_T>
687 bool writeLayer(const std::string &layerName,
688 typename Field<Data_T>::Ptr layer)
689 {
690 return writeLayer<Data_T>(std::string("default"), layerName, layer);
691 }
692
695 template <class Data_T>
696 bool writeLayer(const std::string &partitionName,
697 const std::string &layerName,
698 typename Field<Data_T>::Ptr layer);
699
702 template <class Data_T>
703 bool writeLayer(typename Field<Data_T>::Ptr layer)
704 {
705 return writeLayer<Data_T>(layer->name, layer->attribute, layer);
706 }
707
709
712
714 template <class Data_T>
715 bool writeScalarLayer(const std::string &layerName,
716 typename Field<Data_T>::Ptr layer)
717 {
718 if (m_hdf5) {
719 return m_hdf5->writeScalarLayer<Data_T>(layerName, layer);
720 }
721 return writeScalarLayer<Data_T>(std::string("default"), layerName, layer);
722 }
723
726 template <class Data_T>
727 bool writeScalarLayer(const std::string &partitionName,
728 const std::string &layerName,
729 typename Field<Data_T>::Ptr layer)
730 {
731 if (m_hdf5) {
732 return m_hdf5->writeScalarLayer<Data_T>(partitionName, layerName, layer);
733 }
734 return writeLayer<Data_T>(partitionName, layerName, layer);
735 }
736
739 template <class Data_T>
741 {
742 if (m_hdf5) {
743 return m_hdf5->writeScalarLayer<Data_T>(layer);
744 }
745 return writeLayer<Data_T>(layer);
746 }
747
749 template <class Data_T>
750 bool writeVectorLayer(const std::string &layerName,
751 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
752 {
753 if (m_hdf5) {
754 return m_hdf5->writeVectorLayer<Data_T>(layerName, layer);
755 }
756 return writeVectorLayer<Data_T>(std::string("default"), layerName, layer);
757 }
758
761 template <class Data_T>
762 bool writeVectorLayer(const std::string &partitionName,
763 const std::string &layerName,
764 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
765 {
766 if (m_hdf5) {
767 return m_hdf5->writeVectorLayer<Data_T>(partitionName, layerName, layer);
768 }
769 return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName, layer);
770 }
771
774 template <class Data_T>
776 {
777 if (m_hdf5) {
778 return m_hdf5->writeVectorLayer<Data_T>(layer);
779 }
780 return writeLayer<FIELD3D_VEC3_T<Data_T> >(layer);
781 }
782
784 bool writeGlobalMetadata();
785
788 bool writeGroupMembership();
789
791
792private:
793
794 // From Field3DFileBase ------------------------------------------------------
795
796 virtual void closeInternal()
797 {
798 if (m_hdf5) {
799 m_hdf5->closeInternal();
800 return;
801 }
802 cleanup();
803 }
804
805 void cleanup()
806 {
807 // The destruction of the various Ogawa components must happen in the
808 // right order
809
810 // First, the partition groups
811 m_partitions.clear();
812 // Then the root group
813 m_root.reset();
814 // Finally, the archive
815 m_archive.reset();
816 }
817
818 // Convenience methods -------------------------------------------------------
819
821 std::string incrementPartitionName(std::string &pname);
822
825 createNewPartition(const std::string &partitionName,
826 const std::string &layerName, FieldRes::Ptr field);
830 bool writeMapping(OgOGroup &partitionGroup, FieldMapping::Ptr mapping);
831
833 bool writeMetadata(OgOGroup &metadataGroup, FieldBase::Ptr layer);
834
836 bool writeMetadata(OgOGroup &metadataGroup);
837
838 // Data members --------------------------------------------------------------
839
841 static bool ms_doOgawa;
842
844 boost::shared_ptr<Alembic::Ogawa::OArchive> m_archive;
846 boost::shared_ptr<OgOGroup> m_root;
847
849 boost::shared_ptr<Field3DOutputFileHDF5> m_hdf5;
850
851};
852
853//----------------------------------------------------------------------------//
854
856
857//----------------------------------------------------------------------------//
858
859#endif
Contains the ClassFactory class for registering Field3D classes.
Contains the EmptyField class.
Contains the Field3DFileHDF5 classes.
bool fileExists(const std::string &filename)
checks to see if a file/directory exists or not
Basic container for metedata.
Contains Field, WritableField and ResizableField classes.
Contains forward declarations for Ogawa classes.
#define FIELD3D_VEC3_T
Definition SpiMathLib.h:88
boost::intrusive_ptr< EmptyField > Ptr
Definition EmptyField.h:93
std::vector< Ptr > Vec
Definition EmptyField.h:94
boost::shared_ptr< Field3DFileHDF5Base > m_hdf5Base
HDF5 fallback.
GroupMembershipMap m_groupMembership
Keeps track of group membership for each layer of partition name. The key is the "group" and the valu...
virtual void metadataHasChanged(const std::string &)
This function should implemented by concrete classes to get the callback when metadata changes.
File::Partition::Ptr partition(const std::string &partitionName) const
Returns a pointer to the given partition.
Field3DFileBase(const Field3DFileBase &)
FieldMetadata & metadata()
accessor to the m_metadata class
virtual void closeInternal()=0
Closes the file if open.
File::Partition::Ptr getPartition(const std::string &partitionName) const
Returns a pointer to the given partition.
FieldMetadata m_metadata
metadata
std::vector< File::Partition::Ptr > PartitionList
PartitionList m_partitions
Vector of partitions.
File::Partition::Ptr partition(const std::string &partitionName)
Returns a pointer to the given partition.
std::map< std::string, std::string > GroupMembershipMap
std::vector< std::string > m_partitionNames
This stores partition names.
std::map< std::string, int > PartitionCountMap
PartitionCountMap m_partitionCount
Contains a counter for each partition name. This is used to keep multiple fields with the same name u...
const FieldMetadata & metadata() const
Read only access to the m_metadata class.
std::vector< LayerInfo > m_layerInfo
This stores layer info.
Provides reading of .f3d (internally, hdf5) files.
Provides reading of .f3d (internally, hdf5 or Ogawa) files.
std::string m_filename
Filename, only to be set by open().
Field< Data_T >::Vec readScalarLayers(const std::string &layerName=std::string("")) const
Retrieves all the layers of scalar type and maintains their on-disk data types.
Field< FIELD3D_VEC3_T< Data_T > >::Vec readVectorLayers(const std::string &layerName=std::string("")) const
Retrieves all the layers of vector type and maintains their on-disk data types.
const std::string & encoding() const
Returns an encoding descriptor of the given file.
boost::shared_ptr< OgIGroup > m_root
Pointer to root group.
virtual void closeInternal()
Closes the file if open.
boost::shared_ptr< Field3DInputFileHDF5 > m_hdf5
HDF5 fallback.
Field< Data_T >::Vec readScalarLayers(const std::string &partitionName, const std::string &layerName) const
This one allows the allows the partitionName to be passed in.
boost::shared_ptr< Alembic::Ogawa::IArchive > m_archive
Pointer to the Ogawa archive.
Field< FIELD3D_VEC3_T< Data_T > >::Vec readVectorLayers(const std::string &partitionName, const std::string &layerName) const
This version allows you to pass in the partition name.
Provides writing of .f3d (internally, hdf5) files.
Provides writing of .f3d (internally, hdf5 or Ogawa) files.
static bool ms_doOgawa
Whether to output ogawa files.
virtual void closeInternal()
Closes the file if open.
boost::shared_ptr< Field3DOutputFileHDF5 > m_hdf5
HDF5 fallback.
bool writeLayer(const std::string &layerName, typename Field< Data_T >::Ptr layer)
Writes a scalar layer to the "Default" partition.
static void useOgawa(const bool enabled)
Whether to output ogawa files.
boost::shared_ptr< OgOGroup > m_root
Pointer to root group.
bool writeScalarLayer(const std::string &partitionName, const std::string &layerName, typename Field< Data_T >::Ptr layer)
Writes a layer to a specific partition. The partition will be created if not specified.
bool writeVectorLayer(typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
Writes a layer to a specific partition. The field name and attribute name are used for partition and ...
bool writeVectorLayer(const std::string &partitionName, const std::string &layerName, typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
Writes a layer to a specific partition. The partition will be created if not specified.
bool writeScalarLayer(typename Field< Data_T >::Ptr layer)
Writes a layer to a specific partition. The field name and attribute name are used for partition and ...
boost::shared_ptr< Alembic::Ogawa::OArchive > m_archive
Pointer to the Ogawa archive.
bool writeScalarLayer(const std::string &layerName, typename Field< Data_T >::Ptr layer)
Writes a scalar layer to the "Default" partition.
bool writeLayer(typename Field< Data_T >::Ptr layer)
Writes a layer to a specific partition. The field name and attribute name are used for partition and ...
bool writeVectorLayer(const std::string &layerName, typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
Writes a scalar layer to the "Default" partition.
boost::intrusive_ptr< FieldBase > Ptr
Definition Field.h:97
std::string attribute
Optional name of the attribute the field represents.
Definition Field.h:173
std::string name
Optional name of the field.
Definition Field.h:171
boost::intrusive_ptr< FieldMapping > Ptr
boost::intrusive_ptr< FieldRes > Ptr
Definition Field.h:213
Definition Field.h:390
boost::intrusive_ptr< Field > Ptr
Definition Field.h:395
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
Definition Field.h:403
std::string name
The name of the layer (always available)
std::string parent
The name of the parent partition. We need this in order to open its group.
boost::shared_ptr< OgOGroup > m_group
Group representing the partition.
LayerList m_layers
The layers belonging to this partition.
std::string name
Name of the partition.
boost::intrusive_ptr< const Partition > CPtr
FieldMapping::Ptr mapping
Pointer to the mapping object.
static const char * staticClassType()
RefBase base
Convenience typedef for referring to base class.
std::vector< Layer > LayerList
boost::intrusive_ptr< Partition > Ptr
Partition class_type
RefBase & operator=(const RefBase &)
Assignment operator.
Definition RefCount.h:134
Namespace for file I/O specifics.
Definition Field3DFile.h:87
#define FIELD3D_API
Definition ns.h:77
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition ns.h:58
LayerInfo(std::string par, std::string nm, int cpt)