Developer documentation
Version 3.0.3-105-gd3941f44
warp.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 __filter_warp_h__
18#define __filter_warp_h__
19
20#include "datatype.h"
21#include "adapter/reslice.h"
22#include "adapter/warp.h"
23#include "algo/threaded_copy.h"
24#include "algo/threaded_loop.h"
25#include "interp/cubic.h"
26#include "filter/reslice.h"
27
28namespace MR
29{
30 namespace Filter
31 {
32
33
34 // TODO if there is a use for this elsewhere then we should have threaded_copy4D convenience functions
36 public:
37 template <class InputImageType, class OutputImageType>
38 FORCE_INLINE void operator() (InputImageType& in, OutputImageType& out) const {
39 out.row(3) = in.row(3);
40 }
41 };
42
43
44
46
64 template <template <class VoxelType> class Interpolator, class ImageTypeDestination, class ImageTypeSource, class WarpType>
65 void warp (
66 ImageTypeSource& source,
67 ImageTypeDestination& destination,
68 WarpType& warp,
69 const typename ImageTypeDestination::value_type value_when_out_of_bounds = Interpolator<ImageTypeSource>::default_out_of_bounds_value(),
71 const bool jacobian_modulate = false )
72 {
73
74 // reslice warp onto destination grid
75 if (warp.transform().matrix() != destination.transform().matrix() ||
76 !dimensions_match (warp, destination, 0, 3) ||
77 !spacings_match (warp, destination, 0, 3)) {
78
79 Header header (destination);
80 header.ndim() = 4;
81 header.size(3) = 3;
82 Stride::set (header, Stride::contiguous_along_axis (3, header));
83 auto warp_resliced = Image<typename WarpType::value_type>::scratch (header);
84 reslice<Interp::Cubic> (warp, warp_resliced, Adapter::NoTransform, oversample);
85
86 Adapter::Warp<Interpolator, ImageTypeSource, Image<typename WarpType::value_type> > interp (source, warp_resliced, value_when_out_of_bounds, jacobian_modulate);
87
88 if (destination.ndim() == 4)
89 ThreadedLoop ("warping \"" + source.name() + "\"" + (jacobian_modulate? " with Jacobian intensity modulation" : ""), interp, 0, 3, 1).run (CopyKernel4D(), interp, destination);
90 else
91 threaded_copy_with_progress_message ("warping \"" + source.name() + "\"" + (jacobian_modulate? " with Jacobian intensity modulation" : ""), interp, destination);
92
93 // no need to reslice warp
94 } else {
95 Adapter::Warp<Interpolator, ImageTypeSource, Image<typename WarpType::value_type> > interp (source, warp, value_when_out_of_bounds, jacobian_modulate);
96 if (destination.ndim() == 4 && destination.is_direct_io())
97 ThreadedLoop ("warping \"" + source.name() + "\"" + (jacobian_modulate? " with Jacobian intensity modulation" : ""), interp, 0, 3, 1).run (CopyKernel4D(), interp, destination);
98 else
99 threaded_copy_with_progress_message ("warping \"" + source.name() + "\"" + (jacobian_modulate? " with Jacobian intensity modulation" : ""), interp, destination, 0, destination.ndim(), 2);
100 }
101 }
102
104 }
105}
106
107#endif
108
109
110
111
an Image providing interpolated values from another Image
Definition: warp.h:54
FORCE_INLINE void operator()(InputImageType &in, OutputImageType &out) const
Definition: warp.h:38
static Image scratch(const Header &template_header, const std::string &label="scratch image")
Definition: image.h:195
#define NOMEMALIGN
Definition: memory.h:22
const vector< uint32_t > AutoOverSample
const transform_type NoTransform
void warp(ImageTypeSource &source, ImageTypeDestination &destination, WarpType &warp, const typename ImageTypeDestination::value_type value_when_out_of_bounds=Interpolator< ImageTypeSource >::default_out_of_bounds_value(), const vector< uint32_t > oversample=Adapter::AutoOverSample, const bool jacobian_modulate=false)
convenience function to warp one image onto another
Definition: warp.h:65
MR::default_type value_type
Definition: typedefs.h:33
List contiguous_along_axis(size_t axis)
convenience function to get volume-contiguous strides
Definition: stride.h:386
void set(HeaderType &header, const List &stride)
set the strides of header from a vector<ssize_t>
Definition: stride.h:135
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.
void threaded_copy_with_progress_message(const std::string &message, InputImageType &source, OutputImageType &destination, const vector< size_t > &axes, size_t num_axes_in_thread=1)
Definition: threaded_copy.h:69
#define FORCE_INLINE
Definition: types.h:156