-
Notifications
You must be signed in to change notification settings - Fork 276
/
EntityComponentManager.cc
1352 lines (1154 loc) · 41.1 KB
/
EntityComponentManager.cc
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 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.
*
*/
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <ignition/common/Profiler.hh>
#include <ignition/math/graph/GraphAlgorithms.hh>
#include "ignition/gazebo/components/Component.hh"
#include "ignition/gazebo/components/Factory.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
using namespace ignition;
using namespace gazebo;
class ignition::gazebo::EntityComponentManagerPrivate
{
/// \brief Implementation of the CreateEntity function, which takes a specific
/// entity as input.
/// \param[in] _entity Entity to be created.
/// \return Created entity, which should match the input.
public: Entity CreateEntityImplementation(Entity _entity);
/// \brief Recursively insert an entity and all its descendants into a given
/// set.
/// \param[in] _entity Entity to be inserted.
/// \param[in, out] _set Set to be filled.
public: void InsertEntityRecursive(Entity _entity,
std::unordered_set<Entity> &_set);
/// \brief Register a new component type.
/// \param[in] _typeId Type if of the new component.
/// \return True if created successfully.
public: bool CreateComponentStorage(const ComponentTypeId _typeId);
/// \brief Allots the work for multiple threads prior to running
/// `AddEntityToMessage`.
public: void CalculateStateThreadLoad();
/// \brief Map of component storage classes. The key is a component
/// type id, and the value is a pointer to the component storage.
public: std::unordered_map<ComponentTypeId,
std::unique_ptr<ComponentStorageBase>> components;
/// \brief A graph holding all entities, arranged according to their
/// parenting.
public: EntityGraph entities;
/// \brief Components that have been changed through a peridic change.
public: std::set<ComponentKey> periodicChangedComponents;
/// \brief Components that have been changed through a one-time change.
public: std::set<ComponentKey> oneTimeChangedComponents;
/// \brief Entities that have just been created
public: std::unordered_set<Entity> newlyCreatedEntities;
/// \brief Entities that need to be removed.
public: std::unordered_set<Entity> toRemoveEntities;
/// \brief Flag that indicates if all entities should be removed.
public: bool removeAllEntities{false};
/// \brief True if the entityComponents map was changed. Primarily used
/// by the multithreading functionality in `State()` to allocate work to
/// each thread.
public: bool entityComponentsDirty{true};
/// \brief The set of components that each entity has.
/// NOTE: Any modification of this data structure must be followed
/// by setting `entityComponentsDirty` to true.
public: std::unordered_map<Entity,
std::unordered_map<ComponentTypeId, ComponentKey>> entityComponents;
/// \brief A vector of iterators to evenly distributed spots in the
/// `entityComponents` map. Threads in the `State` function use this
/// vector for easy access of their pre-allocated work. This vector
/// is recalculated if `entityComponents` is changed (when
/// `entityComponentsDirty` == true).
public: std::vector<std::unordered_map<Entity,
std::unordered_map<ComponentTypeId, ComponentKey>>::iterator>
entityComponentIterators;
/// \brief A mutex to protect newly created entityes.
public: std::mutex entityCreatedMutex;
/// \brief A mutex to protect entity remove.
public: std::mutex entityRemoveMutex;
/// \brief A mutex to protect from concurrent writes to views
public: mutable std::mutex viewsMutex;
/// \brief The set of all views.
public: mutable std::map<detail::ComponentTypeKey, detail::View> views;
/// \brief Cache of previously queried descendants. The key is the parent
/// entity for which descendants were queried, and the value are all its
/// descendants.
public: mutable std::unordered_map<Entity, std::unordered_set<Entity>>
descendantCache;
/// \brief Keep track of entities already used to ensure uniqueness.
public: uint64_t entityCount{0};
};
//////////////////////////////////////////////////
EntityComponentManager::EntityComponentManager()
: dataPtr(new EntityComponentManagerPrivate)
{
}
//////////////////////////////////////////////////
EntityComponentManager::~EntityComponentManager() = default;
//////////////////////////////////////////////////
size_t EntityComponentManager::EntityCount() const
{
return this->dataPtr->entities.Vertices().size();
}
/////////////////////////////////////////////////
Entity EntityComponentManager::CreateEntity()
{
Entity entity = ++this->dataPtr->entityCount;
if (entity == std::numeric_limits<int64_t>::max())
{
ignwarn << "Reached maximum number of entities [" << entity << "]"
<< std::endl;
return entity;
}
return this->dataPtr->CreateEntityImplementation(entity);
}
/////////////////////////////////////////////////
Entity EntityComponentManagerPrivate::CreateEntityImplementation(Entity _entity)
{
IGN_PROFILE("EntityComponentManager::CreateEntityImplementation");
this->entities.AddVertex(std::to_string(_entity), _entity, _entity);
// Add entity to the list of newly created entities
{
std::lock_guard<std::mutex> lock(this->entityCreatedMutex);
this->newlyCreatedEntities.insert(_entity);
}
// Reset descendants cache
this->descendantCache.clear();
return _entity;
}
/////////////////////////////////////////////////
void EntityComponentManager::ClearNewlyCreatedEntities()
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityCreatedMutex);
this->dataPtr->newlyCreatedEntities.clear();
for (auto &view : this->dataPtr->views)
{
view.second.ClearNewEntities();
}
}
/////////////////////////////////////////////////
void EntityComponentManagerPrivate::InsertEntityRecursive(Entity _entity,
std::unordered_set<Entity> &_set)
{
for (const auto &vertex : this->entities.AdjacentsFrom(_entity))
{
this->InsertEntityRecursive(vertex.first, _set);
}
_set.insert(_entity);
}
/////////////////////////////////////////////////
void EntityComponentManager::RequestRemoveEntity(Entity _entity,
bool _recursive)
{
// Store the to-be-removed entities in a temporary set so we can call
// UpdateViews on each of them
std::unordered_set<Entity> tmpToRemoveEntities;
if (!_recursive)
{
tmpToRemoveEntities.insert(_entity);
}
else
{
this->dataPtr->InsertEntityRecursive(_entity, tmpToRemoveEntities);
}
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityRemoveMutex);
this->dataPtr->toRemoveEntities.insert(tmpToRemoveEntities.begin(),
tmpToRemoveEntities.end());
}
for (const auto &removedEntity : tmpToRemoveEntities)
{
this->UpdateViews(removedEntity);
}
}
/////////////////////////////////////////////////
void EntityComponentManager::RequestRemoveEntities()
{
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityRemoveMutex);
this->dataPtr->removeAllEntities = true;
}
this->RebuildViews();
}
/////////////////////////////////////////////////
void EntityComponentManager::ProcessRemoveEntityRequests()
{
IGN_PROFILE("EntityComponentManager::ProcessRemoveEntityRequests");
std::lock_guard<std::mutex> lock(this->dataPtr->entityRemoveMutex);
// Short-cut if erasing all entities
if (this->dataPtr->removeAllEntities)
{
IGN_PROFILE("RemoveAll");
this->dataPtr->removeAllEntities = false;
this->dataPtr->entities = EntityGraph();
this->dataPtr->entityComponents.clear();
this->dataPtr->toRemoveEntities.clear();
this->dataPtr->entityComponentsDirty = true;
for (std::pair<const ComponentTypeId,
std::unique_ptr<ComponentStorageBase>> &comp: this->dataPtr->components)
{
comp.second->RemoveAll();
}
// All views are now invalid.
this->dataPtr->views.clear();
}
else
{
IGN_PROFILE("Remove");
// Otherwise iterate through the list of entities to remove.
for (const Entity entity : this->dataPtr->toRemoveEntities)
{
// Make sure the entity exists and is not removed.
if (!this->HasEntity(entity))
continue;
// Remove from graph
this->dataPtr->entities.RemoveVertex(entity);
auto entityIter = this->dataPtr->entityComponents.find(entity);
// Remove the components, if any.
if (entityIter != this->dataPtr->entityComponents.end())
{
for (const auto &key : entityIter->second)
{
this->dataPtr->components.at(key.second.first)->Remove(
key.second.second);
}
// Remove the entry in the entityComponent map
this->dataPtr->entityComponents.erase(entity);
this->dataPtr->entityComponentsDirty = true;
}
// Remove the entity from views.
for (auto &view : this->dataPtr->views)
{
view.second.RemoveEntity(entity, view.first);
}
}
// Clear the set of entities to remove.
this->dataPtr->toRemoveEntities.clear();
}
// Reset descendants cache
this->dataPtr->descendantCache.clear();
}
/////////////////////////////////////////////////
bool EntityComponentManager::RemoveComponent(
const Entity _entity, const ComponentTypeId &_typeId)
{
auto componentId = this->EntityComponentIdFromType(_entity, _typeId);
ComponentKey key{_typeId, componentId};
return this->RemoveComponent(_entity, key);
}
/////////////////////////////////////////////////
bool EntityComponentManager::RemoveComponent(
const Entity _entity, const ComponentKey &_key)
{
IGN_PROFILE("EntityComponentManager::RemoveComponent");
// Make sure the entity exists and has the component.
if (!this->EntityHasComponent(_entity, _key))
return false;
this->dataPtr->components.at(_key.first)->Remove(_key.second);
this->dataPtr->entityComponents[_entity].erase(_key.first);
this->dataPtr->oneTimeChangedComponents.erase(_key);
this->dataPtr->periodicChangedComponents.erase(_key);
this->dataPtr->entityComponentsDirty = true;
this->UpdateViews(_entity);
return true;
}
/////////////////////////////////////////////////
bool EntityComponentManager::EntityHasComponent(const Entity _entity,
const ComponentKey &_key) const
{
if (!this->HasEntity(_entity))
return false;
auto &compMap = this->dataPtr->entityComponents[_entity];
return compMap.find(_key.first) != compMap.end();
}
/////////////////////////////////////////////////
bool EntityComponentManager::EntityHasComponentType(const Entity _entity,
const ComponentTypeId &_typeId) const
{
if (!this->HasEntity(_entity))
return false;
auto iter = this->dataPtr->entityComponents.find(_entity);
if (iter == this->dataPtr->entityComponents.end())
return false;
auto typeIter = iter->second.find(_typeId);
return (typeIter != iter->second.end());
}
/////////////////////////////////////////////////
bool EntityComponentManager::IsNewEntity(const Entity _entity) const
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityCreatedMutex);
return this->dataPtr->newlyCreatedEntities.find(_entity) !=
this->dataPtr->newlyCreatedEntities.end();
}
/////////////////////////////////////////////////
bool EntityComponentManager::IsMarkedForRemoval(const Entity _entity) const
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityRemoveMutex);
if (this->dataPtr->removeAllEntities)
{
return true;
}
return this->dataPtr->toRemoveEntities.find(_entity) !=
this->dataPtr->toRemoveEntities.end();
}
/////////////////////////////////////////////////
ComponentState EntityComponentManager::ComponentState(const Entity _entity,
const ComponentTypeId _typeId) const
{
auto result = ComponentState::NoChange;
auto ecIter = this->dataPtr->entityComponents.find(_entity);
if (ecIter == this->dataPtr->entityComponents.end())
return result;
auto typeKey = ecIter->second.find(_typeId);
if (typeKey == ecIter->second.end())
return result;
if (this->dataPtr->oneTimeChangedComponents.find(typeKey->second) !=
this->dataPtr->oneTimeChangedComponents.end())
{
result = ComponentState::OneTimeChange;
}
else if (this->dataPtr->periodicChangedComponents.find(typeKey->second) !=
this->dataPtr->periodicChangedComponents.end())
{
result = ComponentState::PeriodicChange;
}
return result;
}
/////////////////////////////////////////////////
bool EntityComponentManager::HasNewEntities() const
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityCreatedMutex);
return !this->dataPtr->newlyCreatedEntities.empty();
}
/////////////////////////////////////////////////
bool EntityComponentManager::HasEntitiesMarkedForRemoval() const
{
std::lock_guard<std::mutex> lock(this->dataPtr->entityRemoveMutex);
return this->dataPtr->removeAllEntities ||
!this->dataPtr->toRemoveEntities.empty();
}
/////////////////////////////////////////////////
bool EntityComponentManager::HasOneTimeComponentChanges() const
{
return !this->dataPtr->oneTimeChangedComponents.empty();
}
/////////////////////////////////////////////////
std::unordered_set<ComponentTypeId>
EntityComponentManager::ComponentTypesWithPeriodicChanges() const
{
std::unordered_set<ComponentTypeId> periodicComponents;
for (const auto& compPair : this->dataPtr->periodicChangedComponents)
{
periodicComponents.insert(compPair.first);
}
return periodicComponents;
}
/////////////////////////////////////////////////
bool EntityComponentManager::HasEntity(const Entity _entity) const
{
auto vertex = this->dataPtr->entities.VertexFromId(_entity);
return vertex.Id() != math::graph::kNullId;
}
/////////////////////////////////////////////////
Entity EntityComponentManager::ParentEntity(const Entity _entity) const
{
auto parents = this->Entities().AdjacentsTo(_entity);
if (parents.empty())
return kNullEntity;
// TODO(louise) Do we want to support multiple parents?
return parents.begin()->first;
}
/////////////////////////////////////////////////
bool EntityComponentManager::SetParentEntity(const Entity _child,
const Entity _parent)
{
// Remove current parent(s)
auto parents = this->Entities().AdjacentsTo(_child);
for (const auto &parent : parents)
{
auto edge = this->dataPtr->entities.EdgeFromVertices(parent.first, _child);
this->dataPtr->entities.RemoveEdge(edge);
}
// Leave parent-less
if (_parent == kNullEntity)
{
return true;
}
// Add edge
auto edge = this->dataPtr->entities.AddEdge({_parent, _child}, true);
return (math::graph::kNullId != edge.Id());
}
/////////////////////////////////////////////////
ComponentKey EntityComponentManager::CreateComponentImplementation(
const Entity _entity, const ComponentTypeId _componentTypeId,
const components::BaseComponent *_data)
{
// If type hasn't been instantiated yet, create a storage for it
if (!this->HasComponentType(_componentTypeId))
{
if (!this->dataPtr->CreateComponentStorage(_componentTypeId))
{
ignerr << "Failed to create component of type [" << _componentTypeId
<< "] for entity [" << _entity
<< "]. Type has not been properly registered." << std::endl;
return ComponentKey();
}
}
// Instantiate the new component.
std::pair<ComponentId, bool> componentIdPair =
this->dataPtr->components[_componentTypeId]->Create(_data);
ComponentKey componentKey{_componentTypeId, componentIdPair.first};
this->dataPtr->entityComponents[_entity].insert(
{_componentTypeId, componentKey});
this->dataPtr->oneTimeChangedComponents.insert(componentKey);
this->dataPtr->entityComponentsDirty = true;
if (componentIdPair.second)
this->RebuildViews();
else
this->UpdateViews(_entity);
return componentKey;
}
/////////////////////////////////////////////////
bool EntityComponentManager::EntityMatches(Entity _entity,
const std::set<ComponentTypeId> &_types) const
{
auto iter = this->dataPtr->entityComponents.find(_entity);
if (iter == this->dataPtr->entityComponents.end())
return false;
// \todo(nkoenig) The performance of this could be improved.
// It might be possible to create bitmask for component sets.
// Fixing this might not be high priority, unless we expect frequent
// creation of entities and/or queries.
for (const ComponentTypeId &type : _types)
{
auto typeIter = iter->second.find(type);
if (typeIter == iter->second.end())
return false;
}
return true;
}
/////////////////////////////////////////////////
ComponentId EntityComponentManager::EntityComponentIdFromType(
const Entity _entity, const ComponentTypeId _type) const
{
auto ecIter = this->dataPtr->entityComponents.find(_entity);
if (ecIter == this->dataPtr->entityComponents.end())
return -1;
auto typeIter = ecIter->second.find(_type);
if (typeIter != ecIter->second.end())
return typeIter->second.second;
return -1;
}
/////////////////////////////////////////////////
const components::BaseComponent
*EntityComponentManager::ComponentImplementation(
const Entity _entity, const ComponentTypeId _type) const
{
IGN_PROFILE("EntityComponentManager::ComponentImplementation");
auto ecIter = this->dataPtr->entityComponents.find(_entity);
if (ecIter == this->dataPtr->entityComponents.end())
return nullptr;
auto typeIter = ecIter->second.find(_type);
if (typeIter != ecIter->second.end())
return this->dataPtr->components.at(typeIter->second.first)->Component(
typeIter->second.second);
return nullptr;
}
/////////////////////////////////////////////////
components::BaseComponent *EntityComponentManager::ComponentImplementation(
const Entity _entity, const ComponentTypeId _type)
{
auto ecIter = this->dataPtr->entityComponents.find(_entity);
if (ecIter == this->dataPtr->entityComponents.end())
return nullptr;
auto typeIter = ecIter->second.find(_type);
if (typeIter != ecIter->second.end())
return this->dataPtr->components.at(typeIter->second.first)->Component(
typeIter->second.second);
return nullptr;
}
/////////////////////////////////////////////////
const components::BaseComponent
*EntityComponentManager::ComponentImplementation(
const ComponentKey &_key) const
{
if (this->dataPtr->components.find(_key.first) !=
this->dataPtr->components.end())
{
return this->dataPtr->components.at(_key.first)->Component(_key.second);
}
return nullptr;
}
/////////////////////////////////////////////////
components::BaseComponent *EntityComponentManager::ComponentImplementation(
const ComponentKey &_key)
{
if (this->dataPtr->components.find(_key.first) !=
this->dataPtr->components.end())
{
return this->dataPtr->components.at(_key.first)->Component(_key.second);
}
return nullptr;
}
/////////////////////////////////////////////////
bool EntityComponentManager::HasComponentType(
const ComponentTypeId _typeId) const
{
return this->dataPtr->components.find(_typeId) !=
this->dataPtr->components.end();
}
/////////////////////////////////////////////////
bool EntityComponentManagerPrivate::CreateComponentStorage(
const ComponentTypeId _typeId)
{
auto storage = components::Factory::Instance()->NewStorage(_typeId);
if (nullptr == storage)
{
ignerr << "Internal errror: failed to create storage for type [" << _typeId
<< "]" << std::endl;
return false;
}
this->components[_typeId] = std::move(storage);
igndbg << "Using components of type [" << _typeId << "] / ["
<< components::Factory::Instance()->Name(_typeId) << "].\n";
return true;
}
/////////////////////////////////////////////////
components::BaseComponent *EntityComponentManager::First(
const ComponentTypeId _componentTypeId)
{
auto iter = this->dataPtr->components.find(_componentTypeId);
if (iter != this->dataPtr->components.end())
{
return iter->second->First();
}
return nullptr;
}
//////////////////////////////////////////////////
const EntityGraph &EntityComponentManager::Entities() const
{
return this->dataPtr->entities;
}
//////////////////////////////////////////////////
bool EntityComponentManager::FindView(const std::set<ComponentTypeId> &_types,
std::map<detail::ComponentTypeKey, detail::View>::iterator &_iter) const
{
std::lock_guard<std::mutex> lockViews(this->dataPtr->viewsMutex);
_iter = this->dataPtr->views.find(_types);
return _iter != this->dataPtr->views.end();
}
//////////////////////////////////////////////////
std::map<detail::ComponentTypeKey, detail::View>::iterator
EntityComponentManager::AddView(const std::set<ComponentTypeId> &_types,
detail::View &&_view) const
{
// If the view already exists, then the map will return the iterator to
// the location that prevented the insertion.
std::lock_guard<std::mutex> lockViews(this->dataPtr->viewsMutex);
return this->dataPtr->views.insert(
std::make_pair(_types, std::move(_view))).first;
}
//////////////////////////////////////////////////
void EntityComponentManager::UpdateViews(const Entity _entity)
{
IGN_PROFILE("EntityComponentManager::UpdateViews");
for (auto &view : this->dataPtr->views)
{
// Add/update the entity if it matches the view.
if (this->EntityMatches(_entity, view.first))
{
view.second.AddEntity(_entity, this->IsNewEntity(_entity));
// If there is a request to delete this entity, update the view as
// well
if (this->IsMarkedForRemoval(_entity))
{
view.second.AddEntityToRemoved(_entity);
}
for (const ComponentTypeId &compTypeId : view.first)
{
view.second.AddComponent(_entity, compTypeId,
this->EntityComponentIdFromType(_entity, compTypeId));
}
}
else
{
view.second.RemoveEntity(_entity, view.first);
}
}
}
//////////////////////////////////////////////////
void EntityComponentManager::RebuildViews()
{
IGN_PROFILE("EntityComponentManager::RebuildViews");
for (auto &view : this->dataPtr->views)
{
view.second.entities.clear();
view.second.components.clear();
// Add all the entities that match the component types to the
// view.
for (const auto &vertex : this->dataPtr->entities.Vertices())
{
Entity entity = vertex.first;
if (this->EntityMatches(entity, view.first))
{
view.second.AddEntity(entity, this->IsNewEntity(entity));
// If there is a request to delete this entity, update the view as
// well
if (this->IsMarkedForRemoval(entity))
{
view.second.AddEntityToRemoved(entity);
}
// Store pointers to all the components. This recursively adds
// all the ComponentTypeTs that belong to the entity to the view.
for (const ComponentTypeId &compTypeId : view.first)
{
view.second.AddComponent(entity, compTypeId,
this->EntityComponentIdFromType(
entity, compTypeId));
}
}
}
}
}
//////////////////////////////////////////////////
void EntityComponentManager::AddEntityToMessage(msgs::SerializedState &_msg,
Entity _entity, const std::unordered_set<ComponentTypeId> &_types) const
{
auto entityMsg = _msg.add_entities();
entityMsg->set_id(_entity);
auto iter = this->dataPtr->entityComponents.find(_entity);
if (iter == this->dataPtr->entityComponents.end())
return;
if (this->dataPtr->toRemoveEntities.find(_entity) !=
this->dataPtr->toRemoveEntities.end())
{
entityMsg->set_remove(true);
}
// Insert all of the entity's components if the passed in types
// set is empty
auto types = _types;
if (types.empty())
{
for (auto &type : this->dataPtr->entityComponents[_entity])
{
types.insert(type.first);
}
}
for (const ComponentTypeId type : types)
{
// If the entity does not have the component, continue
std::unordered_map<ComponentTypeId, ComponentKey>::iterator typeIter =
iter->second.find(type);
if (typeIter == iter->second.end())
{
continue;
}
ComponentKey comp = (typeIter->second);
auto compMsg = entityMsg->add_components();
auto compBase = this->ComponentImplementation(_entity, comp.first);
compMsg->set_type(compBase->TypeId());
std::ostringstream ostr;
compBase->Serialize(ostr);
compMsg->set_component(ostr.str());
// TODO(anyone) Set component being removed once we have a way to queue it
}
}
//////////////////////////////////////////////////
void EntityComponentManager::AddEntityToMessage(msgs::SerializedStateMap &_msg,
Entity _entity, const std::unordered_set<ComponentTypeId> &_types,
bool _full) const
{
auto iter = this->dataPtr->entityComponents.find(_entity);
if (iter == this->dataPtr->entityComponents.end())
return;
// Set the default entity iterator to the end. This will allow us to know
// if the entity has been added to the message.
auto entIter = _msg.mutable_entities()->end();
// Add an entity to the message and set it to be removed if the entity
// exists in the toRemoveEntities list.
if (this->dataPtr->toRemoveEntities.find(_entity) !=
this->dataPtr->toRemoveEntities.end())
{
// Find the entity in the message, and add if not present.
entIter = _msg.mutable_entities()->find(_entity);
if (entIter == _msg.mutable_entities()->end())
{
msgs::SerializedEntityMap ent;
ent.set_id(_entity);
(*_msg.mutable_entities())[static_cast<uint64_t>(_entity)] = ent;
entIter = _msg.mutable_entities()->find(_entity);
}
entIter->second.set_remove(true);
}
// Insert all of the entity's components if the passed in types
// set is empty
auto types = _types;
if (types.empty())
{
for (auto &type : this->dataPtr->entityComponents[_entity])
{
types.insert(type.first);
}
}
// Empty means all types
for (const ComponentTypeId type : types)
{
std::unordered_map<ComponentTypeId, ComponentKey>::iterator typeIter =
iter->second.find(type);
if (typeIter == iter->second.end())
{
continue;
}
ComponentKey comp = typeIter->second;
const components::BaseComponent *compBase =
this->ComponentImplementation(_entity, comp.first);
// If not sending full state, skip unchanged components
if (!_full &&
this->dataPtr->oneTimeChangedComponents.find(comp) ==
this->dataPtr->oneTimeChangedComponents.end() &&
this->dataPtr->periodicChangedComponents.find(comp) ==
this->dataPtr->periodicChangedComponents.end())
{
continue;
}
/// Find the entity in the message, if not already found.
/// Add the entity to the message, if not already added.
if (entIter == _msg.mutable_entities()->end())
{
entIter = _msg.mutable_entities()->find(_entity);
if (entIter == _msg.mutable_entities()->end())
{
msgs::SerializedEntityMap ent;
ent.set_id(_entity);
(*_msg.mutable_entities())[static_cast<uint64_t>(_entity)] = ent;
entIter = _msg.mutable_entities()->find(_entity);
}
}
auto compIter = entIter->second.mutable_components()->find(comp.first);
// Find the component in the message, and add the component to the
// message if it's not present.
if (compIter == entIter->second.mutable_components()->end())
{
msgs::SerializedComponent cmp;
cmp.set_type(compBase->TypeId());
(*(entIter->second.mutable_components()))[
static_cast<int64_t>(comp.first)] = cmp;
compIter = entIter->second.mutable_components()->find(comp.first);
}
// Serialize and store the message
std::ostringstream ostr;
compBase->Serialize(ostr);
compIter->second.set_component(ostr.str());
// TODO(anyone) Set component being removed once we have a way to queue it
}
}
//////////////////////////////////////////////////
ignition::msgs::SerializedState EntityComponentManager::ChangedState() const
{
ignition::msgs::SerializedState stateMsg;
// New entities
for (const auto &entity : this->dataPtr->newlyCreatedEntities)
{
this->AddEntityToMessage(stateMsg, entity);
}
// Entities being removed
for (const auto &entity : this->dataPtr->toRemoveEntities)
{
this->AddEntityToMessage(stateMsg, entity);
}
// TODO(anyone) New / removed / changed components
return stateMsg;
}
//////////////////////////////////////////////////
void EntityComponentManager::ChangedState(
ignition::msgs::SerializedStateMap &_state) const
{
// New entities
for (const auto &entity : this->dataPtr->newlyCreatedEntities)
{
this->AddEntityToMessage(_state, entity);
}
// Entities being removed
for (const auto &entity : this->dataPtr->toRemoveEntities)
{
this->AddEntityToMessage(_state, entity);
}
// TODO(anyone) New / removed / changed components
}
//////////////////////////////////////////////////
void EntityComponentManagerPrivate::CalculateStateThreadLoad()
{
// If the entity component vector is dirty, we need to recalculate the
// threads and each threads work load
if (!this->entityComponentsDirty)
return;
this->entityComponentsDirty = false;
this->entityComponentIterators.clear();
auto startIt = this->entityComponents.begin();
int numComponents = this->entityComponents.size();
// Set the number of threads to spawn to the min of the calculated thread
// count or max threads that the hardware supports
int maxThreads = std::thread::hardware_concurrency();
uint64_t numThreads = std::min(numComponents, maxThreads);
int componentsPerThread = std::ceil(static_cast<double>(numComponents) /
numThreads);
igndbg << "Updated state thread iterators: " << numThreads
<< " threads processing around " << componentsPerThread
<< " components each." << std::endl;
// Push back the starting iterator
this->entityComponentIterators.push_back(startIt);
for (uint64_t i = 0; i < numThreads; ++i)
{
// If we have added all of the components to the iterator vector, we are
// done so push back the end iterator
numComponents -= componentsPerThread;
if (numComponents <= 0)
{
this->entityComponentIterators.push_back(
this->entityComponents.end());
break;
}
// Get the iterator to the next starting group of components
auto nextIt = std::next(startIt, componentsPerThread);
this->entityComponentIterators.push_back(nextIt);
startIt = nextIt;
}
}
//////////////////////////////////////////////////
ignition::msgs::SerializedState EntityComponentManager::State(
const std::unordered_set<Entity> &_entities,
const std::unordered_set<ComponentTypeId> &_types) const
{
ignition::msgs::SerializedState stateMsg;
for (const auto &it : this->dataPtr->entityComponents)
{
auto entity = it.first;
if (!_entities.empty() && _entities.find(entity) == _entities.end())
{
continue;
}
this->AddEntityToMessage(stateMsg, entity, _types);
}
return stateMsg;
}
//////////////////////////////////////////////////
void EntityComponentManager::State(