Updating lbpm_uCT_pp.cpp with new Array class, adding imfilter
This commit is contained in:
404
common/Array.h
404
common/Array.h
@@ -1,48 +1,68 @@
|
||||
#ifndef included_ArrayClass
|
||||
#define included_ArrayClass
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "shared_ptr.h"
|
||||
#include "common/Utilities.h"
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#define GET_ARRAY_INDEX(i1,i2,i3,i4) i1+d_N[0]*(i2+d_N[1]*(i3+d_N[2]*i4))
|
||||
#if defined(DEBUG) || defined(_DEBUG)
|
||||
#define CHECK_ARRAY_INDEX(i1,i2,i3,i4) \
|
||||
if ( GET_ARRAY_INDEX(i1,i2,i3,i4)>d_length ) \
|
||||
ERROR("Index exceeds array bounds");
|
||||
#define ARRAY_NDIM_MAX 5 // Maximum number of dimensions supported
|
||||
|
||||
|
||||
#define GET_ARRAY_INDEX3D( N, i1, i2, i3 ) i1 + N[0] * ( i2 + N[1] * i3 )
|
||||
#define GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) i1 + N[0] * ( i2 + N[1] * ( i3 + N[2] * i4 ) )
|
||||
#define GET_ARRAY_INDEX5D( N, i1, i2, i3, i4, i5 ) i1 + N[0] * ( i2 + N[1] * ( i3 + N[2] * ( i4 + N[3] * i5 ) ) )
|
||||
|
||||
#if defined( DEBUG ) || defined( _DEBUG )
|
||||
#define CHECK_ARRAY_INDEX3D( N, i1, i2, i3 ) \
|
||||
if ( GET_ARRAY_INDEX3D( N, i1, i2, i3 ) < 0 || GET_ARRAY_INDEX3D( N, i1, i2, i3 ) >= d_length ) \
|
||||
throw std::logic_error( "Index exceeds array bounds" );
|
||||
#define CHECK_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) \
|
||||
if ( GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) < 0 || \
|
||||
GET_ARRAY_INDEX4D( N, i1, i2, i3, i4 ) >= d_length ) \
|
||||
throw std::logic_error( "Index exceeds array bounds" );
|
||||
#else
|
||||
#define CHECK_ARRAY_INDEX(i1,i2,i3,i4)
|
||||
#define CHECK_ARRAY_INDEX3D( N, i1, i2, i3 )
|
||||
#define CHECK_ARRAY_INDEX4D( N, i1, i2, i3, i4 )
|
||||
#endif
|
||||
|
||||
|
||||
#if defined( __CUDA_ARCH__ )
|
||||
#include <cuda.h>
|
||||
#define HOST_DEVICE __host__ __device__
|
||||
#else
|
||||
#define HOST_DEVICE
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
* Class Array is a simple array class
|
||||
* Class Array is a multi-dimensional array class written by Mark Berrill
|
||||
*/
|
||||
template<class TYPE>
|
||||
template <class TYPE>
|
||||
class Array
|
||||
{
|
||||
public:
|
||||
|
||||
/*!
|
||||
* Create a new empty Array
|
||||
*/
|
||||
Array( );
|
||||
Array();
|
||||
|
||||
/*!
|
||||
* Create a new 1D Array with the given number of elements
|
||||
* @param N Number of elements in the array
|
||||
*/
|
||||
Array( size_t N );
|
||||
explicit Array( size_t N );
|
||||
|
||||
/*!
|
||||
* Create a new 2D Array with the given number of rows and columns
|
||||
* @param N_rows Number of rows
|
||||
* @param N_columns Number of columns
|
||||
*/
|
||||
Array( size_t N_rows, size_t N_columns );
|
||||
explicit Array( size_t N_rows, size_t N_columns );
|
||||
|
||||
/*!
|
||||
* Create a new 3D Array with the given number of rows and columns
|
||||
@@ -50,33 +70,52 @@ public:
|
||||
* @param N2 Number of columns
|
||||
* @param N3 Number of elements in the third dimension
|
||||
*/
|
||||
Array( size_t N1, size_t N2, size_t N3 );
|
||||
explicit Array( size_t N1, size_t N2, size_t N3 );
|
||||
|
||||
/*!
|
||||
* Create a multi-dimensional Array with the given number of elements
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Optional raw array to copy the src data
|
||||
*/
|
||||
Array( const std::vector<size_t>& N );
|
||||
explicit Array( const std::vector<size_t> &N, const TYPE *data = NULL );
|
||||
|
||||
/*!
|
||||
* Copy constructor
|
||||
* @param rhs Array to copy
|
||||
*/
|
||||
Array( const Array& rhs );
|
||||
Array( const Array &rhs );
|
||||
|
||||
/*!
|
||||
* Move constructor
|
||||
* @param rhs Array to copy
|
||||
*/
|
||||
Array( Array &&rhs );
|
||||
|
||||
/*!
|
||||
* Assignment operator
|
||||
* @param rhs Array to copy
|
||||
*/
|
||||
Array& operator=( const Array& rhs );
|
||||
|
||||
|
||||
Array &operator=( const Array &rhs );
|
||||
|
||||
/*!
|
||||
* Move assignment operator
|
||||
* @param rhs Array to copy
|
||||
*/
|
||||
Array &operator=( Array &&rhs );
|
||||
|
||||
/*!
|
||||
* Assignment operator
|
||||
* @param rhs std::vector to copy
|
||||
*/
|
||||
Array &operator=( const std::vector<TYPE> &rhs );
|
||||
|
||||
|
||||
/*!
|
||||
* Create a 1D Array view to a raw block of data
|
||||
* @param N Number of elements in the array
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<Array> view( size_t N, std::shared_ptr<TYPE> data );
|
||||
static std::shared_ptr<Array> view( size_t N, std::shared_ptr<TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a new 2D Array with the given number of rows and columns
|
||||
@@ -84,7 +123,8 @@ public:
|
||||
* @param N_columns Number of columns
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<Array> view( size_t N_rows, size_t N_columns, std::shared_ptr<TYPE> data );
|
||||
static std::shared_ptr<Array> view(
|
||||
size_t N_rows, size_t N_columns, std::shared_ptr<TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a new 3D Array view to a raw block of data
|
||||
@@ -93,22 +133,25 @@ public:
|
||||
* @param N3 Number of elements in the third dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<Array> view( size_t N1, size_t N2, size_t N3, std::shared_ptr<TYPE> data );
|
||||
static std::shared_ptr<Array> view(
|
||||
size_t N1, size_t N2, size_t N3, std::shared_ptr<TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a multi-dimensional Array view to a raw block of data
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<Array> view( const std::vector<size_t>& N, std::shared_ptr<TYPE> data );
|
||||
static std::shared_ptr<Array> view(
|
||||
const std::vector<size_t> &N, std::shared_ptr<TYPE> const &data );
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
* Create a 1D Array view to a raw block of data
|
||||
* @param N Number of elements in the array
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<const Array> constView( size_t N, std::shared_ptr<const TYPE> data );
|
||||
static std::shared_ptr<const Array> constView(
|
||||
size_t N, std::shared_ptr<const TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a new 2D Array with the given number of rows and columns
|
||||
@@ -116,7 +159,8 @@ public:
|
||||
* @param N_columns Number of columns
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<const Array> constView( size_t N_rows, size_t N_columns, std::shared_ptr<const TYPE> data );
|
||||
static std::shared_ptr<const Array> constView(
|
||||
size_t N_rows, size_t N_columns, std::shared_ptr<const TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a new 3D Array view to a raw block of data
|
||||
@@ -125,63 +169,121 @@ public:
|
||||
* @param N3 Number of elements in the third dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<const Array> constView( size_t N1, size_t N2, size_t N3, std::shared_ptr<const TYPE> data );
|
||||
static std::shared_ptr<const Array> constView(
|
||||
size_t N1, size_t N2, size_t N3, std::shared_ptr<const TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Create a multi-dimensional Array view to a raw block of data
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
static std::shared_ptr<const Array> constView( const std::vector<size_t>& N, std::shared_ptr<const TYPE> data );
|
||||
static std::shared_ptr<const Array> constView(
|
||||
const std::vector<size_t> &N, std::shared_ptr<const TYPE> const &data );
|
||||
|
||||
|
||||
/*!
|
||||
* Make this object a view of the src
|
||||
* @param src Source vector to take the view of
|
||||
*/
|
||||
void view2( Array &src );
|
||||
|
||||
/*!
|
||||
* Make this object a view of the data
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
void view2( const std::vector<size_t> &N, std::shared_ptr<TYPE> const &data );
|
||||
|
||||
/*!
|
||||
* Make this object a view of the raw data (expert use only).
|
||||
* Use view2( N, std::shared_ptr(data,[](TYPE*){}) ) instead.
|
||||
* Note: this interface is not recommended as it does not protect from
|
||||
* the src data being deleted while still being used by the Array.
|
||||
* Additionally for maximum performance it does not set the internal shared_ptr
|
||||
* so functions like getPtr and resize will not work correctly.
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
void viewRaw( const std::initializer_list<size_t> &N, TYPE *data );
|
||||
|
||||
/*!
|
||||
* Make this object a view of the raw data (expert use only).
|
||||
* Use view2( N, std::shared_ptr(data,[](TYPE*){}) ) instead.
|
||||
* Note: this interface is not recommended as it does not protect from
|
||||
* the src data being deleted while still being used by the Array.
|
||||
* Additionally for maximum performance it does not set the internal shared_ptr
|
||||
* so functions like getPtr and resize will not work correctly.
|
||||
* @param N Number of elements in each dimension
|
||||
* @param data Pointer to the data
|
||||
*/
|
||||
void viewRaw( const std::vector<size_t> &N, TYPE *data );
|
||||
|
||||
/*!
|
||||
* Convert an array of one type to another. This may or may not allocate new memory.
|
||||
* @param array Input array
|
||||
*/
|
||||
template <class TYPE2>
|
||||
static std::shared_ptr<Array<TYPE2>> convert( std::shared_ptr<Array<TYPE>> array );
|
||||
|
||||
|
||||
/*!
|
||||
* Convert an array of one type to another. This may or may not allocate new memory.
|
||||
* @param array Input array
|
||||
*/
|
||||
template<class TYPE2>
|
||||
static std::shared_ptr<Array<TYPE2> > convert( std::shared_ptr<Array<TYPE> > array );
|
||||
|
||||
|
||||
/*!
|
||||
* Convert an array of one type to another. This may or may not allocate new memory.
|
||||
* @param array Input array
|
||||
*/
|
||||
template<class TYPE2>
|
||||
static std::shared_ptr<const Array<TYPE2> > convert( std::shared_ptr<const Array<TYPE> > array );
|
||||
template <class TYPE2>
|
||||
static std::shared_ptr<const Array<TYPE2>> convert( std::shared_ptr<const Array<TYPE>> array );
|
||||
|
||||
|
||||
/*!
|
||||
* Copy and convert data from another array to this array
|
||||
* @param array Source array
|
||||
*/
|
||||
template<class TYPE2>
|
||||
void copy( const Array<TYPE2>& array );
|
||||
template <class TYPE2>
|
||||
void copy( const Array<TYPE2> &array );
|
||||
|
||||
/*!
|
||||
* Copy and convert data from a raw vector to this array.
|
||||
* Note: The current array must be allocated to the proper size first.
|
||||
* @param array Source array
|
||||
*/
|
||||
template<class TYPE2>
|
||||
void copy( const TYPE2* array );
|
||||
template <class TYPE2>
|
||||
void copy( const TYPE2 *array );
|
||||
|
||||
/*!
|
||||
* Copy and convert data from this array to a raw vector.
|
||||
* @param array Source array
|
||||
*/
|
||||
template <class TYPE2>
|
||||
void copyTo( TYPE2 *array ) const;
|
||||
|
||||
|
||||
/*!
|
||||
* Fill the array with the given value
|
||||
* @param value Value to fill
|
||||
*/
|
||||
void fill( const TYPE& value );
|
||||
void fill( const TYPE &value );
|
||||
|
||||
/*!
|
||||
* Scale the array by the given value
|
||||
* @param scale Value to scale by
|
||||
*/
|
||||
void scale( const TYPE &scale );
|
||||
|
||||
|
||||
//! Destructor
|
||||
~Array( );
|
||||
~Array();
|
||||
|
||||
|
||||
//! Clear the data in the array
|
||||
void clear();
|
||||
|
||||
|
||||
//! Return the size of the Array
|
||||
inline int ndim( ) const { return d_ndim; }
|
||||
inline int ndim() const { return d_ndim; }
|
||||
|
||||
|
||||
//! Return the size of the Array
|
||||
inline std::vector<size_t> size( ) const { return std::vector<size_t>(d_N,d_N+d_ndim); }
|
||||
std::vector<size_t> size() const;
|
||||
|
||||
|
||||
//! Return the size of the Array
|
||||
@@ -189,11 +291,11 @@ public:
|
||||
|
||||
|
||||
//! Return the size of the Array
|
||||
inline size_t length( ) const { return d_length; }
|
||||
inline size_t length() const { return d_length; }
|
||||
|
||||
|
||||
//! Return true if the Array is empty
|
||||
inline bool empty( ) const { return d_length==0; }
|
||||
inline bool empty() const { return d_length == 0; }
|
||||
|
||||
|
||||
/*!
|
||||
@@ -221,126 +323,232 @@ public:
|
||||
* Resize the Array
|
||||
* @param N Number of elements in each dimension
|
||||
*/
|
||||
void resize( const std::vector<size_t>& N );
|
||||
void resize( const std::vector<size_t> &N );
|
||||
|
||||
/*!
|
||||
* Resize the given dimension of the array
|
||||
* @param dim The dimension to resize
|
||||
* @param N Number of elements for the given dimension
|
||||
* @param value Value to initialize new elements
|
||||
*/
|
||||
void resizeDim( int dim, size_t N, const TYPE &value );
|
||||
|
||||
|
||||
/*!
|
||||
* Reshape the Array (total size of array will not change)
|
||||
* @param N Number of elements in each dimension
|
||||
*/
|
||||
void reshape( const std::vector<size_t>& N );
|
||||
void reshape( const std::vector<size_t> &N );
|
||||
|
||||
|
||||
/*!
|
||||
* Subset the Array (total size of array will not change)
|
||||
* @param index Index to subset (imin,imax,jmin,jmax,kmin,kmax,...)
|
||||
*/
|
||||
template<class TYPE2=TYPE>
|
||||
Array<TYPE2> subset( const std::vector<size_t> &index ) const;
|
||||
|
||||
/*!
|
||||
* Copy data from an array into a subset of this array
|
||||
* @param index Index of the subset (imin,imax,jmin,jmax,kmin,kmax,...)
|
||||
* @param subset The subset array to copy from
|
||||
*/
|
||||
template <class TYPE2>
|
||||
void copySubset( const std::vector<size_t> &index, const Array<TYPE2> &subset );
|
||||
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
*/
|
||||
HOST_DEVICE inline TYPE &operator()( size_t i )
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, 0, 0 ) return d_data[i];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
*/
|
||||
HOST_DEVICE inline const TYPE &operator()( size_t i ) const
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, 0, 0 ) return d_data[i];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
*/
|
||||
inline TYPE& operator()( size_t i ) { CHECK_ARRAY_INDEX(i,0,0,0) return d_data[i]; }
|
||||
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j )
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, j, 0 ) return d_data[i + j * d_N[0]];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
*/
|
||||
inline const TYPE& operator()( size_t i ) const { CHECK_ARRAY_INDEX(i,0,0,0) return d_data[i]; }
|
||||
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j ) const
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, j, 0 ) return d_data[i + j * d_N[0]];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
* @param k The third index
|
||||
*/
|
||||
inline TYPE& operator()( size_t i, size_t j ) { CHECK_ARRAY_INDEX(i,j,0,0) return d_data[i+j*d_N[0]]; }
|
||||
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j, size_t k )
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, j, k ) return d_data[GET_ARRAY_INDEX3D( d_N, i, j, k )];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
* @param k The third index
|
||||
*/
|
||||
inline const TYPE& operator()( size_t i, size_t j ) const { CHECK_ARRAY_INDEX(i,j,0,0) return d_data[i+j*d_N[0]]; }
|
||||
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j, size_t k ) const
|
||||
{
|
||||
CHECK_ARRAY_INDEX3D( d_N, i, j, k ) return d_data[GET_ARRAY_INDEX3D( d_N, i, j, k )];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
* @param k The third index
|
||||
* @param l The fourth index
|
||||
*/
|
||||
inline TYPE& operator()( size_t i, size_t j, size_t k ) { CHECK_ARRAY_INDEX(i,j,k,0) return d_data[GET_ARRAY_INDEX(i,j,k,0)]; }
|
||||
HOST_DEVICE inline TYPE &operator()( size_t i, size_t j, size_t k, size_t l )
|
||||
{
|
||||
CHECK_ARRAY_INDEX4D( d_N, i, j, k, l ) return d_data[GET_ARRAY_INDEX4D( d_N, i, j, k, l )];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
* @param k The third index
|
||||
* @param l The fourth index
|
||||
*/
|
||||
inline const TYPE& operator()( size_t i, size_t j, size_t k ) const { CHECK_ARRAY_INDEX(i,j,k,0) return d_data[GET_ARRAY_INDEX(i,j,k,0)]; }
|
||||
HOST_DEVICE inline const TYPE &operator()( size_t i, size_t j, size_t k, size_t l ) const
|
||||
{
|
||||
CHECK_ARRAY_INDEX4D( d_N, i, j, k, l ) return d_data[GET_ARRAY_INDEX4D( d_N, i, j, k, l )];
|
||||
}
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
*/
|
||||
inline TYPE& operator()( size_t i, size_t j, size_t k, size_t m ) { CHECK_ARRAY_INDEX(i,j,k,m) return d_data[GET_ARRAY_INDEX(i,j,k,m)]; }
|
||||
//! Check if two matrices are equal
|
||||
// Equality means the dimensions and data have to be identical
|
||||
bool operator==( const Array &rhs ) const;
|
||||
|
||||
/*!
|
||||
* Access the desired element
|
||||
* @param i The row index
|
||||
* @param j The column index
|
||||
*/
|
||||
inline const TYPE& operator()( size_t i, size_t j, size_t k, size_t m ) const { CHECK_ARRAY_INDEX(i,j,k,m) return d_data[GET_ARRAY_INDEX(i,j,k,m)]; }
|
||||
|
||||
|
||||
//! Check if two matricies are equal
|
||||
bool operator==( const Array& rhs ) const;
|
||||
|
||||
//! Check if two matricies are not equal
|
||||
inline bool operator!=( const Array& rhs ) const { return !this->operator==(rhs); }
|
||||
//! Check if two matrices are not equal
|
||||
inline bool operator!=( const Array &rhs ) const { return !this->operator==( rhs ); }
|
||||
|
||||
|
||||
//! Return the pointer to the raw data
|
||||
inline std::shared_ptr<TYPE> getPtr( ) { return d_ptr; }
|
||||
inline std::shared_ptr<TYPE> getPtr() { return d_ptr; }
|
||||
|
||||
//! Return the pointer to the raw data
|
||||
inline std::shared_ptr<const TYPE> getPtr( ) const { return d_ptr; }
|
||||
inline std::shared_ptr<const TYPE> getPtr() const { return d_ptr; }
|
||||
|
||||
//! Return the pointer to the raw data
|
||||
inline TYPE* get( ) { return d_data; }
|
||||
HOST_DEVICE inline TYPE *data() { return d_data; }
|
||||
|
||||
//! Return the pointer to the raw data
|
||||
inline const TYPE* get( ) const { return d_data; }
|
||||
HOST_DEVICE inline const TYPE *data() const { return d_data; }
|
||||
|
||||
|
||||
//! Return true if NaNs are present
|
||||
inline bool NaNs( ) const;
|
||||
inline bool NaNs() const;
|
||||
|
||||
//! Return the smallest value
|
||||
inline TYPE min( ) const;
|
||||
inline TYPE min() const;
|
||||
|
||||
//! Return the largest value
|
||||
inline TYPE max( ) const;
|
||||
inline TYPE max() const;
|
||||
|
||||
//! Return the sum of all elements
|
||||
inline TYPE sum( ) const;
|
||||
inline TYPE sum() const;
|
||||
|
||||
//! Return the mean of all elements
|
||||
inline TYPE mean() const;
|
||||
|
||||
//! Return the min of all elements in a given direction
|
||||
Array<TYPE> min( int dir ) const;
|
||||
|
||||
//! Return the max of all elements in a given direction
|
||||
Array<TYPE> max( int dir ) const;
|
||||
|
||||
//! Return the sum of all elements in a given direction
|
||||
std::shared_ptr<Array<TYPE> > sum( int dir ) const;
|
||||
Array<TYPE> sum( int dir ) const;
|
||||
|
||||
//! Swap the data in this with rhs
|
||||
inline void swap( Array& rhs );
|
||||
//! Return the smallest value
|
||||
inline TYPE min( const std::vector<size_t> &index ) const;
|
||||
|
||||
//! Return the largest value
|
||||
inline TYPE max( const std::vector<size_t> &index ) const;
|
||||
|
||||
//! Return the sum of all elements
|
||||
inline TYPE sum( const std::vector<size_t> &index ) const;
|
||||
|
||||
//! Return the mean of all elements
|
||||
inline TYPE mean( const std::vector<size_t> &index ) const;
|
||||
|
||||
//! Find all elements that match the operator
|
||||
std::vector<size_t> find(
|
||||
const TYPE &value, std::function<bool( const TYPE &, const TYPE & )> compare ) const;
|
||||
|
||||
//! Add another array
|
||||
Array &operator+=( const Array &rhs );
|
||||
|
||||
//! Subtract another array
|
||||
Array &operator-=( const Array &rhs );
|
||||
|
||||
//! Add a scalar
|
||||
Array &operator+=( const TYPE &rhs );
|
||||
|
||||
//! Subtract a scalar
|
||||
Array &operator-=( const TYPE &rhs );
|
||||
|
||||
//! Print an array
|
||||
void print( std::ostream& os, const std::string& name="A", const std::string& prefix="" ) const;
|
||||
|
||||
//! Multiply two arrays
|
||||
static Array multiply( const Array& a, const Array& b );
|
||||
|
||||
//! Transpose an array
|
||||
Array<TYPE> reverseDim( ) const;
|
||||
|
||||
//! Coarsen an array using the given filter
|
||||
Array<TYPE> coarsen( const Array<TYPE>& filter ) const;
|
||||
|
||||
private:
|
||||
int d_ndim;
|
||||
size_t d_N[4];
|
||||
size_t d_length;
|
||||
TYPE *d_data;
|
||||
std::shared_ptr<TYPE> d_ptr;
|
||||
void allocate( const std::vector<size_t>& N );
|
||||
int d_ndim; // Number of dimensions in array
|
||||
size_t d_N[ARRAY_NDIM_MAX]; // Size of each dimension
|
||||
size_t d_length; // Total length of array
|
||||
TYPE *d_data; // Raw pointer to data in array
|
||||
std::shared_ptr<TYPE> d_ptr; // Shared pointer to data in array
|
||||
void allocate( const std::vector<size_t> &N );
|
||||
|
||||
private:
|
||||
template<class TYPE2>
|
||||
inline bool sizeMatch( const Array<TYPE2>& rhs ) const;
|
||||
inline void checkSubsetIndex( const std::vector<size_t> &index ) const;
|
||||
inline std::array<size_t, 5> getDimArray() const;
|
||||
static inline void getSubsetArrays( const std::vector<size_t> &index,
|
||||
std::array<size_t, 5> &first, std::array<size_t, 5> &last, std::array<size_t, 5> &N );
|
||||
};
|
||||
|
||||
|
||||
typedef Array<int> IntArray;
|
||||
typedef Array<double> DoubleArray;
|
||||
typedef Array<float> FloatArray;
|
||||
typedef Array<int> IntArray;
|
||||
|
||||
|
||||
#include "common/Array.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
1026
common/Array.hpp
1026
common/Array.hpp
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,8 @@
|
||||
#define COMMUNICATION_H_INC
|
||||
|
||||
#include "common/MPI_Helpers.h"
|
||||
#include "Array.h"
|
||||
#include "common/Utilities.h"
|
||||
#include "common/Array.h"
|
||||
|
||||
// ********** COMMUNICTION **************************************
|
||||
/*
|
||||
@@ -287,7 +288,7 @@ inline void CommunicateMeshHalo(DoubleArray &Mesh, MPI_Comm Communicator,
|
||||
{
|
||||
int sendtag, recvtag;
|
||||
sendtag = recvtag = 7;
|
||||
double *MeshData = Mesh.get();
|
||||
double *MeshData = Mesh.data();
|
||||
PackMeshData(sendList_x, sendCount_x ,sendbuf_x, MeshData);
|
||||
PackMeshData(sendList_X, sendCount_X ,sendbuf_X, MeshData);
|
||||
PackMeshData(sendList_y, sendCount_y ,sendbuf_y, MeshData);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "common/Communication.h"
|
||||
#include "common/MPI_Helpers.h"
|
||||
#include "common/Utilities.h"
|
||||
#include "ProfilerApp.h"
|
||||
|
||||
|
||||
|
||||
@@ -547,7 +547,7 @@ void Domain::CommunicateMeshHalo(DoubleArray &Mesh)
|
||||
{
|
||||
int sendtag, recvtag;
|
||||
sendtag = recvtag = 7;
|
||||
double *MeshData = Mesh.get();
|
||||
double *MeshData = Mesh.data();
|
||||
PackMeshData(sendList_x, sendCount_x ,sendData_x, MeshData);
|
||||
PackMeshData(sendList_X, sendCount_X ,sendData_X, MeshData);
|
||||
PackMeshData(sendList_y, sendCount_y ,sendData_y, MeshData);
|
||||
@@ -631,7 +631,7 @@ void Domain::BlobComm(MPI_Comm Communicator)
|
||||
int sendtag, recvtag;
|
||||
sendtag = recvtag = 51;
|
||||
//......................................................................................
|
||||
int *BlobLabelData = BlobLabel.get();
|
||||
int *BlobLabelData = BlobLabel.data();
|
||||
PackBlobData(sendList_x, sendCount_x ,sendBuf_x, BlobLabelData);
|
||||
PackBlobData(sendList_X, sendCount_X ,sendBuf_X, BlobLabelData);
|
||||
PackBlobData(sendList_y, sendCount_y ,sendBuf_y, BlobLabelData);
|
||||
|
||||
@@ -790,8 +790,8 @@ void TwoPhase::ComponentAverages()
|
||||
}
|
||||
*/
|
||||
MPI_Barrier(Dm.Comm);
|
||||
MPI_Allreduce(ComponentAverages_NWP.get(),RecvBuffer.get(),BLOB_AVG_COUNT*NumberComponents_NWP, MPI_DOUBLE,MPI_SUM,Dm.Comm);
|
||||
// MPI_Reduce(ComponentAverages_NWP.get(),RecvBuffer.get(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,0,Dm.Comm);
|
||||
MPI_Allreduce(ComponentAverages_NWP.data(),RecvBuffer.data(),BLOB_AVG_COUNT*NumberComponents_NWP, MPI_DOUBLE,MPI_SUM,Dm.Comm);
|
||||
// MPI_Reduce(ComponentAverages_NWP.data(),RecvBuffer.data(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,0,Dm.Comm);
|
||||
|
||||
if (Dm.rank==0){
|
||||
printf("rescaling... \n");
|
||||
@@ -888,8 +888,8 @@ void TwoPhase::ComponentAverages()
|
||||
// reduce the wetting phase averages
|
||||
for (int b=0; b<NumberComponents_WP; b++){
|
||||
MPI_Barrier(Dm.Comm);
|
||||
// MPI_Allreduce(&ComponentAverages_WP(0,b),RecvBuffer.get(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,Dm.Comm);
|
||||
MPI_Reduce(&ComponentAverages_WP(0,b),RecvBuffer.get(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,0,Dm.Comm);
|
||||
// MPI_Allreduce(&ComponentAverages_WP(0,b),RecvBuffer.data(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,Dm.Comm);
|
||||
MPI_Reduce(&ComponentAverages_WP(0,b),RecvBuffer.data(),BLOB_AVG_COUNT,MPI_DOUBLE,MPI_SUM,0,Dm.Comm);
|
||||
for (int idx=0; idx<BLOB_AVG_COUNT; idx++) ComponentAverages_WP(idx,b)=RecvBuffer(idx);
|
||||
}
|
||||
|
||||
|
||||
159
common/imfilter.h
Normal file
159
common/imfilter.h
Normal file
@@ -0,0 +1,159 @@
|
||||
// These functions mimic the behavior of imfilter in MATLAB
|
||||
#ifndef included_imfilter
|
||||
#define included_imfilter
|
||||
|
||||
#include "common/Utilities.h"
|
||||
#include "common/Array.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace imfilter {
|
||||
|
||||
|
||||
//! enum to store the BC type
|
||||
enum class BC { fixed=0, symmetric=1, replicate=2, circular=3 };
|
||||
|
||||
|
||||
/*!
|
||||
* @brief N-D filtering of multidimensional images
|
||||
* @details imfilter filters the multidimensional array A with the
|
||||
* multidimensional filter H. The result B has the same size and class as A.
|
||||
* @param[in] A The input array (Nx,Ny,Nz)
|
||||
* @param[in] H The filter (2*Nhx+1,2*Nhy+1,...)
|
||||
* @param[in] boundary The boundary conditions to apply (ndim):
|
||||
* fixed - Input array values outside the bounds of the array are
|
||||
* implicitly assumed to have the value X
|
||||
* symmetric - Input array values outside the bounds of the array are
|
||||
* computed by mirror-reflecting the array across the array border
|
||||
* replicate - Input array values outside the bounds of the array are
|
||||
* assumed to equal the nearest array border value
|
||||
* circular - Input array values outside the bounds of the array are
|
||||
* computed by implicitly assuming the input array is periodic.
|
||||
* @param[in] X The value to use for boundary conditions (only used if boundary==fixed)
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter( const Array<TYPE>& A, const Array<TYPE>& H, const std::vector<imfilter::BC>& boundary, const TYPE X=0 );
|
||||
|
||||
|
||||
/*!
|
||||
* @brief N-D filtering of multidimensional images
|
||||
* @details imfilter filters the multidimensional array A with the
|
||||
* multidimensional filter H. The result B has the same size and class as A.
|
||||
* @param[in] A The input array (Nx,Ny,Nz)
|
||||
* @param[in] Nh The size of the filter
|
||||
* @param[in] H The filter function to use ( y = H(data) )
|
||||
* Note that the data passed to this function will be of size 2*Nh+1
|
||||
* @param[in] boundary The boundary conditions to apply (ndim):
|
||||
* fixed - Input array values outside the bounds of the array are
|
||||
* implicitly assumed to have the value X
|
||||
* symmetric - Input array values outside the bounds of the array are
|
||||
* computed by mirror-reflecting the array across the array border
|
||||
* replicate - Input array values outside the bounds of the array are
|
||||
* assumed to equal the nearest array border value
|
||||
* circular - Input array values outside the bounds of the array are
|
||||
* computed by implicitly assuming the input array is periodic.
|
||||
* @param[in] X The value to use for boundary conditions (only used if boundary==fixed)
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter( const Array<TYPE>& A, const std::vector<int>& Nh,
|
||||
std::function<TYPE(const Array<TYPE>&)> H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X=0 );
|
||||
|
||||
|
||||
/*!
|
||||
* @brief N-D filtering of multidimensional images
|
||||
* @details imfilter filters the multidimensional array A with the
|
||||
* multidimensional filter H. The result B has the same size and class as A.
|
||||
* This version works with separable filters and is more efficient than a single filter.
|
||||
* @param[in] A The input array (Nx,Ny,Nz)
|
||||
* @param[in] H The filter [2*Nhx+1,2*Nhy+1,...]
|
||||
* @param[in] boundary The boundary conditions to apply (ndim):
|
||||
* fixed - Input array values outside the bounds of the array are
|
||||
* implicitly assumed to have the value X
|
||||
* symmetric - Input array values outside the bounds of the array are
|
||||
* computed by mirror-reflecting the array across the array border
|
||||
* replicate - Input array values outside the bounds of the array are
|
||||
* assumed to equal the nearest array border value
|
||||
* circular - Input array values outside the bounds of the array are
|
||||
* computed by implicitly assuming the input array is periodic.
|
||||
* @param[in] X The value to use for boundary conditions (only used if boundary==fixed)
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter_separable( const Array<TYPE>& A, const std::vector<Array<TYPE>>& H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X=0 );
|
||||
|
||||
|
||||
/*!
|
||||
* @brief N-D filtering of multidimensional images
|
||||
* @details imfilter filters the multidimensional array A with the
|
||||
* multidimensional filter H. The result B has the same size and class as A.
|
||||
* This version works with separable filters and is more efficient than a single filter.
|
||||
* @param[in] A The input array (Nx,Ny,Nz)
|
||||
* @param[in] H The filter [2*Nhx+1,2*Nhy+1,...]
|
||||
* @param[in] boundary The boundary conditions to apply (ndim):
|
||||
* fixed - Input array values outside the bounds of the array are
|
||||
* implicitly assumed to have the value X
|
||||
* symmetric - Input array values outside the bounds of the array are
|
||||
* computed by mirror-reflecting the array across the array border
|
||||
* replicate - Input array values outside the bounds of the array are
|
||||
* assumed to equal the nearest array border value
|
||||
* circular - Input array values outside the bounds of the array are
|
||||
* computed by implicitly assuming the input array is periodic.
|
||||
* @param[in] X The value to use for boundary conditions (only used if boundary==fixed)
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter_separable( const Array<TYPE>& A, const std::vector<int>& Nh,
|
||||
std::vector<std::function<TYPE(const Array<TYPE>&)>> H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X=0 );
|
||||
|
||||
|
||||
/*!
|
||||
* @brief N-D filtering of multidimensional images
|
||||
* @details imfilter filters the multidimensional array A with the
|
||||
* multidimensional filter H. The result B has the same size and class as A.
|
||||
* This version works with separable filters and is more efficient than a single filter.
|
||||
* @param[in] A The input array (Nx,Ny,Nz)
|
||||
* @param[in] H The filter [2*Nhx+1,2*Nhy+1,...]
|
||||
* @param[in] boundary The boundary conditions to apply (ndim):
|
||||
* fixed - Input array values outside the bounds of the array are
|
||||
* implicitly assumed to have the value X
|
||||
* symmetric - Input array values outside the bounds of the array are
|
||||
* computed by mirror-reflecting the array across the array border
|
||||
* replicate - Input array values outside the bounds of the array are
|
||||
* assumed to equal the nearest array border value
|
||||
* circular - Input array values outside the bounds of the array are
|
||||
* computed by implicitly assuming the input array is periodic.
|
||||
* @param[in] X The value to use for boundary conditions (only used if boundary==fixed)
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter_separable( const Array<TYPE>& A, const std::vector<int>& Nh,
|
||||
std::vector<std::function<TYPE(int, const TYPE*)>> H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X=0 );
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create a filter to use with imfilter
|
||||
* @details This function creates one of several predefined filters
|
||||
* to use with imfilter. The filter will always sum to 1.
|
||||
* Note: this function allocates memory with the new command, the user must call delete.
|
||||
*
|
||||
* @param[in] N The stencil size in each direction
|
||||
* @param[in] type The type of filter to create
|
||||
* average - Simple averaging filter
|
||||
* gaussian - Gaussian filter with given standard deviation.
|
||||
* Optional argument is a double array of size ndim
|
||||
* giving the standard deviation in each direction.
|
||||
* A default value of 0.5 is used if not provided.
|
||||
* \param[in] args An optional argument that some of the filters use
|
||||
*/
|
||||
template<class TYPE>
|
||||
Array<TYPE> create_filter( const std::vector<int>& N, const std::string &type, const void *args = NULL );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#include "common/imfilter.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
378
common/imfilter.hpp
Normal file
378
common/imfilter.hpp
Normal file
@@ -0,0 +1,378 @@
|
||||
#include "common/imfilter.h"
|
||||
#include "ProfilerApp.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define IMFILTER_INSIST INSIST
|
||||
#define IMFILTER_ASSERT ASSERT
|
||||
#define IMFILTER_ERROR ERROR
|
||||
|
||||
|
||||
// Function to convert an index
|
||||
static inline int imfilter_index( int index, const int N, const imfilter::BC bc )
|
||||
{
|
||||
if ( index < 0 || index >= N ) {
|
||||
if ( bc == imfilter::BC::symmetric ) {
|
||||
index = ( 2 * N - index ) % N;
|
||||
} else if ( bc == imfilter::BC::replicate ) {
|
||||
index = index < 0 ? 0 : N - 1;
|
||||
} else if ( bc == imfilter::BC::circular ) {
|
||||
index = ( index + N ) % N;
|
||||
} else if ( bc == imfilter::BC::fixed ) {
|
||||
index = -1;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
// Function to copy a 1D array and pad with the appropriate BC
|
||||
template<class TYPE>
|
||||
static inline void copy_array( const int N, const int Ns, const int Nh,
|
||||
const TYPE *A, const imfilter::BC BC, const TYPE X, TYPE *B )
|
||||
{
|
||||
// Fill the center with a memcpy
|
||||
for (int i=0; i<N; i++ )
|
||||
B[i+Nh] = A[i*Ns];
|
||||
// Fill the boundaries
|
||||
for (int i=0; i<Nh; i++ ) {
|
||||
int j1 = imfilter_index( -(i+1), N, BC );
|
||||
int j2 = imfilter_index( N+i, N, BC );
|
||||
B[Nh-i-1] = j1==-1 ? X : B[Nh+j1];
|
||||
B[N+Nh+i] = j2==-1 ? X : B[Nh+j2];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************************************************
|
||||
* Perform a 1D filter in a single direction *
|
||||
********************************************************/
|
||||
template<class TYPE>
|
||||
static void filter_direction( int Ns, int N, int Ne, int Nh, const TYPE *H,
|
||||
imfilter::BC boundary, TYPE X, TYPE *A )
|
||||
{
|
||||
if ( Nh < 0 )
|
||||
IMFILTER_ERROR("Invalid filter size");
|
||||
if ( Nh == 0 ) {
|
||||
for (int i=0; i<Ns*N*Ne; i++)
|
||||
A[i] *= H[0];
|
||||
return;
|
||||
}
|
||||
TYPE *tmp = new TYPE[N+2*Nh];
|
||||
for (int j=0; j<Ne; j++) {
|
||||
for (int i=0; i<Ns; i++) {
|
||||
copy_array( N, Ns, Nh, &A[i+j*Ns*N], boundary, X, tmp );
|
||||
for (int k=0; k<N; k++) {
|
||||
TYPE tmp2 = 0;
|
||||
for (int m=0; m<=2*Nh; m++)
|
||||
tmp2 += H[m] * tmp[k+m];
|
||||
A[i+k*Ns+j*Ns*N] = tmp2;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete[] tmp;
|
||||
}
|
||||
template<class TYPE>
|
||||
static void filter_direction( int Ns, int N, int Ne, int Nh,
|
||||
std::function<TYPE(const Array<TYPE>&)> H, imfilter::BC boundary, TYPE X, TYPE *A )
|
||||
{
|
||||
if ( Nh < 0 )
|
||||
IMFILTER_ERROR("Invalid filter size");
|
||||
TYPE *tmp = new TYPE[N+2*Nh];
|
||||
Array<TYPE> tmp2(2*Nh+1);
|
||||
for (int j=0; j<Ne; j++) {
|
||||
for (int i=0; i<Ns; i++) {
|
||||
copy_array( N, Ns, Nh, &A[i+j*Ns*N], boundary, X, tmp );
|
||||
for (int k=0; k<N; k++) {
|
||||
for (int m=0; m<=2*Nh; m++)
|
||||
tmp2(m) = tmp[k+m];
|
||||
A[i+k*Ns+j*Ns*N] = H(tmp2);
|
||||
}
|
||||
}
|
||||
}
|
||||
delete[] tmp;
|
||||
}
|
||||
template<class TYPE>
|
||||
static void filter_direction( int Ns, int N, int Ne, int Nh,
|
||||
std::function<TYPE(int, const TYPE*)> H, imfilter::BC boundary, TYPE X, TYPE *A )
|
||||
{
|
||||
if ( Nh < 0 )
|
||||
IMFILTER_ERROR("Invalid filter size");
|
||||
TYPE *tmp = new TYPE[N+2*Nh];
|
||||
int Nh2 = 2*Nh+1;
|
||||
for (int j=0; j<Ne; j++) {
|
||||
for (int i=0; i<Ns; i++) {
|
||||
copy_array( N, Ns, Nh, &A[i+j*Ns*N], boundary, X, tmp );
|
||||
for (int k=0; k<N; k++)
|
||||
A[i+k*Ns+j*Ns*N] = H(Nh2,&tmp[k]);
|
||||
}
|
||||
}
|
||||
delete[] tmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
* Create a filter *
|
||||
********************************************************/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::create_filter( const std::vector<int>& N0, const std::string &type, const void *args )
|
||||
{
|
||||
std::vector<size_t> N2(N0.size());
|
||||
for (size_t i=0; i<N2.size(); i++)
|
||||
N2[i] = 2*N0[i]+1;
|
||||
Array<TYPE> h(N2);
|
||||
h.fill(0);
|
||||
if ( type == "average" ) {
|
||||
// average
|
||||
h.fill( 1.0 / static_cast<TYPE>( h.length() ) );
|
||||
} else if ( type == "gaussian" ) {
|
||||
// gaussian
|
||||
if ( N0.size() > 3 )
|
||||
IMFILTER_ERROR( "Not implimented for dimensions > 3" );
|
||||
TYPE std[3] = { 0.5, 0.5, 0.5 };
|
||||
if ( args != NULL ) {
|
||||
const TYPE *args2 = reinterpret_cast<const TYPE*>( args );
|
||||
for ( int d = 0; d < N0.size(); d++ )
|
||||
std[d] = args2[d];
|
||||
}
|
||||
auto N = N0;
|
||||
N.resize(3,0);
|
||||
for ( int k = -N[2]; k <= N[2]; k++ ) {
|
||||
for ( int j = -N[1]; j <= N[1]; j++ ) {
|
||||
for ( int i = -N[0]; i <= N[0]; i++ ) {
|
||||
h(i+N[0],j+N[1],k+N[2]) =
|
||||
exp( -i * i / ( 2 * std[0] * std[0] ) ) *
|
||||
exp( -j * j / ( 2 * std[1] * std[1] ) ) *
|
||||
exp( -k * k / ( 2 * std[2] * std[2] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
h.scale( 1.0/h.sum() );
|
||||
} else {
|
||||
IMFILTER_ERROR( "Unknown filter" );
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
// Perform 2-D filtering
|
||||
template<class TYPE>
|
||||
void imfilter_2D( int Nx, int Ny, const TYPE *A, int Nhx, int Nhy, const TYPE *H,
|
||||
imfilter::BC BCx, imfilter::BC BCy, const TYPE X, TYPE *B )
|
||||
{
|
||||
IMFILTER_ASSERT( A != B );
|
||||
PROFILE_START( "imfilter_2D" );
|
||||
memset( B, 0, Nx * Ny * sizeof( TYPE ) );
|
||||
for ( int j1 = 0; j1 < Ny; j1++ ) {
|
||||
for ( int i1 = 0; i1 < Nx; i1++ ) {
|
||||
TYPE tmp = 0;
|
||||
if ( i1 >= Nhx && i1 < Nx - Nhx && j1 >= Nhy && j1 < Ny - Nhy ) {
|
||||
int ijkh = 0;
|
||||
for ( int j2 = j1 - Nhy; j2 <= j1 + Nhy; j2++ ) {
|
||||
for ( int i2 = i1 - Nhx; i2 <= i1 + Nhx; i2++, ijkh++ )
|
||||
tmp += H[ijkh] * A[i2 + j2 * Nx];
|
||||
}
|
||||
} else {
|
||||
int ijkh = 0;
|
||||
for ( int jh = -Nhy; jh <= Nhy; jh++ ) {
|
||||
int j2 = imfilter_index( j1+jh, Ny, BCy );
|
||||
for ( int ih = -Nhx; ih <= Nhx; ih++ ) {
|
||||
int i2 = imfilter_index( i1+ih, Nx, BCx );
|
||||
bool fixed = i2 == -1 || j2 == -1;
|
||||
TYPE A2 = fixed ? X : A[i2 + j2 * Nx];
|
||||
tmp += H[ijkh] * A2;
|
||||
ijkh++;
|
||||
}
|
||||
}
|
||||
}
|
||||
B[i1 + j1 * Nx] = tmp;
|
||||
}
|
||||
}
|
||||
PROFILE_STOP( "imfilter_2D" );
|
||||
}
|
||||
|
||||
|
||||
// Perform 3-D filtering
|
||||
template<class TYPE>
|
||||
void imfilter_3D( int Nx, int Ny, int Nz, const TYPE *A, int Nhx, int Nhy, int Nhz,
|
||||
const TYPE *H, imfilter::BC BCx, imfilter::BC BCy, imfilter::BC BCz,
|
||||
const TYPE X, TYPE *B )
|
||||
{
|
||||
IMFILTER_ASSERT( A != B );
|
||||
PROFILE_START( "imfilter_3D" );
|
||||
memset( B, 0, Nx * Ny * Nz * sizeof( TYPE ) );
|
||||
for ( int k1 = 0; k1 < Nz; k1++ ) {
|
||||
for ( int j1 = 0; j1 < Ny; j1++ ) {
|
||||
for ( int i1 = 0; i1 < Nx; i1++ ) {
|
||||
TYPE tmp = 0;
|
||||
int ijkh = 0;
|
||||
for ( int kh = -Nhz; kh <= Nhz; kh++ ) {
|
||||
int k2 = imfilter_index( k1+kh, Nz, BCz );
|
||||
for ( int jh = -Nhy; jh <= Nhy; jh++ ) {
|
||||
int j2 = imfilter_index( j1+jh, Ny, BCy );
|
||||
for ( int ih = -Nhx; ih <= Nhx; ih++ ) {
|
||||
int i2 = imfilter_index( i1+ih, Nx, BCx );
|
||||
bool fixed = i2 == -1 || j2 == -1 || k2 == -1;
|
||||
TYPE A2 = fixed ? X : A[i2 + j2 * Nx + k2 * Nx * Ny];
|
||||
tmp += H[ijkh] * A2;
|
||||
ijkh++;
|
||||
}
|
||||
}
|
||||
}
|
||||
B[i1 + j1 * Nx + k1 * Nx * Ny] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
PROFILE_STOP( "imfilter_3D" );
|
||||
}
|
||||
|
||||
|
||||
/********************************************************
|
||||
* Perform N-D filtering *
|
||||
********************************************************/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::imfilter( const Array<TYPE>& A,
|
||||
const Array<TYPE>& H, const std::vector<imfilter::BC>& BC, const TYPE X )
|
||||
{
|
||||
IMFILTER_ASSERT( A.ndim() == H.ndim() );
|
||||
IMFILTER_ASSERT( A.ndim() == BC.size() );
|
||||
std::vector<size_t> Nh = H.size();
|
||||
for (int d=0; d<A.ndim(); d++) {
|
||||
Nh[d] = (H.size(d)-1)/2;
|
||||
IMFILTER_INSIST(2*Nh[d]+1==H.size(d),"Filter must be of size 2*N+1");
|
||||
}
|
||||
auto B = A;
|
||||
if ( A.ndim() == 1 ) {
|
||||
PROFILE_START( "imfilter_1D" );
|
||||
filter_direction( 1, A.size(0), 1, Nh[0], H.data(), BC[0], X, B.data() );
|
||||
PROFILE_STOP( "imfilter_1D" );
|
||||
} else if ( A.ndim() == 2 ) {
|
||||
imfilter_2D( A.size(0), A.size(1), A.data(), Nh[0], Nh[1], H.data(), BC[0], BC[1], X, B.data() );
|
||||
} else if ( A.ndim() == 3 ) {
|
||||
imfilter_3D( A.size(0), A.size(1), A.size(2), A.data(),
|
||||
Nh[0], Nh[1], Nh[2], H.data(), BC[0], BC[1], BC[2], X, B.data() );
|
||||
} else {
|
||||
IMFILTER_ERROR( "Arbitrary dimension not yet supported" );
|
||||
}
|
||||
return B;
|
||||
}
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::imfilter( const Array<TYPE>& A, const std::vector<int>& Nh0,
|
||||
std::function<TYPE(const Array<TYPE>&)> H,
|
||||
const std::vector<imfilter::BC>& BC0, const TYPE X )
|
||||
{
|
||||
PROFILE_START( "imfilter (lambda)" );
|
||||
IMFILTER_ASSERT( A.ndim() == Nh0.size() );
|
||||
IMFILTER_ASSERT( A.ndim() == BC0.size() );
|
||||
std::vector<size_t> Nh2( A.size() );
|
||||
for (int d=0; d<A.ndim(); d++)
|
||||
Nh2[d] = 2*Nh0[d]+1;
|
||||
auto B = A;
|
||||
Array<TYPE> data(Nh2);
|
||||
IMFILTER_INSIST(A.ndim()<=3,"Not programmed for more than 3 dimensions yet");
|
||||
auto N = A.size();
|
||||
auto Nh = Nh0;
|
||||
auto BC = BC0;
|
||||
N.resize(3,1);
|
||||
Nh.resize(3,0);
|
||||
BC.resize(3,imfilter::BC::fixed);
|
||||
for ( int k1 = 0; k1 < N[2]; k1++ ) {
|
||||
for ( int j1 = 0; j1 < N[1]; j1++ ) {
|
||||
for ( int i1 = 0; i1 < N[0]; i1++ ) {
|
||||
for ( int kh = -Nh[2]; kh <= Nh[2]; kh++ ) {
|
||||
int k2 = imfilter_index( k1+kh, N[2], BC[2] );
|
||||
for ( int jh = -Nh[1]; jh <= Nh[1]; jh++ ) {
|
||||
int j2 = imfilter_index( j1+jh, N[1], BC[1] );
|
||||
for ( int ih = -Nh[0]; ih <= Nh[0]; ih++ ) {
|
||||
int i2 = imfilter_index( i1+ih, N[0], BC[0] );
|
||||
bool fixed = i2 == -1 || j2 == -1 || k2 == -1;
|
||||
data(ih+Nh[0],jh+Nh[1],kh+Nh[2]) = fixed ? X : A(i2,j2,k2);
|
||||
}
|
||||
}
|
||||
}
|
||||
B(i1,j1,k1) = H( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
PROFILE_STOP( "imfilter (lambda)" );
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************
|
||||
* imfilter with separable filter functions *
|
||||
********************************************************/
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::imfilter_separable( const Array<TYPE>& A,
|
||||
const std::vector<Array<TYPE>>& H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X )
|
||||
{
|
||||
PROFILE_START( "imfilter_separable" );
|
||||
IMFILTER_ASSERT( A.ndim() == H.size() );
|
||||
IMFILTER_ASSERT( A.ndim() == boundary.size() );
|
||||
std::vector<size_t> Nh( H.size() );
|
||||
for (int d=0; d<A.ndim(); d++) {
|
||||
IMFILTER_ASSERT(H[d].ndim()==1);
|
||||
Nh[d] = (H[d].length()-1)/2;
|
||||
IMFILTER_INSIST(2*Nh[d]+1==H[d].length(),"Filter must be of size 2*N+1");
|
||||
}
|
||||
auto B = A;
|
||||
for ( int d = 0; d < A.ndim(); d++ ) {
|
||||
int N = A.size(d);
|
||||
int Ns = 1;
|
||||
int Ne = 1;
|
||||
for ( int d2 = 0; d2 < d; d2++ )
|
||||
Ns *= A.size(d2);
|
||||
for ( int d2 = d+1; d2 < A.ndim(); d2++ )
|
||||
Ne *= A.size(d2);
|
||||
filter_direction( Ns, N, Ne, Nh[d], H[d].data(), boundary[d], X, B.data() );
|
||||
}
|
||||
PROFILE_STOP( "imfilter_separable" );
|
||||
return B;
|
||||
}
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::imfilter_separable( const Array<TYPE>& A, const std::vector<int>& Nh,
|
||||
std::vector<std::function<TYPE(const Array<TYPE>&)>> H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X )
|
||||
{
|
||||
PROFILE_START( "imfilter_separable (lambda)" );
|
||||
IMFILTER_ASSERT( A.ndim() == boundary.size() );
|
||||
auto B = A;
|
||||
for ( int d = 0; d < A.ndim(); d++ ) {
|
||||
int N = A.size(d);
|
||||
int Ns = 1;
|
||||
int Ne = 1;
|
||||
for ( int d2 = 0; d2 < d; d2++ )
|
||||
Ns *= A.size(d2);
|
||||
for ( int d2 = d+1; d2 < A.ndim(); d2++ )
|
||||
Ne *= A.size(d2);
|
||||
filter_direction( Ns, N, Ne, Nh[d], H[d], boundary[d], X, B.data() );
|
||||
}
|
||||
PROFILE_STOP( "imfilter_separable (lambda)" );
|
||||
return B;
|
||||
}
|
||||
template<class TYPE>
|
||||
Array<TYPE> imfilter::imfilter_separable( const Array<TYPE>& A, const std::vector<int>& Nh,
|
||||
std::vector<std::function<TYPE(int, const TYPE*)>> H,
|
||||
const std::vector<imfilter::BC>& boundary, const TYPE X )
|
||||
{
|
||||
PROFILE_START( "imfilter_separable (function)" );
|
||||
IMFILTER_ASSERT( A.ndim() == boundary.size() );
|
||||
auto B = A;
|
||||
for ( int d = 0; d < A.ndim(); d++ ) {
|
||||
int N = A.size(d);
|
||||
int Ns = 1;
|
||||
int Ne = 1;
|
||||
for ( int d2 = 0; d2 < d; d2++ )
|
||||
Ns *= A.size(d2);
|
||||
for ( int d2 = d+1; d2 < A.ndim(); d2++ )
|
||||
Ne *= A.size(d2);
|
||||
filter_direction( Ns, N, Ne, Nh[d], H[d], boundary[d], X, B.data() );
|
||||
}
|
||||
PROFILE_STOP( "imfilter_separable (function)" );
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <math.h>
|
||||
#include "Array.h"
|
||||
#include "common/Array.h"
|
||||
#include "PointList.h"
|
||||
#include "Utilities.h"
|
||||
#include "common/Utilities.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user