]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkStreamLineScalarSeg.cpp
#2453 creaVtk Feature New Normal - Stream Lines segmentation by Scalars
[creaVtk.git] / lib / creaVtk / creaVtkStreamLineScalarSeg.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 "creaVtkStreamLineScalarSeg.h"
29 #include "vtkPointData.h"
30 #include "vtkDataArray.h"
31 #include "vtkDoubleArray.h"
32
33
34 // ------------------------------------------------------------------------
35 creaVtkStreamLineScalarSeg::creaVtkStreamLineScalarSeg()
36 {
37         _vtkidlist                      = vtkIdList::New();
38    _newvtkpolydata      = vtkPolyData::New();   
39    _newvtkcellarray     = vtkCellArray::New();  
40 }
41
42 // ------------------------------------------------------------------------
43 creaVtkStreamLineScalarSeg::~creaVtkStreamLineScalarSeg()
44 {
45 }
46
47 // ------------------------------------------------------------------------
48 void creaVtkStreamLineScalarSeg::SetScalarStreamLinesIn( vtkPolyData* polydata)
49 {
50         _StreamLinesIn = polydata;
51 }
52
53 // ------------------------------------------------------------------------
54 void creaVtkStreamLineScalarSeg::SetScalarArrayName( std::string name)
55 {
56         _ScalarArrayName = name;
57 }
58
59 // ------------------------------------------------------------------------
60 void creaVtkStreamLineScalarSeg::SetThresholdLower( double value)
61 {
62         _ThresholdLower = value;        
63 }
64
65 // ------------------------------------------------------------------------
66 void creaVtkStreamLineScalarSeg::SetThresholdUpper( double value )
67 {
68         _ThresholdUpper = value;        
69 }
70
71 // ------------------------------------------------------------------------
72 void creaVtkStreamLineScalarSeg::Process()
73 {
74         if (_ScalarArrayName=="")
75         {
76                 printf(".\n ");
77                 printf(".\n ");
78                 printf("BBTK Warnning! ScalarArrayName is empty in creaVtkStreamLineScalarSeg \n");
79                 printf(".\n ");
80                 printf(".\n ");
81         }
82
83         if ((_StreamLinesIn!=NULL) && (_ScalarArrayName!="") )
84         {
85                 int iLine;
86
87                 _newvtkcellarray->Initialize();
88
89         vtkCellArray    *vtkcellarray           = _StreamLinesIn->GetLines();
90         int                             nLinesCell                      = vtkcellarray->GetNumberOfCells(); 
91 //      int                             vtkcellarraysize        = vtkcellarray->GetSize();
92         int                             numberofids; 
93         int                             ii;
94                 vtkDataArray    *scalarArray = _StreamLinesIn->GetPointData()->GetArray( _ScalarArrayName.c_str() );
95
96
97                 vtkDoubleArray  *newScalarArray                         = vtkDoubleArray::New();
98                 newScalarArray->SetName( _ScalarArrayName.c_str() );            
99                 newScalarArray->SetNumberOfComponents(1); 
100
101         // FOR EACH LINE
102                 double scalarValue;
103                 long int iLineNew=0;
104         ii=0;
105         for ( iLine=0 ; iLine<nLinesCell ; iLine++ )
106         {
107                         vtkcellarray->GetCell(ii, _vtkidlist );
108                         numberofids = _vtkidlist->GetNumberOfIds();
109                         scalarValue = scalarArray->GetTuple1(iLine);
110                         if ( ( scalarValue>=_ThresholdLower ) && ( scalarValue<=_ThresholdUpper ) )
111                         {
112                                 _newvtkcellarray->InsertNextCell( _vtkidlist );
113                                 newScalarArray->InsertTuple1( iLineNew, scalarValue );
114                                 iLineNew++;
115                         }
116
117                         ii=ii+numberofids+1;
118         } // for iLine
119
120                 _newvtkpolydata->SetPoints( _StreamLinesIn->GetPoints() );
121                 _newvtkpolydata->SetLines( _newvtkcellarray );
122                 int iArrays,arraysSize = _StreamLinesIn->GetPointData()->GetNumberOfArrays();
123                 for ( iArrays=0 ; iArrays<arraysSize ; iArrays++ )
124                 {       
125                         _newvtkpolydata->GetPointData()->AddArray( _StreamLinesIn->GetPointData()->GetArray(iArrays) );
126                 }               
127                 _newvtkpolydata->GetPointData()->RemoveArray( _ScalarArrayName.c_str() );
128                 _newvtkpolydata->GetPointData()->AddArray( newScalarArray );
129                 _newvtkpolydata->Update();
130
131         _StreamLinesOut = _newvtkpolydata;
132
133         } // _StreamLinesIn
134
135 }
136
137 // ------------------------------------------------------------------------
138 vtkPolyData* creaVtkStreamLineScalarSeg::GetStreamLinesOut()
139 {
140         return _StreamLinesOut;
141 }
142
143