`node yaml` C++ Examples

30 C++ code examples are found related to "node yaml". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: benchmark-test.cpp    From polygon_coverage_planning with GNU General Public License v3.0 6 votes vote down vote up
bool loadPolygonFromNode(const YAML::Node& node, Polygon_2* poly) {
  CHECK_NOTNULL(poly);
  if (!node) return false;
  YAML::Node points = node["points"];
  if (!points) return false;
  if (points.size() < 3) return false;
  poly->clear();

  for (size_t i = 0; i < points.size(); ++i) {
    YAML::Node point = points[i];
    if (!point["x"]) return false;
    if (!point["y"]) return false;
    Point_2 p(kMapScale * point["x"].as<double>(),
              kMapScale * point["y"].as<double>());
    poly->push_back(p);
  }

  return true;
} 
Example 2
Source File: parameter.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void Parameter::deserialize_yaml(const YAML::Node& n)
{
    if (!n["name"].IsDefined()) {
        return;
    }
    name_ = n["name"].as<std::string>();

    if (n["interactive"].IsDefined()) {
        interactive_ = n["interactive"].as<bool>();
        std::cout << "Param " << name_ << " is interactive" << std::endl;
    }
    if (n["dict"].IsDefined()) {
        dict_ = n["dict"].as<std::map<std::string, param::ParameterPtr>>();
    }

    try {
        doDeserialize(n);
    } catch (const std::exception& e) {
        std::cerr << "cannot deserialize parameter " << name() << ": " << e.what() << std::endl;
        throw e;
    }
} 
Example 3
Source File: designerio.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void DesignerIO::saveBox(const UUID& node_uuid, GraphView* view, YAML::Node& yaml)
{
    NodeBox* box = view->getBox(node_uuid);
    NodeAdapter::Ptr na = box->getNodeAdapter();
    GenericStatePtr m = na->getState();
    if (m) {
        YAML::Node doc;
        doc["uuid"] = node_uuid.getFullName();

        YAML::Node state;
        m->writeYaml(state);
        doc["state"] = state;

        yaml.push_back(doc);
    }
} 
Example 4
Source File: parameter.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void Parameter::serialize_yaml(YAML::Node& n) const
{
    n["type"] = getParameterType();
    n["name"] = name();

    if (interactive_) {
        n["interactive"] = interactive_;
    }

    if (!dict_.empty()) {
        n["dict"] = dict_;
    }

    try {
        doSerialize(n);
    } catch (const std::exception& e) {
        std::cerr << "cannot serialize parameter " << name() << ": " << e.what() << std::endl;
        throw e;
    }
} 
Example 5
Source File: camera-factory.cc    From aslam_cv2 with Apache License 2.0 6 votes vote down vote up
Camera::Ptr createCamera(const YAML::Node& yaml_node) {
  if (!yaml_node.IsDefined() || yaml_node.IsNull()) {
    LOG(ERROR) << "Camera YAML node is invalid.";
    return nullptr;
  }

  if (!yaml_node.IsMap()) {
    LOG(ERROR)
        << "Intrinsics node for camera is not a map.";
    return nullptr;
  }

  std::string camera_type;
  if (!YAML::safeGet(yaml_node, "type", &camera_type)) {
    LOG(ERROR) << "Unable to get camera type.";
    return nullptr;
  }

  // Based on the type deserialize the camera
  Camera::Ptr camera;
  if (camera_type == "pinhole") {
    camera = std::dynamic_pointer_cast<Camera>(aligned_shared<PinholeCamera>());
  } else if 
Example 6
Source File: generic_state.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void GenericState::readYaml(const YAML::Node& node)
{
    if (node["params"].IsDefined()) {
        auto serialized_params = node["params"].as<std::map<std::string, csapex::param::Parameter::Ptr> >();
        for (auto pair : serialized_params) {
            if(pair.first != pair.second->name()) {
                // backwards compatibility: if the key of a parameter is not equal to its name, rename the parameter
                pair.second->setName(pair.first);
            }
            auto pos = params.find(pair.first);
            if (pos == params.end()) {
                params[pair.first] = pair.second;
                legacy_parameter_added(params[pair.first]);
            } else {
                param::ParameterPtr p = pos->second;
                p->cloneDataFrom(*pair.second);
            }
            legacy.insert(pair.first);
        }
    } 
Example 7
Source File: io.cpp    From mav_trajectory_generation with Apache License 2.0 6 votes vote down vote up
bool segmentFromYaml(const YAML::Node& node, Segment* segment) {
  CHECK_NOTNULL(segment);

  if (!node[kNumCoefficientsKey]) return false;
  if (!node[kDimKey]) return false;
  if (!node[kSegmentTimeKey]) return false;
  if (!node[kCoefficientsKey]) return false;
  if (!node[kCoefficientsKey].IsSequence()) return false;

  *segment =
      Segment(node[kNumCoefficientsKey].as<int>(), node[kDimKey].as<int>());

  for (size_t i = 0; i < segment->D(); ++i) {
    Eigen::VectorXd coeffs;
    if (!coefficientsFromYaml(node[kCoefficientsKey][i], &coeffs)) return false;
    (*segment)[i] = coeffs;
  }

  segment->setTimeNSec(node[kSegmentTimeKey].as<uint64_t>());

  return true;
} 
Example 8
Source File: graphio.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void GraphIO::saveGraphTo(YAML::Node& yaml)
{
    TimerPtr timer = getProfiler()->getTimer("save graph");
    timer->restart();

    yaml["version"] = csapex::info::CSAPEX_VERSION;

    saveNodes(yaml);
    saveConnections(yaml);

    {
        auto interlude = timer->step("save view");
        saveViewRequest(graph_, yaml);
    }

    timer->finish();
} 
Example 9
Source File: io.cpp    From mav_trajectory_generation with Apache License 2.0 5 votes vote down vote up
bool trajectoryFromYaml(const YAML::Node& node, Trajectory* trajectory) {
  CHECK_NOTNULL(trajectory);

  Segment::Vector segments;
  if (!segmentsFromYaml(node[kSegmentsKey], &segments)) return false;
  trajectory->setSegments(segments);

  return true;
} 
Example 10
Source File: output_text_parameter.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void OutputTextParameter::doDeserialize(const YAML::Node& n)
{
    if (!n["text"].IsDefined()) {
        return;
    }

    text_ = n["text"].as<std::string>();
} 
Example 11
Source File: io.cpp    From mav_trajectory_generation with Apache License 2.0 5 votes vote down vote up
bool segmentsFromYaml(const YAML::Node& node, Segment::Vector* segments) {
  CHECK_NOTNULL(segments);
  if (!node.IsSequence()) return false;

  segments->resize(node.size(), Segment(0, 0));
  for (size_t i = 0; i < node.size(); ++i) {
    if (!segmentFromYaml(node[i], &(*segments)[i])) return false;
  }

  return true;
} 
Example 12
Source File: io.cpp    From mav_trajectory_generation with Apache License 2.0 5 votes vote down vote up
bool coefficientsFromYaml(const YAML::Node& node,
                          Eigen::VectorXd* coefficients) {
  CHECK_NOTNULL(coefficients);
  if (!node.IsSequence()) return false;
  *coefficients = Eigen::VectorXd(node.size());
  for (std::size_t i = 0; i < node.size(); ++i) {
    (*coefficients)(i) = node[i].as<double>();
  }
  return true;
} 
Example 13
Source File: yaml_preprocessor_test.cpp    From flatland with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void compareNodes(const char *p1, YAML::Node &a, YAML::Node &b) {
  try {
    EXPECT_STREQ(b[p1].as<std::string>().c_str(),
                 a[p1].as<std::string>().c_str())
        << "at " << p1;
  } catch (...) {
    ADD_FAILURE() << "Failure to compare " << p1;
  }
} 
Example 14
Source File: camera.cc    From aslam_cv2 with Apache License 2.0 5 votes vote down vote up
bool Camera::loadFromYamlNodeImpl(const YAML::Node& yaml_node) {
  if (!yaml_node.IsMap()) {
    LOG(ERROR) << "Unable to parse the camera because the node is not a map.";
    return false;
  }

  // Determine the distortion type. Start with no distortion.
  const YAML::Node& distortion_config = yaml_node["distortion"];
  if (distortion_config.IsDefined() && !distortion_config.IsNull()) {
    if (!distortion_config.IsMap()) {
      LOG(ERROR) << "Unable to parse the camera because the distortion node is "
                    "not a map.";
      return false;
    }

    std::string distortion_type;
    Eigen::VectorXd distortion_parameters;
    if (YAML::safeGet(distortion_config, "type", &distortion_type) &&
        YAML::safeGet(
            distortion_config, "parameters", &distortion_parameters)) {
      if (distortion_type == "none") {
        distortion_.reset(new aslam::NullDistortion());
      } else if (distortion_type == "equidistant") {
        if (aslam::EquidistantDistortion::areParametersValid(
                distortion_parameters)) {
          distortion_.reset(
              new aslam::EquidistantDistortion(distortion_parameters));
        } else {
          LOG(ERROR)
              << "Invalid distortion parameters for the Equidistant distortion "
                 "model: "
              << distortion_parameters.transpose() << std::endl
              << "See aslam::EquidistantDistortion::areParametersValid(...) "
                 "for conditions on what "
                 "valid Equidistant distortion parameters look like.";
          return false;
        }
      } else if 
Example 15
Source File: config.cpp    From libwave with MIT License 5 votes vote down vote up
ConfigStatus ConfigParser::getYamlNode(const std::string &key,
                                       YAML::Node &node) {
    std::string element;
    std::istringstream iss(key);
    std::vector<YAML::Node> traversal;

    // pre-check
    if (this->config_loaded == false) {
        return ConfigStatus::FileError;
    }

    // recurse down config key
    traversal.push_back(this->root);
    while (std::getline(iss, element, '.')) {
        traversal.push_back(traversal.back()[element]);
    }
    node = traversal.back();
    // Note:
    //
    //    yaml_node = yaml_node["some_level_deeper"];
    //
    // YAML::Node is mutable, by doing the above it destroys the parsed yaml
    // tree/graph, to avoid this problem we store the visited YAML::Node into
    // a std::vector and return the last visited YAML::Node

    return ConfigStatus::OK;
} 
Example 16
Source File: yaml_preprocessor_test.cpp    From flatland with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void compareNodes(const char *p1, int p2, YAML::Node &a, YAML::Node &b) {
  try {
    EXPECT_STREQ(b[p1][p2].as<std::string>().c_str(),
                 a[p1][p2].as<std::string>().c_str())
        << "at " << p1 << ":" << p2;
  } catch (...) {
    ADD_FAILURE() << "Failure to compare " << p1 << ":" << p2;
  }
} 
Example 17
Source File: yaml_preprocessor_test.cpp    From flatland with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void compareNodes(const char *p1, const char *p2, YAML::Node &a,
                  YAML::Node &b) {
  try {
    EXPECT_STREQ(b[p1][p2].as<std::string>().c_str(),
                 a[p1][p2].as<std::string>().c_str())
        << "at " << p1 << ":" << p2;
  } catch (...) {
    ADD_FAILURE() << "Failure to compare " << p1 << ":" << p2;
  }
} 
Example 18
Source File: sensor-system.cc    From maplab with Apache License 2.0 5 votes vote down vote up
bool SensorSystem::deserialize(const YAML::Node& sensor_system_node) {
  CHECK(!sensor_system_node.IsNull());
  std::string id_as_string;
  if (YAML::safeGet(
          sensor_system_node,
          static_cast<std::string>(kYamlFieldNameSensorSysteId),
          &id_as_string)) {
    CHECK(!id_as_string.empty());
    CHECK(id_.fromHexString(id_as_string));
  } else { 
Example 19
Source File: clipboard.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void ClipBoard::set(const YAML::Node& serialized)
{
    QMimeData* mime = new QMimeData;
    std::stringstream yaml_txt;
    yaml_txt << serialized;

    auto data = QString::fromStdString(yaml_txt.str()).toUtf8();
    for (const QString& type : valid_types) {
        mime->setData(type, data);
    }

    QApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
} 
Example 20
Source File: designerio.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void DesignerIO::saveBoxes(YAML::Node& yaml, const GraphFacade* graph, GraphView* view)
{
    YAML::Node adapters(YAML::NodeType::Sequence);
    for (const UUID& uuid : graph->enumerateAllNodes()) {
        saveBox(uuid, view, adapters);
    }
    yaml["adapters"] = adapters;
} 
Example 21
Source File: graphio.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void GraphIO::loadGraphFrom(const YAML::Node& doc)
{
    TimerPtr timer = getProfiler()->getTimer("load graph");
    timer->restart();

    SemanticVersion version;
    if(doc["version"].IsDefined()) {
        version = doc["version"].as<SemanticVersion>();
    } else { 
Example 22
Source File: message_serializer.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
TokenData::Ptr MessageSerializer::deserializeYamlMessage(const YAML::Node& node)
{
    MessageSerializer& i = instance();

    std::string type;
    try {
        type = node["type"].as<std::string>();

    } catch (const std::exception& e) {
        throw DeserializationError("cannot get type");
    }

    if (i.type_to_yaml_converter.empty()) {
        throw DeserializationError("MessageSerializer: no connection types registered!");
    }

    std::string converter_type = type;
    const std::string ns = "csapex::connection_types::";
    if (type.find(ns) != std::string::npos) {
        converter_type.erase(0, ns.size());
    }

    if (i.type_to_yaml_converter.find(converter_type) == i.type_to_yaml_converter.end()) {
        throw DeserializationError(std::string("cannot deserialize, no such type (") + type + ")");
    }

    TokenData::Ptr msg = MessageFactory::createMessage(converter_type);
    try {
        i.type_to_yaml_converter.at(converter_type).decoder(node["data"], *msg);
    } catch (const YAML::Exception& e) {
        throw DeserializationError(std::string("error while deserializing: ") + e.msg);
    }

    return msg;
} 
Example 23
Source File: designerio.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void DesignerIO::loadBoxes(const YAML::Node& doc, GraphView* view)
{
    if (doc["adapters"].IsDefined()) {
        const YAML::Node& adapters = doc["adapters"];
        for (std::size_t i = 0; i < adapters.size(); ++i) {
            const YAML::Node& e = adapters[i];

            YAML::Node x = e["uuid"];
            apex_assert_hard(x.Type() == YAML::NodeType::Scalar);

            UUID uuid = UUIDProvider::makeUUID_without_parent(e["uuid"].as<std::string>());

            NodeBox* box = view->getBox(uuid);
            if (box) {
                NodeAdapter::Ptr na = box->getNodeAdapter();
                na->readLegacyYaml(e["state"]);

                /// deprecated:
                GenericStatePtr m = na->getState();
                if (m) {
                    m->readYaml(e["state"]);
                    box->getNodeAdapter()->setParameterState(m);
                }
            }
        }
    }
} 
Example 24
Source File: node_serializer.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void NodeSerializer::deserialize(csapex::Node& node, const YAML::Node& doc)
{
    const csapex::Node& const_node = node;
    auto fn = deserializers.find(std::type_index(typeid(const_node)));
    if (fn != deserializers.end()) {
        (fn->second)(node, doc);
    }
} 
Example 25
Source File: message_serializer.cpp    From csapex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
TokenData::Ptr MessageSerializer::readYaml(const YAML::Node& node)
{
    TokenData::Ptr msg = MessageSerializer::deserializeYamlMessage(node);
    if (!msg) {
        std::string type = node["type"].as<std::string>();
        throw DeserializationError(std::string("message type '") + type + "' unknown");
    }

    return msg;
} 
Example 26
Source File: sensor-extrinsics.cc    From maplab with Apache License 2.0 5 votes vote down vote up
bool Extrinsics::deserialize(const YAML::Node& yaml_node) {
  std::string type_as_string;
  if (YAML::safeGet(
          yaml_node, static_cast<std::string>(kYamlFieldNameExtrinsicsType),
          &type_as_string)) {
    CHECK(!type_as_string.empty());
    extrinsics_type_ = stringToExtrinsicsType(type_as_string);
  } else { 
Example 27
Source File: sensor-system.cc    From maplab with Apache License 2.0 5 votes vote down vote up
void SensorSystem::serialize(YAML::Node* yaml_node_ptr) const {
  YAML::Node& sensor_node = *CHECK_NOTNULL(yaml_node_ptr);

  CHECK(id_.isValid());
  sensor_node[static_cast<std::string>(kYamlFieldNameSensorSysteId)] =
      id_.hexString();

  CHECK(reference_sensor_id_.isValid());
  sensor_node[static_cast<std::string>(kYamlFieldNameReferenceSensorId)] =
      reference_sensor_id_.hexString();

  YAML::Node sensors_extrinsics_node(YAML::NodeType::Sequence);
  for (const ExtrinsicsMap::value_type& sensor_with_extrinsics :
       sensor_id_to_extrinsics_map_) {
    YAML::Node sensor_extrinsics_node;
    const SensorId& sensor_id = sensor_with_extrinsics.first;
    CHECK(sensor_id.isValid());
    sensor_extrinsics_node[static_cast<std::string>(kYamlFieldNameSensorId)] =
        sensor_id.hexString();
    YAML::Node extrinsics_node;
    sensor_with_extrinsics.second.serialize(&extrinsics_node);
    sensor_extrinsics_node[static_cast<std::string>(kYamlFieldNameExtrinsics)] =
        extrinsics_node;
    sensors_extrinsics_node.push_back(sensor_extrinsics_node);
  }
  sensor_node[static_cast<std::string>(kYamlFieldNameSensorIdWithExtrinsics)] =
      sensors_extrinsics_node;
} 
Example 28
Source File: sensor-factory.cc    From maplab with Apache License 2.0 5 votes vote down vote up
Sensor::UniquePtr createSensorFromYaml(const YAML::Node& sensor_node) {
  std::string sensor_type_as_string;
  CHECK(sensor_node.IsDefined() && sensor_node.IsMap());
  if (!YAML::safeGet(sensor_node, "sensor_type", &sensor_type_as_string)) {
    LOG(FATAL) << "Unable to retrieve the sensor type from the given "
               << "YAML node.";
  }

  const SensorType sensor_type = stringToSensorType(sensor_type_as_string);

  switch (sensor_type) {
    case SensorType::kImu:
      return sensors::internal::createFromYaml<Imu>(sensor_node);
      break;
    case SensorType::kRelative6DoFPose:
      return sensors::internal::createFromYaml<Relative6DoFPose>(sensor_node);
      break;
    case SensorType::kGpsUtm:
      return sensors::internal::createFromYaml<GpsUtm>(sensor_node);
      break;
    case SensorType::kGpsWgs:
      return sensors::internal::createFromYaml<GpsWgs>(sensor_node);
      break;
    default:
      LOG(ERROR) << "Unknown sensor type: " << static_cast<int>(sensor_type);
      break;
  }

  return nullptr;
} 
Example 29
Source File: sensor-extrinsics.cc    From maplab with Apache License 2.0 5 votes vote down vote up
Extrinsics::UniquePtr Extrinsics::createFromYaml(const YAML::Node& yaml_node) {
  Extrinsics::UniquePtr extrinsics(new Extrinsics());
  if (!extrinsics->deserialize(yaml_node)) {
    LOG(ERROR) << "YAML deserialization failed.";
    extrinsics.release();
  }
  return extrinsics;
} 
Example 30
Source File: sensor-system.cc    From maplab with Apache License 2.0 5 votes vote down vote up
SensorSystem::UniquePtr SensorSystem::createFromYaml(
    const YAML::Node& yaml_node) {
  SensorSystem::UniquePtr sensor_system(new SensorSystem);
  if (!sensor_system->deserialize(yaml_node)) {
    sensor_system.reset();
  }
  return sensor_system;
}