]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSurfaceTexture.cxx
#3301 BBTK Bug New Normal - SurfaceTexture
[bbtk.git] / packages / vtk / src / bbvtkSurfaceTexture.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 "bbvtkSurfaceTexture.h"
5 #include "bbvtkPackage.h"
6
7 #include <vtkPointData.h>
8 #include <vtkMath.h>
9
10 namespace bbvtk
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,SurfaceTexture)
14 BBTK_BLACK_BOX_IMPLEMENTATION(SurfaceTexture,bbtk::AtomicBlackBox);
15 //===== 
16 // 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)
17 //===== 
18 void SurfaceTexture::Process()
19 {
20 printf("EED SurfaceTexture:Process() Start\n");
21
22 // THE MAIN PROCESSING METHOD BODY
23 //   Here we simply set the input 'In' value to the output 'Out'
24 //   And print out the output value
25 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
26 //    void bbSet{Input|Output}NAME(const TYPE&)
27 //    const TYPE& bbGet{Input|Output}NAME() const 
28 //    Where :
29 //    * NAME is the name of the input/output
30 //      (the one provided in the attribute 'name' of the tag 'input')
31 //    * TYPE is the C++ type of the input/output
32 //      (the one provided in the attribute 'type' of the tag 'input')
33
34 //    bbSetOutputOut( bbGetInputIn() );
35 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
36   
37
38
39         int     i;
40         double  spc[3];
41         double  range[2];
42         int     ext[6];
43
44         if ((bbGetInputImage()!=NULL) && (bbGetInputMesh()!=NULL))
45         {
46
47
48                 bbGetInputImage()->GetSpacing(spc);
49                 bbGetInputImage()->GetScalarRange(range);
50
51         //EED 2017-01-01 Migration VTK7
52         #if VTK_MAJOR_VERSION <= 5
53                 bbGetInputImage()->GetWholeExtent(ext);
54         #else
55                 bbGetInputImage()->GetExtent(ext);
56         #endif
57
58                 int maxX = ext[1]-ext[0]+1;
59                 int maxY = ext[3]-ext[2]+1;
60                 int maxZ = ext[5]-ext[4]+1;
61                 
62                 if (firsttime==true)
63                 {
64                         firsttime=false;
65                         // Generate the colors for each point based on the color map
66                         colors = vtkUnsignedCharArray::New(); 
67                         colors->SetNumberOfComponents(3);
68                         colors->SetName("Colors");
69                         
70                         // Create the color map
71                         if (bbGetInputColorType()==1)
72                         {
73                                 colorLookupTableWL      = vtkWindowLevelLookupTable::New();
74                                 colorLookupTableWL->InverseVideoOn();
75                                 colorLookupTable        = colorLookupTableWL;
76                         } else {
77                                 colorLookupTable = vtkLookupTable::New();
78                         }
79                         colorLookupTable->SetTableRange(range[0],range[1]);
80                         colorLookupTable->Build();
81                         double rgba1[4];
82                         double rgba2[4];
83                         for (int iLookTable = 0; iLookTable<128; iLookTable++)
84                         {
85                           colorLookupTable->GetTableValue(      iLookTable, rgba1);
86                           colorLookupTable->GetTableValue(256-1-iLookTable, rgba2);
87
88                           colorLookupTable->SetTableValue(256-1-iLookTable , rgba1[0],rgba1[1],rgba1[2],rgba1[3]);
89                           colorLookupTable->SetTableValue(      iLookTable , rgba2[0],rgba2[1],rgba2[2],rgba2[3]);
90                         } // for iLookTable
91
92         //EED 2018-06-8 ***********************ARDS Projet***********************************************
93                         if (bbGetInputColorType()==2)
94                         {
95                                 int tableSize=256;
96                                 colorLookupTable = vtkLookupTable::New();
97         //                      colorLookupTable->SetNumberOfTableValues(tableSize); 
98                                 colorLookupTable->SetTableRange(range[0],range[1]);
99         //                      colorLookupTable->SetTableRange(0,11);
100                                 colorLookupTable->Build();
101
102         /*
103           for (unsigned int i = 0; i < tableSize; ++i)
104                 {
105                 colorLookupTable->SetTableValue(i,
106                                    vtkMath::Random(.25, 1.0),
107                                    vtkMath::Random(.25, 1.0),
108                                    vtkMath::Random(.25, 1.0),
109                                    1.0);
110                 }
111         */
112
113                                 double rgba1[4];
114                                 double rgba2[4];
115                                 int i,j;
116                                 for (int iLookTable = 0; iLookTable<tableSize*3; iLookTable++)
117                                 {
118                                         i=rand() % tableSize;
119                                         j=rand() % tableSize;
120                                         colorLookupTable->GetTableValue(i, rgba1);
121                                         colorLookupTable->GetTableValue(j, rgba2);
122
123                                   colorLookupTable->SetTableValue(j, rgba1[0],rgba1[1],rgba1[2],rgba1[3]);
124                                   colorLookupTable->SetTableValue(i, rgba2[0],rgba2[1],rgba2[2],rgba2[3]);
125                                 } // for iLookTable
126
127                         } // type 2
128
129
130                         if (bbGetInputColorType()==3)
131                         {
132                                 int tableSize=13;
133                                 colorLookupTable = vtkLookupTable::New();
134                                 colorLookupTable->SetNumberOfTableValues(tableSize); 
135                                 colorLookupTable->SetTableRange(6,range[1]);
136                                 colorLookupTable->Build();
137
138                           colorLookupTable->SetTableValue(0, 1  ,1      ,1      ,1);
139                           colorLookupTable->SetTableValue(1, 1  ,0      ,0      ,1);
140                           colorLookupTable->SetTableValue(2, 0  ,1      ,0      ,1);
141                           colorLookupTable->SetTableValue(3, 0  ,0      ,1      ,1);
142                           colorLookupTable->SetTableValue(4, 0  ,1      ,1      ,1);
143                           colorLookupTable->SetTableValue(5, 1  ,1      ,0      ,1);
144                           colorLookupTable->SetTableValue(6, 1  ,0      ,1      ,1);
145
146                           colorLookupTable->SetTableValue(7, 0.5,1      ,1      ,1);
147                           colorLookupTable->SetTableValue(8, 1  ,0.5,1  ,1);
148                           colorLookupTable->SetTableValue(9, 1  ,1      ,0.5,1);
149
150                           colorLookupTable->SetTableValue(10,1  ,0.5,0.5,1);
151                           colorLookupTable->SetTableValue(11,0.5,0.5,1  ,1);
152
153                         } // type 3
154
155
156                         
157
158                 }  // firsttime
159
160
161                 bbGetInputMesh()->GetPointData()->SetScalars(colors);
162
163                 if (bbGetInputColorType()==1)
164                 {
165                         colorLookupTableWL->SetLevel( bbGetInputColorLevel() );
166                         colorLookupTableWL->SetWindow( bbGetInputColorWindow() );
167                 }       
168
169
170                 if (bbGetInputExternalLookupTable()!=NULL) 
171                 {
172                         generalLookupTable = bbGetInputExternalLookupTable();
173                 } else {
174                         generalLookupTable = colorLookupTable;
175                 }
176                         
177                 int missingpoints = bbGetInputMesh()->GetNumberOfPoints() - colors->GetDataSize()/colors->GetNumberOfComponents();
178                 for(i = 0; i < missingpoints; i++)
179                 {
180                         colors->InsertNextTuple3(0,0,0);
181                 }       
182
183                 if (bbGetInputTransform()!=NULL)
184                 {
185                         bbGetInputTransform()->Update();
186                 }
187                 
188                 double gl; 
189                 double p1[3];
190                 double p2[3];
191                 double dcolor[3];
192                 for(i = 0; i < bbGetInputMesh()->GetNumberOfPoints(); i++)
193                 {
194                         if (bbGetInputTransform()!=NULL)
195                         {
196                                 bbGetInputMesh()->GetPoint(i,p1);
197                                 bbGetInputTransform()->TransformPoint(p1,p2);
198                         } else {
199                                 bbGetInputMesh()->GetPoint(i,p2);
200                         }
201                         p2[0] = p2[0]/spc[0];
202                         p2[1] = p2[1]/spc[1];
203                         p2[2] = p2[2]/spc[2];   
204                         
205                         if ( (p2[0]>=0) && (p2[0]<maxX) && (p2[1]>=0) && (p2[1]<maxY) &&(p2[2]>=0) && (p2[2]<maxZ)  )
206                         {
207                            gl =  bbGetInputImage()->GetScalarComponentAsDouble(round(p2[0]), round(p2[1]), round(p2[2]),0);
208                         } else {
209                                 gl=0;
210                         }
211                         generalLookupTable->GetColor(gl, dcolor);
212                         colors->SetTuple3(i,255*dcolor[0],255*dcolor[1],255*dcolor[2]);         
213                 } // for i
214                 bbGetInputMesh()->Modified();   
215
216         } // if Image && Mesh
217 printf("EED SurfaceTexture:Process() End\n");
218
219
220 }
221 //===== 
222 // 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)
223 //===== 
224 void SurfaceTexture::bbUserSetDefaultValues()
225 {
226
227 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
228 //    Here we initialize the input 'In' to 0
229         bbSetInputMesh(NULL);
230         bbSetInputImage(NULL);
231         bbSetInputColorType(0);
232         bbSetInputColorLevel(500);
233         bbSetInputColorWindow(500);
234         bbSetInputTransform(NULL);
235         bbSetInputExternalLookupTable(NULL);
236   
237         firsttime                               = true;
238         colors                                  = NULL;
239         colorLookupTable                = NULL;
240         colorLookupTableWL              = NULL;
241 }
242 //===== 
243 // 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)
244 //===== 
245 void SurfaceTexture::bbUserInitializeProcessing()
246 {
247
248 //  THE INITIALIZATION METHOD BODY :
249 //    Here does nothing 
250 //    but this is where you should allocate the internal/output pointers 
251 //    if any 
252
253   
254 }
255 //===== 
256 // 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)
257 //===== 
258 void SurfaceTexture::bbUserFinalizeProcessing()
259 {
260
261 //  THE FINALIZATION METHOD BODY :
262 //    Here does nothing 
263 //    but this is where you should desallocate the internal/output pointers 
264 //    if any
265   
266 }
267 }
268 // EO namespace bbvtk
269
270