]> 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                 tg->SetSourceData(superquadratic->GetOutput());
65 #endif
66         } else if(GetTypeForm()==1) {                                   // source sphere
67 #if VTK_MAJOR_VERSION <= 5
68                 tg->SetSource(ss->GetOutput());
69 #else
70                 tg->SetSourceData(ss->GetOutput());
71 #endif
72         } else {                                                                                                // source cube
73 #if VTK_MAJOR_VERSION <= 5
74                 tg->SetSource(cs->GetOutput());         
75 #else
76                 tg->SetSourceData(cs->GetOutput());     
77 #endif
78         }
79
80 #if VTK_MAJOR_VERSION <= 5
81         tg->SetInput( GetDataObject() );
82 #else
83         tg->SetInputData( GetDataObject() );
84 #endif
85         //              tg->ScalingOn();
86         //              tg->SetScaling(25);
87         tg->SetScaleFactor( GetScaleFactor() );
88
89         //The normals are needed to generate the right colors and if
90         // not used some of the glyphs are black.
91 //EED 2017-01-01 Migration VTK7
92 #if VTK_MAJOR_VERSION <= 5
93         pdn->SetInput(tg->GetOutput());
94         _pdm->SetInput( pdn->GetOutput() );
95 #else
96         pdn->SetInputData(tg->GetOutput());
97         _pdm->SetInputData( pdn->GetOutput() );
98 #endif
99         _actor->SetMapper( _pdm );
100    _actor->GetProperty()->SetOpacity( GetOpacity() );
101
102    tg->SetColorModeToEigenvalues();
103 //   tg->SetColorModeToScalars();
104    tg->Update();
105
106 //EED 2017-01-01 Migration VTK7
107 #if VTK_MAJOR_VERSION <= 5
108         pd = tg->GetOutput();
109         pd->Update();
110 #else
111         tg->Update();
112         pd = tg->GetOutput();
113 #endif
114
115         pod = pd->GetPointData();
116         pod->Update();
117
118         tg->Update();
119         vtkPolyData             *pd_pdn                         = pdn->GetOutput();
120         vtkPointData    *pointdata_pdn          = pd_pdn->GetPointData();
121
122         vtkPolyData             *pd_tg                          = tg->GetOutput();
123         vtkPointData    *pointdata_tg           = pd_tg->GetPointData();
124
125         vtkPolyData             *pd_do                          = (vtkPolyData*)GetDataObject();
126         vtkPointData    *pointdata_do           = pd_do->GetPointData();
127
128         int numPnts = ((vtkPolyData *)GetDataObject())->GetNumberOfPoints();
129
130         vtkIdType numSourcePts;
131 //      vtkIdType numSourceCells;
132
133         vtkPoints *sourcePts = tg->GetSource()->GetPoints();
134         numSourcePts = sourcePts->GetNumberOfPoints();
135 //      numSourceCells = tg->GetSource()->GetNumberOfCells();
136
137         vtkDataArray *tensorsArray = pointdata_do->GetArray("tensors");
138         if (tensorsArray)
139    {
140                 vtkDoubleArray  *newScalarArray                         = vtkDoubleArray::New();
141                 newScalarArray->SetName( "NormalsEED" );
142                 newScalarArray->SetNumberOfComponents(3); 
143                 int             iNumPts,iNumSourcePts;
144                 int             iNormalsEED=0;
145                 double  *normalValue;
146                 double   sumEigenvalue,sumEigenvalue1,sumEigenvalue2,sumEigenvalue3;
147                 unsigned char   dirx,diry,dirz;
148                 int      idEigen;
149                 for (iNumPts=0;iNumPts<numPnts;iNumPts++)
150                 {
151                         normalValue = tensorsArray->GetTuple9(iNumPts);
152          sumEigenvalue1 = sqrt (normalValue[0]*normalValue[0] + normalValue[1]*normalValue[1] + normalValue[2]*normalValue[2]);
153          sumEigenvalue2 = sqrt (normalValue[3]*normalValue[3] + normalValue[4]*normalValue[4] + normalValue[5]*normalValue[5]);
154          sumEigenvalue3 = sqrt (normalValue[6]*normalValue[6] + normalValue[7]*normalValue[7] + normalValue[8]*normalValue[8]);
155
156                         if ((sumEigenvalue1>=sumEigenvalue2) && (sumEigenvalue1>=sumEigenvalue3))
157                         {
158                                 sumEigenvalue=sumEigenvalue1;
159                                 idEigen=0;
160                         }
161                         if ((sumEigenvalue2>sumEigenvalue1) && (sumEigenvalue2>=sumEigenvalue3))
162                         {
163                                 sumEigenvalue=sumEigenvalue2;
164                                 idEigen=3;
165                         }
166                         if ((sumEigenvalue3>=sumEigenvalue1) && (sumEigenvalue3>=sumEigenvalue2))
167                         {
168                                 sumEigenvalue=sumEigenvalue3;
169                                 idEigen=6;
170                         }
171
172                         dirx = (unsigned char)( 255*fabs(normalValue[idEigen+0]/sumEigenvalue) );
173                         diry = (unsigned char)( 255*fabs(normalValue[idEigen+1]/sumEigenvalue) );
174                         dirz = (unsigned char)( 255*fabs(normalValue[idEigen+2]/sumEigenvalue) );
175
176                         for (iNumSourcePts=0;iNumSourcePts<numSourcePts;iNumSourcePts++)
177                         {
178 //                              newScalarArray->InsertTuple3( iNormalsEED, dirx,diry,dirz);
179                                 newScalarArray->InsertTuple3( iNormalsEED, normalValue[idEigen+0],normalValue[idEigen+1],normalValue[idEigen+2] );
180                                 iNormalsEED++;
181                         } // for iNumSourcePts
182
183                 } // for iNumPts
184                 pointdata_tg->RemoveArray( "NormalsEED" );
185                 pointdata_tg->AddArray( newScalarArray );
186 //              pointdata_pdn->Update();
187                 tg->Update();
188         } // if  tensorsArray
189
190
191
192         _pdm->ScalarVisibilityOn();
193         _pdm->SetColorModeToMapScalars();
194         //_pdm->SetColorModeToDefault();
195         // _pdm->SetColorModeToDirectScalars();  // NOT compile
196         _pdm->SetScalarModeToUsePointFieldData();
197         _pdm->ImmediateModeRenderingOn();
198
199         _LutEED->SetVectorModeToComponent();
200         //_LutEED->SetVectorModeToRGBColors();
201         //_LutEED->SetVectorModeToMagnitud();
202         _pdm->SetLookupTable( _LutEED );
203         _pdm->SelectColorArray( "NormalsEED" );
204
205
206 /*
207                         int i,sizeDa = pointdata_tg->GetNumberOfArrays();
208                         printf("EED vtkTensorsVisu::Process  sizeDa %d \n", sizeDa );
209                         for (i=0;i<sizeDa;i++)
210                         {
211                                 int faces = pointdata_tg->GetArray(i)->GetSize() / numPnts;
212 //pointdata_pdn->GetArray(i)->Print( std::cout );
213                                 printf("EED vtkTensorsVisu::Process-name %d, %s, size=%d   %d \n", i, pointdata_tg->GetArrayName(i) , pointdata_tg->GetArray(i)->GetSize(), faces );
214
215                         }
216 */
217
218
219   } // _active
220
221     VisibilityActor(); 
222
223 }
224