#include "testNpassiveNode.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 <maya/MAnimControl.h>
#include "stdio.h"
const MTypeId testNpassiveNode::id( 0x85004 );
 
MObject testNpassiveNode::startState;
 
MObject testNpassiveNode::currentState;
 
MObject testNpassiveNode::currentTime;
 
MObject testNpassiveNode::inputGeom;
 
{
    if ( !stat )
    {
        cout<<msg<<"\n";
    }
}
testNpassiveNode::testNpassiveNode()
{
    
    fNObject.createNRigid();
}
{
    if ( plug == currentState )
    { 
      
      
      
        int ii,jj;
        
                
        unsigned int numVerts = 0;
        numVerts = inputMesh.numVertices();
        unsigned int prevNumVerts;
        fNObject.getNumVertices(prevNumVerts);
        if(numVerts != prevNumVerts) {
                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;        
                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);
        } else {
                fNObject.getPositions(prevVertexArray);
            
            
            
            double dt = 1.0/24.0;
                for(ii=0;ii<(int)numVerts;ii++) {
                velocitiesArray[ii].x = (float)( (vertexArray[ii].x - prevVertexArray[ii].x)/dt);
                velocitiesArray[ii].y = (float)( (vertexArray[ii].y - prevVertexArray[ii].y)/dt);
                velocitiesArray[ii].z = (float)( (vertexArray[ii].x - prevVertexArray[ii].z)/dt);
                velocitiesArray[ii].w = 0.0f;
            }
            fNObject.setVelocities(velocitiesArray);
            fNObject.setPositions(vertexArray,true);
        }
        
        fNObject.setThickness(0.1f);
        fNObject.setBounce(0.0f);
        fNObject.setFriction(0.1f);
        fNObject.setCollisionFlags(true, true, true);               
        
        currStateOutputHandle.
set(outputData.
object());
      
    }
    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();        
            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.1f);
            fNObject.setBounce(0.0f);
            fNObject.setFriction(0.1f);
            fNObject.setCollisionFlags(true, true, true);               
        
        
            startStateOutputHandle.
set(outputData.
object());
    }
    else {
    }
    return stat;
}
MStatus testNpassiveNode::initialize()
 
{
    statCheck(stat, "failed to create inputGeom");
    statCheck(stat, "failed to create currentState");
    statCheck(stat, "failed to create startState");
    addAttribute(inputGeom);
    addAttribute(currentTime);
    addAttribute(startState);
    addAttribute(currentState);
    
    attributeAffects(inputGeom, startState);
    attributeAffects(inputGeom, currentState);
    attributeAffects(currentTime, currentState);    
    attributeAffects(currentTime, startState);    
}
{
    MFnPlugin plugin(obj, 
"Autodesk - nCloth Prototype 5", 
"9.0", 
"Any");
 
    status = plugin.registerNode ( "testNpassiveNode", testNpassiveNode::id,testNpassiveNode ::creator, testNpassiveNode::initialize );
    if ( !status )
    {
        status.
perror(
"registerNode");
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode(testNpassiveNode::id);
    if ( !status )
    {
        status.
perror(
"deregisterNode");
        return status;
    }
    return status;
}