]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkScalarsToColors.cxx
3511 bbcreaVtkPointPicker bug init point
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkScalarsToColors.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkScalarsToColors.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "vtkLookupTable.h"
8 #include "vtkObjectFactory.h"
9
10 namespace bbcreaVtk
11 {
12
13 class /*VTK_COMMON_EXPORT*/ vtkLookupTableDirectionVector2 : public vtkScalarsToColors
14 {
15 public:
16         // Description:
17         // Construct with range=[0,1]; and hsv ranges set up for rainbow color table 
18         // (from red to blue).
19         static vtkLookupTableDirectionVector2 *New();
20         vtkTypeMacro(vtkLookupTableDirectionVector2,vtkScalarsToColors);
21         void PrintSelf(ostream& os, vtkIndent indent);
22         double *GetRange() { return this->GetTableRange(); };
23         void SetRange(double min, double max) { this->SetTableRange(min, max); };
24         void SetRange(double rng[2]) { this->SetRange(rng[0], rng[1]); };
25         void SetTableRange(double r[2]); 
26         virtual void SetTableRange(double min, double max);
27         vtkGetVectorMacro(TableRange,double,2);
28         unsigned char *MapValue(double v);
29         void GetColor(double x, double rgb[3]);
30         void MapScalarsThroughTable2(void *input, unsigned char *output,
31                                                                  int inputDataType, int numberOfValues,
32                                                                  int inputIncrement, int outputIncrement);
33         void SetTypeTable( int typetable );
34 protected:
35         double TableRange[2];
36         vtkLookupTableDirectionVector2(int sze=256, int ext=256);
37         ~vtkLookupTableDirectionVector2();
38 private:
39         int TypeTable;                                                                                                                  //EED
40         vtkLookupTableDirectionVector2(const vtkLookupTableDirectionVector2&);  // Not implemented.
41         void operator=(const vtkLookupTableDirectionVector2&);                                  // Not implemented.
42 };
43
44
45
46
47 vtkStandardNewMacro(vtkLookupTableDirectionVector2);
48
49 // Construct with range=(0,1); and hsv ranges set up for rainbow color table 
50 // (from red to blue).
51 vtkLookupTableDirectionVector2::vtkLookupTableDirectionVector2(int sze, int ext)
52 {
53         this->TableRange[0] = 0.0;
54         this->TableRange[1] = 1.0;
55         TypeTable=0;
56 }
57
58 //----------------------------------------------------------------------------
59 vtkLookupTableDirectionVector2::~vtkLookupTableDirectionVector2()
60 {
61 }
62
63 unsigned char *vtkLookupTableDirectionVector2::MapValue(double v)
64 {
65         
66         //int idx = this->GetIndex(v);
67         //return (this->Table->GetPointer(0) + 4*idx);  
68         return 0;
69 }
70
71 void vtkLookupTableDirectionVector2::GetColor(double v, double rgb[3])
72 {
73 //      unsigned char *rgb8 = this->MapValue(v);
74 //      rgb[0] = rgb8[0]/255.0;
75 //      rgb[1] = rgb8[1]/255.0;
76 //      rgb[2] = rgb8[2]/255.0;
77
78         rgb[0] = 1;
79         rgb[1] = 1;
80         rgb[2] = 0;
81 }
82
83
84 void vtkLookupTableDirectionVector2::SetTableRange(double r[2])
85 {
86         this->SetTableRange(r[0],r[1]);
87 }
88
89 void vtkLookupTableDirectionVector2::SetTypeTable( int typetable )
90 {
91         TypeTable=typetable;
92 }
93
94 //----------------------------------------------------------------------------
95 // Set the minimum/maximum scalar values for scalar mapping. Scalar values
96 // less than minimum range value are clamped to minimum range value.
97 // Scalar values greater than maximum range value are clamped to maximum
98 // range value.
99 void vtkLookupTableDirectionVector2::SetTableRange(double rmin, double rmax)
100 {
101         if (rmax < rmin)
102     {
103                 vtkErrorMacro("Bad table range: ["<<rmin<<", "<<rmax<<"]");
104                 return;
105     }
106         
107         if (this->TableRange[0] == rmin && this->TableRange[1] == rmax)
108     {
109                 return;
110     }
111         
112         this->TableRange[0] = rmin;
113         this->TableRange[1] = rmax;
114         
115         this->Modified();
116 }
117
118
119
120 //----------------------------------------------------------------------------
121 // Although this is a relatively expensive calculation,
122 // it is only done on the first render. Colors are cached
123 // for subsequent renders.
124 template<class T>
125 void vtkLookupTableMapDirVectorEED2(vtkLookupTableDirectionVector2 *self, T *input, 
126                           unsigned char *output, int length, 
127                           int inIncr, int outFormat,int TypeTable)
128 {
129
130         if (TypeTable==0)
131         {
132                 double sum;
133                 int i;
134                 double dirx,diry,dirz;
135                 for (i = 0; i < length; ++i)
136                 {
137                                 dirx    = static_cast<T>(input[0]);
138                                 diry    = static_cast<T>(input[1]);
139                                 dirz    = static_cast<T>(input[2]);
140                                 input   = input+inIncr;
141                                 sum             = sqrt( dirx*dirx + diry*diry + dirz*dirz );
142                                 *output++ = (unsigned char) abs( (255*dirx/sum) );
143                                 *output++ = (unsigned char) abs( (255*diry/sum) );
144                                 *output++ = (unsigned char) abs( (255*dirz/sum) );
145                                 *output++ = 255;
146                 //              printf("%d %d %d   ",(int)(255*dirx/sum),(int)(255*diry/sum),(int)(255*dirz/sum));
147                 //              printf(" C     %d        %f %f %f   \n",inIncr,dirx,diry,dirz);
148
149                 } // for
150         } // typeTable == 0
151
152         if (TypeTable==1)
153         {
154                 int i;
155                 for (i = 0; i < length; ++i)
156                 {
157                                 *output++ = static_cast<T>(input[0]);  // red
158                                 *output++ = static_cast<T>(input[1]);  // green
159                                 *output++ = static_cast<T>(input[2]);  // blue
160 //                              *output++ = 255;
161                                 input   = input+inIncr;
162                 } // for
163         } // typeTable == 1
164
165
166 }
167
168
169 //----------------------------------------------------------------------------
170 void vtkLookupTableDirectionVector2::MapScalarsThroughTable2(void *input, 
171                                              unsigned char *output,
172                                              int inputDataType, 
173                                              int numberOfValues,
174                                              int inputIncrement,
175                                              int outputFormat)
176 {
177 // if (this->UseMagnitude && inputIncrement > 1)
178 // {
179      switch (inputDataType)
180      {
181        vtkTemplateMacro( vtkLookupTableMapDirVectorEED2(this,static_cast<VTK_TT*>(input),output,
182                          numberOfValues,inputIncrement,outputFormat,TypeTable);
183                          return 
184                         );
185         case VTK_BIT:
186             vtkErrorMacro("Cannot comput magnitude of bit array.");
187             break;
188         default:
189             vtkErrorMacro(<< "MapImageThroughTable: Unknown input ScalarType");
190      } /// switch
191 // } //if
192 }  
193
194
195 //----------------------------------------------------------------------------
196 void vtkLookupTableDirectionVector2::PrintSelf(ostream& os, vtkIndent indent)
197 {
198         this->Superclass::PrintSelf(os,indent); 
199 }
200
201
202
203
204
205
206 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ScalarsToColors)
207 BBTK_BLACK_BOX_IMPLEMENTATION(ScalarsToColors,bbtk::AtomicBlackBox);
208 //===== 
209 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
210 //===== 
211 void ScalarsToColors::Process()
212 {
213
214 // THE MAIN PROCESSING METHOD BODY
215 //   Here we simply set the input 'In' value to the output 'Out'
216 //   And print out the output value
217 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
218 //    void bbSet{Input|Output}NAME(const TYPE&)
219 //    const TYPE& bbGet{Input|Output}NAME() const 
220 //    Where :
221 //    * NAME is the name of the input/output
222 //      (the one provided in the attribute 'name' of the tag 'input')
223 //    * TYPE is the C++ type of the input/output
224 //      (the one provided in the attribute 'type' of the tag 'input')
225
226 //    bbSetOutputOut( bbGetInputIn() );
227 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
228   
229
230 //      if (firsttime==true)
231 //      {
232 //              firsttime=false;
233                 // Create the color map
234                 if ((bbGetInputType()==0) || (bbGetInputType()==100) )
235                 {
236                         vtkLookupTable *colorLookupTable = vtkLookupTable::New();
237                         if (bbGetInputRange().size()==2)
238                         {
239                                 colorLookupTable->SetRange( bbGetInputRange()[0],bbGetInputRange()[1]);
240 //                              colorLookupTable->SetTableRange( bbGetInputRange()[0],bbGetInputRange()[1]);
241             } else if (bbGetInputWindowColorLevel().size()==2) {
242                 double w    = bbGetInputWindowColorLevel()[0] / 2;
243                 double c    = bbGetInputWindowColorLevel()[1];
244                 colorLookupTable->SetRange( c-w , c+w );
245             }else {
246                                 colorLookupTable->SetRange(0,255);
247                         }
248             
249             colorLookupTable->UseBelowRangeColorOn();
250             colorLookupTable->UseAboveRangeColorOn();
251
252                         int NumberOfColors=1000;
253                         int NumberOfColorsHalf=NumberOfColors/2;
254                         colorLookupTable->SetNumberOfTableValues( NumberOfColors );
255                         colorLookupTable->Build();
256                         double rgba1[4];
257                         double rgba2[4];
258                         int iLookTable;
259                         for (iLookTable = 0; iLookTable<NumberOfColorsHalf; iLookTable++)
260                         {
261                                 colorLookupTable->GetTableValue(                         iLookTable, rgba1      );
262                                 colorLookupTable->GetTableValue(NumberOfColors-1-iLookTable, rgba2      );
263                                 colorLookupTable->SetTableValue(NumberOfColors-1-iLookTable, rgba1[0],rgba1[1],rgba1[2],rgba1[3]        );
264                                 colorLookupTable->SetTableValue(                         iLookTable, rgba2[0],rgba2[1],rgba2[2],rgba2[3]        );
265                         } // for iLookTable     
266                         
267                         // Transparency
268                         if (bbGetInputType()==0)   
269                         {
270                                 // First Element
271                                 double rgba[4];
272                                 colorLookupTable->GetTableValue(0,rgba);
273                                 rgba[3]=0;
274                                 colorLookupTable->SetTableValue(0,rgba);
275
276                                 colorLookupTable->UseBelowRangeColorOn();
277                                 colorLookupTable->UseAboveRangeColorOn();
278                                 if (bbGetInputBelowAboveRangeTransparence()==true)
279                                 {
280                                         colorLookupTable->SetBelowRangeColor(1,1,1,0);  // White transparent 
281                                         colorLookupTable->SetAboveRangeColor(1,1,1,0);  // White transparent 
282                                 } else {
283                     colorLookupTable->SetBelowRangeColor(0,0,0,1);  // White transparent
284                                         colorLookupTable->SetAboveRangeColor(1,1,1,1);  // White transparent 
285                                 }
286                                 
287                                 // Middle  range
288                                 int i,iMax=(NumberOfColors/16)*4;
289                                 for (i=0;i<iMax;i++)
290                                 {
291                                         colorLookupTable->GetTableValue(NumberOfColorsHalf+i, rgba);
292                                         rgba[3]=(double)i/(double)iMax;
293                                         colorLookupTable->SetTableValue(NumberOfColorsHalf+i,rgba);
294                                         colorLookupTable->GetTableValue(NumberOfColorsHalf-i, rgba);
295                                         rgba[3]=(double)i/(double)iMax;
296                                         colorLookupTable->SetTableValue(NumberOfColorsHalf-i,rgba);
297                                 } // for
298                         } //if Type 0                   
299                         
300                         colorLookupTable->Modified();
301                         _scalarstocolors = colorLookupTable;
302                 } // if Type 0 || 100 
303
304 //EED 2018-06-8 ***********************ARDS Projet***********************************************
305                 if ((bbGetInputType()==1) || (bbGetInputType()==101) )
306                 {
307                         vtkLookupTable *colorLookupTable = vtkLookupTable::New();
308             if (bbGetInputRange().size()==2)
309             {
310                 colorLookupTable->SetRange( bbGetInputRange()[0],bbGetInputRange()[1]);
311 //                colorLookupTable->SetTableRange( bbGetInputRange()[0],bbGetInputRange()[1]);
312             } else if (bbGetInputWindowColorLevel().size()==2) {
313                 double w    = bbGetInputWindowColorLevel()[0] / 2;
314                 double c    = bbGetInputWindowColorLevel()[1];
315                 colorLookupTable->SetRange( c-w , c+w );
316             }else {
317                 colorLookupTable->SetRange(0,255);
318             }
319             
320             colorLookupTable->UseBelowRangeColorOn();
321             colorLookupTable->UseAboveRangeColorOn();
322             if (bbGetInputBelowAboveRangeTransparence()==true)
323             {
324                 colorLookupTable->SetBelowRangeColor(1,1,1,0);  // White transparent
325                 colorLookupTable->SetAboveRangeColor(1,1,1,0);  // White transparent
326             } else {
327                 colorLookupTable->SetBelowRangeColor(0,0,0,1);  // White transparent
328                 colorLookupTable->SetAboveRangeColor(1,1,1,1);  // White transparent
329             }
330
331                         colorLookupTable->SetValueRange(0.0, 1.0); // from black to white
332                         colorLookupTable->SetSaturationRange(0.0, 0.0); // no color saturation
333                         colorLookupTable->SetRampToLinear();
334                         colorLookupTable->Build();
335                         // Transparency
336                         if (bbGetInputType()==1) 
337                         {
338                                 // First Element
339                                 double rgba[4];
340                                 colorLookupTable->GetTableValue(0,rgba);
341                                 rgba[3]=0;
342                                 colorLookupTable->SetTableValue(0,rgba);
343                         } // if Type 1
344                         _scalarstocolors = colorLookupTable;
345                 } // If Type 1 || 101
346
347
348                 if (bbGetInputType()==2)  // Direction Color Vector
349                 {
350                         vtkLookupTableDirectionVector2 *_LutEED = vtkLookupTableDirectionVector2::New();
351                         _LutEED->SetVectorModeToRGBColors();
352                         _LutEED->SetTypeTable(0);
353                         _scalarstocolors = _LutEED;
354                 } // If Type 2
355
356                 if (bbGetInputType()==3)  // Componets image rgb [0 255]
357                 {
358                         vtkLookupTableDirectionVector2 *_LutEED = vtkLookupTableDirectionVector2::New();
359                         _LutEED->SetVectorModeToRGBColors();
360                         _LutEED->SetTypeTable(1);                                // for components image
361                         _scalarstocolors = _LutEED;
362                 } // If Type 3
363
364 //      } // firsttime
365         double rgb[3];
366         std::vector<double>colorRGB;
367         _scalarstocolors->GetColor( bbGetInputScalarValue() , rgb );
368         colorRGB.push_back( rgb[0] );
369         colorRGB.push_back( rgb[1] );
370         colorRGB.push_back( rgb[2] );
371         bbSetOutputColor( colorRGB );
372         bbSetOutputLookupTable( _scalarstocolors );
373 }
374
375 //===== 
376 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
377 //===== 
378 void ScalarsToColors::bbUserSetDefaultValues()
379 {
380 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
381 //    Here we initialize the input 'In' to 0
382    bbSetInputType(0);
383 //   std::vector<double>range;
384 //   range.push_back( 0 );
385 //   range.push_back( 1 );
386 //      bbSetInputRange(range);
387    bbSetInputScalarValue(0);
388    bbSetInputBelowAboveRangeTransparence(true);
389 //   firsttime          = true;
390    _scalarstocolors     = NULL;
391 }
392
393 //===== 
394 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
395 //===== 
396 void ScalarsToColors::bbUserInitializeProcessing()
397 {
398 //  THE INITIALIZATION METHOD BODY :
399 //    Here does nothing 
400 //    but this is where you should allocate the internal/output pointers 
401 //    if any
402 }
403
404 //===== 
405 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
406 //===== 
407 void ScalarsToColors::bbUserFinalizeProcessing()
408 {
409 //  THE FINALIZATION METHOD BODY :
410 //    Here does nothing 
411 //    but this is where you should desallocate the internal/output pointers 
412 //    if any
413 }
414
415 }// EO namespace bbcreaVtk
416
417