Skip to content

Commit

Permalink
replace generic resource embedder with const array
Browse files Browse the repository at this point in the history
this avoids pulling in iostream and float parsing
  • Loading branch information
tomjnixon committed Jan 29, 2024
1 parent 168a0b4 commit dd4f4ba
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 116 deletions.
70 changes: 0 additions & 70 deletions cmake/embed_resource.cmake

This file was deleted.

13 changes: 0 additions & 13 deletions cmake/embedded_resource.cpp.in

This file was deleted.

10 changes: 0 additions & 10 deletions cmake/embedded_resource.hpp.in

This file was deleted.

14 changes: 14 additions & 0 deletions cmake/generate_points_file.cmake
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions cmake/points_file.cpp.in
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

namespace ear {
namespace points_file {
extern const double points[][2] = {
@POINTS@
};
extern const size_t num_points = sizeof(points) / sizeof(points[0]);
}
}
13 changes: 5 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/submodules/kissfft>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # resources.hpp
PUBLIC
# Headers used from source/build location:
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
Expand Down
19 changes: 4 additions & 15 deletions src/hoa/hoa.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
#include "hoa.hpp"
#include "ear/helpers/assert.hpp"
#include "resources.hpp"
#include "points_file.hpp"

Eigen::Matrix<double, Eigen::Dynamic, 3> ear::hoa::load_points() {
std::stringstream points_file;
Eigen::Matrix<double, Eigen::Dynamic, 3> 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<double, Eigen::Dynamic, 3> 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;
}
11 changes: 11 additions & 0 deletions src/hoa/points_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <cstddef>

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

0 comments on commit dd4f4ba

Please sign in to comment.