Skip to content

Commit

Permalink
added move constructors for C++11 compatibility to Filter1D, Filter2D…
Browse files Browse the repository at this point in the history
…, and Filter3D (in "filter1d.hpp", "filter2d.hpp", and "filter3d.hpp").
  • Loading branch information
jewettaij committed Jul 11, 2021
1 parent 22912ca commit 9e666f0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 54 deletions.
34 changes: 22 additions & 12 deletions lib/visfd/filter1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace visfd {




/// @class Filter1D
/// @brief A simple class for general linear (convolutional) filters in 1D

template<typename Scalar, typename Integer>

Expand Down Expand Up @@ -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;
Expand All @@ -334,6 +329,12 @@ class Filter1D {
}


void Init() {
halfwidth = -1;
afH = nullptr;
}


void Resize(Integer set_halfwidth) {
Dealloc();
Alloc(set_halfwidth);
Expand All @@ -351,6 +352,13 @@ class Filter1D {
}


// destructor, copy and move constructor, swap, and assignment operator

~Filter1D() {
Dealloc();
}


Filter1D(const Filter1D<Scalar, Integer>& source) {
Init();
Resize(source.halfwidth); // allocates and initializes afH
Expand All @@ -360,17 +368,19 @@ class Filter1D {
std::copy(source.afH, source.afH + array_size, afH);
}


~Filter1D() {
Dealloc();
}

void swap(Filter1D<Scalar, Integer> &other) {
std::swap(afH, other.afH);
std::swap(halfwidth, other.halfwidth);
std::swap(array_size, other.array_size);
}

// Move constructor (C++11)
Filter1D(Filter1D<Scalar, Integer>&& other) {
Init();
this->swap(other);
}

// Using the "copy-swap" idiom for the assignment operator
Filter1D<Scalar, Integer>&
operator = (Filter1D<Scalar, Integer> source) {
this->swap(source);
Expand Down
39 changes: 26 additions & 13 deletions lib/visfd/filter2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Scalar, typename Integer>

Expand Down Expand Up @@ -271,19 +276,6 @@ class Filter2D {
}


Filter2D(const Filter2D<Scalar, Integer>& 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;
Expand All @@ -305,19 +297,40 @@ class Filter2D {
}


// destructor, copy and move constructor, swap, and assignment operator

~Filter2D() {
Dealloc();
}


Filter2D(const Filter2D<Scalar, Integer>& 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<Scalar, Integer> &other) {
std::swap(afH, other.afH);
std::swap(aafH, other.aafH);
std::swap(halfwidth, other.halfwidth);
std::swap(array_size, other.array_size);
}

// Move constructor (C++11)
Filter2D(Filter2D<Scalar, Integer>&& other) {
Init();
this->swap(other);
}

// Using the "copy-swap" idiom for the assignment operator
Filter2D<Scalar, Integer>&
operator = (Filter2D<Scalar, Integer> source) {
this->swap(source);
Expand Down
72 changes: 43 additions & 29 deletions lib/visfd/filter3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Scalar, typename Integer>

Expand Down Expand Up @@ -194,18 +194,19 @@ class Filter3D {
} // Apply()



Filter3D(const Filter3D<Scalar, Integer>& 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;
}


Expand All @@ -220,40 +221,53 @@ class Filter3D {
}


// destructor, copy and move constructor, swap, and assignment operator

~Filter3D() {
Dealloc();
}


Filter3D(const Filter3D<Scalar, Integer>& 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<Scalar, Integer> &other) {
std::swap(afH, other.afH);
std::swap(aaafH, other.aaafH);
std::swap(halfwidth, other.halfwidth);
std::swap(array_size, other.array_size);
}

// Move constructor (C++11)
Filter3D(Filter3D<Scalar, Integer>&& other) {
Init();
this->swap(other);
}

// Using the "copy-swap" idiom for the assignment operator
Filter3D<Scalar, Integer>&
operator = (Filter3D<Scalar, Integer> source) {
this->swap(source);
return *this;
}


/// @ 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
Expand Down

0 comments on commit 9e666f0

Please sign in to comment.