/** @file  tst_psCoord.c
*
*  @brief The code will ...
*
*
*  @author GLG, MHPCC
*
*  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
*  @date $Date: 2005-10-12 21:02:20 $
*
*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
*/

#include "psTest.h"
#include "pslib_strict.h"
static psS32 testPlaneTransformAlloc(void);
static psS32 testPlaneDistortAlloc(void);
static psS32 testPlaneTransformApply(void);
static psS32 testPlaneDistortApply(void);

testDescription tests[] = {
                              {testPlaneTransformAlloc, 826, "psPlaneTransformAlloc()", 0, false},
                              {testPlaneDistortAlloc, 827, "psPlaneDistortAlloc()", 0, false},
                              {testPlaneTransformApply, 831, "psPlaneTransformApply()", 0, false},
                              {testPlaneDistortApply, 832, "psPlaneDistortApply()", 0, false},
                              {NULL}
                          };

psS32 main( psS32 argc, char* argv[] )
{
    psLogSetFormat("HLNM");
    psLogSetLevel( PS_LOG_INFO );

    return ( ! runTestSuite( stderr, "psCoord", tests, argc, argv ) );
}

#define NX_TERMS 2
#define NY_TERMS 3
#define NZ_TERMS 4
#define NT_TERMS 5

psS32 testPlaneTransformAlloc( void )
{
    // Allocate psPlaneTransform with known x and y terms
    psPlaneTransform *myPT = psPlaneTransformAlloc(NX_TERMS, NY_TERMS);

    // Verify returned value is not NULL
    if(myPT == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Function returned NULL not expected.");
        return 1;
    }

    // Verify returned transform has members set properly
    if ((1 + myPT->x->nX) != NX_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPT->x->nX is %d, should be %d",
                myPT->x->nX, NX_TERMS-1);
        return 2;
    }
    if ((1 + myPT->y->nX) != NX_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPT->y->nX is %d, should be %d",
                myPT->y->nX, NX_TERMS-1);
        return 3;
    }
    if ((1 + myPT->x->nY) != NY_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPT->x->nY is %d, should be %d",
                myPT->x->nY, NY_TERMS-1);
        return 4;
    }
    if ((1 + myPT->y->nY) != NY_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPT->y->nY is %d, should be %d",
                myPT->y->nY, NY_TERMS-1);
        return 5;
    }

    // Free plane transform
    psFree(myPT);

    // Attempt to specify negative x terms and verify NULL returned and errror message generated
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message: negative x terms");
    myPT = psPlaneTransformAlloc(-1.0, 1.0);
    if (myPT != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneTransformAlloc() did not return NULL.");
        return 6;
    }

    // Attempt to specify negative y terms and verify NULL returned and error message generated
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message: negative y terms");
    myPT = psPlaneTransformAlloc(1.0, -1.0);
    if (myPT != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneTransformAlloc() did not return NULL.");
        return 7;
    }

    psFree(myPT);

    return 0;
}


