FramedPointCloud.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 
5 #include <pcl/point_cloud.h>
6 #include <pcl/common/transforms.h>
7 
8 #include <VirtualRobot/Robot.h>
9 
11 
12 
13 
14 namespace visionx
15 {
16 
17  /**
18  * @brief Transform a point cloud from a robot frame to another.
19  * @param sourceCloud The source point cloud.
20  * @param targetCloud The target point cloud (may be same as `sourceCloud`).
21  * @param sourceFrame The current frame of `sourceCloud`.
22  * @param targetFrame The current frame to transform to.
23  * @param robot The reference robot to use for transformation.
24  */
25  template <class PointT>
26  void transformPointCloudFromTo(const pcl::PointCloud<PointT>& sourceCloud,
27  pcl::PointCloud<PointT>& targetCloud,
28  const std::string& sourceFrame, const std::string& targetFrame,
29  const VirtualRobot::RobotPtr& robot);
30 
31 
32  /**
33  * @brief A point cloud which keeps track of its reference coordinate frame
34  * and allows changing frames using `armarx::FramedPose`.
35  */
36  template <class _PointT>
37  class FramedPointCloud
38  {
39  public:
40  using PointT = _PointT;
41  using PointCloudT = pcl::PointCloud<PointT>;
42  using PointCloudPtrT = typename PointCloudT::Ptr;
43 
44  public:
45 
46  /// Construct an empty point cloud in global frame.
47  FramedPointCloud() = default;
48 
49  /// Construct an empty point cloud residing in given frame.
50  FramedPointCloud(const std::string& frameName)
51  : frame(frameName)
52  {}
53 
54  /// Construct a framed point cloud managing given point cloud in global frame.
56  : cloud(pointCloudPtr)
57  {}
58 
59  /// Construct a framed point cloud managing given point cloud residing in given frame.
60  FramedPointCloud(PointCloudPtrT pointCloudPtr, const std::string& frameName)
61  : cloud(pointCloudPtr), frame(frameName)
62  {}
63 
64 
65  /// Get the current frame.
66  std::string getFrame() const
67  {
68  return frame;
69  }
70  void setFrameWithoutTransformation(const std::string& f)
71  {
72  frame = f;
73  }
74 
75  /// Get the current pose as `FramedPose`.
76  armarx::FramedPose getFramedPose(const std::string& agentName) const
77  {
78  return armarx::FramedPose(Eigen::Matrix4f::Identity(), frame, agentName);
79  }
81  {
82  return getFramedPose(robot->getName());
83  }
84 
85  /// Get the current global pose.
87  {
88  return getFramedPose(robot).toGlobalEigen(robot);
89  }
90 
91  /// Transform the point cloud from the current frame to the new frame using the given reference robot.
92  void changeFrame(const std::string& newFrame, const VirtualRobot::RobotPtr& robot)
93  {
94  transformPointCloudFromTo<PointT>(*cloud, *cloud, frame, newFrame, robot);
95  this->frame = newFrame;
96  }
97 
99  {
100  pcl::transformPointCloud(*cloud, *cloud, transform);
101  }
102  public:
103 
104  /// The point cloud.
106 
107 
108  private:
109 
110  /// The current frame.
111  std::string frame = armarx::GlobalFrame;
112 
113  };
114 
115 
116  // Template deduction guidelines for PointT from PointCloudPtrT.
117  template <class PointCloudPtrT>
118  FramedPointCloud(PointCloudPtrT pointCloudPtr)
119  -> FramedPointCloud<typename PointCloudPtrT::element_type::PointType>;
120 
121  template <class PointCloudPtrT>
122  FramedPointCloud(PointCloudPtrT pointCloudPtr, const std::string& frameName)
123  -> FramedPointCloud<typename PointCloudPtrT::element_type::PointType>;
124 
125 }
126 
127 
128 template <class PointT>
129 void visionx::transformPointCloudFromTo(const pcl::PointCloud<PointT>& sourceCloud,
130  pcl::PointCloud<PointT>& targetCloud,
131  const std::string& sourceFrame, const std::string& targetFrame,
132  const VirtualRobot::RobotPtr& robot)
133 {
134  if (sourceFrame != targetFrame)
135  {
136  armarx::FramedPose framedPose(Eigen::Matrix4f::Identity(), sourceFrame, robot->getName());
137  framedPose.changeFrame(robot, targetFrame);
138  const Eigen::Matrix4f sourceToTarget = framedPose.toEigen();
139  pcl::transformPointCloud(sourceCloud, targetCloud, sourceToTarget);
140  }
141 }
142 
143 
visionx::FramedPointCloud::getFramedPose
armarx::FramedPose getFramedPose(const std::string &agentName) const
Get the current pose as FramedPose.
Definition: FramedPointCloud.h:76
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
visionx::transformPointCloudFromTo
void transformPointCloudFromTo(const pcl::PointCloud< PointT > &sourceCloud, pcl::PointCloud< PointT > &targetCloud, const std::string &sourceFrame, const std::string &targetFrame, const VirtualRobot::RobotPtr &robot)
Transform a point cloud from a robot frame to another.
Definition: FramedPointCloud.h:129
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
visionx::FramedPointCloud::getGlobalPose
Eigen::Matrix4f getGlobalPose(const VirtualRobot::RobotPtr &robot) const
Get the current global pose.
Definition: FramedPointCloud.h:86
visionx::FramedPointCloud::getFramedPose
armarx::FramedPose getFramedPose(const VirtualRobot::RobotPtr &robot) const
Definition: FramedPointCloud.h:80
visionx::FramedPointCloud::setFrameWithoutTransformation
void setFrameWithoutTransformation(const std::string &f)
Definition: FramedPointCloud.h:70
visionx::FramedPointCloud::PointT
_PointT PointT
Definition: FramedPointCloud.h:40
visionx::FramedPointCloud::getFrame
std::string getFrame() const
Get the current frame.
Definition: FramedPointCloud.h:66
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::FramedPose::toGlobalEigen
Eigen::Matrix4f toGlobalEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:517
FramedPose.h
visionx::FramedPointCloud::FramedPointCloud
FramedPointCloud()=default
Construct an empty point cloud in global frame.
visionx::FramedPointCloud::cloud
PointCloudPtrT cloud
The point cloud.
Definition: FramedPointCloud.h:105
armarx::Pose::toEigen
virtual Eigen::Matrix4f toEigen() const
Definition: Pose.cpp:334
visionx::FramedPointCloud::FramedPointCloud
FramedPointCloud(const std::string &frameName)
Construct an empty point cloud residing in given frame.
Definition: FramedPointCloud.h:50
visionx::FramedPointCloud::PointCloudPtrT
typename PointCloudT::Ptr PointCloudPtrT
Definition: FramedPointCloud.h:42
visionx::FramedPointCloud::changeFrame
void changeFrame(const std::string &newFrame, const VirtualRobot::RobotPtr &robot)
Transform the point cloud from the current frame to the new frame using the given reference robot.
Definition: FramedPointCloud.h:92
visionx::FramedPointCloud::PointCloudT
pcl::PointCloud< PointT > PointCloudT
Definition: FramedPointCloud.h:41
visionx::FramedPointCloud::FramedPointCloud
FramedPointCloud(PointCloudPtrT pointCloudPtr)
Construct a framed point cloud managing given point cloud in global frame.
Definition: FramedPointCloud.h:55
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
visionx::FramedPointCloud::transformBy
void transformBy(const Eigen::Matrix4f &transform)
Definition: FramedPointCloud.h:98
visionx::FramedPointCloud
FramedPointCloud(PointCloudPtrT pointCloudPtr) -> FramedPointCloud< typename PointCloudPtrT::element_type::PointType >
visionx::FramedPointCloud::FramedPointCloud
FramedPointCloud(PointCloudPtrT pointCloudPtr, const std::string &frameName)
Construct a framed point cloud managing given point cloud residing in given frame.
Definition: FramedPointCloud.h:60
armarx::FramedPose::changeFrame
void changeFrame(const SharedRobotInterfacePrx &referenceRobot, const std::string &newFrame)
Definition: FramedPose.cpp:406
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18