messages.h 19.2 KB
Newer Older
1 2 3 4 5
#ifndef MESSAGES_H
#define MESSAGES_H

#include <iostream>
#include <vector>
6 7
#include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/remove_reference.hpp>
8

9 10 11
#include "ros_bridge/rapidjson/include/rapidjson/rapidjson.h"
#include "ros_bridge/rapidjson/include/rapidjson/document.h"

12 13 14
#include "utilities.h"
#include "ROSMessageTraits.h"

15 16
namespace ros_bridge {

17 18
namespace Time {
    //! @brief C++ representation of std_msgs/Time
19 20
    class Time{
    public:
21 22
        Time(): _secs(0), _nsecs(0) {}
        Time(uint32_t secs, uint32_t nsecs): _secs(secs), _nsecs(nsecs) {}
23

24 25
        uint32_t secs()  const {return _secs;}
        uint32_t nSecs() const {return _nsecs;}
26 27 28

        void setSecs(uint32_t secs)     {_secs = secs;}
        void setNSecs(uint32_t nsecs)   {_nsecs = nsecs;}
29 30 31 32 33 34

    private:
        uint32_t _secs;
        uint32_t _nsecs;
    };

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    template<class TimeType>
    bool toJson(const TimeType &time,
                rapidjson::Document &doc,
                rapidjson::Document::AllocatorType &allocator)
    {
        doc.AddMember("secs", rapidjson::Value().SetUint((uint32_t)time.secs()), allocator);
        doc.AddMember("nsecs", rapidjson::Value().SetUint((uint32_t)time.nSecs()), allocator);

        return true;
    }

}

namespace Header {



52 53 54
    //! @brief C++ representation of std_msgs/Header
    class Header{
    public:
55 56
        Header() : _seq(0), _stamp(Time::Time()), _frameId("") {}
        Header(uint32_t seq, const Time::Time &stamp, const std::string &frame_id) :
57 58 59
            _seq(seq)
          , _stamp(stamp)
          , _frameId(frame_id) {}
60

61 62 63 64 65 66
        uint32_t            seq()       const {return _seq;};
        const Time::Time   &stamp()     const {return _stamp;};
        const std::string  &frameId()   const {return _frameId;};

        Time::Time   &stamp()         {return _stamp;};
        std::string  &frameId()       {return _frameId;};
67

68
        void setSeq     (uint32_t seq)                  {_seq = seq;}
69
        void setStamp   (const Time::Time &stamp)       {_stamp = stamp;}
70
        void setFrameId (const std::string  &frameId)   {_frameId = frameId;}
71 72
    private:
        uint32_t    _seq;
73
        Time::Time        _stamp;
74
        std::string _frameId;
75 76 77
    };


78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    template <class HeaderType>
    bool toJson(const HeaderType &header,
                rapidjson::Document &doc,
                rapidjson::Document::AllocatorType &allocator)     {
        doc.AddMember("seq", rapidjson::Value().SetUint((uint32_t)header.seq()), allocator);

        rapidjson::Document stamp(rapidjson::kObjectType);
        if (!Time::toJson(header.stamp(), doc, allocator))
                return false;
        doc.AddMember("stamp", stamp, allocator);

        doc.AddMember("frame_id",
                      rapidjson::Value().SetString(header.frameId().data(),
                                                   header.frameId().length(),
                                                   allocator),
                      allocator);

        return true;
    }

}

namespace Point32 {
using namespace ros_bridge::traits;

        //! @brief C++ representation of geometry_msgs/Point32.
        template<typename FloatType = _Float64>
        class Point{
        public:
            Point(FloatType x, FloatType y, FloatType z) : _x(x), _y(y), _z(z){}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            FloatType x() const {return _x;}
            FloatType y() const {return _y;}
            FloatType z() const {return _z;}

            void setX(FloatType x) {_x = x;}
            void setY(FloatType y) {_y = y;}
            void setZ(FloatType z) {_z = z;}


        private:
            FloatType _x;
            FloatType _y;
            FloatType _z;
        };

        typedef Point<> Point64;
        typedef Point<_Float32> Point32;

        namespace det { //detail

            template <typename FloatType, class T, typename V>
            bool toJson(const T &p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator, Type2Type<V>)
            {
                doc.AddMember("x", rapidjson::Value().SetFloat((FloatType)p.x()), allocator);
                doc.AddMember("y", rapidjson::Value().SetFloat((FloatType)p.y()), allocator);
                doc.AddMember("z", rapidjson::Value().SetFloat((FloatType)p.z()), allocator);

                return true;
            }

            template <typename FloatType, class T>
            bool toJson(const T &p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator, Type2Type<Has2Components>)
            {
                doc.AddMember("x", rapidjson::Value().SetFloat((FloatType)p.x()), allocator);
                doc.AddMember("y", rapidjson::Value().SetFloat((FloatType)p.y()), allocator);
                doc.AddMember("z", rapidjson::Value().SetFloat((FloatType)0.0), allocator); // _point has no member z() -> add 0.

                return true;
            }
        }

