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