]> SALOME platform Git repositories - modules/paravis.git/blob - src/Plugins/CADRepresentation/cadRepresentations/CADRepresentations/vtkCADRepresentation.cxx
Salome HOME
updated copyright message
[modules/paravis.git] / src / Plugins / CADRepresentation / cadRepresentations / CADRepresentations / vtkCADRepresentation.cxx
1 // Copyright (C) 2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "vtkCADMapper.h"
21 #include "vtkCADRepresentation.h"
22
23 #include <vtkActor.h>
24 #include <vtkCellData.h>
25 #include <vtkDataObject.h>
26 #include <vtkInformation.h>
27 #include <vtkInformationVector.h>
28 #include <vtkObjectFactory.h>
29 #include <vtkPolyData.h>
30 #include <vtkPVRenderView.h>
31 #include <vtkPVView.h>
32 #include <vtkRenderer.h>
33 #include <vtkSelection.h>
34
35 #include <vtkMultiProcessController.h>
36 #include <vtkAlgorithmOutput.h>
37 #include <vtkSMSession.h>
38 #include <vtkProcessModule.h>
39 #include <vtkSelectionNode.h>
40
41 vtkStandardNewMacro(vtkCADRepresentation);
42 //----------------------------------------------------------------------------
43 vtkCADRepresentation::vtkCADRepresentation()
44 {
45   this->Actor->SetMapper(this->Mapper);
46
47   vtkNew<vtkSelection> sel;
48   this->Mapper->SetSelection(sel);
49
50   this->SetArrayIdNames(nullptr, nullptr);
51 }
52
53 //----------------------------------------------------------------------------
54 int vtkCADRepresentation::ProcessViewRequest(vtkInformationRequestKey* request_type, vtkInformation* inInfo,
55   vtkInformation* outInfo)
56 {
57   if (!this->Superclass::ProcessViewRequest(request_type, inInfo, outInfo))
58   {
59     return 0;
60   }
61
62   if (request_type == vtkPVView::REQUEST_UPDATE())
63   {
64     vtkPVRenderView::SetPiece(inInfo, this, this->PolyData);
65     vtkPVRenderView::SetGeometryBounds(inInfo, this, this->PolyData->GetBounds());
66   }
67   else if (request_type == vtkPVView::REQUEST_RENDER())
68   {
69     vtkAlgorithmOutput* producerPort = vtkPVRenderView::GetPieceProducer(inInfo, this);
70     this->Mapper->SetInputConnection(producerPort);
71
72     if(!this->IsInitialized)
73     {
74       this->Mapper->Initialize();
75       this->IsInitialized = true;
76     }
77   }
78
79   return 1;
80 }
81
82 //------------------------------------------------------------------------------
83 void vtkCADRepresentation::SetVisibility(bool value)
84 {
85   this->Superclass::SetVisibility(value);
86   this->Actor->SetVisibility(value);
87 }
88
89 //----------------------------------------------------------------------------
90 void vtkCADRepresentation::BeginSelect()
91 {
92   if(this->IsInitialized)
93   {
94     this->Mapper->BeginSelect();
95   }
96 }
97
98 //----------------------------------------------------------------------------
99 void vtkCADRepresentation::EndSelect()
100 {
101   if(this->IsInitialized)
102   {
103     this->Mapper->EndSelect();
104   }
105 }
106
107 //----------------------------------------------------------------------------
108 void vtkCADRepresentation::CreateGroup()
109 {
110   if(this->IsInitialized)
111   {
112     this->Mapper->CreateGroup();
113   }
114 }
115
116 //----------------------------------------------------------------------------
117 void vtkCADRepresentation::SetGroupVisibility(int groupIdx, bool visibility)
118 {
119   if(this->IsInitialized)
120   {
121     this->Mapper->SetGroupVisibility(groupIdx, visibility);
122   }
123 }
124
125 //----------------------------------------------------------------------------
126 void vtkCADRepresentation::SetGroupOpacity(int groupIdx, unsigned char opacity)
127 {
128   if(this->IsInitialized)
129   {
130     this->Mapper->SetGroupOpacity(groupIdx, opacity);
131   }
132 }
133
134 //----------------------------------------------------------------------------
135 void vtkCADRepresentation::SetGroupModeEnabled(bool enabled)
136 {
137   if(this->IsInitialized)
138   {
139     this->Mapper->SetGroupModeEnabled(enabled);
140   }
141 }
142
143 //----------------------------------------------------------------------------
144 void vtkCADRepresentation::Reset()
145 {
146   if(this->IsInitialized)
147   {
148     this->Mapper->ResetSelection();
149   }
150 }
151
152 //------------------------------------------------------------------------------
153 int vtkCADRepresentation::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
154 {
155   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
156
157   // Saying INPUT_IS_OPTIONAL() is essential, since representations don't have
158   // any inputs on client-side (in client-server, client-render-server mode) and
159   // render-server-side (in client-render-server mode).
160   info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
161
162   return 1;
163 }
164
165 //------------------------------------------------------------------------------
166 int vtkCADRepresentation::RequestData(
167   vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
168 {
169   if (inputVector[0]->GetNumberOfInformationObjects() == 1)
170   {
171     vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
172     vtkPolyData* polyData = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
173     this->PolyData->ShallowCopy(polyData);
174   }
175   else
176   {
177     this->PolyData->Initialize();
178   }
179
180   return this->Superclass::RequestData(request, inputVector, outputVector);
181 }
182
183 //------------------------------------------------------------------------------
184 bool vtkCADRepresentation::AddToView(vtkView* view)
185 {
186   vtkPVRenderView* rview = vtkPVRenderView::SafeDownCast(view);
187   if (rview)
188   {
189     rview->GetRenderer()->AddActor(this->Actor);
190
191     // Indicate that this is prop that we are rendering when hardware selection
192     // is enabled.
193     rview->RegisterPropForHardwareSelection(this, this->Actor);
194     return this->Superclass::AddToView(view);
195   }
196   return false;
197 }
198
199 //------------------------------------------------------------------------------
200 bool vtkCADRepresentation::RemoveFromView(vtkView* view)
201 {
202   vtkPVRenderView* rview = vtkPVRenderView::SafeDownCast(view);
203   if (rview)
204   {
205     rview->GetRenderer()->RemoveActor(this->Actor);
206     rview->UnRegisterPropForHardwareSelection(this, this->Actor);
207     return this->Superclass::RemoveFromView(view);
208   }
209   return false;
210 }
211
212 //----------------------------------------------------------------------------
213 void vtkCADRepresentation::PrintSelf(ostream& os, vtkIndent indent)
214 {
215   this->Superclass::PrintSelf(os, indent);
216 }
217
218 //----------------------------------------------------------------------------
219 void vtkCADRepresentation::SetSelectionConnection(vtkAlgorithmOutput* input)
220 {
221   // Copied from vtkCompositeRepresentation
222   if (!input)
223   {
224     return;
225   }
226
227   vtkMultiProcessController* controller = vtkMultiProcessController::GetGlobalController();
228   int numPiece = controller->GetNumberOfProcesses();
229   int piece = controller->GetLocalProcessId();
230   input->GetProducer()->UpdatePiece(piece, numPiece, 0);
231
232   vtkSmartPointer<vtkSelection> sel;
233   int actualNbPieces = numPiece;
234
235   vtkSMSession* session =
236     vtkSMSession::SafeDownCast(vtkProcessModule::GetProcessModule()->GetSession());
237   if (session)
238   {
239     actualNbPieces = session->GetNumberOfProcesses(vtkPVSession::DATA_SERVER);
240   }
241
242   // in order to handle the case where we are connected to a parallel server using
243   // local rendering, we have to compare the number of processes here
244   if (numPiece < actualNbPieces && piece == 0)
245   {
246     vtkSelection* localSel =
247       vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
248     sel = vtkSmartPointer<vtkSelection>::New();
249     sel->ShallowCopy(localSel);
250
251     for (int i = 1; i < actualNbPieces; i++)
252     {
253       input->GetProducer()->UpdatePiece(i, actualNbPieces, 0);
254       localSel = vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
255       if (localSel->GetNumberOfNodes() > 1)
256       {
257         vtkWarningMacro("Only the first node of a selection will be considered.");
258       }
259       vtkSelectionNode* node = localSel->GetNode(0);
260       node->GetProperties()->Set(vtkSelectionNode::PROCESS_ID(), i);
261       sel->AddNode(localSel->GetNode(0));
262     }
263   }
264   else
265   {
266     sel = vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
267     if (sel->GetNumberOfNodes() > 1)
268     {
269       vtkWarningMacro("Only the first node of a selection will be considered.");
270     }
271     sel->GetNode(0)->GetProperties()->Set(vtkSelectionNode::PROCESS_ID(), piece);
272   }
273
274   this->Mapper->GetSelection()->ShallowCopy(sel);
275 }
276
277 //----------------------------------------------------------------------------
278 void vtkCADRepresentation::SetArrayIdNames(const char* pointArray, const char* cellArray)
279 {
280   vtkCADMapper* mapper = vtkCADMapper::SafeDownCast(this->Mapper);
281   mapper->SetPointIdArrayName(pointArray ? pointArray : "vtkOriginalPointIds");
282   mapper->SetCellIdArrayName(cellArray ? cellArray : "vtkOriginalCellIds");
283 }