Field.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2019 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. int64_t elementWidth = 0;
  39. int64_t elementHeight = 0;
  40. T *elements = nullptr;
  41. public:
  42. // Constructor
  43. Field(const int64_t width, const int64_t height, const T& defaultValue)
  44. : elementWidth(width), elementHeight(height) {
  45. impl_nonZeroLengthCheck(width, "New array width");
  46. impl_nonZeroLengthCheck(height, "New array height");
  47. int64_t size = width * height;
  48. this->elements = new T[size];
  49. for (int64_t index = 0; index < size; index++) {
  50. this->elements[index] = defaultValue;
  51. }
  52. }
  53. // Bound check
  54. bool inside(int64_t x, int64_t y) const {
  55. return x >= 0 && x < this->elementWidth && y >= 0 && y < this->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. T& unsafe_writeAccess(int64_t x, int64_t y) {
  60. assert(this->inside(x, y));
  61. return this->elements[x + y * this->elementWidth];
  62. }
  63. // Precondition: this->inside(x, y)
  64. const T& unsafe_readAccess(int64_t x, int64_t y) const {
  65. assert(this->inside(x, y));
  66. return this->elements[x + y * this->elementWidth];
  67. }
  68. // Clonable by default!
  69. // Be very careful not to accidentally pass a Field by value instead of reference,
  70. // otherwise your side-effects might write to a temporary copy
  71. // or time is wasted to clone an Field every time you look something up.
  72. Field(const Field<T>& source) {
  73. // Allocate to the same size as source.
  74. int64_t newSize = source.elementWidth * source.elementHeight;
  75. this->elements = new T[newSize];
  76. this->elementWidth = source.elementWidth;
  77. this->elementHeight = source.elementHeight;
  78. // Copy elements from source.
  79. for (int64_t e = 0; e < newSize; e++) {
  80. // Assign one element at a time, so that objects can be copy constructed.
  81. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  82. this->elements[e] = source.elements[e];
  83. }
  84. };
  85. // When assigning to the field, memory can be reused when the number of elements is the same.
  86. Field& operator=(const Field<T>& source) {
  87. int64_t oldSize = this->elementWidth * this->elementHeight;
  88. int64_t newSize = source.elementWidth * source.elementHeight;
  89. // Reallocate to the same size as source if needed.
  90. if (oldSize != newSize) {
  91. if (this->elements) delete[] this->elements;
  92. this->elements = new T[newSize];
  93. }
  94. // Update dimensions, even if the combined allocation size is the same.
  95. this->elementWidth = source.elementWidth;
  96. this->elementHeight = source.elementHeight;
  97. // Copy elements from source.
  98. for (int64_t e = 0; e < newSize; e++) {
  99. // Assign one element at a time, so that objects can be copy constructed.
  100. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  101. this->elements[e] = source.elements[e];
  102. }
  103. return *this;
  104. };
  105. // Destructor
  106. ~Field() { if (this->elements) delete[] this->elements; }
  107. // Get the element at (x, y) or the outside value when (x, y) is out-of-bound.
  108. T read_border(int64_t x, int64_t y, const T& outside) const {
  109. if (this->inside(x, y)) {
  110. return this->unsafe_readAccess(x, y);
  111. } else {
  112. return outside;
  113. }
  114. }
  115. // Get the element closest to (x, y), by clamping the coordinate to valid bounds.
  116. T read_clamp(int64_t x, int64_t y) const {
  117. if (x < 0) x = 0;
  118. if (x >= this->elementWidth) x = this->elementWidth - 1;
  119. if (y < 0) y = 0;
  120. if (y >= this->elementHeight) y = this->elementHeight - 1;
  121. return this->unsafe_readAccess(x, y);
  122. }
  123. // Write value to the element at (x, y) when inside of the bounds, ignoring the operation silently when outside.
  124. void write_ignore(int64_t x, int64_t y, const T& value) {
  125. if (this->inside(x, y)) {
  126. this->unsafe_writeAccess(x, y) = value;
  127. }
  128. }
  129. int64_t width() const {
  130. return this->elementWidth;
  131. }
  132. int64_t height() const {
  133. return this->elementHeight;
  134. }
  135. // Wrappers for access using UVector instead of separate (x, y) coordinates.
  136. bool inside(const UVector2D& location) const { return this->inside(location.x, location.y); }
  137. T& unsafe_writeAccess(const UVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  138. const T& unsafe_readAccess(const UVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  139. T read_border(const UVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  140. T read_clamp(UVector2D location) const { return this->read_clamp(location.x, location.y); }
  141. void write_ignore(const UVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  142. // Wrappers for access using IVector instead of separate (x, y) coordinates.
  143. bool inside(const IVector2D& location) const { return this->inside(location.x, location.y); }
  144. T& unsafe_writeAccess(const IVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  145. const T& unsafe_readAccess(const IVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  146. T read_border(const IVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  147. T read_clamp(IVector2D location) const { return this->read_clamp(location.x, location.y); }
  148. void write_ignore(const IVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  149. // Wrappers for access using LVector instead of separate (x, y) coordinates.
  150. bool inside(const LVector2D& location) const { return this->inside(location.x, location.y); }
  151. T& unsafe_writeAccess(const LVector2D &location) { return this->unsafe_writeAccess(location.x, location.y); }
  152. const T& unsafe_readAccess(const LVector2D &location) const { return this->unsafe_readAccess(location.x, location.y); }
  153. T read_border(const LVector2D& location, const T& outside) const { return this->read_border(location.x, location.y, outside); }
  154. T read_clamp(LVector2D location) const { return this->read_clamp(location.x, location.y); }
  155. void write_ignore(const LVector2D& location, const T& value) { this->write_ignore(location.x, location.y); }
  156. };
  157. }
  158. #endif