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