Skip to content

Commit 7d32c63

Browse files
fix serialization and more string constants (#72)
* fix serialization and more string constants * (fix) force string type on ident serialization * update precommit and fix formatting * build ci multi-threaded
1 parent dc55793 commit 7d32c63

File tree

10 files changed

+54
-19
lines changed

10 files changed

+54
-19
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
3636
-DCMAKE_C_COMPILER_LAUNCHER=ccache
3737
- name: Build
38-
run: cmake --build ${{github.workspace}}/build --config Release
38+
run: cmake --build ${{github.workspace}}/build --config Release -j
3939
- name: Test
4040
working-directory: ${{github.workspace}}/build
4141
run: ctest -C Release

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exclude: python/tests/resources|.clang-format
55
repos:
66
- repo: https://github.com/astral-sh/ruff-pre-commit
77
# Ruff version.
8-
rev: v0.9.2
8+
rev: v0.14.2
99
hooks:
1010
- id: ruff
1111
args: [--select, I, --fix]
@@ -14,11 +14,11 @@ repos:
1414
# Run the formatter.
1515
- id: ruff-format
1616
- repo: https://github.com/pre-commit/mirrors-clang-format
17-
rev: v14.0.6
17+
rev: v21.1.2
1818
hooks:
1919
- id: clang-format
2020
- repo: https://github.com/pre-commit/pre-commit-hooks
21-
rev: v3.2.0
21+
rev: v6.0.0
2222
hooks:
2323
- id: trailing-whitespace
2424
- id: end-of-file-fixer

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_EXTENSIONS OFF)
77

88
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
9-
add_compile_options(-Wall -Wextra)
9+
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

1212
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)

