Field.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2025 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_COLLECTION_FIELD
  25. #define DFPSR_COLLECTION_FIELD
  26. #include "collections.h"
  27. #include "../math/IVector.h"
  28. #include "../math/LVector.h"
  29. #include "../math/UVector.h"
  30. namespace dsr {
  31. // A 2D version of Array with methods for padding reads and ignoring writes that are out-of-bound.
  32. // If you need more speed, pack elements into a Buffer and iterate
  33. // over them using SafePointer with SIMD aligned stride between rows.
  34. // Unlike Buffer, Field is a value type, so be careful not to pass it by value unless you intend to clone its content.
  35. template <typename T>
  36. class Field {
  37. private:
  38. Array<T> impl_elements;
  39. intptr_t impl_elementWidth = 0;
  40. intptr_t impl_elementHeight = 0;
  41. public:
  42. // Constructors
  43. Field()
  44. : impl_elements(), impl_elementWidth(0), impl_elementHeight(0) {
  45. }
  46. Field(const intptr_t width, const intptr_t height, const T& defaultValue) {
  47. if (width > 0 && height > 0) {
  48. this->impl_elements = Array<T>(width * height, defaultValue);
  49. this->impl_elementWidth = width;
  50. this->impl_elementHeight = height;
  51. }
  52. }
  53. // Bound check
  54. inline bool inside(intptr_t x, intptr_t y) const {
  55. return x >= 0 && x < this->impl_elementWidth && y >= 0 && y < this->impl_elementHeight;
  56. }
  57. // Direct memory access where bound checks are only applied in debug mode, so access out of bound will crash.
  58. // Precondition: this->inside(x, y)
  59. inline T& unsafe_writeAccess(intptr_t x, intptr_t y) {
  60. assert(this->inside(x, y));
  61. return this->impl_elements.unsafe_writeAccess(x + y * this->impl_elementWidth);
  62. }
  63. // Precondition: this->inside(x, y)
  64. inline const T& unsafe_readAccess(intptr_t x, intptr_t y) const {
  65. assert(this->inside(x, y));
  66. return this->impl_elements.unsafe_readAccess(x + y * this->impl_elementWidth);
  67. }
  68. // Destructor
  69. //~Field() { if (this->impl_elements) delete[] this->impl_elements; }
  70. // Get the element at (x, y) or the outside value when (x, y) is out-of-bound.
  71. T read_border(intptr_t x, intptr_t y, const T& outside) const {
  72. if (this->inside(x, y)) {
  73. return this->unsafe_readAccess(x, y);
  74. } else {
  75. return outside;
  76. }
  77. }
  78. // Get the element closest to (x, y), by clamping the coordinate to valid bounds.
  79. T read_clamp(intptr_t x, intptr_t y) const {
  80. if (x < 0) x = 0;
  81. if (x >= this->impl_elementWidth) x = this->impl_elementWidth - 1;
  82. if (y < 0) y = 0;
  83. if (y >= this->impl_elementHeight) y = this->impl_elementHeight - 1;
  84. return this->unsafe_readAccess(x, y);
  85. }
  86. // Write value to the element at (x, y) when inside of the bounds, ignoring the operation silently when outside.
  87. void write_ignore(intptr_t x, intptr_t y, const T& value) {
  88. if (this->inside(x, y)) {
  89. this->unsafe_writeAccess(x, y) = value;
  90. }
  91. }
  92. inline intptr_t width() const {
  93. return this->impl_elementWidth;
  94. }
  95. inline intptr_t height() const {
  96. return this->impl_elementHeight;
  97. }
  98. // Wrappers for access using UVector instead of separate (x, y) coordinates.
  99. bool inside(const UVector2D& location) const { return this->inside(location.x, location.y); }
  100. T& unsafe_writeAccess(const UVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  101. const T& unsafe_readAccess(const UVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  102. T read_border(const UVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  103. T read_clamp(UVector2D location) const { return this->read_clamp(location.x, location.y); }
  104. void write_ignore(const UVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  105. // Wrappers for access using IVector instead of separate (x, y) coordinates.
  106. bool inside(const IVector2D& location) const { return this->inside(location.x, location.y); }
  107. T& unsafe_writeAccess(const IVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  108. const T& unsafe_readAccess(const IVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  109. T read_border(const IVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  110. T read_clamp(IVector2D location) const { return this->read_clamp(location.x, location.y); }
  111. void write_ignore(const IVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  112. // Wrappers for access using LVector instead of separate (x, y) coordinates.
  113. bool inside(const LVector2D& location) const { return this->inside(location.x, location.y); }
  114. T& unsafe_writeAccess(const LVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  115. const T& unsafe_readAccess(const LVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  116. T read_border(const LVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  117. T read_clamp(LVector2D location) const { return this->read_clamp(location.x, location.y); }
  118. void write_ignore(const LVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  119. };
  120. }
  121. #endif