#include "latticeNoise.h"
#include <string.h>
#include <maya/MIOStream.h>
#include <stdio.h>
#include <math.h>
#include <maya/MFnUnitAttribute.h>  
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>  
#include <maya/MFnLattice.h>
#include <maya/MFnLatticeData.h>
#include <maya/MString.h> 
#include <maya/MTypeId.h> 
#include <maya/MPlug.h> 
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h> 
#include <maya/MTime.h>
#define DEF_AMPLITUDE 0.2
#define DEF_FREQ      4.0 
#define McheckErr(stat,msg)          \
    if ( MS::kSuccess != stat ) {   \
        cerr << msg;                \
        return MS::kFailure;        \
    }
MTypeId     latticeNoiseNode::id( 0x80010 );
 
MObject     latticeNoiseNode::amplitude;  
 
MObject     latticeNoiseNode::frequency;  
 
{ 
    float noiseAmplitude;
    float noiseFreq;
 
    if( plug == output )
    {
        
        
        
        
        
        
        McheckErr( returnStatus, "ERROR getting lattice data handle\n" ); 
        
        
        
        
        MObject lattice = dataFn.lattice();
 
        McheckErr( returnStatus, "ERROR getting lattice geometry\n" );  
        
        
        McheckErr( returnStatus, "ERROR getting lattice data handle\n" );
        
        
        latticeData = outputData.
data(); 
            
            
            
            latticeData = dataFn.create();
        } else {
            
            
            dataFn.setObject( latticeData );
        }
        
        
        MObject outLattice = dataFn.lattice();
 
        MFnLattice outLattFn( outLattice, &returnStatus );
 
        McheckErr( returnStatus, "ERROR getting lattice geometry\n" );  
        
        
        McheckErr( returnStatus, "ERROR getting amplitude\n" );
        noiseAmplitude = ampData.
asFloat(); 
        McheckErr( returnStatus, "ERROR getting frequency\n" );
        
        
        McheckErr( returnStatus, "ERROR getting time data handle\n" );
        
        
        seconds = seconds * noiseFreq;
        
        
        
        unsigned s, t, u;
        lattFn.getDivisions( s, t, u );
        
        
        outLattFn.setDivisions( s, t, u );   
        for ( unsigned i = 0; i < s; i++ ) {
            for ( unsigned j = 0; j < t; j++ ) {
                for ( unsigned k = 0; k < u; k++ ) {
                    MPoint & point = lattFn.point( i, j, k );
 
                    MPoint & outPoint = outLattFn.point( i, j, k );
 
                    pnt noisePnt = noise::atPointAndTime( (
float)point.
x, (
float)point.
y, 
                                                          (
float)point.
z, seconds );
                    
                    
                    noisePnt.x =  ( noisePnt.x * 2.0F ) - 1.0F;
                    noisePnt.y =  ( noisePnt.y * 2.0F ) - 1.0F;
                    noisePnt.z =  ( noisePnt.z * 2.0F ) - 1.0F;
                    outPoint.
x = point.
x + ( noisePnt.x * noiseAmplitude );
                    outPoint.
y = point.
y + ( noisePnt.y * noiseAmplitude );
                    outPoint.
z = point.
z + ( noisePnt.z * noiseAmplitude );  
                }
            }
        } 
        outputData.
set( latticeData );
    } else {
    }
}
void* latticeNoiseNode::creator()
{
    return new latticeNoiseNode;
}
MStatus latticeNoiseNode::initialize()
 
{ 
    if ( !stat ) {
        stat.
perror(
"ERROR creating latticeNoiseNode input lattice attribute");
        return stat;
    }
    amplitude = numAttr.
create( 
"amplitude", 
"amp", 
                                               DEF_AMPLITUDE,
                                               &stat );
    if ( !stat ) {
        stat.
perror(
"ERROR creating latticeNoiseNode amplitude attribute");
        return stat;
    }
    frequency = numAttr.
create( 
"frequency", 
"fq", 
                                               DEF_FREQ,
                                               &stat );
    if ( !stat ) {
        stat.
perror(
"ERROR creating latticeNoiseNode frequency attribute");
        return stat;
    }
                                            0.0, &stat );
    if ( !stat ) {
        stat.
perror(
"ERROR creating latticeNoiseNode time attribute");
        return stat;
    }
    if ( !stat ) {
        stat.
perror(
"ERROR creating latticeNoiseNode output attribute");
        return stat;
    }
    stat = addAttribute( input );
        if (!stat) { stat.
perror(
"addAttribute"); 
return stat;}
 
    stat = addAttribute( amplitude );
        if (!stat) { stat.
perror(
"addAttribute"); 
return stat;}
 
    stat = addAttribute( frequency );
        if (!stat) { stat.
perror(
"addAttribute"); 
return stat;}
 
    stat = addAttribute( time );
        if (!stat) { stat.
perror(
"addAttribute"); 
return stat;}
 
    stat = addAttribute( output );
        if (!stat) { stat.
perror(
"addAttribute"); 
return stat;}
 
    stat = attributeAffects( input, output );
        if (!stat) { stat.
perror(
"attributeAffects"); 
return stat;}
 
    stat = attributeAffects( amplitude, output );
        if (!stat) { stat.
perror(
"attributeAffects"); 
return stat;}
 
    stat = attributeAffects( frequency, output );
        if (!stat) { stat.
perror(
"attributeAffects"); 
return stat;}
 
    stat = attributeAffects( time, output );
        if (!stat) { stat.
perror(
"attributeAffects"); 
return stat;}
 
}