21#ifndef LIBMV_IMAGE_ARRAY_ND_H
22#define LIBMV_IMAGE_ARRAY_ND_H
35template <
typename T,
int N>
55 std::memcpy(
Data(),
b.Data(),
sizeof(T) * Size());
80 std::memcpy(
Data(),
b.Data(),
sizeof(T) * Size());
96 for (
int i =
N - 1; i > 0; --i) {
108 template <
typename D>
125 int shape[
N] = {s0, s1};
126 for (
int i = 2; i <
N; ++i) {
138 int shape[] = {s0, s1, s2};
142 template <
typename D>
146 const D* other_data = other.Data();
147 for (
int i = 0; i <
Size(); ++i) {
148 data[i] =
T(other_data[i]);
153 for (
int i = 0; i <
Size(); ++i) {
160 for (
int i = 0; i <
Size(); ++i) {
177 for (
int i = 0; i <
N; ++i) {
195 for (
int i = 0; i <
N; ++i) {
196 offset += index(i) *
Stride(i);
214 int Offset(
int i0,
int i1,
int i2)
const {
230 assert(0 <= i0 && i0 <
Shape(0));
231 assert(0 <= i1 && i1 <
Shape(1));
237 assert(0 <= i0 && i0 <
Shape(0));
238 assert(0 <= i1 && i1 <
Shape(1));
239 assert(0 <= i2 && i2 <
Shape(2));
253 assert(0 <= i0 && i0 <
Shape(0));
254 assert(0 <= i1 && i1 <
Shape(1));
265 for (
int i = 0; i <
N; ++i) {
266 if (index(i) < 0 || index(i) >=
Shape(i)) {
278 return 0 <= i0 && i0 <
Shape(0) && 0 <= i1 && i1 <
Shape(1);
283 return 0 <= i0 && i0 <
Shape(0) && 0 <= i1 && i1 <
Shape(1) && 0 <= i2 &&
288 if (
shape_ != other.shape_) {
294 for (
int i = 0; i <
Size(); ++i) {
295 if (this->
Data()[i] != other.Data()[i]) {
303 return !(*
this == other);
307 assert(
Shape() = other.Shape());
310 for (
int i = 0; i < res.
Size(); ++i) {
311 res.
Data()[i] =
Data()[i] * other.Data()[i];
337 Array3D(
int height,
int width,
int depth = 1) :
Base(height, width, depth) {}
338 Array3D(T* data,
int height,
int width,
int depth = 1)
339 :
Base(data, height, width, depth) {}
341 void Resize(
int height,
int width,
int depth = 1) {
359 assert(0 <= i0 && i0 <
Height());
360 assert(0 <= i1 && i1 <
Width());
364 assert(0 <= i0 && i0 <
Height());
365 assert(0 <= i1 && i1 <
Width());
391 bool automatic_range_detection =
false);
397template <
typename AArrayType,
typename BArrayType,
typename CArrayType>
402 assert(a.Shape() ==
b.Shape());
407 typename CArrayType::Index index;
410 const typename CArrayType::Index& cShape = c->Shape();
411 for (
int i = 0; i < CArrayType::Index::SIZE; ++i) {
412 index(i) = cShape(i) - 1;
420 while (index(0) >= 0) {
421 (*c)(index) = a(index) *
b(index);
423 int dimension = CArrayType::Index::SIZE - 1;
424 index(dimension) = index(dimension) - 1;
425 while (dimension > 0 && index(dimension) < 0) {
426 index(dimension) = cShape(dimension) - 1;
427 index(dimension - 1) = index(dimension - 1) - 1;
433template <
typename TA,
typename TB,
typename TC>
439 assert(a.Shape(0) ==
b.Shape(0));
440 assert(a.Shape(1) ==
b.Shape(1));
441 assert(a.Shape(2) ==
b.Shape(2));
442 for (
int i = 0; i < a.Shape(0); ++i) {
443 for (
int j = 0; j < a.Shape(1); ++j) {
444 for (
int k = 0; k < a.Shape(2); ++k) {
445 (*c)(i, j, k) = TC(a(i, j, k) *
b(i, j, k));
451template <
typename TA,
typename TB,
typename TC>
457 assert(a.Shape(0) ==
b.Shape(0));
458 assert(a.Shape(1) ==
b.Shape(1));
459 assert(a.Shape(2) ==
b.Shape(2));
460 for (
int i = 0; i < a.Shape(0); ++i) {
461 for (
int j = 0; j < a.Shape(1); ++j) {
462 for (
int k = 0; k < a.Shape(2); ++k) {
463 (*c)(i, j, k) = TC(a(i, j, k) *
b(i, j, k));
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
3D array (row, column, channel).
const T & operator()(int i0, int i1, int i2=0) const
T & operator()(int i0, int i1, int i2=0)
Enable accessing with 2 indices for grayscale images.
void Resize(int height, int width, int depth=1)
Array3D(int height, int width, int depth=1)
Array3D(T *data, int height, int width, int depth=1)
A multidimensional array class.
ArrayND(int *shape)
Create an array with the specified shape.
bool own_data_
Flag if this Array either own or reference the data.
const Index & Strides() const
void resize(int rows, int cols)
T & operator()(int i0, int i1)
2D specialization.
void Resize(const Index &new_shape)
Create an array of shape s.
T * data_
Pointer to the first element of the array.
ArrayND()
Create an empty array.
ArrayND(const ArrayND< T, N > &b)
Copy constructor.
void ResizeLike(const ArrayND< D, N > &other)
int MemorySizeInBytes() const
Return the total amount of memory used by the array.
bool Contains(int i0, int i1) const
2D specialization.
void CopyFrom(const ArrayND< D, N > &other)
const Index & Shape() const
Return a tuple containing the length of each axis.
~ArrayND()
Destructor deletes pixel data.
ArrayND(T *data, int s0, int s1, int s2)
void Resize(const int *new_shape_array)
Resizes the array to shape s. All data is lost.
void Resize(int s0, int s1)
Resize a 2D array to shape (s0,s1).
const T * Data() const
Constant pointer to the first element of the array.
T & operator()(const Index &index)
Return a reference to the element at position index.
void Resize(int s0, int s1, int s2)
Resize a 3D array to shape (s0,s1,s2).
void Resize(int s0)
Resize a 1D array to length s0.
Tuple< int, N > Index
Type for the multidimensional indices.
ArrayND< T, N > operator*(const ArrayND< T, N > &other) const
int Shape(int axis) const
Return the length of an axis.
ArrayND(int s0, int s1, int s2)
const Index & Shapes() const
bool operator==(const ArrayND< T, N > &other) const
bool operator!=(const ArrayND< T, N > &other) const
const T & operator()(const Index &index) const
Return a constant reference to the element at position index.
const T & operator()(int i0, int i1, int i2) const
3D specialization.
const T & operator()(int i0, int i1) const
2D specialization.
T * Data()
Pointer to the first element of the array.
T & operator()(int i0, int i1, int i2)
3D specialization.
bool Contains(const Index &index) const
True if index is inside array.
int Offset(const Index &index) const
Distance between the first element and the element at position index.
int Offset(int i0, int i1, int i2) const
3D specialization.
int Stride(int axis) const
Return the distance between neighboring elements along axis.
bool Contains(int i0) const
1D specialization.
int Size() const
Return the number of elements of the array.
bool Contains(int i0, int i1, int i2) const
3D specialization.
Index strides_
How to jump to neighbors in each dimension.
ArrayND & operator=(const ArrayND< T, N > &b)
Assignation copies pixel data.
T & operator()(int i0)
1D specialization.
int Offset(int i0, int i1) const
2D specialization.
int Offset(int i0) const
1D specialization.
ArrayND(const Index &shape)
Create an array with the specified shape.
Index shape_
The number of element in each dimension.
const T & operator()(int i0) const
1D specialization.
void Reset(const Tuple< D, N > &b)
local_group_size(16, 16) .push_constant(Type b
void SplitChannels(const Array3Df &input, Array3Df *channel0, Array3Df *channel1, Array3Df *channel2)
void ByteArrayToScaledFloatArray(const Array3Du &byte_array, Array3Df *float_array)
Convert a byte array into a float array by dividing values by 255.
Array3D< unsigned char > Array3Du
void PrintArray(const Array3Df &array)
Array3D< unsigned int > Array3Dui
Array3D< float > Array3Df
Array3D< short > Array3Ds
void FloatArrayToScaledByteArray(const Array3Df &float_array, Array3Du *byte_array, bool automatic_range_detection)
void MultiplyElements(const AArrayType &a, const BArrayType &b, CArrayType *c)