Commit 79d8857a authored by Robert Menzel's avatar Robert Menzel

added approx equal functions for matrices and vectors in Math

parent bcada566
......@@ -97,38 +97,69 @@ inline T min(T a, T b) {return(a < b ? a : b);}
inline uint_t ring(int_t a, uint_t mod) {bool b = a < 0; a = abs(a) % mod; return(b ? mod - a : a);}
inline glm::mat3 rotationMatrixX(float degree)
{
glm::mat3 matrix;
matrix[0][0] = 1.0f; matrix[1][0] = 0.0f; matrix[2][0] = 0.0f;
matrix[0][1] = 0.0f; matrix[1][1] = cosDeg(degree); matrix[2][1] = -sinDeg(degree);
matrix[0][2] = 0.0f; matrix[1][2] = sinDeg(degree); matrix[2][2] = cosDeg(degree);
return matrix;
}
/**
* @brief Returns a rotation matrix that rotates a givn degree around the X-axis.
* @param degree is the angle in degree, not radians!
* @return The rotation matrix.
*/
glm::mat3 rotationMatrixX(float degree);
inline glm::mat3 rotationMatrixY(float degree)
{
glm::mat3 matrix;
matrix[0][0] = cosDeg(degree); matrix[1][0] = 0.0f; matrix[2][0] = sinDeg(degree);
matrix[0][1] = 0.0f; matrix[1][1] = 1.0f; matrix[2][1] = 0.0f;
matrix[0][2] = -sinDeg(degree); matrix[1][2] = 0.0f; matrix[2][2] = cosDeg(degree);
return matrix;
}
/**
* @brief Returns a rotation matrix that rotates a givn degree around the Y-axis.
* @param degree is the angle in degree, not radians!
* @return The rotation matrix.
*/
glm::mat3 rotationMatrixY(float degree);
inline glm::mat3 rotationMatrixZ(float degree)
{
glm::mat3 matrix;
matrix[0][0] = cosDeg(degree); matrix[1][0] = -sinDeg(degree); matrix[2][0] = 0.0f;
matrix[0][1] = sinDeg(degree); matrix[1][1] = cosDeg(degree); matrix[2][1] = 0.0f;
matrix[0][2] = 0.0f; matrix[1][2] = 0.0f; matrix[2][2] = 1.0f;
return matrix;
}
/**
* @brief Returns a rotation matrix that rotates a givn degree around the Z-axis.
* @param degree is the angle in degree, not radians!
* @return The rotation matrix.
*/
glm::mat3 rotationMatrixZ(float degree);
/**
* @brief Returns the inverse transpose of the given matrix to use as a normal matrix.
* @param The e.g. modelview matrix.
* @return The inverse transposed.
*/
inline glm::mat3 normalMatrix(const glm::mat4& matrix)
{
return glm::inverseTranspose( glm::mat3( matrix ) );
}
/**
* @brief Test if two glm vectors are equal with respect to a given epsilon.
*/
template<typename T>
bool isApproxEqual(const T &_v1, const T &_v2, float _eps = .01)
{
return glm::distance(_v1, _v2) < _eps;
}
/**
* @brief isApproxEqual returns whether two glm 4x4 matrices are equal with respect to a small epsilon.
* @param _v1: first matrix
* @param _v2: second matrix
* @param _eps: small numerical value
* @return true if the sum off the componentwise difference is smaller than _eps.
*/
bool isApproxEqual(const glm::mat4 &_v1, const glm::mat4 &_v2, float _eps = .01);
/**
* @brief isApproxEqual returns whether two glm 3x3 matrices are equal with respect to a small epsilon.
* @param _v1: first matrix
* @param _v2: second matrix
* @param _eps: small numerical value
* @return true if the sum off the componentwise difference is smaller than _eps.
*/
bool isApproxEqual(const glm::mat3 &_v1, const glm::mat3 &_v2, float _eps = .01);
/**
* @brief Checks if a given matrix is orthonormal.
*/
bool isOrthonormalMatrix(const glm::mat3 &_matrix);
} // Functions
} // Math
} // ACGL
......
#include <ACGL/Math/Math.hh> // will include the functions.hh but also the GLM
namespace ACGL{
namespace Math{
namespace Functions{
glm::mat3 rotationMatrixX(float degree)
{
glm::mat3 matrix;
matrix[0][0] = 1.0f; matrix[1][0] = 0.0f; matrix[2][0] = 0.0f;
matrix[0][1] = 0.0f; matrix[1][1] = cosDeg(degree); matrix[2][1] = -sinDeg(degree);
matrix[0][2] = 0.0f; matrix[1][2] = sinDeg(degree); matrix[2][2] = cosDeg(degree);
return matrix;
}
glm::mat3 rotationMatrixY(float degree)
{
glm::mat3 matrix;
matrix[0][0] = cosDeg(degree); matrix[1][0] = 0.0f; matrix[2][0] = sinDeg(degree);
matrix[0][1] = 0.0f; matrix[1][1] = 1.0f; matrix[2][1] = 0.0f;
matrix[0][2] = -sinDeg(degree); matrix[1][2] = 0.0f; matrix[2][2] = cosDeg(degree);
return matrix;
}
glm::mat3 rotationMatrixZ(float degree)
{
glm::mat3 matrix;
matrix[0][0] = cosDeg(degree); matrix[1][0] = -sinDeg(degree); matrix[2][0] = 0.0f;
matrix[0][1] = sinDeg(degree); matrix[1][1] = cosDeg(degree); matrix[2][1] = 0.0f;
matrix[0][2] = 0.0f; matrix[1][2] = 0.0f; matrix[2][2] = 1.0f;
return matrix;
}
bool isApproxEqual(const glm::mat4 &_v1, const glm::mat4 &_v2, float _eps)
{
glm::mat4 diff = _v1 - _v2;
float d = 0;
d += glm::abs(diff[0][0]);
d += glm::abs(diff[0][1]);
d += glm::abs(diff[0][2]);
d += glm::abs(diff[0][3]);
d += glm::abs(diff[1][0]);
d += glm::abs(diff[1][1]);
d += glm::abs(diff[1][2]);
d += glm::abs(diff[1][3]);
d += glm::abs(diff[2][0]);
d += glm::abs(diff[2][1]);
d += glm::abs(diff[2][2]);
d += glm::abs(diff[2][3]);
d += glm::abs(diff[3][0]);
d += glm::abs(diff[3][1]);
d += glm::abs(diff[3][2]);
d += glm::abs(diff[3][3]);
return d < _eps;
}
bool isApproxEqual(const glm::mat3 &_v1, const glm::mat3 &_v2, float _eps)
{
glm::mat3 diff = _v1 - _v2;
float d = 0;
d += glm::abs(diff[0][0]);
d += glm::abs(diff[0][1]);
d += glm::abs(diff[0][2]);
d += glm::abs(diff[1][0]);
d += glm::abs(diff[1][1]);
d += glm::abs(diff[1][2]);
d += glm::abs(diff[2][0]);
d += glm::abs(diff[2][1]);
d += glm::abs(diff[2][2]);
return d < _eps;
}
bool isOrthonormalMatrix(const glm::mat3 &_matrix)
{
return isApproxEqual(glm::inverse(_matrix), glm::transpose(_matrix));
}
} // namespace
} // namespace
} // namespace
......@@ -6,6 +6,7 @@
#include <ACGL/Scene/Camera.hh>
using namespace ACGL;
using namespace ACGL::Scene;
glm::mat4 Camera::getProjectionMatrix(void) const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment