disparity_publisher.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roboception GmbH
3  * All rights reserved
4  *
5  * Author: Heiko Hirschmueller
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  * may be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "disparity_publisher.h"
35 
37 
39 
40 namespace rc
41 {
42 DisparityPublisher::DisparityPublisher(ros::NodeHandle& nh, const std::string& frame_id_prefix, double _f, double _t,
43  double _scale)
44  : GenICam2RosPublisher(frame_id_prefix)
45 {
46  seq = 0;
47  f = _f;
48  t = _t;
49  scale = _scale;
50  disprange = 0;
51 
52  pub = nh.advertise<stereo_msgs::DisparityImage>("disparity", 1);
53 }
54 
56 {
57  disprange = _disprange;
58 }
59 
61 {
62  return pub.getNumSubscribers() > 0;
63 }
64 
65 void DisparityPublisher::publish(const rcg::Buffer* buffer, uint32_t part, uint64_t pixelformat)
66 {
67  if (pub.getNumSubscribers() > 0 && pixelformat == Coord3D_C16)
68  {
69  // allocate new image message and set meta information
70 
71  stereo_msgs::DisparityImagePtr p = boost::make_shared<stereo_msgs::DisparityImage>();
72 
73  const uint64_t freq = 1000000000ul;
74  uint64_t time = buffer->getTimestampNS();
75 
76  p->header.seq = seq++;
77  p->header.stamp.sec = time / freq;
78  p->header.stamp.nsec = time - freq * p->header.stamp.sec;
79  p->header.frame_id = frame_id;
80 
81  // prepare size and format of outgoing image
82 
83  p->image.header = p->header;
84  p->image.width = static_cast<uint32_t>(buffer->getWidth(part));
85  p->image.height = static_cast<uint32_t>(buffer->getHeight(part));
86  p->image.encoding = sensor_msgs::image_encodings::TYPE_32FC1;
87  p->image.is_bigendian = rcg::isHostBigEndian();
88  p->image.step = p->image.width * sizeof(float);
89 
90  size_t px = buffer->getXPadding(part);
91  const uint8_t* ps = static_cast<const uint8_t*>(buffer->getBase(part));
92 
93  // convert image information
94 
95  p->image.data.resize(p->image.step * p->image.height);
96 
97  float* pt = reinterpret_cast<float*>(&p->image.data[0]);
98  float dmax = 0;
99 
100  bool bigendian = buffer->isBigEndian();
101 
102  for (uint32_t k = 0; k < p->image.height; k++)
103  {
104  if (bigendian)
105  {
106  for (uint32_t i = 0; i < p->image.width; i++)
107  {
108  uint16_t d = (ps[0] << 8) | ps[1];
109 
110  *pt = -1.0f;
111 
112  if (d != 0)
113  {
114  *pt = scale * d;
115  dmax = std::max(dmax, *pt);
116  }
117 
118  ps += 2;
119  pt++;
120  }
121  }
122  else
123  {
124  for (uint32_t i = 0; i < p->image.width; i++)
125  {
126  uint16_t d = (ps[1] << 8) | ps[0];
127 
128  *pt = -1.0f;
129 
130  if (d != 0)
131  {
132  *pt = scale * d;
133  dmax = std::max(dmax, *pt);
134  }
135 
136  ps += 2;
137  pt++;
138  }
139  }
140 
141  ps += px;
142  }
143 
144  p->f = f * p->image.width;
145  p->T = t;
146  p->valid_window.x_offset = 0;
147  p->valid_window.y_offset = 0;
148  p->valid_window.width = p->image.width;
149  p->valid_window.height = p->image.height;
150  p->min_disparity = 0;
151  p->max_disparity = std::max(dmax, static_cast<float>(disprange));
152  p->delta_d = scale;
153 
154  // publish message
155 
156  pub.publish(p);
157  }
158 }
159 }
Interface for all publishers relating to images, point clouds or other stereo-camera data...
d
void publish(const rcg::Buffer *buffer, uint32_t part, uint64_t pixelformat) override
Offers a buffer for publication.
void publish(const boost::shared_ptr< M > &message) const
bool used() override
Returns true if there are subscribers to the topic.
Coord3D_C16
size_t getXPadding(std::uint32_t part) const
size_t getWidth(std::uint32_t part) const
void * getBase(std::uint32_t part) const
const std::string TYPE_32FC1
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint64_t getTimestampNS() const
uint32_t getNumSubscribers() const
void setDisprange(int disprange)
Set the disparity range for scaling of images.
bool isBigEndian() const
bool isHostBigEndian()
DisparityPublisher(ros::NodeHandle &nh, const std::string &frame_id_prefix, double f, double t, double scale)
Initialization of publisher.
size_t getHeight(std::uint32_t part) const


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Wed Mar 20 2019 07:55:49