11-19-2010 12:23 PM
I've been attempting to convert a number of 2D, dynamically allocated arrays to VARIANTS in order to generate an ActiveX 3D Parametric Surface plot. Unfortunatly, the CA_VariantSet2DArray function has issues with this. As I understand the issue, a translation to VARIANT cannot be completed if non-contiguous memory is passed into the function. I AM able to pass statically defined arrays into the CA_VariantSet2DArray function and generate my 3D Parametric Surface plot just fine. Ultimately, however, the array sizes that will be ploted may vary each time the user plots data. Since the size info isn't known at compile time, I cannot define static arrays.
I've seen a few people who have asked similar questions in these forums in the past, but there don't seem to be any solutions. Is there any a way in which the array to varrient conversion functions can be used with dynamically created arrays??? Is there any sort or work-arround? I tried defining very large static arrays and then only using a portion of them since the CA_VariantSet2DArray requires array dimensions as inputs. Unfortuanately, the unused portions of the static arrays are still copied into the VARIANT and bad data is thus plotted to the 3D Parametric Surface plot.
Any help would be GREATLY appreciated! THANKS!!
Here is a simplfied snippet of my code:
-----------------------------------------------------------------
THIS DOES NOT WORK....
-----------------------------------------------------------------
VARIANT xVt, yVt, zVt;
nRows = 40;
nCols = 39;
float **xVals;
float **yVals;
float **zVals;
int row, col;
xVals = (float **) malloc(nRows * sizeof(float *));
yVals = (float **) malloc(nRows * sizeof(float *));
zVals = (float **) malloc(nRows * sizeof(float *));
for(row = 0; row < nRows; row++)
{
xVals[row] = (float *) malloc(nCols * sizeof(float));
yVals[row] = (float *) malloc(nCols * sizeof(float));
zVals[row] = (float *) malloc(nCols * sizeof(float));
}
for(row = 0; row < nRows; row++)
{
for(col = 0; col < nCols; col++)
{
xVals[row][col] = 0;
yVals[row][col] = 0;
zVals[row][col] = 0;
}
}
// Valid plot values copied into xVals, yVals, and zVals here.
CA_VariantSet2DArray(&xVt, CAVT_FLOAT, nRows, nCols, xVals);
CA_VariantSet2DArray(&yVt, CAVT_FLOAT, nRows, nCols, yVals);
CA_VariantSet2DArray(&zVt, CAVT_FLOAT, nRows, nCols, zVals);
CW3DGraphLib__DCWGraph3DPlot3DParametricSurface (surfGraphHndl, NULL, xVt, yVt, zVt, CA_DEFAULT_VAL);
-----------------------------------------------------------------
THIS DOES WORK...
-----------------------------------------------------------------
VARIANT xVt, yVt, zVt;
nRows = 40;
nCols = 39;
float **xVals;
float **yVals;
float **zVals;
float xtemp[40][39];
float ytemp[40][39];
float ztemp[40][39];
int row, col;
xVals = (float **) malloc(nRows * sizeof(float *));
yVals = (float **) malloc(nRows * sizeof(float *));
zVals = (float **) malloc(nRows * sizeof(float *));
for(row = 0; row < nRows; x++)
{
xVals[row] = (float *) malloc(nCols * sizeof(float));
yVals[row] = (float *) malloc(nCols * sizeof(float));
zVals[row] = (float *) malloc(nCols * sizeof(float));
}
for(row = 0; row < nRows; row++)
{
for(col = 0; col < nCols; col++)
{
xVals[row][col] = 0;
yVals[row][col] = 0;
zVals[row][col] = 0;
}
}
// Valid plot values copied into xVals, yVals, and zVals here.
for (row = 0; row < nRows; row++)
{
for (col = 0; col < nCols; col++)
{
xtemp[row][col] = xVals[row][col];
ytemp[row][col] = yVals[row][col];
ztemp[row][col] = zVals[row][col];
}
}
CA_VariantSet2DArray(&xVt, CAVT_FLOAT, nRows, nCols, xtemp);
CA_VariantSet2DArray(&yVt, CAVT_FLOAT, nRows, nCols, ytemp);
CA_VariantSet2DArray(&zVt, CAVT_FLOAT, nRows, nCols, ztemp);
CW3DGraphLib__DCWGraph3DPlot3DParametricSurface (surfGraphHndl, NULL, xVt, yVt, zVt, CA_DEFAULT_VAL);
Solved! Go to Solution.
11-22-2010 05:38 AM
You need to allocate a single block of memory for each array, something like:
VARIANT xVt, yVt, zVt;
nRows = 40;
nCols = 39;
float *xVals;
float *yVals;
float *zVals;
int row, col;
xVals = (float *) malloc(nCols * nRows * sizeof(float));
yVals = (float *) malloc(nCols * nRows * sizeof(float));
zVals = (float *) malloc(nCols * nRows * sizeof(float));
for(row = 0; row < nRows; row++)
{
for(col = 0; col < nCols; col++)
{
xVals[nCols*row + col] = 0;
yVals[nCols*row + col] = 0;
zVals[nCols*row + col] = 0;
}
}
// Valid plot values copied into xVals, yVals, and zVals here.
CA_VariantSet2DArray(&xVt, CAVT_FLOAT, nRows, nCols, xVals);
CA_VariantSet2DArray(&yVt, CAVT_FLOAT, nRows, nCols, yVals);
CA_VariantSet2DArray(&zVt, CAVT_FLOAT, nRows, nCols, zVals);
CW3DGraphLib__DCWGraph3DPlot3DParametricSurface (surfGraphHndl, NULL, xVt, yVt, zVt, CA_DEFAULT_VAL);
11-22-2010 12:25 PM
Martin,
Thanks for the reply! This does seem to work as desired. I had started to go down that road, but didn't think this would work since it essentially generates a 1D array of size 1 x nRows*nCols. I guess since it's arranged in memory in the same order that a 2D array would be, the conversion to VARIANT works as expected. I just need to make sure that any other time the array is accessed that it is done with a single index (e.g. array[(nCol*row)+col]) instead of two separate indecies for row and column (e.g. array[row][col]).
Thanks again!