16 size_t pos = content.find(attr_name +
"=\"");
17 if (pos == std::string::npos)
20 size_t start = pos + attr_name.length() + 2;
21 size_t end = content.find(
"\"", start);
22 if (end == std::string::npos)
25 return content.substr(start, end - start);
29 std::ifstream file(filename, std::ios::binary);
30 if (!file.is_open()) {
31 throw std::runtime_error(
"Could not open file: " + filename);
52 std::string header_buffer;
54 bool found_marker =
false;
71 std::vector<char> buffer(8192);
72 file.read(buffer.data(), buffer.size());
73 std::string header(buffer.data(), file.gcount());
77 if (extent_str.empty())
78 throw std::runtime_error(
"Could not find WholeExtent in VTI header");
80 int x1, x2, y1, y2, z1, z2;
81 std::stringstream ss_ext(extent_str);
82 ss_ext >> x1 >> x2 >> y1 >> y2 >> z1 >> z2;
83 data.
resolution = {x2 - x1 + 1, y2 - y1 + 1, z2 - z1 + 1};
87 std::stringstream ss_org(origin_str);
92 std::stringstream ss_spc(spacing_str);
97 std::string appended_tag =
"<AppendedData encoding=\"raw\">";
98 size_t tag_pos = header.find(appended_tag);
99 if (tag_pos == std::string::npos)
100 throw std::runtime_error(
"Could not find AppendedData tag");
105 size_t underscore_pos = header.find(
"_", tag_pos);
106 if (underscore_pos == std::string::npos) {
110 throw std::runtime_error(
"Could not find binary data marker '_' in first 8KB");
115 file.seekg(underscore_pos + 1, std::ios::beg);
118 uint64_t data_bytes = 0;
119 file.read(
reinterpret_cast<char *
>(&data_bytes),
sizeof(uint64_t));
123 throw std::runtime_error(
"Failed to read data_bytes (UInt64) from file.");
126 std::cout <<
"Read data length: " << data_bytes << std::endl;
131 size_t expected_bytes = data.
size() *
sizeof(float);
132 if (data_bytes != expected_bytes) {
133 std::cerr <<
"Warning: Data length in file (" << data_bytes
134 <<
") does not match expected size from resolution (" << expected_bytes <<
")."
144 file.read(
reinterpret_cast<char *
>(data.
sdf_values.data()), data_bytes);
146 if (file.gcount() !=
static_cast<std::streamsize
>(data_bytes)) {
147 std::cerr <<
"Warning: Could not read all data. Read " << file.gcount() <<
" bytes."