|
| 1 | +// Copyright Contributors to the OpenVDB Project |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#ifndef OPENVDB_IO_CODECS_POINTINDEXCODEC_HAS_BEEN_INCLUDED |
| 5 | +#define OPENVDB_IO_CODECS_POINTINDEXCODEC_HAS_BEEN_INCLUDED |
| 6 | + |
| 7 | +#include <openvdb/io/Codec.h> |
| 8 | + |
| 9 | +#include <openvdb/tools/PointIndexGrid.h> |
| 10 | + |
| 11 | +#include "ScalarLeafCodec.h" |
| 12 | +#include "TopologyCodec.h" |
| 13 | + |
| 14 | +namespace openvdb { |
| 15 | +OPENVDB_USE_VERSION_NAMESPACE |
| 16 | +namespace OPENVDB_VERSION_NAME { |
| 17 | +namespace codecs { |
| 18 | +namespace internal { |
| 19 | + |
| 20 | +template <typename GridT> |
| 21 | +struct ReadPointIndexBuffersOp |
| 22 | +{ |
| 23 | + using TreeT = typename GridT::TreeType; |
| 24 | + using RootT = typename TreeT::RootNodeType; |
| 25 | + using LeafT = typename TreeT::LeafNodeType; |
| 26 | + using ValueT = typename TreeT::ValueType; |
| 27 | + |
| 28 | + ReadPointIndexBuffersOp(std::istream& _is, bool _saveFloatAsHalf, |
| 29 | + const ValueT& _background) |
| 30 | + : is(_is) |
| 31 | + , saveFloatAsHalf(_saveFloatAsHalf) |
| 32 | + , background(_background) { } |
| 33 | + |
| 34 | + template <typename NodeT> |
| 35 | + void operator()(NodeT&, size_t) { } |
| 36 | + |
| 37 | + void operator()(LeafT& leaf, size_t) |
| 38 | + { |
| 39 | + using BaseLeaf = typename LeafT::BaseLeaf; |
| 40 | + |
| 41 | + // Read the value mask and voxel data via base class |
| 42 | + BaseLeaf& baseLeaf = static_cast<BaseLeaf&>(leaf); |
| 43 | + readScalarLeafBuffers(baseLeaf, is, saveFloatAsHalf, background, /*skip=*/false, /*clipBBox=*/nullptr); |
| 44 | + |
| 45 | + // Read the number of indices. |
| 46 | + Index64 numIndices = Index64(0); |
| 47 | + is.read(reinterpret_cast<char*>(&numIndices), sizeof(Index64)); |
| 48 | + |
| 49 | + // Read the indices data. |
| 50 | + leaf.indices().resize(size_t(numIndices)); |
| 51 | + is.read(reinterpret_cast<char*>(leaf.indices().data()), numIndices * sizeof(ValueT)); |
| 52 | + |
| 53 | + // Reserved for future use. |
| 54 | + Index64 auxDataBytes = Index64(0); |
| 55 | + is.read(reinterpret_cast<char*>(&auxDataBytes), sizeof(Index64)); |
| 56 | + if (auxDataBytes > 0) { |
| 57 | + // For now, read and discard any auxiliary data. |
| 58 | + std::unique_ptr<char[]> auxData{new char[auxDataBytes]}; |
| 59 | + is.read(auxData.get(), auxDataBytes); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + std::istream& is; |
| 64 | + const bool saveFloatAsHalf; |
| 65 | + const ValueT& background; |
| 66 | +}; // struct ReadPointIndexBuffersOp |
| 67 | + |
| 68 | +template <typename GridT> |
| 69 | +struct WritePointIndexBuffersOp |
| 70 | +{ |
| 71 | + using TreeT = typename GridT::TreeType; |
| 72 | + using LeafT = typename TreeT::LeafNodeType; |
| 73 | + using ValueT = typename TreeT::ValueType; |
| 74 | + |
| 75 | + WritePointIndexBuffersOp(std::ostream& _os, bool _saveFloatAsHalf) |
| 76 | + : os(_os) |
| 77 | + , saveFloatAsHalf(_saveFloatAsHalf) { } |
| 78 | + |
| 79 | + template <typename NodeT> |
| 80 | + void operator()(const NodeT&, size_t) { } |
| 81 | + |
| 82 | + void operator()(const LeafT& leaf, size_t) |
| 83 | + { |
| 84 | + using BaseLeaf = typename LeafT::BaseLeaf; |
| 85 | + |
| 86 | + // Write out the value mask and voxel values via base class |
| 87 | + const BaseLeaf& baseLeaf = static_cast<const BaseLeaf&>(leaf); |
| 88 | + writeScalarLeafBuffers(baseLeaf, os, saveFloatAsHalf); |
| 89 | + |
| 90 | + // Write the number of indices. |
| 91 | + Index64 numIndices = Index64(leaf.indices().size()); |
| 92 | + os.write(reinterpret_cast<const char*>(&numIndices), sizeof(Index64)); |
| 93 | + |
| 94 | + // Write the indices data. |
| 95 | + os.write(reinterpret_cast<const char*>(leaf.indices().data()), numIndices * sizeof(ValueT)); |
| 96 | + |
| 97 | + // Reserved for future use. |
| 98 | + const Index64 auxDataBytes = Index64(0); |
| 99 | + os.write(reinterpret_cast<const char*>(&auxDataBytes), sizeof(Index64)); |
| 100 | + } |
| 101 | + |
| 102 | + std::ostream& os; |
| 103 | + const bool saveFloatAsHalf; |
| 104 | +}; // struct WritePointIndexBuffersOp |
| 105 | + |
| 106 | +} // namespace internal |
| 107 | + |
| 108 | +template <typename GridT> |
| 109 | +struct PointIndexCodec : public TopologyCodec<GridT> |
| 110 | +{ |
| 111 | + using Ptr = std::unique_ptr<PointIndexCodec<GridT>>; |
| 112 | + |
| 113 | + ~PointIndexCodec() noexcept = default; |
| 114 | + |
| 115 | + static inline std::string name() { return GridT::gridType(); } |
| 116 | + |
| 117 | + void readBuffers(std::istream& is, io::CodecData& data, const io::ReadOptions& options, io::ReadDiagnostics& diagnostics) final |
| 118 | + { |
| 119 | + GridT& grid = static_cast<GridT&>(*data.grid); |
| 120 | + |
| 121 | + if (grid.hasMultiPassIO()) { |
| 122 | + OPENVDB_THROW(IoError, "Multi-pass IO is not supported in PointIndexCodec"); |
| 123 | + } |
| 124 | + |
| 125 | + io::checkFormatVersion(is); |
| 126 | + |
| 127 | + bool saveFloatAsHalf = grid.saveFloatAsHalf(); |
| 128 | + |
| 129 | + auto& tree = grid.tree(); |
| 130 | + tree.clearAllAccessors(); |
| 131 | + |
| 132 | + if (options.clipBBox.isSorted()) { |
| 133 | + diagnostics.addWarning(grid.getName(), "bounding box clipping is not supported for PointIndexGrids"); |
| 134 | + } |
| 135 | + |
| 136 | + internal::ReadPointIndexBuffersOp<GridT> readBuffersOp(is, saveFloatAsHalf, tree.background()); |
| 137 | + tools::visitNodesDepthFirst(grid.tree(), readBuffersOp, /*idx=*/0, /*topDown=*/false); |
| 138 | + } |
| 139 | + |
| 140 | + void writeBuffers(std::ostream& os, const GridBase& gridBase, const io::WriteOptions&) final |
| 141 | + { |
| 142 | + const GridT& grid = static_cast<const GridT&>(gridBase); |
| 143 | + |
| 144 | + if (grid.hasMultiPassIO()) { |
| 145 | + OPENVDB_THROW(IoError, "Multi-pass IO is not supported in PointIndexCodec"); |
| 146 | + } |
| 147 | + |
| 148 | + internal::WritePointIndexBuffersOp<GridT> writeBuffersOp(os, grid.saveFloatAsHalf()); |
| 149 | + tools::visitNodesDepthFirst(grid.tree(), writeBuffersOp); |
| 150 | + } |
| 151 | +}; // struct PointIndexCodec |
| 152 | + |
| 153 | +} // namespace codecs |
| 154 | +} // namespace OPENVDB_VERSION_NAME |
| 155 | +} // namespace openvdb |
| 156 | + |
| 157 | +#endif // OPENVDB_IO_CODECS_POINTINDEXCODEC_HAS_BEEN_INCLUDED |
0 commit comments