Added Quaternion spheric interpolation
Fixed NzVector(2/3)::Length() and NzQuaternion::Magnitude() returning double instead of template type Added quaternion dot product Added gitignore
This commit is contained in:
parent
e2a38b3790
commit
0f84f8eda8
|
|
@ -0,0 +1,151 @@
|
|||
# Codeblocks
|
||||
*.cbp
|
||||
*.cscope_file_list
|
||||
*.depend
|
||||
*.layout
|
||||
*.workspace
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug*/
|
||||
[Rr]elease/
|
||||
|
||||
build/
|
||||
|
||||
|
||||
[Tt]est[Rr]esult
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
|
||||
*.pidb
|
||||
|
||||
*.log
|
||||
*.scc
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
|
||||
*.[Rr]e[Ss]harper
|
||||
|
||||
# NCrunch
|
||||
*.ncrunch*
|
||||
.*crunch*.local.xml
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
|
||||
# Others
|
||||
[Bb]in
|
||||
[Oo]bj
|
||||
sql
|
||||
TestResults
|
||||
[Tt]est[Rr]esult*
|
||||
*.Cache
|
||||
ClientBin
|
||||
[Ss]tyle[Cc]op.*
|
||||
~$*
|
||||
*.dbmdl
|
||||
|
||||
*.[Pp]ublish.xml
|
||||
|
||||
Generated_Code #added for RIA/Silverlight projects
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
|
||||
# NuGet
|
||||
packages/
|
||||
|
|
@ -26,11 +26,13 @@ template<typename T> class NzQuaternion
|
|||
NzQuaternion(const NzQuaternion& quat) = default;
|
||||
~NzQuaternion() = default;
|
||||
|
||||
T DotProduct(const NzQuaternion& vec) const;
|
||||
|
||||
NzQuaternion GetConjugate() const;
|
||||
NzQuaternion GetNormalized() const;
|
||||
|
||||
double Magnitude() const;
|
||||
double Normalize();
|
||||
T Magnitude() const;
|
||||
T Normalize();
|
||||
T SquaredMagnitude() const;
|
||||
|
||||
void Set(T W, T X, T Y, T Z);
|
||||
|
|
@ -47,14 +49,16 @@ template<typename T> class NzQuaternion
|
|||
//NzMatrix3<T> ToRotationMatrix() const;
|
||||
NzString ToString() const;
|
||||
|
||||
NzQuaternion operator+(const NzQuaternion& quat) const;
|
||||
NzQuaternion operator*(const NzQuaternion& quat) const;
|
||||
NzVector3<T> operator*(const NzVector3<T>& vec) const;
|
||||
NzQuaternion operator*(T scale) const;
|
||||
NzQuaternion operator/(const NzQuaternion& quat) const;
|
||||
|
||||
NzQuaternion operator*=(const NzQuaternion& quat);
|
||||
NzQuaternion operator*=(T scale);
|
||||
NzQuaternion operator/=(const NzQuaternion& quat);
|
||||
NzQuaternion& operator+=(const NzQuaternion& quat);
|
||||
NzQuaternion& operator*=(const NzQuaternion& quat);
|
||||
NzQuaternion& operator*=(T scale);
|
||||
NzQuaternion& operator/=(const NzQuaternion& quat);
|
||||
|
||||
bool operator==(const NzQuaternion& quat) const;
|
||||
bool operator!=(const NzQuaternion& quat) const;
|
||||
|
|
@ -63,6 +67,8 @@ template<typename T> class NzQuaternion
|
|||
bool operator>(const NzQuaternion& quat) const;
|
||||
bool operator>=(const NzQuaternion& quat) const;
|
||||
|
||||
static NzQuaternion Slerp(const NzQuaternion& quatA, const NzQuaternion& quatB, T interp);
|
||||
|
||||
T w, x, y, z;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -52,6 +52,12 @@ NzQuaternion<T>::NzQuaternion(const NzQuaternion<U>& quat)
|
|||
Set(quat);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T NzQuaternion<T>::DotProduct(const NzQuaternion& vec) const
|
||||
{
|
||||
return w*vec.w + x*vec.x + y*vec.y + z.vec.z;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::GetConjugate() const
|
||||
{
|
||||
|
|
@ -68,19 +74,19 @@ NzQuaternion<T> NzQuaternion<T>::GetNormalized() const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
double NzQuaternion<T>::Magnitude() const
|
||||
T NzQuaternion<T>::Magnitude() const
|
||||
{
|
||||
return std::sqrt(SquaredMagnitude());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
double NzQuaternion<T>::Normalize()
|
||||
T NzQuaternion<T>::Normalize()
|
||||
{
|
||||
T squaredLength = SquaredMagnitude();
|
||||
|
||||
if (std::fabs(squaredLength) > 0.00001 && std::fabs(squaredLength - 1.0) > 0.00001)
|
||||
{
|
||||
double length = std::sqrt(squaredLength);
|
||||
T length = std::sqrt(squaredLength);
|
||||
|
||||
w /= length;
|
||||
x /= length;
|
||||
|
|
@ -90,7 +96,7 @@ double NzQuaternion<T>::Normalize()
|
|||
return length;
|
||||
}
|
||||
else
|
||||
return std::sqrt(squaredLength);
|
||||
return 1.0; // Le quaternion est déjà normalisé
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -106,8 +112,6 @@ void NzQuaternion<T>::Set(T W, T X, T Y, T Z)
|
|||
x = X;
|
||||
y = Y;
|
||||
z = Z;
|
||||
|
||||
Normalize();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -117,8 +121,6 @@ void NzQuaternion<T>::Set(T quat[4])
|
|||
x = quat[1];
|
||||
y = quat[2];
|
||||
z = quat[3];
|
||||
|
||||
Normalize();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -175,6 +177,52 @@ void NzQuaternion<T>::SetZero()
|
|||
Set(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::Slerp(const NzQuaternion& quatA, const NzQuaternion& quatB, T interp)
|
||||
{
|
||||
if (interp <= 0.0)
|
||||
return quatA;
|
||||
|
||||
if (interp >= 1.0)
|
||||
return quatB;
|
||||
|
||||
NzQuaternion q;
|
||||
|
||||
T cosOmega = quatA.DotProduct(quatB);
|
||||
if (cosOmega < 0.0)
|
||||
{
|
||||
// On inverse tout
|
||||
q.Set(-quatB.w, -quatB.x, -quatB.y, -quatB.z);
|
||||
cosOmega = -cosOmega;
|
||||
}
|
||||
else
|
||||
q.Set(quatB);
|
||||
|
||||
T k0, k1;
|
||||
if (cosOmega > 0.9999)
|
||||
{
|
||||
// Interpolation linéaire pour éviter une division par zéro
|
||||
k0 = 1.0 - interp;
|
||||
k1 = interp;
|
||||
}
|
||||
else
|
||||
{
|
||||
T sinOmega = std::sqrt(1.0f - (cosOmega * cosOmega));
|
||||
T omega = std::atan2(sinOmega, cosOmega);
|
||||
|
||||
// Pour éviter deux divisions
|
||||
sinOmega = 1/sinOmega;
|
||||
|
||||
k0 = std::sin((1.0 - interp) * omega) * sinOmega;
|
||||
k1 = std::sin(interp * omega) * sinOmega;
|
||||
}
|
||||
|
||||
/* interpolate and return new quaternion */
|
||||
NzQuaternion result(k0 * quatA.w, k0 * quatA.x, k0 * quatA.y, k0 * quatA.z);
|
||||
|
||||
return result += q;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzEulerAngles<T> NzQuaternion<T>::ToEulerAngles() const
|
||||
{
|
||||
|
|
@ -203,17 +251,22 @@ NzString NzQuaternion<T>::ToString() const
|
|||
return ss << "Quaternion(" << w << " | " << x << ", " << y << ", " << z << ')';
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::operator+(const NzQuaternion& quat) const
|
||||
{
|
||||
return NzQuaternion(w + quat.w,
|
||||
x + quat.x,
|
||||
y + quat.y,
|
||||
z + quat.z);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::operator*(const NzQuaternion& quat) const
|
||||
{
|
||||
NzQuaternion result(w * quat.w - x * quat.x - y * quat.y - z * quat.z,
|
||||
return NzQuaternion(w * quat.w - x * quat.x - y * quat.y - z * quat.z,
|
||||
w * quat.x + x * quat.w + y * quat.z - z * quat.y,
|
||||
w * quat.y + y * quat.w + z * quat.x - x * quat.z,
|
||||
w * quat.z + z * quat.w + x * quat.y - y * quat.x);
|
||||
|
||||
result.Normalize();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -223,9 +276,7 @@ NzVector3<T> NzQuaternion<T>::operator*(const NzVector3<T>& vec) const
|
|||
normal.Normalise();
|
||||
|
||||
NzQuaternion qvec(0.0, normal.x, normal.y, normal.z);
|
||||
NzQuaternion result;
|
||||
|
||||
result = operator*(qvec * GetConjugate());
|
||||
NzQuaternion result = operator*(qvec * GetConjugate());
|
||||
|
||||
return NzVector3<T>(result.x, result.y, result.z);
|
||||
|
||||
|
|
@ -247,30 +298,27 @@ NzQuaternion<T> NzQuaternion<T>::operator/(const NzQuaternion& quat) const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::operator*=(const NzQuaternion& quat)
|
||||
NzQuaternion<T>& NzQuaternion<T>::operator+=(const NzQuaternion& quat)
|
||||
{
|
||||
NzQuaternion q(*this);
|
||||
|
||||
return operator=(q * quat);
|
||||
return operator=(operator+(quat));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::operator*=(T scale)
|
||||
NzQuaternion<T>& NzQuaternion<T>::operator*=(const NzQuaternion& quat)
|
||||
{
|
||||
w *= scale;
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
z *= scale;
|
||||
|
||||
return *this;
|
||||
return operator=(operator*(quat));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzQuaternion<T> NzQuaternion<T>::operator/=(const NzQuaternion& quat)
|
||||
NzQuaternion<T>& NzQuaternion<T>::operator*=(T scale)
|
||||
{
|
||||
NzQuaternion q(*this);
|
||||
return operator=(operator*(scale));
|
||||
}
|
||||
|
||||
return operator=(q / quat);
|
||||
template<typename T>
|
||||
NzQuaternion<T>& NzQuaternion<T>::operator/=(const NzQuaternion& quat)
|
||||
{
|
||||
return operator=(operator/(quat));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ template<typename T> class NzVector2
|
|||
~NzVector2() = default;
|
||||
|
||||
T AbsDotProduct(const NzVector2& vec) const;
|
||||
double Distance(const NzVector2& vec) const;
|
||||
T Distance(const NzVector2& vec) const;
|
||||
T DotProduct(const NzVector2& vec) const;
|
||||
NzVector2 GetNormal() const;
|
||||
void MakeCeil(const NzVector2& vec);
|
||||
void MakeFloor(const NzVector2& vec);
|
||||
double Length() const;
|
||||
double Normalize();
|
||||
T Length() const;
|
||||
T Normalize();
|
||||
T SquaredDistance(const NzVector2& vec) const;
|
||||
T SquaredLength() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ template<> inline int NzVector2<int>::AbsDotProduct(const NzVector2<int>& vec) c
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector2<T>::Distance(const NzVector2& vec) const
|
||||
T NzVector2<T>::Distance(const NzVector2& vec) const
|
||||
{
|
||||
return std::sqrt(SquaredDistance(vec));
|
||||
}
|
||||
|
|
@ -96,15 +96,15 @@ void NzVector2<T>::MakeFloor(const NzVector2& vec)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector2<T>::Length() const
|
||||
T NzVector2<T>::Length() const
|
||||
{
|
||||
return std::sqrt(SquaredLength());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector2<T>::Normalize()
|
||||
T NzVector2<T>::Normalize()
|
||||
{
|
||||
double length = Length();
|
||||
T length = Length();
|
||||
|
||||
if (length != 0.f)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@ template<typename T> class NzVector3
|
|||
|
||||
T AbsDotProduct(const NzVector3& vec) const;
|
||||
NzVector3 CrossProduct(const NzVector3& vec) const;
|
||||
double Distance(const NzVector3& vec) const;
|
||||
T Distance(const NzVector3& vec) const;
|
||||
T DotProduct(const NzVector3& vec) const;
|
||||
NzVector3 GetNormal() const;
|
||||
void MakeCeil(const NzVector3& vec);
|
||||
void MakeFloor(const NzVector3& vec);
|
||||
double Length() const;
|
||||
double Normalize();
|
||||
T Length() const;
|
||||
T Normalize();
|
||||
T SquaredDistance(const NzVector3& vec) const;
|
||||
T SquaredLength() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ NzVector3<T> NzVector3<T>::CrossProduct(const NzVector3& vec) const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector3<T>::Distance(const NzVector3& vec) const
|
||||
T NzVector3<T>::Distance(const NzVector3& vec) const
|
||||
{
|
||||
return std::sqrt(SquaredDistance(vec));
|
||||
}
|
||||
|
|
@ -112,15 +112,15 @@ void NzVector3<T>::MakeFloor(const NzVector3& vec)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector3<T>::Length() const
|
||||
T NzVector3<T>::Length() const
|
||||
{
|
||||
return std::sqrt(SquaredLength());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
double NzVector3<T>::Normalize()
|
||||
T NzVector3<T>::Normalize()
|
||||
{
|
||||
double length = Length();
|
||||
T length = Length();
|
||||
|
||||
if (length != 0.f)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue