Salome HOME
Fix problem with highlighting polyhedrons
[modules/smesh.git] / src / OBJECT / SMESH_SVTKActor.cxx
1 // Copyright (C) 2007-2013  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.
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
43 #include <Utils_SALOME_Exception.hxx>
44
45 vtkStandardNewMacro(SMESH_SVTKActor);
46
47 /*!
48   Constructor
49 */
50 SMESH_SVTKActor::SMESH_SVTKActor():
51   my0DGrid(vtkUnstructuredGrid::New()),
52   myBallGrid(vtkUnstructuredGrid::New())
53 {
54   my0DActor = SVTK_DeviceActor::New();
55   myBallActor = SVTK_DeviceActor::New();
56
57   myBallActor->SetResolveCoincidentTopology(false);
58   myBallActor->SetCoincident3DAllowed(true);
59   myBallActor->PickableOff();
60   my0DActor->SetResolveCoincidentTopology(false);
61   my0DActor->SetCoincident3DAllowed(true);
62   my0DActor->PickableOff();
63
64   my0DGrid->Allocate();
65   myBallGrid->Allocate();
66 }
67
68 /*!
69   Constructor
70 */
71 SMESH_SVTKActor::~SMESH_SVTKActor() {
72   my0DActor->Delete();
73   myBallActor->Delete();
74   my0DGrid->Delete();
75   myBallGrid->Delete();
76 }
77
78 /*!
79   Publishes the actor in all its internal devices
80 */
81 void SMESH_SVTKActor::AddToRender(vtkRenderer* theRenderer) {
82   Superclass::AddToRender(theRenderer);
83   float a0D = my0DActor->GetProperty()->GetPointSize();
84   float aBall = myBallActor->GetProperty()->GetPointSize();
85   my0DActor->GetProperty()->DeepCopy(GetProperty());
86   myBallActor->GetProperty()->DeepCopy(GetProperty());
87   my0DActor->GetProperty()->SetPointSize(a0D);
88   myBallActor->GetProperty()->SetPointSize(aBall);
89   theRenderer->AddActor(my0DActor);
90   theRenderer->AddActor(myBallActor);
91   
92 }
93
94 /*!
95   Removes the actor from all its internal devices
96 */
97 void 
98 SMESH_SVTKActor
99 ::RemoveFromRender(vtkRenderer* theRenderer)
100 {
101   Superclass::RemoveFromRender(theRenderer);
102   theRenderer->RemoveActor( myBallActor );
103   theRenderer->RemoveActor( my0DActor );
104 }
105
106 void
107 SMESH_SVTKActor
108 ::MapCells(SALOME_Actor* theMapActor,
109            const TColStd_IndexedMapOfInteger& theMapIndex)
110 {
111   myUnstructuredGrid->Initialize();
112   myUnstructuredGrid->Allocate();
113
114   my0DGrid->Initialize();
115   my0DGrid->Allocate();
116
117   myBallGrid->Initialize();
118   myBallGrid->Allocate();
119
120   vtkDataSet *aSourceDataSet = theMapActor->GetInput();
121   SVTK::CopyPoints( GetSource(), aSourceDataSet );
122   SVTK::CopyPoints( myBallGrid, aSourceDataSet );
123   SVTK::CopyPoints( my0DGrid,    aSourceDataSet );
124
125   int aNbOfParts = theMapIndex.Extent();
126   for(int ind = 1; ind <= aNbOfParts; ind++){
127     int aPartId = theMapIndex( ind );
128     if(vtkCell* aCell = theMapActor->GetElemCell(aPartId))
129     {
130       if (aCell->GetCellType() != VTK_POLYHEDRON)
131       {
132         if(aCell->GetCellType() == VTK_VERTEX ) {
133           my0DGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
134         } else if(aCell->GetCellType() == VTK_POLY_VERTEX ) {
135           myBallGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
136         } else {
137           myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
138         }
139       }
140       else
141       {
142         vtkPolyhedron *polyhedron = dynamic_cast<vtkPolyhedron*>(aCell);
143         if (!polyhedron)
144           throw SALOME_Exception(LOCALIZED ("not a polyhedron"));
145         vtkIdType *pts = polyhedron->GetFaces();
146         myUnstructuredGrid->InsertNextCell(aCell->GetCellType(),pts[0], pts+1);
147       }
148     }
149   }
150
151   UnShrink();
152   if(theMapActor->IsShrunk()){
153     SetShrinkFactor(theMapActor->GetShrinkFactor());
154     SetShrink();
155   }
156
157   myMapIndex = theMapIndex;
158 }
159
160 void
161 SMESH_SVTKActor
162 ::Initialize()
163 {
164   Superclass::Initialize();
165   my0DActor->SetInputData(my0DGrid);
166   myBallActor->SetInputData(myBallGrid);
167 }
168
169
170 void SMESH_SVTKActor::SetVisibility( int theVisibility ) {
171   Superclass::SetVisibility( theVisibility );  
172   my0DActor->SetVisibility( theVisibility );
173   myBallActor->SetVisibility( theVisibility );
174 }
175
176
177 void SMESH_SVTKActor::Set0DSize(float theSize) {
178   my0DActor->GetProperty()->SetPointSize(theSize);  
179 }
180
181 void SMESH_SVTKActor::SetBallSize(float theSize) {
182   myBallActor->GetProperty()->SetPointSize(theSize);
183 }
184