GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoMegaImageProcessor.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #include "GoMegaImageProcessor.h"
36 
37 #include "vtkMath.h"
38 
39 //--------------------------------------------------------------------------
40 void
43 {
44  m_MegaImageReader = iReader;
45 
46  // update general parameters
47  //--------------------
48  // todo Nicolas- Create a method for that...
49  m_BoundsTime[0] = m_MegaImageReader->GetMinTimePoint();
50  m_BoundsTime[1] = m_MegaImageReader->GetMaxTimePoint();
51  m_BoundsChannel[0] = m_MegaImageReader->GetMinChannel();
52  m_BoundsChannel[1] = m_MegaImageReader->GetMaxChannel();
53 
54  m_TimeInterval = m_MegaImageReader->GetTimeInterval();
55 }
56 //--------------------------------------------------------------------------
57 
58 //--------------------------------------------------------------------------
59 void
61 initTimePoint(const unsigned int& iTime)
62 {
63  if( iTime != this->m_CurrentTimePoint )
64  {
65  //check if time point exists
66  if(iTime >= m_BoundsTime[0] && iTime <= m_BoundsTime[1])
67  {
68  this->m_CurrentTimePoint = iTime;
69 
70  m_MegaImageReader->SetTimePoint(iTime);
71  m_MegaImageReader->Update();
72  m_MegaImageContainer.clear();
73  }
74  else
75  {
76  return;
77  }
78 
79  // update the container
80  // Get Number of channels from reader
81  int n = getNumberOfChannels();
82  // while(numberOfChannels>0)
83 
84  #ifdef HAS_OPENMP
85  #pragma omp parallel for
86  #endif
87  for( int kk = 0; kk < n; ++kk )
88  {
89  int numberOfChannels = n - ( kk + 1 );
90 // --numberOfChannels;
91 
92  // Get useful information from the reader
93  // Nicolas Get Image or get output...?
94  vtkSmartPointer<vtkImageData> image =
95  m_MegaImageReader->GetOutput(numberOfChannels);
96 
97  // capacity of image -> rescale in multichannelmode
98  int type = image->GetScalarSize();
99  double threshold = pow((double)2, (int)8*type) - 1;
100  m_MaxImage = threshold;
101  // max pixel in image
102  double range = image->GetScalarRange()[1];
103  if(m_MaxThreshold < range)
104  {
105  m_MaxThreshold = range;
106  }
107 
108  // Get Color
109  std::vector<std::vector<int> >channelColor =
110  m_MegaImageReader->GetChannelColor();
111 
112 
113  double random1 = channelColor[numberOfChannels][0];
114  double value1 = random1;
115 
116  double random2 = channelColor[numberOfChannels][1];
117  double value2 = random2;
118 
119  double random3 = channelColor[numberOfChannels][2];
120  double value3 = random3;
121 
122  std::vector<double> color( 4 );
123  color[0] = value1;
124  color[1] = value2;
125  color[2] = value3;
126  color[3] = 255;
127 
128  // Create LUT
129  vtkSmartPointer<vtkLookupTable> lut = createLUT(color[0],
130  color[1],
131  color[2],
132  color[3]);
133 
134  // create name...
135  std::stringstream channelName;
136  channelName << "Channel ";
137  channelName << numberOfChannels;
138 
139  // Update the MegaImageStructure
140  // image, LUT, channel, time point
141  m_MegaImageContainer.insert(GoMegaImageStructure(numberOfChannels,
142  lut,
143  image,
144  color,
145  true,
146  channelName.str()));
147  }
148  }
149 }
150 //--------------------------------------------------------------------------
151 
152 //--------------------------------------------------------------------------
153 void
155 setTimePoint(const unsigned int& iTime)
156 {
157  if( iTime != this->m_CurrentTimePoint )
158  {
159  //check if time point exists
160  if(iTime >= m_BoundsTime[0] && iTime <= m_BoundsTime[1])
161  {
162  m_MegaImageReader->SetTimePoint(iTime);
163  m_MegaImageReader->Update();
164  }
165  else
166  {
167  return;
168  }
169 
170  this->m_CurrentTimePoint = iTime;
171 
172  // update the container
173  // Get Number of channels from reader
174  //int numberOfChannels = getNumberOfChannels();
175 
176  int n = getNumberOfChannels();
177 
178  #ifdef HAS_OPENMP
179  #pragma omp parallel for
180  #endif
181  for( int kk = 0; kk < n; kk++ )
182  {
183  int numberOfChannels = n - ( kk + 1 );
184 
185  // Get useful information from the reader
186  // Nicolas Get Image or get output...?
187  vtkSmartPointer<vtkImageData> image =
188  m_MegaImageReader->GetOutput(numberOfChannels);
189 
190  // capacity of image -> rescale in multichannelmode
191  int type = image->GetScalarSize();
192  double threshold = pow((double)2, (int)8*type) - 1;
193  m_MaxImage = threshold;
194  // max pixel in image
195  double range = image->GetScalarRange()[1];
196  if(m_MaxThreshold < range)
197  {
198  m_MaxThreshold = range;
199  }
200 
201  GoMegaImageStructureMultiIndexContainer::index<Index>::type::iterator it =
202  m_MegaImageContainer.get< Index >().find(numberOfChannels);
203 
204  if(it!=m_MegaImageContainer.get< Index >().end())
205  {
206  m_MegaImageContainer.get< Index >().modify( it , set_image(image) );
207  }
208  }
209  }
210 }
211 //--------------------------------------------------------------------------
212 
213 //--------------------------------------------------------------------------
214 void
216 setDoppler(const unsigned int& iTime, const unsigned int& iPrevious)
217 {
218  //to optimize doppler view later on
219  (void) iPrevious;
220 
221  //check if time point exists
222  if(iTime >= m_BoundsTime[0] && iTime <= m_BoundsTime[1])
223  {
224  m_MegaImageReader->SetTimePoint(iTime);
225  m_MegaImageReader->Update();
226  m_MegaImageContainer.clear();
227  }
228  else
229  {
230  return;
231  }
232 
233  std::vector<int> dopplerTime = getDopplerTime(iTime);
234 
235  int DopplerSize = static_cast< int >( this->getDopplerSize() );
236 
237 #ifdef HAS_OPENMP
238 #pragma omp parallel for
239 #endif
240  for(int i=0; i < DopplerSize; ++i)
241  {
242  if(dopplerTime[i] >= 0)
243  {
244  // Get useful information from the reader
245  // Nicolas Get Image or get output...?
246  vtkSmartPointer<vtkImageData> image =
247  m_MegaImageReader->GetImage(m_DopplerChannel,dopplerTime[i]);
248 
249  // hue: 0 to 0.7
250  double* rgb = vtkMath::HSVToRGB(
251  static_cast<double>(i)/static_cast<double>(getDopplerSize()),1,1);
252 
253  // color from red to blue
254  std::vector<double> color;
255  color.push_back(rgb[0]*255);
256  color.push_back(rgb[1]*255);
257  color.push_back(rgb[2]*255);
258  color.push_back(255);
259 
260  // Create LUT
261  vtkSmartPointer<vtkLookupTable> lut = createLUT(color[0],
262  color[1],
263  color[2],
264  color[3]);
265 
266  // channel name
267  std::stringstream channelName;
268  //channelName << "t: ";
269  channelName << dopplerTime[i];
270 
271  // Update the MegaImageStructure
272  // image, LUT, channel, time point
273  m_MegaImageContainer.insert(GoMegaImageStructure(dopplerTime[i],
274  lut,
275  image,
276  color,
277  true,
278  channelName.str()));
279 
280  }
281  }
282 
283 /*
284  vtkSmartPointer< vtkImageAppendComponents > append_filter =
285  vtkSmartPointer< vtkImageAppendComponents >::New();
286 
287  // if step != 1 and we have previous and next time point loaded
288  if( m_DopplerStep != 1 || iPreviousT == 0 )
289  {
290  // resize internal image
291  // clean the vector since is is a vector of smartpointers
292  m_InternalImages.resize(3, NULL);
293 
294  vtkSmartPointer< vtkImageData > i0 = vtkSmartPointer< vtkImageData >::New();
295  i0->ShallowCopy( m_MegaCaptureReader->GetImage(iChannel, t0) );
296  m_InternalImages[0] = i0;
297  append_filter->AddInput(m_InternalImages[0]);
298 
299  vtkSmartPointer< vtkImageData > i1 = vtkSmartPointer< vtkImageData >::New();
300  i1->ShallowCopy( m_MegaCaptureReader->GetImage(iChannel, t1) );
301  m_InternalImages[1] = i1;
302  append_filter->AddInput(m_InternalImages[1]);
303 
304  vtkSmartPointer< vtkImageData > i2 = vtkSmartPointer< vtkImageData >::New();
305  i2->ShallowCopy( m_MegaCaptureReader->GetImage(iChannel, t2) );
306  m_InternalImages[2] = i2;
307  append_filter->AddInput(m_InternalImages[2]);
308  }
309  else
310  {
311  // if we go FORWARD and step == 1
312  if( iPreviousT < m_TCoord)
313  {
314  // assume we imcrease t point all the time for testing
315  vtkSmartPointer< vtkImageData > i0 = vtkSmartPointer< vtkImageData >::New();
316  i0->ShallowCopy(m_InternalImages[1]);
317  // clean smartpointer
318  m_InternalImages[0] = NULL;
319  m_InternalImages[0] = i0;
320  append_filter->AddInput(m_InternalImages[0]);
321 
322  vtkSmartPointer< vtkImageData > i1 = vtkSmartPointer< vtkImageData >::New();
323  i1->ShallowCopy(m_InternalImages[2]);
324  // clean smartpointer
325  m_InternalImages[1] = NULL;
326  m_InternalImages[1] = i1;
327  append_filter->AddInput(m_InternalImages[1]);
328 
329  vtkSmartPointer< vtkImageData > i2 = vtkSmartPointer< vtkImageData >::New();
330  i2->ShallowCopy( m_MegaCaptureReader->GetImage(iChannel, t2) );
331  // clean smartpointer
332  m_InternalImages[2] = NULL;
333  m_InternalImages[2] = i2;
334  append_filter->AddInput(m_InternalImages[2]);
335  }
336  // if we go BACKWARD and step == 1
337  else
338  {
339  vtkSmartPointer< vtkImageData > i2 = vtkSmartPointer< vtkImageData >::New();
340  i2->ShallowCopy(m_InternalImages[1]);
341  // clean smartpointer
342  m_InternalImages[2] = NULL;
343  m_InternalImages[2] = i2;
344  append_filter->AddInput(m_InternalImages[2]);
345 
346  vtkSmartPointer< vtkImageData > i1 = vtkSmartPointer< vtkImageData >::New();
347  i1->ShallowCopy(m_InternalImages[0]);
348  // clean smartpointer
349  m_InternalImages[1] = NULL;
350  m_InternalImages[1] = i1;
351  append_filter->AddInput(m_InternalImages[1]);
352 
353  vtkSmartPointer< vtkImageData > i0 = vtkSmartPointer< vtkImageData >::New();
354  i0->ShallowCopy( m_MegaCaptureReader->GetImage(iChannel, t0) );
355  // clean smartpointer
356  m_InternalImages[0] = NULL;
357  m_InternalImages[0] = i0;
358  append_filter->AddInput(m_InternalImages[0]);
359  }
360  }
361  */
362 }
363 //--------------------------------------------------------------------------
itk::MegaCaptureReader::Pointer m_MegaImageReader
virtual void initTimePoint(const unsigned int &iTime)
load all the channels for the given time point into the GoMegaImageStructure
unsigned int m_CurrentTimePoint
void setReader(itk::MegaCaptureReader::Pointer iReader)
Set the reader.
std::vector< int > getDopplerTime(unsigned int iTime)
change visibility of given structure
unsigned int m_BoundsTime[2]
Convenience structure to store visible image.
unsigned int getDopplerSize()
unsigned int getNumberOfChannels()
unsigned int m_DopplerChannel
unsigned int m_BoundsChannel[2]
virtual void setTimePoint(const unsigned int &iTime)
update images from the current GoMegaImageStructure
GoMegaImageStructureMultiIndexContainer m_MegaImageContainer
unsigned int m_TimeInterval
SmartPointer< Self > Pointer
vtkSmartPointer< vtkLookupTable > createLUT(const double &iRed, const double &iGreen, const double &iBlue, const double &iAlpha)
create a lookuptable (LUT) given r, g, b, a and range LUT will go from black to the color...
virtual void setDoppler(const unsigned int &iTime, const unsigned int &iPrevious)
load all time points of the given channel into the GoMegaImageStructure. Called Doppler View...