Salome HOME
Merge from V5_1_main 10/06/2010
[modules/visu.git] / src / PIPELINE / VISU_VectorsPL.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU OBJECT : interactive object for VISU entities implementation
24 // File:    VISU_PipeLine.cxx
25 // Author:  Alexey PETROV
26 // Module : VISU
27 //
28 #include "VISU_VectorsPL.hxx"
29 #include "VISU_FieldTransform.hxx"
30 #include "VISU_PipeLineUtils.hxx"
31 #include "VISU_UsedPointsFilter.hxx"
32 #include "VTKViewer_TransformFilter.h"
33 #include "VTKViewer_Transform.h"
34
35 #include <vtkGlyph3D.h>
36 #include <vtkConeSource.h>
37 #include <vtkLineSource.h>
38 #include <vtkGlyphSource2D.h>
39 #include <vtkPolyData.h>
40
41
42 //----------------------------------------------------------------------------
43 vtkStandardNewMacro(VISU_VectorsPL);
44
45 //----------------------------------------------------------------------------
46 template<class TOutputFilter>
47 void ToCellCenters( TOutputFilter *theOutputFilter, 
48                     vtkCellCenters *theCellCenters,
49                     vtkDataSet* theDataSet,
50                     VISU_UsedPointsFilter* theUsedPointsFilter )
51 {
52   if ( VISU::IsDataOnCells( theDataSet ) ) {
53     theCellCenters->SetInput( theDataSet );
54     theCellCenters->VertexCellsOn();
55     theOutputFilter->SetInput( theCellCenters->GetOutput() );
56   }else {
57     theUsedPointsFilter->SetInput( theDataSet );
58     theOutputFilter->SetInput( theUsedPointsFilter->GetOutput() );
59   }
60 }
61
62
63 VISU_VectorsPL
64 ::VISU_VectorsPL()
65 {
66   SetIsShrinkable(false);
67   SetIsFeatureEdgesAllowed(false);
68
69   myGlyph = vtkGlyph3D::New();
70
71   myGlyphSource = vtkGlyphSource2D::New();
72   myConeSource = vtkConeSource::New();
73   myLineSource = vtkLineSource::New();
74
75   myCenters = vtkCellCenters::New();
76   myTransformFilter = VTKViewer_TransformFilter::New();
77
78   myUsedPointsFilter = VISU_UsedPointsFilter::New();
79 }
80
81
82 //----------------------------------------------------------------------------
83 VISU_VectorsPL
84 ::~VISU_VectorsPL()
85 {
86   myGlyph->Delete();
87
88   myCenters->Delete();
89
90   myGlyphSource->Delete();
91
92   myConeSource->Delete();
93
94   myLineSource->Delete();
95
96   myTransformFilter->Delete();
97   
98   myUsedPointsFilter->Delete();
99 }
100
101
102 //----------------------------------------------------------------------------
103 unsigned long int 
104 VISU_VectorsPL
105 ::GetMTime()
106 {
107   unsigned long int aTime = Superclass::GetMTime();
108
109   aTime = std::max(aTime, myGlyph->GetMTime());
110   aTime = std::max(aTime, myCenters->GetMTime());
111   aTime = std::max(aTime, myGlyphSource->GetMTime());
112   aTime = std::max(aTime, myConeSource->GetMTime());
113   aTime = std::max(aTime, myLineSource->GetMTime());
114   aTime = std::max(aTime, myTransformFilter->GetMTime());
115
116   return aTime;
117 }
118
119
120 //----------------------------------------------------------------------------
121 void
122 VISU_VectorsPL
123 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
124                 bool theIsCopyInput)
125 {
126   Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
127
128   if(VISU_VectorsPL *aPipeLine = dynamic_cast<VISU_VectorsPL*>(thePipeLine)){
129     SetGlyphType(aPipeLine->GetGlyphType());
130     SetGlyphPos(aPipeLine->GetGlyphPos());
131   }
132 }
133
134
135 //----------------------------------------------------------------------------
136 void
137 VISU_VectorsPL
138 ::SetTransform( VTKViewer_Transform* theTransform )
139 {
140   myTransformFilter->SetTransform( theTransform );
141   myTransformFilter->Modified();
142 }
143
144
145 //----------------------------------------------------------------------------
146 VTKViewer_Transform* 
147 VISU_VectorsPL
148 ::GetTransform()
149 {
150   return static_cast< VTKViewer_Transform* >( myTransformFilter->GetTransform() );
151 }
152
153
154 //----------------------------------------------------------------------------
155 void
156 VISU_VectorsPL
157 ::SetScale(vtkFloatingPointType theScale) 
158 {
159   if ( VISU::CheckIsSameValue( myScaleFactor, theScale ) )
160     return;
161
162   myGlyph->SetScaleFactor( theScale );
163
164   myScaleFactor = theScale;
165
166   Modified();
167 }
168
169
170 //----------------------------------------------------------------------------
171 vtkFloatingPointType
172 VISU_VectorsPL
173 ::GetScale() 
174 {
175   return myGlyph->GetScaleFactor();
176 }
177
178
179 //----------------------------------------------------------------------------
180 void
181 VISU_VectorsPL
182 ::SetGlyphType(VISU_VectorsPL::GlyphType theType) 
183 {
184   if(myTypeGlyph == theType)
185     return;
186
187   myTypeGlyph = theType;
188   Modified();
189 }
190
191
192 //----------------------------------------------------------------------------
193 VISU_VectorsPL::GlyphType
194 VISU_VectorsPL
195 ::GetGlyphType() const
196 {
197   return myTypeGlyph;
198 }
199
200
201 //----------------------------------------------------------------------------
202 void
203 VISU_VectorsPL
204 ::SetGlyphPos(VISU_VectorsPL::GlyphPos thePos) 
205 {
206   if(myPosGlyph == thePos)
207     return;
208
209   myPosGlyph = thePos;
210   Modified();
211 }
212
213
214 //----------------------------------------------------------------------------
215 VISU_VectorsPL::GlyphPos
216 VISU_VectorsPL
217 ::GetGlyphPos() const
218 {
219   return myPosGlyph;
220 }
221
222
223 //----------------------------------------------------------------------------
224 void
225 VISU_VectorsPL
226 ::Init()
227 {
228   Superclass::Init();
229
230   SetGlyphType(ARROW);
231   SetGlyphPos(TAIL);
232 }
233
234
235 //----------------------------------------------------------------------------
236 void
237 VISU_VectorsPL
238 ::Build()
239 {
240   Superclass::Build();
241   
242   ToCellCenters( myTransformFilter,
243                  myCenters,
244                  GetMergedInput(),
245                  myUsedPointsFilter );
246
247   myGlyph->SetInput( myTransformFilter->GetOutput() );
248   myGlyph->SetVectorModeToUseVector();
249   myGlyph->SetScaleModeToScaleByVector();
250   myGlyph->SetColorModeToColorByScalar();
251 }
252
253
254 //----------------------------------------------------------------------------
255 vtkDataSet* 
256 VISU_VectorsPL
257 ::InsertCustomPL()
258 {
259   return myGlyph->GetOutput();
260 }
261
262
263 //----------------------------------------------------------------------------
264 void
265 VISU_VectorsPL
266 ::Update()
267 {
268   switch (myTypeGlyph) {
269   case ARROW: {
270     myGlyphSource->SetGlyphTypeToArrow();
271     myGlyphSource->SetFilled(0);
272     switch (myPosGlyph) {
273     case TAIL:
274       myGlyphSource->SetCenter(0.5, 0.0, 0.0);
275       break;
276     case HEAD:
277       myGlyphSource->SetCenter(-0.5, 0.0, 0.0);
278       break;
279     case CENTER:
280       myGlyphSource->SetCenter(0.0, 0.0, 0.0);
281     }
282     myGlyph->SetSource(myGlyphSource->GetOutput());
283   }
284     break;
285   case CONE2:
286   case CONE6: {
287     if (myTypeGlyph == CONE2)
288       myConeSource->SetResolution(3);
289     else
290       myConeSource->SetResolution(7);
291     myConeSource->SetHeight(1.0);
292     myConeSource->SetRadius(.1);
293
294     switch (myPosGlyph) {
295     case TAIL:
296       myConeSource->SetCenter(0.5, 0.0, 0.0);
297       break;
298     case HEAD:
299       myConeSource->SetCenter(-0.5, 0.0, 0.0);
300       break;
301     case CENTER:
302       myConeSource->SetCenter(0.0, 0.0, 0.0);
303     }
304     myGlyph->SetSource(myConeSource->GetOutput());
305   }
306     break;
307   case NONE:
308   default: {
309     myGlyph->SetSource(myLineSource->GetOutput());
310   }
311   }
312
313   Superclass::Update();
314 }
315
316
317 //----------------------------------------------------------------------------
318 unsigned long int
319 VISU_VectorsPL
320 ::GetMemorySize()
321 {
322   unsigned long int aSize = Superclass::GetMemorySize();
323
324   if(vtkDataSet* aDataSet = myGlyph->GetOutput())
325     aSize += aDataSet->GetActualMemorySize() * 1024;
326   
327   if(vtkDataSet* aDataSet = myCenters->GetOutput())
328     aSize += aDataSet->GetActualMemorySize() * 1024;
329
330   return aSize;
331 }
332
333
334 //----------------------------------------------------------------------------
335 vtkDataSet* 
336 VISU_VectorsPL
337 ::GetOutput()
338 {
339   myGlyph->Update();
340
341   return myGlyph->GetOutput();
342 }
343
344
345 //----------------------------------------------------------------------------
346 void
347 VISU_VectorsPL
348 ::SetMapScale(vtkFloatingPointType theMapScale)
349 {
350   VISU_ScalarMapPL::SetMapScale(theMapScale);
351   myMapScaleFactor = theMapScale;
352
353   myGlyph->SetScaleFactor( myScaleFactor*theMapScale );
354
355   Modified();
356 }
357
358
359 //----------------------------------------------------------------------------