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