simulate_primitive_shape_on_plane.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 import numpy as np
6 import rospy
7 from sensor_msgs.msg import PointCloud2, PointField
8 from geometry_msgs.msg import PolygonStamped
9 from geometry_msgs.msg import Point32
10 from jsk_recognition_msgs.msg import PolygonArray
11 from sensor_msgs.point_cloud2 import create_cloud
12 from std_msgs.msg import Header
13 
14 
15 def create_cloud_xyzrgb(header, xyzrgb):
16  fields = [
17  PointField(name='x', offset=0, datatype=PointField.FLOAT32, count=1),
18  PointField(name='y', offset=4, datatype=PointField.FLOAT32, count=1),
19  PointField(name='z', offset=8, datatype=PointField.FLOAT32, count=1),
20  PointField(name='rgba', offset=16, datatype=PointField.UINT32, count=1),
21  ]
22  return create_cloud(header, fields, xyzrgb)
23 
24 
26  def __init__(self):
27  self.pub_plane = rospy.Publisher("~output/plane", PolygonArray, queue_size=10)
28  self.pub_cloud = rospy.Publisher("~output/cloud", PointCloud2, queue_size=10)
29  self.timer = rospy.Timer(rospy.Duration(0.1), self.timer_cb)
30 
31  def generate_plane(self, header,
32  xmin=-0.0, xmax=1.0,
33  ymin=-1.0, ymax=1.0):
34  msg = PolygonArray()
35  msg.header = header
36  p = PolygonStamped()
37  p.header = header
38  p.polygon.points = [Point32(x=xmin, y=ymin),
39  Point32(x=xmax, y=ymin),
40  Point32(x=xmax, y=ymax),
41  Point32(x=xmin, y=ymax)]
42  msg.polygons.append(p)
43  return msg
44 
45  def generate_box_cloud(self, d=0.01, noise=0.0015,
46  xmin=0.2, xmax=0.4,
47  ymin=-0.05, ymax=0.05,
48  zmin=0.01, zmax=0.1):
49  pts = []
50 
51  for x in np.arange(xmin, xmax, d):
52  for y in np.arange(ymin, ymax, d):
53  pts.append([x, y, zmax] + np.random.normal(0.0, noise, 3))
54 
55  for z in np.arange(zmin, zmax, d):
56  for y in np.arange(ymin, ymax, d):
57  pts.append([xmin, y, z] + np.random.normal(0.0, noise, 3))
58  return pts
59 
60  def generate_circle_cloud(self, d=0.01, noise=0.0015,
61  radius=0.05,
62  xpos=0.2, ypos=0.2,
63  rmin=np.pi/2., rmax=np.pi*3./2,
64  zmin=0.01, zmax=0.15):
65  pts = []
66 
67  for z in np.arange(zmin, zmax, d):
68  for rad in np.arange(rmin, rmax, d*2.):
69  pts.append([xpos + radius * np.cos(rad),
70  ypos + radius * np.sin(rad),
71  z] + np.random.normal(0.0, noise, 3))
72 
73  for r in np.arange(0, radius, d):
74  for rad in np.arange(0, np.pi * 2., d*2.):
75  pts.append([xpos + r * np.cos(rad),
76  ypos + r * np.sin(rad),
77  zmax] + np.random.normal(0.0, noise, 3))
78 
79  return pts
80 
81  def generate_plane_cloud(self, xmin, xmax, ymin, ymax):
82  d = 0.02
83  plane = []
84  for x in np.arange(xmin, xmax, d):
85  for y in np.arange(ymin, ymax, d):
86  plane.append([x, y, 1.0])
87  return plane
88 
89  def timer_cb(self, args=None):
90  header = Header(frame_id="camera_rgb_optical_frame", stamp=rospy.Time.now())
91 
92  plane = self.generate_plane(header)
93  self.pub_plane.publish(plane)
94 
95  pts = [pt.tolist() + [0xff0000ff] for pt in self.generate_box_cloud()]
96  pts += [pt.tolist() + [0xffffffff] for pt in self.generate_circle_cloud()]
97  cloud = create_cloud_xyzrgb(header, pts)
98  self.pub_cloud.publish(cloud)
99 
100 
101 if __name__ == '__main__':
102  rospy.init_node("test_primitive_classifier")
104  rospy.spin()
def generate_circle_cloud(self, d=0.01, noise=0.0015, radius=0.05, xpos=0.2, ypos=0.2, rmin=np.pi/2., rmax=np.pi *3./2, zmin=0.01, zmax=0.15)
def generate_plane(self, header, xmin=-0.0, xmax=1.0, ymin=-1.0, ymax=1.0)
def generate_box_cloud(self, d=0.01, noise=0.0015, xmin=0.2, xmax=0.4, ymin=-0.05, ymax=0.05, zmin=0.01, zmax=0.1)


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:47