#include "meshMapUtils.h"
#include "meshRemapCmd.h"
#include <maya/MArgList.h>
#include <maya/MDagPathArray.h>
#include <maya/MFnMesh.h>
#include <maya/MFnNumericData.h>
#include <maya/MIOStream.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
#include <maya/MStatus.h>
meshRemapCommand::meshRemapCommand()
{
    fColorArrays = NULL;
    fRepArray = NULL;
    fClampedArray = NULL;
    fUArrays = NULL;
    fVArrays = NULL;
    fUVCountsArrays = NULL;
    fUVIdsArrays = NULL;
}
meshRemapCommand::~meshRemapCommand()
{
    reset();
}
void* meshRemapCommand::creator()
{
    return new meshRemapCommand;
}
{
    {
        displayError("6 vertices must be specified");
    }
    int argIdx = 0;
    for (unsigned int i = 0; i < 2; i++)
    {
        for (unsigned int j = 0; j < 3; j++)
        {
            {
                displayError( "Can't parse arg");
                return stat;
            }
            {
                err = arg + ": no such component";
                displayError(err);
            }
            if (selectionIt.isDone ())
            {
                err = arg + ": not a component";
                displayError (err);
            }
            if( selectionIt.getDagPath (selectedPath[j], selectedComponent[j]) != 
MStatus::kSuccess )
 
            {
                displayError( "Can't get path for");
                return stat;
            }
            {
                err = arg + ": Invalid type!  Only a mesh or its transform can be specified!";
                displayError (err);
            }
            argIdx++;
        }
        if( i == 0 )
        {
            if( ( stat = meshMapUtils::validateFaceSelection( selectedPath, selectedComponent, &fFaceIdxSrc, &fFaceVtxSrc ) ) != 
MStatus::kSuccess )
 
            {
                displayError("Selected vertices don't define a unique face on source mesh");
                return stat;
            }
            fDagPathSrc = selectedPath[0];
        }
        else
        {
            if( ( stat = meshMapUtils::validateFaceSelection( selectedPath, selectedComponent, &fFaceIdxDst, &fFaceVtxDst ) ) != 
MStatus::kSuccess )
 
            {
                displayError("Selected vertices don't define a unique face on target mesh");
                return stat;
            }
            fDagPathDst = selectedPath[0];
        }
    }
    if( fDagPathSrc == fDagPathDst )
    {
        displayError("Cannot use one mesh for both source and target");
        return stat;
    }
}
{
    {
        displayError ("Error parsing arguments");
        return stat;
    }
    return redoIt();
}
{
    int i;
    
    fDagPathDst.extendToShape();
    MFnMesh theMeshDst( fDagPathDst, &stat );
 
    {
        displayError(" MFnMesh creation");
        return stat;
    }
    
    MPlug   historyPlug = theMeshDst.findPlug(
"inMesh", 
true);
 
        displayError("The destination mesh has history. Its geometry cannot be modified.");
    }
    fDagPathSrc.extendToShape();
    MFnMesh theMeshSrc( fDagPathSrc, &stat );
 
    {
        displayError(" MFnMesh creation");
        return stat;
    }
    {
        displayError(" MFnMesh getPoints");
        return stat;
    }
    
    
    
    
    
    
    MIntArray           faceTraversal( theMeshSrc.numPolygons(), false );
 
    MIntArray           cvMapping( theMeshSrc.numVertices(), -1 );
 
    MIntArray           cvMappingInverse( theMeshSrc.numVertices(), -1 ); 
 
    
    
    
    stat = meshMapUtils::traverseFace( fDagPathSrc, fFaceIdxSrc, fFaceVtxSrc[0], fFaceVtxSrc[1], faceTraversal,
                    cvMapping, cvMappingInverse, 
                    newPolygonCounts, newPolygonConnects, 
                    origVertices, newVertices );
    {
        displayError(" could not process all the mesh faces.");
        return stat;
    }
    
    newPolygonCounts.
