[Cleanup] Cleanup position methods (#4015)

# Notes
- Cleanup logic.
This commit is contained in:
Alex King 2024-01-24 23:47:24 -05:00 committed by GitHub
parent 7fed8fc8c8
commit 5b85f89c21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,19 +3,38 @@
#include <string> #include <string>
#include <cmath> #include <cmath>
#include "../common/strings.h" #include "../common/strings.h"
#include "../common/data_verification.h"
static const float position_eps = 0.0001f; constexpr float position_eps = 0.0001f;
std::string to_string(const glm::vec4 &position) { std::string to_string(const glm::vec4 &position)
return StringFormat("(%.3f, %.3f, %.3f, %.3f)", position.x,position.y,position.z,position.w); {
return fmt::format(
"({:.3f}, {:.3f}, {:.3f}, {:.3f})",
position.x,
position.y,
position.z,
position.w
);
} }
std::string to_string(const glm::vec3 &position){ std::string to_string(const glm::vec3 &position)
return StringFormat("(%.3f, %.3f, %.3f)", position.x,position.y,position.z); {
return fmt::format(
"({:.3f}, {:.3f}, {:.3f})",
position.x,
position.y,
position.z
);
} }
std::string to_string(const glm::vec2 &position){ std::string to_string(const glm::vec2 &position)
return StringFormat("(%.3f, %.3f)", position.x,position.y); {
return fmt::format(
"({:.3f}, {:.3f})",
position.x,
position.y
);
} }
bool IsOrigin(const glm::vec3 &position) { bool IsOrigin(const glm::vec3 &position) {
@ -103,14 +122,23 @@ float DistanceSquaredNoZ(const glm::vec4& point1, const glm::vec4& point2) {
* box (3 dimensional) formed from the points minimum and maximum. * box (3 dimensional) formed from the points minimum and maximum.
*/ */
bool IsWithinAxisAlignedBox(const glm::vec3 &position, const glm::vec3 &minimum, const glm::vec3 &maximum) { bool IsWithinAxisAlignedBox(const glm::vec3 &position, const glm::vec3 &minimum, const glm::vec3 &maximum) {
auto actualMinimum = glm::vec3(std::min(minimum.x, maximum.x), std::min(minimum.y, maximum.y),std::min(minimum.z, maximum.z)); auto min = glm::vec3(
auto actualMaximum = glm::vec3(std::max(minimum.x, maximum.x), std::max(minimum.y, maximum.y),std::max(minimum.z, maximum.z)); std::min(minimum.x, maximum.x),
std::min(minimum.y, maximum.y),
std::min(minimum.z, maximum.z)
);
bool xcheck = position.x >= actualMinimum.x && position.x <= actualMaximum.x; auto max = glm::vec3(
bool ycheck = position.y >= actualMinimum.y && position.y <= actualMaximum.y; std::max(minimum.x, maximum.x),
bool zcheck = position.z >= actualMinimum.z && position.z <= actualMaximum.z; std::max(minimum.y, maximum.y),
std::max(minimum.z, maximum.z)
);
return xcheck && ycheck && zcheck; const bool x_check = EQ::ValueWithin(position.x, min.x, max.x);
const bool y_check = EQ::ValueWithin(position.y, min.y, max.y);
const bool z_check = EQ::ValueWithin(position.z, min.z, max.z);
return x_check && y_check && z_check;
} }
/** /**
@ -118,13 +146,13 @@ bool IsWithinAxisAlignedBox(const glm::vec3 &position, const glm::vec3 &minimum,
* box (2 dimensional) formed from the points minimum and maximum. * box (2 dimensional) formed from the points minimum and maximum.
*/ */
bool IsWithinAxisAlignedBox(const glm::vec2 &position, const glm::vec2 &minimum, const glm::vec2 &maximum) { bool IsWithinAxisAlignedBox(const glm::vec2 &position, const glm::vec2 &minimum, const glm::vec2 &maximum) {
auto actualMinimum = glm::vec2(std::min(minimum.x, maximum.x), std::min(minimum.y, maximum.y)); auto min = glm::vec2(std::min(minimum.x, maximum.x), std::min(minimum.y, maximum.y));
auto actualMaximum = glm::vec2(std::max(minimum.x, maximum.x), std::max(minimum.y, maximum.y)); auto max = glm::vec2(std::max(minimum.x, maximum.x), std::max(minimum.y, maximum.y));
bool xcheck = position.x >= actualMinimum.x && position.x <= actualMaximum.x; const bool x_check = EQ::ValueWithin(position.x, min.x, max.x);
bool ycheck = position.y >= actualMinimum.y && position.y <= actualMaximum.y; const bool y_check = EQ::ValueWithin(position.y, min.y, max.y);
return xcheck && ycheck; return x_check && y_check;
} }
/** /**
@ -144,10 +172,10 @@ float GetReciprocalHeading(const glm::vec4& point1) {
*/ */
float GetReciprocalHeading(const float heading) float GetReciprocalHeading(const float heading)
{ {
float result = 0; float result;
// Convert to radians // Convert to radians
float h = (heading / 512.0f) * 6.283184f; const float h = (heading / 512.0f) * 6.283184f;
// Calculate the reciprocal heading in radians // Calculate the reciprocal heading in radians
result = h + 3.141592f; result = h + 3.141592f;
@ -228,23 +256,29 @@ bool IsPositionWithinSimpleCylinder(const glm::vec4 &p1, const glm::vec4 &cylind
float CalculateHeadingAngleBetweenPositions(float x1, float y1, float x2, float y2) float CalculateHeadingAngleBetweenPositions(float x1, float y1, float x2, float y2)
{ {
float y_diff = std::abs(y1 - y2);
float x_diff = std::abs(x1 - x2); float x_diff = std::abs(x1 - x2);
if (y_diff < 0.0000009999999974752427) float y_diff = std::abs(y1 - y2);
if (y_diff < 0.0000009999999974752427) {
y_diff = 0.0000009999999974752427; y_diff = 0.0000009999999974752427;
}
float angle = atan2(x_diff, y_diff) * 180.0f * 0.3183099014828645f; // angle, nice "pi" const float angle = atan2(x_diff, y_diff) * 180.0f * 0.3183099014828645f; // angle, nice "pi"
// return the right thing based on relative quadrant // return the right thing based on relative quadrant
// I'm sure this could be improved for readability, but whatever // I'm sure this could be improved for readability, but whatever
if (y1 >= y2) { if (y1 >= y2) {
if (x2 >= x1) if (x2 >= x1) {
return (90.0f - angle + 90.0f) * 511.5f * 0.0027777778f; return (90.0f - angle + 90.0f) * 511.5f * 0.0027777778f;
if (x2 <= x1) }
if (x2 <= x1) {
return (angle + 180.0f) * 511.5f * 0.0027777778f; return (angle + 180.0f) * 511.5f * 0.0027777778f;
} }
if (y1 > y2 || x2 > x1) }
if (y1 > y2 || x2 > x1) {
return angle * 511.5f * 0.0027777778f; return angle * 511.5f * 0.0027777778f;
else } else {
return (90.0f - angle + 270.0f) * 511.5f * 0.0027777778f; return (90.0f - angle + 270.0f) * 511.5f * 0.0027777778f;
} }
}