-
Notifications
You must be signed in to change notification settings - Fork 38
/
Base.hh
237 lines (206 loc) · 6.64 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* 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 GZ_PHYSICS_TPE_PLUGIN_SRC_BASE_HH_
#define GZ_PHYSICS_TPE_PLUGIN_SRC_BASE_HH_
#include <gz/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 gz {
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: template <typename EntityType>
inline std::pair<std::size_t, EntityType> indexInContainerToId(
const std::size_t _containerId,
const std::size_t _index,
const std::map<std::size_t, EntityType> &_idMap) const
{
std::size_t counter = 0;
auto it = this->childIdToParentId.begin();
while (counter <= _index && it != this->childIdToParentId.end())
{
if (it->second == _containerId)
{
auto idMapIt = _idMap.find(it->first);
// only count if the entity is found in the idMap. This makes sure we're
// counting only the entities with the correct EntityType
if (idMapIt != _idMap.end())
{
if (counter == _index)
{
return *idMapIt;
}
else
{
++counter;
}
}
}
++it;
}
// return invalid id if entity not found
return {INVALID_ENTITY_ID, nullptr};
}
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: bool RemoveModelImpl(std::size_t _modelID)
{
auto parentIt = this->childIdToParentId.find(_modelID);
if (parentIt == this->childIdToParentId.end())
return false;
auto modelInfoIt = this->models.find(_modelID);
if (modelInfoIt == this->models.end())
return false;
tpelib::Entity *parentEntity = nullptr;
auto worldIt = this->worlds.find(parentIt->second);
if (worldIt != this->worlds.end())
parentEntity = worldIt->second->world.get();
if (nullptr == parentEntity)
{
// If the parent entity is not a world, try a parent model of a nested
// model
auto modelIt = this->models.find(parentIt->second);
if (modelIt != this->models.end())
parentEntity = modelIt->second->model;
}
if (nullptr == parentEntity)
return false;
bool result = true;
for (std::size_t i = 0; i < modelInfoIt->second->model->GetChildCount();
++i)
{
// Get a reference so we can dynamic cast
auto &ent = modelInfoIt->second->model->GetChildByIndex(i);
auto modelEnt = dynamic_cast<tpelib::Model *>(&ent);
if (modelEnt)
{
result &= this->RemoveModelImpl(modelEnt->GetId());
}
}
result &= this->RemoveModelFromParent(_modelID, parentEntity);
return result;
}
public: bool RemoveModelFromParent(std::size_t _modelID,
tpelib::Entity *_parentEntity)
{
if (nullptr == _parentEntity)
return false;
bool result = this->models.erase(_modelID) == 1;
result &= this->childIdToParentId.erase(_modelID) == 1;
result &= _parentEntity->RemoveChildById(_modelID);
return result;
}
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