53 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height) *
static_cast<size_t>(_depth));
57 for (
const auto& v : rhs) *(dest_v++) = v;
62 Array3D(uint32_t width, uint32_t height, uint32_t depth,
Properties in_properties = {}) :
63 Data(in_properties,
sizeof(value_type)),
69 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height) *
static_cast<size_t>(_depth));
73 Array3D(uint32_t width, uint32_t height, uint32_t depth, value_type* data,
Properties in_properties = {}) :
74 Data(in_properties,
sizeof(value_type)),
78 _depth(depth) {
dirty(); }
80 Array3D(uint32_t width, uint32_t height, uint32_t depth,
const value_type& value,
Properties in_properties = {}) :
81 Data(in_properties,
sizeof(value_type)),
87 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height) *
static_cast<size_t>(_depth));
90 for (
auto& v : *
this) v = value;
102 assign(data, offset, stride, width, height, depth, in_properties);
105 template<
typename... Args>
116 size_t sizeofObject() const noexcept
override {
return sizeof(Array3D); }
117 const char* className() const noexcept
override {
return type_name<Array3D>(); }
118 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
119 bool is_compatible(
const std::type_info& type)
const noexcept override {
return typeid(
Array3D) == type || Data::is_compatible(type); }
122 void accept(Visitor& visitor)
override;
123 void accept(ConstVisitor& visitor)
const override;
125 void read(Input& input)
override
127 size_t original_size = size();
131 uint32_t w = input.readValue<uint32_t>(
"width");
132 uint32_t h = input.readValue<uint32_t>(
"height");
133 uint32_t d = input.readValue<uint32_t>(
"depth");
135 if (
auto data_storage = input.readObject<Data>(
"storage"))
137 uint32_t offset = input.readValue<uint32_t>(
"offset");
142 if (input.matchPropertyName(
"data"))
144 size_t new_size = computeValueCountIncludingMipmaps(w, h, d,
properties.maxNumMipmaps);
148 if (original_size != new_size)
151 _data = _allocate(new_size);
156 _data = _allocate(new_size);
165 if (_data) input.read(new_size, _data);
171 void write(Output& output)
const override
175 output.writeValue<uint32_t>(
"width", _width);
176 output.writeValue<uint32_t>(
"height", _height);
177 output.writeValue<uint32_t>(
"depth", _depth);
179 output.writeObject(
"storage", _storage);
182 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
183 output.writeValue<uint32_t>(
"offset", offset);
187 output.writePropertyName(
"data");
188 output.write(valueCount(), _data);
189 output.writeEndOfLine();
192 size_t size()
const {
return (
properties.maxNumMipmaps <= 1) ? (
static_cast<size_t>(_width) * _height * _depth) : computeValueCountIncludingMipmaps(_width, _height, _depth,
properties.maxNumMipmaps); }
194 bool available()
const {
return _data !=
nullptr; }
195 bool empty()
const {
return _data ==
nullptr; }
208 Array3D& operator=(
const Array3D& rhs)
210 if (&rhs ==
this)
return *
this;
216 _height = rhs._height;
219 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height) *
static_cast<size_t>(_depth));
223 for (
const auto& v : rhs) *(dest_v++) = v;
231 void assign(uint32_t width, uint32_t height, uint32_t depth, value_type* data, Properties in_properties = {})
246 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t width, uint32_t height, uint32_t depth, Properties in_properties = {})
253 if (_storage && _storage->dataPointer())
255 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
273 void* dataRelease()
override
290 size_t valueSize()
const override {
return sizeof(value_type); }
291 size_t valueCount()
const override {
return size(); }
293 bool dataAvailable()
const override {
return available(); }
294 size_t dataSize()
const override {
return size() *
properties.stride; }
296 void* dataPointer()
override {
return _data; }
297 const void* dataPointer()
const override {
return _data; }
299 void* dataPointer(
size_t i)
override {
return data(i); }
300 const void* dataPointer(
size_t i)
const override {
return data(i); }
302 uint32_t dimensions()
const override {
return 3; }
304 uint32_t width()
const override {
return _width; }
305 uint32_t height()
const override {
return _height; }
306 uint32_t depth()
const override {
return _depth; }
308 value_type* data() {
return _data; }
309 const value_type* data()
const {
return _data; }
311 inline value_type* data(
size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
static_cast<size_t>(
properties.stride)); }
312 inline const value_type* data(
size_t i)
const {
return reinterpret_cast<const value_type*
>(
reinterpret_cast<const uint8_t*
>(_data) + i *
static_cast<size_t>(
properties.stride)); }
314 size_t index(uint32_t i, uint32_t j, uint32_t k)
const noexcept {
return static_cast<size_t>(
static_cast<size_t>(k) *
static_cast<size_t>(_width) *
static_cast<size_t>(_height) +
static_cast<size_t>(j) *
static_cast<size_t>(_width) +
static_cast<size_t>(i)); }
316 value_type& operator[](
size_t i) {
return *data(i); }
317 const value_type& operator[](
size_t i)
const {
return *data(i); }
319 value_type& at(
size_t i) {
return *data(i); }
320 const value_type& at(
size_t i)
const {
return *data(i); }
322 value_type& operator()(uint32_t i, uint32_t j, uint32_t k) {
return *data(index(i, j, k)); }
323 const value_type& operator()(uint32_t i, uint32_t j, uint32_t k)
const {
return *data(index(i, j, k)); }
325 value_type& at(uint32_t i, uint32_t j, uint32_t k) {
return *data(index(i, j, k)); }
326 const value_type& at(uint32_t i, uint32_t j, uint32_t k)
const {
return *data(index(i, j, k)); }
328 void set(
size_t i,
const value_type& v) { *data(i) = v; }
329 void set(uint32_t i, uint32_t j, uint32_t k,
const value_type& v) { *data(index(i, j, k)) = v; }
331 Data* storage() {
return _storage; }
332 const Data* storage()
const {
return _storage; }
334 iterator begin() {
return iterator{_data,
properties.stride}; }
335 const_iterator begin()
const {
return const_iterator{_data,
properties.stride}; }
337 iterator end() {
return iterator{data(_width * _height * _depth),
properties.stride}; }
338 const_iterator end()
const {
return const_iterator{data(_width * _height * _depth),
properties.stride}; }
346 value_type* _allocate(
size_t size)
const
351 return new value_type[size];
353 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
355 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
360 if (!_storage && _data)
367 vsg::deallocate(_data);
376 ref_ptr<Data> _storage;