STIR 6.5.0
ArrayFunction.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2000 PARAPET partners
3 Copyright (C) 2000- 2007, Hammersmith Imanet Ltd
4 Copyright (C) 2026, University Medical Center Groningen
5 This file is part of STIR.
6
7 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
8
9 See STIR/LICENSE.txt for details
10*/
45
46/* History:
47
48 KT 21/05/2001
49 added in_place_apply_array_function_on_1st_index,
50 in_place_apply_array_function_on_each_index
51
52 KT 06/12/2001
53 added apply_array_function_on_1st_index,
54 apply_array_function_on_each_index
55*/
56
57#ifndef __stir_ArrayFunction_H__
58#define __stir_ArrayFunction_H__
59
60#include "stir/Array.h"
61#include "stir/shared_ptr.h"
63#include <set>
64
65START_NAMESPACE_STIR
66
67//----------------------------------------------------------------------
68// element wise and in place numeric functions
69//----------------------------------------------------------------------
70
72
73template <class elemT>
74inline Array<1, elemT>& in_place_log(Array<1, elemT>& v);
75
77
78template <int num_dimensions, class elemT>
79inline Array<num_dimensions, elemT>& in_place_log(Array<num_dimensions, elemT>& v);
80
82
83template <class elemT>
84inline Array<1, elemT>& in_place_exp(Array<1, elemT>& v);
85
87
88template <int num_dimensions, class elemT>
89inline Array<num_dimensions, elemT>& in_place_exp(Array<num_dimensions, elemT>& v);
90
92
95template <class elemT>
96inline Array<1, elemT>& in_place_abs(Array<1, elemT>& v);
97
99
102template <int num_dimensions, class elemT>
103inline Array<num_dimensions, elemT>& in_place_abs(Array<num_dimensions, elemT>& v);
104
106
115template <class T, class FUNCTION>
116inline T& in_place_apply_function(T& v, FUNCTION f);
117
119
146template <int num_dim, typename elemT, typename FunctionObjectPtr>
147inline void in_place_apply_array_function_on_1st_index(Array<num_dim, elemT>& array, FunctionObjectPtr f);
148
150
158template <int num_dim, typename elemT, typename FunctionObjectPtr>
159inline void
160apply_array_function_on_1st_index(Array<num_dim, elemT>& out_array, const Array<num_dim, elemT>& in_array, FunctionObjectPtr f);
161
162/* local #define used for 2 purposes:
163 - in partial template specialisation that uses ArrayFunctionObject types
164 - in complete template specialisation for deficient compilers
165
166 This is done with a #define to keep code reasonably clean. Also, it allows
167 adjusting the type according to what you need.
168 Still, it's terribly ugly. Sorry.
169
170 Note that you shouldn't/cannot use this define outside of this include file
171 (and its .inl) partner.
172
173 Ideally, the code should be rewritten to work with any kind of (smart) ptr. TODO
174*/
175#if !defined(__GNUC__) && !defined(_MSC_VER)
176# define ActualFunctionObjectPtrIter VectorWithOffset<shared_ptr<ArrayFunctionObject<1, elemT>>>::const_iterator
177#else
178/*
179 Puzzlingly, although the code is actually called with iterators of the type above,
180 gcc 3.0 (and others?) gets confused and refuses to compile the
181 partial template specialisation (it says it's ambiguous).
182 VC also refuses to compile it.
183 A work-around is to use the following type
184*/
185# define ActualFunctionObjectPtrIter shared_ptr<ArrayFunctionObject<1, elemT>> const*
186#endif
187
189
202// TODO add specialisation that uses ArrayFunctionObject::is_trivial
203template <int num_dim, typename elemT, typename FunctionObjectPtrIter>
204inline void in_place_apply_array_functions_on_each_index(Array<num_dim, elemT>& array,
205 FunctionObjectPtrIter start,
206 FunctionObjectPtrIter stop);
207
209template <typename elemT, typename FunctionObjectPtrIter>
210inline void
211in_place_apply_array_functions_on_each_index(Array<1, elemT>& array, FunctionObjectPtrIter start, FunctionObjectPtrIter stop);
212
214
228template <int num_dim, typename elemT, typename FunctionObjectPtrIter>
229inline void apply_array_functions_on_each_index(Array<num_dim, elemT>& out_array,
230 const Array<num_dim, elemT>& in_array,
231 FunctionObjectPtrIter start,
232 FunctionObjectPtrIter stop);
233
235
242template <int num_dim, typename elemT>
243inline void apply_array_functions_on_each_index(Array<num_dim, elemT>& out_array,
244 const Array<num_dim, elemT>& in_array,
245 ActualFunctionObjectPtrIter start,
246 ActualFunctionObjectPtrIter stop);
247
249
251// has to be here to get general 1D specialisation to compile
252template <typename elemT>
253inline void apply_array_functions_on_each_index(Array<1, elemT>& out_array,
254 const Array<1, elemT>& in_array,
255 ActualFunctionObjectPtrIter start,
256 ActualFunctionObjectPtrIter stop);
257
258template <typename elemT, typename FunctionObjectPtrIter>
260
262inline void apply_array_functions_on_each_index(Array<1, elemT>& out_array,
263 const Array<1, elemT>& in_array,
264 FunctionObjectPtrIter start,
265 FunctionObjectPtrIter stop);
266
267template <int num_dim, typename elemT>
268inline void transform_array_to_periodic_indices(Array<num_dim, elemT>& out_array, const Array<num_dim, elemT>& in_array);
269template <int num_dim, typename elemT>
270inline void transform_array_from_periodic_indices(Array<num_dim, elemT>& out_array, const Array<num_dim, elemT>& in_array);
271
272template <typename InputIt, typename elemT>
273inline void find_unique_values(std::set<elemT>& values, InputIt begin, InputIt end);
274
275// inline bool
276// get_spacing_uniform(std::vector<float>& spacing, const std::set<float>& unsorted_block_poss, double epsilon = 1e-4)
277// {
278// std::vector<float> sorted_z(unsorted_block_poss.begin(), unsorted_block_poss.end());
279// for (size_t i = 1; i < sorted_z.size(); ++i)
280// {
281// spacing.push_back(std::abs(sorted_z[i] - sorted_z[i - 1]));
282// }
283
284// return std::all_of(spacing.begin(), spacing.end(), [&](float s) { return std::abs(s - spacing.front()) <= epsilon; });
285// }
286
287END_NAMESPACE_STIR
288
289#include "stir/ArrayFunction.inl"
290#undef ActualFunctionObjectPtrIter
291
292#endif
Declaration of class stir::ArrayFunctionObject.
Implementations for ArrayFunction.h.
defines the stir::Array class for multi-dimensional (numeric) arrays
Array< 1, elemT > & in_place_abs(Array< 1, elemT > &v)
Replace elements by their absolute value, 1D version.
Definition ArrayFunction.inl:83
void in_place_apply_array_function_on_1st_index(Array< num_dim, elemT > &array, FunctionObjectPtr f)
Apply a function object on all possible 1d arrays extracted by keeping all indices fixed,...
Definition ArrayFunction.inl:116
T & in_place_apply_function(T &v, FUNCTION f)
apply any function(object) to each element of the multi-dimensional array
Definition ArrayFunction.inl:102
void apply_array_function_on_1st_index(Array< num_dim, elemT > &out_array, const Array< num_dim, elemT > &in_array, FunctionObjectPtr f)
apply any function(object) to each element of the multi-dimensional array, storing results in a diffe...
Definition ArrayFunction.inl:153
void in_place_apply_array_functions_on_each_index(Array< num_dim, elemT > &array, FunctionObjectPtrIter start, FunctionObjectPtrIter stop)
Apply a sequence of 1d array-function objects on every dimension of the input array.
Definition ArrayFunction.inl:198
void apply_array_functions_on_each_index(Array< num_dim, elemT > &out_array, const Array< num_dim, elemT > &in_array, FunctionObjectPtrIter start, FunctionObjectPtrIter stop)
Apply a sequence of 1d array-function objects on every dimension of the input array,...
Definition ArrayFunction.inl:221
Array< 1, elemT > & in_place_exp(Array< 1, elemT > &v)
Replace elements by their exponentiation, 1D version.
Definition ArrayFunction.inl:65
Array< 1, elemT > & in_place_log(Array< 1, elemT > &v)
Replace elements by their logarithm, 1D version.
Definition ArrayFunction.inl:47
Import of std::shared_ptr, std::dynamic_pointer_cast and std::static_pointer_cast into the stir names...