Skip to content

Commit 195cf81

Browse files
committed
Return single value from operation nodes when all of the inputs are single.
1 parent deaa65d commit 195cf81

6 files changed

Lines changed: 72 additions & 15 deletions

File tree

Sources/BuiltInNodes/BI_BinaryOperationNodes.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,25 @@ NE::ValueConstPtr BinaryOperationNode::Calculate (NE::EvaluationEnv& env) const
4545
return nullptr;
4646
}
4747

48-
NE::ListValuePtr resultListValue (new NE::ListValue ());
49-
std::shared_ptr<ValueCombinationFeature> valueCombination = GetValueCombinationFeature (this);
50-
bool isValid = valueCombination->CombineValues ({aValue, bValue}, [&] (const NE::ValueCombination& combination) {
51-
double aDouble = NE::NumberValue::ToDouble (combination.GetValue (0));
52-
double bDouble = NE::NumberValue::ToDouble (combination.GetValue (1));
53-
double result = DoOperation (aDouble, bDouble);
54-
if (std::isnan (result) || std::isinf (result)) {
55-
return false;
48+
if (NE::IsSingleValue (aValue) && NE::IsSingleValue (bValue)) {
49+
NE::ValuePtr result = DoSingleOperation (aValue, bValue);
50+
return result;
51+
} else {
52+
NE::ListValuePtr resultListValue (new NE::ListValue ());
53+
std::shared_ptr<ValueCombinationFeature> valueCombination = GetValueCombinationFeature (this);
54+
bool isValid = valueCombination->CombineValues ({ aValue, bValue }, [&] (const NE::ValueCombination& combination) {
55+
NE::ValuePtr result = DoSingleOperation (combination.GetValue (0), combination.GetValue (1));
56+
if (result == nullptr) {
57+
return false;
58+
}
59+
resultListValue->Push (result);
60+
return true;
61+
});
62+
if (!isValid) {
63+
return nullptr;
5664
}
57-
resultListValue->Push (NE::ValuePtr (new NE::DoubleValue (result)));
58-
return true;
59-
});
60-
61-
if (!isValid) {
62-
return nullptr;
65+
return resultListValue;
6366
}
64-
return resultListValue;
6567
}
6668

6769
void BinaryOperationNode::RegisterParameters (NUIE::NodeParameterList& parameterList) const
@@ -90,6 +92,17 @@ NE::Stream::Status BinaryOperationNode::Write (NE::OutputStream& outputStream) c
9092
return outputStream.GetStatus ();
9193
}
9294

95+
NE::ValuePtr BinaryOperationNode::DoSingleOperation (const NE::ValueConstPtr& aValue, const NE::ValueConstPtr& bValue) const
96+
{
97+
double aDouble = NE::NumberValue::ToDouble (aValue);
98+
double bDouble = NE::NumberValue::ToDouble (bValue);
99+
double result = DoOperation (aDouble, bDouble);
100+
if (std::isnan (result) || std::isinf (result)) {
101+
return nullptr;
102+
}
103+
return NE::ValuePtr (new NE::DoubleValue (result));
104+
}
105+
93106
AdditionNode::AdditionNode () :
94107
BinaryOperationNode ()
95108
{

Sources/BuiltInNodes/BI_BinaryOperationNodes.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BinaryOperationNode : public BasicUINode
2727
virtual NE::Stream::Status Write (NE::OutputStream& outputStream) const override;
2828

2929
private:
30+
NE::ValuePtr DoSingleOperation (const NE::ValueConstPtr& aValue, const NE::ValueConstPtr& bValue) const;
3031
virtual double DoOperation (double a, double b) const = 0;
3132
};
3233

Sources/NodeEngine/NE_Value.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ void ValueToListValueAdapter::Enumerate (const std::function<void (const ValueCo
188188
processor (val);
189189
}
190190

191+
bool IsSingleValue (const ValueConstPtr& value)
192+
{
193+
return Value::IsType<SingleValue> (value);
194+
}
195+
196+
bool IsListValue (const ValueConstPtr& value)
197+
{
198+
return Value::IsType<ListValue> (value);
199+
}
200+
191201
ValueConstPtr CreateSingleValue (const ValueConstPtr& value)
192202
{
193203
if (Value::IsType<SingleValue> (value)) {

Sources/NodeEngine/NE_Value.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ bool IsComplexType (const ValueConstPtr& val)
205205
return false;
206206
}
207207

208+
bool IsSingleValue (const ValueConstPtr& value);
209+
bool IsListValue (const ValueConstPtr& value);
210+
208211
ValueConstPtr CreateSingleValue (const ValueConstPtr& value);
209212
IListValueConstPtr CreateListValue (const ValueConstPtr& value);
213+
210214
void FlatEnumerate (const ValueConstPtr& value, const std::function<void (const ValueConstPtr&)>& processor);
211215
ValueConstPtr FlattenValue (const ValueConstPtr& value);
212216

Sources/NodeEngineTest/BinaryOperationNodesTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ TEST (TestAdditionNode)
2727
ASSERT (IsEqual (DoubleValue::Get (CreateSingleValue (val)), 3.0));
2828
}
2929

30+
TEST (TestAdditionNodeWithList)
31+
{
32+
TestUIEnvironment env;
33+
NodeUIManager uiManager (env);
34+
35+
UINodePtr val1 = uiManager.AddNode (UINodePtr (new DoubleIncrementedNode (LocString (L"Value1"), Point (0, 0))), EmptyEvaluationEnv);
36+
UINodePtr val2 = uiManager.AddNode (UINodePtr (new DoubleIncrementedNode (LocString (L"Value2"), Point (0, 0))), EmptyEvaluationEnv);
37+
UINodePtr op = uiManager.AddNode (UINodePtr (new AdditionNode (LocString (L"Addition"), Point (0, 0))), EmptyEvaluationEnv);
38+
uiManager.ConnectOutputSlotToInputSlot (val1->GetUIOutputSlot (SlotId ("out")), op->GetUIInputSlot (SlotId ("a")));
39+
uiManager.ConnectOutputSlotToInputSlot (val2->GetUIOutputSlot (SlotId ("out")), op->GetUIInputSlot (SlotId ("b")));
40+
41+
ValueConstPtr val = op->Evaluate (EmptyEvaluationEnv);
42+
ASSERT (Value::IsType<ListValue> (val));
43+
std::vector<double> values;
44+
FlatEnumerate (val, [&] (const ValueConstPtr& v) {
45+
values.push_back (NumberValue::ToDouble (v));
46+
});
47+
ASSERT (values == std::vector<double> ({ 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0 }));
48+
}
49+
3050
TEST (TestSubtractionNode)
3151
{
3252
TestUIEnvironment env;

Sources/NodeEngineTest/ValueTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ TEST (ValueTest)
8787
{
8888
IntValue intValue (5);
8989
ValuePtr intValuePtr (new IntValue (6));
90+
ASSERT (IsSingleValue (intValuePtr));
91+
ASSERT (!IsListValue (intValuePtr));
9092
ASSERT (Value::IsType<NumberValue> (&intValue));
9193
ASSERT (Value::IsType<IntValue> (&intValue));
9294
ASSERT (Value::IsType<IntValue> (intValuePtr));
@@ -100,6 +102,13 @@ TEST (ValueTest)
100102
ASSERT (NumberValue::ToDouble (intValuePtr) == 6.0);
101103
}
102104

105+
TEST (ListValueTest)
106+
{
107+
ValuePtr listValue (new ListValue ());
108+
ASSERT (!IsSingleValue (listValue));
109+
ASSERT (IsListValue (listValue));
110+
}
111+
103112
TEST (BooleanValueTest)
104113
{
105114
BooleanValue val (true);

0 commit comments

Comments
 (0)