Skip to content

Commit 5de37ce

Browse files
committed
Add negative and sqrt nodes.
1 parent 7973427 commit 5de37ce

9 files changed

Lines changed: 132 additions & 21 deletions

File tree

Sources/BuiltInNodes/BI_UnaryOperationNodes.cpp

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ namespace BI
88
{
99

1010
SERIALIZATION_INFO (UnaryOperationNode, 1);
11-
DYNAMIC_SERIALIZATION_INFO (AbsoluteNode, 1, "{125E8E5E-F1CB-4AE4-8EA9-53343ACD193B}");
12-
DYNAMIC_SERIALIZATION_INFO (CeilNode, 1, "{60B0DFF2-2718-46A1-B7D5-AA614BF21FDD}");
11+
DYNAMIC_SERIALIZATION_INFO (AbsNode, 1, "{125E8E5E-F1CB-4AE4-8EA9-53343ACD193B}");
1312
DYNAMIC_SERIALIZATION_INFO (FloorNode, 1, "{0DB3D5E3-8B32-43A4-82D2-F5B816AB5CC1}");
13+
DYNAMIC_SERIALIZATION_INFO (CeilNode, 1, "{60B0DFF2-2718-46A1-B7D5-AA614BF21FDD}");
14+
DYNAMIC_SERIALIZATION_INFO (NegativeNode, 1, "{6440468C-E161-4245-9F8A-4DB637869BB5}");
15+
DYNAMIC_SERIALIZATION_INFO (SqrtNode, 1, "{FCDA9CB6-43CA-4E95-907E-63405D410D79}");
1416

1517
UnaryOperationNode::UnaryOperationNode () :
1618
UnaryOperationNode (NE::LocString (), NUIE::Point ())
@@ -88,31 +90,39 @@ NE::Stream::Status UnaryOperationNode::Write (NE::OutputStream& outputStream) co
8890
NE::ValuePtr UnaryOperationNode::DoSingleOperation (const NE::ValueConstPtr& aValue) const
8991
{
9092
double aDouble = NE::NumberValue::ToDouble (aValue);
93+
if (!IsValidInput (aDouble)) {
94+
return nullptr;
95+
}
9196
double result = DoOperation (aDouble);
9297
if (std::isnan (result) || std::isinf (result)) {
9398
return nullptr;
9499
}
95100
return NE::ValuePtr (new NE::DoubleValue (result));
96101
}
97102

98-
AbsoluteNode::AbsoluteNode () :
103+
bool UnaryOperationNode::IsValidInput (double) const
104+
{
105+
return true;
106+
}
107+
108+
AbsNode::AbsNode () :
99109
UnaryOperationNode ()
100110
{
101111

102112
}
103113

104-
AbsoluteNode::AbsoluteNode (const NE::LocString& name, const NUIE::Point& position) :
114+
AbsNode::AbsNode (const NE::LocString& name, const NUIE::Point& position) :
105115
UnaryOperationNode (name, position)
106116
{
107117

108118
}
109119

110-
AbsoluteNode::~AbsoluteNode ()
120+
AbsNode::~AbsNode ()
111121
{
112122

113123
}
114124

115-
double AbsoluteNode::DoOperation (double a) const
125+
double AbsNode::DoOperation (double a) const
116126
{
117127
return std::abs (a);
118128
}
@@ -161,4 +171,53 @@ double CeilNode::DoOperation (double a) const
161171
return std::ceil (a);
162172
}
163173

174+
NegativeNode::NegativeNode () :
175+
UnaryOperationNode ()
176+
{
177+
178+
}
179+
180+
NegativeNode::NegativeNode (const NE::LocString& name, const NUIE::Point& position) :
181+
UnaryOperationNode (name, position)
182+
{
183+
184+
}
185+
186+
NegativeNode::~NegativeNode ()
187+
{
188+
189+
}
190+
191+
double NegativeNode::DoOperation (double a) const
192+
{
193+
return a * -1.0;
194+
}
195+
196+
SqrtNode::SqrtNode () :
197+
UnaryOperationNode ()
198+
{
199+
200+
}
201+
202+
SqrtNode::SqrtNode (const NE::LocString& name, const NUIE::Point& position) :
203+
UnaryOperationNode (name, position)
204+
{
205+
206+
}
207+
208+
SqrtNode::~SqrtNode ()
209+
{
210+
211+
}
212+
213+
bool SqrtNode::IsValidInput (double a) const
214+
{
215+
return a >= 0.0;
216+
}
217+
218+
double SqrtNode::DoOperation (double a) const
219+
{
220+
return sqrt (a);
221+
}
222+
164223
}

Sources/BuiltInNodes/BI_UnaryOperationNodes.hpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ class UnaryOperationNode : public BasicUINode
2828

2929
private:
3030
NE::ValuePtr DoSingleOperation (const NE::ValueConstPtr& aValue) const;
31+
virtual bool IsValidInput (double a) const;
3132
virtual double DoOperation (double a) const = 0;
3233
};
3334

34-
class AbsoluteNode : public UnaryOperationNode
35+
class AbsNode : public UnaryOperationNode
3536
{
36-
DYNAMIC_SERIALIZABLE (AbsoluteNode);
37+
DYNAMIC_SERIALIZABLE (AbsNode);
3738

3839
public:
39-
AbsoluteNode ();
40-
AbsoluteNode (const NE::LocString& name, const NUIE::Point& position);
41-
virtual ~AbsoluteNode ();
40+
AbsNode ();
41+
AbsNode (const NE::LocString& name, const NUIE::Point& position);
42+
virtual ~AbsNode ();
4243

4344
private:
4445
virtual double DoOperation (double a) const override;
@@ -70,6 +71,33 @@ class CeilNode : public UnaryOperationNode
7071
virtual double DoOperation (double a) const override;
7172
};
7273

74+
class NegativeNode : public UnaryOperationNode
75+
{
76+
DYNAMIC_SERIALIZABLE (NegativeNode);
77+
78+
public:
79+
NegativeNode ();
80+
NegativeNode (const NE::LocString& name, const NUIE::Point& position);
81+
virtual ~NegativeNode ();
82+
83+
private:
84+
virtual double DoOperation (double a) const override;
85+
};
86+
87+
class SqrtNode : public UnaryOperationNode
88+
{
89+
DYNAMIC_SERIALIZABLE (SqrtNode);
90+
91+
public:
92+
SqrtNode ();
93+
SqrtNode (const NE::LocString& name, const NUIE::Point& position);
94+
virtual ~SqrtNode ();
95+
96+
private:
97+
virtual bool IsValidInput (double a) const override;
98+
virtual double DoOperation (double a) const override;
99+
};
100+
73101
}
74102

75103
#endif

Sources/NodeEngineTest/UnaryOperationNodesTest.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static double GetUnaryOperationResult (double a, const std::function<UINodePtr (
2727
TEST (TestAbsoluteNode)
2828
{
2929
double result = GetUnaryOperationResult (-2.0, [&] () {
30-
return UINodePtr (new AbsoluteNode (LocString (L"AbsoluteValue"), Point (0, 0)));
30+
return UINodePtr (new AbsNode (LocString (L"Abs"), Point (0, 0)));
3131
});
3232
ASSERT (IsEqual (result, 2.0));
3333
}
@@ -39,7 +39,7 @@ TEST (TestAbsoluteNodeWithList)
3939

4040
UINodePtr val = uiManager.AddNode (UINodePtr (new DoubleUpDownNode (LocString (L"Value"), Point (0, 0), -5.0, 1.0)), EmptyEvaluationEnv);
4141
UINodePtr listVal = uiManager.AddNode (UINodePtr (new DoubleIncrementedNode (LocString (L"Value"), Point (0, 0))), EmptyEvaluationEnv);
42-
UINodePtr op = uiManager.AddNode (UINodePtr (new AbsoluteNode (LocString (L"AbsoluteValue"), Point (0, 0))), EmptyEvaluationEnv);
42+
UINodePtr op = uiManager.AddNode (UINodePtr (new AbsNode (LocString (L"Abs"), Point (0, 0))), EmptyEvaluationEnv);
4343
uiManager.ConnectOutputSlotToInputSlot (val->GetUIOutputSlot (SlotId ("out")), listVal->GetUIInputSlot (SlotId ("start")));
4444
uiManager.ConnectOutputSlotToInputSlot (listVal->GetUIOutputSlot (SlotId ("out")), op->GetUIInputSlot (SlotId ("a")));
4545

@@ -69,4 +69,20 @@ TEST (TestCeilNode)
6969
ASSERT (IsEqual (result, 3.0));
7070
}
7171

72+
TEST (TestNegativeNode)
73+
{
74+
double result = GetUnaryOperationResult (2.0, [&] () {
75+
return UINodePtr (new NegativeNode (LocString (L"Negative"), Point (0, 0)));
76+
});
77+
ASSERT (IsEqual (result, -2.0));
78+
}
79+
80+
TEST (TestSqrtNode)
81+
{
82+
double result = GetUnaryOperationResult (9.0, [&] () {
83+
return UINodePtr (new SqrtNode (LocString (L"Sqrt"), Point (0, 0)));
84+
});
85+
ASSERT (IsEqual (result, 3.0));
86+
}
87+
7288
}
-78 Bytes
Loading
-66 Bytes
Loading
693 Bytes
Loading
549 Bytes
Loading

