C++ API Reference: geometrytools.cpp

geometrytools.cpp
#include <iostream>
#include <maya/MStatus.h>
#include <maya/MIOStream.h>
#include <maya/MPxNode.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnGeometryData.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnMesh.h>
#include <maya/MGlobal.h>
#include <maya/MPointArray.h>
#include <cmath>
#include <algorithm>
constexpr double kFloatEpsilon {1.0e-4};
// Plugin MeshCompare Shader Class //
class MeshCompare : public MPxNode
{
public:
MeshCompare();
~MeshCompare() override;
MStatus compute( const MPlug&, MDataBlock& ) override;
SchedulingType schedulingType() const override { return SchedulingType::kParallel; }
static void * creator();
static MStatus initialize();
static MTypeId id;
protected:
static MObject aGeomA;
static MObject aGeomB;
static MObject aOutValue;
};
//
// DESCRIPTION: node classification
MTypeId MeshCompare::id( 0x58000880 );
//
// DESCRIPTION: attribute information
MObject MeshCompare::aGeomA;
MObject MeshCompare::aGeomB;
MObject MeshCompare::aOutValue;
//
// DESCRIPTION:
MeshCompare::MeshCompare()
{
}
//
// DESCRIPTION:
MeshCompare::~MeshCompare()
{
}
//
// DESCRIPTION:
void* MeshCompare::creator()
{
return new MeshCompare();
}
//
// DESCRIPTION:
MStatus MeshCompare::initialize()
{
MStatus stat;
aGeomA = tAttr.create( "inputA", "inA", MFnData::kMesh, MObject::kNullObj, &stat );
if (!stat) { stat.perror("create attribute inputA"); return stat; }
aGeomB = tAttr.create( "inputB", "inB", MFnData::kMesh, MObject::kNullObj, &stat );
if (!stat) { stat.perror("create attribute inputB"); return stat; }
// Outputs
aOutValue = nAttr.create( "outValue", "ov", MFnNumericData::kBoolean);
CHECK_MSTATUS ( nAttr.setHidden(false) );
CHECK_MSTATUS ( nAttr.setReadable(true) );
CHECK_MSTATUS ( nAttr.setWritable(false) );
CHECK_MSTATUS ( addAttribute(aGeomA) );
CHECK_MSTATUS ( addAttribute(aGeomB) );
CHECK_MSTATUS ( addAttribute(aOutValue) );
CHECK_MSTATUS ( attributeAffects (aGeomA, aOutValue) );
CHECK_MSTATUS ( attributeAffects (aGeomB, aOutValue) );
return MS::kSuccess;
}
//
// DESCRIPTION:
MStatus MeshCompare::compute(
const MPlug& plug,
MDataBlock& block )
{
if( plug == aOutValue )
{
MFnMesh aMesh(block.inputValue( aGeomA ).asMesh());
MFnMesh bMesh(block.inputValue( aGeomB ).asMesh());
aMesh.getPoints(a, MSpace::kWorld);
bMesh.getPoints(b, MSpace::kWorld);
auto c = a.length();
bool same = (c == b.length());
if(same)
{
for(unsigned int i = 0; i < c; ++i){
same = false;
auto pta = a[i];
auto ptb = b[i];
auto eps = std::max({pta[0]*kFloatEpsilon, ptb[0]*kFloatEpsilon, kFloatEpsilon});
if(std::fabs(pta[0] - ptb[0]) > eps) break;
eps = std::max({pta[1]*kFloatEpsilon, ptb[1]*kFloatEpsilon, kFloatEpsilon});
if(std::fabs(pta[1] - ptb[1]) > eps) break;
eps = std::max({pta[2]*kFloatEpsilon, ptb[2]*kFloatEpsilon, kFloatEpsilon});
if(std::fabs(pta[2] - ptb[2]) > eps) break;
same = true;
}
}
// set ouput color attribute
MDataHandle outHandle = block.outputValue( aOutValue );
outHandle.set(same);
} else
return MS::kUnknownParameter;
return MS::kSuccess;
}
//
// DESCRIPTION:
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin( obj, "Autodesk", "1.0", "Any");
CHECK_MSTATUS ( plugin.registerNode( "MeshCompare", MeshCompare::id,
MeshCompare::creator, MeshCompare::initialize,
return MS::kSuccess;
}
//
// DESCRIPTION:
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS ( plugin.deregisterNode( MeshCompare::id ) );
return MS::kSuccess;
}
// =====================================================================
// Copyright 2018 Autodesk, Inc. All rights reserved.
//
// This computer source code and related instructions and comments are
// the unpublished confidential and proprietary information of Autodesk,
// Inc. and are protected under applicable copyright and trade secret
// law. They may not be disclosed to, copied or used by any third party
// without the prior written consent of Autodesk, Inc.
// =====================================================================