From 508adf5016fda7c274279e43d8899ea29e003015 Mon Sep 17 00:00:00 2001 From: Mazzoleni Alice Francesca Date: Tue, 1 Apr 2025 10:01:23 +0200 Subject: [PATCH] refactoring of remaining files --- CMakeLists.txt | 9 +- include/aare/CalculateEta.hpp | 192 +++++++++++++++++++++++++++++++++ include/aare/Cluster.hpp | 11 +- include/aare/ClusterFileV2.hpp | 28 +++-- include/aare/ClusterFinder.hpp | 51 ++++----- src/Cluster.test.cpp | 19 ++-- src/ClusterFinder.test.cpp | 24 ++--- src/ClusterVector.test.cpp | 154 ++++++++++++-------------- 8 files changed, 338 insertions(+), 150 deletions(-) create mode 100644 include/aare/CalculateEta.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index efb3a0c..0bdc317 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -309,7 +309,7 @@ set(PUBLICHEADERS include/aare/ArrayExpr.hpp include/aare/CalculateEta.hpp include/aare/Cluster.hpp - #include/aare/ClusterFinder.hpp + include/aare/ClusterFinder.hpp include/aare/ClusterFile.hpp include/aare/CtbRawFile.hpp include/aare/ClusterVector.hpp @@ -330,14 +330,13 @@ set(PUBLICHEADERS include/aare/RawFile.hpp include/aare/RawMasterFile.hpp include/aare/RawSubFile.hpp - #include/aare/VarClusterFinder.hpp + include/aare/VarClusterFinder.hpp include/aare/utils/task.hpp ) set(SourceFiles ${CMAKE_CURRENT_SOURCE_DIR}/src/CtbRawFile.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFile.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/defs.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Dtype.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/decode.cpp @@ -393,8 +392,8 @@ if(AARE_TESTS) ${CMAKE_CURRENT_SOURCE_DIR}/src/RawMasterFile.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/NDArray.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/NDView.test.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFinder.test.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterVector.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFinder.test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterVector.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Cluster.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Pedestal.test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/NumpyFile.test.cpp diff --git a/include/aare/CalculateEta.hpp b/include/aare/CalculateEta.hpp new file mode 100644 index 0000000..29c5cc4 --- /dev/null +++ b/include/aare/CalculateEta.hpp @@ -0,0 +1,192 @@ +#pragma once + +#include "aare/Cluster.hpp" +#include "aare/ClusterVector.hpp" +#include "aare/NDArray.hpp" + +namespace aare { + +typedef enum { + cBottomLeft = 0, + cBottomRight = 1, + cTopLeft = 2, + cTopRight = 3 +} corner; + +typedef enum { + pBottomLeft = 0, + pBottom = 1, + pBottomRight = 2, + pLeft = 3, + pCenter = 4, + pRight = 5, + pTopLeft = 6, + pTop = 7, + pTopRight = 8 +} pixel; + +// TODO: maybe template this!!!!!! why int32_t???? +struct Eta2 { + double x; + double y; + int c; + int32_t sum; +}; + +/** + * @brief Calculate the eta2 values for all clusters in a Clsutervector + */ +template >> +NDArray calculate_eta2(const ClusterVector &clusters) { + NDArray eta2({static_cast(clusters.size()), 2}); + + for (size_t i = 0; i < clusters.size(); i++) { + auto e = calculate_eta2(clusters.at(i)); + eta2(i, 0) = e.x; + eta2(i, 1) = e.y; + } + + return eta2; +} + +/** + * @brief Calculate the eta2 values for a generic sized cluster and return them + * in a Eta2 struct containing etay, etax and the index of the respective 2x2 + * subcluster. + */ +template +Eta2 calculate_eta2( + const Cluster &cl) { + Eta2 eta{}; + + constexpr size_t num_2x2_subclusters = + (ClusterSizeX - 1) * (ClusterSizeY - 1); + std::array sum_2x2_subcluster; + for (size_t i = 0; i < ClusterSizeY - 1; ++i) { + for (size_t j = 0; j < ClusterSizeX - 1; ++j) + sum_2x2_subcluster[i * (ClusterSizeX - 1) + j] = + cl.data[i * ClusterSizeX + j] + + cl.data[i * ClusterSizeX + j + 1] + + cl.data[(i + 1) * ClusterSizeX + j] + + cl.data[(i + 1) * ClusterSizeX + j + 1]; + } + + auto c = + std::max_element(sum_2x2_subcluster.begin(), sum_2x2_subcluster.end()) - + sum_2x2_subcluster.begin(); + + eta.sum = sum_2x2_subcluster[c]; + + size_t index_bottom_left_max_2x2_subcluster = + (int(c / (ClusterSizeX - 1))) * ClusterSizeX + c % (ClusterSizeX - 1); + + if ((cl.data[index_bottom_left_max_2x2_subcluster] + + cl.data[index_bottom_left_max_2x2_subcluster + 1]) != 0) + eta.x = static_cast( + cl.data[index_bottom_left_max_2x2_subcluster + 1]) / + (cl.data[index_bottom_left_max_2x2_subcluster] + + cl.data[index_bottom_left_max_2x2_subcluster + 1]); + + if ((cl.data[index_bottom_left_max_2x2_subcluster] + + cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]) != 0) + eta.y = + static_cast( + cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]) / + (cl.data[index_bottom_left_max_2x2_subcluster] + + cl.data[index_bottom_left_max_2x2_subcluster + ClusterSizeX]); + + eta.c = c; // TODO only supported for 2x2 and 3x3 clusters -> at least no + // underyling enum class + return eta; +} + +/** + * @brief Calculate the eta2 values for a 3x3 cluster and return them in a Eta2 + * struct containing etay, etax and the corner of the cluster. + */ +template Eta2 calculate_eta2(const Cluster &cl) { + Eta2 eta{}; + + std::array tot2; + tot2[0] = cl.data[0] + cl.data[1] + cl.data[3] + cl.data[4]; + tot2[1] = cl.data[1] + cl.data[2] + cl.data[4] + cl.data[5]; + tot2[2] = cl.data[3] + cl.data[4] + cl.data[6] + cl.data[7]; + tot2[3] = cl.data[4] + cl.data[5] + cl.data[7] + cl.data[8]; + + auto c = std::max_element(tot2.begin(), tot2.end()) - tot2.begin(); + eta.sum = tot2[c]; + switch (c) { + case cBottomLeft: + if ((cl.data[3] + cl.data[4]) != 0) + eta.x = static_cast(cl.data[4]) / (cl.data[3] + cl.data[4]); + if ((cl.data[1] + cl.data[4]) != 0) + eta.y = static_cast(cl.data[4]) / (cl.data[1] + cl.data[4]); + eta.c = cBottomLeft; + break; + case cBottomRight: + if ((cl.data[2] + cl.data[5]) != 0) + eta.x = static_cast(cl.data[5]) / (cl.data[4] + cl.data[5]); + if ((cl.data[1] + cl.data[4]) != 0) + eta.y = static_cast(cl.data[4]) / (cl.data[1] + cl.data[4]); + eta.c = cBottomRight; + break; + case cTopLeft: + if ((cl.data[7] + cl.data[4]) != 0) + eta.x = static_cast(cl.data[4]) / (cl.data[3] + cl.data[4]); + if ((cl.data[7] + cl.data[4]) != 0) + eta.y = static_cast(cl.data[7]) / (cl.data[7] + cl.data[4]); + eta.c = cTopLeft; + break; + case cTopRight: + if ((cl.data[5] + cl.data[4]) != 0) + eta.x = static_cast(cl.data[5]) / (cl.data[5] + cl.data[4]); + if ((cl.data[7] + cl.data[4]) != 0) + eta.y = static_cast(cl.data[7]) / (cl.data[7] + cl.data[4]); + eta.c = cTopRight; + break; + } + return eta; +} + +template Eta2 calculate_eta2(const Cluster &cl) { + Eta2 eta{}; + + eta.x = static_cast(cl.data[1]) / (cl.data[0] + cl.data[1]); + eta.y = static_cast(cl.data[2]) / (cl.data[0] + cl.data[2]); + eta.sum = cl.data[0] + cl.data[1] + cl.data[2] + cl.data[3]; + eta.c = cBottomLeft; // TODO! This is not correct, but need to put something + return eta; +} + +// calculates Eta3 for 3x3 cluster based on code from analyze_cluster +// TODO only supported for 3x3 Clusters +template Eta2 calculate_eta3(const Cluster &cl) { + + Eta2 eta{}; + + T sum = 0; + + std::for_each(std::begin(cl.data), std::end(cl.data), + [&sum](T x) { sum += x; }); + + eta.sum = sum; + + eta.c = corner::cBottomLeft; + + if ((cl.data[3] + cl.data[4] + cl.data[5]) != 0) + + eta.x = static_cast(-cl.data[3] + cl.data[3 + 2]) / + + (cl.data[3] + cl.data[4] + cl.data[5]); + + if ((cl.data[1] + cl.data[4] + cl.data[7]) != 0) + + eta.y = static_cast(-cl.data[1] + cl.data[2 * 3 + 1]) / + + (cl.data[1] + cl.data[4] + cl.data[7]); + + return eta; +} + +} // namespace aare \ No newline at end of file diff --git a/include/aare/Cluster.hpp b/include/aare/Cluster.hpp index 90701ea..b2d1d3c 100644 --- a/include/aare/Cluster.hpp +++ b/include/aare/Cluster.hpp @@ -13,12 +13,17 @@ namespace aare { +template +constexpr bool is_valid_cluster = + std::is_arithmetic_v && std::is_integral_v && + (ClusterSizeX > 0) && (ClusterSizeY > 0); + // requires clause c++20 maybe update template && std::is_integral_v && - (ClusterSizeX > 1) && (ClusterSizeY > 1)>> + is_valid_cluster>> struct Cluster { CoordType x; CoordType y; @@ -29,11 +34,9 @@ struct Cluster { template struct is_cluster : std::false_type {}; // Default case: Not a Cluster -// TODO: Do i need the require clause here as well? template struct is_cluster> : std::true_type {}; // Cluster -// helper template constexpr bool is_cluster_v = is_cluster::value; } // namespace aare diff --git a/include/aare/ClusterFileV2.hpp b/include/aare/ClusterFileV2.hpp index 99f5976..55b8a2b 100644 --- a/include/aare/ClusterFileV2.hpp +++ b/include/aare/ClusterFileV2.hpp @@ -1,15 +1,16 @@ #pragma once #include "aare/core/defs.hpp" #include -#include #include +#include namespace aare { struct ClusterHeader { int32_t frame_number; int32_t n_clusters; std::string to_string() const { - return "frame_number: " + std::to_string(frame_number) + ", n_clusters: " + std::to_string(n_clusters); + return "frame_number: " + std::to_string(frame_number) + + ", n_clusters: " + std::to_string(n_clusters); } }; @@ -24,7 +25,8 @@ struct ClusterV2_ { data_str += std::to_string(d) + ", "; } data_str += "]"; - return "x: " + std::to_string(x) + ", y: " + std::to_string(y) + ", data: " + data_str; + return "x: " + std::to_string(x) + ", y: " + std::to_string(y) + + ", data: " + data_str; } return "x: " + std::to_string(x) + ", y: " + std::to_string(y); } @@ -34,27 +36,31 @@ struct ClusterV2 { ClusterV2_ cluster; int32_t frame_number; std::string to_string() const { - return "frame_number: " + std::to_string(frame_number) + ", " + cluster.to_string(); + return "frame_number: " + std::to_string(frame_number) + ", " + + cluster.to_string(); } }; /** * @brief - * important not: fp always points to the clusters header and does not point to individual clusters + * important not: fp always points to the clusters header and does not point to + * individual clusters * */ class ClusterFileV2 { - std::filesystem::path m_fpath; + std::filesystem::path m_fpath; std::string m_mode; FILE *fp{nullptr}; - void check_open(){ + void check_open() { if (!fp) - throw std::runtime_error(fmt::format("File: {} not open", m_fpath.string())); + throw std::runtime_error( + fmt::format("File: {} not open", m_fpath.string())); } public: - ClusterFileV2(std::filesystem::path const &fpath, std::string const &mode): m_fpath(fpath), m_mode(mode) { + ClusterFileV2(std::filesystem::path const &fpath, std::string const &mode) + : m_fpath(fpath), m_mode(mode) { if (m_mode != "r" && m_mode != "w") throw std::invalid_argument("mode must be 'r' or 'w'"); if (m_mode == "r" && !std::filesystem::exists(m_fpath)) @@ -77,7 +83,7 @@ class ClusterFileV2 { check_open(); ClusterHeader header; - fread(&header, sizeof(ClusterHeader), 1, fp); + fread(&header, sizeof(ClusterHeader), 1, fp); std::vector clusters_(header.n_clusters); fread(clusters_.data(), sizeof(ClusterV2_), header.n_clusters, fp); std::vector clusters; @@ -117,7 +123,7 @@ class ClusterFileV2 { size_t write(std::vector> const &clusters) { check_open(); - if (m_mode != "w") + if (m_mode != "w") throw std::runtime_error("File not opened in write mode"); size_t n_clusters = 0; diff --git a/include/aare/ClusterFinder.hpp b/include/aare/ClusterFinder.hpp index 84b207b..9a15448 100644 --- a/include/aare/ClusterFinder.hpp +++ b/include/aare/ClusterFinder.hpp @@ -10,17 +10,16 @@ namespace aare { -template +template class ClusterFinder { Shape<2> m_image_size; - const int m_cluster_sizeX; - const int m_cluster_sizeY; const PEDESTAL_TYPE m_nSigma; const PEDESTAL_TYPE c2; const PEDESTAL_TYPE c3; Pedestal m_pedestal; - ClusterVector m_clusters; + ClusterVector> m_clusters; public: /** @@ -31,15 +30,12 @@ class ClusterFinder { * @param capacity initial capacity of the cluster vector * */ - ClusterFinder(Shape<2> image_size, Shape<2> cluster_size, - PEDESTAL_TYPE nSigma = 5.0, size_t capacity = 1000000) - : m_image_size(image_size), m_cluster_sizeX(cluster_size[0]), - m_cluster_sizeY(cluster_size[1]), - m_nSigma(nSigma), - c2(sqrt((m_cluster_sizeY + 1) / 2 * (m_cluster_sizeX + 1) / 2)), - c3(sqrt(m_cluster_sizeX * m_cluster_sizeY)), - m_pedestal(image_size[0], image_size[1]), - m_clusters(m_cluster_sizeX, m_cluster_sizeY, capacity) {}; + ClusterFinder(Shape<2> image_size, PEDESTAL_TYPE nSigma = 5.0, + size_t capacity = 1000000) + : m_image_size(image_size), m_nSigma(nSigma), + c2(sqrt((ClusterSizeY + 1) / 2 * (ClusterSizeX + 1) / 2)), + c3(sqrt(ClusterSizeX * ClusterSizeY)), + m_pedestal(image_size[0], image_size[1]), m_clusters(capacity) {}; void push_pedestal_frame(NDView frame) { m_pedestal.push(frame); @@ -56,23 +52,26 @@ class ClusterFinder { * same capacity as the old one * */ - ClusterVector steal_clusters(bool realloc_same_capacity = false) { - ClusterVector tmp = std::move(m_clusters); + ClusterVector> + steal_clusters(bool realloc_same_capacity = false) { + ClusterVector> tmp = + std::move(m_clusters); if (realloc_same_capacity) - m_clusters = ClusterVector(m_cluster_sizeX, m_cluster_sizeY, - tmp.capacity()); + m_clusters = ClusterVector>( + tmp.capacity()); else - m_clusters = ClusterVector(m_cluster_sizeX, m_cluster_sizeY); + m_clusters = + ClusterVector>{}; return tmp; } void find_clusters(NDView frame, uint64_t frame_number = 0) { // // TODO! deal with even size clusters // // currently 3,3 -> +/- 1 // // 4,4 -> +/- 2 - int dy = m_cluster_sizeY / 2; - int dx = m_cluster_sizeX / 2; + int dy = ClusterSizeY / 2; + int dx = ClusterSizeX / 2; m_clusters.set_frame_number(frame_number); - std::vector cluster_data(m_cluster_sizeX * m_cluster_sizeY); + std::vector cluster_data(ClusterSizeX * ClusterSizeY); for (int iy = 0; iy < frame.shape(0); iy++) { for (int ix = 0; ix < frame.shape(1); ix++) { @@ -109,8 +108,12 @@ class ClusterFinder { // pass } else { // m_pedestal.push(iy, ix, frame(iy, ix)); // Safe option - m_pedestal.push_fast(iy, ix, frame(iy, ix)); // Assume we have reached n_samples in the pedestal, slight performance improvement - continue; // It was a pedestal value nothing to store + m_pedestal.push_fast( + iy, ix, + frame(iy, + ix)); // Assume we have reached n_samples in the + // pedestal, slight performance improvement + continue; // It was a pedestal value nothing to store } // Store cluster diff --git a/src/Cluster.test.cpp b/src/Cluster.test.cpp index de53e6e..20c3948 100644 --- a/src/Cluster.test.cpp +++ b/src/Cluster.test.cpp @@ -4,6 +4,7 @@ ***********************************************/ #include "aare/Cluster.hpp" +#include "aare/CalculateEta.hpp" #include "aare/ClusterFile.hpp" // #include "catch.hpp" @@ -13,26 +14,24 @@ using namespace aare; -/* TEST_CASE("Correct Instantiation of Cluster and ClusterVector", "[.cluster][.instantiation]") { + CHECK(is_valid_cluster); + CHECK(is_valid_cluster); + CHECK(not is_valid_cluster); + CHECK(not is_valid_cluster); + CHECK(not is_valid_cluster); - REQUIRE(not std::is_constructible_v>); - - // all 1,2 and 0,4 are not defined!! - std::make_tuple(Cluster, ), - std::make_tuple(Cluster, ) - - + CHECK(not is_cluster_v); + CHECK(is_cluster_v>); } -*/ using ClusterTypes = std::variant, Cluster, Cluster, Cluster, Cluster>; -TEST_CASE("calculate_eta2", "[.cluster][.instantiation]") { +TEST_CASE("calculate_eta2", "[.cluster][.eta_calculation]") { // weird expect cluster_start to be in bottom_left corner -> row major -> // check how its used should be an image!! diff --git a/src/ClusterFinder.test.cpp b/src/ClusterFinder.test.cpp index 768e632..8989581 100644 --- a/src/ClusterFinder.test.cpp +++ b/src/ClusterFinder.test.cpp @@ -1,19 +1,18 @@ #include "aare/ClusterFinder.hpp" #include "aare/Pedestal.hpp" -#include #include +#include #include #include using namespace aare; -//TODO! Find a way to test the cluster finder - - +// TODO! Find a way to test the cluster finder // class ClusterFinderUnitTest : public ClusterFinder { // public: -// ClusterFinderUnitTest(int cluster_sizeX, int cluster_sizeY, double nSigma = 5.0, double threshold = 0.0) +// ClusterFinderUnitTest(int cluster_sizeX, int cluster_sizeY, double nSigma +// = 5.0, double threshold = 0.0) // : ClusterFinder(cluster_sizeX, cluster_sizeY, nSigma, threshold) {} // double get_c2() { return c2; } // double get_c3() { return c3; } @@ -37,8 +36,8 @@ using namespace aare; // REQUIRE_THAT(cf.get_c3(), Catch::Matchers::WithinRel(c3, 1e-9)); // } -TEST_CASE("Construct a cluster finder"){ - ClusterFinder clusterFinder({400,400}, {3,3}); +TEST_CASE("Construct a cluster finder") { + ClusterFinder clusterFinder({400, 400}); // REQUIRE(clusterFinder.get_cluster_sizeX() == 3); // REQUIRE(clusterFinder.get_cluster_sizeY() == 3); // REQUIRE(clusterFinder.get_threshold() == 1); @@ -49,16 +48,17 @@ TEST_CASE("Construct a cluster finder"){ // aare::Pedestal pedestal(10, 10, 5); // NDArray frame({10, 10}); // frame = 0; -// ClusterFinder clusterFinder(3, 3, 1, 1); // 3x3 cluster, 1 nSigma, 1 threshold +// ClusterFinder clusterFinder(3, 3, 1, 1); // 3x3 cluster, 1 nSigma, 1 +// threshold -// auto clusters = clusterFinder.find_clusters_without_threshold(frame.span(), pedestal); +// auto clusters = +// clusterFinder.find_clusters_without_threshold(frame.span(), pedestal); // REQUIRE(clusters.size() == 0); // frame(5, 5) = 10; -// clusters = clusterFinder.find_clusters_without_threshold(frame.span(), pedestal); -// REQUIRE(clusters.size() == 1); -// REQUIRE(clusters[0].x == 5); +// clusters = clusterFinder.find_clusters_without_threshold(frame.span(), +// pedestal); REQUIRE(clusters.size() == 1); REQUIRE(clusters[0].x == 5); // REQUIRE(clusters[0].y == 5); // for (int i = 0; i < 3; i++) { // for (int j = 0; j < 3; j++) { diff --git a/src/ClusterVector.test.cpp b/src/ClusterVector.test.cpp index 8ca3b1e..acbbf56 100644 --- a/src/ClusterVector.test.cpp +++ b/src/ClusterVector.test.cpp @@ -1,21 +1,15 @@ -#include #include "aare/ClusterVector.hpp" +#include -#include #include +#include +using aare::Cluster; using aare::ClusterVector; -struct Cluster_i2x2 { - int16_t x; - int16_t y; - int32_t data[4]; -}; - TEST_CASE("ClusterVector 2x2 int32_t capacity 4, push back then read") { - - ClusterVector cv(2, 2, 4); + ClusterVector> cv(4); REQUIRE(cv.capacity() == 4); REQUIRE(cv.size() == 0); REQUIRE(cv.cluster_size_x() == 2); @@ -23,51 +17,45 @@ TEST_CASE("ClusterVector 2x2 int32_t capacity 4, push back then read") { // int16_t, int16_t, 2x2 int32_t = 20 bytes REQUIRE(cv.item_size() == 20); - //Create a cluster and push back into the vector - Cluster_i2x2 c1 = {1, 2, {3, 4, 5, 6}}; - cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + // Create a cluster and push back into the vector + Cluster c1 = {1, 2, {3, 4, 5, 6}}; + cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); REQUIRE(cv.size() == 1); REQUIRE(cv.capacity() == 4); - //Read the cluster back out using copy. TODO! Can we improve the API? - Cluster_i2x2 c2; + // Read the cluster back out using copy. TODO! Can we improve the API? + Cluster c2; std::byte *ptr = cv.element_ptr(0); - std::copy(ptr, ptr + cv.item_size(), reinterpret_cast(&c2)); + std::copy(ptr, ptr + cv.item_size(), reinterpret_cast(&c2)); - //Check that the data is the same + // Check that the data is the same REQUIRE(c1.x == c2.x); REQUIRE(c1.y == c2.y); - for(size_t i = 0; i < 4; i++) { + for (size_t i = 0; i < 4; i++) { REQUIRE(c1.data[i] == c2.data[i]); } } -TEST_CASE("Summing 3x1 clusters of int64"){ - struct Cluster_l3x1{ - int16_t x; - int16_t y; - int32_t data[3]; - }; - - ClusterVector cv(3, 1, 2); +TEST_CASE("Summing 3x1 clusters of int64") { + ClusterVector> cv(2); REQUIRE(cv.capacity() == 2); REQUIRE(cv.size() == 0); REQUIRE(cv.cluster_size_x() == 3); REQUIRE(cv.cluster_size_y() == 1); - //Create a cluster and push back into the vector - Cluster_l3x1 c1 = {1, 2, {3, 4, 5}}; - cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + // Create a cluster and push back into the vector + Cluster c1 = {1, 2, {3, 4, 5}}; + cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); REQUIRE(cv.capacity() == 2); REQUIRE(cv.size() == 1); - Cluster_l3x1 c2 = {6, 7, {8, 9, 10}}; - cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); + Cluster c2 = {6, 7, {8, 9, 10}}; + cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); REQUIRE(cv.capacity() == 2); REQUIRE(cv.size() == 2); - Cluster_l3x1 c3 = {11, 12, {13, 14, 15}}; - cv.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); + Cluster c3 = {11, 12, {13, 14, 15}}; + cv.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); REQUIRE(cv.capacity() == 4); REQUIRE(cv.size() == 3); @@ -78,28 +66,22 @@ TEST_CASE("Summing 3x1 clusters of int64"){ REQUIRE(sums[2] == 42); } -TEST_CASE("Storing floats"){ - struct Cluster_f4x2{ - int16_t x; - int16_t y; - float data[8]; - }; - - ClusterVector cv(2, 4, 10); +TEST_CASE("Storing floats") { + ClusterVector> cv(10); REQUIRE(cv.capacity() == 10); REQUIRE(cv.size() == 0); REQUIRE(cv.cluster_size_x() == 2); REQUIRE(cv.cluster_size_y() == 4); - //Create a cluster and push back into the vector - Cluster_f4x2 c1 = {1, 2, {3.0, 4.0, 5.0, 6.0,3.0, 4.0, 5.0, 6.0}}; - cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + // Create a cluster and push back into the vector + Cluster c1 = {1, 2, {3.0, 4.0, 5.0, 6.0, 3.0, 4.0, 5.0, 6.0}}; + cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); REQUIRE(cv.capacity() == 10); REQUIRE(cv.size() == 1); - - Cluster_f4x2 c2 = {6, 7, {8.0, 9.0, 10.0, 11.0,8.0, 9.0, 10.0, 11.0}}; - cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); + Cluster c2 = { + 6, 7, {8.0, 9.0, 10.0, 11.0, 8.0, 9.0, 10.0, 11.0}}; + cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); REQUIRE(cv.capacity() == 10); REQUIRE(cv.size() == 2); @@ -109,26 +91,27 @@ TEST_CASE("Storing floats"){ REQUIRE_THAT(sums[1], Catch::Matchers::WithinAbs(76.0, 1e-6)); } -TEST_CASE("Push back more than initial capacity"){ - - ClusterVector cv(2, 2, 2); +TEST_CASE("Push back more than initial capacity") { + + ClusterVector> cv(2); auto initial_data = cv.data(); - Cluster_i2x2 c1 = {1, 2, {3, 4, 5, 6}}; - cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + Cluster c1 = {1, 2, {3, 4, 5, 6}}; + cv.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); REQUIRE(cv.size() == 1); REQUIRE(cv.capacity() == 2); - Cluster_i2x2 c2 = {6, 7, {8, 9, 10, 11}}; - cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); + Cluster c2 = {6, 7, {8, 9, 10, 11}}; + cv.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); REQUIRE(cv.size() == 2); REQUIRE(cv.capacity() == 2); - Cluster_i2x2 c3 = {11, 12, {13, 14, 15, 16}}; - cv.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); - REQUIRE(cv.size() == 3); + Cluster c3 = {11, 12, {13, 14, 15, 16}}; + cv.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); + REQUIRE(cv.size() == 3); REQUIRE(cv.capacity() == 4); - Cluster_i2x2* ptr = reinterpret_cast(cv.data()); + Cluster *ptr = + reinterpret_cast *>(cv.data()); REQUIRE(ptr[0].x == 1); REQUIRE(ptr[0].y == 2); REQUIRE(ptr[1].x == 6); @@ -136,29 +119,31 @@ TEST_CASE("Push back more than initial capacity"){ REQUIRE(ptr[2].x == 11); REQUIRE(ptr[2].y == 12); - //We should have allocated a new buffer, since we outgrew the initial capacity + // We should have allocated a new buffer, since we outgrew the initial + // capacity REQUIRE(initial_data != cv.data()); - } -TEST_CASE("Concatenate two cluster vectors where the first has enough capacity"){ - ClusterVector cv1(2, 2, 12); - Cluster_i2x2 c1 = {1, 2, {3, 4, 5, 6}}; - cv1.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); - Cluster_i2x2 c2 = {6, 7, {8, 9, 10, 11}}; - cv1.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); +TEST_CASE( + "Concatenate two cluster vectors where the first has enough capacity") { + ClusterVector> cv1(12); + Cluster c1 = {1, 2, {3, 4, 5, 6}}; + cv1.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + Cluster c2 = {6, 7, {8, 9, 10, 11}}; + cv1.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); - ClusterVector cv2(2, 2, 2); - Cluster_i2x2 c3 = {11, 12, {13, 14, 15, 16}}; - cv2.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); - Cluster_i2x2 c4 = {16, 17, {18, 19, 20, 21}}; - cv2.push_back(c4.x, c4.y, reinterpret_cast(&c4.data[0])); + ClusterVector> cv2(2); + Cluster c3 = {11, 12, {13, 14, 15, 16}}; + cv2.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); + Cluster c4 = {16, 17, {18, 19, 20, 21}}; + cv2.push_back(c4.x, c4.y, reinterpret_cast(&c4.data[0])); cv1 += cv2; REQUIRE(cv1.size() == 4); REQUIRE(cv1.capacity() == 12); - Cluster_i2x2* ptr = reinterpret_cast(cv1.data()); + Cluster *ptr = + reinterpret_cast *>(cv1.data()); REQUIRE(ptr[0].x == 1); REQUIRE(ptr[0].y == 2); REQUIRE(ptr[1].x == 6); @@ -169,24 +154,25 @@ TEST_CASE("Concatenate two cluster vectors where the first has enough capacity") REQUIRE(ptr[3].y == 17); } -TEST_CASE("Concatenate two cluster vectors where we need to allocate"){ - ClusterVector cv1(2, 2, 2); - Cluster_i2x2 c1 = {1, 2, {3, 4, 5, 6}}; - cv1.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); - Cluster_i2x2 c2 = {6, 7, {8, 9, 10, 11}}; - cv1.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); +TEST_CASE("Concatenate two cluster vectors where we need to allocate") { + ClusterVector> cv1(2); + Cluster c1 = {1, 2, {3, 4, 5, 6}}; + cv1.push_back(c1.x, c1.y, reinterpret_cast(&c1.data[0])); + Cluster c2 = {6, 7, {8, 9, 10, 11}}; + cv1.push_back(c2.x, c2.y, reinterpret_cast(&c2.data[0])); - ClusterVector cv2(2, 2, 2); - Cluster_i2x2 c3 = {11, 12, {13, 14, 15, 16}}; - cv2.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); - Cluster_i2x2 c4 = {16, 17, {18, 19, 20, 21}}; - cv2.push_back(c4.x, c4.y, reinterpret_cast(&c4.data[0])); + ClusterVector> cv2(2); + Cluster c3 = {11, 12, {13, 14, 15, 16}}; + cv2.push_back(c3.x, c3.y, reinterpret_cast(&c3.data[0])); + Cluster c4 = {16, 17, {18, 19, 20, 21}}; + cv2.push_back(c4.x, c4.y, reinterpret_cast(&c4.data[0])); cv1 += cv2; REQUIRE(cv1.size() == 4); REQUIRE(cv1.capacity() == 4); - Cluster_i2x2* ptr = reinterpret_cast(cv1.data()); + Cluster *ptr = + reinterpret_cast *>(cv1.data()); REQUIRE(ptr[0].x == 1); REQUIRE(ptr[0].y == 2); REQUIRE(ptr[1].x == 6);