Index: /trunk/psLib/test/math/tst_psPolyFit3DOrd.c
===================================================================
--- /trunk/psLib/test/math/tst_psPolyFit3DOrd.c	(revision 5820)
+++ /trunk/psLib/test/math/tst_psPolyFit3DOrd.c	(revision 5820)
@@ -0,0 +1,144 @@
+/*****************************************************************************
+This routine must ensure that the psVectorFitPolynomial3D works correctly.
+We create a vectors of data points (x and y), and populate them with the
+values from an arbitrary function setData().  We then call
+psVectorFitPolynomial3D() with a regular polynomial data structure.  We then
+evaluate the polynomial with the coefficients generated above and determine
+if they are within an error tolerance of the expected values.
+     *****************************************************************************/
+#include <stdio.h>
+#include "pslib_strict.h"
+#include "psTest.h"
+#include "psMemory.h"
+#include "psVector.h"
+#include "psImage.h"
+#include "psMinimize.h"
+#include <math.h>
+#define NUM_DATA 10
+#define POLY_ORDER 3
+#define A 1.0
+#define B 2.0
+#define C 3.0
+#define D 4.0
+#define E 5.0
+#define F 4.0
+#define H 3.0
+#define J 3.0
+#define K 1.0
+#define L 5.0
+#define ERROR_TOLERANCE 0.10
+#define FERR 1.0
+#define VERBOSE 0
+
+psF64 setDataF32(psF64 x, psF64 y, psF64 z)
+{
+    if (0) {
+        // Linear case, for testing.
+        return(A + (B * x) + (D * y) + (H * z));
+    } else {
+        return(A + (B * x) + (C * x * x) + (D * y) + (E * y * y) + (F * x * y) + (H * z) +
+               (J * z * z) + (K * x * z) + (L * y * z));
+    }
+}
+
+psS32 t00()
+{
+    psS32 currentId = psMemGetId();
+    psS32 testStatus = true;
+    psS32 memLeaks = 0;
+    psF64 expectData;
+    psF64 actualData;
+
+    psPolynomial3D *myPoly = psPolynomial3DAlloc(POLY_ORDER, POLY_ORDER, POLY_ORDER, PS_POLYNOMIAL_ORD);
+    psVector *x = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *y = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *z = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *f = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *fErr = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        x->data.F32[i] = (psF64) i;
+        y->data.F32[i] = (psF64) i;
+        z->data.F32[i] = (psF64) i;
+        f->data.F32[i] = setDataF32(x->data.F32[i], y->data.F32[i], z->data.F32[i]);
+        fErr->data.F32[i] = FERR;
+    }
+
+    printPositiveTestHeader(stdout,
+                            "psMinimize functions",
+                            "psVectorFitPolynomial3D(): equal difference in variable fErr");
+
+    psVectorFitPolynomial3D(myPoly, NULL, 0, f, fErr, x, y, z);
+
+    if (VERBOSE) {
+        for (psS32 i=0;i<POLY_ORDER+1;i++) {
+            for (psS32 j=0;j<POLY_ORDER+1;j++) {
+                for (psS32 k=0;k<POLY_ORDER+1;k++) {
+                    printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i][j][k]);
+                }
+            }
+        }
+    }
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setDataF32(x->data.F32[i],  y->data.F32[i], z->data.F32[i]);
+        actualData = psPolynomial3DEval(myPoly, x->data.F32[i], y->data.F32[i], z->data.F32[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("TEST ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (VERBOSE) {
+            printf("GOOD: Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+
+    if (0) {
+        printf("Running test with fErr set to NULL.\n");
+        psVectorFitPolynomial3D(myPoly, NULL, 0, f, NULL, x, y, z);
+        for (psS32 i=0;i<NUM_DATA;i++) {
+            expectData = setDataF32(x->data.F32[i],  y->data.F32[i], z->data.F32[i]);
+            actualData = psPolynomial3DEval(myPoly, x->data.F32[i], y->data.F32[i], z->data.F32[i]);
+            if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+                printf("TEST ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                       i, x->data.F32[i], y->data.F32[i], actualData, expectData);
+                testStatus = false;
+            }
+            if (VERBOSE) {
+                printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+            }
+        }
+        printf("Running test with x, y set to NULL.  Should generate error.\n");
+        myPoly = psVectorFitPolynomial3D(NULL, NULL, 0, f, fErr, NULL, NULL, NULL);
+        if (myPoly != NULL) {
+            printf("TEST ERROR: psVectorFitPolynomial3D() returned non-NULL.\n");
+            testStatus = false;
+        }
+    }
+
+    psMemCheckCorruption(1);
+    psFree(myPoly);
+    psFree(x);
+    psFree(y);
+    psFree(z);
+    psFree(f);
+    psFree(fErr);
+    psMemCheckCorruption(1);
+    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
+    if (0 != memLeaks) {
+        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
+    }
+
+    printFooter(stdout,
+                "psMinimize functions",
+                "psVectorFitPolynomial3D(): equal differences in variable fErr",
+                testStatus);
+
+    return (!testStatus);
+}
+
+psS32 main()
+{
+    psLogSetFormat("HLNM");
+    t00();
+}
