Skip to content

Commit

Permalink
Moved the parsing of the labels options parsing to a specific method.
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Dafarra committed Jan 17, 2022
1 parent 25a1f97 commit 91dea3c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 99 deletions.
108 changes: 108 additions & 0 deletions src/devices/openxrheadset/LabelPortToQuadLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,111 @@ void LabelPortToQuadLayer::setEnabled(bool enabled)
m_timeoutExpired = false;
m_enabled = enabled;
}

bool LabelPortToQuadLayer::Options::parseFromConfigurationFile(const std::string& inputPortName, yarp::os::Searchable &labelGroup)
{
portName = inputPortName;
labelPrefix = labelGroup.check("prefix", yarp::os::Value("")).asString();
labelSuffix = labelGroup.check("suffix", yarp::os::Value("")).asString();
fontPath = labelGroup.check("font", yarp::os::Value("Roboto/Roboto-Black.ttf")).asString();
pixelSize = labelGroup.check("pixel_size", yarp::os::Value(64)).asInt32();
automaticallyEnabled = labelGroup.check("automatically_enabled", yarp::os::Value(true)).asBool();
disableTimeoutInS = labelGroup.check("disable_timeout_in_S", yarp::os::Value(-1.0)).asFloat64();

std::string inputHorAlignement = labelGroup.check("horizontal_alignement", yarp::os::Value("center")).asString();
std::transform(inputHorAlignement.begin(), inputHorAlignement.end(), inputHorAlignement.begin(), ::tolower);
if (inputHorAlignement == "left")
{
horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Left;
}
else if (inputHorAlignement == "right")
{
horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Right;
}
else if (inputHorAlignement == "center")
{
horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Center;
}
else
{
yCError(OPENXRHEADSET) << "Unrecognized horizontal_alignement."
<< "Allowed entries: \"left\", \"right\", \"center\".";
return false;
}

std::string inputVertAlignement = labelGroup.check("vertical_alignement", yarp::os::Value("center")).asString();
std::transform(inputVertAlignement.begin(), inputVertAlignement.end(), inputVertAlignement.begin(), ::tolower);
if (inputVertAlignement == "top")
{
verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Top;
}
else if (inputVertAlignement == "bottom")
{
verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Bottom;
}
else if (inputVertAlignement == "center")
{
verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Center;
}
else
{
yCError(OPENXRHEADSET) << "Unrecognized vertical_alignement."
<< "Allowed entries: \"top\", \"bottom\", \"center\".";
return false;
}

auto fetchColor = [](const yarp::os::Searchable& group, const std::string& paramName,
const Eigen::Vector4f& defaultColor, Eigen::Vector4f& outputColor) -> bool
{
if (!group.check(paramName))
{
outputColor = defaultColor;
return true;
}

yarp::os::Value list = group.find(paramName);
if (!list.isList())
{
yCError(OPENXRHEADSET) << "The parameter" << paramName << "is specified but it is not a list.";
return false;
}

yarp::os::Bottle* yarpVector = list.asList();

if (yarpVector->size() != 4)
{
yCError(OPENXRHEADSET) << "The parameter" << paramName << "is specified but it is not a list of size 4.";
return false;
}

for (size_t i = 0; i < 4; ++i)
{
if (!yarpVector->get(i).isFloat64())
{
yCError(OPENXRHEADSET) << "The entry" << i << " of parameter" << paramName << "is not a double.";
return false;
}
double value = yarpVector->get(i).asFloat64();

if (value < 0.0 || value > 1.0)
{
yCError(OPENXRHEADSET) << "The entry" << i << " of parameter" << paramName << "is not in the range [0, 1].";
return false;
}
outputColor[i] = value;
}

return true;
};

if (!fetchColor(labelGroup, "color", {1.0, 1.0, 1.0, 1.0}, labelColor))
{
return false;
}

if (!fetchColor(labelGroup, "background_color", {0.0, 0.0, 0.0, 0.0}, backgroundColor))
{
return false;
}
return true;
}
3 changes: 3 additions & 0 deletions src/devices/openxrheadset/LabelPortToQuadLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <Eigen/Geometry>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/LogStream.h>
#include <yarp/os/Searchable.h>

#include <thread>
#include <cassert>
Expand Down Expand Up @@ -55,6 +56,8 @@ class LabelPortToQuadLayer
Eigen::Vector4f labelColor;
VerticalAlignement verticalAlignement{VerticalAlignement::Center};
HorizontalAlignement horizontalAlignement{HorizontalAlignement::Center};

bool parseFromConfigurationFile(const std::string &portName, yarp::os::Searchable& labelGroup);
};

