From dd4f4ba9ec981f6ca0b5eb6bdf14565ec38c98ea Mon Sep 17 00:00:00 2001 From: Thomas Nixon Date: Mon, 5 Jun 2023 12:02:07 +0100 Subject: [PATCH] replace generic resource embedder with const array this avoids pulling in iostream and float parsing --- cmake/embed_resource.cmake | 70 -------------------------------- cmake/embedded_resource.cpp.in | 13 ------ cmake/embedded_resource.hpp.in | 10 ----- cmake/generate_points_file.cmake | 14 +++++++ cmake/points_file.cpp.in | 13 ++++++ src/CMakeLists.txt | 13 +++--- src/hoa/hoa.cpp | 19 ++------- src/hoa/points_file.hpp | 11 +++++ 8 files changed, 47 insertions(+), 116 deletions(-) delete mode 100644 cmake/embed_resource.cmake delete mode 100644 cmake/embedded_resource.cpp.in delete mode 100644 cmake/embedded_resource.hpp.in create mode 100644 cmake/generate_points_file.cmake create mode 100644 cmake/points_file.cpp.in create mode 100644 src/hoa/points_file.hpp diff --git a/cmake/embed_resource.cmake b/cmake/embed_resource.cmake deleted file mode 100644 index 708749b..0000000 --- a/cmake/embed_resource.cmake +++ /dev/null @@ -1,70 +0,0 @@ -set(current_dir ${CMAKE_CURRENT_LIST_DIR}) -function(embed_resource) - # parse function arguments - set(options "") - set(oneValueArguments NAMESPACE BASE_NAME) - set(multiValueArguments RESOURCE_FILES) - cmake_parse_arguments(EMBED "${options}" "${oneValueArguments}" - "${multiValueArguments}" ${ARGN}) - foreach(arg ${oneValueArguments} ${multiValueArguments}) - if(NOT EMBED_${arg}) - message(WARNING "Argument ${arg} not defined in call to embed_resource") - endif() - endforeach() - - set_property( - DIRECTORY - APPEND - PROPERTY CMAKE_CONFIGURE_DEPENDS ${EMBED_RESOURCE_FILES}) - - set(byte_array_template - "const char file_@FILE_COUNT@[] = { @FILE_BYTE_ARRAY@ }") - set(lookup_template - "if (fileName == \"@FILE_NAME@\") { stream.write(file_@FILE_COUNT@, @ARRAY_SIZE@)\; return true\; }" - ) - - set(FILE_COUNT 1) - foreach(file_path ${EMBED_RESOURCE_FILES}) - # read resource file as hex with no byte separator - file(READ ${file_path} file_contents HEX) - # split into 2 char hex bytes - string(REGEX MATCHALL "(..)" file_bytes ${file_contents}) - # need length for call to ostream.write() - list(LENGTH file_bytes ARRAY_SIZE) - - # add 0x prefix and , separator TRANSFORM only valid in cmake 3.14+ or we - # could just do list(TRANSFORM file_bytes PREPEND "0x") list(JOIN file_bytes - # "," FILE_BYTE_ARRAY) - list(POP_FRONT file_bytes first_byte) - string(PREPEND first_byte "0x") - list(PREPEND file_bytes ${first_byte}) - list(JOIN file_bytes ",0x" FILE_BYTE_ARRAY) - - # strip path from data file, leaving name - get_filename_component(FILE_NAME "${file_path}" NAME) - # generate single byte array from template - string(CONFIGURE ${byte_array_template} byte_array @ONLY) - # add the trailing semicolon here as it gets interpreted as a list seperator - # if in template - list(APPEND byte_arrays "${byte_array}\;") - # generate single lookup entry - string(CONFIGURE "${lookup_template}" lookup @ONLY) - list(APPEND lookups "${lookup}") - math(EXPR FILE_COUNT "${FILE_COUNT} + 1") - endforeach() - - # join entries with newline for readability - if(WIN32) - set(line_end "\n\r") - else() - set(line_end "\n") - endif() - list(JOIN byte_arrays "${line_end}" EMBED_BYTE_ARRAYS) - list(JOIN lookups "${line_end}" EMBED_LOOKUPS) - - # generate files - configure_file("${current_dir}/embedded_resource.hpp.in" - "${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.hpp" @ONLY) - configure_file("${current_dir}/embedded_resource.cpp.in" - "${CMAKE_CURRENT_BINARY_DIR}/${EMBED_BASE_NAME}.cpp" @ONLY) -endfunction() diff --git a/cmake/embedded_resource.cpp.in b/cmake/embedded_resource.cpp.in deleted file mode 100644 index 549d0de..0000000 --- a/cmake/embedded_resource.cpp.in +++ /dev/null @@ -1,13 +0,0 @@ -// WARNING This file is auto-generated during configuration by the cmake -// function embed_resource(). Do not manually edit as changes will be lost - -#include "@EMBED_BASE_NAME@.hpp" - -namespace { -@EMBED_BYTE_ARRAYS@ -} - -bool @EMBED_NAMESPACE@::getEmbeddedFile(std::string const& fileName, std::ostream& stream) { -@EMBED_LOOKUPS@ - return false; -} diff --git a/cmake/embedded_resource.hpp.in b/cmake/embedded_resource.hpp.in deleted file mode 100644 index 7c90f42..0000000 --- a/cmake/embedded_resource.hpp.in +++ /dev/null @@ -1,10 +0,0 @@ -// WARNING This file is auto-generated during configuration by the cmake -// function embed_resource(). Do not manually edit as changes will be lost - -#pragma once -#include -#include - -namespace @EMBED_NAMESPACE@ { - bool getEmbeddedFile(std::string const& fileName, std::ostream& stream); -} diff --git a/cmake/generate_points_file.cmake b/cmake/generate_points_file.cmake new file mode 100644 index 0000000..845b5be --- /dev/null +++ b/cmake/generate_points_file.cmake @@ -0,0 +1,14 @@ +set(current_dir ${CMAKE_CURRENT_LIST_DIR}) + +function(generate_points_file infile outfile) + file(READ "${infile}" contents) + string(REGEX REPLACE "([^ \n]+) +([^ \n]+)\n" "{\\1, \\2},\n" POINTS + "${contents}") + + configure_file("${current_dir}/points_file.cpp.in" "${outfile}" @ONLY) + + set_property( + DIRECTORY + APPEND + PROPERTY CMAKE_CONFIGURE_DEPENDS "${infile}") +endfunction() diff --git a/cmake/points_file.cpp.in b/cmake/points_file.cpp.in new file mode 100644 index 0000000..f8ba136 --- /dev/null +++ b/cmake/points_file.cpp.in @@ -0,0 +1,13 @@ +// WARNING This file is auto-generated during configuration by the cmake +// function generate_points_file. Do not manually edit as changes will be lost + +#include + +namespace ear { + namespace points_file { + extern const double points[][2] = { + @POINTS@ + }; + extern const size_t num_points = sizeof(points) / sizeof(points[0]); + } +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2afa40f..218a081 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,9 @@ # --- ear library --- -include(embed_resource) -embed_resource( - NAMESPACE ear - BASE_NAME resources - RESOURCE_FILES "${PROJECT_SOURCE_DIR}/resources/Design_5200_100_random.dat") +include(generate_points_file) +generate_points_file( + "${PROJECT_SOURCE_DIR}/resources/Design_5200_100_random.dat" + ${CMAKE_CURRENT_BINARY_DIR}/hoa_points_file.cpp) add_library(ear bs2051.cpp @@ -32,14 +31,12 @@ add_library(ear object_based/polar_extent_simd.cpp object_based/gain_calculator_objects.cpp warnings.cpp - ${CMAKE_CURRENT_BINARY_DIR}/resources.hpp - ${CMAKE_CURRENT_BINARY_DIR}/resources.cpp + ${CMAKE_CURRENT_BINARY_DIR}/hoa_points_file.cpp ) target_include_directories(ear PRIVATE $ - $ # resources.hpp PUBLIC # Headers used from source/build location: $ diff --git a/src/hoa/hoa.cpp b/src/hoa/hoa.cpp index d4622db..9fc8a78 100644 --- a/src/hoa/hoa.cpp +++ b/src/hoa/hoa.cpp @@ -1,26 +1,15 @@ #include "hoa.hpp" -#include "ear/helpers/assert.hpp" -#include "resources.hpp" +#include "points_file.hpp" Eigen::Matrix ear::hoa::load_points() { - std::stringstream points_file; + Eigen::Matrix points(points_file::num_points, 3); - ear_assert(getEmbeddedFile("Design_5200_100_random.dat", points_file), - "could not find embedded file Design_5200_100_random.dat, this is " - "probably a compilation error"); - size_t len = 5200; - - Eigen::Matrix points(len, 3); - for (size_t i = 0; i < len; i++) { - double phi, theta; - points_file >> phi >> theta; + for (size_t i = 0; i < points_file::num_points; i++) { + double phi = points_file::points[i][0], theta = points_file::points[i][1]; points(i, 0) = std::sin(theta) * std::cos(phi); points(i, 1) = std::sin(theta) * std::sin(phi); points(i, 2) = std::cos(theta); } - ear_assert(points_file.good(), - "failure when reading HOA points from embedded file"); - return points; } diff --git a/src/hoa/points_file.hpp b/src/hoa/points_file.hpp new file mode 100644 index 0000000..3a8b15b --- /dev/null +++ b/src/hoa/points_file.hpp @@ -0,0 +1,11 @@ +#pragma once +#include + +namespace ear { + namespace points_file { + // defined in autogenerated .cpp file from + // resources/Design_5200_100_random.dat + extern const double points[][2]; + extern const size_t num_points; + } // namespace points_file +} // namespace ear