[DEV] update some functions

This commit is contained in:
Edouard DUPIN 2017-06-16 20:43:14 +02:00
parent 13de53b408
commit 90fff0caff
2 changed files with 55 additions and 0 deletions

View File

@ -234,6 +234,35 @@ namespace etk {
tmp.normalize();
return tmp;
}
/**
* @brief Normalize this quaternion x^2 + y^2 + z^2 + w^2 = 1
* @return Local reference of the quaternion normalized
*/
Quaternion& safeNormalize() {
float lengthTmp = length();
if (lengthTmp == 0.0f) {
m_floats[0] = 0.0f;
m_floats[1] = 0.0f;
m_floats[2] = 0.0f;
m_floats[3] = 1.0f;
return *this;
}
float invLength = 1.0f / lengthTmp;
m_floats[0] *= invLength;
m_floats[1] *= invLength;
m_floats[2] *= invLength;
m_floats[3] *= invLength;
return *this;
}
/**
* @brief Return a normalized version of this quaternion
* @return New quaternion containing the value
*/
Quaternion safeNormalized() const {
etk::Quaternion tmp = *this;
tmp.normalized();
return tmp;
}
/**
* @brief Return a quaternion will the absolute values of each element
* @return New quaternion with the absolute value

View File

@ -36,6 +36,7 @@ namespace etk {
* @brief No initialization constructor (faster ...)
*/
Vector3D() {
#if 0
#ifdef DEBUG
// in debug mode we set supid value to prevent forget of the inits ...
m_floats[0] = (T)34673363;
@ -49,6 +50,13 @@ namespace etk {
// hide a bullet warning
(void)btInfinityMask;
#endif
#else
m_floats[0] = 0;
m_floats[1] = 0;
m_floats[2] = 0;
m_floats[3] = 0;
#endif
}
/**
* @brief Constructor from scalars
@ -551,6 +559,24 @@ namespace etk {
int32_t getMaxAxis() const {
return (m_floats[0] < m_floats[1] ? (m_floats[1] < m_floats[2] ? 2 : 1) : (m_floats[0] < m_floats[2] ? 2 : 0));
}
/**
* @breif Get the orthogonalm vector of the current vector
* @return The ortho vector
*/
Vector3D<T> getOrthoVector() const {
Vector3D<T> vectorAbs(std::abs(m_floats[0]), std::abs(m_floats[1]), std::abs(m_floats[2]));
int32_t minElement = vectorAbs.getMinAxis();
if (minElement == 0) {
float devider = 1.0f / std::sqrt(m_floats[1]*m_floats[1] + m_floats[2]*m_floats[2]);
return Vector3D<T>(0.0f, -m_floats[2]*devider, m_floats[1]*devider);
} else if (minElement == 1) {
float devider = 1.0f / std::sqrt(m_floats[0]*m_floats[0] + m_floats[2]*m_floats[2]);
return Vector3D<T>(-m_floats[2]*devider, 0.0f, m_floats[0]*devider);
}
float devider = 1.0f / std::sqrt(m_floats[0]*m_floats[0] + m_floats[1]*m_floats[1]);
return Vector3D<T>(-m_floats[1]*devider, m_floats[0]*devider, 0.0f);
}
};
//! @not_in_doc
std::ostream& operator <<(std::ostream& _os, const etk::Vector3D<float>& _obj);