Improved Transform class.

This commit is contained in:
hyzboy 2024-08-04 22:29:46 +08:00
parent f44142486d
commit 8e471259d2
2 changed files with 241 additions and 155 deletions

View File

@ -9,20 +9,28 @@ namespace hgl
*/ */
class Transform class Transform
{ {
protected: uint32 version; ///<版本号用于记录任何变化而不是matrix
uint32 version; void UpdateVersion()
{
if(version>=0x70000000)
version=0;
else
++version;
}
protected:
bool is_identity; bool is_identity;
bool is_zero_rotate; bool is_zero_rotate;
bool matrix_dirty; bool matrix_dirty;
//matrix其实用的少所以在不取matrix时并不会计算。
Matrix4f matrix; Matrix4f matrix;
Matrix4f inverse_matrix; Matrix4f inverse_matrix;
Matrix4f transpose_inverse_matrix; Matrix4f transpose_inverse_matrix;
Vector3f translation_vector; Vector3f translation_vector;
Quatf rotation_quat; Quatf rotation_quat;
Vector3f rotation_axis; Vector3f rotation_axis;
float rotate_angle; float rotate_angle;
@ -31,79 +39,15 @@ namespace hgl
protected: protected:
void UpdateQuat() void UpdateQuat();
{
if(IsNearlyZero(rotate_angle))
{
if(is_zero_rotate)
return;
is_zero_rotate=true;
rotation_quat=IdentityQuatf;
return;
}
else
{
is_zero_rotate=false;
rotation_quat=RotationQuat(rotate_angle,rotation_axis);
}
matrix_dirty=true;
}
public: public:
void UpdateMatrix() void UpdateMatrix();
{
if(!matrix_dirty)
return;
if(translation_vector==ZeroVector3f const uint32 GetVersion (){UpdateMatrix();return version;}
&&rotation_quat==IdentityQuatf const bool IsIdentity (){UpdateMatrix();return is_identity;}
&&scale_vector==OneVector3f) const Matrix4f &GetMatrix (){UpdateMatrix();return matrix;}
{
if(is_identity) //如果之前就是空的,那不更新版本号
{
matrix_dirty=false;
return;
}
SetToIdentity();
}
else
{
matrix=translate(translation_vector)*ToMatrix(rotation_quat)*scale(scale_vector);
inverse_matrix=inverse(matrix);
transpose_inverse_matrix=transpose(inverse_matrix);
is_identity=false;
}
matrix_dirty=false;
if(version>0xF0000000)
version=0;
else
++version;
}
const uint32 GetVersion()
{
UpdateMatrix();
return version;
}
const bool IsIdentity()
{
UpdateMatrix();
return is_identity;
}
const Matrix4f &GetMatrix()
{
UpdateMatrix();
return matrix;
}
const Matrix4f GetMatrix()const //不能执行UpdateMatrix时的获取 const Matrix4f GetMatrix()const //不能执行UpdateMatrix时的获取
{ {
@ -113,7 +57,8 @@ namespace hgl
return matrix; return matrix;
} }
operator const Matrix4f &(){return GetMatrix();} operator const Matrix4f & (){return GetMatrix();}
operator const Transform & (){UpdateMatrix();return *this;}
const Matrix4f &GetInverseMatrix() const Matrix4f &GetInverseMatrix()
{ {
@ -121,6 +66,7 @@ namespace hgl
return inverse_matrix; return inverse_matrix;
} }
const Vector3f &GetTranslation ()const{return translation_vector;} const Vector3f &GetTranslation ()const{return translation_vector;}
const Vector3f &GetScale ()const{return scale_vector;} const Vector3f &GetScale ()const{return scale_vector;}
@ -130,6 +76,16 @@ namespace hgl
void SetTranslation(const float x,const float y,const float z) void SetTranslation(const float x,const float y,const float z)
{ {
if(is_identity)
{
if(IsNearlyZero(x)
&&IsNearlyZero(y)
&&IsNearlyZero(z))
return;
}
is_identity=false;
translation_vector.x=x; translation_vector.x=x;
translation_vector.y=y; translation_vector.y=y;
translation_vector.z=z; translation_vector.z=z;
@ -138,6 +94,16 @@ namespace hgl
void SetTranslation(const Vector3f &v) void SetTranslation(const Vector3f &v)
{ {
if(is_identity)
{
if(IsNearlyZero(v.x)
&&IsNearlyZero(v.y)
&&IsNearlyZero(v.z))
return;
}
is_identity=false;
translation_vector=v; translation_vector=v;
matrix_dirty=true; matrix_dirty=true;
} }
@ -152,6 +118,14 @@ namespace hgl
void SetRotation(const Quatf &q) void SetRotation(const Quatf &q)
{ {
if(is_identity)
{
if(q==IdentityQuatf)
return;
}
is_identity=false;
rotation_quat=q; rotation_quat=q;
ExtractedQuat(q,rotation_axis,rotate_angle); ExtractedQuat(q,rotation_axis,rotate_angle);
matrix_dirty=true; matrix_dirty=true;
@ -159,6 +133,14 @@ namespace hgl
void SetRotation(const Vector3f &axis,const float angle) void SetRotation(const Vector3f &axis,const float angle)
{ {
if(is_identity)
{
if(IsNearlyZero(angle)==0)
return;
}
is_identity=false;
rotation_axis=axis; rotation_axis=axis;
rotate_angle=angle; rotate_angle=angle;
UpdateQuat(); UpdateQuat();
@ -166,6 +148,14 @@ namespace hgl
void SetRotation(const AXIS &axis,const float angle) void SetRotation(const AXIS &axis,const float angle)
{ {
if(is_identity)
{
if(IsNearlyZero(angle)==0)
return;
}
is_identity=false;
rotation_axis=GetAxisVector(axis); rotation_axis=GetAxisVector(axis);
rotate_angle=angle; rotate_angle=angle;
UpdateQuat(); UpdateQuat();
@ -173,18 +163,43 @@ namespace hgl
void SetRotateAngle(float angle) void SetRotateAngle(float angle)
{ {
if(is_identity)
{
if(IsNearlyZero(angle)==0)
return;
}
is_identity=false;
rotate_angle=angle; rotate_angle=angle;
UpdateQuat(); UpdateQuat();
} }
void SetScale(const float &v) void SetScale(const float &v)
{ {
if(is_identity)
{
if(IsNearlyEqual(v,1.0f))
return;
}
is_identity=false;
scale_vector=Vector3f(v,v,v); scale_vector=Vector3f(v,v,v);
matrix_dirty=true; matrix_dirty=true;
} }
void SetScale(const float x,const float y,const float z) void SetScale(const float x,const float y,const float z)
{ {
if(is_identity)
{
if(IsNearlyEqual(x,1.0f)
&&IsNearlyEqual(y,1.0f)
&&IsNearlyEqual(z,1.0f))
return;
}
is_identity=false;
scale_vector.x=x; scale_vector.x=x;
scale_vector.y=y; scale_vector.y=y;
scale_vector.z=z; scale_vector.z=z;
@ -194,6 +209,16 @@ namespace hgl
void SetScale(const Vector3f &v) void SetScale(const Vector3f &v)
{ {
if(is_identity)
{
if(IsNearlyEqual(v.x,1.0f)
&&IsNearlyEqual(v.y,1.0f)
&&IsNearlyEqual(v.z,1.0f))
return;
}
is_identity=false;
scale_vector=v; scale_vector=v;
matrix_dirty=true; matrix_dirty=true;
} }
@ -203,113 +228,55 @@ namespace hgl
Transform() Transform()
{ {
SetToIdentity(); SetToIdentity();
version=0;
} }
Transform(const Matrix4f &m) Transform(const Matrix4f &m)
{ {
SetFromMatrix4f(m); SetFromMatrix4f(m);
version=0;
} }
Transform(const Transform &t) Transform(const Transform &t)
{ {
hgl_cpy(*this,t); hgl_cpy(*this,t);
version=0;
} }
void SetToIdentity() void SetToIdentity();
{
is_identity=true;
is_zero_rotate=true;
matrix=Identity4f; const bool operator == (const Transform &t);
inverse_matrix=Identity4f;
matrix_dirty=false;
translation_vector=ZeroVector3f;
rotation_quat=IdentityQuatf;
rotation_axis=ZeroVector3f;
rotate_angle=0;
scale_vector=OneVector3f;
}
const bool operator == (const Transform &t)
{
UpdateMatrix();
if(is_identity)
{
if(t.is_identity)
return(true);
else
return(false);
}
else
{
if(t.is_identity)
return(false);
}
const Matrix4f tm=t.GetMatrix();
return matrix==tm;
}
void operator = (const Transform &t) void operator = (const Transform &t)
{ {
if(operator==(t))
return;
uint32 old_version=version;
hgl_cpy(*this,t); hgl_cpy(*this,t);
version=++old_version;
UpdateMatrix(); UpdateMatrix();
} }
void SetFromMatrix4f(const Matrix4f &m) void SetFromMatrix4f(const Matrix4f &m);
{
is_identity=IsIdentityMatrix(m);
if(is_identity) const bool IsIdentity()const{return is_identity;}
{
SetToIdentity();
return;
}
matrix=m; const bool IsLastVersion()const{return !matrix_dirty;}
inverse_matrix=inverse(m);
matrix_dirty=false; inline Vector3f TransformPosition (const Vector3f &v){UpdateMatrix();return Vector3f(matrix*Vector4f(v,1.0f));}
inline Vector3f TransformDirection (const Vector3f &v){UpdateMatrix();return Vector3f(matrix*Vector4f(v,0.0f));}
inline Vector3f TransformNormal (const Vector3f &v){UpdateMatrix();return normalize(Vector3f(transpose_inverse_matrix*Vector4f(v,0.0f)));}
inline Matrix3f TransformMatrix (const Matrix3f &child){UpdateMatrix();return Matrix3f(matrix*Matrix4f(child));}
inline Matrix4f TransformMatrix (const Matrix4f &child){UpdateMatrix();return matrix*child;}
DecomposeTransform(m,translation_vector,rotation_quat,scale_vector); inline Vector3f InverseTransformPosition (const Vector3f &v){UpdateMatrix();return Vector3f(inverse_matrix*Vector4f(v,1.0f));}
inline Vector3f InverseTransformDirection (const Vector3f &v){UpdateMatrix();return Vector3f(inverse_matrix*Vector4f(v,0.0f));}
ExtractedQuat(rotation_quat,rotation_axis,rotate_angle); inline Vector3f InverseTransformNormal (const Vector3f &v){UpdateMatrix();return normalize(Vector3f(transpose_inverse_matrix*Vector4f(v,0.0f)));}
} inline Matrix3f InverseTransformMatrix (const Matrix3f &child){UpdateMatrix();return Matrix3f(inverse_matrix*Matrix4f(child));}
inline Matrix4f InverseTransformMatrix (const Matrix4f &child){UpdateMatrix();return inverse_matrix*child;}
inline Vector3f TransformPosition(const Vector3f &v)
{
UpdateMatrix();
return Vector3f(matrix*Vector4f(v,1.0f));
}
inline Vector3f TransformDirection(const Vector3f &v)
{
UpdateMatrix();
return Vector3f(matrix*Vector4f(v,0.0f));
}
inline Vector3f TransformNormal(const Vector3f &v)
{
UpdateMatrix();
return normalize(Vector3f(transpose_inverse_matrix*Vector4f(v,0.0f)));
}
inline Matrix3f TransformMatrix(const Matrix3f &child)
{
UpdateMatrix();
return Matrix3f(matrix*Matrix4f(child));
}
inline Matrix4f TransformMatrix(const Matrix4f &child)
{
UpdateMatrix();
return matrix*child;
}
inline Transform TransformTransform(const Transform &child) inline Transform TransformTransform(const Transform &child)
{ {
@ -328,6 +295,8 @@ namespace hgl
} }
};//Transform };//Transform
const Transform IdentityTransform;
constexpr const size_t TransformMatrix4fLength=sizeof(Transform); constexpr const size_t TransformMatrix4fLength=sizeof(Transform);
Transform Lerp(const Transform &from,const Transform &to,const float t); Transform Lerp(const Transform &from,const Transform &to,const float t);

View File

@ -12,4 +12,121 @@ namespace hgl
return result; return result;
} }
void Transform::UpdateQuat()
{
if(IsNearlyZero(rotate_angle))
{
if(is_zero_rotate)
return;
is_zero_rotate=true;
rotation_quat=IdentityQuatf;
return;
}
else
{
is_zero_rotate=false;
rotation_quat=RotationQuat(rotate_angle,rotation_axis);
}
matrix_dirty=true;
}
void Transform::UpdateMatrix()
{
if(!matrix_dirty)
return;
if(translation_vector==ZeroVector3f
&&rotation_quat==IdentityQuatf
&&scale_vector==OneVector3f)
{
if(is_identity) //如果之前就是空的,那不更新版本号
{
matrix_dirty=false;
return;
}
SetToIdentity();
}
else
{
matrix=translate(translation_vector)*ToMatrix(rotation_quat)*scale(scale_vector);
inverse_matrix=inverse(matrix);
transpose_inverse_matrix=transpose(inverse_matrix);
is_identity=false;
}
matrix_dirty=false;
UpdateVersion();
}
void Transform::SetToIdentity()
{
is_identity=true;
is_zero_rotate=true;
matrix=Identity4f;
inverse_matrix=Identity4f;
matrix_dirty=false;
translation_vector=ZeroVector3f;
rotation_quat=IdentityQuatf;
rotation_axis=ZeroVector3f;
rotate_angle=0;
scale_vector=OneVector3f;
}
const bool Transform::operator == (const Transform &t)
{
if(is_identity)
{
if(t.is_identity)
return(true);
else
return(false);
}
else
{
if(t.is_identity)
return(false);
}
if(translation_vector!=t.translation_vector)return(false);
if(rotation_quat!=t.rotation_quat)return(false);
if(scale_vector!=t.scale_vector)return(false);
return(true);
}
void Transform::SetFromMatrix4f(const Matrix4f &m)
{
if(is_identity)
{
if(IsIdentityMatrix(m))
return;
}
is_identity=IsIdentityMatrix(m);
if(is_identity)
{
SetToIdentity();
UpdateVersion();
return;
}
matrix=m;
inverse_matrix=inverse(m);
matrix_dirty=false;
DecomposeTransform(m,translation_vector,rotation_quat,scale_vector);
ExtractedQuat(rotation_quat,rotation_axis,rotate_angle);
}
}//namespace hgl }//namespace hgl