virtual_object_handler.cpp
Go to the documentation of this file.
2 
4 
5 #include <fstream>
6 #include <sstream>
7 #include <stdexcept>
8 
10 
11 #include <nxLib.h>
12 
13 
14 using namespace ensenso_camera;
15 
16 namespace {
17  std::string readFile(const std::string &filename)
18  {
19  std::ifstream file{ filename };
20  if (!file.is_open() || !file.rdbuf()) {
21  throw std::runtime_error("Unable to read objects file");
22  }
23 
24  std::stringstream buffer;
25  buffer << file.rdbuf();
26  return buffer.str();
27  }
28 }
29 
30 VirtualObjectHandler::VirtualObjectHandler(const std::string &filename, const std::string &objectsFrame, const std::string &cameraFrame) :
31  objectsFrame(objectsFrame),
32  cameraFrame(cameraFrame)
33 {
34  nxLibInitialize(false);
35 
36  // Read the file contents and assign it to the objects tag
37  auto objects = NxLibItem{ itmObjects };
38  objects.setJson(readFile(filename));
39 
40  // Get the original poses from file
41  for (int i = 0; i < objects.count(); ++i) {
42  originalPoses.push_back(poseFromNxLib(objects[i][itmLink]));
43  }
44 }
45 
47  // Exit early if we have no work to do
48  if (originalPoses.empty())
49  {
50  return;
51  }
52 
53  // Find transform from the frame in which the objects were defined to the current optical frame
54  tf2::Transform cameraPose;
55  try
56  {
57  cameraPose = fromMsg(tfBuffer.lookupTransform(cameraFrame, objectsFrame, ros::Time(0)).transform);
58  }
59  catch (const tf2::TransformException& e)
60  {
61  ROS_WARN("Could not look up the virtual object pose due to the TF error: %s", e.what());
62  return;
63  }
64 
65  // Apply the transform to all of the original poses
66  for (size_t i = 0; i < originalPoses.size(); ++i)
67  {
68  tf2::Transform objectPose = cameraPose * originalPoses[i];
69  NxLibItem objectLink = NxLibItem{ itmObjects }[static_cast<int>(i)][itmLink];
70  writePoseToNxLib(objectPose.inverse(), objectLink);
71  }
72 }
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
tf2::Transform fromMsg(geometry_msgs::Transform const &transform)
#define ROS_WARN(...)
void writePoseToNxLib(tf2::Transform const &pose, NxLibItem const &node)
std::string objectsFrame
Frame in which objects are defined.
VirtualObjectHandler(const std::string &filename, const std::string &objectsFrame, const std::string &cameraFrame)
Transform inverse() const
std::string cameraFrame
Optical frame of the camera.
std::vector< tf2::Transform > originalPoses
Original object poses in the base frame.
tf2::Transform poseFromNxLib(NxLibItem const &node)


ensenso_camera
Author(s): Ensenso
autogenerated on Thu May 6 2021 02:50:06