//
// Created by Lenn on 2026/1/20.
//

#ifndef TACTILEIPC3D_GLOBALHELPER_H
#define TACTILEIPC3D_GLOBALHELPER_H

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <algorithm>

class GlobalHelper {
public:
    const std::string fuck = "fuck you lsp!!!";
    static GlobalHelper* Instance() {
        static GlobalHelper ins;
        return &ins;
    }

    static void transToMultiMatrix(const cv::Mat& raw_data, float min_threshold,
        float max_range, cv::Size display_res, cv::Mat& out_matrix) {
        CV_Assert(raw_data.type() == CV_32F);

        cv::Mat raw = raw_data.clone();
        raw.setTo(0.0f, raw < min_threshold);

        double maxVal = 0.0;
        cv::minMaxLoc(raw, nullptr, &maxVal);
        if (maxVal > 0.0) {
            cv::GaussianBlur(raw, raw, cv::Size(3, 3), 0.8, 0.8, cv::BORDER_DEFAULT);
        }

        const float safe_range = std::max(max_range, 1e-6f);
        cv::Mat norm_data = raw / safe_range;
        cv::min(norm_data, 1.0f, norm_data);
        cv::max(norm_data, 0.0f, norm_data);

        cv::pow(norm_data, 0.7, norm_data);

        cv::Mat smoothed;
        cv::resize(norm_data, smoothed, display_res, 0.0, 0.0, cv::INTER_CUBIC);

        cv::GaussianBlur(smoothed, smoothed, cv::Size(31, 31), 0.0, 0.0, cv::BORDER_DEFAULT);

        smoothed *= safe_range;
        out_matrix = smoothed;
    }
private:
    GlobalHelper() {}
};

#endif //TACTILEIPC3D_GLOBALHELPER_H
