| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #ifndef REMOTEAR2_SCENE_MANAGER_HPP
- #define REMOTEAR2_SCENE_MANAGER_HPP
- #include "sophiar_connect.h"
- #include <vtkActor.h>
- #include <vtkMatrix4x4.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkProperty.h>
- #include <vtkSmartPointer.h>
- #include <vtkSTLReader.h>
- #include <vector>
- struct scene_manager {
- struct actor_info {
- vtkSmartPointer<vtkActor> actor;
- vtkSmartPointer<vtkMatrix4x4> pose;
- int pose_variable_id = -1; // -1 will not be updated
- };
- using actor_pool_type = std::vector<actor_info>;
- actor_pool_type actor_pool;
- sophiar::variable_io *var_io = nullptr;
- static void transform_to_vtk_matrix(const sophiar::transform_type &trans, vtkMatrix4x4 *matrix) {
- if (matrix == nullptr) return;
- for (int i = 0; i < 4; ++i) {
- for (int j = 0; j < 4; ++j) {
- matrix->SetElement(i, j, trans(i, j));
- }
- }
- matrix->Modified();
- }
- vtkActor *add_actor(std::string_view file_name, int variable_id) {
- vtkNew<vtkSTLReader> reader;
- reader->SetFileName(file_name.data());
- reader->Update();
- vtkNew<vtkPolyDataMapper> mapper;
- mapper->SetInputData(reader->GetOutput());
- vtkNew<vtkActor> actor;
- actor->SetMapper(mapper);
- vtkNew<vtkMatrix4x4> pose_matrix;
- actor->SetUserMatrix(pose_matrix);
- auto &next_actor = actor_pool.emplace_back();
- next_actor.actor = actor;
- next_actor.pose = pose_matrix;
- next_actor.pose_variable_id = variable_id;
- return actor;
- }
- void update_pose() {
- assert(var_io != nullptr);
- for (auto &actor: actor_pool) {
- assert(actor.pose_variable_id != -1);
- auto next_pose = var_io->query_transform_variable(actor.pose_variable_id);
- if (next_pose.has_value()) {
- transform_to_vtk_matrix(*next_pose.value(), actor.pose);
- actor.actor->SetVisibility(true);
- } else {
- actor.actor->SetVisibility(false);
- }
- actor.actor->Modified();
- }
- }
- };
- #endif //REMOTEAR2_SCENE_MANAGER_HPP
|