Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support printing sdf poses in degrees and allow snapping to commonly used angles #689

Merged
merged 60 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
dffcb8d
Ruby option to print in_degrees or snap_to_degrees
aaronchongth Sep 6, 2021
a548788
Basic PrintConfig added
aaronchongth Sep 7, 2021
7bff621
PrintConfig gets passed into printing implementations of Element and …
aaronchongth Sep 7, 2021
648e2c1
Adding basic test for print options
aaronchongth Sep 13, 2021
942a180
Reverting to PrintConfig with basic API
aaronchongth Sep 14, 2021
8dbd8a3
Moved creation of PrintConfig into ign functions
aaronchongth Oct 5, 2021
d1e9dd1
Param value GetPoseAsString and tests
aaronchongth Oct 14, 2021
347d92d
Moved attribute painting to its own function, fixed test strings
aaronchongth Oct 18, 2021
9de0da6
Added basic tests for pose rotation input as quaternions
aaronchongth Oct 18, 2021
91fb532
Using different flags for ign sdf -p, allow snapping to different values
aaronchongth Oct 20, 2021
085a372
Disabling test on windows, fixing comment
aaronchongth Nov 2, 2021
ac0df14
Remove stale function, fixed linting
aaronchongth Nov 2, 2021
8dd6e5e
Adding tolerance as a argument, added tests
aaronchongth Nov 5, 2021
bff39f4
Use 3 spaces when changing rotation formats or snapping to degrees
aaronchongth Nov 6, 2021
c126e19
Added check for tolerance larger than snapping interval
aaronchongth Nov 8, 2021
f600b74
Moving PrintAttributes to ElementPrivate to remain ABI stability
aaronchongth Nov 8, 2021
0c0f296
Using true/false instead of 1/0
aaronchongth Nov 8, 2021
7cc450c
Remove use of SDF_ASSERT in GetAsString
aaronchongth Nov 8, 2021
2d3df38
Added tests for //include/pose
aaronchongth Nov 8, 2021
dafe4d1
Adding parsing passing test for empty quat_xyzw pose
aaronchongth Oct 11, 2021
2299089
Added check for default string values to be modified when rotation_fo…
aaronchongth Oct 11, 2021
735042d
Added tests
aaronchongth Oct 25, 2021
840c7b4
Reparsing translates default value into string to be used if values h…
aaronchongth Oct 25, 2021
c9dd236
Using StringFromValueImpl for getting strings from all ParamVariants
aaronchongth Oct 25, 2021
832fca1
Refactor pose string from value into its own function
aaronchongth Oct 25, 2021
bf6c3a2
Fixing casting erroerror, added documentation and tests for tolerance…
aaronchongth Nov 8, 2021
a56be39
Correcting stale comments
aaronchongth Nov 8, 2021
3ae2518
Fixing snapToInterval math, added more tests
aaronchongth Nov 9, 2021
3456422
Removed unneeded visibility macro
aaronchongth Nov 9, 2021
4cf502c
Adding return documentation and using const reference to variant inst…
aaronchongth Nov 11, 2021
e903b13
Returning string directly, removing stale _config, reverting strValue…
aaronchongth Nov 11, 2021
4eaf06b
Remove use of assertions
aaronchongth Nov 11, 2021
7df7a9f
Suggested changes to #729 (#748)
azeey Nov 12, 2021
ef54667
Using three space delimiter between position and rotation if attribut…
aaronchongth Nov 12, 2021
cb45ae4
Merge branch 'sdf12' into aaron/pose-quat-empty-value
aaronchongth Nov 12, 2021
3e0076e
Merge branch 'sdf12' into aaron/pose-quat-empty-value
aaronchongth Nov 23, 2021
594506a
Added comment regarding use of default PrintConfig in Reparse
aaronchongth Nov 23, 2021
92e0a33
Merge branch 'aaron/pose-quat-empty-value' into aaron/snapping-rotati…
aaronchongth Nov 23, 2021
9b1567a
Adding equality comparison for PrintConfig
aaronchongth Nov 27, 2021
2bf557c
Removed stale include
aaronchongth Nov 27, 2021
8b4f471
Uniied string and value parsing behavior, and modified necessary tests
aaronchongth Nov 27, 2021
fa28849
Merge branch 'sdf12' into aaron/snapping-rotation-printouts
aaronchongth Dec 6, 2021
621933c
Overloaded function to maintain ABI stability
aaronchongth Dec 6, 2021
d3b1cc1
Merge branch 'sdf12' into aaron/snapping-rotation-printouts
aaronchongth Dec 13, 2021
08dcacc
Fixing missing space in test for exec command
aaronchongth Dec 13, 2021
be3a716
Adding comment regarding attributeExceptions
aaronchongth Dec 13, 2021
ea991b2
Indenting help message, adding test for shuffling command flags
aaronchongth Dec 13, 2021
35be3a6
Modifying cmd flag shuffling test to handling flags with arguments too
aaronchongth Dec 13, 2021
b426498
Removed Get from PrintConfig getter functions
aaronchongth Dec 13, 2021
b69f448
Using std optional's converting constructor
aaronchongth Dec 13, 2021
236e858
Modified snapToInterval implementation, added test
aaronchongth Dec 14, 2021
4667c4b
Added bool type specific value parser, values are parsed using ParamS…
aaronchongth Dec 14, 2021
a7dfafb
Merge branch 'sdf12' into aaron/snapping-rotation-printouts
aaronchongth Dec 14, 2021
1400948
Reverting all unnecessary changes made in sdf12 to old tests
aaronchongth Dec 14, 2021
b3c4a5b
Merge branch 'sdf12' into aaron/snapping-rotation-printouts
aaronchongth Dec 18, 2021
b541f34
Added comparison for PreserveIncludes
aaronchongth Dec 18, 2021
0675571
Check for 'type' attribute in unknown elements as well, in order to p…
aaronchongth Dec 18, 2021
3c7d932
Only checking for pose related PrintConfig options for returning stri…
aaronchongth Dec 18, 2021
54b24a5
Added comment regarding sanitizing -0 in test outputs
aaronchongth Dec 21, 2021
27a6148
Merge branch 'sdf12' into aaron/snapping-rotation-printouts
aaronchongth Dec 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/sdf/Element.hh
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace sdf
/// \param[in] _prefix String value to prefix to the output.
/// \param[in] _includeDefaultElements flag to include default elements.
/// \param[in] _includeDefaultAttributes flag to include default attributes.
/// \param[in] _config Configuration for printing the values.
/// \param[in] _config Configuration for converting to string.
/// \return The string representation.
public: std::string ToString(
const std::string &_prefix,
Expand Down Expand Up @@ -589,7 +589,7 @@ namespace sdf
/// \param[in] _includeDefaultElements flag to include default elements.
/// \param[in] _includeDefaultAttributes flag to include default attributes.
/// \param[in] _config Configuration for printing values.
/// \param[out] _out the std::ostreamstream to write output to.
/// \param[out] _out the std::ostringstream to write output to.
private: void PrintValuesImpl(const std::string &_prefix,
bool _includeDefaultElements,
bool _includeDefaultAttributes,
Expand Down Expand Up @@ -685,6 +685,14 @@ namespace sdf

/// \brief XML path of this element.
public: std::string xmlPath;

/// \brief Generate the string (XML) for the attributes.
/// \param[in] _includeDefaultAttributes flag to include default attributes.
/// \param[in] _config Configuration for printing attributes.
/// \param[out] _out the std::ostringstream to write output to.
public: void PrintAttributes(bool _includeDefaultAttributes,
const PrintConfig &_config,
std::ostringstream &_out) const;
};

///////////////////////////////////////////////
Expand Down
33 changes: 25 additions & 8 deletions include/sdf/Param.hh
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,34 @@ namespace sdf
const std::string &_valueStr,
ParamVariant &_valueToSet) const;

