#include "testNobjectNode.h"
#include <maya/MIOStream.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>
#include <maya/MTime.h>
#include <maya/MFnMesh.h>
#include <maya/MFnMeshData.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnIntArrayData.h>
#include <maya/MFnComponentListData.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MDagPath.h>
#include <maya/MPoint.h>
#include <maya/MFloatPointArray.h>
#include <maya/MItMeshVertex.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnNObjectData.h>
#include "stdio.h"
const MTypeId testNobjectNode::id( 0x85003 );
 
MObject testNobjectNode::startState;
 
MObject testNobjectNode::currentState;
 
MObject testNobjectNode::currentTime;
 
MObject testNobjectNode::nextState;
 
MObject testNobjectNode::inputGeom;
 
MObject testNobjectNode::outputGeom;
 
{
    if ( !stat )
    {
        cout<<msg<<"\n";
    }
}
testNobjectNode::testNobjectNode()
{
    
    fNObject.createNCloth();
}
{
    if ( plug == outputGeom )
    {
        
        cerr<<"pull on outputGeom\n";
        newMeshFn.
copy( inMeshObj, newMeshObj );        
        
        
        
        
        
        
        
        
        fNObject.getPositions(pts);
        if(pts.
length() == (
unsigned int) inputMesh.numVertices()) {
 
        }
    }
    if ( plug == currentState )
    {               
        
        currStateOutputHandle.
set(outputData.
object());
      
        cerr<<"pull on currentState\n";
    }
    if ( plug == startState )
    {
        int ii,jj;
        
                
        int numPolygons = inputMesh.numPolygons();
        int * faceVertCounts = new int[numPolygons];
                
        
        int facesArrayLength = 0;
        for(ii=0;ii<numPolygons;ii++) {
            inputMesh.getPolygonVertices(ii,verts);
            faceVertCounts[ii] = verts.
length();
            facesArrayLength += verts.
length();
        }
        int * faces = new int[facesArrayLength];
        int currIndex = 0;
        for(ii=0;ii<numPolygons;ii++) {
            inputMesh.getPolygonVertices(ii,verts);
            for(jj=0;jj<(int)verts.
length();jj++) {
 
                faces[currIndex++] = verts[jj];
            }
        }
        int numEdges = inputMesh.numEdges();
        int * edges = new int[2*numEdges];
        currIndex = 0;
        for(ii=0;ii<numEdges;ii++) {
            int2 edge;
            inputMesh.getEdgeVertices(ii,edge);
            edges[currIndex++] = edge[0];
            edges[currIndex++] = edge[1];
        }
        
        
        fNObject.setTopology(numPolygons, faceVertCounts, faces,numEdges, edges );
        delete[] faceVertCounts;
        delete[] faces;
        delete[] edges;        
        unsigned int numVerts = 0;
        numVerts = inputMesh.numVertices();        
        inputMesh.getPoints(vertexArray);
        fNObject.setPositions(vertexArray,true);
        for(ii=0;ii<(int)numVerts;ii++) {
            velocitiesArray[ii].x = 0.0f;
            velocitiesArray[ii].y = 0.0f;
            velocitiesArray[ii].z = 0.0f;
            velocitiesArray[ii].w = 0.0f;
        }
        fNObject.setVelocities(velocitiesArray);
        fNObject.setThickness(0.05f);
        fNObject.setInverseMass(1.0f);
        fNObject.setBounce(0.0f);
        fNObject.setFriction(0.1f);
        fNObject.setDamping(0.0f);
        fNObject.setBendResistance(0.0f);
        fNObject.setMaxIterations(100);        
        fNObject.setMaxSelfCollisionIterations(100);
        fNObject.setStretchAndCompressionResistance(20.0f,10.0f);
        fNObject.setSelfCollisionFlags(false);
        fNObject.setCollisionFlags(true);               
        
        
        startStateOutputHandle.
set(outputData.
object());
        cerr<<"pull on startState\n";
    }
    else {
    }
    return stat;
}
MStatus testNobjectNode::initialize()
 
{
    statCheck(stat, "failed to create nextState");
    statCheck(stat, "failed to create inputGeom");
    statCheck(stat, "failed to create currentState");
    statCheck(stat, "failed to create startState");
    statCheck(stat, "failed to create outputGeom");
    addAttribute(inputGeom);
    addAttribute(outputGeom);
    addAttribute(currentTime);
    addAttribute(startState);
    addAttribute(currentState);
    addAttribute(nextState);
    
    attributeAffects(inputGeom, outputGeom);
    attributeAffects(nextState, outputGeom);
    attributeAffects(inputGeom, startState);
    attributeAffects(currentTime, outputGeom);
    attributeAffects(currentTime, currentState);    
    attributeAffects(currentTime, startState);    
}
{
    MFnPlugin plugin(obj, 
"Autodesk - nCloth Prototype 5", 
"8.5", 
"Any");
 
    status = plugin.registerNode ( "testNobjectNode", testNobjectNode::id,testNobjectNode ::creator, testNobjectNode::initialize );
    if ( !status )
    {
        status.
perror(
"registerNode");
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode(testNobjectNode::id);
    if ( !status )
    {
        status.
perror(
"deregisterNode");
        return status;
    }
    return status;
}