|
| 1 | +// Copyright Contributors to the OpenVDB Project |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#ifndef OPENVDB_IO_CODECS_SCALARCODEC_HAS_BEEN_INCLUDED |
| 5 | +#define OPENVDB_IO_CODECS_SCALARCODEC_HAS_BEEN_INCLUDED |
| 6 | + |
| 7 | +#include <openvdb/openvdb.h> |
| 8 | +#include <openvdb/tree/Tree.h> |
| 9 | +#include <openvdb/tools/Clip.h> |
| 10 | +#include <openvdb/tools/NodeVisitor.h> |
| 11 | +#include <openvdb/io/Codec.h> |
| 12 | + |
| 13 | +#include "ScalarLeafCodec.h" |
| 14 | +#include "TopologyCodec.h" |
| 15 | + |
| 16 | +namespace openvdb { |
| 17 | +OPENVDB_USE_VERSION_NAMESPACE |
| 18 | +namespace OPENVDB_VERSION_NAME { |
| 19 | +namespace codecs { |
| 20 | +namespace internal { |
| 21 | + |
| 22 | +template <typename TreeT> |
| 23 | +struct WriteBuffersOp |
| 24 | +{ |
| 25 | + using LeafT = typename TreeT::LeafNodeType; |
| 26 | + |
| 27 | + WriteBuffersOp(std::ostream& _os, bool _saveFloatAsHalf) |
| 28 | + : os(_os) |
| 29 | + , saveFloatAsHalf(_saveFloatAsHalf) { } |
| 30 | + |
| 31 | + template <typename NodeT> |
| 32 | + void operator()(const NodeT&, size_t) { } |
| 33 | + |
| 34 | + void operator()(const LeafT& leaf, size_t) |
| 35 | + { |
| 36 | + writeScalarLeafBuffers(leaf, os, saveFloatAsHalf); |
| 37 | + } |
| 38 | + |
| 39 | + std::ostream& os; |
| 40 | + const bool saveFloatAsHalf; |
| 41 | +}; // struct WriteBuffersOp |
| 42 | + |
| 43 | + |
| 44 | +template <typename TreeT, typename StorageTreeT> |
| 45 | +struct ReadBuffersOp |
| 46 | +{ |
| 47 | + using RootT = typename TreeT::RootNodeType; |
| 48 | + using LeafT = typename TreeT::LeafNodeType; |
| 49 | + using ValueT = typename TreeT::ValueType; |
| 50 | + using StorageLeafT = typename StorageTreeT::LeafNodeType; |
| 51 | + |
| 52 | + ReadBuffersOp(std::istream& _is, bool _saveFloatAsHalf, const ValueT& _background, |
| 53 | + const CoordBBox* _clipBBox) |
| 54 | + : is(_is) |
| 55 | + , saveFloatAsHalf(_saveFloatAsHalf) |
| 56 | + , background(_background) |
| 57 | + , clipBBox(_clipBBox) { } |
| 58 | + |
| 59 | + void operator()(RootT& root, size_t) |
| 60 | + { |
| 61 | + if (clipBBox) { |
| 62 | + root.clip(*clipBBox); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + template <typename NodeT> |
| 67 | + void operator()(NodeT& node, size_t) |
| 68 | + { |
| 69 | + if (clipBBox) { |
| 70 | + node.clip(*clipBBox, background); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + void operator()(LeafT& leaf, size_t) |
| 75 | + { |
| 76 | + readScalarLeafBuffers<LeafT, StorageLeafT>(leaf, is, saveFloatAsHalf, background, /*skip=*/false, clipBBox); |
| 77 | + } |
| 78 | + |
| 79 | + std::istream& is; |
| 80 | + const bool saveFloatAsHalf; |
| 81 | + const ValueT& background; |
| 82 | + const CoordBBox* clipBBox = nullptr; |
| 83 | +}; // struct ReadBuffersOp |
| 84 | + |
| 85 | + |
| 86 | +// Free-standing function for both standard and conversion codec cases |
| 87 | +// Uses StorageGridT = GridT by default, but allows different storage type for conversions |
| 88 | +template<typename GridT, typename StorageGridT = GridT> |
| 89 | +void scalarCodecReadBuffers(GridT& grid, std::istream& is, const io::ReadOptions& options) |
| 90 | +{ |
| 91 | + if (grid.hasMultiPassIO()) { |
| 92 | + OPENVDB_THROW(IoError, "Multi-pass IO is not supported in ScalarCodec"); |
| 93 | + } |
| 94 | + |
| 95 | + using TreeT = typename GridT::TreeType; |
| 96 | + using StorageTreeT = typename StorageGridT::TreeType; |
| 97 | + |
| 98 | + io::checkFormatVersion(is); |
| 99 | + |
| 100 | + bool saveFloatAsHalf = grid.saveFloatAsHalf(); |
| 101 | + |
| 102 | + auto& tree = grid.tree(); |
| 103 | + tree.clearAllAccessors(); |
| 104 | + |
| 105 | + std::unique_ptr<CoordBBox> clipIndexBBox; |
| 106 | + if (options.clipBBox.isSorted()) { |
| 107 | + clipIndexBBox = std::make_unique<CoordBBox>(grid.constTransform().worldToIndexNodeCentered(options.clipBBox)); |
| 108 | + } |
| 109 | + |
| 110 | + // Works for both standard (TreeT == StorageTreeT) and conversion cases |
| 111 | + ReadBuffersOp<TreeT, StorageTreeT> readBuffersOp(is, saveFloatAsHalf, tree.background(), |
| 112 | + clipIndexBBox.get()); |
| 113 | + tools::visitNodesDepthFirst(grid.tree(), readBuffersOp, /*idx=*/0, /*topDown=*/false); |
| 114 | +} |
| 115 | + |
| 116 | +// Free-standing function for write case (no StorageGridT needed) |
| 117 | +template<typename GridT> |
| 118 | +void scalarCodecWriteBuffers(const GridT& grid, std::ostream& os) |
| 119 | +{ |
| 120 | + using TreeType = typename GridT::TreeType; |
| 121 | + |
| 122 | + if (grid.hasMultiPassIO()) { |
| 123 | + OPENVDB_THROW(IoError, "Multi-pass IO is not supported in ScalarCodec"); |
| 124 | + } |
| 125 | + |
| 126 | + WriteBuffersOp<TreeType> writeBuffersOp(os, grid.saveFloatAsHalf()); |
| 127 | + tools::visitNodesDepthFirst(grid.tree(), writeBuffersOp); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace internal |
| 131 | + |
| 132 | +template <typename GridT, typename StorageGridT = GridT, io::CodecMode Mode = io::CodecMode::ReadWrite> |
| 133 | +struct ScalarCodec : public TopologyCodec<GridT, StorageGridT, Mode> |
| 134 | +{ |
| 135 | + static_assert(GridT::TreeType::RootNodeType::template SameConfiguration< |
| 136 | + typename StorageGridT::TreeType::RootNodeType>::value, |
| 137 | + "GridT and StorageGridT must have the same configuration"); |
| 138 | + |
| 139 | + using Ptr = std::unique_ptr<ScalarCodec<GridT, StorageGridT, Mode>>; |
| 140 | + |
| 141 | + struct ScalarCodecData : public io::CodecData |
| 142 | + { |
| 143 | + std::vector<int> leafCoords; |
| 144 | + }; |
| 145 | + |
| 146 | + ~ScalarCodec() noexcept = default; |
| 147 | + |
| 148 | + io::CodecData::Ptr createData() final |
| 149 | + { |
| 150 | + auto data = std::make_unique<ScalarCodecData>(); |
| 151 | + data->grid = GridT::create(); |
| 152 | + return data; |
| 153 | + } |
| 154 | + |
| 155 | + static inline std::string name() |
| 156 | + { |
| 157 | + if constexpr (std::is_same_v<GridT, StorageGridT>) { |
| 158 | + return GridT::gridType(); |
| 159 | + } else { |
| 160 | + std::string buildType = typeNameAsString<typename GridT::BuildType>(); |
| 161 | + return StorageGridT::gridType() + "_to_" + buildType; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + void readBuffers(std::istream& is, io::CodecData& data, const io::ReadOptions& options, io::ReadDiagnostics&) final |
| 166 | + { |
| 167 | + GridT& grid = static_cast<GridT&>(*data.grid); |
| 168 | + internal::scalarCodecReadBuffers<GridT, StorageGridT>(grid, is, options); |
| 169 | + } |
| 170 | + |
| 171 | + void writeBuffers(std::ostream& os, const GridBase& gridBase, const io::WriteOptions&) final |
| 172 | + { |
| 173 | + if constexpr (Mode == io::CodecMode::ReadOnly) return; |
| 174 | + |
| 175 | + const GridT& grid = static_cast<const GridT&>(gridBase); |
| 176 | + internal::scalarCodecWriteBuffers(grid, os); |
| 177 | + } |
| 178 | +}; // struct ScalarCodec |
| 179 | + |
| 180 | + |
| 181 | +} // namespace codecs |
| 182 | +} // namespace OPENVDB_VERSION_NAME |
| 183 | +} // namespace openvdb |
| 184 | + |
| 185 | +#endif // OPENVDB_IO_CODECS_SCALARCODEC_HAS_BEEN_INCLUDED |
0 commit comments