From 3f16f0b9304014c2e28bb35284fbbe3ab102f947 Mon Sep 17 00:00:00 2001 From: chn Date: Thu, 28 Sep 2023 10:50:38 +0800 Subject: [PATCH] bug fix --- flake.lock | 6 +++--- flake.nix | 9 +++------ include/ufo/ufo.hpp | 2 ++ include/ufo/ufo.impl.hpp | 15 +++++++++++---- src/plot.cpp | 31 +++++++++++++++++++++++++++---- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/flake.lock b/flake.lock index 76e2f3f..29b18a1 100644 --- a/flake.lock +++ b/flake.lock @@ -807,11 +807,11 @@ "touchix": "touchix" }, "locked": { - "lastModified": 1695703232, - "narHash": "sha256-JqKKV5Zh3r4SAmK1Y0WdZaN5chRuaWRxw5hv9YPCRlQ=", + "lastModified": 1695866766, + "narHash": "sha256-EdTNjwCmgug/NXdxGsLapSOpwJebf0ruU5qkmXjY9eg=", "owner": "CHN-beta", "repo": "nixos", - "rev": "6318b938c2b3742874982d5d1a338656a50e493b", + "rev": "1957d682473ee67f96ace3ce3320c89b77aec8ae", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 2ab5b6e..4256836 100644 --- a/flake.nix +++ b/flake.nix @@ -16,12 +16,9 @@ devShell.x86_64-linux = pkgs.mkShell.override { stdenv = pkgs.gcc13Stdenv; } { packages = with pkgs; [ pkg-config cmake ninja ]; - buildInputs = with pkgs; - [ - yaml-cpp eigen fmt localPackages.concurrencpp highfive tbb_2021_8.dev localPackages.matplotplusplus - localPackages.zpp-bits - ]; - hardeningDisable = [ "all" ]; + buildInputs = (with pkgs; [ eigen yaml-cpp fmt highfive tbb_2021_8.dev ]) + ++ (with localPackages; [ concurrencpp matplotplusplus zpp-bits ]); + # hardeningDisable = [ "all" ]; # NIX_DEBUG = "1"; }; }; diff --git a/include/ufo/ufo.hpp b/include/ufo/ufo.hpp index ceb16e9..0fe548e 100644 --- a/include/ufo/ufo.hpp +++ b/include/ufo/ufo.hpp @@ -17,8 +17,10 @@ # include # include # include +# include # include # include +# include using namespace std::literals; diff --git a/include/ufo/ufo.impl.hpp b/include/ufo/ufo.impl.hpp index e3282c4..12a8be1 100644 --- a/include/ufo/ufo.impl.hpp +++ b/include/ufo/ufo.impl.hpp @@ -239,11 +239,18 @@ inline void Output::write(std::string filename, std::string format, unsigned per inline Output::Output(std::string filename) { - auto [data, in] = zpp::bits::data_in(); - auto input = std::basic_ifstream - (filename, std::ios::binary | std::ios::in); + auto input = std::ifstream(filename, std::ios::binary | std::ios::in); input.exceptions(std::ios::badbit | std::ios::failbit); - data.assign(std::istreambuf_iterator(input), {}); + std::vector data; + { + std::vector string(std::istreambuf_iterator(input), {}); + data.assign + ( + reinterpret_cast(string.data()), + reinterpret_cast(string.data() + string.size()) + ); + } + auto in = zpp::bits::in(data); in(*this).or_throw(); } diff --git a/src/plot.cpp b/src/plot.cpp index e04e117..894b71a 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -90,25 +90,48 @@ int main(int argc, char** argv) auto current_distance = i * total_distance / 1024; auto& _ = interpolated_points.emplace_back(); _.Distance = current_distance; + auto it = std::lower_bound(Points.begin(), Points.end(), current_distance, + [](const Point& a, double b) { return a.Distance < b; }); // 如果是开头或者结尾, 直接赋值, 否则插值 - if (current_distance < Points.front().Distance) + if (it == Points.begin()) { _.Frequency = Points.front().Frequency; _.Weight = Points.front().Weight; } - else if (current_distance > Points.back().Distance) + else if (it == Points.end() - 1) { _.Frequency = Points.back().Frequency; _.Weight = Points.back().Weight; } else { - auto it = std::lower_bound(Points.begin(), Points.end(), current_distance, - [](const Point& a, double b) { return a.Distance < b; }); _.Frequency = (it->Frequency * (it->Distance - current_distance) + (it - 1)->Frequency * (current_distance - (it - 1)->Distance)) / (it->Distance - (it - 1)->Distance); _.Weight = (it->Weight * (it->Distance - current_distance) + (it - 1)->Weight * (current_distance - (it - 1)->Distance)) / (it->Distance - (it - 1)->Distance); } } + + // 将结果对应到像素上的值 + std::vector> weight(400, std::vector(1024, 0)); + for (auto& point : interpolated_points) + { + int x = point.Distance / total_distance * 1024; + if (x < 0) + x = 0; + else if (x >= 1024) + x = 1023; + for (unsigned i = 0; i < point.Frequency.size(); i++) + { + auto y = (point.Frequency(i) + 5) * 10; + if (y < 0) + y = 0; + else if (y >= 400) + y = 399; + weight[y][x] += point.Weight(i); + } + } + + matplot::image(weight); + matplot::show(); }