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

[WIP] Minimum stod change #3945

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 12 additions & 11 deletions OpenSim/Common/APDMDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "FileAdapter.h"
#include "TimeSeriesTable.h"
#include "APDMDataReader.h"
#include "IO.h"

namespace OpenSim {

Expand Down Expand Up @@ -101,7 +102,7 @@ APDMDataReader::extendRead(const std::string& fileName) const {
// Line 2
std::getline(in_stream, line);
tokens = FileAdapter::tokenize(line, ",");
dataRate = std::stod(tokens[1]);
dataRate = OpenSim::IO::stod(tokens[1]);
// Line 3, find columns for IMUs
std::getline(in_stream, line);
tokens = FileAdapter::tokenize(line, ",");
Expand Down Expand Up @@ -155,20 +156,20 @@ APDMDataReader::extendRead(const std::string& fileName) const {
for (int imu_index = 0; imu_index < n_imus; ++imu_index) {
// parse gyro info from in_stream
if (foundLinearAccelerationData)
accel_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[accIndex[imu_index]]),
std::stod(nextRow[accIndex[imu_index] + 1]), std::stod(nextRow[accIndex[imu_index] + 2]));
accel_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[accIndex[imu_index]]),
OpenSim::IO::stod(nextRow[accIndex[imu_index] + 1]), OpenSim::IO::stod(nextRow[accIndex[imu_index] + 2]));
if (foundMagneticHeadingData)
magneto_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[magIndex[imu_index]]),
std::stod(nextRow[magIndex[imu_index] + 1]), std::stod(nextRow[magIndex[imu_index] + 2]));
magneto_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[magIndex[imu_index]]),
OpenSim::IO::stod(nextRow[magIndex[imu_index] + 1]), OpenSim::IO::stod(nextRow[magIndex[imu_index] + 2]));
if (foundAngularVelocityData)
gyro_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[gyroIndex[imu_index]]),
std::stod(nextRow[gyroIndex[imu_index] + 1]), std::stod(nextRow[gyroIndex[imu_index] + 2]));
gyro_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[gyroIndex[imu_index]]),
OpenSim::IO::stod(nextRow[gyroIndex[imu_index] + 1]), OpenSim::IO::stod(nextRow[gyroIndex[imu_index] + 2]));
// Create Quaternion from values in file, assume order in file W, X, Y, Z
orientation_row_vector[imu_index] =
SimTK::Quaternion(std::stod(nextRow[orientationsIndex[imu_index]]),
std::stod(nextRow[orientationsIndex[imu_index] + 1]),
std::stod(nextRow[orientationsIndex[imu_index] + 2]),
std::stod(nextRow[orientationsIndex[imu_index] + 3]));
SimTK::Quaternion(OpenSim::IO::stod(nextRow[orientationsIndex[imu_index]]),
OpenSim::IO::stod(nextRow[orientationsIndex[imu_index] + 1]),
OpenSim::IO::stod(nextRow[orientationsIndex[imu_index] + 2]),
OpenSim::IO::stod(nextRow[orientationsIndex[imu_index] + 3]));
}
// append to the tables
times[rowNumber] = time;
Expand Down
6 changes: 4 additions & 2 deletions OpenSim/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ if (NOT WITH_EZC3D)
unset(ezc3d_LIBRARY)
endif()

