Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions cpp/core/registration/warp/compose.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,36 @@
#include "interp/linear.h"
#include "registration/warp/helpers.h"
#include "transform.h"
#include <mutex>

namespace MR::Registration::Warp {

class MaxNormSquaredFunctor {
public:
explicit MaxNormSquaredFunctor(default_type &overall_max_norm_squared)
: overall_max_norm_squared(overall_max_norm_squared), local_max_norm_squared(0.0) {}
MaxNormSquaredFunctor(const MaxNormSquaredFunctor &) = default;
MaxNormSquaredFunctor(MaxNormSquaredFunctor &&) = default;
MaxNormSquaredFunctor &operator=(const MaxNormSquaredFunctor &) = delete;
MaxNormSquaredFunctor &operator=(MaxNormSquaredFunctor &&) = delete;

~MaxNormSquaredFunctor() {
std::lock_guard<std::mutex> lock(mutex);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'lock' of type 'std::lock_guardstd::mutex' can be declared 'const' [misc-const-correctness]

Suggested change
std::lock_guard<std::mutex> lock(mutex);
std::lock_guard<std::mutex> const lock(mutex);

overall_max_norm_squared = std::max(overall_max_norm_squared, local_max_norm_squared);
}

void operator()(Image<default_type> &update) {
const default_type norm_squared = Eigen::Vector3d(update.row(3)).squaredNorm();
if (norm_squared > local_max_norm_squared)
local_max_norm_squared = norm_squared;
}

private:
default_type &overall_max_norm_squared;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member 'overall_max_norm_squared' of type 'default_type &' (aka 'double &') is a reference [cppcoreguidelines-avoid-const-or-ref-data-members]

  default_type &overall_max_norm_squared;
                ^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we've used this approach throughout the code base, but I think it may have been criticised at some point. Is there something better, even if only for my own knowledge when I need to do similar in the future? My naive instinct would be std::shared_ptr<default_type>, but there's lots in the language I don't know about.

default_type local_max_norm_squared;
inline static std::mutex mutex;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'mutex' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

  inline static std::mutex mutex;
                           ^

};

class ComposeLinearDeformKernel {
public:
ComposeLinearDeformKernel(const transform_type &transform) : transform(transform) {}
Expand Down Expand Up @@ -161,13 +188,9 @@ FORCE_INLINE void update_displacement_scaling_and_squaring(Image<default_type> &
const default_type step = 1.0) {
check_dimensions(input, output, 0, 3);

default_type max_norm = 0.0;
auto max_norm_func = [&max_norm](Image<default_type> &update) {
default_type norm = Eigen::Vector3d(update.row(3)).norm();
if (norm > max_norm)
max_norm = norm;
};
ThreadedLoop(update).run(max_norm_func, update);
default_type max_norm_squared = 0.0;
ThreadedLoop(update).run(MaxNormSquaredFunctor(max_norm_squared), update);
const default_type max_norm = std::sqrt(max_norm_squared);
default_type min_vox_size =
static_cast<default_type>(std::min(input.spacing(0), std::min(input.spacing(1), input.spacing(2))));

Expand Down
Loading