SMILX  1.01
milxGlobal.h
1 /*=========================================================================
2  The Software is copyright (c) Commonwealth Scientific and Industrial Research Organisation (CSIRO)
3  ABN 41 687 119 230.
4  All rights reserved.
5 
6  Licensed under the CSIRO BSD 3-Clause License
7  You may not use this file except in compliance with the License.
8  You may obtain a copy of the License in the file LICENSE.md or at
9 
10  https://stash.csiro.au/projects/SMILI/repos/smili/browse/license.txt
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 =========================================================================*/
18 #ifndef __MILXGLOBAL_H
19 #define __MILXGLOBAL_H
20 
21 //STL
22 #include <limits>
23 #include <stdexcept> //Include stdexcept to handle conversion exceptions
24 #include <sstream> //includes string
25 #include <iomanip>
26 #include <iostream>
27 
28 #ifndef VTK_ONLY
29  //VNL
30  #include <vnl/vnl_vector_fixed.h>
31  //ITK
32  #include <itkCommand.h>
33 #endif
34 #ifndef ITK_ONLY
35  //VTK
36  #include <vtkType.h>
37  #include <vtkVersion.h>
38  #include <vtkSmartPointer.h>
39  #include <vtkCommand.h>
40  #include <vtkObject.h>
41 #endif
42 
43 #ifdef WIN32
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #else
47 #include <unistd.h>
48 #endif
49 
50 namespace milx
51 {
52 
58 //Global Constants
59 const float Version = static_cast<float>(1.0);
60 static bool VerboseMode = true;
61 
62 //Global Typedefs
63 typedef double coordinateType; //compatible with >= VTK 5
64 #ifndef VTK_ONLY
65  typedef unsigned vnl_size_t;
66  typedef vnl_vector_fixed<coordinateType,3> coordinate;
67 #endif
68 #ifdef VTK_ONLY
69  typedef coordinateType* coordinate;
70 #endif
71 const unsigned imgDimension = 3;
72 
73 //C++ Helper Macros
74 // Swap two values
75 template <typename T>
76 inline void Swap(T &a, T&b) {
77  T t=a;
78  a=b;
79  b=t;
80 }
81 
82 // Calculate the minimum of two values
83 template <typename T>
84 inline T Minimum(const T &a, const T &b) {
85  return (a < b) ? a : b;
86 }
87 
88 // Calculate the maximum of two values
89 template <typename T>
90 inline T Maximum(const T &a, const T &b) {
91  return (a > b) ? a : b;
92 }
93 
94 //Global Functions
100 class BadStringConversion : public std::runtime_error
101 {
102 public:
103  BadStringConversion(const std::string& s)
104  : runtime_error(s)
105  { }
106 };
107 
112 inline std::string NumberToString(double num, unsigned zeroPad = 0)
113 {
114  std::ostringstream toString;
115  if (!(toString << std::setw(zeroPad) << std::setfill('0') << num))
116  throw BadStringConversion("NumberToString(double)");
117  return toString.str();
118 }
119 
124 inline unsigned NumberOfProcessors()
125 {
126  unsigned cores = 1;
127 
128 #ifdef _WIN32
129  SYSTEM_INFO sysinfo;
130  GetSystemInfo( &sysinfo );
131 
132  cores = sysinfo.dwNumberOfProcessors;
133 #else
134  cores = sysconf( _SC_NPROCESSORS_ONLN );
135 #endif
136 
137  return cores;
138 }
139 
149 inline void PrintProgressBar( const size_t percent )
150 {
151  std::string bar;
152 
153  for (size_t i = 0; i < 50; i++)
154  {
155  if ( i < (percent/2))
156  bar.replace(i,1,"=");
157  else if ( i == (percent/2))
158  bar.replace(i,1,">");
159  else
160  bar.replace(i,1," ");
161  }
162 
163  std::cout<< "\r" "[" << bar << "] ";
164  std::cout.width( 3 );
165  std::cout<< percent << "% " << std::flush;
166  if (percent == 100)
167  std::cout << std::endl;
168 }
169 
174 inline void PrintInfo(const std::string msg)
175 {
176  std::cout << msg << std::endl;
177 }
178 
183 inline void PrintWarning(const std::string msg)
184 {
185  std::cout << "Warning: " << msg << std::endl;
186 }
187 
192 inline void PrintDebug(const std::string msg)
193 {
194  if(VerboseMode)
195  std::cerr << "Debug: " << msg << std::endl;
196 }
197 
202 inline void PrintError(const std::string msg)
203 {
204  std::cerr << msg << std::endl;
205 }
206 
207 #if defined(WIN32)
208 #if defined(SMILI_DLL)
209 
214 #if defined(SMILI_MAKEDLL) // create a SMILI DLL library
215 #define SMILI_EXPORT __declspec(dllexport)
216 #else // use a SMILI DLL library
217 #define SMILI_EXPORT __declspec(dllimport)
218 #endif
219 #endif // SMILI_DLL
220 #endif // WIN32
221 
222 #ifndef SMILI_EXPORT
223 
227 #define SMILI_EXPORT
228 #endif
229 
230 }
231 
239 #ifndef ITK_ONLY
240  #ifndef VTK_ONLY //Requires VTK and ITK
241  namespace itk
242  {
257  class SMILI_EXPORT ProgressUpdates : public Command
258  {
259  public:
265  {
266  return new ProgressUpdates();
267  }
268  itkTypeMacro(ProgressUpdates, Command);
269 
270  inline vtkObject* GetUpdateObject()
271  { return Filter.GetPointer(); }
272 
273  protected:
275  { Filter = vtkSmartPointer<vtkObject>::New(); }
276  ~ProgressUpdates() {}
277 
278  vtkSmartPointer<vtkObject> Filter;
279 
280  private:
281  inline void Execute(Object *caller, const EventObject & event)
282  { Execute( (const Object *)caller, event); }
283 
284  inline void Execute(const Object * object, const EventObject & event)
285  { Filter->InvokeEvent(vtkCommand::ProgressEvent); } //pass on event using the dummy object
286  };
287  }
288 
289  namespace milx
290  {
291  static itk::SmartPointer<itk::ProgressUpdates> ProgressUpdates = itk::ProgressUpdates::New();
292  }
293  #endif
294 #endif
295 
296 #ifndef ITK_ONLY //Requires VTK
297 
311  class SMILI_EXPORT vtkProgressUpdates : public vtkCommand
312  {
313  public:
319  {
320  return new vtkProgressUpdates();
321  }
322  vtkTypeMacro(vtkProgressUpdates, vtkCommand);
323 
324  inline vtkObject* GetUpdateObject()
325  { return Filter.GetPointer(); }
326 
327  inline bool IsBeingObserved(unsigned long event = vtkCommand::ProgressEvent)
328  { return Filter->HasObserver(event); }
329 
330  protected:
332  { Filter = vtkSmartPointer<vtkObject>::New(); }
333  ~vtkProgressUpdates() {}
334 
335  vtkSmartPointer<vtkObject> Filter;
336 
337  private:
338  inline void Execute(vtkObject *caller, unsigned long observedType, void* message)
339  { Filter->InvokeEvent(vtkCommand::ProgressEvent); } //pass on event using the dummy object
340  };
341 
342  namespace milx
343  {
344  static vtkSmartPointer<vtkProgressUpdates> VTKProgressUpdates = vtkSmartPointer<vtkProgressUpdates>::New();
345  }
346 
351  class SMILI_EXPORT vtkErrorWarning : public vtkCommand
352  {
353  public:
359  { return new vtkErrorWarning(); }
360 
361  vtkTypeMacro(vtkErrorWarning, vtkCommand);
362 
363  inline const char* GetMessage()
364  { return m_Message.c_str(); }
365 
366  inline bool HasFailed()
367  { return m_ErrorEncountered; }
368 
369  inline bool ReportsFailure()
370  { return m_ErrorEncountered; }
371 
372  inline void Initialize()
373  { m_ErrorEncountered = m_WarningEncountered = false; }
374 
375  protected:
377  { Initialize(); }
378  ~vtkErrorWarning() {}
379 
380  bool m_ErrorEncountered;
381  bool m_WarningEncountered;
382  std::string m_Message;
383 
384  private:
385  void Execute(vtkObject *caller, unsigned long observedType, void* message)
386  {
387  m_Message = (const char*) message;
388 
389  if(observedType == vtkCommand::ErrorEvent)
390  m_ErrorEncountered = true;
391  if(observedType == vtkCommand::WarningEvent)
392  m_WarningEncountered = true;
393  }
394  };
395 #endif
396 
397 
398 #endif //__MILXGLOBAL_H
void PrintDebug(const std::string msg)
Displays a generic msg to standard error with carriage return if in Debug mode.
Definition: milxGlobal.h:192
Object for intercepting progress events triggered by ITK based filters and converting them to VTK pro...
Definition: milxGlobal.h:257
Object for intercepting progress events triggered by VTK based filters.
Definition: milxGlobal.h:311
void PrintProgressBar(const size_t percent)
Displays a generic terminal-based progress bar based on the percent provided.
Definition: milxGlobal.h:149
Exception Handler for stringify function. Thrown if the conversion from numeric to string is unsucces...
Definition: milxGlobal.h:100
static vtkErrorWarning * New()
Standard VTK Object Factory.
Definition: milxGlobal.h:358
void PrintError(const std::string msg)
Displays a generic msg to standard error with carriage return.
Definition: milxGlobal.h:202
Object for intercepting errors thrown by VTK based readers.
Definition: milxGlobal.h:351
vtkSmartPointer< vtkObject > Filter
Dummy object for passing on events.
Definition: milxGlobal.h:278
#define SMILI_EXPORT
DLL Function Symbol for Windows. It is empty for other OSes.
Definition: milxGlobal.h:227
void PrintWarning(const std::string msg)
Displays a generic msg to standard output with carriage return.
Definition: milxGlobal.h:183
static ProgressUpdates * New()
Standard ITK Object Factory.
Definition: milxGlobal.h:264
void PrintInfo(const std::string msg)
Displays a generic msg to standard output with carriage return.
Definition: milxGlobal.h:174
std::string NumberToString(double num, unsigned zeroPad=0)
Number to string converter.
Definition: milxGlobal.h:112
vtkSmartPointer< vtkObject > Filter
Dummy object for passing on events.
Definition: milxGlobal.h:335
static vtkProgressUpdates * New()
Standard VTK Object Factory.
Definition: milxGlobal.h:318
unsigned NumberOfProcessors()
Number of processors or cores on the machine.
Definition: milxGlobal.h:124