find_package(FastFloat REQUIRED
HINTS "${OPENSIM_DEPENDENCIES_DIR}/fast_float")
OpenSimAddLibrary(
KIT Common
AUTHORS "Clay_Anderson-Ayman_Habib-Peter_Loan"
# Clients of osimCommon need not link to ezc3d.
LINKLIBS PUBLIC ${Simbody_LIBRARIES} spdlog::spdlog osimLepton
PRIVATE ${ezc3d_LIBRARY}
LINKLIBS PUBLIC ${Simbody_LIBRARIES} spdlog::spdlog osimLepton
PRIVATE ${ezc3d_LIBRARY} FastFloat::fast_float
INCLUDES ${INCLUDES}
SOURCES ${SOURCES}
TESTDIRS "Test"
Expand Down
32 changes: 16 additions & 16 deletions OpenSim/Common/DelimFileAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ DelimFileAdapter<T>::extendRead(const std::string& fileName) const {
}

// Time is column 0.
timeVec.push_back(std::stod(row.front()));
timeVec.push_back(OpenSim::IO::stod(row.front()));
row.erase(row.begin());

auto row_vector = readElems(row);
Expand Down Expand Up @@ -482,7 +482,7 @@ DelimFileAdapter<T>::readElems_impl(const std::vector<std::string>& tokens,
double) const {
SimTK::RowVector_<double> elems{static_cast<int>(tokens.size())};
for(auto i = 0u; i < tokens.size(); ++i)
elems[static_cast<int>(i)] = std::stod(tokens[i]);
elems[static_cast<int>(i)] = OpenSim::IO::stod(tokens[i]);

return elems;
}
Expand All @@ -497,9 +497,9 @@ DelimFileAdapter<T>::readElems_impl(const std::vector<std::string>& tokens,
OPENSIM_THROW_IF(comps.size() != 3,
IncorrectNumTokens,
"Expected 3x (multiple of 3) number of tokens.");
elems[i] = SimTK::UnitVec3{std::stod(comps[0]),
std::stod(comps[1]),
std::stod(comps[2])};
elems[i] = SimTK::UnitVec3{OpenSim::IO::stod(comps[0]),
OpenSim::IO::stod(comps[1]),
OpenSim::IO::stod(comps[2])};
}

return elems;
Expand All @@ -515,10 +515,10 @@ DelimFileAdapter<T>::readElems_impl(const std::vector<std::string>& tokens,
OPENSIM_THROW_IF(comps.size() != 4,
IncorrectNumTokens,
"Expected 4x (multiple of 4) number of tokens.");
elems[i] = SimTK::Quaternion{std::stod(comps[0]),
std::stod(comps[1]),
std::stod(comps[2]),
std::stod(comps[3])};
elems[i] = SimTK::Quaternion{OpenSim::IO::stod(comps[0]),
OpenSim::IO::stod(comps[1]),
OpenSim::IO::stod(comps[2]),
OpenSim::IO::stod(comps[3])};
}

return elems;
Expand All @@ -534,12 +534,12 @@ DelimFileAdapter<T>::readElems_impl(const std::vector<std::string>& tokens,
OPENSIM_THROW_IF(comps.size() != 6,
IncorrectNumTokens,
"Expected 6x (multiple of 6) number of tokens.");
elems[i] = SimTK::SpatialVec{{std::stod(comps[0]),
std::stod(comps[1]),
std::stod(comps[2])},
{std::stod(comps[3]),
std::stod(comps[4]),
std::stod(comps[5])}};
elems[i] = SimTK::SpatialVec{{OpenSim::IO::stod(comps[0]),
OpenSim::IO::stod(comps[1]),
OpenSim::IO::stod(comps[2])},
{OpenSim::IO::stod(comps[3]),
OpenSim::IO::stod(comps[4]),
OpenSim::IO::stod(comps[5])}};
}

return elems;
Expand All @@ -559,7 +559,7 @@ DelimFileAdapter<T>::readElems_impl(const std::vector<std::string>& tokens,
"x (multiple of " + std::to_string(M) +
") number of tokens.");
for(int j = 0; j < M; ++j) {
elems[i][j] = std::stod(comps[j]);
elems[i][j] = OpenSim::IO::stod(comps[j]);
}
}

Expand Down
27 changes: 25 additions & 2 deletions OpenSim/Common/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#include <unistd.h>
#endif

#include "fast_float/fast_float.h"
// #include <sstream>

// CONSTANTS


Expand All @@ -64,6 +67,8 @@ int IO::_Pad = 8;
int IO::_Precision = 8;
char IO::_DoubleFormat[] = "%16.8lf";
bool IO::_PrintOfflineDocuments = true;
std::string IO::_locale = std::locale::classic().name();



