[DEV] add vec3 min max

This commit is contained in:
Edouard DUPIN 2017-05-05 22:27:14 +02:00
parent 01d6aadf13
commit f03e3ebe3d
2 changed files with 75 additions and 70 deletions

View File

@ -184,86 +184,76 @@ void etk::Matrix4::scale(float _sx, float _sy, float _sz) {
m_mat[8] *= _sx; m_mat[9] *= _sy; m_mat[10] *= _sz; m_mat[8] *= _sx; m_mat[9] *= _sy; m_mat[10] *= _sz;
} }
void etk::Matrix4::rotate(const vec3& vect, float angleRad) void etk::Matrix4::rotate(const vec3& _vect, float _angleRad) {
{ etk::Matrix4 tmpMat = etk::matRotate(_vect, _angleRad);
etk::Matrix4 tmpMat = etk::matRotate(vect, angleRad);
*this *= tmpMat; *this *= tmpMat;
} }
void etk::Matrix4::translate(const vec3& vect) void etk::Matrix4::translate(const vec3& _vect) {
{ etk::Matrix4 tmpMat = etk::matTranslate(_vect);
etk::Matrix4 tmpMat = etk::matTranslate(vect);
*this *= tmpMat; *this *= tmpMat;
} }
etk::Matrix4 etk::matFrustum(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar) etk::Matrix4 etk::matFrustum(float _xmin, float _xmax, float _ymin, float _ymax, float _zNear, float _zFar) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
for(int32_t iii=0; iii<4*4 ; iii++) { for(int32_t iii=0; iii<4*4 ; iii++) {
tmp.m_mat[iii] = 0; tmp.m_mat[iii] = 0;
} }
// 0 1 2 3 // 0 1 2 3
// 4 5 6 7 // 4 5 6 7
// 8 9 10 11 // 8 9 10 11
// 12 13 14 15 // 12 13 14 15
tmp.m_mat[0] = (2.0 * _zNear) / (_xmax - _xmin);
tmp.m_mat[0] = (2.0 * zNear) / (xmax - xmin); tmp.m_mat[5] = (2.0 * _zNear) / (_ymax - _ymin);
tmp.m_mat[5] = (2.0 * zNear) / (ymax - ymin); tmp.m_mat[10] = -(_zFar + _zNear) / (_zFar - _zNear);
tmp.m_mat[10] = -(zFar + zNear) / (zFar - zNear); tmp.m_mat[2] = (_xmax + _xmin) / (_xmax - _xmin);
tmp.m_mat[2] = (xmax + xmin) / (xmax - xmin); tmp.m_mat[6] = (_ymax + _ymin) / (_ymax - _ymin);
tmp.m_mat[6] = (ymax + ymin) / (ymax - ymin);
tmp.m_mat[14] = -1.0; tmp.m_mat[14] = -1.0;
tmp.m_mat[11] = -(2.0 * zFar * zNear) / (zFar - zNear); tmp.m_mat[11] = -(2.0 * _zFar * _zNear) / (_zFar - _zNear);
return tmp; return tmp;
} }
etk::Matrix4 etk::matPerspective(float fovx, float aspect, float zNear, float zFar) etk::Matrix4 etk::matPerspective(float _fovx, float _aspect, float _zNear, float _zFar) {
{
//TK_DEBUG("drax perspective: fovx=" << fovx << "->" << aspect << " " << zNear << "->" << zFar); //TK_DEBUG("drax perspective: fovx=" << fovx << "->" << aspect << " " << zNear << "->" << zFar);
float xmax = zNear * tanf(fovx/2.0); float xmax = _zNear * tanf(_fovx/2.0);
float xmin = -xmax; float xmin = -xmax;
float ymin = xmin / aspect; float ymin = xmin / _aspect;
float ymax = xmax / aspect; float ymax = xmax / _aspect;
//TK_DEBUG("drax perspective: " << xmin << "->" << xmax << " & " << ymin << "->" << ymax << " " << zNear << "->" << zFar); //TK_DEBUG("drax perspective: " << xmin << "->" << xmax << " & " << ymin << "->" << ymax << " " << zNear << "->" << zFar);
return etk::matFrustum(xmin, xmax, ymin, ymax, zNear, zFar); return etk::matFrustum(xmin, xmax, ymin, ymax, _zNear, _zFar);
} }
etk::Matrix4 etk::matOrtho(float left, float right, float bottom, float top, float nearVal, float farVal) etk::Matrix4 etk::matOrtho(float _left, float _right, float _bottom, float _top, float _nearVal, float _farVal) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
for(int32_t iii=0; iii<4*4 ; iii++) { for(int32_t iii=0; iii<4*4 ; iii++) {
tmp.m_mat[iii] = 0; tmp.m_mat[iii] = 0;
} }
tmp.m_mat[0] = 2.0 / (right - left); tmp.m_mat[0] = 2.0 / (_right - _left);
tmp.m_mat[5] = 2.0 / (top - bottom); tmp.m_mat[5] = 2.0 / (_top - _bottom);
tmp.m_mat[10] = -2.0 / (farVal - nearVal); tmp.m_mat[10] = -2.0 / (_farVal - _nearVal);
tmp.m_mat[3] = -1*(right + left) / (right - left); tmp.m_mat[3] = -1*(_right + _left) / (_right - _left);
tmp.m_mat[7] = -1*(top + bottom) / (top - bottom); tmp.m_mat[7] = -1*(_top + _bottom) / (_top - _bottom);
tmp.m_mat[11] = -1*(farVal + nearVal) / (farVal - nearVal); tmp.m_mat[11] = -1*(_farVal + _nearVal) / (_farVal - _nearVal);
tmp.m_mat[15] = 1; tmp.m_mat[15] = 1;
return tmp; return tmp;
} }
etk::Matrix4 etk::matTranslate(vec3 vect) etk::Matrix4 etk::matTranslate(vec3 _vect) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
// set translation : // set translation :
tmp.m_mat[3] = vect.x(); tmp.m_mat[3] = _vect.x();
tmp.m_mat[7] = vect.y(); tmp.m_mat[7] = _vect.y();
tmp.m_mat[11] = vect.z(); tmp.m_mat[11] = _vect.z();
//TK_INFO("Translate :"); //TK_INFO("Translate :");
//etk::matrix::Display(tmp); //etk::matrix::Display(tmp);
return tmp; return tmp;
} }
etk::Matrix4 etk::matScale(vec3 vect) etk::Matrix4 etk::matScale(vec3 _vect) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
tmp.scale(vect); tmp.scale(_vect);
/* /*
// set scale : // set scale :
tmp.m_mat[0] = vect.x; tmp.m_mat[0] = vect.x;
@ -275,36 +265,33 @@ etk::Matrix4 etk::matScale(vec3 vect)
return tmp; return tmp;
} }
etk::Matrix4 etk::matRotate(vec3 vect, float angleRad) etk::Matrix4 etk::matRotate(vec3 _vect, float _angleRad) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
float cosVal = cos(angleRad); float cosVal = cos(_angleRad);
float sinVal = sin(angleRad); float sinVal = sin(_angleRad);
float invVal = 1.0-cosVal; float invVal = 1.0-cosVal;
// set rotation : // set rotation :
tmp.m_mat[0] = vect.x() * vect.x() * invVal + cosVal; tmp.m_mat[0] = _vect.x() * _vect.x() * invVal + cosVal;
tmp.m_mat[1] = vect.x() * vect.y() * invVal - vect.z() * sinVal; tmp.m_mat[1] = _vect.x() * _vect.y() * invVal - _vect.z() * sinVal;
tmp.m_mat[2] = vect.x() * vect.z() * invVal + vect.y() * sinVal; tmp.m_mat[2] = _vect.x() * _vect.z() * invVal + _vect.y() * sinVal;
tmp.m_mat[4] = vect.y() * vect.x() * invVal + vect.z() * sinVal; tmp.m_mat[4] = _vect.y() * _vect.x() * invVal + _vect.z() * sinVal;
tmp.m_mat[5] = vect.y() * vect.y() * invVal + cosVal; tmp.m_mat[5] = _vect.y() * _vect.y() * invVal + cosVal;
tmp.m_mat[6] = vect.y() * vect.z() * invVal - vect.x() * sinVal; tmp.m_mat[6] = _vect.y() * _vect.z() * invVal - _vect.x() * sinVal;
tmp.m_mat[8] = vect.z() * vect.x() * invVal - vect.y() * sinVal; tmp.m_mat[8] = _vect.z() * _vect.x() * invVal - _vect.y() * sinVal;
tmp.m_mat[9] = vect.z() * vect.y() * invVal + vect.x() * sinVal; tmp.m_mat[9] = _vect.z() * _vect.y() * invVal + _vect.x() * sinVal;
tmp.m_mat[10] = vect.z() * vect.z() * invVal + cosVal; tmp.m_mat[10] = _vect.z() * _vect.z() * invVal + cosVal;
return tmp; return tmp;
} }
etk::Matrix4 etk::matRotate2(vec3 vect) etk::Matrix4 etk::matRotate2(vec3 _vect) {
{ return matLookAt(_vect, vec3(0,0,0), vec3(0,1,0));
return matLookAt(vect, vec3(0,0,0), vec3(0,1,0));
} }
etk::Matrix4 etk::matLookAt(const vec3& _eye, etk::Matrix4 etk::matLookAt(const vec3& _eye,
const vec3& _target, const vec3& _target,
const vec3& _up) const vec3& _up) {
{
etk::Matrix4 tmp; etk::Matrix4 tmp;
vec3 forward = _eye; vec3 forward = _eye;
@ -338,20 +325,18 @@ etk::Matrix4 etk::matLookAt(const vec3& _eye,
} }
float etk::Matrix4::coFactor(int32_t row, int32_t col) const float etk::Matrix4::coFactor(int32_t _row, int32_t _col) const {
{ return ( ( m_mat[((_row+1)&3)*4 + ((_col+1)&3)] * m_mat[((_row+2)&3)*4 + ((_col+2)&3)] * m_mat[((_row+3)&3)*4 + ((_col+3)&3)]
return ( ( m_mat[((row+1)&3)*4 + ((col+1)&3)] * m_mat[((row+2)&3)*4 + ((col+2)&3)] * m_mat[((row+3)&3)*4 + ((col+3)&3)] + m_mat[((_row+1)&3)*4 + ((_col+2)&3)] * m_mat[((_row+2)&3)*4 + ((_col+3)&3)] * m_mat[((_row+3)&3)*4 + ((_col+1)&3)]
+ m_mat[((row+1)&3)*4 + ((col+2)&3)] * m_mat[((row+2)&3)*4 + ((col+3)&3)] * m_mat[((row+3)&3)*4 + ((col+1)&3)] + m_mat[((_row+1)&3)*4 + ((_col+3)&3)] * m_mat[((_row+2)&3)*4 + ((_col+1)&3)] * m_mat[((_row+3)&3)*4 + ((_col+2)&3)] )
+ m_mat[((row+1)&3)*4 + ((col+3)&3)] * m_mat[((row+2)&3)*4 + ((col+1)&3)] * m_mat[((row+3)&3)*4 + ((col+2)&3)] ) - ( m_mat[((_row+3)&3)*4 + ((_col+1)&3)] * m_mat[((_row+2)&3)*4 + ((_col+2)&3)] * m_mat[((_row+1)&3)*4 + ((_col+3)&3)]
- ( m_mat[((row+3)&3)*4 + ((col+1)&3)] * m_mat[((row+2)&3)*4 + ((col+2)&3)] * m_mat[((row+1)&3)*4 + ((col+3)&3)] + m_mat[((_row+3)&3)*4 + ((_col+2)&3)] * m_mat[((_row+2)&3)*4 + ((_col+3)&3)] * m_mat[((_row+1)&3)*4 + ((_col+1)&3)]
+ m_mat[((row+3)&3)*4 + ((col+2)&3)] * m_mat[((row+2)&3)*4 + ((col+3)&3)] * m_mat[((row+1)&3)*4 + ((col+1)&3)] + m_mat[((_row+3)&3)*4 + ((_col+3)&3)] * m_mat[((_row+2)&3)*4 + ((_col+1)&3)] * m_mat[((_row+1)&3)*4 + ((_col+2)&3)] )
+ m_mat[((row+3)&3)*4 + ((col+3)&3)] * m_mat[((row+2)&3)*4 + ((col+1)&3)] * m_mat[((row+1)&3)*4 + ((col+2)&3)] ) ) * ((_row + _col) & 1 ? -1.0f : +1.0f);
) * ((row + col) & 1 ? -1.0f : +1.0f);
} }
float etk::Matrix4::determinant() const float etk::Matrix4::determinant() const {
{
return m_mat[0] * coFactor(0, 0) + return m_mat[0] * coFactor(0, 0) +
m_mat[1] * coFactor(0, 1) + m_mat[1] * coFactor(0, 1) +
m_mat[2] * coFactor(0, 2) + m_mat[2] * coFactor(0, 2) +
@ -359,8 +344,7 @@ float etk::Matrix4::determinant() const
} }
etk::Matrix4 etk::Matrix4::invert() etk::Matrix4 etk::Matrix4::invert() {
{
float det = determinant(); float det = determinant();
if(fabsf(det) < (1.0e-7f)) { if(fabsf(det) < (1.0e-7f)) {
// The matrix is not invertible! Singular case! // The matrix is not invertible! Singular case!

View File

@ -563,6 +563,27 @@ inline vec3 vec3ClipInt32(const vec3& _val) {
inline vec3 vec3ClipInt64(const vec3& _val) { inline vec3 vec3ClipInt64(const vec3& _val) {
return vec3(int64_t(_val.x()), int64_t(_val.y()), int64_t(_val.z())); return vec3(int64_t(_val.x()), int64_t(_val.y()), int64_t(_val.z()));
} }
#ifdef ETK_BUILD_LINEARMATH
namespace etk {
inline btVector3 min(const btVector3& _val1, const btVector3& _val2) {
return btVector3(std::min(_val1.x(), _val2.x()), std::min(_val1.y(), _val2.y()), std::min(_val1.z(), _val2.z()));
}
inline btVector3 max(const btVector3& _val1, const btVector3& _val2) {
return btVector3(std::max(_val1.x(), _val2.x()), std::max(_val1.y(), _val2.y()), std::max(_val1.z(), _val2.z()));
}
}
#endif
namespace etk {
template<class ETK_TYPE>
inline etk::Vector3D<ETK_TYPE> min(const etk::Vector3D<ETK_TYPE>& _val1, const etk::Vector3D<ETK_TYPE>& _val2) {
return etk::Vector3D<ETK_TYPE>(std::min(_val1.x(), _val2.x()), std::min(_val1.y(), _val2.y()), std::min(_val1.z(), _val2.z()));
}
template<class ETK_TYPE>
inline etk::Vector3D<ETK_TYPE> max(const etk::Vector3D<ETK_TYPE>& _val1, const etk::Vector3D<ETK_TYPE>& _val2) {
return etk::Vector3D<ETK_TYPE>(std::max(_val1.x(), _val2.x()), std::max(_val1.y(), _val2.y()), std::max(_val1.z(), _val2.z()));
}
}
namespace etk { namespace etk {
//! @not_in_doc //! @not_in_doc