        template <typename FloatType = _Float32, class T>
        bool toJson(const T&p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
152
        {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
            typedef typename Select<HasMemberZ<T>::value, Has3Components, Has2Components>::Result
                    Components; // Check if PointType has 2 or 3 dimensions.
            return det::toJson<FloatType>(p, doc, allocator,  Type2Type<Components>());
        }

}

namespace GeoPoint {
using namespace ros_bridge::traits;

        //! @brief C++ representation of geographic_msgs/GeoPoint.
        class GeoPoint{
        public:
            GeoPoint()
                : _latitude(0)
                , _longitude(0)
                , _altitude(0)
            {}
            GeoPoint(_Float64 latitude, _Float64 longitude, _Float64 altitude)
                : _latitude(latitude)
                , _longitude(longitude)
                , _altitude(altitude)
            {}

            _Float64 latitude()     const {return _latitude;}
            _Float64 longitude()    const {return _longitude;}
            _Float64 altitude()     const {return _altitude;}

            void setLatitude    (_Float64 latitude)    {_latitude  = latitude;}
            void setLongitude   (_Float64 longitude)   {_longitude = longitude;}
            void setAltitude    (_Float64 altitude)    {_altitude  = altitude;}


        private:
            _Float64 _latitude;
            _Float64 _longitude;
            _Float64 _altitude;
        };

        namespace det { //detail

            template <class T, typename V>
            bool toJson(const T &p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator, Type2Type<V>)
            {
                doc.AddMember("latitude", rapidjson::Value().SetFloat((_Float64)p.latitude()), allocator);
                doc.AddMember("longitude", rapidjson::Value().SetFloat((_Float64)p.longitude()), allocator);
                doc.AddMember("altitude", rapidjson::Value().SetFloat((_Float64)p.altitude()), allocator);

                return true;
            }
203

204 205 206 207 208 209
            template <class T>
            bool toJson(const T &p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator, Type2Type<Has2Components>)
            {
                doc.AddMember("latitudex", rapidjson::Value().SetFloat((_Float64)p.latitude()), allocator);
                doc.AddMember("longitude", rapidjson::Value().SetFloat((_Float64)p.longitude()), allocator);
                doc.AddMember("altitude", rapidjson::Value().SetFloat((_Float64)0.0), allocator); // _point has no member altitude() -> add 0.
210

211 212
                return true;
            }
213 214
        }

215 216 217 218 219 220 221 222 223 224 225
        template <class T>
        bool toJson(const T&p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
        {
            typedef typename Select<HasMemberAltitude<T>::value, Has3Components, Has2Components>::Result
                    Components; // Check if PointType has 2 or 3 dimensions.
            return det::toJson(p, doc, allocator,  Type2Type<Components>());
        }

}

namespace Polygon {
226 227

    //! @brief C++ representation of geometry_msgs/Polygon
228 229
    template <class PointTypeCVR,
              template <class, class...> class ContainerType =  std::vector>
230
    class Polygon{
231
        typedef typename boost::remove_cv<typename boost::remove_reference<PointTypeCVR>::type>::type PointType;
232 233
    public:
        Polygon(){}
234
        Polygon(const Polygon &poly) : _points(poly.points()){}
235 236


237 238
        const ContainerType<PointType> &points() const {return _points;}
              ContainerType<PointType> &points()       {return _points;}
239

240 241 242
    private:
        ContainerType<PointType> _points;
    };
243

244 245 246 247
    template <class PolygonType>
    bool toJson(const PolygonType &poly, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
    {
        rapidjson::Value points(rapidjson::kArrayType);
248

249 250 251 252 253 254
        for(unsigned long i=0; i < (unsigned long)poly.points().size(); ++i) {
            rapidjson::Document point(rapidjson::kObjectType);
            if ( !Point32::toJson<_Float32>(poly.points()[i], point, allocator) )
                return false;
            points.PushBack(point, allocator);
        }
255

256
        doc.AddMember("points", points, allocator);
257

258 259 260 261 262 263 264
        return true;
    }


}

namespace PolygonStamped {
265 266 267


    //! @brief C++ representation of geometry_msgs/PolygonStamped
268 269 270 271 272
    template <class PointTypeCVR,
              template<class,class...> class PolygonType = Polygon::Polygon, class HeaderType = Header::Header>
    class PolygonStamped {
    typedef typename boost::remove_cv<typename boost::remove_reference<PointTypeCVR>::type>::type PointType;
        typedef PolygonType<PointType> Polygon;
273
    public:
274 275
        PolygonStamped(){};
        PolygonStamped(const HeaderType &header, Polygon &polygon) :
276
            _header(header)
277 278
          , _polygon(polygon)
        {}
279 280


281 282
        const HeaderType  &header()  const   {return _header;}
        const Polygon     &polygon() const   {return _polygon;}
283

284 285
        HeaderType  &header()  {return _header;}
        Polygon     &polygon() {return _polygon;}
286

287 288
        void setHeader(const HeaderType &header)          {_header = header;}
        void setPolygon(const Polygon &polygon)
289
        {
290
            _polygon = polygon;
291
        }
292 293

