|
| 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 | +} |
0 commit comments