0.4.1
Loading...
Searching...
No Matches
Merger.cpp
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
9#include "Merger.hpp"
10
12namespace nm = NAV::NodeManager;
14
15#include "NodeRegistry.hpp"
16
17#include "NodeData/NodeData.hpp"
18
20 : Node(typeStatic())
21{
22 LOG_TRACE("{}: called", name);
23
24 _hasConfig = false;
25 kind = Kind::Simple;
26
30}
31
33{
34 LOG_TRACE("{}: called", nameId());
35}
36
38{
39 return "Merger";
40}
41
42std::string NAV::Merger::type() const
43{
44 return typeStatic();
45}
46
48{
49 return "Utility";
50}
51
52void NAV::Merger::setPinIdentifiers(size_t connectedPinIndex, size_t otherPinIndex, const std::vector<std::string>& dataIdentifiers)
53{
54 LOG_DEBUG("{}: Setting DataIdentifier on pinIndex {}", nameId(), otherPinIndex);
55
56 inputPins.at(connectedPinIndex).dataIdentifier = dataIdentifiers;
57
58 if (!inputPins.at(otherPinIndex).isPinLinked())
59 {
60 inputPins.at(otherPinIndex).dataIdentifier = dataIdentifiers;
61 for (const auto& dataIdentifier : dataIdentifiers)
62 {
63 auto parentIdentifiers = NodeRegistry::GetParentNodeDataTypes(dataIdentifier);
64 std::erase(parentIdentifiers, NodeData::type());
65 inputPins.at(otherPinIndex).dataIdentifier.insert(inputPins.at(otherPinIndex).dataIdentifier.end(), parentIdentifiers.rbegin(), parentIdentifiers.rend());
66 }
67
68 // Update the dataIdentifier of the output pin to the same as input pin
69 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = dataIdentifiers;
70 }
71 else
72 {
73 std::vector<std::string> combinedIdentifiers;
74 for (const auto& dataIdentifier : inputPins.at(connectedPinIndex).dataIdentifier)
75 {
76 if (auto iter = std::find(inputPins.at(otherPinIndex).dataIdentifier.begin(), inputPins.at(otherPinIndex).dataIdentifier.end(), dataIdentifier);
77 iter != inputPins.at(otherPinIndex).dataIdentifier.end())
78 {
79 combinedIdentifiers.push_back(*iter);
80 }
81 }
82 if (!combinedIdentifiers.empty())
83 {
84 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = combinedIdentifiers;
85 return;
86 }
87
88 std::vector<std::string> connectedPinParents;
89 for (const auto& dataIdentifier : inputPins.at(connectedPinIndex).dataIdentifier)
90 {
91 auto parentIdentifiers = NodeRegistry::GetParentNodeDataTypes(dataIdentifier);
92 std::erase(parentIdentifiers, NodeData::type());
93 connectedPinParents.insert(connectedPinParents.begin(), parentIdentifiers.begin(), parentIdentifiers.end());
94 }
95
96 for (const auto& dataIdentifier : inputPins.at(otherPinIndex).dataIdentifier)
97 {
98 if (std::ranges::find(connectedPinParents, dataIdentifier) != connectedPinParents.end())
99 {
100 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = inputPins.at(otherPinIndex).dataIdentifier;
101 }
102 else
103 {
104 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = inputPins.at(connectedPinIndex).dataIdentifier;
105 }
106 }
107 }
108}
109
110void NAV::Merger::updateOutputPin(const std::vector<std::string>& oldDataIdentifiers)
111{
112 // Check if connected links on output port are still valid
113 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
114 {
115 if (auto* endPin = link.getConnectedPin())
116 {
117 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).canCreateLink(*endPin))
118 {
119 continue;
120 }
121
122 // If the link is not valid anymore, delete it
123 outputPins.at(OUTPUT_PORT_INDEX_FLOW).deleteLink(*endPin);
124 }
125 }
126
127 // Refresh all links connected to the output pin if the type changed
128 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier != oldDataIdentifiers)
129 {
130 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
131 {
132 if (auto* connectedPin = link.getConnectedPin())
133 {
134 outputPins.at(OUTPUT_PORT_INDEX_FLOW).recreateLink(*connectedPin);
135 }
136 }
137 }
138}
139
141{
142 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
143
144 if (endPin.parentNode->id != id) // Link on Output Port
145 {
146 if (!inputPins.at(INPUT_PORT_INDEX_FLOW_FIRST).isPinLinked()
147 && !inputPins.at(INPUT_PORT_INDEX_FLOW_SECOND).isPinLinked())
148 {
149 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = endPin.dataIdentifier;
150 }
151 return true;
152 }
153
154 size_t connectedPinIndex = inputPinIndexFromId(endPin.id);
155 size_t otherPinIndex = connectedPinIndex == INPUT_PORT_INDEX_FLOW_FIRST ? INPUT_PORT_INDEX_FLOW_SECOND : INPUT_PORT_INDEX_FLOW_FIRST;
156
157 auto outputPinIdentifier = outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier;
158
159 setPinIdentifiers(connectedPinIndex, otherPinIndex, startPin.dataIdentifier);
160
161 updateOutputPin(outputPinIdentifier);
162
163 return true;
164}
165
166void NAV::Merger::afterDeleteLink([[maybe_unused]] OutputPin& startPin, InputPin& endPin)
167{
168 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
169
170 if (endPin.parentNode->id != id) // Link on Output Pin
171 {
172 return;
173 }
174
175 // Link on Input Pin
176 for (size_t pinIdx = 0; pinIdx < inputPins.size(); pinIdx++)
177 {
178 auto& pin = inputPins[pinIdx];
179 if (endPin.id != pin.id) // The other pin (which is not deleted)
180 {
181 if (pin.isPinLinked())
182 {
184
185 auto* otherNodePin = pin.link.getConnectedPin();
186
187 auto outputPinIdentifier = outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier;
188
189 setPinIdentifiers(pinIdx, otherPinIndex, otherNodePin->dataIdentifier);
190
191 updateOutputPin(outputPinIdentifier);
192 }
193 else
194 {
195 for (auto& pinReset : inputPins)
196 {
197 pinReset.dataIdentifier = { NodeData::type() };
198 }
199 }
200 break;
201 }
202 }
203}
204
205void NAV::Merger::receiveData(InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
206{
208 {
210 }
211
213}
Save/Load the Nodes.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Merger Node.
Abstract NodeData Class.
Manages all Nodes.
Utility class which specifies available nodes.
Input pins of nodes.
Definition Pin.hpp:491
TsDeque< std::shared_ptr< const NAV::NodeData > > NodeDataQueue
Node data queue type.
Definition Pin.hpp:707
bool onCreateLink(OutputPin &startPin, InputPin &endPin) override
Called when a new link is to be established.
Definition Merger.cpp:140
static std::string typeStatic()
String representation of the Class Type.
Definition Merger.cpp:37
static std::string category()
String representation of the Class Category.
Definition Merger.cpp:47
void updateOutputPin(const std::vector< std::string > &oldDataIdentifiers)
Checks if link on the pin is still valid and refreshes if so.
Definition Merger.cpp:110
std::string type() const override
String representation of the Class Type.
Definition Merger.cpp:42
void receiveData(InputPin::NodeDataQueue &queue, size_t pinIdx)
Receive data.
Definition Merger.cpp:205
void setPinIdentifiers(size_t connectedPinIndex, size_t otherPinIndex, const std::vector< std::string > &dataIdentifiers)
Set the Pin Identifiers for the other pin depending on the connected pin.
Definition Merger.cpp:52
void afterDeleteLink(OutputPin &startPin, InputPin &endPin) override
Called when a link was deleted.
Definition Merger.cpp:166
Merger()
Default constructor.
Definition Merger.cpp:19
static constexpr size_t INPUT_PORT_INDEX_FLOW_SECOND
Flow.
Definition Merger.hpp:62
static constexpr size_t INPUT_PORT_INDEX_FLOW_FIRST
Flow.
Definition Merger.hpp:61
static constexpr size_t OUTPUT_PORT_INDEX_FLOW
Flow.
Definition Merger.hpp:60
~Merger() override
Destructor.
Definition Merger.cpp:32
static std::string type()
Returns the type of the data class.
Definition NodeData.hpp:45
std::vector< OutputPin > outputPins
List of output pins.
Definition Node.hpp:399
Node(std::string name)
Constructor.
Definition Node.cpp:30
Kind kind
Kind of the Node.
Definition Node.hpp:393
std::vector< InputPin > inputPins
List of input pins.
Definition Node.hpp:397
bool callbacksEnabled
Enables the callbacks.
Definition Node.hpp:402
std::string nameId() const
Node name and id.
Definition Node.cpp:253
size_t inputPinIndexFromId(ax::NodeEditor::PinId pinId) const
Returns the index of the pin.
Definition Node.cpp:233
std::string name
Name of the Node.
Definition Node.hpp:395
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:180
ax::NodeEditor::NodeId id
Unique Id of the Node.
Definition Node.hpp:391
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
Output pins of nodes.
Definition Pin.hpp:338
Node * parentNode
Reference to the parent node.
Definition Pin.hpp:307
ax::NodeEditor::PinId id
Unique Id of the Pin.
Definition Pin.hpp:297
std::vector< std::string > dataIdentifier
One or multiple Data Identifiers (Unique name which is used for data flows)
Definition Pin.hpp:305
auto extract_front()
Returns a copy of the first element in the container and removes it from the container.
Definition TsDeque.hpp:494
OutputPin * CreateOutputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
InputPin * CreateInputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier={}, InputPin::Callback callback=static_cast< InputPin::FlowFirableCallbackFunc >(nullptr), InputPin::FlowFirableCheckFunc firable=nullptr, int priority=0, int idx=-1)
Create an Input Pin object.
std::vector< std::string > GetParentNodeDataTypes(const std::string &type)
Get the Parent Node Data Types of the specified Node Data Type.
@ Flow
NodeData Trigger.
Definition Pin.hpp:52