Commit 249c7531 authored by Eric Bruneton's avatar Eric Bruneton
Browse files

patch #13238 from Clement Menier

We have added a few operations to ork math objects:
* for all vec{2,3,4} and mat{2,3,4} : possibility to left multiply by a scalar (even if the scalar is not exactly of
the same type than the template of the object)
* for mat{2,3,4}: clarify between 2 operators [] for read-only (const) and [] for read-write.
* for quat:
  -> add a constructor from a rotation matrix
  -> add a function getAxis and a function getAngle.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/ork/trunk@27 28599a00-4e59-401b-b2d8-d34d4661a6c9
parent e0c8c9a5
......@@ -86,7 +86,12 @@ public:
/**
* Returns the row of this matrix whose index is given.
*/
type* operator[](int iRow) const;
const type* operator[](int iRow) const;
/**
* Returns the row of this matrix whose index is given (read-write).
*/
type* operator[](int iRow);
/**
* Returns the column of this matrix whose index is given.
......@@ -236,9 +241,15 @@ inline const type* mat2<type>::coefficients() const
}
template <typename type>
inline type* mat2<type>::operator[](int iRow) const
inline const type* mat2<type>::operator[](int iRow) const
{
return m[iRow];
}
template <typename type>
inline type* mat2<type>::operator[](int iRow)
{
return (type*)m[iRow];
return m[iRow];
}
template <typename type>
......@@ -412,4 +423,13 @@ const mat2<type> mat2<type>::IDENTITY(1, 0,
}
/**
* Returns the product of this matrix and of the given scalar.
*/
template <typename scalarType, typename matType>
inline ork::mat2<matType> operator*(const scalarType scalar, const ork::mat2<matType> &m)
{
return m * static_cast<matType>(scalar);
}
#endif
......@@ -92,7 +92,12 @@ public:
/**
* Returns the row of this matrix whose index is given.
*/
type* operator[](int iRow) const;
const type* operator[](int iRow) const;
/**
* Returns the row of this matrix whose index is given (read-write).
*/
type* operator[](int iRow);
/**
* Returns the column of this matrix whose index is given.
......@@ -274,9 +279,15 @@ inline const type* mat3<type>::coefficients() const
}
template <typename type>
inline type* mat3<type>::operator[](int iRow) const
inline const type* mat3<type>::operator[](int iRow) const
{
return m[iRow];
}
template <typename type>
inline type* mat3<type>::operator[](int iRow)
{
return (type*)m[iRow];
return m[iRow];
}
template <typename type>
......@@ -507,4 +518,13 @@ const mat3<type> mat3<type>::IDENTITY(1, 0, 0,
}
/**
* Returns the product of this matrix and of the given scalar.
*/
template <typename scalarType, typename matType>
inline ork::mat3<matType> operator*(const scalarType scalar, const ork::mat3<matType> &m)
{
return m * static_cast<matType>(scalar);
}
#endif
......@@ -99,6 +99,11 @@ public:
*/
const type* operator[](int iRow) const;
/**
* Returns the row of this matrix whose index is given (read-write).
*/
type *operator[](int iRow);
/**
* Assigns the given matrix to this matrix.
*/
......@@ -328,6 +333,11 @@ inline const type* mat4<type>::operator[](int iRow) const
return m[iRow];
}
template <typename type>
inline type* mat4<type>::operator[](int iRow)
{
return m[iRow];
}
template <typename type>
inline void mat4<type>::operator=(const mat3<type>& m3)
......@@ -666,4 +676,13 @@ const mat4<type> mat4<type>::IDENTITY(
}
/**
* Returns the product of this matrix and of the given scalar.
*/
template <typename scalarType, typename matType>
inline ork::mat4<matType> operator*(const scalarType scalar, const ork::mat4<matType> &m)
{
return m * static_cast<matType>(scalar);
}
#endif
......
......@@ -94,6 +94,11 @@ public:
*/
quat(const vec3<type>& to, const vec3<type>& from);
/**
* Create a normalized quaternion from a 3x3 rotation matrix.
*/
explicit quat(const mat3<type> &m);
/**
* Multiply by another quaternion (replace a 3x3 rotation matrix multiplication).
*/
......@@ -134,6 +139,16 @@ public:
*/
mat4<type> toMat4() const;
/**
* Returns the axis.
*/
vec3<type> getAxis() const;
/**
* Returns the angle.
*/
type getAngle() const;
static const quat ONE;
};
......@@ -222,6 +237,49 @@ quat<type>::quat(const vec3<type>& to, const vec3<type>& from)
}
}
template <typename type>
quat<type>::quat(const mat3<type> &m)
{
type tr = m[0][0] + m[1][1] + m[2][2] + 1;
if (tr > 0) {
type s = 0.5 / sqrt(tr);
w = 0.25 / s;
x = (m[2][1] - m[1][2] ) * s;
y = (m[0][2] - m[2][0] ) * s;
z = (m[1][0] - m[0][1] ) * s;
} else {
if (m[1][1] > m[0][0] && m[2][2] <= m[1][1] ) {
type s = sqrt((m[1][1] - (m[2][2] +m[0][0] )) + 1.0);
y = s * 0.5;
if (s != 0.0) {
s = 0.5 / s;
}
z = (m[1][2] +m[2][1] ) * s;
x = (m[0][1] +m[1][0] ) * s;
w = (m[0][2] -m[2][0] ) * s;
} else if ((m[1][1] <= m[0][0] && m[2][2] > m[0][0] ) || (m[2][2] > m[1][1] )) {
type s = sqrt((m[2][2] - (m[0][0] + m[1][1] )) + 1);
z = s * 0.5;
if (s != 0.0) {
s = 0.5 / s;
}
x = (m[2][0] + m[0][2] ) * s;
y = (m[1][2] + m[2][1] ) * s;
z = (m[1][0] - m[0][1] ) * s;
} else {
type s = sqrt((m[0][0] - (m[1][1] + m[2][2] )) + 1);
x = s * 0.5;
if (s != 0.0) {
s = 0.5 / s;
}
y = (m[0][1] + m[1][0] ) * s;
z = (m[2][0] + m[0][2] ) * s;
w = (m[2][1] - m[1][2] ) * s;
}
}
}
// operations
template <typename type>
......@@ -293,6 +351,18 @@ mat4<type> quat<type>::toMat4() const
return mat4<type>(toMat3());
}
template <typename type>
vec3<type> quat<type>::getAxis() const
{
return vec3<type>(x,y,z).normalize();
}
template <typename type>
type quat<type>::getAngle() const
{
return 2 * acos(w / length());
}
template <typename type>
const quat<type> quat<type>::ONE(0, 0, 0, 1);
......
......@@ -459,6 +459,15 @@ const vec2<type> vec2<type>::UNIT_Y(0, 1);
}
/**
* Returns the product of this vector and of the given scalar.
*/
template <typename scalarType, typename vecType>
inline ork::vec2<vecType> operator*(const scalarType scalar, const ork::vec2<vecType> &v)
{
return v * static_cast<vecType>(scalar);
}
#undef PREDICATE_BOOL_FUNCTION
#endif
......@@ -491,4 +491,13 @@ const vec3<type> vec3<type>::UNIT_Z(0, 0, 1);
}
/**
* Returns the product of this vector and of the given scalar.
*/
template <typename scalarType, typename vecType>
inline ork::vec3<vecType> operator*(const scalarType scalar, const ork::vec3<vecType> &v)
{
return v * static_cast<vecType>(scalar);
}
#endif
......@@ -487,4 +487,13 @@ const vec4<type> vec4<type>::UNIT_W(0, 0, 0, 1);
}
/**
* Returns the product of this vector and of the given scalar.
*/
template <typename scalarType, typename vecType>
inline ork::vec4<vecType> operator*(const scalarType scalar, const ork::vec4<vecType> &v)
{
return v * static_cast<vecType>(scalar);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment