-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStateDis.cpp
202 lines (173 loc) · 5.82 KB
/
StateDis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "ompl/base/StateSpace.h"
#include "ompl/base/spaces/DiscreteStateSpace.h"
#include <boost/math/constants/constants.hpp>
#include "PrimitiveAdapter.cpp"
#include "Primitive.cpp"
#include "State.cpp"
#pragma once
namespace ompl
{
namespace base
{
class StateDis : public CompoundStateSpace
{
private:
PrimitiveAdapter m_adapter;
bool m_metric = true;
public:
class StateType : public CompoundStateSpace::StateType
{
public:
StateType() = default;
int getX() const {
return as<DiscreteStateSpace::StateType>(0)->value;
}
int getY() const {
return as<DiscreteStateSpace::StateType>(1)->value;
}
double getYaw() const {
double pi = boost::math::constants::pi<double>();
double val = as<DiscreteStateSpace::StateType>(2)->value;
return val*15*pi/180;
}
int getSpeed() const {
return as<DiscreteStateSpace::StateType>(3)->value*2;
}
void setX(int x){
as<DiscreteStateSpace::StateType>(0)->value = x;
}
void setY(int y){
as<DiscreteStateSpace::StateType>(1)->value = y;
}
void setXY(int x, int y){
setX(x);
setY(y);
}
void setYaw(int yaw){
as<DiscreteStateSpace::StateType>(2)->value = yaw;
}
void setYaw(double yaw){
double pi = boost::math::constants::pi<double>();
as<DiscreteStateSpace::StateType>(2)->value = (int)yaw*180/15/pi;
}
void setSpeed(int speed){
as<DiscreteStateSpace::StateType>(3)->value = speed/2;
}
};
StateDis(int);
StateDis(int x, int y);
StateDis();
~StateDis () override = default;
/*bool isMetricSpace () const override;
unsigned int getDimension() const override;
double distance();*/
bool isCompound () const override;
State *allocState() const override;
void freeState(State* state) const override;
bool equalStates(const State* state1, const State* state2)const override;
void setAdapter(const PrimitiveAdapter&);
const PrimitiveAdapter& getAdapter();
double distance (const State *state1, const State *state2) const override;
void setBounds(int);
void setNonMetricDistance();
friend std::ostream& operator<<(std::ostream &out, const State *s);
/* Transfor the discrete states to small trajectory primitives */
};
StateDis::StateDis(){}
StateDis::StateDis(int bound)
{
setName("SE2_V_Discrete");
// x
addSubspace(std::make_shared<DiscreteStateSpace>(-bound,bound),1.0);
// y
addSubspace(std::make_shared<DiscreteStateSpace>(-bound,bound),1.0);
// yaw
addSubspace(std::make_shared<DiscreteStateSpace>(0,24),1.0);
// speed
addSubspace(std::make_shared<DiscreteStateSpace>(0,2),1.0);
lock();
}
StateDis::StateDis(int x, int y)
{
setName("SE2_V_Discrete");
// x
addSubspace(std::make_shared<DiscreteStateSpace>(0,x),1.0);
// y
addSubspace(std::make_shared<DiscreteStateSpace>(0,y),1.0);
// yaw
addSubspace(std::make_shared<DiscreteStateSpace>(0,24),1.0);
// speed
addSubspace(std::make_shared<DiscreteStateSpace>(0,2),1.0);
lock();
}
void StateDis::setNonMetricDistance(){
m_metric = false;
}
State* StateDis::allocState() const {
auto *state = new StateType();
allocStateComponents(state);
return state;
}
void StateDis::freeState(State *state) const{
auto *cstate = static_cast<CompoundState *>(state);
for (unsigned int i = 0; i < componentCount_; ++i)
{auto c = components_[i];
components_[i]->freeState(cstate->components[i]);}
delete[] cstate->components;
delete cstate;
}
void StateDis::setAdapter(const PrimitiveAdapter& adapter){
m_adapter = adapter;
}
double StateDis::distance (const State *state1, const State *state2) const{
// if(false){
if(m_metric){
return CompoundStateSpace::distance(state1,state2);
}else{
auto s1 = state1->as<StateDis::StateType>();
auto s2 = state2->as<StateDis::StateType>();
::StateSpace init(s1->getX(), s1->getY(),s1->getYaw(), s1->getSpeed());
::StateSpace fin(s2->getX(), s2->getY(),s2->getYaw(), s2->getSpeed());
Primitive primCandidate(init, fin);
auto prim = m_adapter.findPrimitive(primCandidate);
if(equalStates(state1,state2))
return 0;
else
return prim.getCost();
}
}
bool StateDis::isCompound () const {
return true;
}
bool StateDis::equalStates(const State* state1, const State* state2)const {
auto s1 = state1->as<StateDis::StateType>();
auto s2 = state2->as<StateDis::StateType>();
// For debug purposeses
::StateSpace init(s1->getX(), s1->getY(),s1->getYaw(), s1->getSpeed());
::StateSpace fin(s2->getX(), s2->getY(),s2->getYaw(), s2->getSpeed());
//TODO: Check
return s1->getX() == s2->getX() &&
s1->getY() == s2->getY() &&
s1->getYaw() == s2->getYaw() &&
s1->getSpeed() == s2->getSpeed();
}
std::ostream& operator<<(std::ostream &out, const State *s){
auto state = s->as<StateDis::StateType>();
int x = state->getX();
int y = state->getY();
double w = state->getYaw();
int v = state->getSpeed();
out << x << " " << y << " " << w<< " "<< v << std::endl;
return out;
}
void StateDis::setBounds(int bound){
auto vec = getSubspaces();
//set the given boundries for the x and y space
vec[0]->as<DiscreteStateSpace>()->setBounds(-bound, bound);
vec[1]->as<DiscreteStateSpace>()->setBounds(-bound, bound);
}
const PrimitiveAdapter& StateDis::getAdapter(){
return m_adapter;
}
}
}