Salome HOME
#24666 [CEA][Windows] BLSURPLUGIN compilation issue
[modules/smesh.git] / src / OBJECT / SMESH_SVTKActor.cxx
1 // Copyright (C) 2007-2021  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 //  SMESH OBJECT : interactive object for SMESH visualization
24 //  File   : SMESH_SVTKActor.cxx
25 //  Author : Roman NIKOLAEV
26 //  Module : SMESH
27 //
28
29 #include "SMESH_SVTKActor.h"
30
31 #include <SVTK_Utils.h>
32 #include <SALOME_Actor.h>
33
34 #include <SVTK_DeviceActor.h>
35 #include <vtkPoints.h>
36 #include <vtkRenderer.h>
37 #include <vtkObjectFactory.h>
38 #include <vtkUnstructuredGrid.h>
39 #include <vtkCell.h>
40 #include <vtkDataSetMapper.h>
41 #include <vtkPolyhedron.h>
42 #include <vtkCellData.h>
43
44 #include <Utils_SALOME_Exception.hxx>
45
46 vtkStandardNewMacro(SMESH_SVTKActor)
47
48 /*!
49   Constructor
50 */
51 SMESH_SVTKActor::SMESH_SVTKActor():
52   my0DGrid(vtkUnstructuredGrid::New()),
53   myBallGrid(vtkUnstructuredGrid::New())
54 {
55   my0DActor = SVTK_DeviceActor::New();
56   myBallActor = SVTK_DeviceActor::New();
57   myBallActor->SetBallEnabled(true);
58
59   myBallActor->SetResolveCoincidentTopology(false);
60   myBallActor->SetCoincident3DAllowed(true);
61   myBallActor->PickableOff();
62   my0DActor->SetResolveCoincidentTopology(false);
63   my0DActor->SetCoincident3DAllowed(true);
64   my0DActor->PickableOff();
65
66   my0DGrid->Allocate();
67   myBallGrid->Allocate();
68 }
69
70 /*!
71   Constructor
72 */
73 SMESH_SVTKActor::~SMESH_SVTKActor() {
74   my0DActor->Delete();
75   myBallActor->Delete();
76   my0DGrid->Delete();
77   myBallGrid->Delete();
78 }
79
80 /*!
81   Publishes the actor in all its internal devices
82 */
83 void SMESH_SVTKActor::AddToRender(vtkRenderer* theRenderer) {
84   Superclass::AddToRender(theRenderer);
85   float a0D = my0DActor->GetProperty()->GetPointSize();
86   float aBall = myBallActor->GetProperty()->GetPointSize();
87   my0DActor->GetProperty()->DeepCopy(GetProperty());
88   myBallActor->GetProperty()->DeepCopy(GetProperty());
89   my0DActor->GetProperty()->SetPointSize(a0D);
90   myBallActor->GetProperty()->SetPointSize(aBall);
91   theRenderer->AddActor(my0DActor);
92   theRenderer->AddActor(myBallActor);
93   
94 }
95
96 /*!
97   Removes the actor from all its internal devices
98 */
99 void 
100 SMESH_SVTKActor
101 ::RemoveFromRender(vtkRenderer* theRenderer)
102 {
103   Superclass::RemoveFromRender(theRenderer);
104   theRenderer->RemoveActor( myBallActor );
105   theRenderer->RemoveActor( my0DActor );
106 }
107
108 void
109 SMESH_SVTKActor
110 ::MapCells(SALOME_Actor* theMapActor,
111            const SVTK_TIndexedMapOfVtkId& theMapIndex)
112 {
113   myUnstructuredGrid->Initialize();
114   myUnstructuredGrid->Allocate();
115
116   my0DGrid->Initialize();
117   my0DGrid->Allocate();
118
119   myBallGrid->Initialize();
120   myBallGrid->Allocate();
121
122   vtkUnstructuredGrid * aSourceGrid = (vtkUnstructuredGrid *)theMapActor->GetInput();
123   GetSource()->SetPoints( aSourceGrid->GetPoints() );
124   myBallGrid->SetPoints( aSourceGrid->GetPoints() );
125   my0DGrid->SetPoints( aSourceGrid->GetPoints() );
126
127   int aNbOfParts = theMapIndex.Extent();
128
129   vtkCellData* cd = 0;
130   vtkCellData* outputCD = 0;
131   //Copy deamaters of the balls
132   if(myVisualObj) {
133     outputCD = myBallGrid->GetCellData();
134     cd = aSourceGrid->GetCellData();
135   }
136   outputCD->CopyAllocate(cd,aNbOfParts,aNbOfParts/2);
137   for(int ind = 1; ind <= aNbOfParts; ind++)
138   {
139     vtkIdType aPartId = theMapIndex( ind );
140     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
141     {
142       if (aCell->GetCellType() != VTK_POLYHEDRON)
143       {
144         if(aCell->GetCellType() == VTK_VERTEX ) {
145           my0DGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
146         }
147         else if(aCell->GetCellType() == VTK_POLY_VERTEX ) {
148           vtkIdType newCellId = myBallGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
149           if(myVisualObj) {
150             outputCD->CopyData(cd, myVisualObj->GetElemVTKId(aPartId), newCellId);
151           }
152         }
153         else {
154           myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
155         }
156       }
157       else
158       {
159         vtkPolyhedron *polyhedron = dynamic_cast<vtkPolyhedron*>(aCell);
160         if (!polyhedron)
161           throw SALOME_Exception(LOCALIZED ("not a polyhedron"));
162         vtkIdType *pts = polyhedron->GetFaces();
163         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),pts[0], pts+1);
164       }
165     }
166   }
167
168   UnShrink();
169   if(theMapActor->IsShrunk()){
170     SetShrinkFactor(theMapActor->GetShrinkFactor());
171     SetShrink();
172   }
173
174   myMapIndex = theMapIndex;
175 }
176
177 void
178 SMESH_SVTKActor
179 ::Initialize()
180 {
181   Superclass::Initialize();
182   my0DActor->SetInputData(my0DGrid);
183   myBallActor->SetInputData(myBallGrid);
184 }
185
186
187 void SMESH_SVTKActor::SetVisibility( int theVisibility ) {
188   Superclass::SetVisibility( theVisibility );  
189   my0DActor->SetVisibility( theVisibility );
190   myBallActor->SetVisibility( theVisibility );
191 }
192
193
194 void SMESH_SVTKActor::Set0DSize(float theSize) {
195   my0DActor->GetProperty()->SetPointSize(theSize);  
196 }
197
198 void SMESH_SVTKActor::SetBallSize(float theSize) {
199   myBallActor->GetProperty()->SetPointSize(theSize);
200 }
201
202 void SMESH_SVTKActor::SetBallScale(double theScale) {
203   myBallActor->SetBallScale(theScale);
204 }
205
206 void SMESH_SVTKActor::SetVisualObject(TVisualObjPtr theVisualObj) {
207   myVisualObj = theVisualObj;
208 }
209