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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
|
/************************************************************************
*
* Copyright (C) 2009-2024 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#pragma once
#include <sight/data/config.hpp>
#include "data/exception.hpp"
#include "data/factory/new.hpp"
#include "data/iterator.hpp"
#include "data/object.hpp"
#include <core/memory/buffer_object.hpp>
#include <core/memory/buffered.hpp>
#include <core/type.hpp>
#include <boost/range/iterator_range_core.hpp>
namespace sight::data
{
/**
* @brief Provides a way to manage a view on a multidimensional array.
*
* If the array owns its buffer, it will perform the allocation, reallocation,
* destruction of the buffer. Else, this class will provide an array "view" of the buffer.
*
* The array can be multi-dimensional, the number of dimensions is defined by the number of elements in the size vector
* (size()).
*
* @section Usage Usage
*
* @subsection Allocation Allocation
*
* The array buffer is allocated using the resize() method.
* You can get the allocated size using size_in_bytes().
*
* @warning The allocated size can be different from the array size: it can happen if you called resize(..., false).
* It may be useful when you don't want to reallocate the image too often, but you need to be sure to allocate enough
* memory.
*
* To resize the array, you must define the Type ([u]int[8|16|32|64], double, float) and the size of the buffer. You can
* use setType(const core::type& type) and resize(const size_t& size, bool reallocate) or directly call
* resize(const size_t& size, const core::type& type, bool reallocate).
*
* @section Access Buffer access
*
* You can access buffer values using at<type>(const std::size_t& offset) or at<type>({x, y, z}) methods. These methods
* are slow and should not be used to parse the entire buffer (see iterators).
*
* @warning The array must be locked for dump before accessing the buffer. It prevents the buffer to be dumped on the
* disk.
*
* \b Example:
*
* @code{.cpp}
// 2D array of std::int16_t
// prevent the buffer to be dumped on the disk
const auto dumpLock = array->dump_lock();
// retrieve the value at index (x, y)
value = array->at<std::int16_t>({x, y});
// or you can compute the index like
const auto size = array->size();
const std::size_t index = x + y*size[0];
value = array->at<std::int16_t>(index);
@endcode
*
* @subsection iterators iterators
*
* To parse the buffer from beginning to end, the iterator can be used.
*
* The iteration depends on the given format. The format can be the buffer type ([u]int[8|16|32|64], double, float), but
* can also be a simple struct like:
*
* @code{.cpp}
struct rgba {
std::uint8_t r;
std::uint8_t g;
std::uint8_t b;
std::uint8_t a;
};
@endcode
*
* This struct allows to parse the array as an rgba buffer (RGBARGBARGBA....).
*
* To get an iterator on the array, use begin<T>() and end<T>() methods.
*
* @warning The iterator does not assert that the array type is not the same as the given format. It only asserts (in
* debug)
* that the iterator does iterate outside of the buffer bounds).
*
* \b Example :
*/
/* *INDENT-OFF* */
/**
* @code{.cpp}
array::sptr array = array::New();
array->resize({1920, 1080}, core::type::INT16);
auto iter = array->begin<std::int16_t>();
const auto iterEnd = array->end<std::int16_t>();
for (; iter != iterEnd; ++iter)
{
(*iter) = value;
}
@endcode
*
* @note If you need to know (x, y) indices, you can parse the array looping from the last dimension to the first, like:
* @code{.cpp}
auto iter = array->begin<std::int16_t>();
const auto size = array->size();
for (std::size_t y=0 ; y<size[1] ; ++y)
{
for (std::size_t x=0 ; x<size[0] ; ++x)
{
// do something with x and y ....
// retrieve the value
*iter = value;
// increment iterator
++iter;
}
}
@endcode
*/
/* *INDENT-ON* */
class SIGHT_DATA_CLASS_API array final : public object,
public core::memory::buffered
{
public:
SIGHT_DECLARE_CLASS(array, object);
/**
* @brief array size type
*/
using size_t = std::vector<std::size_t>;
/**
* @brief Offset type
*/
using offset_t = std::vector<std::size_t>;
/**
* @brief Index type
*/
using index_t = offset_t;
/**
* @brief Constructor
*/
SIGHT_DATA_API array();
SIGHT_DATA_API ~array() override;
/**
* @brief Resizes and allocate (if needed) the array.
*
* If no buffer is allocated and reallocate is true, this method will allocate a buffer and take ownership of it.
* If the combination of type and size parameters does not match the size of the previously allocated buffer
* anymore, a reallocation is needed.
* In this case :
* - if reallocate is true and if the array does not own the buffer, an exception is thrown
* - else if reallocate is false, the array will update the view's information
* - else, the reallocation is performed.
*
* @param _size New size of the array or the view.
* @param _type New type of the array or the view
* @param _reallocate If true, allow buffer reallocation
*
* @return return the size of the array view
*
* @throw Exception
*/
SIGHT_DATA_API std::size_t resize(const size_t& _size, const core::type& _type, bool _reallocate = true);
/**
* @brief Resizes and allocate (if needed) the array.
*
* If no buffer is allocated and reallocate is true, this method will allocate a buffer and take it ownership,
* the type of the array is not changed.
*
* If the combination of type and size parameters do not match anymore the size of the previously allocated
* buffer, a reallocation is needed.
* In this case :
* - if reallocate is true and if the array do not own the buffer, an exception is thrown
* - else if reallocate is false, the array will update the view informations
* - else, the reallocation is performed.
*
* @param _size Size of the array view
* @param _reallocate If true, allow buffer reallocation
*
* @return return the size of the array view
*
* @throw Exception
*/
SIGHT_DATA_API std::size_t resize(const size_t& _size, bool _reallocate = true);
/**
* @brief Clear this array.
* Size and type are reset, buffer is released.
*/
SIGHT_DATA_API void clear();
/**
* @brief Test whether array is empty
*
* @return Returns whether array is empty, ie. this->size() is an empty
* vector.
*/
SIGHT_DATA_API bool empty() const;
/**
* @brief Get the size of one element of the array,
* ie. sizeof type * nbOfComponents
*
* @return One array element size in bytes.
*/
SIGHT_DATA_API std::size_t element_size_in_bytes() const;
/**
* @brief Get the number of elements of type <getType()> in the array
*
* @return number of array elements
*/
SIGHT_DATA_API std::size_t num_elements() const;
/**
* @brief Getter for the array view size
*
* @return array view size in bytes.
*/
SIGHT_DATA_API std::size_t size_in_bytes() const;
/**
* @brief Getter for the array size
*
* @return vector of size lengths in each dimension
*/
SIGHT_DATA_API const size_t& size() const;
/**
* @brief Getter for the array strides
*
* @return vector of steps in each dimension for array walking
*/
SIGHT_DATA_API const offset_t& get_strides() const;
/**
* @brief Getter for number of dimensions, ie. size().size()
*
* @return array's number of dimensions
*/
SIGHT_DATA_API std::size_t num_dimensions() const;
/**
* @brief Set array's buffer ownership
*
* @param _own New ownership value
*/
SIGHT_DATA_API void set_is_buffer_owner(bool _own);
/**
* @brief Getter for array's buffer ownership
*
* @return Current array buffer ownership
*/
SIGHT_DATA_API bool get_is_buffer_owner() const;
/**
* @brief Getter for array's type
*
* @return Type of array
*/
SIGHT_DATA_API core::type type() const;
/**
* @brief Compute strides for given parameters
*
* @param _size array size
* @param _size_of_type size of a component
*/
SIGHT_DATA_API static offset_t compute_strides(size_t _size, std::size_t _size_of_type);
/// Return buffer object
///@{
core::memory::buffer_object::sptr get_buffer_object();
core::memory::buffer_object::csptr get_buffer_object() const;
///@}
/// Set buffer object
void set_buffer_object(const core::memory::buffer_object::sptr& _buffer_obj);
/// Exchanges the content of the array with the content of _source.
SIGHT_DATA_API void swap(array::sptr _source) noexcept;
template<typename T>
using iterator = array_iterator<T>;
template<typename T>
using const_iterator = array_iterator<const T>;
/**
* @brief Get the value of an element
*
* @tparam T Type in which the pointer will be returned
* @param _id Item array index
*
* @return Buffer value cast to T
* @warning This method is slow and should not be used intensively
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
* @throw Exception Index out of bounds
*/
template<typename T>
T& at(const array::index_t& _id);
/**
* @brief Get the value of an element
*
* @tparam T Type in which the pointer will be returned
* @param _id Item id
*
* @return Buffer value cast to T
* @warning This method is slow and should not be used intensively
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
* @throw Exception Index out of bounds
*/
template<typename T>
const T& at(const array::index_t& _id) const;
/**
* @brief Get the value of an element
*
* @tparam T Type in which the pointer will be returned
* @param _offset Index of the item in the buffer cast to T
*
* @return Buffer value cast to T
* @warning This method is slow and should not be used intensively
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
* @throw Exception Index out of bounds
*/
template<typename T>
T& at(const std::size_t& _offset);
/**
* @brief Get the value of an element
*
* @tparam T Type in which the pointer will be returned
* @param _offset Index of the item in the buffer cast to T
*
* @return Buffer value cast to T
* @warning This method is slow and should not be used intensively
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
* @throw Exception Index out of bounds
*/
template<typename T>
const T& at(const std::size_t& _offset) const;
/**
* @brief Getter for the array buffer
*
* @return array's buffer, if exists, else NULL
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
* @{
*/
SIGHT_DATA_API void* buffer();
SIGHT_DATA_API const void* buffer() const;
///@}
/**
* @brief Setter for the array buffer.
*
* An existing buffer will be released if the array own it.
*
* @param _buf Buffer to set as array's buffer
* @param _take_ownership if true, the array will manage allocation and destroy the buffer when needed.
* @param _size Size of the array view
* @param _type Type of the array view
* @param _policy If the array takes ownership of the buffer, specifies the buffer allocation policy.
* @throw Exception The buffer cannot be accessed if the array is not locked (see dump_lock_impl()).
*/
SIGHT_DATA_API void set_buffer(
void* _buf,
bool _take_ownership,
const array::size_t& _size,
const core::type& _type,
core::memory::buffer_allocation_policy::sptr _policy = std::make_shared<core::memory::buffer_malloc_policy>()
);
/**
* @brief Returns the beginning/end iterators to the array buffer, cast to T
* @warning Print a warning if T is different from the array type
* @note These functions lock the buffer
* @{
*/
template<typename T>
iterator<T> begin();
template<typename T>
iterator<T> end();
template<typename T>
const_iterator<T> begin() const;
template<typename T>
const_iterator<T> end() const;
template<typename T>
const_iterator<T> cbegin() const;
template<typename T>
const_iterator<T> cend() const;
/// @}
/**
* @brief Returns a range of begin/end iterators, especially useful to be used in "for range loops".
* @{
*/
template<typename T>
auto range();
template<typename T>
auto range() const;
template<typename T>
auto crange() const;
/// @}
///
/**
* @brief Returns the beginning/end iterators to the array buffer, cast to char*
* @note These functions lock the buffer
*
* The iteration depends of the given format. The format can be the buffer type ([u]int[8|16|32|64], double, float),
* but
* can also be a simple struct like:
*
* @code{.cpp}
struct rgba {
std::uint8_t r;
std::uint8_t g;
std::uint8_t b;
std::uint8_t a;
};
@endcode
*
* This struct allows to parse the array as an rgba buffer (RGBARGBARGBA....).
*
* Example :
* @code{.cpp}
array::sptr array = array::New();
array->resize({1920, 1080}, core::type::INT16);
auto iter = array->begin<std::int16_t>();
const auto iterEnd = array->end<std::int16_t>();
for (; iter != iterEnd; ++iter)
{
* iter = value;
}
@endcode
* @warning The iterator does not assert that the array type is the same as the given format. It only asserts (in
* debug) that the iterator does not iterate outside of the buffer bounds).
* @note These functions lock the buffer for dump (see lock()).
* @{
*/
SIGHT_DATA_API iterator<char> begin();
SIGHT_DATA_API iterator<char> end();
SIGHT_DATA_API const_iterator<char> begin() const;
SIGHT_DATA_API const_iterator<char> end() const;
/// @}
/// Equality comparison operators
/// @{
SIGHT_DATA_API bool operator==(const array& _other) const noexcept;
SIGHT_DATA_API bool operator!=(const array& _other) const noexcept;
/// @}
/// Defines shallow copy
/// @throws data::exception if an errors occurs during copy
/// @param[in] _source the source object to copy
SIGHT_DATA_API void shallow_copy(const object::csptr& _source) override;
/// Defines deep copy
/// @throws data::exception if an errors occurs during copy
/// @param _source source object to copy
/// @param _cache cache used to deduplicate pointers
SIGHT_DATA_API void deep_copy(
const object::csptr& _source,
const std::unique_ptr<deep_copy_cache_t>& _cache = std::make_unique<deep_copy_cache_t>()
) override;
protected:
/**
* @brief Protected setter for the array buffer.
*
* Releases the previous buffer if it is owned by the array.
*
* @param _buf Buffer to set as array's buffer
* @param _take_ownership if true, the array will manage allocation and destroy the buffer when needed.
* @param _policy If the array takes ownership of the buffer, specifies the buffer allocation policy.
*/
SIGHT_DATA_API void set_buffer(
void* _buf,
bool _take_ownership = false,
core::memory::buffer_allocation_policy::sptr _policy = std::make_shared<core::memory::buffer_malloc_policy>()
);
/**
* @brief Retrieves a pointer to the value at the given index.
*
* @param _id Item array index
* @return buffer item pointer
* @{
*/
SIGHT_DATA_API char* get_buffer_ptr(const array::index_t& _id);
SIGHT_DATA_API const char* get_buffer_ptr(const array::index_t& _id) const;
///@}
/**
* @brief Compute the offset of an element in the buffer.
* @param _id Item array index
* @return buffer offset
*/
SIGHT_DATA_API std::size_t get_buffer_offset(const array::index_t& _id) const;
/// Add a lock on the array in the given vector to prevent from dumping the buffer on the disk
/// This is needed for IBuffered interface implementation
SIGHT_DATA_API void dump_lock_impl(std::vector<core::memory::buffer_object::lock_t>& _locks) const override;
/// Not implemented
array(const array&);
array& operator=(const array&);
private:
offset_t m_strides {0};
core::type m_type;
core::memory::buffer_object::sptr m_buffer_object;
size_t m_size;
bool m_is_buffer_owner {true};
};
//-----------------------------------------------------------------------------
inline core::memory::buffer_object::csptr array::get_buffer_object() const
{
return m_buffer_object;
}
//-----------------------------------------------------------------------------
inline core::memory::buffer_object::sptr array::get_buffer_object()
{
return m_buffer_object;
}
//-----------------------------------------------------------------------------
inline void array::set_buffer_object(const core::memory::buffer_object::sptr& _buffer_obj)
{
m_buffer_object = _buffer_obj;
}
//------------------------------------------------------------------------------
template<typename T>
inline T& array::at(const array::index_t& _id)
{
const bool is_index_in_bounds =
std::equal(
_id.begin(),
_id.end(),
m_size.begin(),
[](const index_t::value_type& _a, const index_t::value_type& _b)
{
return _a < _b;
});
SIGHT_THROW_EXCEPTION_IF(exception("Index out of bounds"), !is_index_in_bounds);
return *reinterpret_cast<T*>(this->get_buffer_ptr(_id));
}
//------------------------------------------------------------------------------
template<typename T>
inline const T& array::at(const array::index_t& _id) const
{
const bool is_index_in_bounds =
std::equal(_id.begin(), _id.end(), m_size.begin(), std::less<>());
SIGHT_THROW_EXCEPTION_IF(exception("Index out of bounds"), !is_index_in_bounds);
return *reinterpret_cast<T*>(this->get_buffer_ptr(_id));
}
//------------------------------------------------------------------------------
template<typename T>
inline T& array::at(const std::size_t& _offset)
{
SIGHT_THROW_EXCEPTION_IF(
exception(
"Index out of bounds, " + std::to_string(_offset) + " is not in [0-"
+ std::to_string(this->size_in_bytes() / sizeof(T) - 1) + "]"
),
_offset >= this->size_in_bytes() / sizeof(T)
);
return *(reinterpret_cast<T*>(this->buffer()) + _offset);
}
//------------------------------------------------------------------------------
template<typename T>
inline const T& array::at(const std::size_t& _offset) const
{
SIGHT_THROW_EXCEPTION_IF(
exception(
"Index out of bounds, " + std::to_string(_offset) + " is not in [0-"
+ std::to_string(this->size_in_bytes() / sizeof(T) - 1) + "]"
),
_offset >= this->size_in_bytes() / sizeof(T)
);
return *(reinterpret_cast<const T*>(this->buffer()) + _offset);
}
//------------------------------------------------------------------------------
template<typename T>
inline array::iterator<T> array::begin()
{
return iterator<T>(static_cast<typename iterator<T>::pointer_t>(buffer()));
}
//------------------------------------------------------------------------------
template<typename T>
inline array::iterator<T> array::end()
{
auto itr = begin<T>();
itr += static_cast<typename iterator<T>::difference_type>(this->size_in_bytes() / sizeof(T));
return itr;
}
//------------------------------------------------------------------------------
template<typename T>
inline array::const_iterator<T> array::begin() const
{
return const_iterator<T>(static_cast<typename const_iterator<T>::pointer_t>(buffer()));
}
//------------------------------------------------------------------------------
template<typename T>
inline array::const_iterator<T> array::end() const
{
auto itr = begin<T>();
itr += static_cast<typename const_iterator<T>::difference_type>(this->size_in_bytes() / sizeof(T));
return itr;
}
//------------------------------------------------------------------------------
template<typename T>
inline array::const_iterator<T> array::cbegin() const
{
return const_iterator<T>(static_cast<typename const_iterator<T>::pointer_t>(buffer()));
}
//------------------------------------------------------------------------------
template<typename T>
inline array::const_iterator<T> array::cend() const
{
auto itr = begin<T>();
itr += static_cast<typename const_iterator<T>::difference_type>(this->size_in_bytes() / sizeof(T));
return itr;
}
//------------------------------------------------------------------------------
template<typename T>
auto array::range()
{
auto b = begin<T>();
auto e = end<T>();
return boost::make_iterator_range(b, e);
}
//------------------------------------------------------------------------------
template<typename T>
auto array::range() const
{
auto b = cbegin<T>();
auto e = cend<T>();
return boost::make_iterator_range(b, e);
}
//------------------------------------------------------------------------------
template<typename T>
auto array::crange() const
{
auto b = cbegin<T>();
auto e = cend<T>();
return boost::make_iterator_range(b, e);
}
} // namespace sight::data
|