psS32 testPlaneDistortAlloc( void )
{
    // Invoke function with known parameters
    psPlaneDistort *myPD = psPlaneDistortAlloc(NX_TERMS, NY_TERMS, NZ_TERMS, NT_TERMS);

    // Verify NULL is not returned
    if(myPD == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Return of NULL not expected");
        return 1;
    }
    // Verify the terms are properly set after allocation
    if ((1 + myPD->x->nX) != NX_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->x->nX is %d, should be %d",
                myPD->x->nX, NX_TERMS-1);
        return 2;
    }
    if ((1 + myPD->y->nX) != NX_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->y->nX is %d, should be %d",
                myPD->y->nX, NX_TERMS-1);
        return 3;
    }
    if ((1 + myPD->x->nY) != NY_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->x->nY is %d, should be %d",
                myPD->x->nY, NY_TERMS-1);
        return 4;
    }
    if ((1 + myPD->y->nY) != NY_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->y->nY is %d, should be %d",
                myPD->y->nY, NY_TERMS-1);
        return 5;
    }
    if ((1 + myPD->x->nZ) != NZ_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->x->nZ is %d, should be %d",
                myPD->x->nX, NZ_TERMS-1);
        return 6;
    }
    if ((1 + myPD->y->nZ) != NZ_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->y->nZ is %d, should be %d",
                myPD->y->nZ, NZ_TERMS-1);
        return 7;
    }
    if ((1 + myPD->x->nT) != NT_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->x->nT is %d, should be %d",
                myPD->x->nT, NT_TERMS-1);
        return 8;
    }
    if ((1 + myPD->y->nT) != NT_TERMS) {
        psError(PS_ERR_UNKNOWN,true,"myPD->y->nT is %d, should be %d",
                myPD->y->nT, NT_TERMS-1);
        return 9;
    }
    // Free psPlaneTransform
    psFree(myPD);

    // Invoke function with negative x term parameter
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for negative x term");
    myPD = psPlaneDistortAlloc(-1.0, 1.0, 1.0, 1.0);
    if (myPD != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneDistortAlloc() did not return NULL.");
        return 10;
    }

    // Invoke function with negative y term parameter
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for negative y term");
    myPD = psPlaneDistortAlloc(1.0, -1.0, 1.0, 1.0);
    if (myPD != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneDistortAlloc() did not return NULL.");
        return 11;
    }

    // Invoke function with negative z term parameter
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for negative z term");
    myPD = psPlaneDistortAlloc(1.0, 1.0, -1.0, 1.0);
    if (myPD != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneDistortAlloc() did not return NULL.");
        return 12;
    }

    // Invoke function with negative w term parameter
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for negative w term");
    myPD = psPlaneDistortAlloc(1.0, 1.0, 1.0, -1.0);
    if (myPD != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psPlaneDistortAlloc() did not return NULL.");
    }

    return 0;
}

#define N 10
// We do a simple identity transformation on a few x,y pairs.
psS32 testPlaneTransformApply( void )
{
    psPlane*          out = NULL;
    psPolynomial2D*  tmp2DPoly = NULL;
    psPlane*          rc;

    // Create input coordinate
    psPlane* in = psPlaneAlloc();
    if(in == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Did not expect NULL return from psPlaneAlloc");
        return 1;
    }

    // Create transform
    psPlaneTransform* pt = psPlaneTransformAlloc(2,2);

    // Set transform coefficients so the x coord input will equal x coord output, same for y
    pt->x->coeff[1][0] = 1.0;
    pt->y->coeff[0][1] = 1.0;

    // Apply transform for several points
    for (psS32 i = 0; i < N; i++) {
        in->x = (psF64) i;
        in->y = (psF64) (i + 5.0);
        in->xErr = 0.0;
        in->yErr = 0.0;

        // Verify function does not return NULL
        out = psPlaneTransformApply(out, pt, in);
        if( out  == NULL) {
            psError(PS_ERR_UNKNOWN,true,"Expected non-NULL return value");
            return 1;
        }
        // Verify the expected values are returned
        if (FLT_EPSILON < fabs(out->x - in->x)) {
            psError(PS_ERR_UNKNOWN,true,"out.x is %lf, should be %lf", out->x, in->x);
            return 3;
        }
        if (FLT_EPSILON < fabs(out->y - in->y)) {
            psError(PS_ERR_UNKNOWN,true,"out.y is %lf, should be %lf", out->y, in->y);
            return 4;
        }
    }
    psFree(out);

    // Verify return null and error message generater for null specified transform
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for NULL psPlaneTransform");
    rc = psPlaneTransformApply(NULL, NULL, in);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Function did not return NULL as expected for NULL psPlaneTransform.");
        return 5;
    }

    // Verify return null and error message generated for null psPlaneTransform x coeff
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for NULL x coeff psPlaneTransform");
    tmp2DPoly = pt->x;
    pt->x = NULL;
    rc = psPlaneTransformApply(NULL, pt, in);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Function did not return NULL as expected for NULL x coeff psPlaneTransform");
        return 6;
    }
    pt->x = tmp2DPoly;

    // Verify return null and error message generated for null psPlaneTransform y coeff
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for NULL y coeff psPlaneTransform");
    tmp2DPoly = pt->y;
    pt->y = NULL;
    rc = psPlaneTransformApply(NULL, pt, in);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Function did not return NULL as expected for NULL y coeff psPlaneTransform");
        return 7;
    }
    pt->y = tmp2DPoly;

    // Verify return null and error message generated for null psPlane coordinate
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message for NULL psPlane");
    rc = psPlaneTransformApply(NULL, pt, NULL);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Function did not return NULL as expected for NULL psPlane");
        return 8;
    }

    psFree(pt);
    psFree(in);

    return 0;
}