//=============================================================================
Expand Down Expand Up @@ -450,10 +455,14 @@ OpenFile(const string &aFileName,const string &aMode)
/**
* Open a file.
*/
ifstream *IO::
std::ifstream *IO::
OpenInputFile(const string &aFileName,ios_base::openmode mode)
{
ifstream *fs = new ifstream(aFileName.c_str(), ios_base::in | mode);
// std::ifstream *fs = new std::ifstream();
log_info(_locale);
// fs->imbue(std::locale(_locale));
// fs->open(aFileName.c_str(), ios_base::in | mode);
if(!fs || !(*fs)) {
log_error("IO.OpenInputFile(const string&,openmode mode): "
"failed to open {}.", aFileName);
Expand All @@ -462,10 +471,13 @@ OpenInputFile(const string &aFileName,ios_base::openmode mode)

return(fs);
}
ofstream *IO::
std::ofstream *IO::
OpenOutputFile(const string &aFileName,ios_base::openmode mode)
{
ofstream *fs = new ofstream(aFileName.c_str(), ios_base::out | mode);
// std::ofstream *fs = new std::ofstream();
// fs->imbue(std::locale(_locale));
// fs->open(aFileName.c_str(), ios_base::out | mode);
if(!fs || !(*fs)) {
log_error("IO.OpenOutputFile(const string&,openmode mode): failed to "
"open {}.", aFileName);
Expand All @@ -474,6 +486,17 @@ OpenOutputFile(const string &aFileName,ios_base::openmode mode)

return(fs);
}

double IO::
stod(const std::string& __str, std::size_t* __idx)
{
double result;
// std::istringstream iss(__str);
// iss.imbue(std::locale(_locale));
// iss >> result;
fast_float::from_chars(__str.data(), __str.data()+__str.size(), result);
return result;
}
//_____________________________________________________________________________
/**
* Create a directory. Potentially platform dependent.
Expand Down
5 changes: 5 additions & 0 deletions OpenSim/Common/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class OSIMCOMMON_API IO {
static char _DoubleFormat[IO_DBLFMTLEN];
/** Whether offline documents should also be printed when Object::print is called. */
static bool _PrintOfflineDocuments;
/** Locale specifier for reading and writing files */
static std::string _locale;


//=============================================================================
Expand Down Expand Up @@ -103,7 +105,10 @@ class OSIMCOMMON_API IO {
static FILE* OpenFile(const std::string &aFileName,const std::string &aMode);
static std::ifstream* OpenInputFile(const std::string &aFileName,std::ios_base::openmode mode=std::ios_base::in);
static std::ofstream* OpenOutputFile(const std::string &aFileName,std::ios_base::openmode mode=std::ios_base::out);
static double stod(const std::string& __str, std::size_t* __idx = 0);
#endif

//
// Directory management
static int makeDir(const std::string &aDirName);
static int chDir(const std::string &aDirName);
Expand Down
8 changes: 4 additions & 4 deletions OpenSim/Common/TRCFileAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ TRCFileAdapter::extendRead(const std::string& fileName) const {
//only if each component is specified read process as a Vec3
if ( !(row.at(c).empty() || row.at(c + 1).empty()
|| row.at(c + 2).empty()) ) {
row_vector[ind] = SimTK::Vec3{ std::stod(row.at(c)),
std::stod(row.at(c + 1)),
std::stod(row.at(c + 2)) };
row_vector[ind] = SimTK::Vec3{ OpenSim::IO::stod(row.at(c)),
OpenSim::IO::stod(row.at(c + 1)),
OpenSim::IO::stod(row.at(c + 2)) };
} // otherwise the value will remain NaN (default)
++ind;
}
markerData[rowNumber] = row_vector;
// Column 1 is time.
times[rowNumber] = std::stod(row.at(1));
times[rowNumber] = OpenSim::IO::stod(row.at(1));
rowNumber++;
if (rowNumber== last_size) {
// resize all Data/Matrices, double the size while keeping data
Expand Down
5 changes: 3 additions & 2 deletions OpenSim/Common/Test/testSTOFileAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "OpenSim/Common/Adapters.h"
#include "OpenSim/Common/CommonUtilities.h"
#include "OpenSim/Common/IO.h"
#include <cstdio>
#include <fstream>
#include <unordered_set>
Expand Down Expand Up @@ -140,8 +141,8 @@ void compareFiles(const std::string& filenameA,
double d_tokenA{};
double d_tokenB{};
try {
d_tokenA = std::stod(tokenA);
d_tokenB = std::stod(tokenB);
d_tokenA = OpenSim::IO::stod(tokenA);
d_tokenB = OpenSim::IO::stod(tokenB);
} catch(std::invalid_argument&) {
testFailed(filenameA, tokenA, tokenB);
}
Expand Down
4 changes: 2 additions & 2 deletions OpenSim/Common/Test/testTRCFileAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void compareFiles(const std::string& filenameA,
double d_tokenA{};
double d_tokenB{};
try {
d_tokenA = std::stod(tokenA);
d_tokenB = std::stod(tokenB);
d_tokenA = OpenSim::IO::stod(tokenA);
d_tokenB = OpenSim::IO::stod(tokenB);
}
catch (std::invalid_argument&) {
testFailed(filenameA, tokenA, tokenB);
Expand Down
16 changes: 8 additions & 8 deletions OpenSim/Common/XsensDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ XsensDataReader::extendRead(const std::string& folderName) const {
std::map<std::string, std::string>::iterator it =
headersKeyValuePairs.find("Update Rate");
if (it != headersKeyValuePairs.end())
dataRate = std::stod(it->second);
dataRate = OpenSim::IO::stod(it->second);
else
dataRate = 40.0; // Need confirmation from XSens as later files don't specify rate
// internally keep track of what data was found in input files
Expand Down Expand Up @@ -122,20 +122,20 @@ XsensDataReader::extendRead(const std::string& folderName) const {
break;
}
if (foundLinearAccelerationData)
accel_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[accIndex]),
std::stod(nextRow[accIndex + 1]), std::stod(nextRow[accIndex + 2]));
accel_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[accIndex]),
OpenSim::IO::stod(nextRow[accIndex + 1]), OpenSim::IO::stod(nextRow[accIndex + 2]));
if (foundMagneticHeadingData)
magneto_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[magIndex]),
std::stod(nextRow[magIndex + 1]), std::stod(nextRow[magIndex + 2]));
magneto_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[magIndex]),
OpenSim::IO::stod(nextRow[magIndex + 1]), OpenSim::IO::stod(nextRow[magIndex + 2]));
if (foundAngularVelocityData)
gyro_row_vector[imu_index] = SimTK::Vec3(std::stod(nextRow[gyroIndex]),
std::stod(nextRow[gyroIndex + 1]), std::stod(nextRow[gyroIndex + 2]));
gyro_row_vector[imu_index] = SimTK::Vec3(OpenSim::IO::stod(nextRow[gyroIndex]),
OpenSim::IO::stod(nextRow[gyroIndex + 1]), OpenSim::IO::stod(nextRow[gyroIndex + 2]));
// Create Mat33 then convert into Quaternion
SimTK::Mat33 imu_matrix{ SimTK::NaN };
int matrix_entry_index = 0;
for (int mcol = 0; mcol < 3; mcol++) {
for (int mrow = 0; mrow < 3; mrow++) {
imu_matrix[mrow][mcol] = std::stod(nextRow[rotationsIndex + matrix_entry_index]);
imu_matrix[mrow][mcol] = OpenSim::IO::stod(nextRow[rotationsIndex + matrix_entry_index]);
matrix_entry_index++;
}
}
Expand Down
14 changes: 7 additions & 7 deletions OpenSim/Sandbox/ImuStreaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ parseImuData(const char* str, const size_t len) {
std::smatch result_ts{};
if(!std::regex_search(data, result_ts, regex_ts))
throw std::runtime_error{"No timestamp in data stream."};
double timestamp{std::stod(result_ts[0])};
double timestamp{OpenSim::IO::stod(result_ts[0])};
// Acceleration.
std::array<double, 3> gravity{};
std::smatch result_gravity{};
if(std::regex_search(data, result_gravity, regex_gravity)) {
gravity[0] = std::stod(result_gravity[1]);
gravity[1] = std::stod(result_gravity[2]);
gravity[2] = std::stod(result_gravity[3]);
gravity[0] = OpenSim::IO::stod(result_gravity[1]);
gravity[1] = OpenSim::IO::stod(result_gravity[2]);
gravity[2] = OpenSim::IO::stod(result_gravity[3]);
}
// Angular veclocity.
std::array<double, 3> omega{};
std::smatch result_omega{};
if(std::regex_search(data, result_omega, regex_omega)) {
omega[0] = std::stod(result_omega[1]);
omega[1] = std::stod(result_omega[2]);
omega[2] = std::stod(result_omega[3]);
omega[0] = OpenSim::IO::stod(result_omega[1]);
omega[1] = OpenSim::IO::stod(result_omega[2]);
omega[2] = OpenSim::IO::stod(result_omega[3]);
}

return std::make_tuple(timestamp, gravity, omega);
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Simulation/MarkersReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ double
MarkersReference::getSamplingFrequency() const {
if(_markerTable.hasTableMetaDataKey("DataRate")) {
auto datarate = _markerTable.getTableMetaData<std::string>("DataRate");
return std::stod(datarate);
return IO::stod(datarate);
} else
return SimTK::NaN;
}
Expand Down
9 changes: 9 additions & 0 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ AddDependency(NAME catch2
GIT_URL https://github.com/catchorg/Catch2.git
GIT_TAG v3.5.0)


AddDependency(NAME fast_float
DEFAULT ON
GIT_URL https://github.com/fastfloat/fast_float.git
GIT_TAG tags/v6.1.6
GIT_SHALLOW TRUE)

mark_as_advanced(SUPERBUILD_fast_float)

# Moco settings.
# --------------
option(OPENSIM_WITH_CASADI
Expand Down
Loading