Control Libraries 7.4.0
Loading...
Searching...
No Matches
SpatialState.cpp
1#include "state_representation/space/SpatialState.hpp"
2
3#include "state_representation/exceptions/InvalidCastException.hpp"
4
5namespace state_representation {
6
7SpatialState::SpatialState() : State(), reference_frame_("world") {
8 this->set_type(StateType::SPATIAL_STATE);
9}
10
11SpatialState::SpatialState(const std::string& name, const std::string& reference_frame) :
12 State(name), reference_frame_(reference_frame) {
13 this->set_type(StateType::SPATIAL_STATE);
14}
15
16SpatialState::SpatialState(const SpatialState& state) : State(state), reference_frame_(state.reference_frame_) {
17 this->set_type(StateType::SPATIAL_STATE);
18}
19
21 SpatialState tmp(state);
22 swap(*this, tmp);
23 return *this;
24}
25
26const std::string& SpatialState::get_reference_frame() const {
27 return this->reference_frame_;
28}
29
30void SpatialState::set_reference_frame(const std::string& reference_frame) {
31 this->reference_frame_ = reference_frame;
32 this->reset_timestamp();
33}
34
35bool SpatialState::is_incompatible(const State& state) const {
36 try {
37 auto other = dynamic_cast<const SpatialState&>(state);
38 // the three conditions for compatibility are:
39 // 1) this name matches other reference frame (this is parent transform of other)
40 // 2) this reference frame matches other name (this is child transform of other)
41 // 3) this reference frame matches other reference frame (this is sibling transform of other)
42 return (this->get_name() != other.reference_frame_) && (this->reference_frame_ != other.get_name())
43 && (this->get_reference_frame() != other.reference_frame_);
44 } catch (const std::bad_cast& ex) {
46 std::string("Could not cast the given object to a SpatialState: ") + ex.what());
47 }
48}
49
50std::string SpatialState::to_string() const {
51 auto state = this->State::to_string();
52 return state + " expressed in frame '" + this->get_reference_frame() + "'";
53}
54
55std::ostream& operator<<(std::ostream& os, const SpatialState& state) {
56 os << state.to_string();
57 return os;
58}
59}// namespace state_representation
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
bool is_incompatible(const State &state) const override
Check if the spatial state is incompatible for operations with the state given as argument.
SpatialState & operator=(const SpatialState &state)
Copy assignment operator that has to be defined to the custom assignment operator.
std::string to_string() const override
Convert the state to its string representation.
const std::string & get_reference_frame() const
Getter of the reference frame as const reference.
friend void swap(SpatialState &state1, SpatialState &state2)
Swap the values of the two SpatialState.
Abstract class to represent a state.
Definition State.hpp:25
void reset_timestamp()
Reset the timestamp attribute to now.
Definition State.cpp:61
const std::string & get_name() const
Getter of the name attribute.
Definition State.cpp:29
void set_type(const StateType &type)
Setter of the state type attribute.
Definition State.cpp:41
virtual std::string to_string() const
Convert the state to its string representation.
Definition State.cpp:99
Core state variables and objects.
std::ostream & operator<<(std::ostream &os, const AnalogIOState &state)