#define COLOR 1.0
#define MAGNITUDE 1.0
// We do a simple identity transformation on a few x,y pairs.  For x and y,
// we add in the COLOR and MAGNITUDE.
psS32 testPlaneDistortApply( void )
{
    psPlane*         out       = NULL;
    psPlane*         rc        = NULL;
    psPolynomial4D* tmp4DPoly = NULL;

    // Allocate input coordinate
    psPlane* in = psPlaneAlloc();

    // Create psPlaneTransform structure
    psPlaneDistort*  pt = psPlaneDistortAlloc(2, 2, 2, 2);

    pt->x->coeff[1][0][0][0] = 1.0;
    pt->x->coeff[0][0][1][0] = 1.0;
    pt->x->coeff[0][0][0][1] = 1.0;

    pt->y->coeff[0][1][0][0] = 1.0;
    pt->y->coeff[0][0][1][0] = 1.0;
    pt->y->coeff[0][0][0][1] = 1.0;

    for (psS32 i = 0; i < N; i++) {
        in->x = (psF64) i;
        in->y = (psF64) (i + 5.0);
        in->xErr = 0.0;
        in->yErr = 0.0;
        out = psPlaneDistortApply(out, pt, in, COLOR, MAGNITUDE);

        // Verify return value is not NULL
        if(out == NULL) {
            psError(PS_ERR_UNKNOWN,true,"Expected non-NULL return");
            return 1;
        }
        // Verify return value contains expected values
        if (FLT_EPSILON < fabs(out->x - COLOR - MAGNITUDE - in->x)) {
            psError(PS_ERR_UNKNOWN,true,"out->x is %lf, should be %lf",
                    out->x, in->x + COLOR + MAGNITUDE);
            return 2;
        }
        if (FLT_EPSILON < fabs(out->y - COLOR - MAGNITUDE - in->y)) {
            psError(PS_ERR_UNKNOWN,true,"out->y is %lf, should be %lf",
                    out->y, in->y + COLOR + MAGNITUDE);
            return 3;
        }
    }
    psFree(out);

    // Attempt to invoke psPlaneDistortApply with NULL psPlaneDistort
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for null psPlaneDistort");
    rc = psPlaneDistortApply(NULL, NULL, in, COLOR, MAGNITUDE);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Did not return NULL.");
        return 4;
    }

    // Attempt to invoke psPlaneDistortApply with NULL psPlaneDistort x member
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for null x member psPlaneDistort");
    tmp4DPoly = pt->x;
    pt->x = NULL;
    rc = psPlaneDistortApply(NULL, pt, in, COLOR, MAGNITUDE);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Did not return NULL.");
        return 5;
    }
    pt->x = tmp4DPoly;

    // Attempt to invoke psPlaneDistortApply with NULL psPlaneDistort y member
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for null y member psPlaneDistort");
    tmp4DPoly = pt->y;
    pt->y = NULL;
    rc = psPlaneDistortApply(NULL, pt, in, COLOR, MAGNITUDE);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Did not return NULL.");
        return 6;
    }
    pt->y = tmp4DPoly;

    // Attempt to invoke psPlaneDistortApply with NULL input psPlane
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for null input psPlane");
    rc = psPlaneDistortApply(NULL, pt, NULL, COLOR, MAGNITUDE);
    if (rc != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Did not return NULL");
        return 7;
    }

    psFree(pt);
    psFree(in);

    return 0;
}

