-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add examples/histogram * Add build CI. * Fix tests, update CMake presets. * Fix non-constexpr function.
- Loading branch information
Showing
9 changed files
with
200 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: ci | ||
on: [push] | ||
jobs: | ||
build-linux: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: init | ||
run: sudo apt update -yqq && sudo apt install -yqq ninja-build g++-13 clang-15 | ||
- name: configure gcc | ||
run: cmake -S . --preset=default -B build -DCMAKE_CXX_COMPILER=g++-13 | ||
- name: configure clang | ||
run: cmake -S . --preset=ninja-clang -B clang -DCMAKE_CXX_COMPILER=clang++-15 | ||
- name: build gcc | ||
run: cmake --build build --config=Release | ||
- name: build clang | ||
run: cmake --build clang --config=Release | ||
- name: test | ||
run: cd build && ctest -C Release | ||
build-windows: | ||
runs-on: windows-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: configure | ||
run: cmake -S . --preset=vs22 -B build | ||
- name: build | ||
run: cmake --build build --config=Release | ||
- name: test | ||
run: cd build && ctest -C Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(histogram) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
project(noiz-histogram) | ||
|
||
add_executable(${PROJECT_NAME}) | ||
|
||
target_link_libraries(${PROJECT_NAME} PRIVATE | ||
noiz::noiz-lib | ||
noiz::noiz-compile-options | ||
) | ||
|
||
target_sources(${PROJECT_NAME} PRIVATE | ||
histogram.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include <noiz/noise2.hpp> | ||
#include <charconv> | ||
#include <filesystem> | ||
#include <format> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace { | ||
template <typename Type> | ||
concept NumberT = std::integral<Type> || std::floating_point<Type>; | ||
|
||
template <NumberT Type> | ||
auto parse_as(Type& out, std::string_view const value) -> bool { | ||
if (value.empty()) { return false; } | ||
|
||
auto const* end = value.data() + value.size(); | ||
auto const [ptr, ec] = std::from_chars(value.data(), end, out); | ||
|
||
return ec == std::errc{} && ptr == end; | ||
} | ||
|
||
struct Args { | ||
std::span<char const* const> args{}; | ||
|
||
[[nodiscard]] constexpr auto next() -> std::string_view { | ||
if (args.empty()) { return {}; } | ||
auto const* ret = args.front(); | ||
args = args.subspan(1); | ||
return ret; | ||
} | ||
|
||
template <NumberT Type> | ||
auto next_as(Type& out, std::string_view const key) -> bool { | ||
auto value = next(); | ||
if (value.empty()) { return true; } | ||
|
||
if (!parse_as(out, value)) { | ||
std::cerr << std::format("invalid argument: '{}' for '{}'\n", value, key); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
struct Config { | ||
noiz::Seed seed{noiz::detail::Generator::make_random_seed()}; | ||
noiz::GridExtent2 grid_extent{50, 1}; // NOLINT | ||
int count{100}; // NOLINT | ||
float step{0.1f}; // NOLINT | ||
|
||
// syntax: [count] [step] | ||
auto parse_args(Args args) -> bool { | ||
if (!args.next_as(count, "count")) { return false; } | ||
if (!args.next_as(step, "step")) { return false; } | ||
if (!args.args.empty()) { | ||
std::cerr << std::format("unrecognized argument: '{}'\n", args.next()); | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
// contains logic to store data points and draw them | ||
class Histogram { | ||
public: | ||
explicit Histogram(std::size_t const levels = 10) : rows(levels) {} | ||
|
||
auto add(float const value) -> Histogram& { | ||
// compute height relative to number of rows/levels | ||
auto const height = static_cast<std::size_t>((value + 1.0f) * 0.5f * static_cast<float>(rows.size())); | ||
// add a new column | ||
for (std::size_t index = 0; index < rows.size(); ++index) { | ||
// set output "pixel" value (0 or 1) | ||
auto const ch = index > height ? symbol : background; | ||
// append to each row | ||
rows.at(index) += ch; | ||
} | ||
return *this; | ||
} | ||
|
||
[[nodiscard]] auto build() const -> std::string { | ||
auto ret = std::string{}; | ||
for (auto const& row : rows) { | ||
ret += row; | ||
ret += '\n'; | ||
} | ||
return ret; | ||
} | ||
|
||
char symbol{'x'}; | ||
char background{'-'}; | ||
|
||
private: | ||
std::vector<std::string> rows{}; | ||
}; | ||
} // namespace | ||
|
||
auto main(int argc, char** argv) -> int { | ||
auto config = Config{}; | ||
auto noise = noiz::Noise2f{noiz::Seed{config.seed}, config.grid_extent}; | ||
|
||
// skip exe name (argv[0]) | ||
auto const args = Args{std::span{argv, static_cast<std::size_t>(argc)}.subspan(1)}; | ||
|
||
// handle --help | ||
if (!args.args.empty() && args.args.front() == std::string_view{"--help"}) { | ||
std::cout << std::format("Usage: {} [count(=100)] [step(=0.1)]\n", std::filesystem::path{*argv}.stem().string()); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
// parse args, if any | ||
if (!config.parse_args(args)) { return EXIT_FAILURE; } | ||
|
||
// build histogram | ||
auto histogram = Histogram{}; | ||
for (int i = 0; i < config.count; ++i) { | ||
// build point on line (y = 0) | ||
auto const point = noiz::Vec2f{.x = static_cast<float>(i) * config.step}; | ||
// add noise at point | ||
histogram.add(noise.at(point)); | ||
} | ||
|
||
// output histogram | ||
std::cout << histogram.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters