From 7907644e2dafbc1c22ca2b7eb05e726763f9d8b3 Mon Sep 17 00:00:00 2001 From: Laurent Pugin Date: Mon, 16 Dec 2024 14:42:08 +0100 Subject: [PATCH 1/2] Make typedef for data_DEGREES and default to MEI_UNSET --- libmei/addons/attdef.h | 5 +++++ libmei/datatypes.yml | 4 ++-- libmei/dist/attmodule.cpp | 12 ++++++------ libmei/dist/atts_gestural.cpp | 16 ++++++++-------- libmei/dist/atts_gestural.h | 12 ++++++------ libmei/dist/atts_shared.cpp | 8 ++++---- libmei/dist/atts_shared.h | 6 +++--- 7 files changed, 34 insertions(+), 29 deletions(-) diff --git a/libmei/addons/attdef.h b/libmei/addons/attdef.h index 04ee0b3c3ed..01494ea2bc8 100644 --- a/libmei/addons/attdef.h +++ b/libmei/addons/attdef.h @@ -73,6 +73,11 @@ enum data_BEATRPT_REND { */ typedef std::vector> data_BULGE; +/** + * MEI data.DEGREES + */ +typedef double data_DEGREES; + /** * MEI data.DURATION */ diff --git a/libmei/datatypes.yml b/libmei/datatypes.yml index 165866c920e..796033cb757 100644 --- a/libmei/datatypes.yml +++ b/libmei/datatypes.yml @@ -44,8 +44,6 @@ mapped: std::string data.CONFIDENCE: double - data.DEGREES: - double data.CLEFLINE: char data.COURSENUMBER: @@ -135,6 +133,8 @@ defaults: std::vector>() data_COMPASSDIRECTION: data_COMPASSDIRECTION() + data_DEGREES: + MEI_UNSET data_EVENTREL: data_EVENTREL() data_HEXNUM: diff --git a/libmei/dist/attmodule.cpp b/libmei/dist/attmodule.cpp index e0c35c7635a..b4dd982efb6 100644 --- a/libmei/dist/attmodule.cpp +++ b/libmei/dist/attmodule.cpp @@ -1756,11 +1756,11 @@ bool AttModule::SetGestural(Object *element, const std::string &attrType, const AttSoundLocation *att = dynamic_cast(element); assert(att); if (attrType == "azimuth") { - att->SetAzimuth(att->StrToDbl(attrValue)); + att->SetAzimuth(att->StrToDegrees(attrValue)); return true; } if (attrType == "elevation") { - att->SetElevation(att->StrToDbl(attrValue)); + att->SetElevation(att->StrToDegrees(attrValue)); return true; } } @@ -1878,10 +1878,10 @@ void AttModule::GetGestural(const Object *element, ArrayOfStrAttr *attributes) const AttSoundLocation *att = dynamic_cast(element); assert(att); if (att->HasAzimuth()) { - attributes->push_back({ "azimuth", att->DblToStr(att->GetAzimuth()) }); + attributes->push_back({ "azimuth", att->DegreesToStr(att->GetAzimuth()) }); } if (att->HasElevation()) { - attributes->push_back({ "elevation", att->DblToStr(att->GetElevation()) }); + attributes->push_back({ "elevation", att->DegreesToStr(att->GetElevation()) }); } } if (element->HasAttClass(ATT_TIMESTAMPGES)) { @@ -3252,7 +3252,7 @@ bool AttModule::SetShared(Object *element, const std::string &attrType, const st return true; } if (attrType == "rotate") { - att->SetRotate(att->StrToDbl(attrValue)); + att->SetRotate(att->StrToDegrees(attrValue)); return true; } } @@ -4856,7 +4856,7 @@ void AttModule::GetShared(const Object *element, ArrayOfStrAttr *attributes) attributes->push_back({ "lry", att->IntToStr(att->GetLry()) }); } if (att->HasRotate()) { - attributes->push_back({ "rotate", att->DblToStr(att->GetRotate()) }); + attributes->push_back({ "rotate", att->DegreesToStr(att->GetRotate()) }); } } if (element->HasAttClass(ATT_COORDINATEDUL)) { diff --git a/libmei/dist/atts_gestural.cpp b/libmei/dist/atts_gestural.cpp index 28917cfcbf6..b45d22c563b 100644 --- a/libmei/dist/atts_gestural.cpp +++ b/libmei/dist/atts_gestural.cpp @@ -473,20 +473,20 @@ AttSoundLocation::AttSoundLocation() : Att() void AttSoundLocation::ResetSoundLocation() { - m_azimuth = 0.0; - m_elevation = 0.0; + m_azimuth = MEI_UNSET; + m_elevation = MEI_UNSET; } bool AttSoundLocation::ReadSoundLocation(pugi::xml_node element, bool removeAttr) { bool hasAttribute = false; if (element.attribute("azimuth")) { - this->SetAzimuth(StrToDbl(element.attribute("azimuth").value())); + this->SetAzimuth(StrToDegrees(element.attribute("azimuth").value())); if (removeAttr) element.remove_attribute("azimuth"); hasAttribute = true; } if (element.attribute("elevation")) { - this->SetElevation(StrToDbl(element.attribute("elevation").value())); + this->SetElevation(StrToDegrees(element.attribute("elevation").value())); if (removeAttr) element.remove_attribute("elevation"); hasAttribute = true; } @@ -497,11 +497,11 @@ bool AttSoundLocation::WriteSoundLocation(pugi::xml_node element) { bool wroteAttribute = false; if (this->HasAzimuth()) { - element.append_attribute("azimuth") = DblToStr(this->GetAzimuth()).c_str(); + element.append_attribute("azimuth") = DegreesToStr(this->GetAzimuth()).c_str(); wroteAttribute = true; } if (this->HasElevation()) { - element.append_attribute("elevation") = DblToStr(this->GetElevation()).c_str(); + element.append_attribute("elevation") = DegreesToStr(this->GetElevation()).c_str(); wroteAttribute = true; } return wroteAttribute; @@ -509,12 +509,12 @@ bool AttSoundLocation::WriteSoundLocation(pugi::xml_node element) bool AttSoundLocation::HasAzimuth() const { - return (m_azimuth != 0.0); + return (m_azimuth != MEI_UNSET); } bool AttSoundLocation::HasElevation() const { - return (m_elevation != 0.0); + return (m_elevation != MEI_UNSET); } //---------------------------------------------------------------------------- diff --git a/libmei/dist/atts_gestural.h b/libmei/dist/atts_gestural.h index 9400a9d9bf1..ebd750a3729 100644 --- a/libmei/dist/atts_gestural.h +++ b/libmei/dist/atts_gestural.h @@ -503,20 +503,20 @@ class AttSoundLocation : public Att { * to the default value) **/ ///@{ - void SetAzimuth(double azimuth_) { m_azimuth = azimuth_; } - double GetAzimuth() const { return m_azimuth; } + void SetAzimuth(data_DEGREES azimuth_) { m_azimuth = azimuth_; } + data_DEGREES GetAzimuth() const { return m_azimuth; } bool HasAzimuth() const; // - void SetElevation(double elevation_) { m_elevation = elevation_; } - double GetElevation() const { return m_elevation; } + void SetElevation(data_DEGREES elevation_) { m_elevation = elevation_; } + data_DEGREES GetElevation() const { return m_elevation; } bool HasElevation() const; ///@} private: /** The lateral or left-to-right plane. **/ - double m_azimuth; + data_DEGREES m_azimuth; /** The above-to-below axis. **/ - double m_elevation; + data_DEGREES m_elevation; }; //---------------------------------------------------------------------------- diff --git a/libmei/dist/atts_shared.cpp b/libmei/dist/atts_shared.cpp index d65dd28bf23..3636cd2dfca 100644 --- a/libmei/dist/atts_shared.cpp +++ b/libmei/dist/atts_shared.cpp @@ -1005,7 +1005,7 @@ void AttCoordinated::ResetCoordinated() { m_lrx = MEI_UNSET; m_lry = MEI_UNSET; - m_rotate = 0.0; + m_rotate = MEI_UNSET; } bool AttCoordinated::ReadCoordinated(pugi::xml_node element, bool removeAttr) @@ -1022,7 +1022,7 @@ bool AttCoordinated::ReadCoordinated(pugi::xml_node element, bool removeAttr) hasAttribute = true; } if (element.attribute("rotate")) { - this->SetRotate(StrToDbl(element.attribute("rotate").value())); + this->SetRotate(StrToDegrees(element.attribute("rotate").value())); if (removeAttr) element.remove_attribute("rotate"); hasAttribute = true; } @@ -1041,7 +1041,7 @@ bool AttCoordinated::WriteCoordinated(pugi::xml_node element) wroteAttribute = true; } if (this->HasRotate()) { - element.append_attribute("rotate") = DblToStr(this->GetRotate()).c_str(); + element.append_attribute("rotate") = DegreesToStr(this->GetRotate()).c_str(); wroteAttribute = true; } return wroteAttribute; @@ -1059,7 +1059,7 @@ bool AttCoordinated::HasLry() const bool AttCoordinated::HasRotate() const { - return (m_rotate != 0.0); + return (m_rotate != MEI_UNSET); } //---------------------------------------------------------------------------- diff --git a/libmei/dist/atts_shared.h b/libmei/dist/atts_shared.h index b86e2877bab..69f913a1ed1 100644 --- a/libmei/dist/atts_shared.h +++ b/libmei/dist/atts_shared.h @@ -1223,8 +1223,8 @@ class AttCoordinated : public Att { int GetLry() const { return m_lry; } bool HasLry() const; // - void SetRotate(double rotate_) { m_rotate = rotate_; } - double GetRotate() const { return m_rotate; } + void SetRotate(data_DEGREES rotate_) { m_rotate = rotate_; } + data_DEGREES GetRotate() const { return m_rotate; } bool HasRotate() const; ///@} @@ -1239,7 +1239,7 @@ class AttCoordinated : public Att { * interpreted, with respect to the normal orientation of the parent surface. * The orientation is expressed in arc degrees. **/ - double m_rotate; + data_DEGREES m_rotate; }; //---------------------------------------------------------------------------- From 8da8e589b3bfa8bab40a355ee30f964170c4d18c Mon Sep 17 00:00:00 2001 From: Laurent Pugin Date: Mon, 16 Dec 2024 15:02:27 +0100 Subject: [PATCH 2/2] Add converter methods --- libmei/addons/att.cpp | 15 +++++++++++++++ libmei/addons/att.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/libmei/addons/att.cpp b/libmei/addons/att.cpp index 74cb90d94dc..aa1db04f77f 100644 --- a/libmei/addons/att.cpp +++ b/libmei/addons/att.cpp @@ -158,6 +158,21 @@ data_BULGE Att::StrToBulge(const std::string &value, bool logWarning) const return bulge; } +std::string Att::DegreesToStr(data_DEGREES data) const +{ + return StringFormat("%f", data); +} + +data_DEGREES Att::StrToDegrees(const std::string &value, bool logWarning) const +{ + double degrees = atof(value.c_str()); + if ((degrees > 360.0) || (degrees < -360.0)) { + if (logWarning) LogWarning("Unsupported data.DEGREES '%s'", value.c_str()); + return 0; + } + return degrees; +} + std::string Att::DurationToStr(data_DURATION data) const { std::string value; diff --git a/libmei/addons/att.h b/libmei/addons/att.h index 5b4c696080e..bcf7a75ca4b 100644 --- a/libmei/addons/att.h +++ b/libmei/addons/att.h @@ -69,6 +69,9 @@ class Att : public AttConverterBase { std::string BulgeToStr(const data_BULGE &data) const; data_BULGE StrToBulge(const std::string &value, bool logWarning = true) const; + std::string DegreesToStr(data_DEGREES data) const; + data_DEGREES StrToDegrees(const std::string &value, bool logWarning = true) const; + std::string DurationToStr(data_DURATION data) const; data_DURATION StrToDuration(const std::string &value, bool = true) const;