Skip to content

Commit

Permalink
logical ops
Browse files Browse the repository at this point in the history
  • Loading branch information
serguei-k committed May 18, 2018
1 parent e81f968 commit 0beb4ce
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/Condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,83 @@ SELECT_NODE(MVector, SelectVector);
SELECT_NODE(MMatrix, SelectMatrix);
SELECT_NODE(MEulerRotation, SelectRotation);
SELECT_NODE(MQuaternion, SelectQuaternion);


template <typename TType>
inline bool logical_and(TType a, TType b)
{
return a && b;
}

template <typename TType>
inline bool logical_or(TType a, TType b)
{
return a || b;
}

template <typename TType>
inline bool logical_xor(TType a, TType b)
{
return !a != !b;
}

template<typename TAttrType, typename TClass, const char* TTypeName, bool (*TFuncPtr)(TAttrType, TAttrType)>
class LogicalNode : public BaseNode<TClass, TTypeName>
{
public:
static MStatus initialize()
{
createAttribute(input1Attr_, "input1", DefaultValue<TAttrType>());
createAttribute(input2Attr_, "input2", DefaultValue<TAttrType>());
createAttribute(outputAttr_, "output", DefaultValue<bool>(), false);

MPxNode::addAttribute(input1Attr_);
MPxNode::addAttribute(input2Attr_);
MPxNode::addAttribute(outputAttr_);

MPxNode::attributeAffects(input1Attr_, outputAttr_);
MPxNode::attributeAffects(input2Attr_, outputAttr_);

return MS::kSuccess;
}

MStatus compute(const MPlug& plug, MDataBlock& dataBlock) override
{
if (plug == outputAttr_ || (plug.isChild() && plug.parent() == outputAttr_))
{
const auto input1Value = getAttribute<TAttrType>(dataBlock, input1Attr_);
const auto input2Value = getAttribute<TAttrType>(dataBlock, input2Attr_);

setAttribute(dataBlock, outputAttr_, TFuncPtr(input1Value, input2Value));

return MS::kSuccess;
}

return MS::kUnknownParameter;
}

private:
static Attribute input1Attr_;
static Attribute input2Attr_;
static Attribute outputAttr_;
};

template<typename TAttrType, typename TClass, const char* TTypeName, bool (*TFuncPtr)(TAttrType, TAttrType)>
Attribute LogicalNode<TAttrType, TClass, TTypeName, TFuncPtr>::input1Attr_;

template<typename TAttrType, typename TClass, const char* TTypeName, bool (*TFuncPtr)(TAttrType, TAttrType)>
Attribute LogicalNode<TAttrType, TClass, TTypeName, TFuncPtr>::input2Attr_;

template<typename TAttrType, typename TClass, const char* TTypeName, bool (*TFuncPtr)(TAttrType, TAttrType)>
Attribute LogicalNode<TAttrType, TClass, TTypeName, TFuncPtr>::outputAttr_;

#define LOGICAL_NODE(AttrType, NodeName, FuncPtr) \
TEMPLATE_PARAMETER_LINKAGE char name##NodeName[] = #NodeName; \
class NodeName : public LogicalNode<AttrType, NodeName, name##NodeName, FuncPtr> {};

LOGICAL_NODE(bool, AndBool, &logical_and);
LOGICAL_NODE(bool, OrBool, &logical_or);
LOGICAL_NODE(bool, XorBool, &logical_xor);
LOGICAL_NODE(int, AndInt, &logical_and);
LOGICAL_NODE(int, OrInt, &logical_or);
LOGICAL_NODE(int, XorInt, &logical_xor);
12 changes: 12 additions & 0 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ initializePlugin(MObject pluginObj)
VectorLengthSquared::registerNode(pluginFn, typeId++);

// 1.1.0
AndBool::registerNode(pluginFn, typeId++);
AndInt::registerNode(pluginFn, typeId++);
Average::registerNode(pluginFn, typeId++);
AverageAngle::registerNode(pluginFn, typeId++);
AverageInt::registerNode(pluginFn, typeId++);
AverageMatrix::registerNode(pluginFn, typeId++);
AverageRotation::registerNode(pluginFn, typeId++);
AverageVector::registerNode(pluginFn, typeId++);
AverageQuaternion::registerNode(pluginFn, typeId++);
OrBool::registerNode(pluginFn, typeId++);
OrInt::registerNode(pluginFn, typeId++);
Sum::registerNode(pluginFn, typeId++);
SumAngle::registerNode(pluginFn, typeId++);
SumInt::registerNode(pluginFn, typeId++);
Expand All @@ -139,6 +143,8 @@ initializePlugin(MObject pluginObj)
WeightedAverageQuaternion::registerNode(pluginFn, typeId++);
WeightedAverageRotation::registerNode(pluginFn, typeId++);
WeightedAverageVector::registerNode(pluginFn, typeId++);
XorBool::registerNode(pluginFn, typeId++);
XorInt::registerNode(pluginFn, typeId++);

return MS::kSuccess;
}
Expand Down Expand Up @@ -238,13 +244,17 @@ uninitializePlugin(MObject pluginObj)
VectorLength::deregisterNode(pluginFn);
VectorLengthSquared::deregisterNode(pluginFn);

AndBool::deregisterNode(pluginFn);
AndInt::deregisterNode(pluginFn);
Average::deregisterNode(pluginFn);
AverageAngle::deregisterNode(pluginFn);
AverageInt::deregisterNode(pluginFn);
AverageMatrix::deregisterNode(pluginFn);
AverageRotation::deregisterNode(pluginFn);
AverageVector::deregisterNode(pluginFn);
AverageQuaternion::deregisterNode(pluginFn);
OrBool::deregisterNode(pluginFn);
OrInt::deregisterNode(pluginFn);
Sum::deregisterNode(pluginFn);
SumAngle::deregisterNode(pluginFn);
SumInt::deregisterNode(pluginFn);
Expand All @@ -256,6 +266,8 @@ uninitializePlugin(MObject pluginObj)
WeightedAverageQuaternion::deregisterNode(pluginFn);
WeightedAverageRotation::deregisterNode(pluginFn);
WeightedAverageVector::deregisterNode(pluginFn);
XorBool::deregisterNode(pluginFn);
XorInt::deregisterNode(pluginFn);

return MS::kSuccess;
}
18 changes: 18 additions & 0 deletions tests/test_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@ def test_select_matrix(self):

def test_select_quaternion(self):
self.create_node('SelectQuaternion', {'input2': [1.0, 0.0, 0.0, 1.0], 'condition': True}, [1.0, 0.0, 0.0, 1.0])

def test_and_bool(self):
self.create_node('AndBool', {'input1': True, 'input2': True}, True)

def test_and_int(self):
self.create_node('AndInt', {'input1': 1, 'input2': 0}, False)

def test_or_bool(self):
self.create_node('OrBool', {'input1': True, 'input2': True}, True)

def test_or_int(self):
self.create_node('OrInt', {'input1': 0, 'input2': 0}, False)

def test_xor_bool(self):
self.create_node('XorBool', {'input1': True, 'input2': True}, False)

def test_xor_int(self):
self.create_node('XorInt', {'input1': 1, 'input2': 0}, True)

0 comments on commit 0beb4ce

Please sign in to comment.