/// \brief Method used to get the string representation from a ParamVariant
/// \brief Method used to get the string representation from a ParamVariant,
/// or the string that was used to set it.
/// \param[in] _config Print configuration for the string output
/// \param[in] _typeName The data type of the value
/// \param[in] _value The value
/// \param[in] _valueStr The string representation of the value
/// \return True if the string was successfully retrieved from the value,
/// false otherwise.
public: bool StringFromValueImpl(const PrintConfig &_config,
const std::string &_typeName,
const ParamVariant &_value,
std::string &_valueStr) const;
/// \param[out] _valueStr The output string.
/// \return True if the string was successfully retrieved, false otherwise.
public: bool StringFromValueImpl(
const PrintConfig &_config,
const std::string &_typeName,
const ParamVariant &_value,
std::string &_valueStr) const;

/// \brief Method used to get the string representation from a ParamVariant,
/// or the string that was used to set it.
/// \param[in] _config Print configuration for the string output
/// \param[in] _typeName The data type of the value
/// \param[in] _value The value
/// \param[in] _orignalStr The original string that was used to set the
/// value. A nullopt can be passed in if it is not available.
/// \param[out] _valueStr The output string.
/// \return True if the string was successfully retrieved, false otherwise.
public: bool StringFromValueImpl(
const PrintConfig &_config,
const std::string &_typeName,
const ParamVariant &_value,
const std::optional<std::string> &_originalStr,
std::string &_valueStr) const;

/// \brief Data type to string mapping
/// \return The type as a string, empty string if unknown type
Expand Down
40 changes: 39 additions & 1 deletion include/sdf/PrintConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef SDF_PRINTCONFIG_HH_
#define SDF_PRINTCONFIG_HH_

#include <optional>
#include <ignition/utils/ImplPtr.hh>

