diff --git a/lib/visfd/filter1d.hpp b/lib/visfd/filter1d.hpp index 491e12b..742cb71 100644 --- a/lib/visfd/filter1d.hpp +++ b/lib/visfd/filter1d.hpp @@ -18,7 +18,8 @@ namespace visfd { - +/// @class Filter1D +/// @brief A simple class for general linear (convolutional) filters in 1D template @@ -305,12 +306,6 @@ class Filter1D { } - void Init() { - halfwidth = -1; - afH = nullptr; - } - - void Alloc(Integer set_halfwidth) { halfwidth = set_halfwidth; array_size = 1 + 2*halfwidth; @@ -334,6 +329,12 @@ class Filter1D { } + void Init() { + halfwidth = -1; + afH = nullptr; + } + + void Resize(Integer set_halfwidth) { Dealloc(); Alloc(set_halfwidth); @@ -351,6 +352,13 @@ class Filter1D { } + // destructor, copy and move constructor, swap, and assignment operator + + ~Filter1D() { + Dealloc(); + } + + Filter1D(const Filter1D& source) { Init(); Resize(source.halfwidth); // allocates and initializes afH @@ -360,17 +368,19 @@ class Filter1D { std::copy(source.afH, source.afH + array_size, afH); } - - ~Filter1D() { - Dealloc(); - } - void swap(Filter1D &other) { std::swap(afH, other.afH); std::swap(halfwidth, other.halfwidth); std::swap(array_size, other.array_size); } + // Move constructor (C++11) + Filter1D(Filter1D&& other) { + Init(); + this->swap(other); + } + + // Using the "copy-swap" idiom for the assignment operator Filter1D& operator = (Filter1D source) { this->swap(source); diff --git a/lib/visfd/filter2d.hpp b/lib/visfd/filter2d.hpp index 01d1269..f117442 100644 --- a/lib/visfd/filter2d.hpp +++ b/lib/visfd/filter2d.hpp @@ -19,6 +19,11 @@ using namespace std; namespace visfd { +/// @class Filter2D +/// @brief A simple class for general linear (convolutional) filters in 2D +/// +/// @note In practice, this class is not used often because most operations +/// we need can be performed with separable filters which are faster. template @@ -271,19 +276,6 @@ class Filter2D { } - Filter2D(const Filter2D& source) { - Init(); - Resize(source.halfwidth); // allocates and initializes afH and aafH - //for(Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) - // for(Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) - // aafH[iy][ix] = source.aafH[iy][ix]; - // -- Use std:copy() instead: -- - std::copy(source.afH, - source.afH + (array_size[0] * array_size[1]), - afH); - } - - void Init() { halfwidth[0] = -1; halfwidth[1] = -1; @@ -305,11 +297,26 @@ class Filter2D { } + // destructor, copy and move constructor, swap, and assignment operator + ~Filter2D() { Dealloc(); } + Filter2D(const Filter2D& source) { + Init(); + Resize(source.halfwidth); // allocates and initializes afH and aafH + //for(Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) + // for(Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) + // aafH[iy][ix] = source.aafH[iy][ix]; + // -- Use std:copy() instead: -- + std::copy(source.afH, + source.afH + (array_size[0] * array_size[1]), + afH); + } + + void swap(Filter2D &other) { std::swap(afH, other.afH); std::swap(aafH, other.aafH); @@ -317,7 +324,13 @@ class Filter2D { std::swap(array_size, other.array_size); } + // Move constructor (C++11) + Filter2D(Filter2D&& other) { + Init(); + this->swap(other); + } + // Using the "copy-swap" idiom for the assignment operator Filter2D& operator = (Filter2D source) { this->swap(source); diff --git a/lib/visfd/filter3d.hpp b/lib/visfd/filter3d.hpp index 090375e..d3355a3 100644 --- a/lib/visfd/filter3d.hpp +++ b/lib/visfd/filter3d.hpp @@ -23,10 +23,10 @@ namespace visfd { /// @class Filter3D -/// @brief A simple class for general linear (convolutional) filters in 3D +/// @brief A class for general linear (convolutional) filters in 3D /// -/// @note In practice, this class is not used often because separable filters -/// based on Gaussians are much faster. +/// @note In practice, this class is not used often because most operations +/// we need can be performed with separable filters which are faster. template @@ -194,18 +194,19 @@ class Filter3D { } // Apply() - - Filter3D(const Filter3D& source) { - Init(); - Resize(source.halfwidth); // allocates and initializes afH and aaafH - //for(Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) - // for(Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) - // for(Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) - // aaafH[iz][iy][ix] = source.aaafH[iz][iy][ix]; - // -- Use std:copy() instead: -- - std::copy(source.afH, - source.afH + (array_size[0] * array_size[1] * array_size[2]), - afH); + /// @ brief Make sure the sum of the filter weights (in aaafH) is 1 + void Normalize() { + Scalar total = 0.0; + for (Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) + for (Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) + for (Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) + total += aaafH[iz][iy][ix]; + + assert(total > 0.0); + for (Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) + for (Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) + for (Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) + aaafH[iz][iy][ix] /= total; } @@ -220,11 +221,27 @@ class Filter3D { } + // destructor, copy and move constructor, swap, and assignment operator + ~Filter3D() { Dealloc(); } + Filter3D(const Filter3D& source) { + Init(); + Resize(source.halfwidth); // allocates and initializes afH and aaafH + //for(Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) + // for(Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) + // for(Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) + // aaafH[iz][iy][ix] = source.aaafH[iz][iy][ix]; + // -- Use std:copy() instead: -- + std::copy(source.afH, + source.afH + (array_size[0] * array_size[1] * array_size[2]), + afH); + } + + void swap(Filter3D &other) { std::swap(afH, other.afH); std::swap(aaafH, other.aaafH); @@ -232,7 +249,13 @@ class Filter3D { std::swap(array_size, other.array_size); } + // Move constructor (C++11) + Filter3D(Filter3D&& other) { + Init(); + this->swap(other); + } + // Using the "copy-swap" idiom for the assignment operator Filter3D& operator = (Filter3D source) { this->swap(source); @@ -240,20 +263,11 @@ class Filter3D { } - /// @ brief Make sure the sum of the filter weights (in aaafH) is 1 - void Normalize() { - Scalar total = 0.0; - for (Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) - for (Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) - for (Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) - total += aaafH[iz][iy][ix]; - - assert(total > 0.0); - for (Integer iz=-halfwidth[2]; iz<=halfwidth[2]; iz++) - for (Integer iy=-halfwidth[1]; iy<=halfwidth[1]; iy++) - for (Integer ix=-halfwidth[0]; ix<=halfwidth[0]; ix++) - aaafH[iz][iy][ix] /= total; - } + + // Miscellaneous functions + // (COMMENT: Most of these functions are no longer needed. + // I may remove them in the future. -A 2021-7-11) + /// @brief Calculate the (weighted) average value of the filter array aaafH /// @param aaafW optional weights used when calculating the averages