0.2.0
Loading...
Searching...
No Matches
Node.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <imgui.h>
17#include <imgui_node_editor.h>
18#include <imgui_stdlib.h>
19
20#include "internal/Node/Pin.hpp"
22
23#include "util/Logger.hpp"
24
25#include <string>
26#include <vector>
27#include <deque>
28#include <thread>
29#include <mutex>
30#include <condition_variable>
31#include <atomic>
32#include <chrono>
33#include <map>
34
35#include <nlohmann/json.hpp>
36using json = nlohmann::json;
37
38namespace NAV
39{
40class Node;
41class NodeData;
42class GroupBox;
43
44namespace NodeRegistry
45{
46
47void RegisterNodeTypes(); // NOLINT(readability-redundant-declaration) - false warning. This is needed for the friend declaration below
48
49} // namespace NodeRegistry
50
51namespace FlowExecutor
52{
53
55void execute(); // NOLINT(readability-redundant-declaration) - false warning. This is needed for the friend declaration below
56
58void deinitialize(); // NOLINT(readability-redundant-declaration) - false warning. This is needed for the friend declaration below
59
60} // namespace FlowExecutor
61
62namespace gui
63{
64class NodeEditorApplication;
65
66namespace menus
67{
68
69void ShowRunMenu();
70
71} // namespace menus
72
73} // namespace gui
74
78void to_json(json& j, const Node& node);
82void from_json(const json& j, Node& node);
83
85class Node
86{
87 public:
89 struct Kind
90 {
92 enum Value : uint8_t
93 {
97 };
98
100 Kind() = default;
101
104 constexpr Kind(Value kind) // NOLINT(hicpp-explicit-conversions, google-explicit-constructor)
105 : value(kind)
106 {}
107
110 explicit Kind(const std::string& string)
111 {
112 if (string == "Blueprint")
113 {
114 value = Kind::Blueprint;
115 }
116 else if (string == "Simple")
117 {
118 value = Kind::Simple;
119 }
120 else if (string == "GroupBox")
121 {
122 value = Kind::GroupBox;
123 }
124 }
125
127 explicit operator Value() const { return value; }
129 explicit operator bool() = delete;
134 {
135 value = v;
136 return *this;
137 }
138
139 friend constexpr bool operator==(const Node::Kind& lhs, const Node::Kind& rhs);
140 friend constexpr bool operator!=(const Node::Kind& lhs, const Node::Kind& rhs);
141
142 friend constexpr bool operator==(const Node::Kind& lhs, const Node::Kind::Value& rhs);
143 friend constexpr bool operator==(const Node::Kind::Value& lhs, const Node::Kind& rhs);
144 friend constexpr bool operator!=(const Node::Kind& lhs, const Node::Kind::Value& rhs);
145 friend constexpr bool operator!=(const Node::Kind::Value& lhs, const Node::Kind& rhs);
146
149 explicit operator std::string() const
150 {
151 switch (value)
152 {
153 case Kind::Blueprint:
154 return "Blueprint";
155 case Kind::Simple:
156 return "Simple";
157 case Kind::GroupBox:
158 return "GroupBox";
159 }
160 return "";
161 }
162
163 private:
165 Value value;
166 };
167
169 enum class State
170 {
171 Disabled,
178 DoShutdown,
179 Shutdown,
180 };
181
183 enum class Mode
184 {
185 REAL_TIME,
187 };
188
191 explicit Node(std::string name);
193 virtual ~Node();
195 Node(const Node&) = delete;
197 Node(Node&&) = delete;
199 Node& operator=(const Node&) = delete;
201 Node& operator=(Node&&) = delete;
202
203 /* -------------------------------------------------------------------------------------------------------- */
204 /* Interface */
205 /* -------------------------------------------------------------------------------------------------------- */
206
208 [[nodiscard]] virtual std::string type() const = 0;
209
212 virtual void guiConfig();
213
215 [[nodiscard]] virtual json save() const;
216
219 virtual void restore(const json& j);
220
223 virtual void restoreAtferLink(const json& j);
224
226 virtual bool initialize();
227
229 virtual void deinitialize();
230
232 virtual bool resetNode();
233
238 virtual bool onCreateLink(OutputPin& startPin, InputPin& endPin);
239
243 virtual void onDeleteLink(OutputPin& startPin, InputPin& endPin);
244
248 virtual void afterCreateLink(OutputPin& startPin, InputPin& endPin);
249
253 virtual void afterDeleteLink(OutputPin& startPin, InputPin& endPin);
254
256 virtual void flush();
257
258 /* -------------------------------------------------------------------------------------------------------- */
259 /* Member functions */
260 /* -------------------------------------------------------------------------------------------------------- */
261
266 void notifyOutputValueChanged(size_t pinIdx, const InsTime& insTime, const std::scoped_lock<std::mutex>& guard);
267
270 [[nodiscard]] std::scoped_lock<std::mutex> requestOutputValueLock(size_t pinIdx);
271
276 template<typename T>
277 [[nodiscard]] std::optional<InputPin::IncomingLink::ValueWrapper<T>> getInputValue(size_t portIndex) const
278 {
279 return inputPins.at(portIndex).link.getValue<T>();
280 }
281
284 void releaseInputValue(size_t portIndex);
285
289 void invokeCallbacks(size_t portIndex, const std::shared_ptr<const NodeData>& data);
290
294 [[nodiscard]] InputPin& inputPinFromId(ax::NodeEditor::PinId pinId);
295
299 [[nodiscard]] OutputPin& outputPinFromId(ax::NodeEditor::PinId pinId);
300
304 [[nodiscard]] size_t inputPinIndexFromId(ax::NodeEditor::PinId pinId) const;
305
309 [[nodiscard]] size_t outputPinIndexFromId(ax::NodeEditor::PinId pinId) const;
310
312 [[nodiscard]] std::string nameId() const;
313
315 [[nodiscard]] const ImVec2& getSize() const;
316
317 // ------------------------------------------ State handling ---------------------------------------------
318
322 static std::string toString(State state);
323
325 [[nodiscard]] State getState() const;
326
328 [[nodiscard]] Mode getMode() const;
329
333 bool doInitialize(bool wait = false);
334
338 bool doReinitialize(bool wait = false);
339
343 bool doDeinitialize(bool wait = false);
344
348 bool doDisable(bool wait = false);
349
352 bool doEnable();
353
356
358 [[nodiscard]] bool isDisabled() const;
359
361 [[nodiscard]] bool isInitialized() const;
362
364 [[nodiscard]] bool isTransient() const;
365
367 [[nodiscard]] bool isOnlyRealtime() const;
368
369 /* -------------------------------------------------------------------------------------------------------- */
370 /* Member variables */
371 /* -------------------------------------------------------------------------------------------------------- */
372
374 ax::NodeEditor::NodeId id = 0;
378 std::string name;
380 std::vector<InputPin> inputPins;
382 std::vector<OutputPin> outputPins;
383
385 bool callbacksEnabled = false;
386
388 std::multimap<InsTime, std::pair<OutputPin*, size_t>> pollEvents;
389
390 protected:
393 ImVec2 _guiConfigDefaultWindowSize{ 500.0F, 400.0F };
394
396 bool _hasConfig = false;
397
400
402 bool _onlyRealTime = false;
403
404 private:
405 State _state = State::Deinitialized;
406 mutable std::mutex _stateMutex;
407
409 std::atomic<Mode> _mode = Mode::REAL_TIME;
410
412 bool _reinitialize = false;
413
415 bool _disable = false;
416
418 bool _showConfig = false;
419
421 std::mutex _configWindowMutex;
423 bool _configWindowForceCollapse = false;
425 bool _configWindowIsCollapsed = false;
426
428 bool _configWindowFocus = false;
429
431 ImVec2 _size{ 0, 0 };
432
434 static inline bool _autostartWorker = true;
435
436 std::chrono::duration<int64_t> _workerTimeout = std::chrono::minutes(1);
437 std::thread _worker;
438 std::mutex _workerMutex;
439 std::condition_variable _workerConditionVariable;
440 bool _workerWakeup = false;
441
444 static void workerThread(Node* node);
445
447 virtual void workerTimeoutHandler();
448
451 bool workerInitializeNode();
452
455 bool workerDeinitializeNode();
456
457 friend class gui::NodeEditorApplication;
458 friend class NAV::GroupBox;
459
461 friend void NAV::FlowExecutor::execute();
463 friend void NAV::FlowExecutor::deinitialize();
465 friend void NAV::NodeRegistry::RegisterNodeTypes();
466
470 friend void NAV::to_json(json& j, const Node& node);
474 friend void NAV::from_json(const json& j, Node& node);
475
478};
479
484constexpr bool operator==(const Node::Kind& lhs, const Node::Kind& rhs) { return lhs.value == rhs.value; }
489constexpr bool operator!=(const Node::Kind& lhs, const Node::Kind& rhs) { return !(lhs == rhs); }
490
495constexpr bool operator==(const Node::Kind& lhs, const Node::Kind::Value& rhs) { return lhs.value == rhs; }
500constexpr bool operator==(const Node::Kind::Value& lhs, const Node::Kind& rhs) { return lhs == rhs.value; }
505constexpr bool operator!=(const Node::Kind& lhs, const Node::Kind::Value& rhs) { return !(lhs == rhs); }
510constexpr bool operator!=(const Node::Kind::Value& lhs, const Node::Kind& rhs) { return !(lhs == rhs); }
511
512} // namespace NAV
nlohmann::json json
json namespace
Definition FlowManager.hpp:21
The class is responsible for all time-related tasks.
Utility class for logging to console and file.
void to_json(json &j, const Node &node)
Converts the provided node into a json object.
void from_json(const json &j, Node &node)
Converts the provided json object into a node object.
void RegisterNodeTypes()
Register all available Node types for the program.
void execute()
Main task of the FlowExecutor thread.
Pin class.
Group Box.
Definition GroupBox.hpp:22
Input pins of nodes.
Definition Pin.hpp:491
The class is responsible for all time-related tasks.
Definition InsTime.hpp:667
Abstract parent class for all nodes.
Definition Node.hpp:86
bool isDisabled() const
Checks if the node is disabled.
bool isOnlyRealtime() const
Checks if the node is only working in real time (sensors, network interfaces, ...)
State
Possible states of the node.
Definition Node.hpp:170
@ DoInitialize
Node should be initialized.
@ Shutdown
Node is shutting down.
@ DoShutdown
Node should shut down.
@ Initializing
Node is currently initializing.
@ Initialized
Node is initialized (green)
@ DoDeinitialize
Node should be deinitialized.
@ Disabled
Node is disabled and won't be initialized.
@ Deinitializing
Node is currently deinitializing.
@ Deinitialized
Node is deinitialized (red)
bool isInitialized() const
Checks if the node is initialized.
bool doDeinitialize(bool wait=false)
Asks the node worker to deinitialize the node.
void releaseInputValue(size_t portIndex)
Unblocks the connected node. Has to be called when the input value should be released and getInputVal...
bool doDisable(bool wait=false)
Asks the node worker to disable the node.
void wakeWorker()
Wakes the worker thread.
OutputPin & outputPinFromId(ax::NodeEditor::PinId pinId)
Returns the pin with the given id.
virtual void onDeleteLink(OutputPin &startPin, InputPin &endPin)
Called when a link is to be deleted.
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:393
const ImVec2 & getSize() const
Get the size of the node.
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:382
void notifyOutputValueChanged(size_t pinIdx, const InsTime &insTime, const std::scoped_lock< std::mutex > &guard)
Notifies connected nodes about the change.
Mode
Different Modes the Node can work in.
Definition Node.hpp:184
@ POST_PROCESSING
Node running in post-processing mode.
@ REAL_TIME
Node running in real-time mode.
Node(Node &&)=delete
Move constructor.
virtual void restore(const json &j)
Restores the node from a json object.
Node(std::string name)
Constructor.
virtual void afterCreateLink(OutputPin &startPin, InputPin &endPin)
Called when a new link was established.
virtual void deinitialize()
Deinitialize the Node.
virtual bool initialize()
Initialize the Node.
std::optional< InputPin::IncomingLink::ValueWrapper< T > > getInputValue(size_t portIndex) const
Get Input Value connected on the pin. Only const data types.
Definition Node.hpp:277
std::multimap< InsTime, std::pair< OutputPin *, size_t > > pollEvents
Map with callback events (sorted by time)
Definition Node.hpp:388
virtual void afterDeleteLink(OutputPin &startPin, InputPin &endPin)
Called when a link was deleted.
Kind kind
Kind of the Node.
Definition Node.hpp:376
Node & operator=(Node &&)=delete
Move assignment operator.
static std::string toString(State state)
Converts the state into a printable text.
InputPin & inputPinFromId(ax::NodeEditor::PinId pinId)
Returns the pin with the given id.
size_t outputPinIndexFromId(ax::NodeEditor::PinId pinId) const
Returns the index of the pin.
std::vector< InputPin > inputPins
List of input pins.
Definition Node.hpp:380
bool doInitialize(bool wait=false)
Asks the node worker to initialize the node.
virtual json save() const
Saves the node into a json object.
bool callbacksEnabled
Enables the callbacks.
Definition Node.hpp:385
std::string nameId() const
Node name and id.
bool _lockConfigDuringRun
Lock the config when executing post-processing.
Definition Node.hpp:399
virtual void restoreAtferLink(const json &j)
Restores link related properties of the node from a json object.
size_t inputPinIndexFromId(ax::NodeEditor::PinId pinId) const
Returns the index of the pin.
Node & operator=(const Node &)=delete
Copy assignment operator.
friend void gui::menus::ShowRunMenu()
Show the run menu dropdown.
std::string name
Name of the Node.
Definition Node.hpp:378
bool _onlyRealTime
Whether the node can run in post-processing or only real-time.
Definition Node.hpp:402
bool doEnable()
Enable the node.
virtual void flush()
Function called by the flow executer after finishing to flush out remaining data.
bool doReinitialize(bool wait=false)
Asks the node worker to reinitialize the node.
Node(const Node &)=delete
Copy constructor.
virtual ~Node()
Destructor.
std::scoped_lock< std::mutex > requestOutputValueLock(size_t pinIdx)
Blocks the thread till the output values was read by all connected nodes.
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
virtual bool resetNode()
Resets the node. It is guaranteed that the node is initialized when this is called.
State getState() const
Get the current state of the node.
Mode getMode() const
Get the current mode of the node.
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:396
virtual std::string type() const =0
String representation of the Class Type.
virtual void guiConfig()
ImGui config window which is shown on double click.
virtual bool onCreateLink(OutputPin &startPin, InputPin &endPin)
Called when a new link is to be established.
bool isTransient() const
Checks if the node is changing its state currently.
Output pins of nodes.
Definition Pin.hpp:338
Application class providing all relevant GUI callbacks.
Definition NodeEditorApplication.hpp:35
bool operator==(const ImVec4 &lhs, const ImVec4 &rhs)
Equal comparison operator.
bool operator!=(const ImVec4 &lhs, const ImVec4 &rhs)
Unequal comparison operator.
Kind information class.
Definition Node.hpp:90
constexpr Kind(Value kind)
Implicit Constructor from Value type.
Definition Node.hpp:104
Kind()=default
Default Constructor.
Value
Possible kinds of Nodes.
Definition Node.hpp:93
@ Blueprint
Node with header.
Definition Node.hpp:94
@ GroupBox
Group box which can group other nodes and drag them together.
Definition Node.hpp:96
@ Simple
Node without header, which displays its name in the center of the content.
Definition Node.hpp:95
Kind & operator=(Value v)
Assignment operator from Value type.
Definition Node.hpp:133
friend constexpr bool operator==(const Node::Kind &lhs, const Node::Kind &rhs)
Equal compares Node::Kind values.
Definition Node.hpp:484
Kind(const std::string &string)
Constructor from std::string.
Definition Node.hpp:110
friend constexpr bool operator!=(const Node::Kind &lhs, const Node::Kind &rhs)
Inequal compares Node::Kind values.
Definition Node.hpp:489