1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
//----------------------------------------------------------------------------//
#ifndef _INCLUDED_Field3D_OgUtil_H_
#define _INCLUDED_Field3D_OgUtil_H_
//----------------------------------------------------------------------------//
// Includes
//----------------------------------------------------------------------------//
#include <iostream>
#include <string>
#include <OpenEXR/ImathVec.h>
#include "All.h"
#include "UtilFoundation.h"
#include "Traits.h"
//----------------------------------------------------------------------------//
// Defines
//----------------------------------------------------------------------------//
#define OGAWA_THREAD 0
#define OGAWA_START_ID 2
#define OGAWA_DATASET_BASEOFFSET 3
#define OGAWA_INVALID_DATASET_INDEX -1
//----------------------------------------------------------------------------//
#include "ns.h"
FIELD3D_NAMESPACE_OPEN
//----------------------------------------------------------------------------//
// Enums
//----------------------------------------------------------------------------//
//! Enumerates the various uses for Ogawa-level groups.
//! \warning Do not under any circumstances alter the order of these!
enum OgGroupType {
F3DGroupType = 0,
F3DAttributeType,
F3DDatasetType,
F3DCompressedDatasetType
};
//----------------------------------------------------------------------------//
// OgawaTypeTraits
//----------------------------------------------------------------------------//
//! Declares the OgawaTypeTraits struct, but does not implement it.
template <typename T>
struct OgawaTypeTraits;
//----------------------------------------------------------------------------//
#define F3D_DECLARE_OG_TRAITS(type, enum, name, def) \
template <> \
struct OgawaTypeTraits<type> \
{ \
typedef type value_type; \
static const char* typeName() { return name; } \
static OgDataType typeEnum() { return enum; } \
static value_type defaultValue() { return def; } \
};
//----------------------------------------------------------------------------//
F3D_DECLARE_OG_TRAITS(int8_t, F3DInt8, "int8_t", 0);
F3D_DECLARE_OG_TRAITS(uint8_t, F3DUint8, "uint8_t", 0);
F3D_DECLARE_OG_TRAITS(int16_t, F3DInt16, "int16_t", 0);
F3D_DECLARE_OG_TRAITS(uint16_t, F3DUint16, "uint16_t", 0);
F3D_DECLARE_OG_TRAITS(int32_t, F3DInt32, "int32_t", 0);
F3D_DECLARE_OG_TRAITS(uint32_t, F3DUint32, "uint32_t", 0);
F3D_DECLARE_OG_TRAITS(int64_t, F3DInt64, "int64_t", 0);
F3D_DECLARE_OG_TRAITS(uint64_t, F3DUint64, "uint64_t", 0);
F3D_DECLARE_OG_TRAITS(float16_t, F3DFloat16, "float16_t", 0);
F3D_DECLARE_OG_TRAITS(float32_t, F3DFloat32, "float32_t", 0);
F3D_DECLARE_OG_TRAITS(float64_t, F3DFloat64, "float64_t", 0);
F3D_DECLARE_OG_TRAITS(vec16_t, F3DVec16, "vec16_t", vec16_t(0));
F3D_DECLARE_OG_TRAITS(vec32_t, F3DVec32, "vec32_t", vec32_t(0));
F3D_DECLARE_OG_TRAITS(vec64_t, F3DVec64, "vec64_t", vec64_t(0));
F3D_DECLARE_OG_TRAITS(veci32_t, F3DVecI32, "veci32_t", veci32_t(0));
F3D_DECLARE_OG_TRAITS(mtx64_t, F3DMtx64, "mtx64_t", mtx64_t());
F3D_DECLARE_OG_TRAITS(std::string, F3DString, "string", "");
//----------------------------------------------------------------------------//
// Helper functions
//----------------------------------------------------------------------------//
//! Gets a string representation of the OgGroupType enum
const char* ogGroupTypeToString(OgGroupType type);
//----------------------------------------------------------------------------//
//! Reads a string
bool readString(Alembic::Ogawa::IGroupPtr group, const size_t idx,
std::string &s);
//----------------------------------------------------------------------------//
//! Reads a single value
template <typename T>
bool readData(Alembic::Ogawa::IGroupPtr group, const size_t idx,
T &value)
{
// Grab data
Alembic::Ogawa::IDataPtr data = group->getData(idx, OGAWA_THREAD);
// Check data length
const size_t sizeLength = sizeof(T);
const size_t length = data->getSize();
if (length != sizeLength) {
return false;
}
// Read the data directly to the input param
data->read(length, &value, 0, OGAWA_THREAD);
// Done
return true;
}
//----------------------------------------------------------------------------//
//! Specialization of readData for strings
template <>
inline bool readData(Alembic::Ogawa::IGroupPtr group, const size_t idx,
std::string &value)
{
return readString(group, idx, value);
}
//----------------------------------------------------------------------------//
//! Reads a single data type value
//! \note Not returning a bool here, since OgDataType has an 'invalid' enum.
OgDataType readDataType(Alembic::Ogawa::IGroupPtr group, const size_t idx);
//----------------------------------------------------------------------------//
//! Lowest-level, writes directly to Ogawa Data.
bool writeString(Alembic::Ogawa::OGroupPtr group, const std::string &s);
//----------------------------------------------------------------------------//
//! Lowest-level, writes directly to Ogawa Data.
template <typename T>
bool writeData(Alembic::Ogawa::OGroupPtr group, const T &value)
{
return group->addData(sizeof(T), &value) != NULL;
}
//----------------------------------------------------------------------------//
//! Specialization of writeData for strings
template <>
inline bool writeData(Alembic::Ogawa::OGroupPtr group, const std::string &value)
{
return writeString(group, value);
}
//----------------------------------------------------------------------------//
//! Lowest-level, writes directly to Ogawa Data.
template <typename T>
bool writeDataType(Alembic::Ogawa::OGroupPtr group)
{
OgDataType dataType = OgawaTypeTraits<T>::typeEnum();
return group->addData(sizeof(OgDataType), &dataType) != NULL;
}
//----------------------------------------------------------------------------//
//! Finds the group name. Really just a wrapper around readString() for the
//! first data set in the group.
bool getGroupName(Alembic::Ogawa::IGroupPtr group, std::string &name);
//----------------------------------------------------------------------------//
// OgIBase
//----------------------------------------------------------------------------//
//! Provides a few standard member functions and data members
class OgIBase
{
public:
// Ctors, dtor ---------------------------------------------------------------
//! No initialization - leaves the group pointer null
OgIBase()
{ /* Empty */ }
//! Initializes the group pointer
OgIBase(Alembic::Ogawa::IGroupPtr group)
: m_group(group)
{ /* Empty */ }
// Main methods --------------------------------------------------------------
//! Whether the group is valid
bool isValid() const
{ return m_group != NULL; }
//! Returns the name
const std::string& name() const
{ return m_name; }
protected:
// Data members --------------------------------------------------------------
Alembic::Ogawa::IGroupPtr m_group;
std::string m_name;
};
//----------------------------------------------------------------------------//
FIELD3D_NAMESPACE_HEADER_CLOSE
//----------------------------------------------------------------------------//
#endif // include guard
//----------------------------------------------------------------------------//
|