Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing out .trl files on xml export #256

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
</MarkerCategory>

<POIs>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
</POIs>
</OverlayData>
57 changes: 56 additions & 1 deletion xml_converter/src/attribute/trail_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#include <stdint.h>

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>

#include "../packaging_xml.hpp"
#include "../rapid_helpers.hpp"
#include "../rapidxml-1.13/rapidxml.hpp"
#include "../string_helper.hpp"
#include "waypoint.pb.h"

using namespace std;
Expand Down Expand Up @@ -75,20 +78,72 @@ void xml_attribute_to_trail_data(
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
// djb2_hash
//
// A simple non cryptographic hash that we use to deterministically generate the
// filename of the trl files.
////////////////////////////////////////////////////////////////////////////////
uint64_t djb2_hash(const unsigned char* str, size_t length) {
uint64_t hash = 5381;
for (size_t i = 0; i < length; i++) {
hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */
}
return hash;
}

////////////////////////////////////////////////////////////////////////////////
// trail_data_to_xml_attribute
//
// Converts a traildata into a fully qualified xml attribute string.
// TODO: Write ".trl" files from data
// TOOD: Determine a better trail path name
////////////////////////////////////////////////////////////////////////////////
uint32_t trail_version_number = 0;
string trail_data_to_xml_attribute(
const string& attribute_name,
XMLWriterState* state,
const TrailData* value,
const int* map_id_value,
const bool* is_map_id_set) {
return " " + attribute_name + "=\"" + "temp_name_of_trail.trl" + "\"";
size_t byte_array_size = sizeof(int) + sizeof(uint32_t) + value->points_x.size() * 3 * sizeof(float);
unsigned char* byte_array = new unsigned char[byte_array_size];

size_t offset = 0;
std::memcpy(byte_array + offset, &trail_version_number, sizeof(trail_version_number));
offset += sizeof(trail_version_number);

std::memcpy(byte_array + offset, map_id_value, sizeof(*map_id_value));
offset += sizeof(*map_id_value);

for (size_t i = 0; i < value->points_x.size(); i++) {
std::memcpy(byte_array + offset, &value->points_x[i], sizeof(float));
offset += sizeof(float);
std::memcpy(byte_array + offset, &value->points_y[i], sizeof(float));
offset += sizeof(float);
std::memcpy(byte_array + offset, &value->points_z[i], sizeof(float));
offset += sizeof(float);
}

// Sanity check offset is where we think it should be.
if (offset != byte_array_size) {
cerr << "Found more data to write then we thought. This might mean there is a programming issue for serializing trl files." << endl;
cerr << "Found " << offset << " instead of " << byte_array_size << endl;
}

string trail_file_name = long_to_hex_string(djb2_hash(byte_array, byte_array_size)) + ".trl";
string trail_file_path = join_file_paths(state->filedir, trail_file_name);

ofstream trail_data_file(trail_file_path, ios::binary);

if (!trail_data_file.good()) {
cerr << "Error opening file. " << trail_file_path << endl;
}
trail_data_file.write(reinterpret_cast<const char*>(byte_array), byte_array_size);
trail_data_file.close();

delete[] byte_array;
return " " + attribute_name + "=\"" + trail_file_name + "\"";
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions xml_converter/src/packaging_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void write_xml_file(string xml_filepath, map<string, Category>* marker_categorie
string tab_string;

XMLWriterState state;
state.filedir = get_base_dir(xml_filepath);

outfile.open(xml_filepath, ios::out);

Expand Down
3 changes: 3 additions & 0 deletions xml_converter/src/state_structs/xml_writer_state.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once

#include <string>

struct XMLWriterState {
std::string filedir;
};
17 changes: 17 additions & 0 deletions xml_converter/src/string_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,20 @@ string join_file_paths(const string& path_a, const string& path_b) {
}
return output + path_b;
}

////////////////////////////////////////////////////////////////////////////////
// long_to_hex_string
//
// A helper function that converts an 8 byte long into a 16 byte hex string.
////////////////////////////////////////////////////////////////////////////////
const char* hex_chars = "0123456789abcdef";
std::string long_to_hex_string(uint64_t number) {
std::string hex_string(16, '0');

for (int i = 15; i >= 0; --i) {
hex_string[i] = hex_chars[number & 0xF];
number >>= 4;
}

return hex_string;
}
2 changes: 2 additions & 0 deletions xml_converter/src/string_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ std::vector<uint8_t> base64_decode(std::string const&);
std::string get_base_dir(std::string filepath);
bool has_suffix(std::string const& fullString, std::string const& ending);
std::string join_file_paths(const std::string& path_a, const std::string& path_b);

std::string long_to_hex_string(uint64_t number);