Skip to content

Commit c2e498f

Browse files
committed
Add absolute value node.
1 parent b11615e commit c2e498f

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

Sources/BuiltInNodes/BI_BuiltInNodes.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#include "BI_InputUINodes.hpp"
55
#include "BI_ViewerUINodes.hpp"
66
#include "BI_BinaryOperationNodes.hpp"
7+
#include "BI_UnaryFunctionNodes.hpp"
78

89
#endif
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "BI_UnaryFunctionNodes.hpp"
2+
#include "NE_Localization.hpp"
3+
#include "NUIE_NodeCommonParameters.hpp"
4+
5+
#include <cmath>
6+
7+
namespace BI
8+
{
9+
10+
SERIALIZATION_INFO (UnaryFunctionNode, 1);
11+
DYNAMIC_SERIALIZATION_INFO (AbsoluteValueNode, 1, "{125E8E5E-F1CB-4AE4-8EA9-53343ACD193B}");
12+
13+
UnaryFunctionNode::UnaryFunctionNode () :
14+
UnaryFunctionNode (NE::LocString (), NUIE::Point ())
15+
{
16+
17+
}
18+
19+
UnaryFunctionNode::UnaryFunctionNode (const NE::LocString& name, const NUIE::Point& position) :
20+
BasicUINode (name, position)
21+
{
22+
}
23+
24+
UnaryFunctionNode::~UnaryFunctionNode ()
25+
{
26+
27+
}
28+
29+
void UnaryFunctionNode::Initialize ()
30+
{
31+
RegisterUIInputSlot (NUIE::UIInputSlotPtr (new NUIE::UIInputSlot (NE::SlotId ("a"), NE::LocString (L"A"), NE::ValuePtr (new NE::DoubleValue (0.0)), NE::OutputSlotConnectionMode::Single)));
32+
RegisterUIOutputSlot (NUIE::UIOutputSlotPtr (new NUIE::UIOutputSlot (NE::SlotId ("result"), NE::LocString (L"Result"))));
33+
}
34+
35+
NE::ValueConstPtr UnaryFunctionNode::Calculate (NE::EvaluationEnv& env) const
36+
{
37+
NE::ValueConstPtr aValue = EvaluateInputSlot (NE::SlotId ("a"), env);
38+
if (!NE::IsComplexType<NE::NumberValue> (aValue)) {
39+
return nullptr;
40+
}
41+
42+
if (NE::IsSingleValue (aValue)) {
43+
return DoSingleOperation (aValue);
44+
} else {
45+
NE::ListValuePtr resultListValue (new NE::ListValue ());
46+
bool isValid = NE::FlatEnumerate (aValue, [&] (const NE::ValueConstPtr& val) {
47+
NE::ValuePtr result = DoSingleOperation (val);
48+
if (result == nullptr) {
49+
return false;
50+
}
51+
resultListValue->Push (result);
52+
return true;
53+
});
54+
if (!isValid) {
55+
return nullptr;
56+
}
57+
return resultListValue;
58+
}
59+
}
60+
61+
void UnaryFunctionNode::RegisterParameters (NUIE::NodeParameterList& parameterList) const
62+
{
63+
BasicUINode::RegisterParameters (parameterList);
64+
NUIE::RegisterSlotDefaultValueNodeParameter<UnaryFunctionNode, NE::DoubleValue> (parameterList, NE::SlotId ("a"), NE::LocString (L"A"), NUIE::ParameterType::Double);
65+
}
66+
67+
bool UnaryFunctionNode::IsForceCalculated () const
68+
{
69+
return true;
70+
}
71+
72+
NE::Stream::Status UnaryFunctionNode::Read (NE::InputStream& inputStream)
73+
{
74+
NE::ObjectHeader header (inputStream);
75+
BasicUINode::Read (inputStream);
76+
return inputStream.GetStatus ();
77+
}
78+
79+
NE::Stream::Status UnaryFunctionNode::Write (NE::OutputStream& outputStream) const
80+
{
81+
NE::ObjectHeader header (outputStream, serializationInfo);
82+
BasicUINode::Write (outputStream);
83+
return outputStream.GetStatus ();
84+
}
85+
86+
NE::ValuePtr UnaryFunctionNode::DoSingleOperation (const NE::ValueConstPtr& aValue) const
87+
{
88+
double aDouble = NE::NumberValue::ToDouble (aValue);
89+
double result = DoOperation (aDouble);
90+
if (std::isnan (result) || std::isinf (result)) {
91+
return nullptr;
92+
}
93+
return NE::ValuePtr (new NE::DoubleValue (result));
94+
}
95+
96+
AbsoluteValueNode::AbsoluteValueNode () :
97+
UnaryFunctionNode ()
98+
{
99+
100+
}
101+
102+
AbsoluteValueNode::AbsoluteValueNode (const NE::LocString& name, const NUIE::Point& position) :
103+
UnaryFunctionNode (name, position)
104+
{
105+
106+
}
107+
108+
AbsoluteValueNode::~AbsoluteValueNode ()
109+
{
110+
111+
}
112+
113+
double AbsoluteValueNode::DoOperation (double a) const
114+
{
115+
return std::abs (a);
116+
}
117+
118+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef BI_UNARYFUNCTIONNODES_HPP
2+
#define BI_UNARYFUNCTIONNODES_HPP
3+
4+
#include "NE_SingleValues.hpp"
5+
#include "BI_BasicUINode.hpp"
6+
#include "BI_BuiltInFeatures.hpp"
7+
8+
namespace BI
9+
{
10+
11+
class UnaryFunctionNode : public BasicUINode
12+
{
13+
SERIALIZABLE;
14+
15+
public:
16+
UnaryFunctionNode ();
17+
UnaryFunctionNode (const NE::LocString& name, const NUIE::Point& position);
18+
virtual ~UnaryFunctionNode ();
19+
20+
virtual void Initialize () override;
21+
virtual NE::ValueConstPtr Calculate (NE::EvaluationEnv& env) const override;
22+
23+
virtual void RegisterParameters (NUIE::NodeParameterList& parameterList) const override;
24+
virtual bool IsForceCalculated () const override;
25+
26+
virtual NE::Stream::Status Read (NE::InputStream& inputStream) override;
27+
virtual NE::Stream::Status Write (NE::OutputStream& outputStream) const override;
28+
29+
private:
30+
NE::ValuePtr DoSingleOperation (const NE::ValueConstPtr& aValue) const;
31+
virtual double DoOperation (double a) const = 0;
32+
};
33+
34+
class AbsoluteValueNode : public UnaryFunctionNode
35+
{
36+
DYNAMIC_SERIALIZABLE (AbsoluteValueNode);
37+
38+
public:
39+
AbsoluteValueNode ();
40+
AbsoluteValueNode (const NE::LocString& name, const NUIE::Point& position);
41+
virtual ~AbsoluteValueNode ();
42+
43+
private:
44+
virtual double DoOperation (double a) const override;
45+
};
46+
47+
}
48+
49+
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "SimpleTest.hpp"
2+
#include "NUIE_NodeUIManager.hpp"
3+
#include "BI_UnaryFunctionNodes.hpp"
4+
#include "BI_InputUINodes.hpp"
5+
#include "TestUtils.hpp"
6+
7+
using namespace NE;
8+
using namespace NUIE;
9+
using namespace BI;
10+
11+
namespace UnaryFunctionNodesTest
12+
{
13+
14+
TEST (TestAbsoluteValueNode)
15+
{
16+
TestUIEnvironment env;
17+
NodeUIManager uiManager (env);
18+
19+
UINodePtr val = uiManager.AddNode (UINodePtr (new DoubleUpDownNode (LocString (L"Value"), Point (0, 0), -1.0, 1.0)), EmptyEvaluationEnv);
20+
UINodePtr op = uiManager.AddNode (UINodePtr (new AbsoluteValueNode (LocString (L"AbsoluteValue"), Point (0, 0))), EmptyEvaluationEnv);
21+
uiManager.ConnectOutputSlotToInputSlot (val->GetUIOutputSlot (SlotId ("out")), op->GetUIInputSlot (SlotId ("a")));
22+
23+
ValueConstPtr value = op->Evaluate (EmptyEvaluationEnv);
24+
ASSERT (IsSingleType<DoubleValue> (value));
25+
ASSERT (IsEqual (DoubleValue::Get (value), 1.0));
26+
}
27+
28+
TEST (TestAbsoluteValueNodeWithList)
29+
{
30+
TestUIEnvironment env;
31+
NodeUIManager uiManager (env);
32+
33+
UINodePtr val = uiManager.AddNode (UINodePtr (new DoubleUpDownNode (LocString (L"Value"), Point (0, 0), -5.0, 1.0)), EmptyEvaluationEnv);
34+
UINodePtr listVal = uiManager.AddNode (UINodePtr (new DoubleIncrementedNode (LocString (L"Value"), Point (0, 0))), EmptyEvaluationEnv);
35+
UINodePtr op = uiManager.AddNode (UINodePtr (new AbsoluteValueNode (LocString (L"AbsoluteValue"), Point (0, 0))), EmptyEvaluationEnv);
36+
uiManager.ConnectOutputSlotToInputSlot (val->GetUIOutputSlot (SlotId ("out")), listVal->GetUIInputSlot (SlotId ("start")));
37+
uiManager.ConnectOutputSlotToInputSlot (listVal->GetUIOutputSlot (SlotId ("out")), op->GetUIInputSlot (SlotId ("a")));
38+
39+
ValueConstPtr value = op->Evaluate (EmptyEvaluationEnv);
40+
ASSERT (Value::IsType<ListValue> (value));
41+
std::vector<double> values;
42+
FlatEnumerate (value, [&] (const ValueConstPtr& v) {
43+
values.push_back (NumberValue::ToDouble (v));
44+
return true;
45+
});
46+
ASSERT (values == std::vector<double> ({ 5.0, 4.0, 3.0, 2.0, 1.0, 0.0, 1.0, 2.0, 3.0, 4.0 }));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)