-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathBase.hh
172 lines (147 loc) · 4.6 KB
/
Base.hh
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
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_PHYSICS_TPE_PLUGIN_SRC_BASE_HH_
#define IGNITION_PHYSICS_TPE_PLUGIN_SRC_BASE_HH_
#include <ignition/physics/Implements.hh>
#include <map>
#include <memory>
#include <string>
#include "lib/src/World.hh"
#include "lib/src/Engine.hh"
#include "lib/src/Model.hh"
#include "lib/src/Link.hh"
#include "lib/src/Collision.hh"
#include "lib/src/Shape.hh"
namespace ignition {
namespace physics {
namespace tpeplugin {
/// \brief The structs tpelib::WorldInfo,
/// tpelib::ModelInfo, LinkInfo, and CollisionInfo are used
/// to provide easy access to tpelib structures in the plugin library
struct WorldInfo
{
std::shared_ptr<tpelib::World> world;
};
struct ModelInfo
{
tpelib::Model *model;
};
struct LinkInfo
{
tpelib::Link *link;
};
struct CollisionInfo
{
tpelib::Collision *collision;
};
class Base : public Implements3d<FeatureList<Feature>>
{
public: inline Identity InitiateEngine(std::size_t /*_engineID*/) override
{
return this->GenerateIdentity(0);
}
public: inline std::size_t idToIndexInContainer(std::size_t _id) const
{
std::size_t index = 0;
auto it = this->childIdToParentId.find(_id);
if (it != this->childIdToParentId.end())
{
for (const auto &pair : this->childIdToParentId)
{
if (pair.first == _id && pair.second == it->second)
{
return index;
}
else if (pair.second == it->second)
{
++index;
}
}
}
// return invalid index if not found in id map
return -1;
}
public: inline std::size_t indexInContainerToId(
const std::size_t _containerId, const std::size_t _index) const
{
std::size_t counter = 0;
auto it = this->childIdToParentId.begin();
while (counter <= _index && it != this->childIdToParentId.end())
{
if (it->second == _containerId && counter == _index)
{
return it->first;
}
else if (it->second == _containerId)
{
++counter;
}
++it;
}
// return invalid id if entity not found
return -1;
}
public: inline Identity AddWorld(std::shared_ptr<tpelib::World> _world)
{
size_t worldId = _world->GetId();
auto worldPtr = std::make_shared<WorldInfo>();
worldPtr->world = _world;
this->worlds.insert({worldId, worldPtr});
this->childIdToParentId.insert({worldId, -1});
return this->GenerateIdentity(worldId, worldPtr);
}
public: inline Identity AddModel(std::size_t _parentId, tpelib::Model &_model)
{
auto modelPtr = std::make_shared<ModelInfo>();
modelPtr->model = &_model;
size_t modelId = _model.GetId();
this->models.insert({modelId, modelPtr});
// keep track of model's corresponding world
this->childIdToParentId.insert({modelId, _parentId});
return this->GenerateIdentity(modelId, modelPtr);
}
public: inline Identity AddLink(std::size_t _modelId, tpelib::Link &_link)
{
auto linkPtr = std::make_shared<LinkInfo>();
linkPtr->link = &_link;
size_t linkId = _link.GetId();
this->links.insert({linkId, linkPtr});
// keep track of link's corresponding model
this->childIdToParentId.insert({linkId, _modelId});
return this->GenerateIdentity(linkId, linkPtr);
}
public: inline Identity AddCollision(std::size_t _linkId,
tpelib::Collision &_collision)
{
auto collisionPtr = std::make_shared<CollisionInfo>();
collisionPtr->collision = &_collision;
size_t collisionId = _collision.GetId();
this->collisions.insert({collisionId, collisionPtr});
// keep track of collision's corresponding link
this->childIdToParentId.insert({collisionId, _linkId});
return this->GenerateIdentity(collisionId, collisionPtr);
}
public: std::map<std::size_t, std::shared_ptr<WorldInfo>> worlds;
public: std::map<std::size_t, std::shared_ptr<ModelInfo>> models;
public: std::map<std::size_t, std::shared_ptr<LinkInfo>> links;
public: std::map<std::size_t, std::shared_ptr<CollisionInfo>> collisions;
public: std::map<std::size_t, std::size_t> childIdToParentId;
};
}
}
}
#endif