]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISUPipeLine.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / PIPELINE / VISUPipeLine.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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 // File:    VISU_PipeLine.hxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26
27 #include "VISU_MeshPL.hxx"
28 #include "VISU_ScalarMapPL.hxx"
29 #include "VISU_IsoSurfacesPL.hxx"
30 #include "VISU_CutPlanesPL.hxx"
31 #include "VISU_CutLinesPL.hxx"
32 #include "VISU_DeformedShapePL.hxx"
33 #include "VISU_VectorsPL.hxx"
34 #include "VISU_StreamLinesPL.hxx"
35 #include "VISU_GaussPointsPL.hxx"
36 #include "VISU_ScalarMapOnDeformedShapePL.hxx"
37 #include "VISU_Plot3DPL.hxx"
38 #include "VISU_ScalarBarActor.hxx"
39
40 #include "VISU_OpenGLPointSpriteMapper.hxx"
41 #include "VTKViewer_GeometryFilter.h"
42 #include <vtkMaskPoints.h>
43
44 #include "VISU_Convertor.hxx"
45 #include "VISU_ConvertorUtils.hxx"
46
47 typedef VISU_GaussPointsPL TPresent;
48
49 #include <vtkUnstructuredGrid.h>
50 #include <vtkDataSetMapper.h>
51
52 #include <vtkInteractorStyleTrackballCamera.h>
53 #include <vtkRenderWindowInteractor.h>
54 #include <vtkRenderWindow.h>
55 #include <vtkImageData.h>
56 #include <vtkRenderer.h>
57 #include <vtkCamera.h>
58 #include <vtkActor.h>
59 #include <vtk3DWidget.h>
60
61 #include <vtkProperty.h>
62
63 static int isOnlyMesh = false;
64
65 //#define _DEBUG_ID_MAPPING_
66
67 //----------------------------------------------------------------------------
68 template<class TPipeLine>
69 VISU_ColoredPL*
70 CreateColoredPL(VISU_Convertor* theConvertor,
71                 const std::string& theMeshName,
72                 const VISU::TEntity& theEntity,
73                 const std::string& theFieldName,
74                 int theTimeStampNumber);
75
76
77 //----------------------------------------------------------------------------
78 template<>
79 VISU_ColoredPL*
80 CreateColoredPL<VISU_GaussPointsPL>(VISU_Convertor* theConvertor,
81                                     const std::string& theMeshName,
82                                     const VISU::TEntity& theEntity,
83                                     const std::string& theFieldName,
84                                     int theTimeStampNumber)
85 {
86   VISU_GaussPointsPL* aPresent = VISU_GaussPointsPL::New();
87   VISU::PGaussPtsIDMapper aGaussPtsIDMapper = 
88     theConvertor->GetTimeStampOnGaussPts(theMeshName,
89                                          theEntity,
90                                          theFieldName,
91                                          theTimeStampNumber);
92   aPresent->SetGaussPtsIDMapper(aGaussPtsIDMapper);
93
94   char aMainTexture[80];
95   strcpy( aMainTexture, getenv( "VISU_ROOT_DIR" ) );
96   strcat( aMainTexture, "/share/salome/resources/visu/sprite_texture.bmp" );
97   
98   char anAlphaTexture[80];
99   strcpy( anAlphaTexture, getenv( "VISU_ROOT_DIR" ) );
100   strcat( anAlphaTexture, "/share/salome/resources/visu/sprite_alpha.bmp" );
101   
102   vtkSmartPointer<vtkImageData> aTextureValue = 
103     VISU_GaussPointsPL::MakeTexture( aMainTexture, anAlphaTexture );
104   aPresent->SetImageData( aTextureValue.GetPointer() );
105
106   aPresent->Update();
107
108 #ifdef _DEBUG_ID_MAPPING_
109   vtkDataSet* aDataSet = aPresent->GetOutput();
110   aDataSet->Update();
111   int aNbCells = aDataSet->GetNumberOfCells();
112   cout<<"aNbCells = "<<aNbCells<<endl;
113   for(int anCellId = 0; anCellId < aNbCells; anCellId++){
114     vtkIdType anObjID = aPresent->GetNodeObjID(anCellId);
115     vtkIdType aVtkID = aPresent->GetNodeVTKID(anObjID);
116     cout<<anObjID<<"; "<<aVtkID<<"; - ";
117     vtkFloatingPointType* aCoord = aPresent->GetNodeCoord(anObjID);
118     cout<<aCoord[0]<<"; "<<aCoord[1]<<"; "<<aCoord[2]<<endl;
119   }
120 #endif
121
122   return aPresent;
123 }
124
125
126 //----------------------------------------------------------------------------
127 template<class TPipeLine>
128 VISU_ColoredPL*
129 CreateColoredPL(VISU_Convertor* theConvertor,
130                 const std::string& theMeshName,
131                 const VISU::TEntity& theEntity,
132                 const std::string& theFieldName,
133                 int theTimeStampNumber)
134 {
135   TPipeLine* aPresent = TPipeLine::New();
136   VISU::PUnstructuredGridIDMapper anUnstructuredGridIDMapper = 
137     theConvertor->GetTimeStampOnMesh(theMeshName,
138                                      theEntity,
139                                      theFieldName,
140                                      theTimeStampNumber);
141   aPresent->SetUnstructuredGridIDMapper(anUnstructuredGridIDMapper);
142
143   //aPresent->ClearGeometry();
144   //{
145   //  VISU::PUnstructuredGridIDMapper anIDMapper = 
146   //    theConvertor->GetMeshOnGroup(theMeshName,
147   //                               "groupe1");
148   //  aPresent->AddGeometry(anIDMapper->GetOutput());
149   //}
150   //{
151   //  VISU::PUnstructuredGridIDMapper anIDMapper = 
152   //    theConvertor->GetMeshOnGroup(theMeshName,
153   //                               "TUBEH                                                                           ");
154   //  aPresent->AddGeometry(anIDMapper->GetOutput());
155   //}
156   //{
157   //  VISU::PUnstructuredGridIDMapper anIDMapper = 
158   //    theConvertor->GetMeshOnGroup(theMeshName,
159   //                               "STRI                                                                            ");
160   //  aPresent->AddGeometry(anIDMapper->GetOutput());
161   //}
162   aPresent->Update();
163   return aPresent;
164 }
165
166
167 //----------------------------------------------------------------------------
168 int
169 main(int argc, char** argv)
170 {
171   try{
172     if(argc > 1){
173       vtkRenderWindow *aWindow = vtkRenderWindow::New();
174       vtkRenderer *aRenderer = vtkRenderer::New();
175
176       aWindow->AddRenderer(aRenderer);
177       aRenderer->GetActiveCamera()->ParallelProjectionOn();
178
179       vtkRenderWindowInteractor *anInteractor = vtkRenderWindowInteractor::New();
180       anInteractor->SetRenderWindow(aWindow);
181       aWindow->Delete();
182
183       vtkInteractorStyle* aStyle = vtkInteractorStyleTrackballCamera::New();
184       anInteractor->SetInteractorStyle(aStyle);
185       aStyle->Delete();
186
187       VISU_Convertor* aConvertor = CreateConvertor(argv[1]);
188       aConvertor->BuildEntities();
189       aConvertor->BuildGroups();
190       aConvertor->BuildFields();
191       aConvertor->BuildMinMax();
192       const VISU::TMeshMap& aMeshMap = aConvertor->GetMeshMap();
193       VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
194       if(aMeshMapIter == aMeshMap.end()) return 0;
195       const std::string& aMeshName = aMeshMapIter->first;
196       const VISU::PMesh aMesh = aMeshMapIter->second;
197       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
198       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
199       if(isOnlyMesh){
200         const VISU::TEntity& anEntity = VISU::CELL_ENTITY;
201         aMeshOnEntityMapIter = aMeshOnEntityMap.find(anEntity);
202
203         VISU::PNamedIDMapper anIDMapper = 
204           aConvertor->GetMeshOnEntity(aMeshName,anEntity);
205
206         VISU_MeshPL* aPresent = VISU_MeshPL::New();
207         aPresent->SetUnstructuredGridIDMapper(anIDMapper);
208
209         vtkActor* aActor = vtkActor::New();
210         aActor->SetMapper(aPresent->GetMapper());
211         aActor->GetProperty()->SetRepresentation(VTK_WIREFRAME);
212         //aRenderer->ResetCameraClippingRange();
213
214         aRenderer->AddActor(aActor);
215
216         aWindow->Render();
217         anInteractor->Start();
218         return 0;
219       }
220       //Import fields
221       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
222       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++) {
223         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
224         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
225         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
226         VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
227         for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
228           const VISU::PField aField = aFieldMapIter->second;
229 //        if(aField->myNbComp == 1) 
230 //          continue;
231           const std::string& aFieldName = aFieldMapIter->first;
232           const VISU::TValField& aValField = aField->myValField;
233           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
234           if(aValFieldIter == aValField.end()) return 0;
235           int aTimeStamp = aValFieldIter->first;
236
237           vtkActor* anActor = vtkActor::New();
238           VISU_ColoredPL* aPresent = NULL;
239           if(anEntity != VISU::NODE_ENTITY){
240             aPresent = CreateColoredPL<TPresent>(aConvertor,
241                                                  aMeshName,
242                                                  anEntity,
243                                                  aFieldName,
244                                                  aTimeStamp);
245
246             anActor->SetMapper(aPresent->GetMapper());
247           }else{
248             continue;
249             aPresent = CreateColoredPL<TPresent>(aConvertor,
250                                                  aMeshName,
251                                                  anEntity,
252                                                  aFieldName,
253                                                  aTimeStamp);
254
255             VTKViewer_GeometryFilter* aGeometryFilter = VTKViewer_GeometryFilter::New();
256             aGeometryFilter->SetInput(aPresent->GetOutput());
257             aGeometryFilter->SetInside(true);
258
259             vtkMaskPoints* aMaskPoints = vtkMaskPoints::New();
260             aMaskPoints->SetInput(aGeometryFilter->GetOutput());
261             aMaskPoints->SetGenerateVertices(true);
262             aMaskPoints->SetOnRatio(1);
263
264             VISU_OpenGLPointSpriteMapper* aMapper = VISU_OpenGLPointSpriteMapper::New();
265             aMapper->SetAverageCellSize( VISU_DeformedShapePL::GetScaleFactor( aPresent->GetOutput() ) );
266   
267             char aMainTexture[80];
268             strcpy( aMainTexture, getenv( "VISU_ROOT_DIR" ) );
269             strcat( aMainTexture, "/share/salome/resources/visu/sprite_texture.vti" );
270             
271             char anAlphaTexture[80];
272             strcpy( anAlphaTexture, getenv( "VISU_ROOT_DIR" ) );
273             strcat( anAlphaTexture, "/share/salome/resources/visu/sprite_alpha.vti" );
274             
275             vtkSmartPointer<vtkImageData> aTextureValue = 
276               VISU_GaussPointsPL::MakeTexture( aMainTexture, anAlphaTexture );
277             aMapper->SetImageData( aTextureValue.GetPointer() );
278
279             //vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
280             aMapper->SetLookupTable(aPresent->GetMapperTable());
281             aMapper->SetUseLookupTableScalarRange(true);
282             aMapper->SetColorModeToMapScalars();
283             aMapper->SetScalarVisibility(true);
284
285             aMapper->SetInput(aMaskPoints->GetOutput());
286             aGeometryFilter->Delete();
287
288             anActor->SetMapper(aMapper);
289             aMapper->Delete();
290           }
291
292           VISU_ScalarBarActor * aScalarBar = VISU_ScalarBarActor::New();
293           aScalarBar->SetLookupTable(aPresent->GetBarTable());
294
295           aRenderer->AddActor(anActor);
296           aRenderer->AddActor2D(aScalarBar);
297
298           aWindow->Render();
299           aRenderer->ResetCamera();
300
301           anInteractor->Start();
302           return 0;
303         }
304       }
305     }
306   }catch(std::exception& exc){
307     MSG(true, "Follow exception was occured :\n"<<exc.what());
308   }catch(...){
309     MSG(true, "Unknown exception was occured in VISU_Convertor_impl");
310   }
311   return 1;
312 }