bool initialize(const Options& options);
Expand Down
103 changes: 4 additions & 99 deletions src/devices/openxrheadset/OpenXrHeadset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,108 +313,13 @@ bool yarp::dev::OpenXrHeadset::open(yarp::os::Searchable &cfg)
label.x = labelGroup.find("x").asFloat64();
label.y = labelGroup.find("y").asFloat64();
label.z = -std::max(0.01, std::abs(labelGroup.find("z").asFloat64())); //make sure that z is negative and that is at least 0.01 in modulus
std::transform(groupName.begin(), groupName.end(), groupName.begin(), ::tolower);
label.options.portName = m_prefix + "/" + groupName;
label.options.labelPrefix = labelGroup.check("prefix", yarp::os::Value("")).asString();
label.options.labelSuffix = labelGroup.check("suffix", yarp::os::Value("")).asString();
label.options.fontPath = labelGroup.check("font", yarp::os::Value("Roboto/Roboto-Black.ttf")).asString();
label.options.pixelSize = labelGroup.check("pixel_size", yarp::os::Value(64)).asInt32();
label.options.automaticallyEnabled = labelGroup.check("automatically_enabled", yarp::os::Value(true)).asBool();
label.options.disableTimeoutInS = labelGroup.check("disable_timeout_in_S", yarp::os::Value(-1.0)).asFloat64();

std::string horizontalAlignement = labelGroup.check("horizontal_alignement", yarp::os::Value("center")).asString();
std::transform(horizontalAlignement.begin(), horizontalAlignement.end(), horizontalAlignement.begin(), ::tolower);
if (horizontalAlignement == "left")
{
label.options.horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Left;
}
else if (horizontalAlignement == "right")
{
label.options.horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Right;
}
else if (horizontalAlignement == "center")
{
label.options.horizontalAlignement = LabelPortToQuadLayer::Options::HorizontalAlignement::Center;
}
else
{
yCError(OPENXRHEADSET) << "Unrecognized horizontal_alignement in" << groupName + "."
<< "Allowed entries: \"left\", \"right\", \"center\".";
return false;
}

std::string verticalAlignement = labelGroup.check("vertical_alignement", yarp::os::Value("center")).asString();
std::transform(verticalAlignement.begin(), verticalAlignement.end(), verticalAlignement.begin(), ::tolower);
if (verticalAlignement == "top")
{
label.options.verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Top;
}
else if (verticalAlignement == "bottom")
{
label.options.verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Bottom;
}
else if (verticalAlignement == "center")
{
label.options.verticalAlignement = LabelPortToQuadLayer::Options::VerticalAlignement::Center;
}
else
{
yCError(OPENXRHEADSET) << "Unrecognized vertical_alignement in" << groupName + "."
<< "Allowed entries: \"top\", \"bottom\", \"center\".";
return false;
}

auto fetchColor = [](const std::string& groupName, const yarp::os::Bottle& group,
const std::string& paramName, const Eigen::Vector4f& defaultColor, Eigen::Vector4f& outputColor) -> bool
{
if (!group.check(paramName))
{
outputColor = defaultColor;
return true;
}

yarp::os::Value list = group.find(paramName);
if (!list.isList())
{
yCError(OPENXRHEADSET) << "The parameter" << paramName << "is specified in" << groupName << "but it is not a list.";
return false;
}

yarp::os::Bottle* yarpVector = list.asList();

if (yarpVector->size() != 4)
{
yCError(OPENXRHEADSET) << "The parameter" << paramName << "is specified in" << groupName << "but it is not a list of size 4.";
return false;
}

for (size_t i = 0; i < 4; ++i)
{
if (!yarpVector->get(i).isFloat64())
{
yCError(OPENXRHEADSET) << "The entry" << i << " of parameter" << paramName << "in" << groupName << "is not a double.";
return false;
}
double value = yarpVector->get(i).asFloat64();

if (value < 0.0 || value > 1.0)
{
yCError(OPENXRHEADSET) << "The entry" << i << " of parameter" << paramName << "in" << groupName << "is not in the range [0, 1].";
return false;
}
outputColor[i] = value;
}

return true;
};

if (!fetchColor(groupName, labelGroup, "color", {1.0, 1.0, 1.0, 1.0}, label.options.labelColor))
{
return false;
}
std::transform(groupName.begin(), groupName.end(), groupName.begin(), ::tolower);
std::string portName = m_prefix + "/" + groupName;

if (!fetchColor(groupName, labelGroup, "background_color", {0.0, 0.0, 0.0, 0.0}, label.options.backgroundColor))
if (!label.options.parseFromConfigurationFile(portName, labelGroup))
{
yCError(OPENXRHEADSET) << "Failed to parse" << groupName;
return false;
}
}
Expand Down

0 comments on commit 91dea3c

Please sign in to comment.