parent
7a9c689e89
commit
f389ec6211
@ -0,0 +1,61 @@
|
||||
#include "Arith_FrontProj.h"
|
||||
#include "Arith_Utils.h"
|
||||
#include "StitchStruct.h"
|
||||
#include "Arith_GeoSolver.h"
|
||||
Pole getPoleFromImgWithH(cv::Mat H, cv::Point2f pt, float dep)
|
||||
{
|
||||
// 投影到地面坐标,这一步可以利用前面优化成果
|
||||
cv::Point2f grdPt = warpPointWithH(H, pt);
|
||||
|
||||
// 补深度信息并转为常用的NUE坐标系
|
||||
PointXYZ grdPtXYZ = { 0 };
|
||||
grdPtXYZ.X = grdPt.y;
|
||||
grdPtXYZ.Y = -dep;
|
||||
grdPtXYZ.Z = grdPt.x;
|
||||
|
||||
// 地面点转极坐标
|
||||
Pole pole = getPoleFromXYZ(grdPtXYZ);
|
||||
|
||||
return pole;
|
||||
|
||||
}
|
||||
|
||||
cv::Point2f getImgPosFromPole(cv::Mat H_inv, Pole _pole, float dep)
|
||||
{
|
||||
PointXYZ virPt = getXYZFromPole(_pole);
|
||||
|
||||
//虚拟点投影到地面
|
||||
float ratio = -dep / virPt.Y;
|
||||
PointXYZ realPt = { 0 };
|
||||
realPt.X = virPt.X * ratio;
|
||||
realPt.Y = virPt.Y * ratio;
|
||||
realPt.Z = virPt.Z * ratio;
|
||||
|
||||
|
||||
// 转东北地
|
||||
PointXYZ realPtGeo = { 0 };
|
||||
realPtGeo.X = realPt.Z;
|
||||
realPtGeo.Y = realPt.X;
|
||||
realPtGeo.Z = -realPt.Y;
|
||||
|
||||
// 投影回像方
|
||||
cv::Point2f px = warpPointWithH(H_inv, cv::Point2f(realPtGeo.X, realPtGeo.Y));
|
||||
|
||||
return px;
|
||||
}
|
||||
|
||||
Pole getPoleFromFPan(cv::Point2f pt, FPanInfo _panPara)
|
||||
{
|
||||
Pole _pole = { 0 };
|
||||
_pole.beta = _panPara.center.fAz + (pt.x - _panPara.m_pan_width / 2) * _panPara.fAglRes;
|
||||
_pole.alpha = _panPara.center.fPt + (_panPara.m_pan_height / 2 - pt.y) * _panPara.fAglRes;
|
||||
return _pole;
|
||||
}
|
||||
|
||||
cv::Point2f getFPanFromPole(Pole _pole, FPanInfo _panPara)
|
||||
{
|
||||
cv::Point2f pt = { 0 };
|
||||
pt.x = DEGLIM(_pole.beta - _panPara.center.fAz) / _panPara.fAglRes + _panPara.m_pan_width / 2;
|
||||
pt.y = DEGLIM(_panPara.center.fPt - _pole.alpha) / _panPara.fAglRes + _panPara.m_pan_height / 2;
|
||||
return pt;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#include "Arith_CoordModule.h"
|
||||
#include "Arith_SysStruct.h"
|
||||
#include "opencv2/opencv.hpp"
|
||||
#include "StitchStruct.h"
|
||||
|
||||
// 前视扫描投影变换模型
|
||||
|
||||
Pole getPoleFromImgWithH(cv::Mat H, cv::Point2f pt, float dep);
|
||||
cv::Point2f getImgPosFromPole(cv::Mat H_inv, Pole _pole, float dep);
|
||||
Pole getPoleFromFPan(cv::Point2f pt, FPanInfo _panPara);
|
||||
cv::Point2f getFPanFromPole(Pole _pole, FPanInfo _panPara);
|
@ -1,229 +0,0 @@
|
||||
#include "FileCache.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <stdexcept>
|
||||
#include "StitchStruct.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "openssl/sha.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
FileCache::FileCache(const std::string& cache_dir, size_t max_memory_items)
|
||||
: cache_dir_(cache_dir), max_memory_items_(max_memory_items)
|
||||
{
|
||||
fs::create_directories(cache_dir_);
|
||||
}
|
||||
|
||||
FileCache::~FileCache()
|
||||
{
|
||||
// Clean up memory-mapped files
|
||||
for (auto& [key, mapped_file] : mapped_files_)
|
||||
{
|
||||
unmap_file(&mapped_file, sizeof(FrameCache));
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileCache::hash_key(const KeyType& key) const
|
||||
{
|
||||
// Convert key to a string representation
|
||||
std::string key_str = std::to_string(key);
|
||||
|
||||
// Compute SHA-256 hash
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
SHA256_CTX sha256;
|
||||
SHA256_Init(&sha256);
|
||||
SHA256_Update(&sha256, key_str.c_str(), key_str.size());
|
||||
SHA256_Final(hash, &sha256);
|
||||
|
||||
// Convert hash to a hex string
|
||||
std::ostringstream hex_stream;
|
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
hex_stream << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
|
||||
}
|
||||
|
||||
return hex_stream.str();
|
||||
}
|
||||
|
||||
std::string FileCache::get_file_path(const KeyType& key) const
|
||||
{
|
||||
std::string hashed = hash_key(key);
|
||||
return cache_dir_ + "/" + hashed.substr(2) + ".cache";
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::get(KeyType key,FrameCache* pData)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
if (memory_cache_.count(key))
|
||||
{
|
||||
memcpy(pData,&memory_cache_[key],sizeof(FrameCache));
|
||||
std::cout << "get from memory_cache_:" << pData->_para.nFrmID << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mapped_files_.count(key))
|
||||
{
|
||||
memcpy(pData,&mapped_files_[key],sizeof(FrameCache));
|
||||
std::cout << "get from mapped_files_:" << pData->_para.nFrmID << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
auto file_path = get_file_path(key);
|
||||
return load_from_disk(file_path,key,pData);
|
||||
}
|
||||
|
||||
|
||||
void FileCache::set(KeyType key, FrameCache* pData)
|
||||
{
|
||||
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
|
||||
memcpy(&memory_cache_[key],pData,sizeof(FrameCache));
|
||||
|
||||
std::cout << "set memory_cache:" << memory_cache_[key]._para.nFrmID <<std::endl;
|
||||
|
||||
// Evict if necessary
|
||||
if (memory_cache_.size() > max_memory_items_)
|
||||
{
|
||||
memory_cache_.erase(memory_cache_.begin());
|
||||
}
|
||||
|
||||
|
||||
// Async save to disk
|
||||
auto file_path = get_file_path(key);
|
||||
|
||||
//save_to_disk(file_path, pData);
|
||||
async_tasks_.push_back(std::async(std::launch::async, [this, file_path, pData]() {
|
||||
save_to_disk(file_path, pData);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::save_to_disk(const std::string& file_path, FrameCache* data)
|
||||
{
|
||||
std::ofstream out(file_path, std::ios::binary);
|
||||
if (!out) return false;
|
||||
|
||||
auto nsize = sizeof(FrameCache);
|
||||
|
||||
//std::cout << "set file:" << data->_para.nFrmID <<std::endl;
|
||||
|
||||
out.write(reinterpret_cast<const char*>(data), sizeof(FrameCache));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::load_from_disk(const std::string& file_path,const KeyType& key,FrameCache* pData)
|
||||
{
|
||||
if (!fs::exists(file_path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* str = file_path.c_str();
|
||||
|
||||
size_t file_size = fs::file_size(file_path);
|
||||
void* mapped_data = map_file(file_path, file_size);
|
||||
if (!mapped_data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store the mapped file for later use
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
|
||||
FrameCache* ptr = &mapped_files_[key];
|
||||
|
||||
// 存储到文件映射中
|
||||
memcpy(ptr,mapped_data,sizeof(FrameCache));
|
||||
|
||||
// 返回
|
||||
memcpy(pData,mapped_data,sizeof(FrameCache));
|
||||
|
||||
|
||||
|
||||
std::cout << "get from raw_files_:" << pData->_para.nFrmID << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileCache::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_.clear();
|
||||
for (const auto& [key, mapped_file] : mapped_files_)
|
||||
{
|
||||
unmap_file((void*)&mapped_file, sizeof(FrameCache));
|
||||
}
|
||||
mapped_files_.clear();
|
||||
for (const auto& entry : fs::directory_iterator(cache_dir_))
|
||||
{
|
||||
fs::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
SINT32 FileCache::get_mem_size()
|
||||
{
|
||||
return memory_cache_.size();
|
||||
}
|
||||
|
||||
SINT32 FileCache::get_file_size()
|
||||
{
|
||||
return mapped_files_.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* FileCache::map_file(const std::string& file_path, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HANDLE file = CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (file == INVALID_HANDLE_VALUE) return nullptr;
|
||||
|
||||
HANDLE mapping = CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
||||
if (!mapping)
|
||||
{
|
||||
CloseHandle(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
|
||||
CloseHandle(mapping);
|
||||
CloseHandle(file);
|
||||
return data;
|
||||
#else
|
||||
int fd = open(file_path.c_str(), O_RDONLY);
|
||||
if (fd == -1) return nullptr;
|
||||
|
||||
void* data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
return nullptr;
|
||||
}
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileCache::unmap_file(void* data, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
UnmapViewOfFile(data);
|
||||
#else
|
||||
munmap(data, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1,56 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "StitchStruct.h"
|
||||
|
||||
typedef int KeyType;
|
||||
|
||||
class FileCache
|
||||
{
|
||||
public:
|
||||
FileCache(const std::string& cache_dir = "./tile_cache", size_t max_memory_items = 200);
|
||||
~FileCache();
|
||||
|
||||
|
||||
bool get(KeyType key,FrameCache* pData);
|
||||
|
||||
void set(KeyType key, FrameCache* pData);
|
||||
|
||||
void clear();
|
||||
|
||||
SINT32 get_mem_size();
|
||||
SINT32 get_file_size();
|
||||
|
||||
private:
|
||||
std::string get_file_path(const KeyType& key) const;
|
||||
std::string hash_key(const KeyType& key) const;
|
||||
|
||||
bool save_to_disk(const std::string& file_path, FrameCache* data);
|
||||
|
||||
|
||||
bool load_from_disk(const std::string& file_path,const KeyType& key,FrameCache* pData);
|
||||
|
||||
std::string cache_dir_;
|
||||
size_t max_memory_items_;
|
||||
|
||||
// Memory cache
|
||||
mutable std::mutex cache_mutex_;
|
||||
|
||||
std::unordered_map<KeyType, FrameCache> memory_cache_;
|
||||
|
||||
|
||||
std::unordered_map<KeyType, FrameCache> mapped_files_;
|
||||
|
||||
// Async tasks
|
||||
std::vector<std::future<void>> async_tasks_;
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* map_file(const std::string& file_path, size_t size);
|
||||
void unmap_file(void* data, size_t size);
|
||||
};
|
Loading…
Reference in new issue