RTK  2.7.0
Reconstruction Toolkit
rtkMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright RTK Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef rtkMacro_h
20 #define rtkMacro_h
21 
22 #include <iostream>
23 #include <itkMacro.h>
24 #include <itkImageBase.h>
25 #include "rtkGgoArgsInfoManager.h"
26 #include "rtkIterationCommands.h"
27 
28 //--------------------------------------------------------------------
29 #ifndef CLANG_PRAGMA_PUSH
30 # define ITK_PRAGMA(x) _Pragma(#x)
31 # if defined(__clang__) && defined(__has_warning)
32 # define CLANG_PRAGMA_PUSH ITK_PRAGMA(clang diagnostic push)
33 # define CLANG_PRAGMA_POP ITK_PRAGMA(clang diagnostic pop)
34 # if __has_warning("-Wfloat-equal")
35 # define CLANG_SUPPRESS_Wfloat_equal ITK_PRAGMA(clang diagnostic ignored "-Wfloat-equal")
36 # endif
37 # else
38 # define CLANG_PRAGMA_PUSH
39 # define CLANG_PRAGMA_POP
40 # define CLANG_SUPPRESS_Wfloat_equal
41 # endif
42 #endif
43 //--------------------------------------------------------------------
44 
45 //--------------------------------------------------------------------
52 #ifndef DD
53 # define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
54 #endif
55 //--------------------------------------------------------------------
56 
57 //--------------------------------------------------------------------
64 #define GGO(ggo_filename, args_info) \
65  args_info_##ggo_filename args_info; \
66  cmdline_parser_##ggo_filename##_params args_params; \
67  cmdline_parser_##ggo_filename##_params_init(&args_params); \
68  args_params.print_errors = 1; \
69  args_params.check_required = 0; \
70  args_params.override = 1; \
71  args_params.initialize = 1; \
72  args_params.check_required = 1; \
73  if (0 != cmdline_parser_##ggo_filename##_ext(argc, argv, &args_info, &args_params)) \
74  { \
75  std::cerr << "Error in cmdline_parser_" #ggo_filename "_ext" << std::endl; \
76  exit(1); \
77  } \
78  rtk::args_info_manager<args_info_##ggo_filename> manager_object(args_info, cmdline_parser_##ggo_filename##_free);
79 //--------------------------------------------------------------------
80 
81 //--------------------------------------------------------------------
88 #define TRY_AND_EXIT_ON_ITK_EXCEPTION(execFunc) \
89  try \
90  { \
91  execFunc; \
92  } \
93  catch (itk::ExceptionObject & err) \
94  { \
95  std::cerr << "ExceptionObject caught with " #execFunc << " in file " << __FILE__ << " line " << __LINE__ \
96  << std::endl; \
97  std::cerr << err << std::endl; \
98  itk::InvalidRequestedRegionError * rInv; \
99  rInv = dynamic_cast<itk::InvalidRequestedRegionError *>(&err); \
100  if (rInv) \
101  { \
102  if (rInv->GetDataObject()->GetSource()) \
103  { \
104  std::cerr << "Invalid requested region error triggered by " \
105  << rInv->GetDataObject()->GetSource()->GetNameOfClass() << std::endl; \
106  } \
107  const itk::ImageBase<3> * img = dynamic_cast<const itk::ImageBase<3> *>(rInv->GetDataObject()); \
108  if (img) \
109  { \
110  DD(img->GetRequestedRegion()) \
111  DD(img->GetLargestPossibleRegion()) \
112  } \
113  } \
114  exit(EXIT_FAILURE); \
115  }
116 //--------------------------------------------------------------------
117 
118 //--------------------------------------------------------------------
127 #ifdef RTK_PROBE_EACH_FILTER
128 # undef itkSimpleNewMacro
129 # define itkSimpleNewMacro(x) \
130  static Pointer New(void) \
131  { \
132  Pointer smartPtr = ::itk::ObjectFactory<x>::Create(); \
133  if (smartPtr.GetPointer() == nullptr) \
134  { \
135  smartPtr = new x; \
136  } \
137  smartPtr->UnRegister(); \
138  /* If smartPtr is a ProcessObject, watch it */ \
139  itk::ProcessObject * processObjectPointer = nullptr; \
140  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
141  if (processObjectPointer != nullptr) \
142  { \
143  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
144  } \
145  return smartPtr; \
146  }
147 
148 # undef itkCreateAnotherMacro
149 # define itkCreateAnotherMacro(x) \
150  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
151  { \
152  ::itk::LightObject::Pointer smartPtr; \
153  smartPtr = x::New().GetPointer(); \
154  return smartPtr; \
155  }
156 
157 # undef itkFactorylessNewMacro
158 # define itkFactorylessNewMacro(x) \
159  static Pointer New(void) \
160  { \
161  Pointer smartPtr; \
162  x * rawPtr = new x; \
163  smartPtr = rawPtr; \
164  rawPtr->UnRegister(); \
165  /* If smartPtr is a ProcessObject, watch it */ \
166  itk::ProcessObject * processObjectPointer = nullptr; \
167  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
168  if (processObjectPointer != nullptr) \
169  { \
170  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
171  } \
172  return smartPtr; \
173  } \
174  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
175  { \
176  ::itk::LightObject::Pointer smartPtr; \
177  smartPtr = x::New().GetPointer(); \
178  return smartPtr; \
179  }
180 #endif // RTK_PROBE_EACH_FILTER
181 //--------------------------------------------------------------------
182 
183 //--------------------------------------------------------------------
195 #define REPORT_ITERATIONS(filter, filter_type, output_image_type) \
196  if (args_info.verbose_flag) \
197  { \
198  using VerboseIterationCommandType = rtk::VerboseIterationCommand<filter_type>; \
199  auto verboseIterationCommand = VerboseIterationCommandType::New(); \
200  filter->AddObserver(itk::AnyEvent(), verboseIterationCommand); \
201  } \
202  if (args_info.output_every_given) \
203  { \
204  typedef rtk::OutputIterationCommand<filter_type, output_image_type> OutputIterationCommand; \
205  auto outputIterationCommand = OutputIterationCommand::New(); \
206  outputIterationCommand->SetTriggerEvery(args_info.output_every_arg); \
207  if (args_info.iteration_file_name_given) \
208  { \
209  outputIterationCommand->SetFileFormat(args_info.iteration_file_name_arg); \
210  } \
211  else \
212  { \
213  outputIterationCommand->SetFileFormat("iter%d.mha"); \
214  } \
215  filter->AddObserver(itk::IterationEvent(), outputIterationCommand); \
216  }
217 //--------------------------------------------------------------------
218 
219 #endif