#include <maya/MIOStream.h>
#include <math.h>
#include <stdlib.h>
#include "sweptEmitter.h"
#include <maya/MDataHandle.h>
#include <maya/MFnDynSweptGeometryData.h>
#include <maya/MDynSweptLine.h>
#include <maya/MDynSweptTriangle.h>
#include <maya/MVectorArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MIntArray.h>
#include <maya/MMatrix.h>
#include <maya/MArrayDataBuilder.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnVectorArrayData.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MFnArrayAttrsData.h>
#include <maya/MFnMatrixData.h>
MTypeId sweptEmitter::id( 0x80016 );
 
sweptEmitter::sweptEmitter()
:   lastWorldPoint(0, 0, 0, 1)
{
}
sweptEmitter::~sweptEmitter()
{
}
void *sweptEmitter::creator()
{
    return new sweptEmitter;
}
{
}
{
    
    
    if( !(plug == mOutput) )
    
    
    
    
    McheckErr(status, "ERROR in plug.logicalIndex.\n");
    
    
    
    McheckErr(status, "ERROR in hOutArray = block.outputArrayValue.\n");
    
    
    McheckErr(status, "ERROR in bOutArray = hOutArray.builder.\n");
    
    
    McheckErr(status, "ERROR in hOut = bOutArray.addElement.\n");
    
    
    McheckErr(status, "ERROR in fnOutput.create.\n");
    
    
    
    bool beenFull = isFullValue( multiIndex, block );
    if( beenFull )
    {
    }
    
    
    
    
    MTime cT = currentTimeValue( block );
 
    MTime sT = startTimeValue( multiIndex, block );
 
    MTime dT = deltaTimeValue( multiIndex, block );
 
    if( (cT <= sT) || (dT <= 0.0) )
    {
        
        
        
        
        
        
        
        
    }
    
    
    double speed = speedValue( block );
    MVector dirV = directionVector( block );
 
    double inheritFactor = inheritFactorValue( multiIndex, block );
    
    
    
    
    
    
    MVector rotatedV = useRotation ( dirV );
 
    
    
    
    
    
    MObject thisObj = this->thisMObject();
 
    MPlug sweptPlug( thisObj, mSweptGeometry );
 
    if ( sweptPlug.isConnected() ) 
    {
        
        
        
        if (fnSweptData.lineCount() > 0) {
            int numLines = fnSweptData.lineCount();
        
            for ( int i=0; i<numLines; i++ )
            {
                
                
                status = emitCountPerPoint( plug, block, 2, emitCountPP );
                emit( inPosAry, inVelAry, emitCountPP,
                    dt, speed, inheritFactor, rotatedV, fnOutPos, fnOutVel );
            }
        }
        
        
        if (fnSweptData.triangleCount() > 0) {
            int numTriangles = fnSweptData.triangleCount();
        
            for ( int i=0; i<numTriangles; i++ )
            {
                
                center /= 3.0;
                
                status = emitCountPerPoint( plug, block, 1, emitCountPP );
                emit( inPosAry, inVelAry, emitCountPP,
                    dt, speed, inheritFactor, rotatedV, fnOutPos, fnOutVel );
            }
        }
    }
    
    
}
void sweptEmitter::emit
    (
        double dt,                      
        double speed,                   
        double inheritFactor,           
    )
{
    
    
    int posLength = inPosAry.
length();
 
    int velLength = inVelAry.
length();
 
    int countLength = emitCountPP.
length();
 
    if( (posLength != velLength) || (posLength != countLength) )
        return;
    
    
    int index;
    int totalCount = 0;
    for( index = 0; index < countLength; index ++ )
        totalCount += emitCountPP[index];
    if( totalCount <= 0 )
        return;
    
    
    
    
    int emitCount;
    for( index = 0; index < posLength; index++ )
    {
        emitCount = emitCountPP[index];
        if( emitCount <= 0 )
            continue;
        sPos = inPosAry[index];
        sVel = inVelAry[index];
        prePos = sPos - sVel * dt;
        for( int i = 0; i < emitCount; i++ )
        {
            double alpha = ( (double)i + drand48() ) / (double)emitCount;
            newPos = (1 - alpha) * prePos + alpha * sPos;
            newVel = dirV * speed;
            newPos += newVel * ( dt * (1 - alpha) );
            newVel += sVel * inheritFactor;
            
            
        }
    }
}
{
    
    
    MObject worldMatrixAttr = fnThisNode.attribute( 
"worldMatrix" );
 
    
    
    
    MPlug matrixPlug( thisNode, worldMatrixAttr );
 
    matrixPlug = matrixPlug.elementByLogicalIndex( 0 );
    
    
    status = matrixPlug.getValue( matrixObject );
    if( !status )
    {
        status.
perror(
"sweptEmitter::useRotation: get matrixObject");
        return ( direction );
    }
    if( !status )
    {
        status.
perror(
"sweptEmitter::useRotation: get worldMatrixData");
        return( direction );
    }
    if( !status )
    {
        status.
perror(
"sweptEmitter::useRotation: get worldMatrixData.matrix");
        return( direction );
    }
    rotatedVector = direction * worldMatrix;
    return( rotatedVector );
}
MStatus sweptEmitter::emitCountPerPoint
 
    (
        int length,                 
    )
{
    McheckErr(status, "ERROR in emitCountPerPoint: when plug.logicalIndex.\n");
    
    
    double rate = rateValue( block );
    MTime dt = deltaTimeValue( plugIndex, block );
 
    
    
    int intCount = (int)dblCount;
    for( int i = 0; i < length; i++ )
    {
        emitCountPP.
append( intCount );
    }
}
{
    MFnPlugin plugin(obj, PLUGIN_COMPANY, 
"3.0", 
"Any");
 
    status = plugin.registerNode( "sweptEmitter", sweptEmitter::id,
                            &sweptEmitter::creator, &sweptEmitter::initialize,
    if (!status) {
        status.
perror(
"registerNode");
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode( sweptEmitter::id );
    if (!status) {
        status.
perror(
"deregisterNode");
        return status;
    }
    return status;
}