Skip to content

Commit a81c84c

Browse files
committed
add spatial and articulated edge attributes
1 parent 0a8b29b commit a81c84c

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

include/spark_dsg/edge_attributes.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* purposes notwithstanding any copyright notation herein.
3434
* -------------------------------------------------------------------------- */
3535
#pragma once
36+
#include <Eigen/Geometry>
3637
#include <memory>
3738
#include <nlohmann/json.hpp>
3839
#include <ostream>
@@ -99,7 +100,7 @@ struct EdgeAttributes {
99100
virtual void fill_ostream(std::ostream& out) const;
100101
//! register serialization information about the attributes
101102
virtual void serialization_info();
102-
virtual void serialization_info() const;
103+
void serialization_info() const;
103104
//! compute equality
104105
virtual bool is_equal(const EdgeAttributes& other) const;
105106

@@ -112,4 +113,45 @@ struct EdgeAttributes {
112113
}
113114
};
114115

116+
struct SpatialEdgeAttributes : public EdgeAttributes {
117+
public:
118+
//! pointer type for node
119+
using Ptr = std::unique_ptr<SpatialEdgeAttributes>;
120+
121+
//! alias for semantic label
122+
enum class Type { INSIDE, UNKNOWN } type;
123+
124+
SpatialEdgeAttributes();
125+
virtual ~SpatialEdgeAttributes() = default;
126+
EdgeAttributes::Ptr clone() const override;
127+
128+
protected:
129+
void fill_ostream(std::ostream& out) const override;
130+
void serialization_info() override;
131+
bool is_equal(const EdgeAttributes& other) const override;
132+
// registers derived attributes
133+
REGISTER_EDGE_ATTRIBUTES(SpatialEdgeAttributes);
134+
};
135+
136+
struct ArticulateEdgeAttributes : public EdgeAttributes {
137+
public:
138+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
139+
//! pointer type for node
140+
using Ptr = std::unique_ptr<ArticulateEdgeAttributes>;
141+
142+
//! alias for semantic label
143+
enum class Type { REVOLUTE, PRISMATIC, UNKNOWN } type;
144+
Eigen::Vector3f axis;
145+
146+
ArticulateEdgeAttributes();
147+
virtual ~ArticulateEdgeAttributes() = default;
148+
EdgeAttributes::Ptr clone() const override;
149+
150+
protected:
151+
void fill_ostream(std::ostream& out) const override;
152+
void serialization_info() override;
153+
bool is_equal(const EdgeAttributes& other) const override;
154+
// registers derived attributes
155+
REGISTER_EDGE_ATTRIBUTES(ArticulateEdgeAttributes);
156+
};
115157
} // namespace spark_dsg

src/edge_attributes.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,31 @@
3636

3737
#include <iomanip>
3838

39+
#include "spark_dsg/printing.h"
3940
#include "spark_dsg/serialization/attribute_serialization.h"
4041

4142
namespace spark_dsg {
4243

44+
template <typename Derived>
45+
bool matricesEqual(const Eigen::DenseBase<Derived>& lhs,
46+
const Eigen::DenseBase<Derived>& rhs) {
47+
if (lhs.rows() != rhs.rows() || lhs.cols() != rhs.cols()) {
48+
return false;
49+
}
50+
51+
bool same = true;
52+
for (int r = 0; r < lhs.rows(); ++r) {
53+
for (int c = 0; c < lhs.cols(); ++c) {
54+
const auto lhs_nan = std::isnan(lhs(r, c));
55+
const auto rhs_nan = std::isnan(rhs(r, c));
56+
// if one value is nan, this still works
57+
same &= (lhs_nan && rhs_nan) || lhs(r, c) == rhs(r, c);
58+
}
59+
}
60+
61+
return same;
62+
}
63+
4364
EdgeAttributes::EdgeAttributes() : weighted(false), weight(1.0) {}
4465

4566
EdgeAttributes::EdgeAttributes(double weight) : weighted(true), weight(weight) {}
@@ -78,4 +99,69 @@ bool EdgeAttributes::is_equal(const EdgeAttributes& other) const {
7899
return weighted == other.weighted && weight == other.weight;
79100
}
80101

102+
SpatialEdgeAttributes::SpatialEdgeAttributes()
103+
: EdgeAttributes(), type(SpatialEdgeAttributes::Type::UNKNOWN) {}
104+
105+
EdgeAttributes::Ptr SpatialEdgeAttributes::clone() const {
106+
return std::make_unique<SpatialEdgeAttributes>(*this);
107+
}
108+
109+
void SpatialEdgeAttributes::fill_ostream(std::ostream& out) const {
110+
EdgeAttributes::fill_ostream(out);
111+
out << "\n - type: " << static_cast<int>(type) << "\n"
112+
<< "]";
113+
}
114+
115+
void SpatialEdgeAttributes::serialization_info() {
116+
EdgeAttributes::serialization_info();
117+
int type_val = static_cast<int>(type);
118+
serialization::field("type", type_val);
119+
}
120+
121+
bool SpatialEdgeAttributes::is_equal(const EdgeAttributes& other) const {
122+
const auto derived = dynamic_cast<const SpatialEdgeAttributes*>(&other);
123+
if (!derived) {
124+
return false;
125+
}
126+
127+
if (!EdgeAttributes::is_equal(other)) {
128+
return false;
129+
}
130+
131+
return type == derived->type;
132+
}
133+
134+
ArticulateEdgeAttributes::ArticulateEdgeAttributes()
135+
: EdgeAttributes(), type(ArticulateEdgeAttributes::Type::UNKNOWN) {}
136+
137+
EdgeAttributes::Ptr ArticulateEdgeAttributes::clone() const {
138+
return std::make_unique<ArticulateEdgeAttributes>(*this);
139+
}
140+
141+
void ArticulateEdgeAttributes::fill_ostream(std::ostream& out) const {
142+
auto format = getDefaultVectorFormat();
143+
EdgeAttributes::fill_ostream(out);
144+
out << "\n - type: " << static_cast<int>(type) << "\n"
145+
<< "\n - axis: " << axis.transpose().format(format) << "\n"
146+
<< "]";
147+
}
148+
149+
void ArticulateEdgeAttributes::serialization_info() {
150+
EdgeAttributes::serialization_info();
151+
int type_val = static_cast<int>(type);
152+
serialization::field("type", type_val);
153+
}
154+
155+
bool ArticulateEdgeAttributes::is_equal(const EdgeAttributes& other) const {
156+
const auto derived = dynamic_cast<const ArticulateEdgeAttributes*>(&other);
157+
if (!derived) {
158+
return false;
159+
}
160+
161+
if (!EdgeAttributes::is_equal(other)) {
162+
return false;
163+
}
164+
165+
return type == derived->type && matricesEqual(axis, derived->axis);
166+
}
81167
} // namespace spark_dsg

0 commit comments

Comments
 (0)