0.5.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
12
13#include "NodeRegistry.hpp"
14
15#include "NodeData/NodeData.hpp"
16
29
31{
32 LOG_TRACE("{}: called", nameId());
33}
34
36{
37 return "Merger";
38}
39
40std::string NAV::Merger::type() const
41{
42 return typeStatic();
43}
44
46{
47 return "Utility";
48}
49
50void NAV::Merger::setPinIdentifiers(size_t connectedPinIndex, size_t otherPinIndex, const std::vector<std::string>& dataIdentifiers)
51{
52 LOG_DEBUG("{}: Setting DataIdentifier on pinIndex {}", nameId(), otherPinIndex);
53
54 inputPins.at(connectedPinIndex).dataIdentifier = dataIdentifiers;
55
56 if (!inputPins.at(otherPinIndex).isPinLinked())
57 {
58 inputPins.at(otherPinIndex).dataIdentifier = dataIdentifiers;
59 for (const auto& dataIdentifier : dataIdentifiers)
60 {
61 auto parentIdentifiers = NodeRegistry::GetParentNodeDataTypes(dataIdentifier);
62 std::erase(parentIdentifiers, NodeData::type());
63 inputPins.at(otherPinIndex).dataIdentifier.insert(inputPins.at(otherPinIndex).dataIdentifier.end(), parentIdentifiers.rbegin(), parentIdentifiers.rend());
64 }
65
66 // Update the dataIdentifier of the output pin to the same as input pin
67 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = dataIdentifiers;
68 }
69 else
70 {
71 std::vector<std::string> combinedIdentifiers;
72 for (const auto& dataIdentifier : inputPins.at(connectedPinIndex).dataIdentifier)
73 {
74 if (auto iter = std::find(inputPins.at(otherPinIndex).dataIdentifier.begin(), inputPins.at(otherPinIndex).dataIdentifier.end(), dataIdentifier);
75 iter != inputPins.at(otherPinIndex).dataIdentifier.end())
76 {
77 combinedIdentifiers.push_back(*iter);
78 }
79 }
80 if (!combinedIdentifiers.empty())
81 {
82 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = combinedIdentifiers;
83 return;
84 }
85
86 std::vector<std::string> connectedPinParents;
87 for (const auto& dataIdentifier : inputPins.at(connectedPinIndex).dataIdentifier)
88 {
89 auto parentIdentifiers = NodeRegistry::GetParentNodeDataTypes(dataIdentifier);
90 std::erase(parentIdentifiers, NodeData::type());
91 connectedPinParents.insert(connectedPinParents.begin(), parentIdentifiers.begin(), parentIdentifiers.end());
92 }
93
94 for (const auto& dataIdentifier : inputPins.at(otherPinIndex).dataIdentifier)
95 {
96 if (std::ranges::find(connectedPinParents, dataIdentifier) != connectedPinParents.end())
97 {
98 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = inputPins.at(otherPinIndex).dataIdentifier;
99 }
100 else
101 {
102 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = inputPins.at(connectedPinIndex).dataIdentifier;
103 }
104 }
105 }
106}
107
108void NAV::Merger::updateOutputPin(const std::vector<std::string>& oldDataIdentifiers)
109{
110 // Check if connected links on output port are still valid
111 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
112 {
113 if (auto* endPin = link.getConnectedPin())
114 {
115 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).canCreateLink(*endPin))
116 {
117 continue;
118 }
119
120 // If the link is not valid anymore, delete it
121 outputPins.at(OUTPUT_PORT_INDEX_FLOW).deleteLink(*endPin);
122 }
123 }
124
125 // Refresh all links connected to the output pin if the type changed
126 if (outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier != oldDataIdentifiers)
127 {
128 for (auto& link : outputPins.at(OUTPUT_PORT_INDEX_FLOW).links)
129 {
130 if (auto* connectedPin = link.getConnectedPin())
131 {
132 outputPins.at(OUTPUT_PORT_INDEX_FLOW).recreateLink(*connectedPin);
133 }
134 }
135 }
136}
137
139{
140 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
141
142 if (endPin.parentNode->id != id) // Link on Output Port
143 {
144 if (!inputPins.at(INPUT_PORT_INDEX_FLOW_FIRST).isPinLinked()
145 && !inputPins.at(INPUT_PORT_INDEX_FLOW_SECOND).isPinLinked())
146 {
147 outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier = endPin.dataIdentifier;
148 }
149 return true;
150 }
151
152 size_t connectedPinIndex = inputPinIndexFromId(endPin.id);
153 size_t otherPinIndex = connectedPinIndex == INPUT_PORT_INDEX_FLOW_FIRST ? INPUT_PORT_INDEX_FLOW_SECOND : INPUT_PORT_INDEX_FLOW_FIRST;
154
155 auto outputPinIdentifier = outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier;
156
157 setPinIdentifiers(connectedPinIndex, otherPinIndex, startPin.dataIdentifier);
158
159 updateOutputPin(outputPinIdentifier);
160
161 return true;
162}
163
164void NAV::Merger::afterDeleteLink([[maybe_unused]] OutputPin& startPin, InputPin& endPin)
165{
166 LOG_TRACE("{}: called for {} ==> {}", nameId(), size_t(startPin.id), size_t(endPin.id));
167
168 if (endPin.parentNode->id != id) // Link on Output Pin
169 {
170 return;
171 }
172
173 // Link on Input Pin
174 for (size_t pinIdx = 0; pinIdx < inputPins.size(); pinIdx++)
175 {
176 auto& pin = inputPins[pinIdx];
177 if (endPin.id != pin.id) // The other pin (which is not deleted)
178 {
179 if (pin.isPinLinked())
180 {
182
183 auto* otherNodePin = pin.link.getConnectedPin();
184
185 auto outputPinIdentifier = outputPins.at(OUTPUT_PORT_INDEX_FLOW).dataIdentifier;
186
187 setPinIdentifiers(pinIdx, otherPinIndex, otherNodePin->dataIdentifier);
188
189 updateOutputPin(outputPinIdentifier);
190 }
191 else
192 {
193 for (auto& pinReset : inputPins)
194 {
195 pinReset.dataIdentifier = { NodeData::type() };
196 }
197 }
198 break;
199 }
200 }
201}
202
203void NAV::Merger::receiveData(InputPin::NodeDataQueue& queue, size_t /* pinIdx */)
204{
206 {
208 }
209
211}
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.
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:138
static std::string typeStatic()
String representation of the Class Type.
Definition Merger.cpp:35
static std::string category()
String representation of the Class Category.
Definition Merger.cpp:45
void updateOutputPin(const std::vector< std::string > &oldDataIdentifiers)
Checks if link on the pin is still valid and refreshes if so.
Definition Merger.cpp:108
std::string type() const override
String representation of the Class Type.
Definition Merger.cpp:40
void receiveData(InputPin::NodeDataQueue &queue, size_t pinIdx)
Receive data.
Definition Merger.cpp:203
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:50
void afterDeleteLink(OutputPin &startPin, InputPin &endPin) override
Called when a link was deleted.
Definition Merger.cpp:164
Merger()
Default constructor.
Definition Merger.cpp:17
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:30
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:511
Node(std::string name)
Constructor.
Definition Node.cpp:29
Kind kind
Kind of the Node.
Definition Node.hpp:505
std::vector< InputPin > inputPins
List of input pins.
Definition Node.hpp:509
OutputPin * CreateOutputPin(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.
Definition Node.cpp:278
bool callbacksEnabled
Enables the callbacks.
Definition Node.hpp:514
std::string nameId() const
Node name and id.
Definition Node.cpp:323
size_t inputPinIndexFromId(ax::NodeEditor::PinId pinId) const
Returns the index of the pin.
Definition Node.cpp:232
std::string name
Name of the Node.
Definition Node.hpp:507
InputPin * CreateInputPin(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.
Definition Node.cpp:252
void invokeCallbacks(size_t portIndex, const std::shared_ptr< const NodeData > &data)
Calls all registered callbacks on the specified output port.
Definition Node.cpp:179
ax::NodeEditor::NodeId id
Unique Id of the Node.
Definition Node.hpp:503
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:525
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
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