RTK  2.6.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  std::string configFile; \
78  if (args_info.config_given) \
79  configFile = args_info.config_arg; \
80  cmdline_parser_##ggo_filename##_free(&args_info); \
81  if (configFile != "") \
82  { \
83  if (0 != cmdline_parser_##ggo_filename##_config_file(configFile.c_str(), &args_info, &args_params)) \
84  { \
85  std::cerr << "Error in cmdline_parser_" #ggo_filename "_config_file" << std::endl; \
86  exit(1); \
87  } \
88  args_params.initialize = 0; \
89  } \
90  args_params.check_required = 1; \
91  if (0 != cmdline_parser_##ggo_filename##_ext(argc, argv, &args_info, &args_params)) \
92  { \
93  std::cerr << "Error in cmdline_parser_" #ggo_filename "_ext" << std::endl; \
94  exit(1); \
95  } \
96  rtk::args_info_manager<args_info_##ggo_filename> manager_object(args_info, cmdline_parser_##ggo_filename##_free);
97 //--------------------------------------------------------------------
98 
99 //--------------------------------------------------------------------
106 #define TRY_AND_EXIT_ON_ITK_EXCEPTION(execFunc) \
107  try \
108  { \
109  execFunc; \
110  } \
111  catch (itk::ExceptionObject & err) \
112  { \
113  std::cerr << "ExceptionObject caught with " #execFunc << " in file " << __FILE__ << " line " << __LINE__ \
114  << std::endl; \
115  std::cerr << err << std::endl; \
116  itk::InvalidRequestedRegionError * rInv; \
117  rInv = dynamic_cast<itk::InvalidRequestedRegionError *>(&err); \
118  if (rInv) \
119  { \
120  if (rInv->GetDataObject()->GetSource()) \
121  { \
122  std::cerr << "Invalid requested region error triggered by " \
123  << rInv->GetDataObject()->GetSource()->GetNameOfClass() << std::endl; \
124  } \
125  const itk::ImageBase<3> * img = dynamic_cast<const itk::ImageBase<3> *>(rInv->GetDataObject()); \
126  if (img) \
127  { \
128  DD(img->GetRequestedRegion()) \
129  DD(img->GetLargestPossibleRegion()) \
130  } \
131  } \
132  exit(EXIT_FAILURE); \
133  }
134 //--------------------------------------------------------------------
135 
136 //--------------------------------------------------------------------
145 #ifdef RTK_PROBE_EACH_FILTER
146 # undef itkSimpleNewMacro
147 # define itkSimpleNewMacro(x) \
148  static Pointer New(void) \
149  { \
150  Pointer smartPtr = ::itk::ObjectFactory<x>::Create(); \
151  if (smartPtr.GetPointer() == nullptr) \
152  { \
153  smartPtr = new x; \
154  } \
155  smartPtr->UnRegister(); \
156  /* If smartPtr is a ProcessObject, watch it */ \
157  itk::ProcessObject * processObjectPointer = nullptr; \
158  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
159  if (processObjectPointer != nullptr) \
160  { \
161  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
162  } \
163  return smartPtr; \
164  }
165 
166 # undef itkCreateAnotherMacro
167 # define itkCreateAnotherMacro(x) \
168  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
169  { \
170  ::itk::LightObject::Pointer smartPtr; \
171  smartPtr = x::New().GetPointer(); \
172  return smartPtr; \
173  }
174 
175 # undef itkFactorylessNewMacro
176 # define itkFactorylessNewMacro(x) \
177  static Pointer New(void) \
178  { \
179  Pointer smartPtr; \
180  x * rawPtr = new x; \
181  smartPtr = rawPtr; \
182  rawPtr->UnRegister(); \
183  /* If smartPtr is a ProcessObject, watch it */ \
184  itk::ProcessObject * processObjectPointer = nullptr; \
185  processObjectPointer = dynamic_cast<itk::ProcessObject *>(smartPtr.GetPointer()); \
186  if (processObjectPointer != nullptr) \
187  { \
188  rtk::GlobalResourceProbe::GetInstance()->Watch(processObjectPointer); \
189  } \
190  return smartPtr; \
191  } \
192  virtual ::itk::LightObject::Pointer CreateAnother(void) const override \
193  { \
194  ::itk::LightObject::Pointer smartPtr; \
195  smartPtr = x::New().GetPointer(); \
196  return smartPtr; \
197  }
198 #endif // RTK_PROBE_EACH_FILTER
199 //--------------------------------------------------------------------
200 
201 //--------------------------------------------------------------------
213 #define REPORT_ITERATIONS(filter, filter_type, output_image_type) \
214  if (args_info.verbose_flag) \
215  { \
216  using VerboseIterationCommandType = rtk::VerboseIterationCommand<filter_type>; \
217  typename VerboseIterationCommandType::Pointer verboseIterationCommand = VerboseIterationCommandType::New(); \
218  filter->AddObserver(itk::AnyEvent(), verboseIterationCommand); \
219  } \
220  if (args_info.output_every_given) \
221  { \
222  typedef rtk::OutputIterationCommand<filter_type, output_image_type> OutputIterationCommand; \
223  typename OutputIterationCommand::Pointer outputIterationCommand = OutputIterationCommand::New(); \
224  outputIterationCommand->SetTriggerEvery(args_info.output_every_arg); \
225  if (args_info.iteration_file_name_given) \
226  { \
227  outputIterationCommand->SetFileFormat(args_info.iteration_file_name_arg); \
228  } \
229  else \
230  { \
231  outputIterationCommand->SetFileFormat("iter%d.mha"); \
232  } \
233  filter->AddObserver(itk::IterationEvent(), outputIterationCommand); \
234  }
235 //--------------------------------------------------------------------
236 
237 #endif