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