openmohaa/code/skeletor/SkelMat3.h

197 lines
4 KiB
C
Raw Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
2023-11-06 17:58:58 +01:00
Copyright (C) 2023 the OpenMoHAA team
2016-03-27 11:49:47 +02:00
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// SkelMat3.h : Skeletor
2023-11-06 17:58:58 +01:00
#pragma once
2016-03-27 11:49:47 +02:00
#ifdef __cplusplus
2023-11-06 17:58:58 +01:00
class SkelMat3
{
2016-03-27 11:49:47 +02:00
public:
2023-11-06 17:58:58 +01:00
float val[3][3];
2016-03-27 11:49:47 +02:00
protected:
2023-11-06 17:58:58 +01:00
void copy(const SkelMat3& m);
2016-03-27 11:49:47 +02:00
public:
2023-11-06 17:58:58 +01:00
void MakeZero();
void MakeIdentity();
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
SkelMat3(const SkelVec3& x, const SkelVec3& y, const SkelVec3& z);
SkelMat3(const float *mat[3]);
SkelMat3();
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
SkelVec3 *XAxis();
SkelVec3 *YAxis();
SkelVec3 *ZAxis();
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
operator float *();
operator float *() const;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
float *operator[](int index);
float *operator[](int index) const;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
void GetEulerAngles(float *vec) const;
void GetScale(float *vec) const;
bool IsOrthonormal() const;
bool IsValid() const;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
void Multiply(const float mat1[3][3], const float mat2[3][3]);
float det() const;
float trace() const;
SkelVec3 TransformVector(const SkelVec3 *skel);
void Transpose();
2016-03-27 11:49:47 +02:00
};
2023-11-06 17:58:58 +01:00
inline SkelMat3::SkelMat3(const SkelVec3& x, const SkelVec3& y, const SkelVec3& z)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
VectorCopy(x, val[0]);
VectorCopy(y, val[1]);
VectorCopy(z, val[2]);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelMat3::SkelMat3(const float *mat[3])
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
memcpy(&val, mat, sizeof(val));
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelMat3::SkelMat3()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
MakeIdentity();
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::copy(const SkelMat3& m)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
AxisCopy(m.val, val);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::MakeZero()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
AxisClear(val);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::MakeIdentity()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
MakeZero();
val[0][0] = 1.0f;
val[1][1] = 1.0f;
val[2][2] = 1.0f;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelVec3 *SkelMat3::XAxis()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (SkelVec3 *)&val[0];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelVec3 *SkelMat3::YAxis()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (SkelVec3 *)&val[1];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelVec3 *SkelMat3::ZAxis()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (SkelVec3 *)&val[2];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelMat3::operator float *()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return &val[0][0];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelMat3::operator float *() const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (float *)&val[0][0];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline float *SkelMat3::operator[](int index)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return val[index];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline float *SkelMat3::operator[](int index) const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (float *)val[index];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::GetEulerAngles(float *vec) const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
MatrixToEulerAngles(val, vec);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::GetScale(float *vec) const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
// FIXME: stub
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline bool SkelMat3::IsOrthonormal() const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
// FIXME: stub
return false;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline bool SkelMat3::IsValid() const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
// FIXME: stub
return false;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::Multiply(const float mat1[3][3], const float mat2[3][3])
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
MatrixMultiply(mat1, mat2, val);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline float SkelMat3::det() const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return (val[2][1] * val[1][0] - val[2][0] * val[1][1]) * val[0][2]
+ (val[1][1] * val[2][2] - val[1][2] * val[2][1]) * val[0][0]
- (val[2][2] * val[1][0] - val[1][2] * val[2][0]) * val[0][1];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline float SkelMat3::trace() const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return val[0][0] + val[1][1] + val[2][2];
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline SkelVec3 SkelMat3::TransformVector(const SkelVec3 *skel)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
SkelVec3 out;
MatrixTransformVector(*skel, val, out);
return out;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
inline void SkelMat3::Transpose()
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
SkelMat3 out;
TransposeMatrix(val, out.val);
memcpy(val, out, sizeof(SkelMat3));
2016-03-27 11:49:47 +02:00
}
#else
typedef struct {
2023-11-06 17:58:58 +01:00
float val[3][3];
2016-03-27 11:49:47 +02:00
} SkelMat3;
#endif