Developer documentation
Version 3.0.3-105-gd3941f44
min_max.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __image_min_max_h__
18#define __image_min_max_h__
19
20#include "algo/threaded_loop.h"
21
22namespace MR
23{
24
26 namespace {
27 template <class ImageType>
28 class __MinMax { NOMEMALIGN
29 public:
30 using value_type = typename ImageType::value_type;
31
32 __MinMax (value_type& overall_min, value_type& overall_max) :
33 overall_min (overall_min), overall_max (overall_max),
34 min (std::numeric_limits<value_type>::infinity()),
35 max (-std::numeric_limits<value_type>::infinity()) {
36 overall_min = min;
37 overall_max = max;
38 }
39 ~__MinMax () {
40 std::lock_guard<std::mutex> lock (mutex);
41 overall_min = std::min (overall_min, min);
42 overall_max = std::max (overall_max, max);
43 }
44
45 void operator() (ImageType& vox) {
46 value_type val = vox.value();
47 if (std::isfinite (val)) {
48 if (val < min) min = val;
49 if (val > max) max = val;
50 }
51 }
52
53 template <class MaskType>
54 void operator() (ImageType& vox, MaskType& mask) {
55 if (mask.value()) {
56 value_type val = vox.value();
57 if (std::isfinite (val)) {
58 if (val < min) min = val;
59 if (val > max) max = val;
60 }
61 }
62 }
63
64 value_type& overall_min;
65 value_type& overall_max;
66 value_type min, max;
67
68 static std::mutex mutex;
69 };
70 template <class ImageType> std::mutex __MinMax<ImageType>::mutex;
71 }
73
74 template <class ImageType>
75 inline void min_max (
76 ImageType& in,
77 typename ImageType::value_type& min,
78 typename ImageType::value_type& max,
79 size_t from_axis = 0,
80 size_t to_axis = std::numeric_limits<size_t>::max())
81 {
82 ThreadedLoop ("finding min/max of \"" + shorten (in.name()) + "\"", in, from_axis, to_axis)
83 .run (__MinMax<ImageType> (min, max), in);
84 }
85
86 template <class ImageType, class MaskType>
87 inline void min_max (
88 ImageType& in,
89 MaskType& mask,
90 typename ImageType::value_type& min,
91 typename ImageType::value_type& max,
92 size_t from_axis = 0,
93 size_t to_axis = std::numeric_limits<size_t>::max())
94 {
95 ThreadedLoop ("finding min/max of \"" + shorten (in.name()) + "\"", in, from_axis, to_axis)
96 .run (__MinMax<ImageType> (min, max), in, mask);
97 }
98
99}
100
101#endif
#define NOMEMALIGN
Definition: memory.h:22
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
ThreadedLoopRunOuter< decltype(Loop(vector< size_t >()))> ThreadedLoop(const HeaderType &source, const vector< size_t > &outer_axes, const vector< size_t > &inner_axes)
Multi-threaded loop object.
std::string shorten(const std::string &text, size_t longest=40, size_t prefix=10)
convert a long string to 'beginningofstring...endofstring' for display
Definition: mrtrix.h:88
void min_max(ImageType &in, typename ImageType::value_type &min, typename ImageType::value_type &max, size_t from_axis=0, size_t to_axis=std::numeric_limits< size_t >::max())
Definition: min_max.h:75
Definition: types.h:303