mesh_marker_test.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <visualization_msgs/Marker.h>
4 #include <visualization_msgs/MarkerArray.h>
5 
7 
8 void publishText(int& id, float x, float y, const std::string& text)
9 {
10  visualization_msgs::Marker marker;
11  marker.header.frame_id = "base_link";
12  marker.header.stamp = ros::Time::now();
13  marker.ns = "mesh";
14  marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
15  marker.action = visualization_msgs::Marker::ADD;
16  marker.pose.orientation.x = 0.0;
17  marker.pose.orientation.y = 0.0;
18  marker.pose.orientation.z = 0.0;
19  marker.pose.orientation.w = 1.0;
20  marker.color.r = 1;
21  marker.color.g = 1;
22  marker.color.b = 1;
23  marker.color.a = 1;
24  marker.scale.z = 0.2;
25  marker.text = text;
26  marker.id = ++id;
27  marker.pose.position.x = x;
28  marker.pose.position.y = y;
29 
30  g_marker_pub.publish(marker);
31 }
32 
33 void publishMesh(int& id,
34  float x,
35  float y,
36  float r,
37  float g,
38  float b,
39  float a,
40  bool use_embedded_materials,
41  int mesh)
42 {
43  using visualization_msgs::Marker;
44 
45  Marker marker;
46  marker.header.frame_id = "base_link";
47  marker.header.stamp = ros::Time::now();
48  marker.ns = "mesh";
49  switch (mesh)
50  {
51  case 0:
52  marker.type = Marker::MESH_RESOURCE;
53  marker.mesh_resource = "package://rviz/src/test/meshes/pr2-base.dae";
54  marker.mesh_use_embedded_materials = use_embedded_materials;
55  break;
56  case 1:
57  marker.type = Marker::MESH_RESOURCE;
58  marker.mesh_resource = "package://rviz/src/test/meshes/frame.dae";
59  marker.mesh_use_embedded_materials = use_embedded_materials;
60  break;
61  case 2:
62  marker.type = Marker::SPHERE;
63  break;
64  }
65  marker.action = Marker::ADD;
66  marker.pose.orientation.x = 0.0;
67  marker.pose.orientation.y = 0.0;
68  marker.pose.orientation.z = 0.0995;
69  marker.pose.orientation.w = 0.995;
70  marker.scale.x = 1;
71  marker.scale.y = 1;
72  marker.scale.z = 1;
73  marker.color.r = r;
74  marker.color.g = g;
75  marker.color.b = b;
76  marker.color.a = a;
77  marker.frame_locked = true;
78  marker.id = ++id;
79  marker.pose.position.x = x;
80  marker.pose.position.y = y;
81 
82  g_marker_pub.publish(marker);
83 }
84 
85 void publishMeshes(int& id, float x, float r, float g, float b, float a)
86 {
87  int y = -5;
88  char buffer[30];
89  snprintf(buffer, sizeof(buffer), "rbg: %.1f %.1f %.1f, a: %.1f", r, g, b, a);
90  publishText(id, x, y - 1, buffer);
91 
92  publishMesh(id, x, y++, r, g, b, a, true, 0);
93  publishMesh(id, x, y++, r, g, b, a, false, 0);
94 
95  publishMesh(id, x, y++, r, g, b, a, true, 1);
96  publishMesh(id, x, y++, r, g, b, a, false, 1);
97 
98  publishMesh(id, x, y++, r, g, b, a, true, 2);
99  publishMesh(id, x, y++, r, g, b, a, false, 2);
100 }
101 
103 {
104  ROS_INFO("Publishing");
105 
106  int id = 0;
107  float x = -6;
108  float y = -5;
109 
110  // column headers
111  publishText(id, x, y++, "mesh1, use_embedded_materials = true");
112  publishText(id, x, y++, "mesh1, use_embedded_materials = false");
113  publishText(id, x, y++, "mesh2, use_embedded_materials = true");
114  publishText(id, x, y++, "mesh2, use_embedded_materials = false");
115  publishText(id, x, y++, "sphere, use_embedded_materials = true");
116  publishText(id, x, y++, "sphere, use_embedded_materials = false");
117 
118  publishMeshes(id, ++x, 0, 0, 0, 0);
119 
120  publishMeshes(id, ++x, 1, 1, 1, 1);
121  publishMeshes(id, ++x, 1, 1, 1, 0.5);
122  publishMeshes(id, ++x, 1, 1, 1, 0.0);
123 
124  publishMeshes(id, ++x, 1, 0, 0, 1);
125  publishMeshes(id, ++x, 1, 0, 0, 0.5);
126  publishMeshes(id, ++x, 1, 0, 0, 0);
127 
128  publishMeshes(id, ++x, 1, .5, .5, 1);
129  publishMeshes(id, ++x, 1, .5, .5, 0.5);
130  publishMeshes(id, ++x, 1, .5, .5, 0);
131 
132  static float rgba[4] = {0, 0, 0, 0};
133  publishMeshes(id, ++x, rgba[0], rgba[1], rgba[2], rgba[3]);
134  // evolve colors over time
135  int index = 3;
136  while (index >= 0)
137  {
138  rgba[index] += 0.2;
139  if (rgba[index] > 1.01)
140  rgba[index--] = 0.0f;
141  else
142  break;
143  }
144 }
145 
146 void publishCallback(const ros::TimerEvent& /*unused*/)
147 {
148  return publishMeshes();
149 }
150 
151 int main(int argc, char** argv)
152 {
153  ros::init(argc, argv, "mesh_marker_test");
154  ros::NodeHandle n;
155 
156  bool latch_only = false;
157  if (argc == 2)
158  {
159  if (std::string(argv[1]) == "--latch-only")
160  {
161  latch_only = true;
162  }
163  }
164 
165  g_marker_pub = n.advertise<visualization_msgs::Marker>("mesh_markers", 0, /*latch*/ latch_only);
166 
167 
168  ros::Timer publish_timer;
169  if (latch_only)
170  {
171  ros::Duration(1.0).sleep();
172  publishMeshes();
173  }
174  else
175  {
176  publish_timer = n.createTimer(ros::Duration(1), publishCallback);
177  }
178 
179  ros::spin();
180 }
ros::Publisher
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
publishCallback
void publishCallback(const ros::TimerEvent &)
Definition: mesh_marker_test.cpp:146
ros.h
main
int main(int argc, char **argv)
Definition: mesh_marker_test.cpp:151
publishMesh
void publishMesh(int &id, float x, float y, float r, float g, float b, float a, bool use_embedded_materials, int mesh)
Definition: mesh_marker_test.cpp:33
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
g_marker_pub
ros::Publisher g_marker_pub
Definition: mesh_marker_test.cpp:6
ros::TimerEvent
publishMeshes
void publishMeshes(int &id, float x, float r, float g, float b, float a)
Definition: mesh_marker_test.cpp:85
publishText
void publishText(int &id, float x, float y, const std::string &text)
Definition: mesh_marker_test.cpp:8
ros::spin
ROSCPP_DECL void spin()
ros::Duration::sleep
bool sleep() const
ROS_INFO
#define ROS_INFO(...)
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
ros::Timer
ros::NodeHandle
ros::Time::now
static Time now()


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:09