From 1606228ec2d569535306e2a80cff92a01f743a32 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 17 Nov 2015 21:22:28 +0100 Subject: [PATCH] [DEV] rework stk::Matrix4 and add etk::Matrix2 --- .travis.yml | 4 +- etk/Color.cpp | 9 +- etk/Color.h | 20 ++-- etk/math/Matrix4.cpp | 180 ++++++++++++++++++++++++++++++++++ etk/math/Matrix4.h | 223 +++++++++---------------------------------- lutin_etk.py | 2 + 6 files changed, 242 insertions(+), 196 deletions(-) diff --git a/.travis.yml b/.travis.yml index 34122ed..55177dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -69,13 +69,13 @@ before_script: fi script: - - lutin -w -j4 -C -P $TARGET -c $BUILDER $COMPILATOR_OPTION -m $CONF $GCOV -p etk-test + - lutin -w -j4 -C -P $TARGET -c $BUILDER $COMPILATOR_OPTION -m $CONF $GCOV etk-test after_script: - if [ "$GCOV" != "" ]; then python ./warning_send.py --find-path ./out/Linux_x86_64/$CONF/build/$BUILDER/etk/; fi - - ./out/Linux_x86_64/$CONF/staging/$BUILDER/etk-test/usr/bin/etk-test -l6 | tee out_test.txt + - ./out/Linux_x86_64/$CONF/staging/$BUILDER/etk-test/etk-test.app/bin/etk-test -l6 | tee out_test.txt - if [ "$GCOV" != "" ]; then python ./test_send.py --file=out_test.txt; lutin -C -P -c $TARGET $BUILDER $COMPILATOR_OPTION -m $CONF -p etk?gcov; diff --git a/etk/Color.cpp b/etk/Color.cpp index a10fc05..559f373 100644 --- a/etk/Color.cpp +++ b/etk/Color.cpp @@ -31,7 +31,7 @@ etk::Color etk::parseStringStartWithSharp(const std::string& _input) size_t len = _input.size(); etk::Color outputValue(0,0,0,0); if(len == 3) { - int32_t red=0, green=0, blue=0; + uint32_t red=0, green=0, blue=0; if (sscanf(_input.c_str(), "%1x%1x%1x", &red, &green, &blue) == 3) { outputValue.setR(red | red << 4); outputValue.setG(green | green << 4); @@ -41,7 +41,7 @@ etk::Color etk::parseStringStartWithSharp(const std::string& _input) TK_ERROR(" pb in parsing the color : '" << _input << "'"); } } else if (len==4) { - int32_t red=0, green=0, blue=0, alpha=0; + uint32_t red=0, green=0, blue=0, alpha=0; if (sscanf(_input.c_str(), "%1x%1x%1x%1x", &red, &green, &blue, &alpha) == 4) { outputValue.setR(red | red << 4); outputValue.setG(green | green << 4); @@ -51,7 +51,7 @@ etk::Color etk::parseStringStartWithSharp(const std::string& _input) TK_ERROR(" pb in parsing the color : '" << _input << "'"); } } else if (len == 6) { - int32_t red=0, green=0, blue=0; + uint32_t red=0, green=0, blue=0; if (sscanf(_input.c_str(), "%2x%2x%2x", &red, &green, &blue) == 3) { outputValue.setR(red); outputValue.setG(green); @@ -61,7 +61,7 @@ etk::Color etk::parseStringStartWithSharp(const std::string& _input) TK_ERROR(" pb in parsing the color : '" << _input << "'"); } } else if (len == 8) { - int32_t red=0, green=0, blue=0, alpha=0; + uint32_t red=0, green=0, blue=0, alpha=0; if (sscanf(_input.c_str(), "%2x%2x%2x%2x", &red, &green, &blue, &alpha) == 4) { outputValue.setR(red); outputValue.setG(green); @@ -73,6 +73,7 @@ etk::Color etk::parseStringStartWithSharp(const std::string& _input) } else { TK_ERROR(" pb in parsing the color : '" << _input << "' ==> unknown methode ..."); } + TK_VERBOSE(" result: '" << outputValue << "'"); return outputValue; } diff --git a/etk/Color.h b/etk/Color.h index 844246b..1c450b5 100644 --- a/etk/Color.h +++ b/etk/Color.h @@ -253,7 +253,7 @@ namespace etk { */ void setR(MY_TYPE _r) { if (MY_TYPE_SIZE >= 1) { - m_element[0] = _r; + m_element[0] = MY_TYPE(_r); } }; /** @@ -262,7 +262,7 @@ namespace etk { */ void setG(MY_TYPE _g) { if (MY_TYPE_SIZE >= 2) { - m_element[1] = _g; + m_element[1] = MY_TYPE(_g); } }; /** @@ -271,7 +271,7 @@ namespace etk { */ void setB(MY_TYPE _b) { if (MY_TYPE_SIZE >= 3) { - m_element[2] = _b; + m_element[2] = MY_TYPE(_b); } }; /** @@ -280,7 +280,7 @@ namespace etk { */ void setA(MY_TYPE _a) { if (MY_TYPE_SIZE >= 4) { - m_element[3] = _a; + m_element[3] = MY_TYPE(_a); } }; }; @@ -356,15 +356,15 @@ namespace etk { std::ostringstream oss; if (MY_TYPE_SIZE >= 3) { _os << "#"; - oss << std::setw(2) << std::setfill('0') << std::hex << _obj.r(); + oss << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.r()); if (MY_TYPE_SIZE >= 2) { - oss << std::setw(2) << std::setfill('0') << std::hex << _obj.g(); + oss << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.g()); } if (MY_TYPE_SIZE >= 3) { - oss << std::setw(2) << std::setfill('0') << std::hex << _obj.b(); + oss << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.b()); } if (MY_TYPE_SIZE >= 4) { - oss << std::setw(2) << std::setfill('0') << std::hex << _obj.a(); + oss << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.a()); } _os << oss.str(); } else { @@ -374,10 +374,10 @@ namespace etk { _os << "Mono"; } _os << "[U8]("; - oss << "0x" << std::setw(2) << std::setfill('0') << std::hex << _obj.r(); + oss << "0x" << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.r()); if (MY_TYPE_SIZE >= 2) { _os << ","; - oss << "0x" << std::setw(2) << std::setfill('0') << std::hex << _obj.g(); + oss << "0x" << std::setw(2) << std::setfill('0') << std::hex << uint32_t(_obj.g()); } _os << oss.str(); _os << ")"; diff --git a/etk/math/Matrix4.cpp b/etk/math/Matrix4.cpp index 8ecbbf8..f08f0c9 100644 --- a/etk/math/Matrix4.cpp +++ b/etk/math/Matrix4.cpp @@ -12,6 +12,186 @@ #include #if 1 + +void etk::Matrix4::identity() { + for(int32_t iii=0; iii<4*4 ; iii++) { + m_mat[iii] = 0; + } + m_mat[0] = 1.0; + m_mat[5] = 1.0; + m_mat[10] = 1.0; + m_mat[15] = 1.0; +} + +etk::Matrix4::Matrix4() { + identity(); +} + +etk::Matrix4::Matrix4(const Matrix4& _obj) { + for(int32_t iii=0; iii<4*4 ; iii++) { + m_mat[iii] = _obj.m_mat[iii]; + } +} + +etk::Matrix4::Matrix4(float _a1, float _b1, float _c1, float _d1, + float _a2, float _b2, float _c2, float _d2, + float _a3, float _b3, float _c3, float _d3, + float _a4, float _b4, float _c4, float _d4) { + m_mat[0] = _a1; + m_mat[1] = _b1; + m_mat[2] = _c1; + m_mat[3] = _d1; + m_mat[4] = _a2; + m_mat[5] = _b2; + m_mat[6] = _c2; + m_mat[7] = _d2; + m_mat[8] = _a3; + m_mat[9] = _b3; + m_mat[10] = _c3; + m_mat[11] = _d3; + m_mat[12] = _a4; + m_mat[13] = _b4; + m_mat[14] = _c4; + m_mat[15] = _d4; +} + +etk::Matrix4::Matrix4(float* _obj) { + if (_obj == nullptr) { + identity(); + return; + } + for(int32_t iii=0; iii<4*4 ; ++iii) { + m_mat[iii] = _obj[iii]; + } +} + +etk::Matrix4::~Matrix4() { + +} + +const etk::Matrix4& etk::Matrix4::operator= (const etk::Matrix4& _obj ) { + for(int32_t iii=0; iii<4*4 ; ++iii) { + m_mat[iii] = _obj.m_mat[iii]; + } + return *this; +} + +bool etk::Matrix4::operator== (const etk::Matrix4& _obj) const { + for(int32_t iii=0; iii<4*4 ; ++iii) { + if(m_mat[iii] != _obj.m_mat[iii]) { + return false; + } + } + return true; +} + +bool etk::Matrix4::operator!= (const etk::Matrix4& _obj) const { + for(int32_t iii=0; iii<4*4 ; ++iii) { + if(m_mat[iii] != _obj.m_mat[iii]) { + return true; + } + } + return false; +} + +const etk::Matrix4& etk::Matrix4::operator+= (const etk::Matrix4& _obj) { + for(int32_t iii=0; iii<4*4 ; ++iii) { + m_mat[iii] += _obj.m_mat[iii]; + } + return *this; +} + +etk::Matrix4 etk::Matrix4::operator+ (const etk::Matrix4& _obj) const { + etk::Matrix4 tmpp(*this); + tmpp += _obj; + return tmpp; +} + +const etk::Matrix4& etk::Matrix4::operator-= (const etk::Matrix4& _obj) { + for(int32_t iii=0; iii<4*4 ; ++iii) { + m_mat[iii] -= _obj.m_mat[iii]; + } + return *this; +} + +etk::Matrix4 etk::Matrix4::operator- (const etk::Matrix4& _obj) const { + etk::Matrix4 tmpp(*this); + tmpp += _obj; + return tmpp; +} + +const etk::Matrix4& etk::Matrix4::operator*= (const etk::Matrix4& _obj) { + // output Matrix + float matrixOut[4*4]; + for(int32_t jjj=0; jjj<4 ; jjj++) { + float* tmpLeft = m_mat + jjj*4; + for(int32_t iii=0; iii<4 ; iii++) { + const float* tmpUpper = _obj.m_mat+iii; + float* tmpLeft2 = tmpLeft; + float tmpElement = 0; + for(int32_t kkk=0; kkk<4 ; kkk++) { + tmpElement += *tmpUpper * *tmpLeft2; + tmpUpper += 4; + tmpLeft2++; + } + matrixOut[jjj*4+iii] = tmpElement; + } + } + // set it at the output + for(int32_t iii=0; iii<4*4 ; iii++) { + m_mat[iii] = matrixOut[iii]; + } + return *this; +} + +etk::Matrix4 etk::Matrix4::operator* (const etk::Matrix4& _obj) const { + etk::Matrix4 tmpp(*this); + tmpp *= _obj; + return tmpp; +} + +vec3 etk::Matrix4::operator*(const vec3& _point) const { + return vec3( m_mat[0]*_point.x() + m_mat[1]*_point.y() + m_mat[2]*_point.z() + m_mat[3], + m_mat[4]*_point.x() + m_mat[5]*_point.y() + m_mat[6]*_point.z() + m_mat[7], + m_mat[8]*_point.x() + m_mat[9]*_point.y() + m_mat[10]*_point.z() + m_mat[11] ); +} + +void etk::Matrix4::transpose() { + float tmpVal = m_mat[1]; + m_mat[1] = m_mat[4]; + m_mat[4] = tmpVal; + + tmpVal = m_mat[2]; + m_mat[2] = m_mat[8]; + m_mat[8] = tmpVal; + + tmpVal = m_mat[6]; + m_mat[6] = m_mat[9]; + m_mat[9] = tmpVal; + + tmpVal = m_mat[3]; + m_mat[3] = m_mat[12]; + m_mat[12] = tmpVal; + + tmpVal = m_mat[7]; + m_mat[7] = m_mat[13]; + m_mat[13] = tmpVal; + + tmpVal = m_mat[11]; + m_mat[11] = m_mat[14]; + m_mat[14] = tmpVal; +} + +void etk::Matrix4::scale(const vec3& _vect) { + scale(_vect.x(), _vect.y(), _vect.z()); +} + +void etk::Matrix4::scale(float _sx, float _sy, float _sz) { + m_mat[0] *= _sx; m_mat[1] *= _sy; m_mat[2] *= _sz; + m_mat[4] *= _sx; m_mat[5] *= _sy; m_mat[6] *= _sz; + m_mat[8] *= _sx; m_mat[9] *= _sy; m_mat[10] *= _sz; +} + void etk::Matrix4::rotate(const vec3& vect, float angleRad) { etk::Matrix4 tmpMat = etk::matRotate(vect, angleRad); diff --git a/etk/math/Matrix4.h b/etk/math/Matrix4.h index 7de8d53..ec1211e 100644 --- a/etk/math/Matrix4.h +++ b/etk/math/Matrix4.h @@ -23,224 +23,87 @@ namespace etk { class Matrix4 { public: float m_mat[4*4]; - void identity() { - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] = 0; - } - m_mat[0] = 1.0; - m_mat[5] = 1.0; - m_mat[10] = 1.0; - m_mat[15] = 1.0; - } + void identity(); /***************************************************** * Constructor *****************************************************/ - Matrix4() { - identity(); - } - Matrix4(const Matrix4& obj) { - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] = obj.m_mat[iii]; - } - } - Matrix4(float a1, float b1, float c1, float d1, - float a2, float b2, float c2, float d2, - float a3, float b3, float c3, float d3, - float a4, float b4, float c4, float d4) { - m_mat[0] = a1; - m_mat[1] = b1; - m_mat[2] = c1; - m_mat[3] = d1; - m_mat[4] = a2; - m_mat[5] = b2; - m_mat[6] = c2; - m_mat[7] = d2; - m_mat[8] = a3; - m_mat[9] = b3; - m_mat[10] = c3; - m_mat[11] = d3; - m_mat[12] = a4; - m_mat[13] = b4; - m_mat[14] = c4; - m_mat[15] = d4; - } - Matrix4(float * obj) { - if (NULL == obj) { - identity(); - return; - } - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] = obj[iii]; - } - } + Matrix4(); + Matrix4(const Matrix4& _obj); + Matrix4(float _a1, float _b1, float _c1, float _d1, + float _a2, float _b2, float _c2, float _d2, + float _a3, float _b3, float _c3, float _d3, + float _a4, float _b4, float _c4, float _d4); + Matrix4(float* _obj); /***************************************************** * Destructor *****************************************************/ - virtual ~Matrix4() { - - } + ~Matrix4(); /***************************************************** * = assigment *****************************************************/ - const Matrix4& operator= (const Matrix4& obj ) { - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] = obj.m_mat[iii]; - } - return *this; - } + const Matrix4& operator= (const Matrix4& _obj ); /***************************************************** * == operator *****************************************************/ - bool operator== (const Matrix4& obj) const { - for(int32_t iii=0; iii<4*4 ; iii++) { - if(m_mat[iii] != obj.m_mat[iii]) { - return false; - } - } - return true; - } + bool operator== (const Matrix4& _obj) const; /***************************************************** * != operator *****************************************************/ - bool operator!= (const Matrix4& obj) const { - for(int32_t iii=0; iii<4*4 ; iii++) { - if(m_mat[iii] != obj.m_mat[iii]) { - return true; - } - } - return false; - } + bool operator!= (const Matrix4& _obj) const; /***************************************************** * += operator *****************************************************/ - const Matrix4& operator+= (const Matrix4& obj) { - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] += obj.m_mat[iii]; - } - return *this; - } + const Matrix4& operator+= (const Matrix4& _obj); /***************************************************** * + operator *****************************************************/ - Matrix4 operator+ (const Matrix4& obj) const { - Matrix4 tmpp(*this); - tmpp += obj; - return tmpp; - } + Matrix4 operator+ (const Matrix4& _obj) const; /***************************************************** * -= operator *****************************************************/ - const Matrix4& operator-= (const Matrix4& obj) { - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] -= obj.m_mat[iii]; - } - return *this; - } + const Matrix4& operator-= (const Matrix4& _obj); /***************************************************** * - operator *****************************************************/ - Matrix4 operator- (const Matrix4& obj) const { - Matrix4 tmpp(*this); - tmpp += obj; - return tmpp; - } + Matrix4 operator- (const Matrix4& _obj) const; /***************************************************** * *= operator *****************************************************/ - const Matrix4& operator*= (const Matrix4& obj) { - // output Matrix - float matrixOut[4*4]; - for(int32_t jjj=0; jjj<4 ; jjj++) { - float* tmpLeft = m_mat + jjj*4; - for(int32_t iii=0; iii<4 ; iii++) { - const float* tmpUpper = obj.m_mat+iii; - float* tmpLeft2 = tmpLeft; - float tmpElement = 0; - for(int32_t kkk=0; kkk<4 ; kkk++) { - tmpElement += *tmpUpper * *tmpLeft2; - tmpUpper += 4; - tmpLeft2++; - } - matrixOut[jjj*4+iii] = tmpElement; - } - } - // set it at the output - for(int32_t iii=0; iii<4*4 ; iii++) { - m_mat[iii] = matrixOut[iii]; - } - return *this; - } + const Matrix4& operator*= (const Matrix4& _obj); /***************************************************** * * operator *****************************************************/ - Matrix4 operator* (const Matrix4& obj) const { - Matrix4 tmpp(*this); - tmpp *= obj; - return tmpp; - } - vec3 operator*(const vec3& point) const - { - return vec3( m_mat[0]*point.x() + m_mat[1]*point.y() + m_mat[2]*point.z() + m_mat[3], - m_mat[4]*point.x() + m_mat[5]*point.y() + m_mat[6]*point.z() + m_mat[7], - m_mat[8]*point.x() + m_mat[9]*point.y() + m_mat[10]*point.z() + m_mat[11] ); - } + Matrix4 operator* (const Matrix4& _obj) const; + vec3 operator*(const vec3& _point) const; /***************************************************** * other basic function : *****************************************************/ - void transpose() - { - float tmpVal = m_mat[1]; - m_mat[1] = m_mat[4]; - m_mat[4] = tmpVal; - - tmpVal = m_mat[2]; - m_mat[2] = m_mat[8]; - m_mat[8] = tmpVal; - - tmpVal = m_mat[6]; - m_mat[6] = m_mat[9]; - m_mat[9] = tmpVal; - - tmpVal = m_mat[3]; - m_mat[3] = m_mat[12]; - m_mat[12] = tmpVal; - - tmpVal = m_mat[7]; - m_mat[7] = m_mat[13]; - m_mat[13] = tmpVal; - - tmpVal = m_mat[11]; - m_mat[11] = m_mat[14]; - m_mat[14] = tmpVal; - } - void scale(const vec3& p) - { - scale(p.x(), p.y(), p.z()); - } - void scale(float sx, float sy, float sz) - { - m_mat[0] *= sx; m_mat[1] *= sy; m_mat[2] *= sz; - m_mat[4] *= sx; m_mat[5] *= sy; m_mat[6] *= sz; - m_mat[8] *= sx; m_mat[9] *= sy; m_mat[10] *= sz; - } + void transpose(); + /** + * @brief Scale the current Matrix. + * @param[in] _vect Scale vector to apply. + */ + void scale(const vec3& _vect); + //! @previous + void scale(float _sx, float _sy, float _sz); /** * @brief Makes a rotation matrix about an arbitrary axis. - * @param[in] vect vector to apply the angle. - * @param[in] angleRad angle to apply. + * @param[in] _vect vector to apply the angle. + * @param[in] _angleRad angle to apply. */ - void rotate(const vec3& vect, float angleRad=0.0); + void rotate(const vec3& _vect, float _angleRad=0.0); /** * @brief Makes a translation of the matrix - * @param[in] vect Translation to apply. + * @param[in] _vect Translation to apply. */ - void translate(const vec3& vect); + void translate(const vec3& _vect); /** * @brief Computes a cofactor. Used for matrix inversion. - * @param[in] row Id of raw. - * @param[in] col Id of colomn. + * @param[in] _row Id of raw. + * @param[in] _col Id of colomn. * @return the coFactorValue. */ - float coFactor(int32_t row, int32_t col) const; + float coFactor(int32_t _row, int32_t _col) const; /** * @brief Computes the determinant of the matrix. * @return The determinent Value. @@ -253,13 +116,13 @@ namespace etk { */ Matrix4 invert(); }; - Matrix4 matFrustum(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar); - Matrix4 matPerspective(float foxy, float aspect, float zNear, float zFar); - Matrix4 matOrtho(float left, float right, float bottom, float top, float nearVal, float farVal); - Matrix4 matTranslate(vec3 vect); - Matrix4 matScale(vec3 vect); - Matrix4 matRotate(vec3 vect, float angleRad=0.0); - Matrix4 matRotate2(vec3 vect); + Matrix4 matFrustum(float _xmin, float _xmax, float _ymin, float _ymax, float _zNear, float _zFar); + Matrix4 matPerspective(float _foxy, float _aspect, float _zNear, float _zFar); + Matrix4 matOrtho(float _left, float _right, float _bottom, float _top, float _nearVal, float _farVal); + Matrix4 matTranslate(vec3 _vect); + Matrix4 matScale(vec3 _vect); + Matrix4 matRotate(vec3 _vect, float _angleRad=0.0); + Matrix4 matRotate2(vec3 _vect); Matrix4 matLookAt(const vec3& _eye, const vec3& _target, const vec3& _up); @@ -271,7 +134,7 @@ namespace etk { // To siplify the writing of the code ==> this permit to have the same name with the glsl language... -typedef etk::Matrix4 mat4; +typedef etk::Matrix4 mat4; #else // include matrix from bulletlib mat4 interface: #include diff --git a/lutin_etk.py b/lutin_etk.py index ce6db13..adcbce7 100644 --- a/lutin_etk.py +++ b/lutin_etk.py @@ -37,6 +37,7 @@ def create(target, module_name): 'etk/Noise.cpp', 'etk/Color.cpp', 'etk/thread/tools.cpp', + 'etk/math/Matrix2.cpp', 'etk/math/Matrix4.cpp', 'etk/math/Vector2D.cpp', 'etk/math/Vector3D.cpp', @@ -59,6 +60,7 @@ def create(target, module_name): 'etk/Color.h', 'etk/Hash.h', 'etk/thread/tools.h', + 'etk/math/Matrix2.h', 'etk/math/Matrix4.h', 'etk/math/Vector2D.h', 'etk/math/Vector3D.h',