12#ifndef MATH_H_6014714286
13#define MATH_H_6014714286
15#include <Carna/Carna.h>
23#error MIN macro defined, define NOMINMAX first!
27#error MAX macro defined, define NOMINMAX first!
63 template<
typename T >
72 template<
typename T >
83 return deg * 3.1415926f / 180.f;
91 return 180.f *
rad / 3.1415926f;
97 template<
typename T >
100 return static_cast< T
>( 0 );
127 template<
typename T >
140 template<
typename VectorElementType,
int rows,
int cols >
154 template<
typename T >
164 template<
typename VectorElementType,
int dimension >
167 return x.squaredNorm();
173 template<
typename InputType >
188 inline bool isEqual(
const bool& x,
const bool& y )
193 typedef Eigen::Matrix< float, 4, 4, Eigen::ColMajor >
Matrix4f;
194 typedef Eigen::Matrix< float, 3, 3, Eigen::ColMajor >
Matrix3f;
198 typedef Eigen::Matrix< signed int, 3, 1 >
Vector3i;
208 result.setIdentity();
218 result.setIdentity();
225 template<
typename MatrixType >
249 for(
unsigned int i = 0;
i < 3; ++
i )
274 result.setIdentity();
283 template<
typename Vector >
295 result.setIdentity();
304 template<
typename VectorElementType >
324 const float c = std::cos(
radians );
325 const float s = std::sin(
radians );
328 result.setIdentity();
330 result( 0, 0 ) = x * x * ( 1 -
c ) +
c;
331 result( 1, 0 ) = y * x * ( 1 -
c ) + z *
s;
332 result( 2, 0 ) = x * z * ( 1 -
c ) - y *
s;
334 result( 0, 1 ) = x * y * ( 1 -
c ) - z *
s;
335 result( 1, 1 ) = y * y * ( 1 -
c ) +
c;
336 result( 2, 1 ) = y * z * ( 1 -
c ) + x *
s;
338 result( 0, 2 ) = x * z * ( 1 -
c ) + y *
s;
339 result( 1, 2 ) = y * z * ( 1 -
c ) - x *
s;
340 result( 2, 2 ) = z * z * ( 1 -
c ) +
c;
347 template<
typename Vector >
370 if( std::abs( in.x() - in.y() ) > 1e-4f )
376 if( std::abs( out.x() ) > std::abs( out.y() ) )
386 if( std::abs( in.x() - in.z() ) > 1e-4f )
392 if( std::abs( out.x() ) > std::abs( out.z() ) )
402 if( std::abs( in.y() - in.z() ) > 1e-4f )
408 if( std::abs( out.y() ) > std::abs( out.z() ) )
419 out =
Vector3f( -in.x(), in.y(), 0 );
433 template<
typename VectorElementType,
int rows,
typename WType >
434 Eigen::Matrix< VectorElementType, 4, 1 >
vector4(
const Eigen::Matrix< VectorElementType, rows, 1 >&
v,
WType w )
436 static_assert(
rows >= 3,
"math::vector4 requires input vector with 3 rows or more." );
437 return Eigen::Matrix< VectorElementType, 4, 1 >(
v.x(),
v.y(),
v.z(),
static_cast< VectorElementType >( w ) );
444 template<
typename VectorElementType,
int rows >
445 Eigen::Matrix< VectorElementType, 3, 1 >
vector3(
const Eigen::Matrix< VectorElementType, rows, 1 >&
v )
447 static_assert(
rows >= 3,
"math::vector3 requires input vector with 3 rows or more." );
448 return Eigen::Matrix< VectorElementType, 3, 1 >(
v.x(),
v.y(),
v.z() );
484 return m( 0, 3 ) *
m( 0, 3 ) +
m( 1, 3 ) *
m( 1, 3 ) +
m( 2, 3 ) *
m( 2, 3 );
490 template<
typename Matrix >
493 const std::size_t
length =
m.rows() *
m.cols();
494 typename Matrix::Scalar
maxAbs = 0;
495 for( std::size_t
i = 0;
i <
length; ++
i )
579 template<
typename Sampler >
596 template<
typename ScalarType >
599 CARNA_ASSERT( !std::numeric_limits< ScalarType >::is_signed || x >= 0 );
600 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 );
639 template<
typename MatrixElementType,
int cols,
int rows >
640 Eigen::Matrix< MatrixElementType, cols, rows >
makeEven(
const Eigen::Matrix< MatrixElementType, cols, rows >&
m,
int s )
642 Eigen::Matrix< unsigned int, cols, rows > result;
643 for(
int i = 0;
i <
cols; ++
i )
644 for(
int j = 0;
j <
rows; ++
j )
654 template<
typename T >
681 for( std::size_t
idx = 0;
idx < size; ++
idx )
690 for( std::size_t
idx = 0;
idx < size; ++
idx )
720 template<
typename ResultType,
typename SupportType >
723 return a * ( 1 -
t ) + b *
t;
733#define CARNA_FOR_VECTOR3UI_EX( vecName, vecLimit, vecStart ) \
734 Carna::base::math::Vector3ui vecName; \
735 for( vecName.z() = vecStart.x(); vecName.z() < vecLimit.z(); ++vecName.z() ) \
736 for( vecName.y() = vecStart.y(); vecName.y() < vecLimit.y(); ++vecName.y() ) \
737 for( vecName.x() = vecStart.z(); vecName.x() < vecLimit.x(); ++vecName.x() )
747#define CARNA_FOR_VECTOR3UI( vecName, vecLimit ) \
748 CARNA_FOR_VECTOR3UI_EX( vecName, vecLimit, Carna::base::math::Vector3ui( 0, 0, 0 ) )
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Represents an association.
Maintains an OpenGL texture sampler object. This class realizes the RAII-idiom.
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 ...
Vector3f orthogonal3f(const Vector3f &in)
Constructs vector that is orthogonal to in. The result is undefined if the squared length of in equa...
Eigen::Matrix< float, 2, 1 > Vector2f
Defines vector.
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.
double epsilon< double >()
Defines the maximum difference of two double-precision floating point objects treated as equal.
Eigen::Matrix< float, 4, 1 > Vector4f
Defines vector.
T epsilon()
Defines the maximum difference of two objects treated as equal.
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...
Matrix3f identity3f()
Returns identity matrix.
float epsilon< float >()
Defines the maximum difference of two single-precision floating point objects treated as equal.
Eigen::Matrix< float, 3, 3, Eigen::ColMajor > Matrix3f
Defines matrix.
float rad2deg(float rad)
Converts radians to degrees.
Eigen::Matrix< float, 3, 1 > Vector3f
Defines vector.
Matrix3f rotation3f(float x, float y, float z, float radians)
Creates rotation matrix for homogeneous coordinates, but returns only the upper left sub-matrix.
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.
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.
Matrix4f plane4f(const Vector3f &normal, float distance)
Creates matrix that transforms from the tangent space of a plane with particular normal vector and or...
float translationDistance2(const Matrix4f &m)
Returns the squared length of the translation component of the homogeneous coordinates transformation...
base::math::Vector3f computeFastGradient3f(Sampler func)
Computes fast approximation of the gradient at the origin of the scalar field func.
T sq(T x)
Computes and returns .
T clamp(T val, T my_min, T my_max)
Returns val my_min my_max .
MatrixType zeros()
Returns matrix with zeros in all components.
bool isEqual(const InputType &x, const InputType &y)
Tells whether two objects are equal respectively to epsilon.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
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,...
Eigen::Matrix< signed int, 3, 1 > Vector3i
Defines vector.
ScalarType makeEven(ScalarType x, int s)
Returns if is even and if is odd, where . The data type of must be integral.
T length2(const T &x)
Retrieves the squared length of vector and scalar types. General case assumes scalar type.
Matrix::Scalar maxAbsElement(const Matrix &m)
Returns where $m$ is m.
Matrix4f identity4f()
Returns identity matrix.
ResultType mix(const SupportType &a, const SupportType &b, float t)
Interpolates between and by t linearly.
float deg2rad(float deg)
Converts degrees to radians.
unsigned int round_ui(ScalarType x)
Rounds x to the closest . Either the data type of must be unsigned or .
Matrix4f scaling4f(float x, float y, float z)
Creates scaling matrix for homogeneous coordinates.
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
Matrix4f translation4f(float x, float y, float z)
Returns matrix that translates homogeneous coordinates.
Holds mean and variance of an characteristic.
Statistics< T > & operator=(const Statistics< T > &other)
Copies from other.
Statistics(std::size_t size, const std::function< T(std::size_t) > values)
Computes statistics from size samples queried from values.
T standardDeviation() const
Computes the standard deviation.
Statistics(T mean, T variance)
Initializes with mean and variance.
T variance
Holds the variance.
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.