polygon_display.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <ros/ros.h>
36 #include <angles/angles.h>
37 #include <geometry_msgs/PolygonStamped.h>
38 #include <nav_2d_msgs/Polygon2DStamped.h>
39 #include <nav_2d_msgs/Polygon2DCollection.h>
41 #include <nav_2d_utils/polygons.h>
43 #include <color_util/convert.h>
44 #include <vector>
45 
46 nav_2d_msgs::Polygon2D makeStar(double base_angle, double outer = 3.0, double inner = 1.0, unsigned int N = 5)
47 {
48  nav_2d_msgs::Polygon2D polygon;
49  polygon.points.resize(2 * N + 1);
50 
51  double inc = M_PI / N;
52  for (unsigned int i = 0 ; i < 2 * N; ++i)
53  {
54  double length = i % 2 ? outer : inner;
55  polygon.points[i].x = length * cos(base_angle + i * inc);
56  polygon.points[i].y = length * sin(base_angle + i * inc);
57  }
58  polygon.points[2 * N] = polygon.points[0];
59  return polygon;
60 }
61 
63 
64 int main(int argc, char** argv)
65 {
66  ros::init(argc, argv, "polygon_display");
67  ros::NodeHandle nh("~");
68  ros::Rate r(33);
69 
70  ros::Publisher pub0 = nh.advertise<nav_2d_msgs::Polygon2DStamped>("polygon", 1);
71  ros::Publisher pub1 = nh.advertise<nav_2d_msgs::Polygon2DCollection>("polygons", 1);
72  ros::Publisher pub2 = nh.advertise<geometry_msgs::PolygonStamped>("polygon3d", 1);
73  double degrees = 0.0;
74 
75  std::vector<color_util::ColorRGBA24> rainbow =
76  {
77  color_util::get(NamedColor::RED),
78  color_util::get(NamedColor::ORANGE),
79  color_util::get(NamedColor::YELLOW),
80  color_util::get(NamedColor::GREEN),
81  color_util::get(NamedColor::BLUE),
82  color_util::get(NamedColor::PURPLE)
83  };
84 
85  while (ros::ok())
86  {
87  nav_2d_msgs::Polygon2DStamped polygon;
88  polygon.header.stamp = ros::Time::now();
89  polygon.header.frame_id = "map";
90  polygon.polygon.points.resize(11);
91 
92  double rad = angles::from_degrees(degrees);
93  polygon.polygon = makeStar(rad);
94  pub0.publish(polygon);
95 
96  nav_2d_msgs::Polygon2DCollection polygons;
97  polygons.header.stamp = ros::Time::now();
98  polygons.header.frame_id = "map";
99 
100  for (unsigned int i = 0; i < rainbow.size(); ++i)
101  {
102  nav_2d_msgs::ComplexPolygon2D complex;
103  double w = i * 0.2;
104  complex.outer = makeStar(rad, 4.2 + w, 2.2 + w);
105  complex.inner.push_back(makeStar(rad, 4.0 + w, 2.0 + w));
106 
107  polygons.polygons.push_back(complex);
108  polygons.colors.push_back(color_util::toMsg(rainbow[i]));
109  }
110  pub1.publish(polygons);
111 
112  geometry_msgs::Pose2D pose;
113  pose.x = 5.0 * cos(rad);
114  pose.y = 5.0 * sin(rad);
115 
116  nav_2d_msgs::Polygon2DStamped small_star;
117  small_star.header.stamp = ros::Time::now();
118  small_star.header.frame_id = "map";
119  small_star.polygon = nav_2d_utils::movePolygonToPose(makeStar(-rad, 0.5, 0.25), pose);
120 
121  pub2.publish(nav_2d_utils::polygon2Dto3D(small_star));
122 
123  r.sleep();
124  ros::spinOnce();
125  degrees += 1;
126  }
127 
128  return 0;
129 }
void publish(const boost::shared_ptr< M > &message) const
const ColorRGBA24 & get(const NamedColor &name)
std_msgs::ColorRGBA toMsg(const color_util::ColorRGBA &rgba)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
nav_2d_msgs::Polygon2D makeStar(double base_angle, double outer=3.0, double inner=1.0, unsigned int N=5)
int main(int argc, char **argv)
static double from_degrees(double degrees)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
TFSIMD_FORCE_INLINE const tfScalar & w() const
static Time now()
geometry_msgs::Polygon polygon2Dto3D(const nav_2d_msgs::Polygon2D &polygon_2d)
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
nav_2d_msgs::Polygon2D movePolygonToPose(const nav_2d_msgs::Polygon2D &polygon, const geometry_msgs::Pose2D &pose)
ROSCPP_DECL void spinOnce()
r
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)


robot_nav_viz_demos
Author(s):
autogenerated on Sun Jan 10 2021 04:09:02