Commit 05262e3e authored by David Doria's avatar David Doria
Browse files

ENH: Added Compute*StructuredCoords functions

These functions are the inverse of ComputeCellId
and ComputePointId. They are used to compute structured
coordinates from a linear ids.

Change-Id: Id4f68873a8816f5573eec01ac1c6ad6f577e39f4
parent 9fdf0e26
......@@ -24,6 +24,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
TestPolynomialSolversUnivariate.cxx
TestSmartPointer.cxx
TestSortDataArray.cxx
TestStructuredData.cxx
TestUnicodeStringAPI.cxx
TestUnicodeStringArrayAPI.cxx
TestVariantComparison.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestStructuredData.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSmartPointer.h"
#include "vtkStructuredData.h"
static int TestCellIds();
static int TestPointIds();
int TestStructuredData(int,char *[])
{
int cellIdsResult = TestCellIds();
int pointIdsResult = TestPointIds();
if(cellIdsResult != EXIT_SUCCESS ||
pointIdsResult != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int TestCellIds()
{
int dim[3] = {3,4,5};
for(unsigned int i = 0; i < dim[0] - 1; ++i)
{
for(unsigned int j = 0; j < dim[1] - 1; ++j)
{
for(unsigned int k = 0; k < dim[2] - 1; ++k)
{
int pos[3];
pos[0] = i;
pos[1] = j;
pos[2] = k;
int ijk[3];
vtkIdType id = vtkStructuredData::ComputeCellId(dim, pos);
vtkStructuredData::ComputeCellStructuredCoords(id, dim, ijk);
if(!(pos[0] == ijk[0] && pos[1] == ijk[1] && pos[2] == ijk[2]))
{
std::cerr << "TestStructuredData failed! Structured coords should be ("
<< i << ", " << j << ", " << k << ") but they are ("
<< ijk[0] << ", " << ijk[1] << ", " << ijk[2] << ")" << std::endl;
return EXIT_FAILURE;
}
}
}
}
return EXIT_SUCCESS;
}
int TestPointIds()
{
int dim[3] = {3,4,5};
for(unsigned int i = 0; i < dim[0]; ++i)
{
for(unsigned int j = 0; j < dim[1]; ++j)
{
for(unsigned int k = 0; k < dim[2]; ++k)
{
int pos[3];
pos[0] = i;
pos[1] = j;
pos[2] = k;
int ijk[3];
vtkIdType id = vtkStructuredData::ComputePointId(dim, pos);
vtkStructuredData::ComputePointStructuredCoords(id, dim, ijk);
if(!(pos[0] == ijk[0] && pos[1] == ijk[1] && pos[2] == ijk[2]))
{
std::cerr << "TestStructuredData point structured coords failed!"
<< " Structured coords should be ("
<< i << ", " << j << ", " << k << ") but they are ("
<< ijk[0] << ", " << ijk[1] << ", " << ijk[2] << ")" << std::endl;
return EXIT_FAILURE;
}
}
}
}
return EXIT_SUCCESS;
}
......@@ -111,6 +111,42 @@ public:
static vtkIdType ComputeCellId(int dim[3], int ijk[3]) {
return (ijk[2]*static_cast<vtkIdType>(dim[1]-1) + ijk[1])*(dim[0]-1) + ijk[0];}
// Description:
// Given a cellId and grid dimensions 'dim', get the structured coordinates
// (i-j-k). This method does not adjust for the beginning of the extent.
static void ComputeCellStructuredCoords(const vtkIdType cellId, int dim[3], int ijk[3])
{
int Ni = dim[0]-1;
int Nj = dim[1]-1;
int Nij = Ni*Nj;
int k = cellId/Nij + 1;
int j = (cellId - (k-1)*Nij)/Ni + 1;
int i = cellId - (k-1)*Nij - (j-1)*Ni + 1;
ijk[0] = i-1;
ijk[1] = j-1;
ijk[2] = k-1;
}
// Description:
// Given a pointId and grid dimensions 'dim', get the structured coordinates
// (i-j-k). This method does not adjust for the beginning of the extent.
static void ComputePointStructuredCoords(const vtkIdType cellId, int dim[3], int ijk[3])
{
int Ni = dim[0];
int Nj = dim[1];
int Nij = Ni*Nj;
int k = cellId/Nij + 1;
int j = (cellId - (k-1)*Nij)/Ni + 1;
int i = cellId - (k-1)*Nij - (j-1)*Ni + 1;
ijk[0] = i-1;
ijk[1] = j-1;
ijk[2] = k-1;
}
protected:
vtkStructuredData() {};
~vtkStructuredData() {};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment