From 62635ec285c5ba65a10847b7a7f24f92c048ee61 Mon Sep 17 00:00:00 2001 From: Kp Date: Tue, 24 May 2022 02:32:58 +0000 Subject: [PATCH] Use enum class for vm_magnitude_squared --- common/include/fwd-vecmat.h | 2 +- common/include/vecmat.h | 23 ++++++++++++++--------- common/maths/vecmat.cpp | 3 +-- similar/main/fvi.cpp | 4 ++-- similar/main/laser.cpp | 20 ++++++++++++-------- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/common/include/fwd-vecmat.h b/common/include/fwd-vecmat.h index 533e17128..ced9da492 100644 --- a/common/include/fwd-vecmat.h +++ b/common/include/fwd-vecmat.h @@ -17,7 +17,7 @@ class vm_distance_squared; class vm_distance; class vm_magnitude; class vm_distance_squared; -class vm_magnitude_squared; +enum class vm_magnitude_squared : uint64_t; struct vms_angvec; struct vms_matrix; struct vms_quaternion; diff --git a/common/include/vecmat.h b/common/include/vecmat.h index 35d077678..f74f70b1c 100644 --- a/common/include/vecmat.h +++ b/common/include/vecmat.h @@ -132,6 +132,10 @@ public: d2(f2) { } + constexpr vm_distance_squared(vm_magnitude_squared m) : + d2{static_cast(static_cast(m))} + { + } constexpr bool operator<(const vm_distance_squared &rhs) const { return d2 < rhs.d2; @@ -171,20 +175,21 @@ public: } }; -class vm_magnitude_squared : public vm_distance_squared -{ -public: - constexpr explicit vm_magnitude_squared(const uint64_t &f2) : - vm_distance_squared(static_cast(f2)) - { - } -}; - constexpr vm_distance_squared vm_distance::operator*(const vm_distance &rhs) const { return vm_distance_squared{static_cast(static_cast(*this)) * static_cast(static_cast(rhs))}; } +constexpr bool operator<(const vm_magnitude_squared a, const fix &b) +{ + return static_cast(a) < b; +} + +constexpr bool operator<(const vm_magnitude_squared a, const vm_distance_squared b) +{ + return static_cast(a) < static_cast(b.operator fix64()); +} + #define DEFINE_SERIAL_VMS_VECTOR_TO_MESSAGE() \ DEFINE_SERIAL_UDT_TO_MESSAGE(vms_vector, v, (v.x, v.y, v.z)); \ ASSERT_SERIAL_UDT_MESSAGE_SIZE(vms_vector, 12) diff --git a/common/maths/vecmat.cpp b/common/maths/vecmat.cpp index dffa34144..159ee0355 100644 --- a/common/maths/vecmat.cpp +++ b/common/maths/vecmat.cpp @@ -160,7 +160,6 @@ fix vm_vec_dot(const vms_vector &v0,const vms_vector &v1) return vm_vec_dot3(v0.x, v0.y, v0.z, v1); } -//returns magnitude of a vector vm_magnitude_squared vm_vec_mag2(const vms_vector &v) { const int64_t x = v.x; @@ -172,7 +171,7 @@ vm_magnitude_squared vm_vec_mag2(const vms_vector &v) vm_magnitude vm_vec_mag(const vms_vector &v) { quadint q; - q.q = vm_vec_mag2(v).d2; + q.q = static_cast(vm_vec_mag2(v)); return vm_magnitude{quad_sqrt(q)}; } diff --git a/similar/main/fvi.cpp b/similar/main/fvi.cpp index 38c90cf8c..f76437599 100644 --- a/similar/main/fvi.cpp +++ b/similar/main/fvi.cpp @@ -417,10 +417,10 @@ static vm_distance_squared check_vector_to_sphere_1(vms_vector &intp,const vms_v if (mag_d == 0) { const auto int_dist = vm_vec_mag2(w); intp = p0; - if (int_dist.d2 < sphere_rad) + if (int_dist < sphere_rad) return int_dist; const fix64 sphere_rad64 = sphere_rad; - if (int_dist < vm_distance_squared{sphere_rad64 * sphere_rad64}) + if (int_dist < vm_magnitude_squared{static_cast(sphere_rad64 * sphere_rad64)}) return int_dist; return vm_distance_squared::minimum_value(); } diff --git a/similar/main/laser.cpp b/similar/main/laser.cpp index c09f83dc4..533919b02 100644 --- a/similar/main/laser.cpp +++ b/similar/main/laser.cpp @@ -1105,16 +1105,20 @@ imobjptridx_t find_homing_object_complete(const vms_vector &curpos, const vmobjp } const fix64 HOMING_MAX_TRACKABLE_DIST = F1_0*250; - vm_distance_squared max_trackable_dist{HOMING_MAX_TRACKABLE_DIST * HOMING_MAX_TRACKABLE_DIST}; - fix min_trackable_dot = HOMING_MIN_TRACKABLE_DOT; - + const auto max_trackable_dist = #if defined(DXX_BUILD_DESCENT_II) - if (tracker_id == weapon_id_type::OMEGA_ID) - { - max_trackable_dist = OMEGA_MAX_TRACKABLE_DIST * OMEGA_MAX_TRACKABLE_DIST; - min_trackable_dot = OMEGA_MIN_TRACKABLE_DOT; - } + (tracker_id == weapon_id_type::OMEGA_ID) + ? vm_distance_squared{(OMEGA_MAX_TRACKABLE_DIST * OMEGA_MAX_TRACKABLE_DIST)} + : #endif + vm_distance_squared{HOMING_MAX_TRACKABLE_DIST * HOMING_MAX_TRACKABLE_DIST}; + const auto min_trackable_dot = +#if defined(DXX_BUILD_DESCENT_II) + (tracker_id == weapon_id_type::OMEGA_ID) + ? OMEGA_MIN_TRACKABLE_DOT + : +#endif + HOMING_MIN_TRACKABLE_DOT; imobjptridx_t best_objnum = object_none; range_for (const auto &&curobjp, vmobjptridx)