25 std::string readFile(
const std::string& filename)
27 std::ifstream file{ filename };
28 if (!file.is_open() || !file.rdbuf())
30 throw std::runtime_error(
"Unable to read objects file");
33 std::stringstream buffer;
34 buffer << file.rdbuf();
39 struct VirtualObjectMarkerPublisher
42 const NxLibItem& objects,
const std::string& frame, std::atomic_bool& stop_)
43 : nh(
std::
move(_nh)), rate(publishRate), stop(stop_)
46 publisher = ensenso::ros::create_publisher<visualization_msgs::msg::MarkerArray>(nh, topic, 1);
49 for (
int i = 0; i < objects.count(); ++i)
51 auto&
object = objects[i];
53 visualization_msgs::msg::Marker marker;
58 marker.header.frame_id = frame;
59 marker.action = visualization_msgs::msg::Marker::MODIFY;
62 const auto color =
object[itmLighting][itmColor];
63 if (color.exists() && color.count() == 3)
65 marker.color.r =
static_cast<float>(color[0].asDouble());
66 marker.color.g =
static_cast<float>(color[1].asDouble());
67 marker.color.b =
static_cast<float>(color[2].asDouble());
68 marker.color.a = 1.0f;
82 auto type =
object[itmType];
83 auto filename =
object[itmFilename];
86 if (filename.exists())
88 marker.type = visualization_msgs::msg::Marker::MESH_RESOURCE;
89 marker.mesh_resource =
"file://" + filename.asString();
92 marker.scale.x = 1.0 / 1000.0;
93 marker.scale.y = 1.0 / 1000.0;
94 marker.scale.z = 1.0 / 1000.0;
96 else if (type.asString() == valSphere)
98 marker.type = visualization_msgs::msg::Marker::SPHERE;
101 marker.scale.x =
object[itmRadius].asDouble() * 2.0 / 1000.0;
102 marker.scale.y = marker.scale.x;
103 marker.scale.z = marker.scale.x;
105 else if (type.asString() == valCylinder)
107 marker.type = visualization_msgs::msg::Marker::CYLINDER;
110 marker.scale.x =
object[itmRadius].asDouble() * 2.0 / 1000.0;
111 marker.scale.y = marker.scale.x;
112 marker.scale.z =
object[itmHeight].asDouble() / 1000.0;
118 else if (type.asString() == valCube)
120 marker.type = visualization_msgs::msg::Marker::CUBE;
122 marker.scale.x =
object[itmWidth].asDouble() / 1000.0;
123 marker.scale.y =
object[itmWidth].asDouble() / 1000.0;
124 marker.scale.z =
object[itmWidth].asDouble() / 1000.0;
126 else if (type.asString() == valCuboid)
128 marker.type = visualization_msgs::msg::Marker::CUBE;
130 marker.scale.x =
object[itmWidth].asDouble() / 1000.0;
131 marker.scale.z =
object[itmHeight].asDouble() / 1000.0;
132 marker.scale.y =
object[itmDepth].asDouble() / 1000.0;
140 markers.markers.push_back(marker);
148 publisher->publish(markers);
156 visualization_msgs::msg::MarkerArray markers;
157 std::atomic_bool& stop;
162 const std::string& objectsFrame,
const std::string& linkFrame,
163 const std::string& markerTopic,
double markerPublishRate)
167 auto objects = NxLibItem{ itmObjects };
168 objects.setJson(readFile(filename));
171 for (
int i = 0; i < objects.count(); ++i)
177 if (!markerTopic.empty())
180 VirtualObjectMarkerPublisher publisher{ nh, markerTopic, markerPublishRate,
213 ENSENSO_WARN(
"Could not look up the virtual object pose due to the TF error: %s", e.what());
221 NxLibItem objectLink = NxLibItem{ itmObjects }[
static_cast<int>(i)][itmLink];
void writeTransformToNxLib(Transform const &transform, NxLibItem const &node)
#define TF2_BUFFER_CTOR_ARGS(nh)
void run(class_loader::ClassLoader *loader)
std::vector< tf2::Transform > originalTransforms
Original object transforms in the base frame.
Transform fromMsg(TransformMsg const &transform)
std::string objectsFrame
Frame in which objects are defined.
virtual geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration timeout) const
::ros::NodeHandle NodeHandle
void ENSENSO_WARN(T... args)
::std::unique_ptr< ::ros::Publisher > Publisher
StampedPoseMsg poseFromTransform(StampedTransformMsg const &transform)
inline ::std::string get_node_name(NodeHandle &nh)
void move(std::vector< T > &a, std::vector< T > &b)
Transform transformFromNxLib(NxLibItem const &node)
std::string linkFrame
Link frame where NxLib expects the objects to be defined.
std::thread markerThread
Background thread used to publish object markers.
VirtualObjectHandler(ensenso::ros::NodeHandle &nh, const std::string &filename, const std::string &objectsFrame, const std::string &linkFrame, const std::string &markerTopic, double markerPublishRate)
std::atomic_bool stopMarkerThread
Flag to indicate markerThread should stop.