Sources/WindowsEmbeddingDemo/main.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,21 @@ class AppUIEnvironment : public NUIE::NodeUIEnvironment
146146
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Division", NUIE::IconId (110), [&] (const NUIE::Point& position) {
147147
return NUIE::UINodePtr (new BI::DivisionNode (NE::LocString (L"Division"), position));
148148
});
149-
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Abs", NUIE::IconId (113), [&] (const NUIE::Point& position) {
150-
return NUIE::UINodePtr (new BI::AbsoluteNode (NE::LocString (L"Abs"), position));
151-
});
152-
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Floor", NUIE::IconId (114), [&] (const NUIE::Point& position) {
149+
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Floor", NUIE::IconId (113), [&] (const NUIE::Point& position) {
153150
return NUIE::UINodePtr (new BI::FloorNode (NE::LocString (L"Floor"), position));
154151
});
155-
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Ceil", NUIE::IconId (115), [&] (const NUIE::Point& position) {
152+
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Ceil", NUIE::IconId (114), [&] (const NUIE::Point& position) {
156153
return NUIE::UINodePtr (new BI::CeilNode (NE::LocString (L"Ceil"), position));
157154
});
155+
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Abs", NUIE::IconId (115), [&] (const NUIE::Point& position) {
156+
return NUIE::UINodePtr (new BI::AbsNode (NE::LocString (L"Abs"), position));
157+
});
158+
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Negative", NUIE::IconId (116), [&] (const NUIE::Point& position) {
159+
return NUIE::UINodePtr (new BI::NegativeNode (NE::LocString (L"Negative"), position));
160+
});
161+
AddNodeTreeItem (nodeTree, mathematicalNodes, L"Sqrt", NUIE::IconId (117), [&] (const NUIE::Point& position) {
162+
return NUIE::UINodePtr (new BI::SqrtNode (NE::LocString (L"Sqrt"), position));
163+
});
158164
size_t otherNodes = nodeTree.AddGroup (L"Other Nodes");
159165
AddNodeTreeItem (nodeTree, otherNodes, L"List Builder", NUIE::IconId (111), [&] (const NUIE::Point& position) {
160166
return NUIE::UINodePtr (new BI::ListBuilderNode (NE::LocString (L"List Builder"), position));

Sources/WindowsEmbeddingDemo/resources.rc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
110 IMAGE "icons/Division.png"
1111
111 IMAGE "icons/ListBuilder.png"
1212
112 IMAGE "icons/Viewer.png"
13-
113 IMAGE "icons/Abs.png"
14-
114 IMAGE "icons/Floor.png"
15-
115 IMAGE "icons/Ceil.png"
13+
113 IMAGE "icons/Floor.png"
14+
114 IMAGE "icons/Ceil.png"
15+
115 IMAGE "icons/Abs.png"
16+
116 IMAGE "icons/Negative.png"
17+
117 IMAGE "icons/Sqrt.png"

0 commit comments

Comments
 (0)