From 376ad44e67b73b0e8e98257fd61f22bfab4d088f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 3 May 2024 17:54:55 +0900 Subject: [PATCH] Use std::span instead of ptr + size in MinMax fn Note that std::span requires C++20. --- CMakeLists.txt | 1 + src/sc_sacd.cpp | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c226a8..54c7ff9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(SC_3D_CollisionDetectionHelpers_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/src/sc_ add_library(SC_3D_CollisionDetectionHelpers ${SC_3D_CollisionDetectionHelpers_SOURCES}) set_target_properties(SC_3D_CollisionDetectionHelpers PROPERTIES VERSION 2.0.2 SOVERSION 2) +target_compile_features(SC_3D_CollisionDetectionHelpers PUBLIC cxx_std_20) if(NOT DEFINED CMAKE_BUILD_TYPE OR "${CMAKE_BUILD_TYPE}" STREQUAL "") message("Defaulting to \"Debug\" build type.") diff --git a/src/sc_sacd.cpp b/src/sc_sacd.cpp index 048c27e..3e347f6 100644 --- a/src/sc_sacd.cpp +++ b/src/sc_sacd.cpp @@ -3,6 +3,7 @@ // Standard library includes. #include #include +#include #include #include @@ -213,18 +214,17 @@ struct SC_SACD_MinMax { }; std::vector SC_SACD_Get_Box_MinMax( - const SC_SACD_Generic_Box *box, const SC_SACD_Vec3 *normals, - std::size_t size) { + const SC_SACD_Generic_Box *box, const std::span normals) { std::vector minmaxes; std::vector corners = SC_SACD_Get_Box_Corners(box); // Assuming normals are not normalized, and will not normalize anyway. // MinMax count should be same as normals count. - for (std::size_t idx = 0; idx < size; ++idx) { + for (const auto &normal : normals) { SC_SACD_MinMax minmax{INFINITY, -INFINITY}; for (const auto &corner : corners) { - float projected = SC_SACD_Dot_Product(corner, normals[idx]); + float projected = SC_SACD_Dot_Product(corner, normal); if (projected > minmax.max) { minmax.max = projected; } @@ -284,10 +284,8 @@ int SC_SACD_Generic_Box_Collision(const SC_SACD_Generic_Box *a, } // Get all minmaxes. - std::vector minmaxes_a = - SC_SACD_Get_Box_MinMax(a, normals.data(), normals.size()); - std::vector minmaxes_b = - SC_SACD_Get_Box_MinMax(b, normals.data(), normals.size()); + std::vector minmaxes_a = SC_SACD_Get_Box_MinMax(a, normals); + std::vector minmaxes_b = SC_SACD_Get_Box_MinMax(b, normals); // Check minmaxes. for (unsigned int i = 0; i < normals.size(); ++i) { @@ -367,7 +365,7 @@ int SC_SACD_Sphere_Box_Collision(const SC_SACD_Sphere *sphere, std::vector normals{sphere_box_normal}; std::vector box_minmaxes = - SC_SACD_Get_Box_MinMax(box, normals.data(), normals.size()); + SC_SACD_Get_Box_MinMax(box, normals); float projected_0 = SC_SACD_Dot_Product( sphere_box_normal, sphere_pos + sphere_box_normal * sphere->radius); @@ -386,8 +384,7 @@ int SC_SACD_Sphere_Box_Collision(const SC_SACD_Sphere *sphere, // Next check the planes for the 3 normals of the box. auto box_normals = SC_SACD_Get_Box_Normals(box); - box_minmaxes = - SC_SACD_Get_Box_MinMax(box, box_normals.data(), box_normals.size()); + box_minmaxes = SC_SACD_Get_Box_MinMax(box, box_normals); for (unsigned int i = 0; i < box_normals.size(); ++i) { projected_0 = SC_SACD_Dot_Product( box_normals[i], sphere_pos + box_normals[i] * sphere->radius);