DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkMaskedImageToImageFilter.hxx
Go to the documentation of this file.
1 
19 #ifndef __itkMaskedImageToImageFilter_hxx
20 #define __itkMaskedImageToImageFilter_hxx
21 
23 #include "itkStdStreamLogOutput.h"
24 #include "itkProgressReporter.h"
25 
26 #include "utl.h"
27 
28 namespace itk
29 {
30 template< class TInputImage, class TOutputImage, class TMaskImage >
33 {
34  // Modify superclass default values, can be overridden by subclasses
35  this->SetNumberOfRequiredInputs(1);
36  m_MaskImage = NULL;
37  m_ThreadID=-1;
38 
39  m_LogLevel= LOG_NORMAL;
40 
41  // NOTE: m_Logger makes the estimation a little bit slower once it is newed, even though it is not used.
42  // m_Logger = itk::ThreadLogger::New();
43  // m_LoggerVector= LoggerVectorPointer(new LoggerVectorType())
44 }
45 
46 template< class TInputImage, class TOutputImage, class TMaskImage >
49 {}
50 
51 template< class TInputImage, class TOutputImage, class TMaskImage >
52 void
54 ::SetMaskImage(const std::string& file)
55 {
56  MaskImagePointer mask = MaskImageType::New();
57  itk::ReadImage<MaskImageType>(file, mask);
58  this->SetMaskImage(mask);
59 }
60 
61 template< class TInputImage, class TOutputImage, class TMaskImage >
62 void
65 {
66  utlGlobalException(!m_MaskImage, "no m_MaskImage");
67  InputImagePointer input = const_cast<InputImageType *>(this->GetInput());
68  utlGlobalException(!input, "no input");
69  utlGlobalException((!itk::VerifyImageInformation<InputImageType, MaskImageType>(input, m_MaskImage, true)), "wrong mask information");
70  // utlGlobalException((!itk::VerifyImageSize<InputImageType, MaskImageType>(input, m_MaskImage, true)), "wrong mask information");
71 }
72 
73 template< class TInputImage, class TOutputImage, class TMaskImage >
74 typename LightObject::Pointer
77 {
78  typename LightObject::Pointer loPtr = Superclass::InternalClone();
79 
80  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
81  if(rval.IsNull())
82  {
83  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
84  }
85  rval->m_MaskImage = m_MaskImage;
86  rval->m_Logger = m_Logger;
87  rval->m_LoggerVector = m_LoggerVector;
88  rval->m_ThreadID = m_ThreadID;
89  rval->m_LogLevel = m_LogLevel;
90  rval->SetNumberOfThreads(this->GetNumberOfThreads());
91  rval->SetDebug(this->GetDebug());
92  return loPtr;
93 }
94 
95 template< class TInputImage, class TOutputImage, class TMaskImage >
96 void
99 {
100  utlException(!(this->GetDebug() && this->GetNumberOfThreads()>1), "create m_LoggerVector only when m_Debug is true and multiple threads are used");
101  if (!m_Logger)
102  {
103  itk::StdStreamLogOutput::Pointer coutput = itk::StdStreamLogOutput::New();
104  coutput->SetStream(std::cout);
105  m_Logger = itk::ThreadLogger::New();
106  m_Logger->SetPriorityLevel(itk::LoggerBase::DEBUG);
107  m_Logger->SetLevelForFlushing(itk::LoggerBase::MUSTFLUSH);
108  m_Logger->AddLogOutput(coutput);
109  }
110 
111  int numberofThreads = this->GetNumberOfThreads();
112  m_LoggerVector = LoggerVectorPointer(new LoggerVectorType(numberofThreads));
113  for (int ii = 0; ii < numberofThreads; ++ii)
114  (*m_LoggerVector)[ii] = m_Logger;
115 }
116 
117 template< class TInputImage, class TOutputImage, class TMaskImage >
118 void
120 ::WriteLogger(const std::string& str, const LoggerBase::PriorityLevelType level) const
121 {
122  if (m_ThreadID>=0 && this->GetDebug() && this->GetNumberOfThreads()>1)
123  {
124  utlSAException(this->m_LoggerVector->size()!=this->GetNumberOfThreads())
125  (this->m_LoggerVector->size())(this->GetNumberOfThreads()).msg("need to set m_LoggerVector");
126  (*this->m_LoggerVector)[m_ThreadID]->Write(level, str);
127  (*this->m_LoggerVector)[m_ThreadID]->Flush();
128  }
129  else
130  std::cout << str << std::flush;
131 }
132 
133 template< class TInputImage, class TOutputImage, class TMaskImage >
134 std::string
137 {
138  if (m_ThreadID>=0 && this->GetDebug() && this->GetNumberOfThreads()>1)
139  {
140  std::ostringstream msg;
141  msg << "<Thread " << m_ThreadID << "> : ";
142  return msg.str();
143  }
144  else
145  return "";
146 }
147 
148 template< class TInputImage, class TOutputImage, class TMaskImage >
149 void
152 {
153  utl::InitializeThreadedLibraries(this->GetNumberOfThreads());
154 }
155 
156 template< class TInputImage, class TOutputImage, class TMaskImage >
157 void
159 ::PrintSelf(std::ostream& os, Indent indent) const
160 {
161  Superclass::PrintSelf(os, indent);
162  if (!IsImageEmpty(m_MaskImage))
163  os << indent << "MaskImage = " << m_MaskImage << std::endl << std::flush;
164 }
165 
166 }
167 
168 #endif
169 
170 
virtual void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
helper functions specifically used in dmritool
std::vector< LoggerPointer > LoggerVectorType
virtual void SetMaskImage(MaskImageType *_arg)
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
normal log
Definition: utlCoreMacro.h:191
ImageToImageFilter< TInputImage, TOutputImage > Superclass
void WriteLogger(const std::string &str, const LoggerBase::PriorityLevelType level=LoggerBase::DEBUG) const
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
ImageToImageFilter with mask and threaded logger support.
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
LightObject::Pointer InternalClone() const ITK_OVERRIDE
utl_shared_ptr< LoggerVectorType > LoggerVectorPointer
static void InitializeThreadedLibraries(const int numThreads)
Definition: utl.h:327