6 from geometry_msgs.msg
import Point32, PolygonStamped
7 from jsk_recognition_msgs.msg
import ModelCoefficientsArray
8 from jsk_recognition_msgs.msg
import PolygonArray
9 from pcl_msgs.msg
import ModelCoefficients
13 points = np.array(points)
15 if len(points[0]) < 3:
16 coeffs = np.array([0, 0, 1, 0],
'f')
22 dists = np.linalg.norm(points - p0, axis=1, ord=2)
23 farthest_index = np.argmax(dists)
24 p1 = points[farthest_index]
26 p2 = points[np.argmin(np.dot(points - p0, v0))]
29 normal = np.cross(v0, v1)
30 norm = np.linalg.norm(normal, ord=2)
32 if np.isclose(norm, 0.0):
33 return False, np.array([0, 0, 0, 0],
'f')
34 normal = normal / norm
36 dists = np.abs(np.dot(points - p0, normal))
39 d = - np.dot(normal, p0)
40 coeffs = np.array([normal[0], normal[1], normal[2], d],
'f')
41 return np.allclose(dists, 0.0), coeffs
46 publish_rate = rospy.get_param(
"~publish_rate", 1.0)
49 frame_id = rospy.get_param(
"~frame_id")
50 param = rospy.get_param(
"~polygons")
54 self.
pub_polygons = rospy.Publisher(
"~output", PolygonArray, queue_size=1)
59 '~publish_coeffs is True,'
60 ' but ModelCoefficientsArray is not published'
61 ' because each polygon is not in planar.')
64 '~output/coefficients',
65 ModelCoefficientsArray, queue_size=1)
66 self.
timer = rospy.Timer(rospy.Duration(1. / publish_rate), self.
publish)
70 assert isinstance(param, list),
"polygons must be list"
72 has_label = any(
"label" in p
for p
in param)
73 has_likelihood = any(
"likelihood" in p
for p
in param)
76 assert "points" in polygon
and isinstance(polygon[
"points"], list),\
77 "each polygon must have at least 1 'points'"
78 for point
in polygon[
"points"]:
79 assert len(point) == 3,\
80 "element of 'points' must be list of 3 numbers"
82 assert "label" in polygon,
"missing 'label'"
84 assert "likelihood" in polygon,
"missing 'likelihood'"
88 msg.header.frame_id = frame_id
89 coeffs_array_msg = ModelCoefficientsArray(header=msg.header)
92 ps.header.frame_id = frame_id
93 ps.polygon.points = [Point32(*v)
for v
in polygon[
"points"]]
95 coeffs_msg = ModelCoefficients(header=ps.header)
96 planar, coeffs =
is_planar(polygon[
"points"])
97 coeffs_msg.values = coeffs
99 coeffs_array_msg.coefficients.append(coeffs_msg)
100 msg.polygons.append(ps)
102 msg.labels.append(polygon[
"label"])
104 msg.likelihood.append(polygon[
"likelihood"])
105 return msg, coeffs_array_msg
109 self.msg.header.stamp = event.current_real
110 for p
in self.msg.polygons:
111 p.header.stamp = event.current_real
117 p.header.stamp = event.current_real
121 if __name__ ==
'__main__':
122 rospy.init_node(
"polygon_array_publisher")