#include "sdf/sdf_config.h"
Expand All @@ -29,9 +30,41 @@ namespace sdf
/// This class contains configuration options for printing elements.
class SDFORMAT_VISIBLE PrintConfig
{
/// \brief Default constructor.
/// \brief Default constructor. All options are set to false by default.
public: PrintConfig();

/// \brief Sets the option for printing pose rotations in degrees if true,
/// otherwise they will be printed as radians by default.
/// \param[in] _value Whether to print pose rotations in degrees.
public: void SetRotationInDegrees(bool _value);

/// \brief Returns whether or not pose rotations should be printed in
/// degrees.
/// \return True if pose rotations are printed in degrees, false otherwise.
public: bool RotationInDegrees() const;

/// \brief Sets the option for printing pose rotation in degrees as well as
/// snapping the rotation to the desired interval, with the provided
/// tolerance.
/// \param[in] _interval Degrees interval to snap to, this value must be
/// larger than 0, and less than or equal to 360.
/// \param[in] _tolerance Tolerance which snapping occurs, this value must
/// be larger than 0, less than 360, and less than the provided interval.
/// \return True, unless any of the provided values are not valid.
public: bool SetRotationSnapToDegrees(unsigned int _interval,
double _tolerance);

/// \brief Returns the current degree value that pose rotations will snap to
/// when printed.
/// \return The assigned degrees interval value to snap to. If it has not
/// been assigned, a nullopt will be returned.
public: std::optional<unsigned int> RotationSnapToDegrees() const;

/// \brief Returns the tolerance for snapping degree values when printed.
/// \return The assigned tolerance value which allows snapping to happen. If
/// it has not been assigned, a nullopt will be returned.
public: std::optional<double> RotationSnapTolerance() const;

/// \brief Set print config to preserve <include> tags.
/// \param[in] _preserve True to preserve <include> tags.
/// False to expand included model.
Expand All @@ -42,6 +75,11 @@ namespace sdf
/// False if they are to be expanded.
public: bool PreserveIncludes() const;

/// \brief Return true if both PrintConfig objects contain the same values.
/// \param[in] _config PrintConfig to compare.
/// \return True if 'this' == _config.
public: bool operator==(const PrintConfig &_config) const;

/// \brief Private data pointer.
IGN_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ if (BUILD_SDF_TEST)
Pbr_TEST.cc
Physics_TEST.cc
Plugin_TEST.cc
PrintConfig_TEST.cc
Plane_TEST.cc
PrintConfig_TEST.cc
Root_TEST.cc
Scene_TEST.cc
SemanticPose_TEST.cc
Expand Down
62 changes: 46 additions & 16 deletions src/Element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ void Element::PrintDocLeftPane(std::string &_html, int _spacing,
_html += "</div>\n";
}

/////////////////////////////////////////////////
void Element::PrintValuesImpl(const std::string &_prefix,
bool _includeDefaultElements,
bool _includeDefaultAttributes,
Expand All @@ -513,22 +514,7 @@ void Element::PrintValuesImpl(const std::string &_prefix,
{
_out << _prefix << "<" << this->dataPtr->name;

Param_V::const_iterator aiter;
for (aiter = this->dataPtr->attributes.begin();
aiter != this->dataPtr->attributes.end(); ++aiter)
{
// Only print attribute values if they were set
// TODO(anyone): GetRequired is added here to support up-conversions where
// a new required attribute with a default value is added. We would have
// better separation of concerns if the conversion process set the
// required attributes with their default values.
if ((*aiter)->GetSet() || (*aiter)->GetRequired() ||
_includeDefaultAttributes)
{
_out << " " << (*aiter)->GetKey() << "='"
<< (*aiter)->GetAsString(_config) << "'";
}
}
this->dataPtr->PrintAttributes(_includeDefaultAttributes, _config, _out);

if (this->dataPtr->elements.size() > 0)
{
Expand Down Expand Up @@ -560,6 +546,50 @@ void Element::PrintValuesImpl(const std::string &_prefix,
}
}

/////////////////////////////////////////////////
void ElementPrivate::PrintAttributes(bool _includeDefaultAttributes,
const PrintConfig &_config,
std::ostringstream &_out) const
{
// Attribute exceptions are used in the event of a non-default PrintConfig
// which modifies the Attributes of this Element that are printed out. The
// modifications to an Attribute by a PrintConfig will overwrite the original
// existing Attribute when this Element is printed.
std::set<std::string> attributeExceptions;
if (this->name == "pose")
{
if (_config.RotationInDegrees() || _config.RotationSnapToDegrees())
{
attributeExceptions.insert("degrees");
_out << " " << "degrees='true'";

attributeExceptions.insert("rotation_format");
_out << " " << "rotation_format='euler_rpy'";
}
}

Param_V::const_iterator aiter;
for (aiter = this->attributes.begin();
aiter != this->attributes.end(); ++aiter)
{
// Only print attribute values if they were set
// TODO(anyone): GetRequired is added here to support up-conversions where
// a new required attribute with a default value is added. We would have
// better separation of concerns if the conversion process set the
// required attributes with their default values.
if ((*aiter)->GetSet() || (*aiter)->GetRequired() ||
_includeDefaultAttributes)
{
const std::string key = (*aiter)->GetKey();
const auto it = attributeExceptions.find(key);
if (it == attributeExceptions.end())
{
_out << " " << key << "='" << (*aiter)->GetAsString(_config) << "'";
}
}
}
}

/////////////////////////////////////////////////
void Element::PrintValues(std::string _prefix, const PrintConfig &_config) const
{
Expand Down
Loading