scene_manager.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef REMOTEAR2_SCENE_MANAGER_HPP
  2. #define REMOTEAR2_SCENE_MANAGER_HPP
  3. #include "sophiar_connect.h"
  4. #include <vtkActor.h>
  5. #include <vtkMatrix4x4.h>
  6. #include <vtkPolyDataMapper.h>
  7. #include <vtkProperty.h>
  8. #include <vtkSmartPointer.h>
  9. #include <vtkSTLReader.h>
  10. #include <vector>
  11. struct scene_manager {
  12. struct actor_info {
  13. vtkSmartPointer<vtkActor> actor;
  14. vtkSmartPointer<vtkMatrix4x4> pose;
  15. int pose_variable_id = -1; // -1 will not be updated
  16. };
  17. using actor_pool_type = std::vector<actor_info>;
  18. actor_pool_type actor_pool;
  19. sophiar::variable_io *var_io = nullptr;
  20. static void transform_to_vtk_matrix(const sophiar::transform_type &trans, vtkMatrix4x4 *matrix) {
  21. if (matrix == nullptr) return;
  22. for (int i = 0; i < 4; ++i) {
  23. for (int j = 0; j < 4; ++j) {
  24. matrix->SetElement(i, j, trans(i, j));
  25. }
  26. }
  27. matrix->Modified();
  28. }
  29. vtkActor *add_actor(std::string_view file_name, int variable_id) {
  30. vtkNew<vtkSTLReader> reader;
  31. reader->SetFileName(file_name.data());
  32. reader->Update();
  33. vtkNew<vtkPolyDataMapper> mapper;
  34. mapper->SetInputData(reader->GetOutput());
  35. vtkNew<vtkActor> actor;
  36. actor->SetMapper(mapper);
  37. vtkNew<vtkMatrix4x4> pose_matrix;
  38. actor->SetUserMatrix(pose_matrix);
  39. auto &next_actor = actor_pool.emplace_back();
  40. next_actor.actor = actor;
  41. next_actor.pose = pose_matrix;
  42. next_actor.pose_variable_id = variable_id;
  43. return actor;
  44. }
  45. void update_pose() {
  46. assert(var_io != nullptr);
  47. for (auto &actor: actor_pool) {
  48. assert(actor.pose_variable_id != -1);
  49. auto next_pose = var_io->query_transform_variable(actor.pose_variable_id);
  50. if (next_pose.has_value()) {
  51. transform_to_vtk_matrix(*next_pose.value(), actor.pose);
  52. actor.actor->SetVisibility(true);
  53. } else {
  54. actor.actor->SetVisibility(false);
  55. }
  56. actor.actor->Modified();
  57. }
  58. }
  59. };
  60. #endif //REMOTEAR2_SCENE_MANAGER_HPP