Add functions in Vector2, Vector3 and clamp vector function in mathematics

This commit is contained in:
Daniel Chappuis 2016-03-20 22:54:08 +01:00
parent 63fb6261a3
commit 10549796c8
4 changed files with 27 additions and 0 deletions

View File

@ -141,6 +141,9 @@ struct Vector2 {
/// Return a vector taking the maximum components of two vectors
static Vector2 max(const Vector2& vector1, const Vector2& vector2);
/// Return the zero vector
static Vector2 zero();
// -------------------- Friends -------------------- //
friend Vector2 operator+(const Vector2& vector1, const Vector2& vector2);
@ -333,6 +336,11 @@ inline Vector2 Vector2::max(const Vector2& vector1, const Vector2& vector2) {
std::max(vector1.y, vector2.y));
}
// Return the zero vector
inline Vector2 Vector2::zero() {
return Vector2(0, 0);
}
}
#endif

View File

@ -153,6 +153,9 @@ struct Vector3 {
/// Return a vector taking the maximum components of two vectors
static Vector3 max(const Vector3& vector1, const Vector3& vector2);
/// Return the zero vector
static Vector3 zero();
// -------------------- Friends -------------------- //
friend Vector3 operator+(const Vector3& vector1, const Vector3& vector2);
@ -373,6 +376,11 @@ inline decimal Vector3::getMaxValue() const {
return std::max(std::max(x, y), z);
}
// Return the zero vector
inline Vector3 Vector3::zero() {
return Vector3(0, 0, 0);
}
}
#endif

View File

@ -49,3 +49,11 @@ void reactphysics3d::computeBarycentricCoordinatesInTriangle(const Vector3& a, c
w = (d00 * d21 - d01 * d20) / denom;
u = decimal(1.0) - u - w;
}
// Clamp a vector such that it is no longer than a given maximum length
Vector3 reactphysics3d::clamp(const Vector3& vector, decimal maxLength) {
if (vector.lengthSquare() > maxLength * maxLength) {
return vector.getUnit() * maxLength;
}
return vector;
}

View File

@ -75,6 +75,9 @@ inline bool sameSign(decimal a, decimal b) {
return a * b >= decimal(0.0);
}
/// Clamp a vector such that it is no longer than a given maximum length
Vector3 clamp(const Vector3& vector, decimal maxLength);
/// Compute the barycentric coordinates u, v, w of a point p inside the triangle (a, b, c)
void computeBarycentricCoordinatesInTriangle(const Vector3& a, const Vector3& b, const Vector3& c,
const Vector3& p, decimal& u, decimal& v, decimal& w);