|
36 | 36 |
|
37 | 37 | #include <iomanip> |
38 | 38 |
|
| 39 | +#include "spark_dsg/printing.h" |
39 | 40 | #include "spark_dsg/serialization/attribute_serialization.h" |
40 | 41 |
|
41 | 42 | namespace spark_dsg { |
42 | 43 |
|
| 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 | + |
43 | 64 | EdgeAttributes::EdgeAttributes() : weighted(false), weight(1.0) {} |
44 | 65 |
|
45 | 66 | EdgeAttributes::EdgeAttributes(double weight) : weighted(true), weight(weight) {} |
@@ -78,4 +99,69 @@ bool EdgeAttributes::is_equal(const EdgeAttributes& other) const { |
78 | 99 | return weighted == other.weighted && weight == other.weight; |
79 | 100 | } |
80 | 101 |
|
| 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 | +} |
81 | 167 | } // namespace spark_dsg |
0 commit comments