Trim trailing default values in saved xmls.

For attributes which accept variable-length inputs. Currently used for `gainprm, biasprm, friction, solref, solimp` Example:

- Saved before: `biasprm="0 -3 -4 0 0 0 0 0 0 0"`.
- Saved after: `biasprm="0 -3 -4"`.

PiperOrigin-RevId: 607609550
Change-Id: I0501348280663867693175236872e2aabc703601
This commit is contained in:
Yuval Tassa
2024-02-16 01:35:13 -08:00
committed by Copybara-Service
parent 6a346c42e8
commit f9f7827a64
4 changed files with 65 additions and 26 deletions
+13 -5
View File
@@ -904,7 +904,8 @@ static int Round(double x) {
// write attribute
template<typename T>
void mjXUtil::WriteAttr(XMLElement* elem, string name, int n, const T* data, const T* def) {
void mjXUtil::WriteAttr(XMLElement* elem, string name, int n, const T* data, const T* def,
bool trim) {
// make sure all are defined
if constexpr (std::is_floating_point_v<T>) {
for (int i=0; i<n; i++) {
@@ -919,6 +920,13 @@ void mjXUtil::WriteAttr(XMLElement* elem, string name, int n, const T* data, con
return;
}
// trim identical trailing default values
if (trim) {
while (n > 0 && data[n-1] == def[n-1]) {
n--;
}
}
// increase precision for testing
stringstream stream;
stream.precision(mujoco::_mjPRIVATE__get_xml_precision());
@@ -945,17 +953,17 @@ void mjXUtil::WriteAttr(XMLElement* elem, string name, int n, const T* data, con
template void mjXUtil::WriteAttr(XMLElement* elem, string name, int n,
const double* data, const double* def);
const double* data, const double* def, bool trim);
template void mjXUtil::WriteAttr(XMLElement* elem, string name, int n,
const float* data, const float* def);
const float* data, const float* def, bool trim);
template void mjXUtil::WriteAttr(XMLElement* elem, string name, int n,
const int* data, const int* def);
const int* data, const int* def, bool trim);
template void mjXUtil::WriteAttr(XMLElement* elem, string name, int n,
const unsigned char* data,
const unsigned char* def);
const unsigned char* def, bool trim);
// write vector<double> attribute, default = zero array