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