2021-12-05 17:54:36 +00:00
|
|
|
#include "NiPoint3.h"
|
|
|
|
|
|
|
|
// C++
|
|
|
|
#include <cmath>
|
|
|
|
|
2024-01-29 07:53:12 +00:00
|
|
|
// MARK: Member Functions
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//! Gets the length of the vector
|
2024-01-29 07:53:12 +00:00
|
|
|
float NiPoint3::Length() const {
|
|
|
|
return std::sqrt(x * x + y * y + z * z);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Unitize the vector
|
2024-01-29 07:53:12 +00:00
|
|
|
NiPoint3 NiPoint3::Unitize() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
float length = this->Length();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-29 07:53:12 +00:00
|
|
|
return length != 0 ? *this / length : NiPoint3Constant::ZERO;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helper Functions
|
|
|
|
|
|
|
|
float NiPoint3::Angle(const NiPoint3& a, const NiPoint3& b) {
|
|
|
|
const auto dot = a.DotProduct(b);
|
|
|
|
const auto lenA = a.SquaredLength();
|
|
|
|
const auto lenB = a.SquaredLength();
|
|
|
|
return acos(dot / sqrt(lenA * lenB));
|
|
|
|
}
|
|
|
|
|
|
|
|
float NiPoint3::Distance(const NiPoint3& a, const NiPoint3& b) {
|
|
|
|
const auto dx = a.x - b.x;
|
|
|
|
const auto dy = a.y - b.y;
|
|
|
|
const auto dz = a.z - b.z;
|
|
|
|
|
|
|
|
return std::sqrt(dx * dx + dy * dy + dz * dz);
|
|
|
|
}
|
|
|
|
|
2024-01-29 07:53:12 +00:00
|
|
|
NiPoint3 NiPoint3::MoveTowards(const NiPoint3& current, const NiPoint3& target, const float maxDistanceDelta) {
|
2021-12-05 17:54:36 +00:00
|
|
|
float dx = target.x - current.x;
|
|
|
|
float dy = target.y - current.y;
|
|
|
|
float dz = target.z - current.z;
|
2023-12-28 04:18:20 +00:00
|
|
|
|
|
|
|
float lengthSquared = static_cast<float>(
|
|
|
|
static_cast<double>(dx) * static_cast<double>(dx) +
|
|
|
|
static_cast<double>(dy) * static_cast<double>(dy) +
|
|
|
|
static_cast<double>(dz) * static_cast<double>(dz)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (static_cast<double>(lengthSquared) == 0.0
|
|
|
|
|| static_cast<double>(maxDistanceDelta) >= 0.0
|
|
|
|
&& static_cast<double>(lengthSquared)
|
|
|
|
<= static_cast<double>(maxDistanceDelta) * static_cast<double>(maxDistanceDelta)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return target;
|
2023-12-28 04:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float length = std::sqrt(lengthSquared);
|
2021-12-05 17:54:36 +00:00
|
|
|
return NiPoint3(current.x + dx / length * maxDistanceDelta, current.y + dy / length * maxDistanceDelta, current.z + dz / length * maxDistanceDelta);
|
|
|
|
}
|