1 // Copyright (C) 2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "vtkCADMapper.h"
21 #include "vtkCADRepresentation.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>
35 #include <vtkMultiProcessController.h>
36 #include <vtkAlgorithmOutput.h>
37 #include <vtkSMSession.h>
38 #include <vtkProcessModule.h>
39 #include <vtkSelectionNode.h>
41 vtkStandardNewMacro(vtkCADRepresentation);
42 //----------------------------------------------------------------------------
43 vtkCADRepresentation::vtkCADRepresentation()
45 this->Actor->SetMapper(this->Mapper);
47 vtkNew<vtkSelection> sel;
48 this->Mapper->SetSelection(sel);
50 this->SetArrayIdNames(nullptr, nullptr);
53 //----------------------------------------------------------------------------
54 int vtkCADRepresentation::ProcessViewRequest(vtkInformationRequestKey* request_type, vtkInformation* inInfo,
55 vtkInformation* outInfo)
57 if (!this->Superclass::ProcessViewRequest(request_type, inInfo, outInfo))
62 if (request_type == vtkPVView::REQUEST_UPDATE())
64 vtkPVRenderView::SetPiece(inInfo, this, this->PolyData);
65 vtkPVRenderView::SetGeometryBounds(inInfo, this, this->PolyData->GetBounds());
67 else if (request_type == vtkPVView::REQUEST_RENDER())
69 vtkAlgorithmOutput* producerPort = vtkPVRenderView::GetPieceProducer(inInfo, this);
70 this->Mapper->SetInputConnection(producerPort);
72 if(!this->IsInitialized)
74 this->Mapper->Initialize();
75 this->IsInitialized = true;
82 //------------------------------------------------------------------------------
83 void vtkCADRepresentation::SetVisibility(bool value)
85 this->Superclass::SetVisibility(value);
86 this->Actor->SetVisibility(value);
89 //----------------------------------------------------------------------------
90 void vtkCADRepresentation::BeginSelect()
92 if(this->IsInitialized)
94 this->Mapper->BeginSelect();
98 //----------------------------------------------------------------------------
99 void vtkCADRepresentation::EndSelect()
101 if(this->IsInitialized)
103 this->Mapper->EndSelect();
107 //----------------------------------------------------------------------------
108 void vtkCADRepresentation::CreateGroup()
110 if(this->IsInitialized)
112 this->Mapper->CreateGroup();
116 //----------------------------------------------------------------------------
117 void vtkCADRepresentation::SetGroupVisibility(int groupIdx, bool visibility)
119 if(this->IsInitialized)
121 this->Mapper->SetGroupVisibility(groupIdx, visibility);
125 //----------------------------------------------------------------------------
126 void vtkCADRepresentation::SetGroupOpacity(int groupIdx, unsigned char opacity)
128 if(this->IsInitialized)
130 this->Mapper->SetGroupOpacity(groupIdx, opacity);
134 //----------------------------------------------------------------------------
135 void vtkCADRepresentation::SetGroupModeEnabled(bool enabled)
137 if(this->IsInitialized)
139 this->Mapper->SetGroupModeEnabled(enabled);
143 //----------------------------------------------------------------------------
144 void vtkCADRepresentation::Reset()
146 if(this->IsInitialized)
148 this->Mapper->ResetSelection();
152 //------------------------------------------------------------------------------
153 int vtkCADRepresentation::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
155 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
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);
165 //------------------------------------------------------------------------------
166 int vtkCADRepresentation::RequestData(
167 vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
169 if (inputVector[0]->GetNumberOfInformationObjects() == 1)
171 vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
172 vtkPolyData* polyData = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
173 this->PolyData->ShallowCopy(polyData);
177 this->PolyData->Initialize();
180 return this->Superclass::RequestData(request, inputVector, outputVector);
183 //------------------------------------------------------------------------------
184 bool vtkCADRepresentation::AddToView(vtkView* view)
186 vtkPVRenderView* rview = vtkPVRenderView::SafeDownCast(view);
189 rview->GetRenderer()->AddActor(this->Actor);
191 // Indicate that this is prop that we are rendering when hardware selection
193 rview->RegisterPropForHardwareSelection(this, this->Actor);
194 return this->Superclass::AddToView(view);
199 //------------------------------------------------------------------------------
200 bool vtkCADRepresentation::RemoveFromView(vtkView* view)
202 vtkPVRenderView* rview = vtkPVRenderView::SafeDownCast(view);
205 rview->GetRenderer()->RemoveActor(this->Actor);
206 rview->UnRegisterPropForHardwareSelection(this, this->Actor);
207 return this->Superclass::RemoveFromView(view);
212 //----------------------------------------------------------------------------
213 void vtkCADRepresentation::PrintSelf(ostream& os, vtkIndent indent)
215 this->Superclass::PrintSelf(os, indent);
218 //----------------------------------------------------------------------------
219 void vtkCADRepresentation::SetSelectionConnection(vtkAlgorithmOutput* input)
221 // Copied from vtkCompositeRepresentation
227 vtkMultiProcessController* controller = vtkMultiProcessController::GetGlobalController();
228 int numPiece = controller->GetNumberOfProcesses();
229 int piece = controller->GetLocalProcessId();
230 input->GetProducer()->UpdatePiece(piece, numPiece, 0);
232 vtkSmartPointer<vtkSelection> sel;
233 int actualNbPieces = numPiece;
235 vtkSMSession* session =
236 vtkSMSession::SafeDownCast(vtkProcessModule::GetProcessModule()->GetSession());
239 actualNbPieces = session->GetNumberOfProcesses(vtkPVSession::DATA_SERVER);
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)
246 vtkSelection* localSel =
247 vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
248 sel = vtkSmartPointer<vtkSelection>::New();
249 sel->ShallowCopy(localSel);
251 for (int i = 1; i < actualNbPieces; i++)
253 input->GetProducer()->UpdatePiece(i, actualNbPieces, 0);
254 localSel = vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
255 if (localSel->GetNumberOfNodes() > 1)
257 vtkWarningMacro("Only the first node of a selection will be considered.");
259 vtkSelectionNode* node = localSel->GetNode(0);
260 node->GetProperties()->Set(vtkSelectionNode::PROCESS_ID(), i);
261 sel->AddNode(localSel->GetNode(0));
266 sel = vtkSelection::SafeDownCast(input->GetProducer()->GetOutputDataObject(0));
267 if (sel->GetNumberOfNodes() > 1)
269 vtkWarningMacro("Only the first node of a selection will be considered.");
271 sel->GetNode(0)->GetProperties()->Set(vtkSelectionNode::PROCESS_ID(), piece);
274 this->Mapper->GetSelection()->ShallowCopy(sel);
277 //----------------------------------------------------------------------------
278 void vtkCADRepresentation::SetArrayIdNames(const char* pointArray, const char* cellArray)
280 vtkCADMapper* mapper = vtkCADMapper::SafeDownCast(this->Mapper);
281 mapper->SetPointIdArrayName(pointArray ? pointArray : "vtkOriginalPointIds");
282 mapper->SetCellIdArrayName(cellArray ? cellArray : "vtkOriginalCellIds");