forked from facebookresearch/habitat-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.cpp
More file actions
109 lines (91 loc) · 3.7 KB
/
Agent.cpp
File metadata and controls
109 lines (91 loc) · 3.7 KB
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
// Copyright (c) Facebook, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "Agent.h"
#include <Magnum/EigenIntegration/GeometryIntegration.h>
#include <Magnum/EigenIntegration/Integration.h>
#include "esp/scene/ObjectControls.h"
#include "esp/sensor/Sensor.h"
using Magnum::EigenIntegration::cast;
namespace esp {
namespace agent {
const std::set<std::string> Agent::BodyActions = {"moveRight", "moveLeft",
"moveForward", "moveBackward",
"turnLeft", "turnRight"};
Agent::Agent(scene::SceneNode& agentNode, const AgentConfiguration& cfg)
: Magnum::SceneGraph::AbstractFeature3D(agentNode),
configuration_(cfg),
controls_(scene::ObjectControls::create()) {
agentNode.setType(scene::SceneNodeType::AGENT);
} // Agent::Agent
Agent::~Agent() {
LOG(INFO) << "Deconstructing Agent";
}
bool Agent::act(const std::string& actionName) {
if (hasAction(actionName)) {
const ActionSpec& actionSpec = *configuration_.actionSpace.at(actionName);
if (BodyActions.find(actionSpec.name) != BodyActions.end()) {
controls_->action(object(), actionSpec.name,
actionSpec.actuation.at("amount"),
/*applyFilter=*/true);
} else {
for (const auto& p : node().getNodeSensors()) {
controls_->action(p.second.get().object(), actionSpec.name,
actionSpec.actuation.at("amount"),
/*applyFilter=*/false);
}
}
return true;
} else {
return false;
}
}
bool Agent::hasAction(const std::string& actionName) {
auto actionSpace = configuration_.actionSpace;
return !(actionSpace.find(actionName) == actionSpace.end());
}
void Agent::reset() {
setState(initialState_);
}
void Agent::getState(const AgentState::ptr& state) const {
// TODO this should be done less hackishly
state->position = cast<vec3f>(node().absoluteTransformation().translation());
state->rotation = quatf(node().rotation()).coeffs();
// TODO other state members when implemented
}
void Agent::setState(const AgentState& state,
const bool resetSensors /*= true*/) {
node().setTranslation(Magnum::Vector3(state.position));
const Eigen::Map<const quatf> rot(state.rotation.data());
CHECK_LT(std::abs(rot.norm() - 1.0),
2.0 * Magnum::Math::TypeTraits<float>::epsilon())
<< state.rotation << " not a valid rotation";
node().setRotation(Magnum::Quaternion(quatf(rot)).normalized());
if (resetSensors) {
for (auto& p : node().getNodeSensors()) {
p.second.get().setTransformationFromSpec();
}
}
// TODO other state members when implemented
}
bool operator==(const ActionSpec& a, const ActionSpec& b) {
return a.name == b.name && a.actuation == b.actuation;
}
bool operator!=(const ActionSpec& a, const ActionSpec& b) {
return !(a == b);
}
bool operator==(const AgentConfiguration& a, const AgentConfiguration& b) {
return a.height == b.height && a.radius == b.radius && a.mass == b.mass &&
a.linearAcceleration == b.linearAcceleration &&
a.angularAcceleration == b.angularAcceleration &&
a.linearFriction == b.linearFriction &&
a.angularFriction == b.angularFriction &&
a.coefficientOfRestitution == b.coefficientOfRestitution &&
esp::equal(a.sensorSpecifications, b.sensorSpecifications) &&
esp::equal(a.actionSpace, b.actionSpace) && a.bodyType == b.bodyType;
}
bool operator!=(const AgentConfiguration& a, const AgentConfiguration& b) {
return !(a == b);
}
} // namespace agent
} // namespace esp