This namespace contains various helper functions. More...
Functions | |
bool | fileExists (const char *file_name) |
Checks whether a certain file exists. | |
unsigned long long | fileLength (const char *file_name) |
Returns the size of a certain file. | |
unsigned long long | fileLength (ifstream &file_stream) |
string | getCurrentDate () |
Returns the current date in ISO 8601 format (YYYY-MM-DD). | |
string | getCurrentTime () |
Returns the current time in format hh:mm:ss. | |
double | randn () |
Random sample of the standard normal distribution. | |
template<class T > | |
bool | isZero (const T value) |
Tests for equality with zero. | |
template<class T > | |
T | rand () |
Random sample of the standard uniform distribution. | |
template<class T > | |
T | rand (T a, T b) |
Random sample of a uniform distribution. | |
template<class T > | |
T | randn (T mu, T sigma) |
Random sample of the normal distribution. | |
template<class EigenMatrix > | |
QTransform | Eigen3x3_to_QTransfom (const EigenMatrix &eigenMatrix) |
Converts an Eigen 3x3 matrix into a QTransform. | |
template<class T > | |
std::pair< Eigen::Matrix< T, 3, 1 > , T > | axisAngleFromRotationMatrix (const Eigen::Matrix< T, 3, 3 > &matrix) |
template<class T > | |
std::tuple< T, T, T > | cartesian2Spherical (const T &x, const T &y, const T &z) |
template<class T > | |
Eigen::Matrix< T, 3, 1 > | cartesian2Spherical (const Eigen::Matrix< T, 3, 1 > &point) |
template<class T > | |
Coordinate< T > | cartesian2Spherical (const Coordinate< T > &point) |
unsigned long long | fileLength (std::ifstream &file_stream) |
Returns the size of a certain file stream. | |
template<class Derived , class Base > | |
bool | isClass (Base *base_ptr) |
Checks whether an object is of a certain class. | |
template<class Derived , class Base > | |
bool | isClass (Base &base) |
Checks whether an object is of a certain class. | |
template<typename Base > | |
bool | isDerivedFrom (const Base *base_ptr) |
Checks whether an object is derived from a certain class. | |
template<typename Base > | |
bool | isDerivedFrom (const void *base_ptr) |
Checks whether an object is derived from a certain class. | |
template<class T > | |
bool | isEqual (const T x, const T y) |
Tests for equality of two scalars. | |
template<> | |
bool | isEqual< int > (const int x, const int y) |
template<class T > | |
std::vector< T > | listToVector (const std::list< T > &lst) |
Converts an STL list into an STL vector. | |
template<class Container , class ValueType > | |
ValueType | mean (const Container &container, ValueType null_value=ValueType()) |
Returns the mean of all container elements. | |
template<class WeightsContainer , class ValuesContainer , class ValueType = typename ValuesContainer::value_type> | |
ValueType | weightedMean (const WeightsContainer &weights, const ValuesContainer &values, ValueType null_value=ValueType(0)) |
Returns the weighted mean of all container elements. | |
template<class Container > | |
auto | median (const Container &container)-> std |
Returns the median of all container elements. | |
template<class T > | |
TRTK::Coordinate< T > | orthogonalMatrixToQuaternion (const Eigen::Matrix< T, 3, 3 > &matrix) |
Converts an orthogonal matrix to a unit quaternion. | |
template<class T > | |
TRTK::Coordinate< T > | orthogonalMatrixToQuaternion2 (const Eigen::Matrix< T, 3, 3 > &matrix) |
Converts an orthogonal matrix to a unit quaternion. | |
template<class T > | |
Eigen::Matrix< T, 3, 3 > | quaternionToOrthogonalMatrix (T q0, T q1, T q2, T q3) |
Converts a unit quaternion to an orthogonal matrix. | |
template<class T > | |
Eigen::Matrix< T, 3, 3 > | quaternionToOrthogonalMatrix (const TRTK::Coordinate< T > &quaternion) |
Converts a unit quaternion to an orthogonal matrix. | |
template<> | |
int | rand< int > (int a, int b) |
template<> | |
unsigned | rand< unsigned > (unsigned a, unsigned b) |
template<> | |
int | randn (int mu, int sigma) |
template<class T > | |
int | sign (T value) |
Extracts the sign of a floating point number. | |
template<class T > | |
Eigen::Matrix< T, 3, 3 > | rotationMatrix (const Eigen::Matrix< T, 3, 1 > &axis_, double angle) |
template<class T > | |
T | round (const T number) |
Rounds towards the next (signed) integer value. | |
template<> | |
int | round< int > (const int number) |
template<class T > | |
std::tuple< T, T, T > | spherical2Cartesian (const T &r, const T &theta, const T &phi) |
template<class T > | |
Coordinate< T > | spherical2Cartesian (const Coordinate< T > &point) |
template<class T > | |
Eigen::Matrix< T, 3, 1 > | spherical2Cartesian (const Eigen::Matrix< T, 3, 1 > &point) |
template<class Container , class ValueType > | |
ValueType | standardDeviation (const Container &container, ValueType null_value=ValueType()) |
Computes the corrected sample standard deviation of all container elements. | |
template<class T > | |
std::string | toString (const T &value) |
Converts the input argument (int, double etc.) into a std::string. | |
template<class Container , class ValueType > | |
ValueType | variance (const Container &container, ValueType null_value=ValueType()) |
Computes the sample variance of all container elements. | |
template<class T > | |
std::list< T > | vectorToList (const std::vector< T > &vec) |
Converts an STL vector into an STL list. | |
template<class T1 , class T2 > | |
std::vector< std::pair< T1, T2 > > | zip (const std::vector< T1 > &v1, const std::vector< T2 > &v2) |
Zips two STL vectors into a single one. |
This namespace contains various helper functions.
Helper functions sorted by topic:
std::pair<Eigen::Matrix<T, 3, 1>, T> TRTK::Tools::axisAngleFromRotationMatrix | ( | const Eigen::Matrix< T, 3, 3 > & | matrix ) |
T | scalar (floating point) type |
This function decomposes a given 3-by-3 rotation matrix into the normalized rotation axis and the positive rotation angle where the rotation angle \( \alpha \in [0, \pi] \). The decomposition is unique. The rotation matrix does not need to describe a pure rotation; an additional scaling may be implied but is neglected. The matrix is assumed to be left multiplied to column vectors.
Example:
Matrix3d R = rotationMatrix(Vector3d(1, 1, 1), 0.3); Vector3d axis; double angle; tie(axis, angle) = axisAngleFromRotationMatrix(R);
Then, axis
= (0.5774, 0.5774, 0.5774) and angle
= 0.3.
std::tuple<T, T, T> TRTK::Tools::cartesian2Spherical | ( | const T & | x, |
const T & | y, | ||
const T & | z | ||
) |
T | scalar (floating point) type |
The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).
Eigen::Matrix<T, 3, 1> TRTK::Tools::cartesian2Spherical | ( | const Eigen::Matrix< T, 3, 1 > & | point ) |
T | scalar (floating point) type |
The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).
Coordinate<T> TRTK::Tools::cartesian2Spherical | ( | const Coordinate< T > & | point ) |
T | scalar (floating point) type |
The Cartesian coordinate is converted to spherical coordinates using the ISO 80000-2 convention (i.e. as in physics: radius r, inclination theta, azimuth phi).
QTransform TRTK::Tools::Eigen3x3_to_QTransfom | ( | const EigenMatrix & | eigenMatrix ) |
bool TRTK::Tools::fileExists | ( | const char * | file_name ) |
unsigned long long TRTK::Tools::fileLength | ( | const char * | file_name ) |
Returns the size of a certain file.
If a file does not exist zero is returned.
unsigned long long length = fileLength("file name");
unsigned long long TRTK::Tools::fileLength | ( | ifstream & | file_stream ) |
file_stream
must be a valid stream, otherwise the result is undefined.
std::ifstream file("file name"); if (file) { unsigned long long length = fileLength(file); }
std::string TRTK::Tools::getCurrentDate | ( | ) |
std::string TRTK::Tools::getCurrentTime | ( | ) |
bool TRTK::Tools::isClass | ( | Base * | base_ptr ) | [inline] |
Checks whether an object is of a certain class.
An object is also classified as being of type Derived
, if Derived
is one of its base classes.
Here are some examples also incorporating isClass(Base & base):
class A { public: virtual ~A() {}; }; class B : public A { public: virtual ~B() {}; }; class C { public: virtual ~C() {}; }; int main() { A a1; A * a2 = new A; A * a3 = new B; B b1; B * b2 = new B; C c1; C * c2 = new C; cout << isClass<B>(a1) << endl; // output: 0 cout << isClass<B>(a2) << endl; // output: 0 cout << isClass<B>(a3) << endl; // output: 1 cout << isClass<B>(b1) << endl; // output: 1 cout << isClass<B>(b2) << endl; // output: 1 cout << isClass<B>(c1) << endl; // output: 0 cout << isClass<B>(c2) << endl; // output: 0 return 0; }
bool TRTK::Tools::isClass | ( | Base & | base ) | [inline] |
Checks whether an object is of a certain class.
An object is also classified as being of type Derived
, if Derived
is one of its base classes.
Please have a look at isClass(Base * base_ptr) to see some example code.
bool TRTK::Tools::isDerivedFrom | ( | const Base * | base_ptr ) | [inline] |
Checks whether an object is derived from a certain class.
Example:
class A { public: virtual ~A() {}; }; class B : public A { public: virtual ~B() {}; }; class C { public: virtual ~C() {}; }; int main() { A * a = new A; B * b = new B; C * c = new C; cout << isDerivedFrom<A>(a) << endl; // output: 1 cout << isDerivedFrom<A>(b) << endl; // output: 1 cout << isDerivedFrom<A>(c) << endl; // output: 0 return 0; }
bool TRTK::Tools::isDerivedFrom | ( | const void * | base_ptr ) | [inline] |
bool TRTK::Tools::isEqual | ( | const T | x, |
const T | y | ||
) | [inline] |
Tests for equality of two scalars.
T | scalar type |
This function tests whether the absolute difference between the two input arguments is below a certain threshold. The default threshold is \( 1^{-13} \) or 10 numeric_limits<T>::epsilon()
in case the numeric limit of T
is greater than the default value.
Example:
double value = 1.0; isEqual(value, 1.0); // twice the same argument type isEqual<double>(value, 1); // different argument types
abs()
as well as numeric_limits<T>::epsilon()
must be defined for the given scalar type T
. (A template function specialization for integers is implemented.)bool TRTK::Tools::isZero | ( | const T | value ) | [inline] |
Tests for equality with zero.
T | scalar type |
Actually, this function is only a short form of isEqual<T>(value, T(0))
, hence testing the absolute value against a certain threshold. See isEqual(...) for more details.
T
must provide a single parameter constructor allowing an initialization of the form T(0).Example:
double value = 1.0; isZero(value); // returns false
std::vector<T> TRTK::Tools::listToVector | ( | const std::list< T > & | lst ) |
ValueType TRTK::Tools::mean | ( | const Container & | container, |
ValueType | null_value = ValueType() |
||
) | [inline] |
auto TRTK::Tools::median | ( | const Container & | container ) | [inline] |
TRTK::Coordinate<T> TRTK::Tools::orthogonalMatrixToQuaternion | ( | const Eigen::Matrix< T, 3, 3 > & | matrix ) |
TRTK::Coordinate<T> TRTK::Tools::orthogonalMatrixToQuaternion2 | ( | const Eigen::Matrix< T, 3, 3 > & | matrix ) |
Converts an orthogonal matrix to a unit quaternion.
The given matrix can be nonorthogonal. Then this algorithm computes a quaternion that yields the closest orthogonal matrix to the given matrix.
#include <Eigen/Core> #include <Eigen/Eigenvalues>
[1] Bar-Itzhack, "New Method for Extracting the Quaternion from a Rotation Matrix", J. Guidance, Vol. 23, No. 6: Engineering Notes, 2000
Eigen::Matrix<T, 3, 3> TRTK::Tools::quaternionToOrthogonalMatrix | ( | T | q0, |
T | q1, | ||
T | q2, | ||
T | q3 | ||
) |
Eigen::Matrix<T, 3, 3> TRTK::Tools::quaternionToOrthogonalMatrix | ( | const TRTK::Coordinate< T > & | quaternion ) | [inline] |
T TRTK::Tools::rand | ( | ) | [inline] |
Random sample of the standard uniform distribution.
T | floating point type |
Properties of the standard uniform distribution \( \mathcal{U}(0, 1) \):
T TRTK::Tools::rand | ( | T | a, |
T | b | ||
) |
Random sample of a uniform distribution.
T | floating point type |
[in] | a | minimum value of the distribution |
[in] | b | maximum value of the distribution |
Properties of the uniform distribution \( \mathcal{U}(a, b) \):
T TRTK::Tools::randn | ( | T | mu, |
T | sigma | ||
) | [inline] |
Random sample of the normal distribution.
T | floating point type |
[in] | mu | mean of the distribution |
[in] | sigma | standard deviation of the distribution |
Properties of the normal distribution \( \mathcal{N}(\mu, \sigma^2) \):
double TRTK::Tools::randn | ( | ) |
Random sample of the standard normal distribution.
Convenience function for randn<double>().
T | floating point type |
Properties of the standard normal distribution \( \mathcal{N}(0, 1) \):
randn()
function which uses the double
type.Eigen::Matrix<T, 3, 3> TRTK::Tools::rotationMatrix | ( | const Eigen::Matrix< T, 3, 1 > & | axis_, |
double | angle | ||
) |
T | scalar (floating point) type |
Constructs a 3d rotation matrix from a rotation vector and a rotation angle.
Note, negating the rotation vector or the rotation angle yields the inverse of the rotation. Consequently, if the rotation vector as well as the rotation angle are negated the same rotation is obtained.
T TRTK::Tools::round | ( | const T | number ) |
Rounds towards the next (signed) integer value.
T | scalar type |
Here is an example of how to use this function:
using TRTK::Tools::round; double a = round(-1.7); // a == -2.0 double b = round(-1.3); // b == -1.0 double c = round(1.3); // c == 1.0 double d = round(1.7); // d == 2.0
floor()
and ceil()
must be defined for T.int TRTK::Tools::sign | ( | T | value ) |
Extracts the sign of a floating point number.
It returns
\[ sgn(x) = \begin{cases} -1 & \text{if } x < 0 \\ 0 & \text{if } x = 0 \\ 1 & \text{if } x > 0 \end{cases} \]
T
must be constructable from 0
as well as provide some comparison operators. Eigen::Matrix<T, 3, 1> TRTK::Tools::spherical2Cartesian | ( | const Eigen::Matrix< T, 3, 1 > & | point ) |
T | scalar (floating point) type |
[in] | point | (r, theta, phi) |
The spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi).
std::tuple<T, T, T> TRTK::Tools::spherical2Cartesian | ( | const T & | r, |
const T & | theta, | ||
const T & | phi | ||
) |
T | scalar (floating point) type |
[in] | r | |
[in] | theta | |
[in] | phi | The spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi). |
Coordinate<T> TRTK::Tools::spherical2Cartesian | ( | const Coordinate< T > & | point ) |
T | scalar (floating point) type |
[in] | point | (r, theta, phi) |
The spherical coordinate is converted to a Cartesian coordinate using the ISO 80000-2 convention (i.e., as in physics a coordinate is given as a triple with radius r, inclination theta, and azimuth phi).
ValueType TRTK::Tools::standardDeviation | ( | const Container & | container, |
ValueType | null_value = ValueType() |
||
) | [inline] |
Computes the corrected sample standard deviation of all container elements.
While \( s^2 = \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 \) is an unbiased estimator for the population variance, s is a biased estimator for the population standard deviation [1].
However, for normally and independently distributed random variables an unbiased estimator is given by [2, 3, 4]
\[ s = \frac{1}{c_4(n)} \sqrt{ \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 } \]
where
\[ c_4(n) = \sqrt{\frac{2}{n - 1}} \frac{\Gamma(\frac{n}{2})}{\Gamma(\frac{n-1}{2})} = 1 - \frac{1}{4n} - \frac{7}{32n^2} - \frac{19}{128n^3} + O(n^{-4}) \]
This function implements the above described estimator.
[1] http://en.wikipedia.org/wiki/Sample_standard_deviation
[2] http://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation
[3] Richard M. Brugger, "A Note on Unbiased Estimation of the Standard Deviation", The American Statistician (23) 4 p. 32 (1969)
[4] J. Gurland and R. C. Tripathi, Richard M. Brugger, "A Simple Approximation for Unbiased Estimation of the Standard Deviation", The American Statistician (25) 4 p. 30 (1971)
std::string TRTK::Tools::toString | ( | const T & | value ) | [inline] |
Converts the input argument (int, double etc.) into a std::string.
Here is an example of how to use this function:
using std::string; using TRTK::Tools::toString; double d = -1.7; string str = "The value of d is " + toString(d);
ValueType TRTK::Tools::variance | ( | const Container & | container, |
ValueType | null_value = ValueType() |
||
) | [inline] |
Computes the sample variance of all container elements.
The variance is computed using Bessel's correction:
\[ s^2 = \frac{1}{n - 1} \sum_{i = 1}^{n} (x_i - \bar x)^2 \]
std::list<T> TRTK::Tools::vectorToList | ( | const std::vector< T > & | vec ) |
ValueType TRTK::Tools::weightedMean | ( | const WeightsContainer & | weights, |
const ValuesContainer & | values, | ||
ValueType | null_value = ValueType(0) |
||
) | [inline] |
Returns the weighted mean of all container elements.
This function computes
\[ \bar{x}_{mean} = \frac{\sum_{i=1}^n w_i x_i}{\sum_{i=1}^n w_i} \]
It is assumed that there is an associated weight for each value and that the ordering in both containers is the same. Each containers must provide the STL interface. The weights are assumed to be scalars and its underlying type must be initializable with 0. Also, the container elements must support the addition operation and values must support the multiplication with a scalar.
std::vector<std::pair<T1, T2> > TRTK::Tools::zip | ( | const std::vector< T1 > & | v1, |
const std::vector< T2 > & | v2 | ||
) |
Documentation generated by Doxygen