]> Creatis software - creaVtk.git/blob - lib/creaVtk/vtkStreamLineCreateColorInfo.cpp
#2453 creaVtk Feature New Normal - Stream Lines segmentation by Scalars
[creaVtk.git] / lib / creaVtk / vtkStreamLineCreateColorInfo.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 "vtkStreamLineCreateColorInfo.h"
29 #include "vtkIdList.h"
30 #include "vtkCellArray.h"
31 #include "vtkCharArray.h"
32 #include "vtkDoubleArray.h"
33 #include "vtkPointData.h"
34
35
36 vtkStreamLineCreateColorInfo::vtkStreamLineCreateColorInfo()
37 {
38         _StreamLinesIn = NULL;
39         _vtkidlist              = vtkIdList::New();
40
41 }
42
43 vtkStreamLineCreateColorInfo::~vtkStreamLineCreateColorInfo()
44 {
45 }
46
47 //---------------------------------------------
48 void vtkStreamLineCreateColorInfo::SetStreamLinesIn(vtkPolyData* streamlinein)
49 {
50         _StreamLinesIn = streamlinein;
51 }
52
53 //---------------------------------------------
54 vtkPolyData *vtkStreamLineCreateColorInfo::GetStreamLinesOut()
55 {
56         return _StreamLinesIn;
57 }
58
59 //---------------------------------------------
60 void vtkStreamLineCreateColorInfo::Run()
61 {
62         vtkCellArray    *vtkcellarray           = _StreamLinesIn->GetLines();
63         long int                nLinesCell                      = vtkcellarray->GetNumberOfCells(); 
64         long int                numberofids; 
65         long int                ii,jj,iLine;
66         double                  point1[3];
67         double                  point2[3];
68         long int                id,id1,id2;
69         double                  vx,vy,vz,mag;
70         double                  colorDirX,colorDirY,colorDirZ;
71
72         _StreamLinesIn->Update();
73         vtkCharArray    *mask                           = vtkCharArray::New();
74         vtkDoubleArray  *magnitud                       = vtkDoubleArray::New();
75         vtkDoubleArray  *velocity                       = vtkDoubleArray::New();
76         vtkDoubleArray  *colorDirection = vtkDoubleArray::New();
77
78         mask->SetName("creaMask");  // ... fill the colors array                        
79         mask->SetNumberOfComponents(1); //3d normals (ie x,y,z)
80         mask->SetNumberOfTuples(_StreamLinesIn->GetNumberOfPoints());
81
82         magnitud->SetName("creaMagnitud");  // ... fill the colors array                        
83         magnitud->SetNumberOfComponents(1); //3d normals (ie x,y,z)
84         magnitud->SetNumberOfTuples(_StreamLinesIn->GetNumberOfPoints());
85
86         velocity->SetName("creaVelocity");  // ... fill the colors array                        
87         velocity->SetNumberOfComponents(3); //3d normals (ie x,y,z)
88         velocity->SetNumberOfTuples(_StreamLinesIn->GetNumberOfPoints());
89
90         colorDirection->SetName("creaColorDirection");  // ... fill the colors array                    
91         colorDirection->SetNumberOfComponents(3); //3d normals (ie x,y,z)
92         colorDirection->SetNumberOfTuples(_StreamLinesIn->GetNumberOfPoints());
93
94
95         for (ii=0;ii<_StreamLinesIn->GetNumberOfPoints();ii++)
96         {
97                 mask->SetTuple1 (ii, 0);
98                 magnitud->SetTuple1 (ii, 0);
99                 velocity->SetTuple3 (ii, 0, 0, 0);
100                 colorDirection->SetTuple3 (ii, 0, 0, 0);
101         }
102
103         // FOR EACH LINE
104    ii=0;
105    for ( iLine=0 ; iLine<nLinesCell ; iLine++ )
106    {
107                 vtkcellarray->GetCell(ii, _vtkidlist );
108                 numberofids = _vtkidlist->GetNumberOfIds();
109         
110                 for (jj=0;jj<numberofids;jj++)
111                 {
112                         id = _vtkidlist->GetId(jj);
113                         if (jj-1>=0) {  id1 = _vtkidlist->GetId(jj-1); } else {id1=id;}
114                         if (jj+1<numberofids) {id2 = _vtkidlist->GetId(jj+1);} else {id2=id;}
115                         _StreamLinesIn->GetPoint( id1 ,point1);
116                         _StreamLinesIn->GetPoint( id2 ,point2);
117                         vx= point1[0]-point2[0];                
118                         vy= point1[1]-point2[1];
119                         vz= point1[2]-point2[2];
120                         mag= sqrt( vx*vx +vy*vy + vz*vz );
121                         colorDirX=fabs(vx/mag);
122                         colorDirY=fabs(vy/mag);
123                         colorDirZ=fabs(vz/mag);
124
125                 mask->SetTuple1 (id, 1);
126                 magnitud->SetTuple1 (id, mag);
127                 velocity->SetTuple3 (id, vx,vy,vz);
128                 colorDirection->SetTuple3 (id, colorDirX,colorDirY,colorDirZ);
129                 } // for jj
130
131                 ii=ii+numberofids+1;
132    } // for iLine
133
134         _StreamLinesIn->GetPointData()->AddArray( mask );
135         _StreamLinesIn->GetPointData()->AddArray( magnitud );
136         _StreamLinesIn->GetPointData()->AddArray( velocity );
137         _StreamLinesIn->GetPointData()->AddArray( colorDirection );
138
139 }
140
141 //---------------------------------------------
142 void vtkStreamLineCreateColorInfo::Process()
143 {
144         if (_StreamLinesIn!=NULL)
145         {
146                 Run();
147         }
148 }
149
150