Salome HOME
Merge branch 'master' into pre/penta18
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_IdPreview.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include "SMESHGUI_IdPreview.h"
24
25 #include <SALOME_Actor.h>
26 #include <SMDS_Mesh.hxx>
27 #include <SVTK_ViewWindow.h>
28
29 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
30
31 #include <vtkActor2D.h>
32 #include <vtkDataSetMapper.h>
33 #include <vtkLabeledDataMapper.h>
34 #include <vtkMaskPoints.h>
35 #include <vtkPointData.h>
36 #include <vtkProperty2D.h>
37 #include <vtkRenderer.h>
38 #include <vtkSelectVisiblePoints.h>
39 #include <vtkTextProperty.h>
40 #include <vtkUnstructuredGrid.h>
41
42 // Extracted from SMESHGUI_MergeDlg.cxx
43
44 SMESHGUI_IdPreview::SMESHGUI_IdPreview(SVTK_ViewWindow* theViewWindow):
45   myViewWindow(theViewWindow)
46 {
47   myIdGrid = vtkUnstructuredGrid::New();
48
49   // Create and display actor
50   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
51   aMapper->SetInputData( myIdGrid );
52
53   myIdActor = SALOME_Actor::New();
54   myIdActor->SetInfinitive(true);
55   myIdActor->VisibilityOff();
56   myIdActor->PickableOff();
57
58   myIdActor->SetMapper( aMapper );
59   aMapper->Delete();
60
61   myViewWindow->AddActor(myIdActor);
62
63   //Definition of points numbering pipeline
64   myPointsNumDataSet = vtkUnstructuredGrid::New();
65
66   myPtsMaskPoints = vtkMaskPoints::New();
67   myPtsMaskPoints->SetInputData(myPointsNumDataSet);
68   myPtsMaskPoints->SetOnRatio(1);
69
70   myPtsSelectVisiblePoints = vtkSelectVisiblePoints::New();
71   myPtsSelectVisiblePoints->SetInputConnection(myPtsMaskPoints->GetOutputPort());
72   myPtsSelectVisiblePoints->SelectInvisibleOff();
73   myPtsSelectVisiblePoints->SetTolerance(0.1);
74     
75   myPtsLabeledDataMapper = vtkLabeledDataMapper::New();
76   myPtsLabeledDataMapper->SetInputConnection(myPtsSelectVisiblePoints->GetOutputPort());
77   myPtsLabeledDataMapper->SetLabelModeToLabelScalars();
78     
79   vtkTextProperty* aPtsTextProp = vtkTextProperty::New();
80   aPtsTextProp->SetFontFamilyToTimes();
81   static int aPointsFontSize = 12;
82   aPtsTextProp->SetFontSize(aPointsFontSize);
83   aPtsTextProp->SetBold(1);
84   aPtsTextProp->SetItalic(0);
85   aPtsTextProp->SetShadow(0);
86   myPtsLabeledDataMapper->SetLabelTextProperty(aPtsTextProp);
87   aPtsTextProp->Delete();
88   
89   myIsPointsLabeled = false;
90
91   myPointLabels = vtkActor2D::New();
92   myPointLabels->SetMapper(myPtsLabeledDataMapper);
93   myPointLabels->GetProperty()->SetColor(1,1,1);
94   myPointLabels->SetVisibility(myIsPointsLabeled);
95
96   AddToRender(myViewWindow->getRenderer());
97 }
98
99 void SMESHGUI_IdPreview::SetPointsData ( SMDS_Mesh*                   theMesh,
100                                          const TColStd_MapOfInteger & theNodesIdMap )
101 {
102   vtkPoints* aPoints = vtkPoints::New();
103   aPoints->SetNumberOfPoints(theNodesIdMap.Extent());
104   myIDs.clear();
105
106   TColStd_MapIteratorOfMapOfInteger idIter( theNodesIdMap );
107   for( int i = 0; idIter.More(); idIter.Next(), i++ )
108   {
109     const SMDS_MeshNode* aNode = theMesh->FindNode(idIter.Key());
110     aPoints->SetPoint( i, aNode->X(), aNode->Y(), aNode->Z() );
111     myIDs.push_back(idIter.Key());
112   }
113
114   myIdGrid->SetPoints(aPoints);
115
116   aPoints->Delete();
117
118   myIdActor->GetMapper()->Update();
119 }
120
121 void SMESHGUI_IdPreview::SetElemsData( const std::vector<int> & theElemsIdMap,
122                                        const std::list<gp_XYZ> & aGrCentersXYZ )
123 {
124   vtkPoints* aPoints = vtkPoints::New();
125   aPoints->SetNumberOfPoints( theElemsIdMap.size() );
126   myIDs = theElemsIdMap;
127
128   std::list<gp_XYZ>::const_iterator coordIt = aGrCentersXYZ.begin();
129   for( int i = 0; coordIt != aGrCentersXYZ.end(); coordIt++, i++ )
130     aPoints->SetPoint( i, coordIt->X(), coordIt->Y(), coordIt->Z() );
131
132   myIdGrid->SetPoints(aPoints);
133   aPoints->Delete();
134
135   myIdActor->GetMapper()->Update();
136 }
137
138 void SMESHGUI_IdPreview::AddToRender(vtkRenderer* theRenderer)
139 {
140   myIdActor->AddToRender(theRenderer);
141
142   myPtsSelectVisiblePoints->SetRenderer(theRenderer);
143   theRenderer->AddActor2D(myPointLabels);
144 }
145
146 void SMESHGUI_IdPreview::RemoveFromRender(vtkRenderer* theRenderer)
147 {
148   myIdActor->RemoveFromRender(theRenderer);
149
150   myPtsSelectVisiblePoints->SetRenderer(theRenderer);
151   theRenderer->RemoveActor(myPointLabels);
152 }
153
154 void SMESHGUI_IdPreview::SetPointsLabeled( bool theIsPointsLabeled, bool theIsActorVisible )
155 {
156   myIsPointsLabeled = theIsPointsLabeled && myIdGrid->GetNumberOfPoints();
157
158   if ( myIsPointsLabeled ) {
159     myPointsNumDataSet->ShallowCopy(myIdGrid);
160     vtkDataSet *aDataSet = myPointsNumDataSet;
161     int aNbElem = myIDs.size();
162     vtkIntArray *anArray = vtkIntArray::New();
163     anArray->SetNumberOfValues( aNbElem );
164     for ( int i = 0; i < aNbElem; i++ )
165       anArray->SetValue( i, myIDs[i] );
166     aDataSet->GetPointData()->SetScalars( anArray );
167     anArray->Delete();
168     myPtsMaskPoints->SetInputData( aDataSet );
169     myPointLabels->SetVisibility( theIsActorVisible );
170   }
171   else {
172     myPointLabels->SetVisibility( false );
173   }
174 }
175
176 SMESHGUI_IdPreview::~SMESHGUI_IdPreview()
177 {
178   RemoveFromRender(myViewWindow->getRenderer());
179
180   myIdGrid->Delete();
181
182   myViewWindow->RemoveActor(myIdActor);
183   myIdActor->Delete();
184
185   //Deleting of points numbering pipeline
186   //---------------------------------------
187   myPointsNumDataSet->Delete();
188
189   //myPtsLabeledDataMapper->RemoveAllInputs();        //vtk 5.0 porting
190   myPtsLabeledDataMapper->Delete();
191
192   //myPtsSelectVisiblePoints->UnRegisterAllOutputs(); //vtk 5.0 porting
193   myPtsSelectVisiblePoints->Delete();
194
195   //myPtsMaskPoints->UnRegisterAllOutputs();          //vtk 5.0 porting
196   myPtsMaskPoints->Delete();
197
198   myPointLabels->Delete();
199
200   //       myTimeStamp->Delete();
201 }