clear();
    newPolygonConnects.
clear();
    while(!polyIter.isDone()) 
    {
        newPolygonCounts.append(polyIter.polygonVertexCount());
        for(i = 0; i < (int) polyIter.polygonVertexCount(); i++)
        {
            newPolygonConnects.
append(polyIter.vertexIndex(i));
        }
        polyIter.next();
    }
    
    
    
    {
        displayError(" MFnMesh getPoints");
        return stat;
    }
    
    MIntArray           faceTraversalDst( theMeshDst.numPolygons(), false );
 
    MIntArray           cvMappingDst( theMeshDst.numVertices(), -1 );
 
    MIntArray           cvMappingInverseDst( theMeshDst.numVertices(), -1 ); 
 
    
    
    
    stat = meshMapUtils::traverseFace( fDagPathDst, fFaceIdxDst, fFaceVtxDst[0], fFaceVtxDst[1], faceTraversalDst,
                    cvMappingDst, cvMappingInverseDst, 
                    newPolygonCountsDst, newPolygonConnectsDst, 
                    origVerticesDst, newVerticesDst );
    {
        displayError(" could not process all the mesh faces.");
        return stat;
    }
    
    
    
    
    for( i = 0; i < theMeshDst.numVertices(); i++ )
    {
        newVerticesDst[cvMappingInverse[i]] = origVerticesDst[cvMappingInverseDst[i]];
    }
    
    fVertices.
copy(origVerticesDst);
    while(!polyIterDst.isDone()) 
    {
        fPolygonCounts.append(polyIterDst.polygonVertexCount());
        for(i = 0; i < (int)polyIterDst.polygonVertexCount(); i++)
        {
            fPolygonConnects.append(polyIterDst.vertexIndex(i));
        }
    }
    
    theMeshDst.getColorSetNames(fColorSetNames);
    int numColorSets = fColorSetNames.length();
    fClampedArray = new bool[numColorSets];
    for ( i = 0; i < numColorSets; i++ ) 
    {
        fClampedArray[i] = theMeshDst.isColorClamped(fColorSetNames[i]);
        fRepArray[i] = theMeshDst.getColorRepresentation(fColorSetNames[i]);
        theMeshDst.getVertexColors(fColorArrays[i], &(fColorSetNames[i]));
    }
    
    theMeshDst.getUVSetNames(fUVSetNames);
    int numUVSets = fUVSetNames.length();
    for ( i = 0; i < numUVSets; i++ ) 
    {
        theMeshDst.getAssignedUVs(fUVCountsArrays[i], fUVIdsArrays[i], &(fUVSetNames[i]));
        theMeshDst.getUVs(fUArrays[i], fVArrays[i], &(fUVSetNames[i]));
    }
    
    
    
    
    stat = theMeshDst.createInPlace( newVerticesDst.
length(), newPolygonCounts.length(), newVerticesDst, newPolygonCounts, newPolygonConnects );
    {
        displayError(" mesh copy failed.");
        reset();
        return stat;
    }
    
    numDataFn.
setData( 0.0f, 0.0f, 0.0f );
    MPlug tweaks = depFn.findPlug(
"pnts");
 
    tweaks.getExistingArrayAttributeIndices( tweakIndices );
    int numTweaks = tweakIndices.
length();
 
    for( i = 0; i < numTweaks; i++ )
    {
    }
    
    theMeshSrc.getColorSetNames(colorSetNamesSrc);
    int numColorSetsSrc = colorSetNamesSrc.
