Overte C++ Documentation
Scene.h
1 //
2 // Scene.h
3 // render/src/render
4 //
5 // Created by Sam Gateau on 1/11/15.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2024 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_render_Scene_h
14 #define hifi_render_Scene_h
15 
16 #include "Item.h"
17 #include "SpatialTree.h"
18 #include "Stage.h"
19 #include "Selection.h"
20 #include "Transition.h"
21 #include "HighlightStyle.h"
22 
23 namespace render {
24 
25 class RenderEngine;
26 class Scene;
27 
40 class Transaction {
41  friend class Scene;
42 public:
43 
44  typedef std::function<void(ItemID, const Transition*)> TransitionQueryFunc;
45  typedef std::function<void()> TransitionFinishedFunc;
46  typedef std::function<void(HighlightStyle const*)> SelectionHighlightQueryFunc;
47 
48  Transaction() {}
49  ~Transaction() {}
50 
51  // Item transactions
52 
58  void resetItem(ItemID id, const PayloadPointer& payload);
59 
64  void removeItem(ItemID id);
65 
69  bool hasRemovedItems() const { return !_removedItems.empty(); }
70 
79  template <class T> void updateItem(ItemID id, std::function<void(T&)> func) {
80  updateItem(id, std::make_shared<UpdateFunctor<T>>(func));
81  }
82 
83 
90  void updateItem(ItemID id) { updateItem(id, nullptr); }
91 
92  // Transition (applied to an item) transactions
93 
102  void resetTransitionOnItem(ItemID id, TransitionType transition, ItemID boundId = render::Item::INVALID_ITEM_ID);
103 
110  void removeTransitionFromItem(ItemID id);
111 
120  void setTransitionFinishedOperator(ItemID id, TransitionFinishedFunc func);
121 
128  void queryTransitionOnItem(ItemID id, TransitionQueryFunc func);
129 
130  // Selection transactions
131 
137  void resetSelection(const Selection& selection);
138 
145  void resetSelectionHighlight(const std::string& selectionName, const HighlightStyle& style = HighlightStyle());
146 
152  void removeHighlightFromSelection(const std::string& selectionName);
153 
161  void querySelectionHighlight(const std::string& selectionName, const SelectionHighlightQueryFunc& func);
162 
171  void reserve(const std::vector<Transaction>& transactionContainer);
172 
180  void mergeByCopying(const std::vector<Transaction>& transactionContainer);
181 
189  void mergeByMoving(std::vector<Transaction>&& transactionContainer);
190 
196  void mergeByCopying(const Transaction& transaction);
197 
203  void mergeByMoving(Transaction&& transaction);
204 
208  void clear();
209 
210 private:
211 
219  void updateItem(ItemID id, const UpdateFunctorPointer& functor);
220 
221  using Reset = std::tuple<ItemID, PayloadPointer>;
222  using Remove = ItemID;
223  using Update = std::tuple<ItemID, UpdateFunctorPointer>;
224 
225  using TransitionReset = std::tuple<ItemID, TransitionType, ItemID>;
226  using TransitionRemove = ItemID;
227  using TransitionFinishedOperator = std::tuple<ItemID, TransitionFinishedFunc>;
228  using TransitionQuery = std::tuple<ItemID, TransitionQueryFunc>;
229 
230  using SelectionReset = Selection;
231 
232  using HighlightReset = std::tuple<std::string, HighlightStyle>;
233  using HighlightRemove = std::string;
234  using HighlightQuery = std::tuple<std::string, SelectionHighlightQueryFunc>;
235 
236  using Resets = std::vector<Reset>;
237  using Removes = std::vector<Remove>;
238  using Updates = std::vector<Update>;
239 
240  using TransitionResets = std::vector<TransitionReset>;
241  using TransitionRemoves = std::vector<TransitionRemove>;
242  using TransitionFinishedOperators = std::vector<TransitionFinishedOperator>;
243  using TransitionQueries = std::vector<TransitionQuery>;
244 
245  using SelectionResets = std::vector<SelectionReset>;
246 
247  using HighlightResets = std::vector<HighlightReset>;
248  using HighlightRemoves = std::vector<HighlightRemove>;
249  using HighlightQueries = std::vector<HighlightQuery>;
250 
251  Resets _resetItems;
252  Removes _removedItems;
253  Updates _updatedItems;
254 
255  TransitionResets _resetTransitions;
256  TransitionRemoves _removeTransitions;
257  TransitionFinishedOperators _transitionFinishedOperators;
258  TransitionQueries _queriedTransitions;
259 
260  SelectionResets _resetSelections;
261 
262  HighlightResets _highlightResets;
263  HighlightRemoves _highlightRemoves;
264  HighlightQueries _highlightQueries;
265 };
266 typedef std::vector<Transaction> TransactionQueue;
267 
268 
274 class Scene {
275 public:
280  Scene(glm::vec3 origin, float size);
281  ~Scene();
282 
289  ItemID allocateID();
290 
296  bool isAllocatedID(const ItemID& id) const;
297 
303  size_t getNumItems() const { return _numAllocatedItems.load(); }
304 
311  void enqueueTransaction(const Transaction& transaction);
312 
319  void enqueueTransactionMove(Transaction&& transaction);
320 
327  uint32_t enqueueFrame();
328 
336 
344  Selection getSelection(const Selection::Name& name) const;
345 
353  bool isSelectionEmpty(const Selection::Name& name) const;
354 
362  void addItemToSelection(const std::string& selectionName, ItemID itemID);
363 
370  void removeSelection(const std::string& selectionName);
371 
372  // Following call are NOT threadsafe, you have to call them from the correct thread to avoid any potential issues.
373 
382  const Item& getItem(const ItemID& id) const { return _items[id]; }
383 
391  const Item getItemSafe(const ItemID& id) const { if (isAllocatedID(id)) { return _items[id]; } else { return Item(); } }
392 
399  const ItemSpatialTree& getSpatialTree() const { return _primarySpatialTree; }
400 
407  const ItemIDSet& getNonspatialSet() const { return _primaryNonspatialSet; }
408 
416  StagePointer getStage(const Stage::Name& name) const;
417 
426  template <class T>
427  std::shared_ptr<T> getStage(const Stage::Name& name = T::getName()) const {
428  auto stage = getStage(name);
429  return (stage ? std::static_pointer_cast<T>(stage) : std::shared_ptr<T>());
430  }
431 
439  void resetStage(const Stage::Name& name, const StagePointer& stage);
440 
449  void simulate(ItemID id, RenderArgs* args) { _items[id].renderSimulate(args); }
450 
460  HighlightStyle getOutlineStyle(ItemID id, const ViewFrustum& viewFrustum, uint16_t height) { return _items[id].getOutlineStyle(viewFrustum, height); }
461 
462 private:
470  void setItemTransition(ItemID id, Index transitionId);
471 
478  void removeItemTransition(ItemID id);
479 
480 
481  // Thread safe elements that can be accessed from anywhere
482  std::atomic<unsigned int> _IDAllocator{ 1 }; // first valid itemID will be One
483  std::atomic<unsigned int> _numAllocatedItems{ 1 }; // num of allocated items, matching the _items.size()
484 
486  std::mutex _transactionQueueMutex;
487 
490  TransactionQueue _transactionQueue;
491 
493  std::mutex _transactionFramesMutex;
494 
495  using TransactionFrames = std::vector<Transaction>;
496 
498  TransactionFrames _transactionFrames;
499 
501  uint32_t _transactionFrameNumber{ 0 };
502 
508  void processTransactionFrame(const Transaction& transaction);
509 
510  // The actual database
511 
513  std::mutex _itemsMutex;
514 
516  Item::Vector _items;
517 
519  ItemSpatialTree _primarySpatialTree;
520 
522  ItemIDSet _primaryNonspatialSet;
523 
528  void resetItems(const Transaction::Resets& transactions);
529 
534  void resetTransitionFinishedOperator(const Transaction::TransitionFinishedOperators& transactions);
535 
540  void removeItems(const Transaction::Removes& transactions);
541 
548  void updateItems(const Transaction::Updates& transactions);
549 
554  void resetTransitionItems(const Transaction::TransitionResets& transactions);
555 
560  void removeTransitionItems(const Transaction::TransitionRemoves& transactions);
561 
566  void queryTransitionItems(const Transaction::TransitionQueries& transactions);
567 
572  void resetHighlights(const Transaction::HighlightResets& transactions);
573 
578  void removeHighlights(const Transaction::HighlightRemoves& transactions);
579 
584  void queryHighlights(const Transaction::HighlightQueries& transactions);
585 
591  void collectSubItems(ItemID parentId, ItemIDs& subItems) const;
592 
593  // The Selection map
594  mutable std::mutex _selectionsMutex; // mutable so it can be used in the thread safe getSelection const method
595  SelectionMap _selections;
596 
598  std::unordered_map<int32_t, std::vector<Transaction::TransitionFinishedFunc>> _transitionFinishedOperatorMap;
599 
604  void resetSelections(const Transaction::SelectionResets& transactions);
605  // More actions coming to selections soon:
606  // void removeFromSelection(const Selection& selection);
607  // void appendToSelection(const Selection& selection);
608  // void mergeWithSelection(const Selection& selection);
609 
610  // The Stage map
613  mutable std::mutex _stagesMutex;
614 
616  StageMap _stages;
617 
618 
619  friend class RenderEngine;
620 };
621 
622 typedef std::shared_ptr<Scene> ScenePointer;
623 typedef std::vector<ScenePointer> Scenes;
624 
625 }
626 
627 #endif // hifi_render_Scene_h
Definition: Args.h:100
Scene is a container for Items. Items are introduced, modified or erased in the scene through Transac...
Definition: Scene.h:274
const Item getItemSafe(const ItemID &id) const
Same as getItem, checking if the id is valid.
Definition: Scene.h:391
void addItemToSelection(const std::string &selectionName, ItemID itemID)
Add a single item to a selection by name.
Definition: Scene.cpp:612
StagePointer getStage(const Stage::Name &name) const
Access a particular Stage (empty pointer if it doesn't exist).
Definition: Scene.cpp:630
ItemID allocateID()
Get a new, unique item ID.
Definition: Scene.cpp:195
size_t getNumItems() const
Get the total number of allocated items, this a threadsafe call.
Definition: Scene.h:303
void simulate(ItemID id, RenderArgs *args)
Perform renderer-side simulation of a given item.
Definition: Scene.h:449
void enqueueTransaction(const Transaction &transaction)
Enqueue transaction to the scene.
Definition: Scene.cpp:205
bool isAllocatedID(const ItemID &id) const
Check that the ID is valid and allocated for this scene, this a threadsafe call.
Definition: Scene.cpp:200
const ItemSpatialTree & getSpatialTree() const
Access the spatialized items.
Definition: Scene.h:399
void removeSelection(const std::string &selectionName)
Remove a selection by name.
Definition: Scene.cpp:623
Scene(glm::vec3 origin, float size)
Definition: Scene.cpp:185
void resetStage(const Stage::Name &name, const StagePointer &stage)
Assign a given instance of a stage object to a given name.
Definition: Scene.cpp:641
Selection getSelection(const Selection::Name &name) const
Access a particular selection (empty if doesn't exist)
Definition: Scene.cpp:580
uint32_t enqueueFrame()
Enqueue end of frame transactions boundary.
Definition: Scene.cpp:215
bool isSelectionEmpty(const Selection::Name &name) const
Check if a particular selection is empty (returns ).
Definition: Scene.cpp:590
HighlightStyle getOutlineStyle(ItemID id, const ViewFrustum &viewFrustum, uint16_t height)
Returns outline style properties for a given item.
Definition: Scene.h:460
void enqueueTransactionMove(Transaction &&transaction)
Enqueue transaction to the scene by moving it.
Definition: Scene.cpp:210
const Item & getItem(const ItemID &id) const
Access a particular item from its ID.
Definition: Scene.h:382
std::shared_ptr< T > getStage(const Stage::Name &name=T::getName()) const
Access stage of a given type.
Definition: Scene.h:427
const ItemIDSet & getNonspatialSet() const
Access non-spatialized items (layered objects, backgrounds).
Definition: Scene.h:407
void processTransactionQueue()
Process the pending transactions queued.
Definition: Scene.cpp:234
Transaction is the mechanism to make any change to the scene. Whenever a new item need to be reset,...
Definition: Scene.h:40
void setTransitionFinishedOperator(ItemID id, TransitionFinishedFunc func)
The transaction will add a function to be called when fade effect finishes for a given item.
Definition: Scene.cpp:47
void removeHighlightFromSelection(const std::string &selectionName)
The transaction will remove highlight from a selection with specified name.
Definition: Scene.cpp:63
void resetItem(ItemID id, const PayloadPointer &payload)
The transaction will replace item's payload with new one.
Definition: Scene.cpp:22
void mergeByMoving(std::vector< Transaction > &&transactionContainer)
Moves content of given transactions to the current one and then clears input vector.
Definition: Scene.cpp:119
void mergeByCopying(const std::vector< Transaction > &transactionContainer)
Adds content of given transactions to the current one.
Definition: Scene.cpp:111
void removeItem(ItemID id)
The transaction will remove given item.
Definition: Scene.cpp:31
void querySelectionHighlight(const std::string &selectionName, const SelectionHighlightQueryFunc &func)
The transaction will add a function to be called on the render thread that will be provided with curr...
Definition: Scene.cpp:67
void updateItem(ItemID id)
The transaction will update an item without running a function.
Definition: Scene.h:90
bool hasRemovedItems() const
Definition: Scene.h:69
void updateItem(ItemID id, std::function< void(T &)> func)
The transaction will execute a function on the render thread to update the given render item.
Definition: Scene.h:79
void removeTransitionFromItem(ItemID id)
The transaction will remove fade effect for a given item.
Definition: Scene.cpp:39
void resetSelectionHighlight(const std::string &selectionName, const HighlightStyle &style=HighlightStyle())
The transaction will set a highlight style for a selection with a given name.
Definition: Scene.cpp:59
void queryTransitionOnItem(ItemID id, TransitionQueryFunc func)
The transaction will add a function to be called on the render thread that will be provided with curr...
Definition: Scene.cpp:43
void reserve(const std::vector< Transaction > &transactionContainer)
Reserves space needed to fit content of provided transactions.
Definition: Scene.cpp:71
void clear()
Clears the content of this transaction.
Definition: Scene.cpp:170
void resetTransitionOnItem(ItemID id, TransitionType transition, ItemID boundId=render::Item::INVALID_ITEM_ID)
The transaction will start a new fade effect on a given item.
Definition: Scene.cpp:35
void resetSelection(const Selection &selection)
The transaction will replace selection with a given name with the new one.
Definition: Scene.cpp:55