15#ifndef MATH_H_6014714286
16#define MATH_H_6014714286
26#error MIN macro defined, define NOMINMAX first!
30#error MAX macro defined, define NOMINMAX first!
67 template<
typename T >
76 template<
typename T >
87 return deg * 3.1415926f / 180.f;
95 return 180.f *
rad / 3.1415926f;
101 template<
typename T >
104 return static_cast< T
>( 0 );
131 template<
typename T >
144 template<
typename VectorElementType,
int rows,
int cols >
158 template<
typename T >
168 template<
typename VectorElementType,
int dimension >
171 return x.squaredNorm();
177 template<
typename InputType >
192 inline bool isEqual(
const bool& x,
const bool& y )
197 typedef Eigen::Matrix< float, 4, 4, Eigen::ColMajor >
Matrix4f;
198 typedef Eigen::Matrix< float, 3, 3, Eigen::ColMajor >
Matrix3f;
202 typedef Eigen::Matrix< signed int, 3, 1 >
Vector3i;
212 result.setIdentity();
222 result.setIdentity();
229 template<
typename MatrixType >
253 for(
unsigned int i = 0;
i < 3; ++
i )
278 result.setIdentity();
287 template<
typename Vector >
299 result.setIdentity();
308 template<
typename VectorElementType >
328 const float c = std::cos(
radians );
329 const float s = std::sin(
radians );
332 result.setIdentity();
334 result( 0, 0 ) = x * x * ( 1 -
c ) +
c;
335 result( 1, 0 ) = y * x * ( 1 -
c ) + z *
s;
336 result( 2, 0 ) = x * z * ( 1 -
c ) - y *
s;
338 result( 0, 1 ) = x * y * ( 1 -
c ) - z *
s;
339 result( 1, 1 ) = y * y * ( 1 -
c ) +
c;
340 result( 2, 1 ) = y * z * ( 1 -
c ) + x *
s;
342 result( 0, 2 ) = x * z * ( 1 -
c ) + y *
s;
343 result( 1, 2 ) = y * z * ( 1 -
c ) - x *
s;
344 result( 2, 2 ) = z * z * ( 1 -
c ) +
c;
351 template<
typename Vector >
374 if( std::abs( in.x() - in.y() ) > 1e-4f )
380 if( std::abs( out.x() ) > std::abs( out.y() ) )
390 if( std::abs( in.x() - in.z() ) > 1e-4f )
396 if( std::abs( out.x() ) > std::abs( out.z() ) )
406 if( std::abs( in.y() - in.z() ) > 1e-4f )
412 if( std::abs( out.y() ) > std::abs( out.z() ) )
423 out =
Vector3f( -in.x(), in.y(), 0 );
437 template<
typename VectorElementType,
int rows,
typename WType >
438 Eigen::Matrix< VectorElementType, 4, 1 >
vector4(
const Eigen::Matrix< VectorElementType, rows, 1 >&
v,
WType w )
440 static_assert(
rows >= 3,
"math::vector4 requires input vector with 3 rows or more." );
441 return Eigen::Matrix< VectorElementType, 4, 1 >(
v.x(),
v.y(),
v.z(),
static_cast< VectorElementType >( w ) );
448 template<
typename VectorElementType,
int rows >
449 Eigen::Matrix< VectorElementType, 3, 1 >
vector3(
const Eigen::Matrix< VectorElementType, rows, 1 >&
v )
451 static_assert(
rows >= 3,
"math::vector3 requires input vector with 3 rows or more." );
452 return Eigen::Matrix< VectorElementType, 3, 1 >(
v.x(),
v.y(),
v.z() );
488 return m( 0, 3 ) *
m( 0, 3 ) +
m( 1, 3 ) *
m( 1, 3 ) +
m( 2, 3 ) *
m( 2, 3 );
494 template<
typename Matrix >
497 const std::size_t
length =
m.rows() *
m.cols();
498 typename Matrix::Scalar
maxAbs = 0;
499 for( std::size_t
i = 0;
i <
length; ++
i )
580 template<
typename Sampler >
597 template<
typename ScalarType >
600 LIBCARNA_ASSERT( !std::numeric_limits< ScalarType >::is_signed || x >= 0 );
601 return static_cast< unsigned int >( x +
static_cast< ScalarType >( 0.5 ) );
608 template<
typename MatrixElementType,
int cols,
int rows >
609 Eigen::Matrix< unsigned int, cols, rows >
round_ui(
const Eigen::Matrix< MatrixElementType, cols, rows >&
m )
611 Eigen::Matrix< unsigned int, cols, rows > result;
612 for(
int i = 0;
i <
cols; ++
i )
613 for(
int j = 0;
j <
rows; ++
j )
624 template<
typename ScalarType >
628 static_assert( std::is_integral< ScalarType >::value,
"Only integral data types allowed." );
629 return x +
s * ( x % 2 );
637 template<
typename MatrixElementType,
int cols,
int rows >
638 Eigen::Matrix< MatrixElementType, cols, rows >
makeEven(
const Eigen::Matrix< MatrixElementType, cols, rows >&
m,
int s )
640 Eigen::Matrix< unsigned int, cols, rows > result;
641 for(
int i = 0;
i <
cols; ++
i )
642 for(
int j = 0;
j <
rows; ++
j )
652 template<
typename T >
679 for( std::size_t
idx = 0;
idx < size; ++
idx )
688 for( std::size_t
idx = 0;
idx < size; ++
idx )
718 template<
typename ResultType,
typename SupportType >
721 return a * ( 1 -
t ) + b *
t;
731#define LIBCARNA_FOR_VECTOR3UI_EX( vecName, vecLimit, vecStart ) \
732 LibCarna::base::math::Vector3ui vecName; \
733 for( vecName.z() = vecStart.x(); vecName.z() < vecLimit.z(); ++vecName.z() ) \
734 for( vecName.y() = vecStart.y(); vecName.y() < vecLimit.y(); ++vecName.y() ) \
735 for( vecName.x() = vecStart.z(); vecName.x() < vecLimit.x(); ++vecName.x() )
745#define LIBCARNA_FOR_VECTOR3UI( vecName, vecLimit ) \
746 LIBCARNA_FOR_VECTOR3UI_EX( vecName, vecLimit, LibCarna::base::math::Vector3ui( 0, 0, 0 ) )
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Contains forward-declarations.
Represents an association.
Maintains an OpenGL texture sampler object. This class realizes the RAII-idiom.
float translationDistance2(const Matrix4f &m)
Returns the squared length of the translation component of the homogeneous coordinates transformation...
bool isEqual(const InputType &x, const InputType &y)
Tells whether two objects are equal respectively to epsilon.
base::math::Vector3f computeFastGradient3f(Sampler func)
Computes fast approximation of the gradient at the origin of the scalar field func.
Eigen::Matrix< signed int, 3, 1 > Vector3i
Defines vector.
T length2(const T &x)
Retrieves the squared length of vector and scalar types. General case assumes scalar type.
MatrixType zeros()
Returns matrix with zeros in all components.
Eigen::Matrix< float, 3, 3, Eigen::ColMajor > Matrix3f
Defines matrix.
Matrix4f frustum4f(float left, float right, float bottom, float top, float zNear, float zFar)
Returns the projection matrix that is described by the specified frustum.
Matrix::Scalar maxAbsElement(const Matrix &m)
Returns where $m$ is m.
Matrix3f identity3f()
Returns identity matrix.
Eigen::Matrix< float, 2, 1 > Vector2f
Defines vector.
Matrix4f ortho4f(float left, float right, float bottom, float top, float zNear, float zFar)
Returns the projection matrix that is described by the specified box.
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Matrix4f rotation4f(float x, float y, float z, float radians)
Creates rotation matrix for homogeneous coordinates. The rotation is performed around the axis that i...
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
float epsilon< float >()
Defines the maximum difference of two single-precision floating point objects treated as equal.
Matrix3f rotation3f(float x, float y, float z, float radians)
Creates rotation matrix for homogeneous coordinates, but returns only the upper left sub-matrix.
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Eigen::Matrix< VectorElementType, 4, 1 > vector4(const Eigen::Matrix< VectorElementType, rows, 1 > &v, WType w)
Creates 4-dimensional vector from 3-dimensional (or higher) with same component type,...
ScalarType makeEven(ScalarType x, int s)
Returns if is even and if is odd, where . The data type of must be integral.
Eigen::Matrix< float, 4, 1 > Vector4f
Defines vector.
unsigned int round_ui(ScalarType x)
Rounds x to the closest . Either the data type of must be unsigned or .
float rad2deg(float rad)
Converts radians to degrees.
Vector3f orthogonal3f(const Vector3f &in)
Constructs vector that is orthogonal to in. The result is undefined if the squared length of in equa...
Matrix4f scaling4f(float x, float y, float z)
Creates scaling matrix for homogeneous coordinates.
Matrix4f plane4f(const Vector3f &normal, float distance)
Creates matrix that transforms from the tangent space of a plane with particular normal vector and or...
T sq(T x)
Computes and returns .
float deg2rad(float deg)
Converts degrees to radians.
ResultType mix(const SupportType &a, const SupportType &b, float t)
Interpolates between and by t linearly.
Eigen::Matrix< VectorElementType, 3, 1 > vector3(const Eigen::Matrix< VectorElementType, rows, 1 > &v)
Creates 3-dimensional vector from 4-dimensional (or higher) with same component type by dropping the ...
Matrix4f translation4f(float x, float y, float z)
Returns matrix that translates homogeneous coordinates.
T epsilon()
Defines the maximum difference of two objects treated as equal.
double epsilon< double >()
Defines the maximum difference of two double-precision floating point objects treated as equal.
Matrix4f identity4f()
Returns identity matrix.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
T clamp(T val, T my_min, T my_max)
Returns val my_min my_max .
Matrix4f basis4f(const Vector4f &x, const Vector4f &y, const Vector4f &z, const Vector4f &t=Vector4f(0, 0, 0, 0))
Creates basis embedded into a homogenous coordinates matrix.
Holds mean and variance of an characteristic.
T standardDeviation() const
Computes the standard deviation.
Statistics< T > & operator=(const Statistics< T > &other)
Copies from other.
T variance
Holds the variance.
Statistics(T mean, T variance)
Initializes with mean and variance.
Statistics(std::size_t size, const std::function< T(std::size_t) > values)
Computes statistics from size samples queried from values.
VectorElementType type
The vector element type is known implicitly for each vector type.
Retrieves element types of vectors and scalars. General case assumes a scalar type.
T type
Since T is assumed to be scalar type, it's element type is also T.