length();
 
    for (i = 0; i < numColorSetsSrc; i++) 
    {
        if (i == 0) 
        {
            for (unsigned j = 0; j < fVertices.length(); j++) 
            {
                mapping[j] = j;
            }
        }
        bool        clampedSrc;
        clampedSrc = theMeshSrc.isColorClamped(colorSetNamesSrc[i]);
        repSrc = theMeshSrc.getColorRepresentation(colorSetNamesSrc[i]);
        theMeshSrc.getVertexColors(colorArraySrc, &(colorSetNamesSrc[i]));
        theMeshDst.createColorSet(colorSetNamesSrc[i], NULL, clampedSrc, repSrc);
        if (colorArraySrc.
length() > 0 && colorArraySrc.
length() == fVertices.length())
 
        {
            theMeshDst.setVertexColors(colorArraySrc, mapping, NULL, repSrc);
        }
    }
    
    theMeshSrc.getUVSetNames(uvSetNamesSrc);
    int numUVSetsSrc = uvSetNamesSrc.
length();
 
    theMeshDst.getCurrentUVSetName(defaultUVSetName);
    for (i = 0; i < numUVSetsSrc; i++) 
    {
        theMeshSrc.getUVs(uArraySrc, vArraySrc, &(uvSetNamesSrc[i]));
        if (uvSetNamesSrc[i] != defaultUVSetName) 
        {
            theMeshDst.createUVSet(uvSetNamesSrc[i]);
        }
        {
            theMeshDst.setUVs(uArraySrc, vArraySrc, &(uvSetNamesSrc[i]));
            theMeshSrc.getAssignedUVs(uvCounts, uvIds, &(uvSetNamesSrc[i]));
            theMeshDst.assignUVs(uvCounts, uvIds, &(uvSetNamesSrc[i]));
        }
    }
    return stat;
}
{
    int i;
    stat = theMesh.createInPlace( fVertices.length(), fPolygonCounts.length(), fVertices, fPolygonCounts, fPolygonConnects );
    
    int numColorSets = fColorSetNames.length();
    for (i = 0; i < numColorSets; i++) 
    {
        if (i == 0) 
        {
            for (unsigned j = 0; j < fVertices.length(); j++) 
            {
                mapping[j] = j;
            }
        }
        theMesh.createColorSet(fColorSetNames[i], NULL, fClampedArray[i], fRepArray[i]);
        if (fColorArrays[i].length() > 0 && fColorArrays[i].length() == fVertices.length())
        {
            theMesh.setVertexColors(fColorArrays[i], mapping, NULL, fRepArray[i]);
        }
    }
    
    int numUVSets = fUVSetNames.
length();
 
    theMesh.getCurrentUVSetName(defaultUVSetName);
    for (i = 0; i < numUVSets; i++) 
    {
        if (fUVSetNames[i] != defaultUVSetName)
        {
            theMesh.createUVSet(fUVSetNames[i]);
        }
        if (fUArrays[i].length() > 0 && fUArrays[i].length() == fVArrays[i].length())
        {
            theMesh.setUVs(fUArrays[i], fVArrays[i], &(fUVSetNames[i]));
            theMesh.assignUVs(fUVCountsArrays[i], fUVIdsArrays[i], &(fUVSetNames[i]));
        }
    }
    reset();
    return stat;
}
void meshRemapCommand::reset()
{
    fPolygonCounts.clear();
    fPolygonConnects.clear();
    fColorSetNames.clear();
    fUVSetNames.clear();
    if (fClampedArray != NULL)
    {
        delete [] fClampedArray;
        fClampedArray = NULL;
    }
    if (fRepArray != NULL)
    {
        delete [] fRepArray;
        fRepArray = NULL;
    }
    if (fUArrays != NULL)
    {
        delete [] fUArrays;
        fUArrays = NULL;
    }
    if (fVArrays != NULL)
    {
        delete [] fVArrays;
        fVArrays = NULL;
    }
    if (fColorArrays != NULL)
    {
        delete [] fColorArrays;
        fColorArrays = NULL;
    }
    if (fUVCountsArrays != NULL)
    {
        delete [] fUVCountsArrays;
        fUVCountsArrays = NULL;
    }
    if (fUVIdsArrays != NULL)
    {
        delete [] fUVIdsArrays;
        fUVIdsArrays = NULL;
    }
}