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  if (0 != cmdline_parser_##ggo_filename##_ext(argc, argv, &args_info, &args_params)) \
73  { \
74  std::cerr << "Error in cmdline_parser_" #ggo_filename "_ext" << std::endl; \
75  exit(1); \
76  } \
77  args_params.check_required = 1; \
78  if (0 != cmdline_parser_##ggo_filename##_ext(argc, argv, &args_info, &args_params)) \
79  { \
80  std::cerr << "Error in cmdline_parser_" #ggo_filename "_ext" << std::endl; \
81  exit(1); \
82  } \
83  rtk::args_info_manager<args_info_##ggo_filename> manager_object(args_info, cmdline_parser_##ggo_filename##_free);
84 //--------------------------------------------------------------------
85 
86 //--------------------------------------------------------------------
93 #define TRY_AND_EXIT_ON_ITK_EXCEPTION(execFunc) \
94  try \
95  { \
96  execFunc; \
97  } \
98  catch (itk::ExceptionObject & err) \
99  { \
100  std::cerr << "ExceptionObject caught with " #execFunc << " in file " << __FILE__ << " line " << __LINE__ \
101  << std::endl; \
102  std::cerr << err << std::endl; \
103  itk::InvalidRequestedRegionError * rInv; \
104  rInv = dynamic_cast<itk::InvalidRequestedRegionError *>(&err); \
105  if (rInv) \
106  { \
107  if (rInv->GetDataObject()->GetSource()) \
108  { \
109  std::cerr << "Invalid requested region error triggered by " \
110  << rInv->GetDataObject()->GetSource()->GetNameOfClass() << std::endl; \
111  } \
112  const itk::ImageBase<3> * img = dynamic_cast<const itk::ImageBase<3> *>(rInv->GetDataObject()); \
113  if (img) \
114  { \
115  DD(img->GetRequestedRegion()) \
116  DD(img->GetLargestPossibleRegion()) \
117  } \
118  } \
119  exit(EXIT_FAILURE); \
120  }
121 //--------------------------------------------------------------------
122 
123 //--------------------------------------------------------------------
132 #ifdef RTK_PROBE_EACH_FILTER
133 # undef itkSimpleNewMacro
134 # define itkSimpleNewMacro(x) \
135  static Pointer New(void) \
136  { \
137  Pointer smartPtr = ::itk::ObjectFactory<x>::Create(); \
138  if (smartPtr.GetPointer() == nullptr) \
139  { \
140  smartPtr = new x; \
141  } \
142  smartPtr->UnRegister(); \
143  /* If smartPtr is a ProcessObject, watch it */ \
144  itk::ProcessObject * processObjectPointer = nullptr; \
145  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
146  if (processObjectPointer != nullptr) \
147  { \
148  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
149  } \
150  return smartPtr; \
151  }
152 
153 # undef itkCreateAnotherMacro
154 # define itkCreateAnotherMacro(x) \
155  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
156  { \
157  ::itk::LightObject::Pointer smartPtr; \
158  smartPtr = x::New().GetPointer(); \
159  return smartPtr; \
160  }
161 
162 # undef itkFactorylessNewMacro
163 # define itkFactorylessNewMacro(x) \
164  static Pointer New(void) \
165  { \
166  Pointer smartPtr; \
167  x * rawPtr = new x; \
168  smartPtr = rawPtr; \
169  rawPtr->UnRegister(); \
170  /* If smartPtr is a ProcessObject, watch it */ \
171  itk::ProcessObject * processObjectPointer = nullptr; \
172  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
173  if (processObjectPointer != nullptr) \
174  { \
175  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
176  } \
177  return smartPtr; \
178  } \
179  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
180  { \
181  ::itk::LightObject::Pointer smartPtr; \
182  smartPtr = x::New().GetPointer(); \
183  return smartPtr; \
184  }
185 #endif // RTK_PROBE_EACH_FILTER
186 //--------------------------------------------------------------------
187 
188 //--------------------------------------------------------------------
200 #define REPORT_ITERATIONS(filter, filter_type, output_image_type) \
201  if (args_info.verbose_flag) \
202  { \
203  using VerboseIterationCommandType = rtk::VerboseIterationCommand<filter_type>; \
204  auto verboseIterationCommand = VerboseIterationCommandType::New(); \
205  filter->AddObserver(itk::AnyEvent(), verboseIterationCommand); \
206  } \
207  if (args_info.output_every_given) \
208  { \
209  typedef rtk::OutputIterationCommand<filter_type, output_image_type> OutputIterationCommand; \
210  auto outputIterationCommand = OutputIterationCommand::New(); \
211  outputIterationCommand->SetTriggerEvery(args_info.output_every_arg); \
212  if (args_info.iteration_file_name_given) \
213  { \
214  outputIterationCommand->SetFileFormat(args_info.iteration_file_name_arg); \
215  } \
216  else \
217  { \
218  outputIterationCommand->SetFileFormat("iter%d.mha"); \
219  } \
220  filter->AddObserver(itk::IterationEvent(), outputIterationCommand); \
221  }
222 //--------------------------------------------------------------------
223 
224 #endif