24namespace localization {
31 : map_matrix_(matrix), compression_strategy_(strategy) {}
50 if (create_map_cells) {
84 if (!EnsureDirectory(path)) {
88 if (!EnsureDirectory(path)) {
93 if (!EnsureDirectory(path)) {
97 path = path +
"/north";
99 path = path +
"/south";
101 if (!EnsureDirectory(path)) {
106 if (!EnsureDirectory(path)) {
109 snprintf(buf,
sizeof(buf),
"/%08u",
index_.
m_);
111 if (!EnsureDirectory(path)) {
114 snprintf(buf,
sizeof(buf),
"/%08u",
index_.
n_);
117 AINFO <<
"Save node: " << path;
119 FILE* file = fopen(path.c_str(),
"wb");
126 AERROR <<
"Can't write to file: " << path <<
".";
134 if (!DirectoryExists(path)) {
137 path = path +
"/map";
138 if (!DirectoryExists(path)) {
143 if (!DirectoryExists(path)) {
147 path = path +
"/north";
149 path = path +
"/south";
151 if (!DirectoryExists(path)) {
156 if (!DirectoryExists(path)) {
159 snprintf(buf,
sizeof(buf),
"/%08u",
index_.
m_);
161 if (!DirectoryExists(path)) {
164 snprintf(buf,
sizeof(buf),
"/%08u",
index_.
n_);
167 return Load(path.c_str());
174 FILE* file = fopen(filename,
"rb");
182 AERROR <<
"Can't find the file: " << filename;
190 std::vector<unsigned char> buf(header_size);
191 size_t read_size = fread(&buf[0], 1, header_size, file);
192 CHECK_EQ(read_size, header_size);
194 CHECK_EQ(processed_size, header_size);
201 return processed_size;
206 std::vector<unsigned char> buffer;
207 buffer.resize(buf_size);
209 unsigned int binary_size = 0;
210 std::vector<unsigned char> body_buffer;
217 CHECK_EQ(processed_size, header_size);
219 unsigned int buffer_bias = processed_size;
220 buf_size -= processed_size;
221 binary_size += processed_size;
223 CHECK_GE(buf_size, body_buffer.size());
224 memcpy(&buffer[buffer_bias], &body_buffer[0], body_buffer.size());
225 binary_size +=
static_cast<unsigned int>(body_buffer.size());
226 fwrite(&buffer[0], 1, binary_size, file);
237 unsigned int* p =
reinterpret_cast<unsigned int*
>(buf);
240 int* pp =
reinterpret_cast<int*
>(p);
243 p =
reinterpret_cast<unsigned int*
>(pp);
255 unsigned int buf_size)
const {
257 if (buf_size >= target_size) {
258 unsigned int* p =
reinterpret_cast<unsigned int*
>(buf);
261 int* pp =
reinterpret_cast<int*
>(p);
264 p =
reinterpret_cast<unsigned int*
>(pp);
275 return static_cast<int>(
sizeof(
unsigned int)
277 +
sizeof(
unsigned int)
278 +
sizeof(
unsigned int)
279 +
sizeof(
unsigned int));
298 std::vector<unsigned char> buf_uncompressed;
301 AERROR <<
"compression Decode error: " << ret;
304 AERROR <<
"map node compress ratio: "
305 <<
static_cast<float>(buf->size()) /
306 static_cast<float>(buf_uncompressed.size());
311 std::vector<unsigned char>* buf)
const {
314 buf->resize(body_size);
317 return static_cast<unsigned int>(buf->size());
319 std::vector<unsigned char> buf_uncompressed;
322 buf_uncompressed.resize(body_size);
326 return static_cast<unsigned int>(buf->size());
353 unsigned int* x,
unsigned int* y)
const {
355 int off_x =
static_cast<int>((coordinate[0] - left_top_corner[0]) /
357 int off_y =
static_cast<int>((coordinate[1] - left_top_corner[1]) /
363 *x =
static_cast<unsigned int>(off_x);
364 *y =
static_cast<unsigned int>(off_y);
380 unsigned int* x,
unsigned int* y)
const {
381 Eigen::Vector2d coord2d(coordinate[0], coordinate[1]);
395 unsigned int y)
const {
397 Eigen::Vector2d coord(
417 Eigen::Vector2d coord;
421 static_cast<float>(index.
n_);
425 static_cast<float>(index.
m_);
434 if (!EnsureDirectory(path)) {
437 path = path +
"/image";
438 if (!EnsureDirectory(path)) {
443 if (!EnsureDirectory(path)) {
447 path = path +
"/north";
449 path = path +
"/south";
451 if (!EnsureDirectory(path)) {
456 if (!EnsureDirectory(path)) {
459 snprintf(buf,
sizeof(buf),
"/%08u",
index_.
m_);
461 if (!EnsureDirectory(path)) {
464 snprintf(buf,
sizeof(buf),
"/%08u.png",
index_.
n_);
473 bool success = cv::imwrite(path, image);
The options of the reflectance map.
std::vector< float > map_resolutions_
The pixel resolutions in the map in meters.
std::string map_folder_path_
The map folder path.
Rect2D< double > map_range_
The minimum and maximum UTM range in the map.
unsigned int map_node_size_y_
The map node size in pixels.
unsigned int map_node_size_x_
The map node size in pixels.
The data structure of the map cells in a map node.
virtual unsigned int CreateBinary(unsigned char *buf, unsigned int buf_size) const =0
Create the binary.
virtual void GetIntensityImg(cv::Mat *intensity_img) const =0
get intensity image of node.
virtual void Reset(const BaseMapConfig *config)=0
Reset map cells data.
virtual unsigned int GetBinarySize() const =0
Get the binary size of the object.
virtual unsigned int LoadBinary(unsigned char *buf)=0
Load the map cell from a binary chunk.
virtual void Init(const BaseMapConfig *config)=0
Initialize the map matrix.
const Eigen::Vector2d & GetLeftTopCorner() const
Get the left top corner of the map node.
virtual unsigned int CreateBinary(FILE *file) const
Create the binary.
BaseMapMatrix * map_matrix_
The data structure of the map datas, which is a matrix.
virtual void Init(const BaseMapConfig *map_config, const MapNodeIndex &index, bool create_map_cells=true)
Initialize the map node.
MapNodeIndex index_
The index of this node.
unsigned int file_body_binary_size_
The body binary size in file.
BaseMapNode(BaseMapMatrix *matrix, CompressionStrategy *strategy)
Construct a map node.
virtual void ResetMapNode()
Reset map cells data.
virtual unsigned int LoadBinary(FILE *file)
Load the map cell from a binary chunk.
bool SaveIntensityImage() const
Save intensity image of node.
virtual unsigned int CreateHeaderBinary(unsigned char *buf, unsigned int buf_size) const
Create the binary header.
virtual void InitMapMatrix(const BaseMapConfig *map_config)
Initialize the map matrix.
bool is_changed_
Has the map node been changed.
virtual ~BaseMapNode()
Destruct a map node.
virtual void Finalize()
call before deconstruction or reset.
bool is_reserved_
If the node is reserved in map.
CompressionStrategy * compression_strategy_
@bried The compression strategy.
bool Load()
Load the map node from the disk.
bool Save()
Save the map node to the disk.
virtual unsigned int LoadHeaderBinary(unsigned char *buf)
Load the map node header from a binary chunk.
virtual unsigned int GetBodyBinarySize() const
Get the size of the body in bytes.
const BaseMapConfig * map_config_
The map settings.
virtual unsigned int LoadBodyBinary(std::vector< unsigned char > *buf)
Load the map node body from a binary chunk.
virtual unsigned int CreateBodyBinary(std::vector< unsigned char > *buf) const
Create the binary body.
Eigen::Vector2d left_top_corner_
The left top corner of the map node in the global coordinate system.
float GetMapResolution() const
Get the resolution of this map nodex.
virtual unsigned int GetBinarySize() const
Get the binary size of the object.
bool GetCoordinate(const Eigen::Vector2d &coordinate, unsigned int *x, unsigned int *y) const
Given the global coordinate, get the local 2D coordinate of the map cell matrix.
virtual unsigned int GetHeaderBinarySize() const
Get the size of the header in bytes.
virtual int Decode(BufferStr *buf, BufferStr *buf_uncompressed)=0
virtual int Encode(BufferStr *buf, BufferStr *buf_compressed)=0
unsigned int m_
The map node ID at the northing direction.
unsigned int n_
The map node ID at the easting direction.
unsigned int resolution_id_
The ID of the resolution.
T GetMaxY() const
Get the max y of the rectangle.
T GetMaxX() const
Get the max x of the rectangle.
T GetMinX() const
Get the min x of the rectangle.
T GetMinY() const
Get the min y of the rectangle.
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.