Performance optimizations for math classes
Implemented KRQuaternion::FromAngleAxis
This commit is contained in:
@@ -168,7 +168,8 @@ KRQuaternion KRQuaternion::operator *(float v) const {
|
||||
}
|
||||
|
||||
KRQuaternion KRQuaternion::operator /(float num) const {
|
||||
return KRQuaternion(m_val[0] / num, m_val[1] / num, m_val[2] / num, m_val[3] / num);
|
||||
float inv_num = 1.0f / num;
|
||||
return KRQuaternion(m_val[0] * inv_num, m_val[1] * inv_num, m_val[2] * inv_num, m_val[3] * inv_num);
|
||||
}
|
||||
|
||||
KRQuaternion KRQuaternion::operator +(const KRQuaternion &v) const {
|
||||
@@ -224,10 +225,11 @@ KRQuaternion& KRQuaternion::operator *=(const float& v) {
|
||||
}
|
||||
|
||||
KRQuaternion& KRQuaternion::operator /=(const float& v) {
|
||||
m_val[0] /= v;
|
||||
m_val[1] /= v;
|
||||
m_val[2] /= v;
|
||||
m_val[3] /= v;
|
||||
float inv_v = 1.0f / v;
|
||||
m_val[0] *= inv_v;
|
||||
m_val[1] *= inv_v;
|
||||
m_val[2] *= inv_v;
|
||||
m_val[3] *= inv_v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -240,21 +242,21 @@ KRQuaternion KRQuaternion::operator -() const {
|
||||
}
|
||||
|
||||
KRQuaternion Normalize(const KRQuaternion &v1) {
|
||||
float magnitude = sqrtf(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] + v1[3] * v1[3]);
|
||||
float inv_magnitude = 1.0f / sqrtf(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] + v1[3] * v1[3]);
|
||||
return KRQuaternion(
|
||||
v1[0] / magnitude,
|
||||
v1[1] / magnitude,
|
||||
v1[2] / magnitude,
|
||||
v1[3] / magnitude
|
||||
v1[0] * inv_magnitude,
|
||||
v1[1] * inv_magnitude,
|
||||
v1[2] * inv_magnitude,
|
||||
v1[3] * inv_magnitude
|
||||
);
|
||||
}
|
||||
|
||||
void KRQuaternion::normalize() {
|
||||
float magnitude = sqrtf(m_val[0] * m_val[0] + m_val[1] * m_val[1] + m_val[2] * m_val[2] + m_val[3] * m_val[3]);
|
||||
m_val[0] /= magnitude;
|
||||
m_val[1] /= magnitude;
|
||||
m_val[2] /= magnitude;
|
||||
m_val[3] /= magnitude;
|
||||
float inv_magnitude = 1.0f / sqrtf(m_val[0] * m_val[0] + m_val[1] * m_val[1] + m_val[2] * m_val[2] + m_val[3] * m_val[3]);
|
||||
m_val[0] *= inv_magnitude;
|
||||
m_val[1] *= inv_magnitude;
|
||||
m_val[2] *= inv_magnitude;
|
||||
m_val[3] *= inv_magnitude;
|
||||
}
|
||||
|
||||
KRQuaternion Conjugate(const KRQuaternion &v1) {
|
||||
@@ -285,3 +287,10 @@ KRMat4 KRQuaternion::rotationMatrix() const {
|
||||
return matRotate;
|
||||
}
|
||||
|
||||
|
||||
KRQuaternion KRQuaternion::FromAngleAxis(const KRVector3 &axis, float angle)
|
||||
{
|
||||
float ha = angle * 0.5f;
|
||||
float sha = sin(ha);
|
||||
return KRQuaternion(cos(ha), axis.x * sha, axis.y * sha, axis.z * sha);
|
||||
}
|
||||
|
||||
@@ -62,8 +62,8 @@ public:
|
||||
KRQuaternion& operator *=(const float& v);
|
||||
KRQuaternion& operator /=(const float& v);
|
||||
|
||||
friend bool operator ==(KRVector3 &v1, KRVector3 &v2);
|
||||
friend bool operator !=(KRVector3 &v1, KRVector3 &v2);
|
||||
friend bool operator ==(KRQuaternion &v1, KRQuaternion &v2);
|
||||
friend bool operator !=(KRQuaternion &v1, KRQuaternion &v2);
|
||||
float& operator [](unsigned i);
|
||||
float operator [](unsigned i) const;
|
||||
|
||||
@@ -77,6 +77,8 @@ public:
|
||||
|
||||
void conjugate();
|
||||
static KRQuaternion Conjugate(const KRQuaternion &v1);
|
||||
|
||||
static KRQuaternion FromAngleAxis(const KRVector3 &axis, float angle);
|
||||
private:
|
||||
float m_val[4];
|
||||
};
|
||||
|
||||
@@ -82,7 +82,8 @@ KRVector2 KRVector2::operator *(const float v) const {
|
||||
}
|
||||
|
||||
KRVector2 KRVector2::operator /(const float v) const {
|
||||
return KRVector2(x / v, y / v);
|
||||
float inv_v = 1.0f / v;
|
||||
return KRVector2(x * inv_v, y * inv_v);
|
||||
}
|
||||
|
||||
KRVector2& KRVector2::operator +=(const KRVector2& b) {
|
||||
@@ -106,8 +107,9 @@ KRVector2& KRVector2::operator *=(const float v) {
|
||||
}
|
||||
|
||||
KRVector2& KRVector2::operator /=(const float v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
float inv_v = 1.0f / v;
|
||||
x *= inv_v;
|
||||
y *= inv_v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -169,9 +171,9 @@ float KRVector2::operator[](unsigned i) const {
|
||||
}
|
||||
|
||||
void KRVector2::normalize() {
|
||||
float magnitude = sqrtf(x * x + y * y);
|
||||
x /= magnitude;
|
||||
y /= magnitude;
|
||||
float inv_magnitude = 1.0f / sqrtf(x * x + y * y);
|
||||
x *= inv_magnitude;
|
||||
y *= inv_magnitude;
|
||||
}
|
||||
|
||||
float KRVector2::sqrMagnitude() const {
|
||||
@@ -184,8 +186,8 @@ float KRVector2::magnitude() const {
|
||||
|
||||
|
||||
KRVector2 KRVector2::Normalize(const KRVector2 &v) {
|
||||
float magnitude = sqrtf(v.x * v.x + v.y * v.y);
|
||||
return KRVector2(v.x / magnitude, v.y / magnitude);
|
||||
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y);
|
||||
return KRVector2(v.x * inv_magnitude, v.y * inv_magnitude);
|
||||
}
|
||||
|
||||
float KRVector2::Cross(const KRVector2 &v1, const KRVector2 &v2) {
|
||||
|
||||
@@ -176,7 +176,8 @@ KRVector3 KRVector3::operator *(const float v) const {
|
||||
}
|
||||
|
||||
KRVector3 KRVector3::operator /(const float v) const {
|
||||
return KRVector3(x / v, y / v, z / v);
|
||||
float inv_v = 1.0f / v;
|
||||
return KRVector3(x * inv_v, y * inv_v, z * inv_v);
|
||||
}
|
||||
|
||||
KRVector3& KRVector3::operator +=(const KRVector3& b) {
|
||||
@@ -204,9 +205,10 @@ KRVector3& KRVector3::operator *=(const float v) {
|
||||
}
|
||||
|
||||
KRVector3& KRVector3::operator /=(const float v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
z /= v;
|
||||
float inv_v = 1.0f / v;
|
||||
x *= inv_v;
|
||||
y *= inv_v;
|
||||
z *= inv_v;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -253,14 +255,14 @@ float KRVector3::magnitude() const {
|
||||
}
|
||||
|
||||
void KRVector3::normalize() {
|
||||
float magnitude = sqrtf(x * x + y * y + z * z);
|
||||
x /= magnitude;
|
||||
y /= magnitude;
|
||||
z /= magnitude;
|
||||
float inv_magnitude = 1.0f / sqrtf(x * x + y * y + z * z);
|
||||
x *= inv_magnitude;
|
||||
y *= inv_magnitude;
|
||||
z *= inv_magnitude;
|
||||
}
|
||||
KRVector3 KRVector3::Normalize(const KRVector3 &v) {
|
||||
float magnitude = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
return KRVector3(v.x / magnitude, v.y / magnitude, v.z / magnitude);
|
||||
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
return KRVector3(v.x * inv_magnitude, v.y * inv_magnitude, v.z * inv_magnitude);
|
||||
}
|
||||
|
||||
KRVector3 KRVector3::Cross(const KRVector3 &v1, const KRVector3 &v2) {
|
||||
|
||||
@@ -206,10 +206,11 @@ KRVector4& KRVector4::operator *=(const float v) {
|
||||
}
|
||||
|
||||
KRVector4& KRVector4::operator /=(const float v) {
|
||||
x /= v;
|
||||
y /= v;
|
||||
z /= v;
|
||||
w /= v;
|
||||
float inv_v = 1.0f / v;
|
||||
x *= inv_v;
|
||||
y *= inv_v;
|
||||
z *= inv_v;
|
||||
w *= inv_v;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -260,15 +261,15 @@ float KRVector4::magnitude() const {
|
||||
}
|
||||
|
||||
void KRVector4::normalize() {
|
||||
float magnitude = sqrtf(x * x + y * y + z * z + w * w);
|
||||
x /= magnitude;
|
||||
y /= magnitude;
|
||||
z /= magnitude;
|
||||
w /= magnitude;
|
||||
float inv_magnitude = 1.0f / sqrtf(x * x + y * y + z * z + w * w);
|
||||
x *= inv_magnitude;
|
||||
y *= inv_magnitude;
|
||||
z *= inv_magnitude;
|
||||
w *= inv_magnitude;
|
||||
}
|
||||
KRVector4 KRVector4::Normalize(const KRVector4 &v) {
|
||||
float magnitude = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
|
||||
return KRVector4(v.x / magnitude, v.y / magnitude, v.z / magnitude, v.w / magnitude);
|
||||
float inv_magnitude = 1.0f / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
|
||||
return KRVector4(v.x * inv_magnitude, v.y * inv_magnitude, v.z * inv_magnitude, v.w * inv_magnitude);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user