tile.h 3.26 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#pragma once

#include "ros_bridge/include/messages/geographic_msgs/geopoint.h"
#include "ros_bridge/rapidjson/include/rapidjson/document.h"

#include <vector>

namespace ros_bridge {
//! @brief Namespace containing classes and methodes ros message generation.
namespace messages {
//! @brief Namespace containing classes and methodes for nemo_msgs
//! generation.
namespace nemo_msgs {
//! @brief Namespace containing methodes for nemo_msgs/tile message
//! generation.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
16
namespace tile {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
17 18 19 20 21 22 23 24 25 26

std::string messageType();

namespace {
const char *progressKey = "progress";
const char *idKey = "id";
const char *tileKey = "tile";
} // namespace

//! @brief C++ representation of nemo_msgs/tile message
Valentin Platzgummer's avatar
Valentin Platzgummer committed
27 28
template <class GeoPoint = geographic_msgs::geo_point::GeoPoint,
          template <class, class...> class Container = std::vector>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class GenericTile {
public:
  typedef Container<GeoPoint> GeoContainer;
  GenericTile() {}
  GenericTile(const GeoContainer &tile, double progress, long id)
      : _tile(tile), _id(id), _progress(progress) {}

  long id() const { return _id; }
  void setId(long id) { _id = id; }

  double progress() const { return _progress; }
  void setProgress(double progress) { _progress = progress; }

  const GeoContainer &tile() const { return _tile; }
  GeoContainer &tile() { return _tile; }
  void setTile(const GeoContainer &tile) { _tile = tile; }

protected:
  long _id;
  double _progress;
  GeoContainer _tile;
};
typedef GenericTile<> Tile;

template <class TileType>
bool toJson(const TileType &tile, rapidjson::Value &value,
            rapidjson::Document::AllocatorType &allocator) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
56 57 58 59 60

  using namespace rapidjson;
  value.AddMember(Value(idKey, allocator), Value(tile.id()), allocator);
  value.AddMember(Value(progressKey, allocator), Value(tile.progress()),
                  allocator);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61 62 63 64

  using namespace messages::geographic_msgs;
  rapidjson::Value geoPoints(rapidjson::kArrayType);
  for (unsigned long i = 0; i < std::uint64_t(tile.tile().size()); ++i) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
65
    rapidjson::Value geoPoint(rapidjson::kObjectType);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
66 67 68 69 70
    if (!geo_point::toJson(tile.tile()[i], geoPoint, allocator)) {
      return false;
    }
    geoPoints.PushBack(geoPoint, allocator);
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
71 72 73

  rapidjson::Value key(tileKey, allocator);
  value.AddMember(key, geoPoints, allocator);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
74 75 76 77

  return true;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
78 79
template <class TileType>
bool fromJson(const rapidjson::Value &value, TileType &p) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
80 81 82 83 84 85 86 87
  if (!value.HasMember(progressKey) || !value[progressKey].IsDouble()) {
    return false;
  }

  if (!value.HasMember(idKey) || !value[idKey].IsInt()) {
    return false;
  }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
88 89 90 91
  if (!value.HasMember(tileKey) || !value[tileKey].IsArray()) {
    return false;
  }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
92 93 94
  p.setId(value[idKey].GetInt());
  p.setProgress(value[progressKey].GetDouble());

Valentin Platzgummer's avatar
Valentin Platzgummer committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  using namespace geographic_msgs;

  const auto &jsonArray = value[tileKey].GetArray();
  p.tile().clear();
  p.tile().reserve(jsonArray.Size());
  typedef decltype(p.tile()[0]) PointTypeCVR;
  typedef
      typename std::remove_cv_t<typename std::remove_reference_t<PointTypeCVR>>
          PointType;
  for (long i = 0; i < jsonArray.Size(); ++i) {
    PointType pt;
    if (!geo_point::fromJson(jsonArray[i], pt)) {
      return false;
    }
    p.tile().push_back(std::move(pt));
  }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
112 113 114
  return true;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
115
} // namespace tile
Valentin Platzgummer's avatar
Valentin Platzgummer committed
116 117 118
} // namespace nemo_msgs
} // namespace messages
} // namespace ros_bridge