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

updated file format v4.1 #1249

Merged
merged 5 commits into from
Jun 20, 2021
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ tests2/
.vs
testsr/
site/
tests/sculpting1
tests/cities1
tests/_version40/sculpting1
tests/_version40/cities1
9 changes: 9 additions & 0 deletions libs/yocto/yocto_commonio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,15 @@ static void _json_format_element(
case json_type::array: {
if (json.empty()) {
_json_write_chars(text, "[]", 2);
} else if (!json.at(0).is_array() && !json.at(0).is_object()) {
_json_write_chars(text, "[ ", 2);
auto count = (size_t)0, size = json.size();
for (auto& item : json) {
_json_format_element(text, item, indent + 2);
count += 1;
if (count < size) _json_write_chars(text, ", ", 2);
}
_json_write_chars(text, " ]", 2);
} else {
_json_write_chars(text, "[\n", 2);
auto count = (size_t)0, size = json.size();
Expand Down
49 changes: 39 additions & 10 deletions libs/yocto/yocto_commonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct io_error : std::runtime_error {
static io_error read_error(const string& filename) { return {filename, "read error"}; }
static io_error write_error(const string& filename) { return {filename, "write error"}; }
static io_error parse_error(const string& filename) { return {filename, "parse error"}; }
static io_error parse_error(const string& filename, const string& path) { return {filename, "parse error at " + path}; }
static io_error format_error(const string& filename) { return {filename, "unknown format"}; }
static io_error preset_error(const string& filename) { return {filename, "unknown preset"}; }
static io_error shape_error(const string& filename) { return {filename, "empty shape"}; }
Expand Down Expand Up @@ -269,6 +270,7 @@ struct json_error : std::logic_error {
const json_value* _where = nullptr;
json_error(const string& error, const json_value* where)
: std::logic_error(error), _where(where) {}
const json_value* where() const { return _where; }
};

// Json enum support
Expand Down Expand Up @@ -533,30 +535,57 @@ struct json_value {
#endif

// math types
// clang-format off
void get(vec2i& value) const { get((std::array<int, 2>&)value); }
void get(vec3i& value) const { get((std::array<int, 3>&)value); }
void get(vec4i& value) const { get((std::array<int, 4>&)value); }
void get(vec2f& value) const { get((std::array<float, 2>&)value); }
void get(vec3f& value) const { get((std::array<float, 3>&)value); }
void get(vec4f& value) const { get((std::array<float, 4>&)value); }
void get(frame2f& value) const { get((std::array<float, 6>&)value); }
void get(frame3f& value) const { get((std::array<float, 12>&)value); }
void get(mat2f& value) const { get((std::array<float, 4>&)value); }
void get(mat3f& value) const { get((std::array<float, 9>&)value); }
void get(mat4f& value) const { get((std::array<float, 16>&)value); }
void get(frame2f& value) const { get((std::array<std::array<float, 2>, 3>&)value); }
void get(frame3f& value) const { get((std::array<std::array<float, 3>, 4>&)value); }
void get(mat2f& value) const { get((std::array<std::array<float, 2>, 2>&)value); }
void get(mat3f& value) const { get((std::array<std::array<float, 3>, 3>&)value); }
void get(mat4f& value) const { get((std::array<std::array<float, 4>, 4>&)value); }
// clang-format on

// math types
// clang-format off
void set(const vec2i& value) { set((const std::array<int, 2>&)value); }
void set(const vec3i& value) { set((const std::array<int, 3>&)value); }
void set(const vec4i& value) { set((const std::array<int, 4>&)value); }
void set(const vec2f& value) { set((const std::array<float, 2>&)value); }
void set(const vec3f& value) { set((const std::array<float, 3>&)value); }
void set(const vec4f& value) { set((const std::array<float, 4>&)value); }
void set(const frame2f& value) { set((const std::array<float, 6>&)value); }
void set(const frame3f& value) { set((const std::array<float, 12>&)value); }
void set(const mat2f& value) { set((const std::array<float, 4>&)value); }
void set(const mat3f& value) { set((const std::array<float, 9>&)value); }
void set(const mat4f& value) { set((const std::array<float, 16>&)value); }
void set(const frame2f& value) { set((const std::array<std::array<float, 2>, 3>&)value); }
void set(const frame3f& value) { set((const std::array<std::array<float, 3>, 4>&)value); }
void set(const mat2f& value) { set((const std::array<std::array<float, 2>, 2>&)value); }
void set(const mat3f& value) { set((const std::array<std::array<float, 3>, 3>&)value); }
void set(const mat4f& value) { set((const std::array<std::array<float, 4>, 4>&)value); }
// clang-format on

// get path string (useful for error)
static string get_path(const json_value* root, const json_value* value) {
if (root == value) {
return "/";
} else if (root->is_array()) {
for (auto&& [index, item] : enumerate(root->_get_array())) {
if (&item == value) return "/" + std::to_string(index);
if (auto path = get_path(&item, value); !path.empty())
return "/" + std::to_string(index) + path;
}
return "";
} else if (root->is_object()) {
for (auto& [key, item] : root->_get_object()) {
if (&item == value) return "/" + key;
if (auto path = get_path(&item, value); !path.empty())
return "/" + key + path;
}
return "";
} else {
return "";
}
}

private:
json_type _type = json_type::null;
Expand Down
Loading