    private:
294 295
        HeaderType   _header;
        Polygon      _polygon;
296 297
    };

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

    template <class PolyStamped>
    bool toJson(const PolyStamped &polyStamped, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
    {
        return toJson(polyStamped.polygon(), polyStamped.header(), doc, allocator);
    }

    template <class PolygonType, class HeaderType>
    bool toJson(const PolygonType &poly, const HeaderType &h, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
    {
        rapidjson::Document header(rapidjson::kObjectType);
        if (!Header::toJson(h, header, allocator))
            return false;
        rapidjson::Document polygon(rapidjson::kObjectType);
        if (!Polygon::toJson(poly, polygon, allocator))
            return false;
        doc.AddMember("header", header, allocator);
        doc.AddMember("polygon", polygon, allocator);

        return true;
    }

}

namespace PolygonArray {
using namespace ros_bridge::traits;

325
    //! @brief C++ representation of jsk_recognition_msgs/PolygonArray
326 327
    template <class PointTypeCVR,
              template <class, class...> class Polygon = Polygon::Polygon,
328
              template <class, class...> class ContainerType = std::vector,
329
              class HeaderType =  Header::Header>
330
    class PolygonArray{
331 332
        typedef typename boost::remove_cv<typename boost::remove_reference<PointTypeCVR>::type>::type PointType;
        typedef Polygon<PointType> PolygonType;
333
    public:
334 335 336 337 338 339 340 341 342 343 344 345
        PolygonArray(){}

        //!
        //! \brief PolygonArray
        //! \param header Header
        //! \param polygons Polygons
        //! \param labels Labels
        //! \param likelihood Header
        PolygonArray(const HeaderType                   &header,
                     const ContainerType<PolygonType>   &polygons,
                     const ContainerType<uint32_t>      &labels,
                     const ContainerType<_Float32>      &likelihood)
346 347 348 349 350 351
          : _header(header)
          , _polygons(polygons)
          , _labels(labels)
          , _likelihood(likelihood)
        {}

352 353 354 355 356 357 358 359
        const HeaderType                 &header()     const {return _header;}
              HeaderType                 &header()           {return _header;}
        const ContainerType<PolygonType> &polygons()   const {return _polygons;}
              ContainerType<PolygonType> &polygons()         {return _polygons;}
        const ContainerType<uint32_t>    &labels()     const {return _labels;}
              ContainerType<uint32_t>    &labels()           {return _labels;}
        const ContainerType<_Float32>    &likelyhood() const {return _likelihood;}
              ContainerType<_Float32>    &likelyhood()       {return _likelihood;}
360

361 362 363 364
        void setHeader     (const HeaderType                &header)        {_header     = &header;}
        void setPolygons   (ContainerType<PolygonType>      &polygon)       { _polygons   = polygon; }
        void setLabels     (const ContainerType<uint32_t>    &labels)       {_labels     = labels;}
        void setLikelihood (const ContainerType<_Float32>    &likelihood)   {_likelihood = likelihood;}
365 366 367



368
    private:
369 370 371 372
        HeaderType                  _header;
        ContainerType<PolygonType>  _polygons;
        ContainerType<uint32_t>     _labels;
        ContainerType<_Float32>     _likelihood;
373
    };
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497


    namespace PAdetail {
        //! Helper functions to generate Json entries for labels and likelihood.

        //! \note \p p has member \fn labels().
        template <class PolygonArrayType, int  k>
        void labelsToJson(const PolygonArrayType &p, rapidjson::Value &labels, rapidjson::Document::AllocatorType &allocator, Int2Type<k>){
            for(unsigned long i=0; i < (unsigned long)p.labels().size(); ++i)
                labels.PushBack(rapidjson::Value().SetUint(p.labels()[i]), allocator);
        }

        //! \note \p p has no member \fn labels().
        template <class PolygonArrayType>
        void labelsToJson(const PolygonArrayType &p, rapidjson::Value &labels, rapidjson::Document::AllocatorType &allocator, Int2Type<0>){
            for(unsigned long i=0; i < (unsigned long)(p.polygons().size()); ++i)
                labels.PushBack(rapidjson::Value().SetUint(0), allocator); // use zero!
        }        

        //! \note \p p has member \fn likelihood().
        template <class PolygonArrayType, int  k>
        void likelihoodToJson(const PolygonArrayType &p, rapidjson::Value &likelyhood, rapidjson::Document::AllocatorType &allocator, Int2Type<k>){
            for(unsigned long i=0; i < (unsigned long)p.likelyhood().size(); ++i)
                likelyhood.PushBack(rapidjson::Value().SetFloat(p.likelyhood()[i]), allocator);
        }

        //! \note \p p has no member \fn likelihood().
        template <class PolygonArrayType>
        void likelihoodToJson(const PolygonArrayType &p, rapidjson::Value &likelyhood, rapidjson::Document::AllocatorType &allocator, Int2Type<0>){
            for(unsigned long i=0; i < (unsigned long)p.polygons().size(); ++i)
                likelyhood.PushBack(rapidjson::Value().SetFloat(0), allocator); // use zero!
        }
    }


    //!
    //! Create PolygonArray message from \p p. \p p contains a header.
    //! \param p Class implementing the PolygonArrayType interface.
    //! \param doc Rapidjson document used to store the PolygonArray message.
    //! \param allocator Allocator used by doc. Can be obtained e.g. by calling doc.getAllocator().
    //!
    //! \note The \fn labels() and \fn likelihood() members are optinal. If any of them is missing they
    //! will be replaced by arrays filled with zero and size p.polygons.size().
    //!
    //! \note If this function is called p.polygons[i] (entries implement the the PolygonStampedGroup interface)
    //! must contain a header.
    template <class PolygonArrayType>
    bool toJson(const PolygonArrayType &p, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
    {
        rapidjson::Document header(rapidjson::kObjectType);
        if (Header::toJson(p.header(), header, allocator))
            return false;
        doc.AddMember("header", header, allocator);

        rapidjson::Value polygons(rapidjson::kArrayType);
        for(unsigned long i=0; i < p.polygons().size(); ++i){
            rapidjson::Document polygon(rapidjson::kObjectType);
            if (!PolygonStamped::toJson(p.polygons[i], polygon, allocator))
                return false;
            polygons.PushBack(polygon, allocator);
        }
        doc.AddMember("polygons", polygons, allocator);


        rapidjson::Value labels(rapidjson::kArrayType);
        typedef HasMemberLabels<PolygonArrayType> HasLabels;
        PAdetail::labelsToJson(p, labels, allocator, Int2Type<HasLabels::value>());
        doc.AddMember("labels", labels, allocator);


        rapidjson::Value likelihood(rapidjson::kArrayType);
        typedef HasMemberLikelihood<PolygonArrayType> HasLikelihood;
        PAdetail::likelihoodToJson(p, likelihood, allocator, Int2Type<HasLikelihood::value>());
        doc.AddMember("likelihood", likelihood, allocator);

        return true;
    }


    //!
    //! Create PolygonArray message from \p p and \p h. \p p doesn't have it's own header.
    //! \param p Class implementing the PolygonArrayType interface.
    //! \param h Class implementing the HeaderType interface.
    //! \param doc Rapidjson document used to store the PolygonArray message.
    //! \param allocator Allocator used by doc. Can be obtained e.g. by calling doc.getAllocator().
    //!
    //! \note The \fn labels() and \fn likelihood() members are optinal. If any of them is missing they
    //! will be replaced by arrays filled with zero and size p.polygons.size().
    //!
    //! \note If this function is called the headers in p.polygons[i] (entries implement the the PolygonStampedGroup interface)
    //! are ignored.
    template <class PolygonArrayType, class HeaderType>
    bool toJson(const PolygonArrayType &p, const HeaderType &h, rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
    {
        rapidjson::Document header(rapidjson::kObjectType);
        if (!Header::toJson(h, header, allocator))
            return false;
        doc.AddMember("header", header, allocator);

        rapidjson::Value polygons(rapidjson::kArrayType);
        for(unsigned long i=0; i < (unsigned long)(p.polygons().size()); ++i){
            rapidjson::Document polygon(rapidjson::kObjectType);
            if (!PolygonStamped::toJson(p.polygons()[i], h, polygon, allocator))
                return false;
            polygons.PushBack(polygon, allocator);
        }
        doc.AddMember("polygons", polygons, allocator);


        rapidjson::Value labels(rapidjson::kArrayType);
        typedef HasMemberLabels<PolygonArrayType> HasLabels;
        PAdetail::labelsToJson(p, labels, allocator, Int2Type<HasLabels::value>());
        doc.AddMember("labels", labels, allocator);


        rapidjson::Value likelihood(rapidjson::kArrayType);
        typedef HasMemberLikelihood<PolygonArrayType> HasLikelihood;
        PAdetail::likelihoodToJson(p, likelihood, allocator, Int2Type<HasLikelihood::value>());
        doc.AddMember("likelihood", likelihood, allocator);

        return true;
    }

}
498 499 500
}

#endif // MESSAGES_H