include/spark_dsg/serialization/versioning.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace spark_dsg::io {
4545

4646
// Define the current project and version when serializing data with this
4747
// implementation.
48-
inline const std::string CURRENT_PROJECT_NAME = "main";
48+
inline constexpr static const char* CURRENT_PROJECT_NAME = "main";
4949

5050
// Which project names are compatible to load given the current project. <current,
5151
// {compatible, ...}>. Projects are always assumed to be compatible with themselves.
@@ -89,7 +89,11 @@ struct FileHeader {
8989
// The file type identifier to make sure we're reading compatible binary data.
9090
// ! DO NOT CHANGE THIS VALUE !
9191
// (Otherwise all older files will be unreadable.)
92-
inline static const std::string IDENTIFIER_STRING = "SPARK_DSG";
92+
inline constexpr static const char* IDENTIFIER_STRING = "SPARK_DSG";
93+
//! @brief Get header key used for json serialization
94+
static std::string header_json_key() {
95+
return std::string(IDENTIFIER_STRING) + "_header";
96+
}
9397

9498
// String identifier for different variants of spark-dsg that may not be compatible.
9599
std::string project_name = "main";

src/node_attributes.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,20 @@ void SemanticNodeAttributes::serialization_info() {
193193
serialization::field("name", name);
194194
const auto& header = io::GlobalInfo::loadedHeader();
195195
if (header.version <= io::Version(1, 0, 2)) {
196+
io::warnOutdatedHeader(header);
197+
196198
Eigen::Matrix<uint8_t, 3, 1> color_uint8;
197199
serialization::field("color", color_uint8);
198200
color = Color(color_uint8[0], color_uint8[1], color_uint8[2]);
199-
io::warnOutdatedHeader(header);
200201
} else {
201202
serialization::field("color", color);
202203
}
204+
203205
serialization::field("bounding_box", bounding_box);
204206
serialization::field("semantic_label", semantic_label);
205207
if (header.version <= io::Version(1, 0, 4)) {
208+
io::warnOutdatedHeader(header);
209+
206210
Eigen::MatrixXd feature;
207211
serialization::field("semantic_feature", feature);
208212
semantic_feature = feature.cast<float>();
@@ -458,10 +462,10 @@ void AgentNodeAttributes::serialization_info() {
458462
NodeAttributes::serialization_info();
459463

460464
const auto& header = io::GlobalInfo::loadedHeader();
461-
if (header.version >= io::Version(1, 1, 0)) {
462-
serialization::field("timestamp", timestamp);
463-
} else {
465+
if (header.version < io::Version(1, 1, 0)) {
464466
io::warnOutdatedHeader(header);
467+
} else {
468+
serialization::field("timestamp", timestamp);
465469
}
466470

467471
serialization::field("world_R_body", world_R_body);
@@ -486,7 +490,7 @@ bool AgentNodeAttributes::is_equal(const NodeAttributes& other) const {
486490
dbow_values == derived->dbow_values;
487491
}
488492

489-
KhronosObjectAttributes::KhronosObjectAttributes() : mesh(true, false, false){};
493+
KhronosObjectAttributes::KhronosObjectAttributes() : mesh(true, false, false) {}
490494

491495
NodeAttributes::Ptr KhronosObjectAttributes::clone() const {
492496
return std::make_unique<KhronosObjectAttributes>(*this);
@@ -519,6 +523,7 @@ void KhronosObjectAttributes::serialization_info() {
519523
const auto& header = io::GlobalInfo::loadedHeader();
520524
if (header.version <= io::Version(1, 0, 1)) {
521525
io::warnOutdatedHeader(header);
526+
522527
std::vector<float> xyz;
523528
serialization::field("vertices", xyz);
524529
std::vector<uint8_t> rgb;

src/serialization/binary_conversions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ namespace spark_dsg {
4444
void read_binary(const serialization::BinaryDeserializer& s, BoundingBox& box) {
4545
const auto& header = io::GlobalInfo::loadedHeader();
4646
if (header.version < io::Version(1, 0, 3)) {
47+
io::warnOutdatedHeader(header);
48+
4749
// Legacy bboxes with min/max encoding.
4850
s.checkFixedArrayLength(5);
49-
io::warnOutdatedHeader(header);
5051
} else {
5152
// New bboxes with dimensions encoding.
5253
s.checkFixedArrayLength(4);
@@ -57,6 +58,8 @@ void read_binary(const serialization::BinaryDeserializer& s, BoundingBox& box) {
5758
box.type = static_cast<BoundingBox::Type>(raw_type);
5859

5960
if (header.version < io::Version(1, 0, 3)) {
61+
io::warnOutdatedHeader(header);
62+
6063
Eigen::Vector3f min, max;
6164
s.read(min);
6265
s.read(max);

src/serialization/graph_binary_serialization.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ NodeId parseNode(const AttributeFactory<NodeAttributes>& factory,
8585
PartitionId partition = 0;
8686
const auto& header = io::GlobalInfo::loadedHeader();
8787
if (header.version < io::Version(1, 1, 0)) {
88+
io::warnOutdatedHeader(header);
89+
8890
std::optional<std::chrono::nanoseconds> stamp;
8991
deserializer.read(stamp);
9092
// guess at interlayer IDs based on node symbol prefix
@@ -204,6 +206,8 @@ template <typename Attrs>
204206
AttributeFactory<Attrs> loadFactory(const io::FileHeader& header,
205207
const BinaryDeserializer& deserializer) {
206208
if (header.version < io::Version(1, 0, 2)) {
209+
io::warnOutdatedHeader(header);
210+
207211
return serialization::AttributeRegistry<Attrs>::current();
208212
}
209213

@@ -216,6 +220,8 @@ bool updateGraph(DynamicSceneGraph& graph, const BinaryDeserializer& deserialize
216220
const auto& header = io::GlobalInfo::loadedHeader();
217221

218222
if (header.version < io::Version(1, 1, 2)) {
223+
io::warnOutdatedHeader(header);
224+
219225
// NOTE(nathan) we intentionally don't try to use the layer IDs to populate anything
220226
// because they will not include partitions and cause lots of serialization churn
221227
std::vector<LayerId> layer_ids;
@@ -237,6 +243,8 @@ bool updateGraph(DynamicSceneGraph& graph, const BinaryDeserializer& deserialize
237243
}
238244

239245
if (header.version < io::Version(1, 0, 2)) {
246+
io::warnOutdatedHeader(header);
247+
240248
LayerId mesh_layer_id;
241249
deserializer.read(mesh_layer_id);
242250
}
@@ -247,12 +255,16 @@ bool updateGraph(DynamicSceneGraph& graph, const BinaryDeserializer& deserialize
247255

248256
std::map<std::string, LayerKey> layer_names;
249257
if (header.version < io::Version(1, 1, 0)) {
258+
io::warnOutdatedHeader(header);
259+
250260
layer_names = {{DsgLayers::OBJECTS, 2},
251261
{DsgLayers::AGENTS, 2},
252262
{DsgLayers::PLACES, 3},
253263
{DsgLayers::ROOMS, 4},
254264
{DsgLayers::BUILDINGS, 5}};
255265
} else if (header.version < io::Version(1, 1, 1)) {
266+
io::warnOutdatedHeader(header);
267+
256268
std::map<std::string, LayerId> names;
257269
deserializer.read(names);
258270
layer_names = DynamicSceneGraph::LayerNames(names.begin(), names.end());
@@ -264,7 +276,9 @@ bool updateGraph(DynamicSceneGraph& graph, const BinaryDeserializer& deserialize
264276
graph.addLayer(key.layer, key.partition, name);
265277
}
266278

267-
if (header.version >= io::Version(1, 0, 6)) {
279+
if (header.version < io::Version(1, 0, 6)) {
280+
io::warnOutdatedHeader(header);
281+
} else {
268282
std::string metadata_json;
269283
deserializer.read(metadata_json);
270284
try {
@@ -290,6 +304,8 @@ bool updateGraph(DynamicSceneGraph& graph, const BinaryDeserializer& deserialize
290304
}
291305

292306
if (header.version < io::Version(1, 1, 0)) {
307+
io::warnOutdatedHeader(header);
308+
293309
deserializer.checkDynamicArray();
294310
while (!deserializer.isDynamicArrayEnd()) {
295311
stale_nodes.erase(parseNode(

src/serialization/graph_json_serialization.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void read_node_from_json(const serialization::AttributeFactory<NodeAttributes>&
7171
PartitionId partition = 0;
7272
const auto& header = io::GlobalInfo::loadedHeader();
7373
if (header.version < io::Version(1, 1, 0)) {
74+
io::warnOutdatedHeader(header);
75+
7476
if (record.contains("timestamp")) {
7577
partition = NodeSymbol(node_id).category();
7678
}
@@ -111,8 +113,7 @@ namespace io::json {
111113

112114
std::string writeGraph(const DynamicSceneGraph& graph, bool include_mesh) {
113115
nlohmann::json record;
114-
record[io::FileHeader::IDENTIFIER_STRING + "_header"] = io::FileHeader::current();
115-
116+
record[io::FileHeader::header_json_key()] = io::FileHeader::current();
116117
record["directed"] = false;
117118
record["multigraph"] = false;
118119
record["nodes"] = nlohmann::json::array();
@@ -161,7 +162,7 @@ DynamicSceneGraph::Ptr readGraph(const std::string& contents) {
161162
const auto record = nlohmann::json::parse(contents);
162163

163164
// Parse header.
164-
const std::string header_field_name = FileHeader::IDENTIFIER_STRING + "_header";
165+
const auto header_field_name = FileHeader::header_json_key();
165166
const auto header = record.contains(header_field_name)
166167
? record.at(header_field_name).get<io::FileHeader>()
167168
: io::FileHeader::legacy();
@@ -171,6 +172,8 @@ DynamicSceneGraph::Ptr readGraph(const std::string& contents) {
171172

172173
DynamicSceneGraph::LayerKeys layer_keys;
173174
if (header.version < io::Version(1, 1, 2)) {
175+
io::warnOutdatedHeader(header);
176+
174177
const auto layer_ids = record.at("layer_ids").get<std::vector<LayerId>>();
175178
layer_keys = DynamicSceneGraph::LayerKeys(layer_ids.begin(), layer_ids.end());
176179
} else {
@@ -179,12 +182,16 @@ DynamicSceneGraph::Ptr readGraph(const std::string& contents) {
179182

180183
DynamicSceneGraph::LayerNames layer_names;
181184
if (header.version < io::Version(1, 1, 0)) {
185+
io::warnOutdatedHeader(header);
186+
182187
layer_names = {{DsgLayers::OBJECTS, 2},
183188
{DsgLayers::AGENTS, 2},
184189
{DsgLayers::PLACES, 3},
185190
{DsgLayers::ROOMS, 4},
186191
{DsgLayers::BUILDINGS, 5}};
187192
} else if (header.version < io::Version(1, 1, 1)) {
193+
io::warnOutdatedHeader(header);
194+
188195
const auto names = record.at("layer_names").get<std::map<std::string, LayerId>>();
189196
layer_names = DynamicSceneGraph::LayerNames(names.begin(), names.end());
190197
} else {

src/serialization/versioning.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ std::string Version::toString() const {
124124
std::vector<uint8_t> FileHeader::serializeToBinary() const {
125125
std::vector<uint8_t> buffer;
126126
serialization::BinarySerializer serializer(&buffer);
127-
serializer.write(IDENTIFIER_STRING);
127+
serializer.write(std::string(IDENTIFIER_STRING));
128128
serializer.write(*this);
129129
return buffer;
130130
}

tests/serialization/utest_file_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST(FileIoTests, VersionSerialization) {
6363

6464
// Check deserialization.
6565
const auto result = FileHeader::deserializeFromBinary(buffer);
66-
EXPECT_TRUE(result.has_value());
66+
ASSERT_TRUE(result.has_value());
6767
EXPECT_EQ(header.project_name, result->project_name);
6868
EXPECT_EQ(header.version, result->version);
6969

0 commit comments

Comments
 (0)