]> Creatis software - creaVtk.git/blob - lib/creaVtk/vtkVectorsTensorsVisuBase.cpp
VectorField 2D view
[creaVtk.git] / lib / creaVtk / vtkVectorsTensorsVisuBase.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 "vtkVectorsTensorsVisuBase.h"
29 #include "vtkObjectFactory.h"
30
31 vtkStandardNewMacro(vtkLookupTableDirectionVector);
32
33 // Construct with range=(0,1); and hsv ranges set up for rainbow color table 
34 // (from red to blue).
35 vtkLookupTableDirectionVector::vtkLookupTableDirectionVector(int sze, int ext)
36 {
37         this->TableRange[0] = 0.0;
38         this->TableRange[1] = 1.0;
39 }
40
41 //----------------------------------------------------------------------------
42 vtkLookupTableDirectionVector::~vtkLookupTableDirectionVector()
43 {
44 }
45
46 unsigned char *vtkLookupTableDirectionVector::MapValue(double v)
47 {
48         
49         //int idx = this->GetIndex(v);
50         //return (this->Table->GetPointer(0) + 4*idx);  
51         return 0;
52 }
53
54 void vtkLookupTableDirectionVector::GetColor(double v, double rgb[3])
55 {
56 //      unsigned char *rgb8 = this->MapValue(v);
57 //      rgb[0] = rgb8[0]/255.0;
58 //      rgb[1] = rgb8[1]/255.0;
59 //      rgb[2] = rgb8[2]/255.0;
60
61         rgb[0] = 1;
62         rgb[1] = 1;
63         rgb[2] = 0;
64 }
65
66
67 void vtkLookupTableDirectionVector::SetTableRange(double r[2])
68 {
69         this->SetTableRange(r[0],r[1]);
70 }
71
72 //----------------------------------------------------------------------------
73 // Set the minimum/maximum scalar values for scalar mapping. Scalar values
74 // less than minimum range value are clamped to minimum range value.
75 // Scalar values greater than maximum range value are clamped to maximum
76 // range value.
77 void vtkLookupTableDirectionVector::SetTableRange(double rmin, double rmax)
78 {
79         if (rmax < rmin)
80     {
81                 vtkErrorMacro("Bad table range: ["<<rmin<<", "<<rmax<<"]");
82                 return;
83     }
84         
85         if (this->TableRange[0] == rmin && this->TableRange[1] == rmax)
86     {
87                 return;
88     }
89         
90         this->TableRange[0] = rmin;
91         this->TableRange[1] = rmax;
92         
93         this->Modified();
94 }
95
96
97
98 //----------------------------------------------------------------------------
99 // Although this is a relatively expensive calculation,
100 // it is only done on the first render. Colors are cached
101 // for subsequent renders.
102 template<class T>
103 void vtkLookupTableMapDirVectorEED(vtkLookupTableDirectionVector *self, T *input, 
104                           unsigned char *output, int length, 
105                           int inIncr, int outFormat)
106 {
107         double tmp,sum;
108 //      double *mag;
109         int i, j;
110         double dirx,diry,dirz;
111         
112         
113 inIncr=3;
114 //      printf("EED length %d  %p\n", length,input);
115 //      mag = new double[length];
116         for (i = 0; i < length; ++i)
117     {
118                 dirx    = 0;
119                 diry    = 0;
120                 dirz    = 0;
121                 sum     = 0;
122                 for (j = 0; j < inIncr; ++j)
123                 {
124                         if (j==0) dirx= static_cast<T>(*input);  
125                         if (j==1) diry= static_cast<T>(*input);  
126                         if (j==2) dirz= static_cast<T>(*input);  
127                         tmp = static_cast<T>(*input);  
128                         sum += (tmp * tmp);
129                         ++input;
130                 }
131                 sum=sqrt(sum);
132                 *output++ = (unsigned char) abs( (255*dirx/sum) );
133                 *output++ = (unsigned char) abs( (255*diry/sum) );
134                 *output++ = (unsigned char) abs( (255*dirz/sum) );
135                 *output++ = 255;
136 //              printf("%d %d %d   ",(int)(255*dirx/sum),(int)(255*diry/sum),(int)(255*dirz/sum));
137 //              printf(" C     %d        %f %f %f   \n",inIncr,dirx,diry,dirz);
138     }
139         
140 //      vtkLookupTableMapData(self, mag, output, length, 1, outFormat);
141         
142 //      delete [] mag;
143 }
144
145
146 //----------------------------------------------------------------------------
147 void vtkLookupTableDirectionVector::MapScalarsThroughTable2(void *input, 
148                                              unsigned char *output,
149                                              int inputDataType, 
150                                              int numberOfValues,
151                                              int inputIncrement,
152                                              int outputFormat)
153 {
154 //  printf("vtkLookupTableEED::MapScalarsThroughTable2 inputIncrement=%d   inputDataType=%d\n",inputIncrement,inputDataType);
155 // if (this->UseMagnitude && inputIncrement > 1)
156 // {
157      switch (inputDataType)
158      {
159        vtkTemplateMacro( vtkLookupTableMapDirVectorEED(this,static_cast<VTK_TT*>(input),output,
160                          numberOfValues,inputIncrement,outputFormat);
161                          return 
162                         );
163         case VTK_BIT:
164             vtkErrorMacro("Cannot comput magnitude of bit array.");
165             break;
166         default:
167             vtkErrorMacro(<< "MapImageThroughTable: Unknown input ScalarType");
168      } /// switch
169 // } //if
170 }  
171
172
173 //----------------------------------------------------------------------------
174 void vtkLookupTableDirectionVector::PrintSelf(ostream& os, vtkIndent indent)
175 {
176         this->Superclass::PrintSelf(os,indent); 
177 }
178
179
180
181
182 //----------------------------------
183 //----------------------------------
184 //----------------------------------
185 //----------------------------------
186 //----------------------------------
187
188 vtkVectorsTensorsVisuBase::vtkVectorsTensorsVisuBase()
189 {
190      _firsttime         = true;
191         _active                 = false;
192         _scalefactor    = 1;
193         _opacity                = 1;
194         _dataobject             = NULL;
195         _renderer               = NULL;
196         _typeForm               = 0;
197         _orientation    = 0;
198
199         _LutEED         = vtkLookupTableDirectionVector::New();
200         //_LutEED->SetVectorMode(0);
201         //_LutEED->SetVectorModeToMagnitude();
202         //_LutEED->SetVectorModeToComponent();
203         _LutEED->SetVectorModeToRGBColors();
204
205         _pdm                    = vtkPolyDataMapper::New();
206         _actor                  = vtkActor::New();
207         _actorAdded             = false;
208 }
209
210 vtkVectorsTensorsVisuBase::~vtkVectorsTensorsVisuBase()
211 {
212 }
213
214 //------------------------------------------------------------------------
215 void vtkVectorsTensorsVisuBase::SetDataObject(vtkDataObject *dataobject)
216 {
217         _dataobject = dataobject;
218 }
219
220 //------------------------------------------------------------------------
221 vtkDataObject* vtkVectorsTensorsVisuBase::GetDataObject()
222 {
223         return _dataobject;
224 }
225
226 //------------------------------------------------------------------------
227 void vtkVectorsTensorsVisuBase::SetRenderer(vtkRenderer *renderer)
228 {
229         _renderer=renderer;
230 }
231
232 //------------------------------------------------------------------------
233 vtkRenderer* vtkVectorsTensorsVisuBase::GetRenderer()
234 {
235         return _renderer;
236 }
237
238 //------------------------------------------------------------------------
239 void vtkVectorsTensorsVisuBase::SetScaleFactor(double scalefactor)
240 {
241         _scalefactor=scalefactor;
242 }
243
244 //------------------------------------------------------------------------
245 double vtkVectorsTensorsVisuBase::GetScaleFactor()
246 {
247         return _scalefactor;
248 }
249
250 //------------------------------------------------------------------------
251 void vtkVectorsTensorsVisuBase::SetActive(bool active)
252 {
253    _active = active;
254 }
255
256 //------------------------------------------------------------------------
257 bool vtkVectorsTensorsVisuBase::GetActive()
258 {
259    return _active;
260 }
261
262 //------------------------------------------------------------------------
263 void vtkVectorsTensorsVisuBase::SetOpacity(double opacity)
264 {
265    _opacity = opacity;
266 }
267
268 //------------------------------------------------------------------------
269 double vtkVectorsTensorsVisuBase::GetOpacity()
270 {
271    return _opacity;
272 }
273
274
275
276 //------------------------------------------------------------------------
277 vtkProp3D* vtkVectorsTensorsVisuBase::GetProp3D()
278 {
279    return _actor;
280 }
281
282 //------------------------------------------------------------------------
283 void vtkVectorsTensorsVisuBase::SetTypeForm(int typeForm)
284 {
285      _typeForm=typeForm;
286 }
287
288
289 //------------------------------------------------------------------------
290 int vtkVectorsTensorsVisuBase::GetTypeForm()
291 {
292         return _typeForm;
293 }
294
295 //------------------------------------------------------------------------
296 void vtkVectorsTensorsVisuBase::Process()    // virtual
297 {
298 }
299
300 //------------------------------------------------------------------------------
301 void vtkVectorsTensorsVisuBase::VisibilityActor()
302 {
303
304         if ( (_active==true) && (_actorAdded==false) ){
305                 if (GetRenderer()!=NULL)
306                 {
307               GetRenderer()->AddActor(_actor);
308                    _actorAdded=true;
309        } // if Renderer
310         } // if _active==true 
311
312         if ( (_active==false) && (_actorAdded==true) ){
313                 if (GetRenderer()!=NULL)
314                 {
315               GetRenderer()->RemoveActor(_actor);
316                    _actorAdded=false;
317       } // if Renderer
318         } // if _active==false  
319 }
320
321 //------------------------------------------------------------------------------
322 void vtkVectorsTensorsVisuBase::SetColorLaw(int colorlaw)
323 {
324         _colorlaw = colorlaw;
325 }
326
327 //------------------------------------------------------------------------------
328 void vtkVectorsTensorsVisuBase::SetColor(std::vector<double> rgb)
329 {
330         if (rgb.size()==3)
331         {
332                 _colorR=rgb[0];
333                 _colorG=rgb[1];
334                 _colorB=rgb[2];
335         } else {
336                 _colorR=1;
337                 _colorG=1;
338                 _colorB=1;
339         }
340 }
341
342 //------------------------------------------------------------------------------
343 void vtkVectorsTensorsVisuBase::SetOrientation(int orientation)
344 {
345         _orientation = orientation;
346 }
347
348 //------------------------------------------------------------------------------
349 int vtkVectorsTensorsVisuBase::GetOrientation()
350 {
351         return _orientation;
352 }
353
354