]> Creatis software - creaVtk.git/blob - lib/creaVtk/vtkTensorsVisu.cpp
#3110 creaVtk Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaVtk.git] / lib / creaVtk / vtkTensorsVisu.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sante)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28 #include "vtkTensorsVisu.h"
29
30 #include "vtkProperty.h"
31 #include "vtkDoubleArray.h"
32
33
34 //------------------------------------------------------------------------
35 vtkTensorsVisu::vtkTensorsVisu() : vtkVectorsTensorsVisuBase()
36 {
37         ss                                      = vtkSphereSource::New();
38         superquadratic          = vtkSuperquadricSource::New();
39         cs                                      = vtkCubeSource::New(); 
40         tg                                      = vtkTensorGlyph::New();
41         pdn                             = vtkPolyDataNormals::New();
42         pd                                      = vtkPolyData::New();
43         pod                             = vtkPointData::New();
44 }
45
46 //------------------------------------------------------------------------
47 vtkTensorsVisu::~vtkTensorsVisu()
48 {
49 }
50
51
52 //------------------------------------------------------------------------
53 void vtkTensorsVisu::Process()
54 {
55   if (_active==true)
56   {
57         if(GetTypeForm()==2)                                                            // source superquadratic
58         {
59                 superquadratic->SetThetaResolution(20);
60                 superquadratic->SetPhiResolution(20);
61 #if VTK_MAJOR_VERSION <= 5
62                 tg->SetSource(superquadratic->GetOutput());
63 #else
64                 superquadratic->Update();
65                 tg->SetSourceData(superquadratic->GetOutput());
66 #endif
67         } else if(GetTypeForm()==1) {                                   // source sphere
68 #if VTK_MAJOR_VERSION <= 5
69                 tg->SetSource(ss->GetOutput());
70 #else
71                 ss->Update();
72                 tg->SetSourceData(ss->GetOutput());
73 #endif
74         } else {                                                                                                // source cube
75 #if VTK_MAJOR_VERSION <= 5
76                 tg->SetSource(cs->GetOutput());         
77 #else
78                 cs->Update();
79                 tg->SetSourceData(cs->GetOutput());     
80 #endif
81         }
82
83 #if VTK_MAJOR_VERSION <= 5
84         tg->SetInput( GetDataObject() );
85 #else
86         tg->SetInputData( GetDataObject() );
87 #endif
88         //              tg->ScalingOn();
89         //              tg->SetScaling(25);
90         tg->SetScaleFactor( GetScaleFactor() );
91
92         //The normals are needed to generate the right colors and if
93         // not used some of the glyphs are black.
94 //EED 2017-01-01 Migration VTK7
95 #if VTK_MAJOR_VERSION <= 5
96         pdn->SetInput(tg->GetOutput());
97         _pdm->SetInput( pdn->GetOutput() );
98 #else
99         pdn->SetInputData(tg->GetOutput());
100         pdn->Update();
101         _pdm->SetInputData( pdn->GetOutput() );
102         _pdm->Update();
103 #endif
104         _actor->SetMapper( _pdm );
105    _actor->GetProperty()->SetOpacity( GetOpacity() );
106
107    tg->SetColorModeToEigenvalues();
108 //   tg->SetColorModeToScalars();
109    tg->Update();
110
111 //EED 2017-01-01 Migration VTK7
112 #if VTK_MAJOR_VERSION <= 5
113         pd = tg->GetOutput();
114         pd->Update();
115 #else
116         tg->Update();
117         pd = tg->GetOutput();
118 #endif
119
120         pod = pd->GetPointData();
121         pod->Update();
122
123         tg->Update();
124         vtkPolyData             *pd_pdn                         = pdn->GetOutput();
125         vtkPointData    *pointdata_pdn          = pd_pdn->GetPointData();
126
127         vtkPolyData             *pd_tg                          = tg->GetOutput();
128         vtkPointData    *pointdata_tg           = pd_tg->GetPointData();
129
130         vtkPolyData             *pd_do                          = (vtkPolyData*)GetDataObject();
131         vtkPointData    *pointdata_do           = pd_do->GetPointData();
132
133         int numPnts = ((vtkPolyData *)GetDataObject())->GetNumberOfPoints();
134
135         vtkIdType numSourcePts;
136 //      vtkIdType numSourceCells;
137
138         vtkPoints *sourcePts    = tg->GetSource()->GetPoints();
139
140
141         numSourcePts                    = sourcePts->GetNumberOfPoints();
142 //      numSourceCells = tg->GetSource()->GetNumberOfCells();
143
144         vtkDataArray *tensorsArray = pointdata_do->GetArray("tensors");
145         if (tensorsArray)
146    {
147                 vtkDoubleArray  *newScalarArray                         = vtkDoubleArray::New();
148                 newScalarArray->SetName( "NormalsEED" );
149                 newScalarArray->SetNumberOfComponents(3); 
150                 int     iNumPts,iNumSourcePts;
151                 int             iNormalsEED=0;
152                 double  *normalValue;
153                 double   sumEigenvalue,sumEigenvalue1,sumEigenvalue2,sumEigenvalue3;
154                 unsigned char   dirx,diry,dirz;
155                 int      idEigen;
156                 for (iNumPts=0;iNumPts<numPnts;iNumPts++)
157                 {
158                         normalValue = tensorsArray->GetTuple9(iNumPts);
159                 sumEigenvalue1 = sqrt (normalValue[0]*normalValue[0] + normalValue[1]*normalValue[1] + normalValue[2]*normalValue[2]);
160                 sumEigenvalue2 = sqrt (normalValue[3]*normalValue[3] + normalValue[4]*normalValue[4] + normalValue[5]*normalValue[5]);
161                 sumEigenvalue3 = sqrt (normalValue[6]*normalValue[6] + normalValue[7]*normalValue[7] + normalValue[8]*normalValue[8]);
162
163                         if ((sumEigenvalue1>=sumEigenvalue2) && (sumEigenvalue1>=sumEigenvalue3))
164                         {
165                                 sumEigenvalue=sumEigenvalue1;
166                                 idEigen=0;
167                         }
168                         if ((sumEigenvalue2>sumEigenvalue1) && (sumEigenvalue2>=sumEigenvalue3))
169                         {
170                                 sumEigenvalue=sumEigenvalue2;
171                                 idEigen=3;
172                         }
173                         if ((sumEigenvalue3>=sumEigenvalue1) && (sumEigenvalue3>=sumEigenvalue2))
174                         {
175                                 sumEigenvalue=sumEigenvalue3;
176                                 idEigen=6;
177                         }
178
179                         dirx = (unsigned char)( 255*fabs(normalValue[idEigen+0]/sumEigenvalue) );
180                         diry = (unsigned char)( 255*fabs(normalValue[idEigen+1]/sumEigenvalue) );
181                         dirz = (unsigned char)( 255*fabs(normalValue[idEigen+2]/sumEigenvalue) );
182
183                         for (iNumSourcePts=0;iNumSourcePts<numSourcePts;iNumSourcePts++)
184                         {
185 //                              newScalarArray->InsertTuple3( iNormalsEED, dirx,diry,dirz);
186                                 newScalarArray->InsertTuple3( iNormalsEED, normalValue[idEigen+0],normalValue[idEigen+1],normalValue[idEigen+2] );
187                                 iNormalsEED++;
188                         } // for iNumSourcePts
189
190                 } // for iNumPts
191
192
193                 pointdata_tg->RemoveArray( "NormalsEED" );
194                 pointdata_tg->AddArray( newScalarArray );
195 //              pointdata_pdn->Update();
196                 tg->Update();
197         } // if  tensorsArray
198
199         _pdm->ScalarVisibilityOn();
200         _pdm->SetColorModeToMapScalars();
201         //_pdm->SetColorModeToDefault();
202         // _pdm->SetColorModeToDirectScalars();  // NOT compile
203         _pdm->SetScalarModeToUsePointFieldData();
204         _pdm->ImmediateModeRenderingOn();
205
206         _LutEED->SetVectorModeToComponent();
207         //_LutEED->SetVectorModeToRGBColors();
208         //_LutEED->SetVectorModeToMagnitud();
209         _pdm->SetLookupTable( _LutEED );
210         _pdm->SelectColorArray( "NormalsEED" );
211
212 //EED 2017-01-01 Migration VTK7
213 #if VTK_MAJOR_VERSION <= 5
214   //...
215 #else
216         _pdm->Update();
217 #endif
218
219 /*
220                         int i,sizeDa = pointdata_tg->GetNumberOfArrays();
221                         printf("EED vtkTensorsVisu::Process  sizeDa %d \n", sizeDa );
222                         for (i=0;i<sizeDa;i++)
223                         {
224                                 int faces = pointdata_tg->GetArray(i)->GetSize() / numPnts;
225 //pointdata_pdn->GetArray(i)->Print( std::cout );
226                                 printf("EED vtkTensorsVisu::Process-name %d, %s, size=%d   %d \n", i, pointdata_tg->GetArrayName(i) , pointdata_tg->GetArray(i)->GetSize(), faces );
227
228                         }
229 */
230   } // _active
231   VisibilityActor(); 
232 }
233