My Project 1.0
Holistic Fusion: A Generic Framework for Multimodal Sensor Fusion
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1/*
2Copyright 2022 by Julian Nubert, Robotic Systems Lab, ETH Zurich.
3All rights reserved.
4This file is released under the "BSD-3-Clause License".
5Please see the LICENSE file that has been included as part of this package.
6 */
7
8#ifndef HOLISTIC_FUSION_MATH_UTILS_H
9#define HOLISTIC_FUSION_MATH_UTILS_H
10
11#include <cmath>
13
14namespace holistic_fusion {
15
21inline double rad2deg(double radians) {
22 return radians * 180.0 / M_PI;
23}
24
30inline float rad2deg(float radians) {
31 return (float)(radians * 180.0 / M_PI);
32}
33
39inline double deg2rad(double degrees) {
40 return degrees * M_PI / 180.0;
41}
42
48inline float deg2rad(float degrees) {
49 return (float)(degrees * M_PI / 180.0);
50}
51
58template <typename PointT>
59inline float calcSquaredDiff(const PointT& a, const PointT& b) {
60 float diffX = a.x - b.x;
61 float diffY = a.y - b.y;
62 float diffZ = a.z - b.z;
63
64 return diffX * diffX + diffY * diffY + diffZ * diffZ;
65}
66
74template <typename PointT>
75inline float calcSquaredDiff(const PointT& a, const PointT& b, const float& wb) {
76 float diffX = a.x - b.x * wb;
77 float diffY = a.y - b.y * wb;
78 float diffZ = a.z - b.z * wb;
79
80 return diffX * diffX + diffY * diffY + diffZ * diffZ;
81}
82
88template <typename PointT>
89inline float calcPointDistance(const PointT& p) {
90 return std::sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
91}
92
98template <typename PointT>
99inline float calcSquaredPointDistance(const PointT& p) {
100 return p.x * p.x + p.y * p.y + p.z * p.z;
101}
102
108template <typename PointT>
109inline void rotX(PointT& p, const Angle& ang) {
110 float y = p.y;
111 p.y = ang.cos() * y - ang.sin() * p.z;
112 p.z = ang.sin() * y + ang.cos() * p.z;
113}
114
120template <typename PointT>
121inline void rotY(PointT& p, const Angle& ang) {
122 float x = p.x;
123 p.x = ang.cos() * x + ang.sin() * p.z;
124 p.z = ang.cos() * p.z - ang.sin() * x;
125}
126
132template <typename PointT>
133inline void rotZ(PointT& p, const Angle& ang) {
134 float x = p.x;
135 p.x = ang.cos() * x - ang.sin() * p.y;
136 p.y = ang.sin() * x + ang.cos() * p.y;
137}
138
146template <typename PointT>
147inline void rotateZXY(PointT& p, const Angle& angZ, const Angle& angX, const Angle& angY) {
148 rotZ(p, angZ);
149 rotX(p, angX);
150 rotY(p, angY);
151}
152
160template <typename PointT>
161inline void rotateYXZ(PointT& p, const Angle& angY, const Angle& angX, const Angle& angZ) {
162 rotY(p, angY);
163 rotX(p, angX);
164 rotZ(p, angZ);
165}
166
172inline void invertHomogenousMatrix(const Eigen::Matrix4d& m_in, Eigen::Matrix4d& m_out) {
173 m_out = Eigen::Matrix4d::Identity();
174 Eigen::Matrix3d R = m_in.block<3, 3>(0, 0);
175 Eigen::Vector3d t = m_in.block<3, 1>(0, 3);
176 m_out.block<3, 3>(0, 0) = R.transpose();
177 m_out.block<3, 1>(0, 3) = -R.transpose() * t;
178}
179
180} // namespace holistic_fusion
181
182#endif // HOLISTIC_FUSION_MATH_UTILS_H
Class for holding an angle.
Definition Angle.h:13
float cos() const
Definition Angle.h:47
float sin() const
Definition Angle.h:49
float calcPointDistance(const PointT &p)
Calculate the absolute distance of the point to the origin.
Definition math_utils.h:89
void rotX(PointT &p, const Angle &ang)
Rotate the given point by the specified angle around the x-axis.
Definition math_utils.h:109
double deg2rad(double degrees)
Convert the given degree angle to radian.
Definition math_utils.h:39
double rad2deg(double radians)
Convert the given radian angle to degrees.
Definition math_utils.h:21
float calcSquaredPointDistance(const PointT &p)
Calculate the squared distance of the point to the origin.
Definition math_utils.h:99
float calcSquaredDiff(const PointT &a, const PointT &b)
Calculate the squared difference of the given two points.
Definition math_utils.h:59
void invertHomogenousMatrix(const Eigen::Matrix4d &m_in, Eigen::Matrix4d &m_out)
Invert a homogenous transformation matrix.
Definition math_utils.h:172
void rotateZXY(PointT &p, const Angle &angZ, const Angle &angX, const Angle &angY)
Rotate the given point by the specified angles around the z-, x- respectively y-axis.
Definition math_utils.h:147
void rotY(PointT &p, const Angle &ang)
Rotate the given point by the specified angle around the y-axis.
Definition math_utils.h:121
void rotZ(PointT &p, const Angle &ang)
Rotate the given point by the specified angle around the z-axis.
Definition math_utils.h:133
void rotateYXZ(PointT &p, const Angle &angY, const Angle &angX, const Angle &angZ)
Rotate the given point by the specified angles around the y-, x- respectively z-axis.
Definition math_utils.h:161