Skip to content

Instantly share code, notes, and snippets.

@daviddoria
Created March 21, 2014 18:51
Show Gist options
  • Save daviddoria/9693245 to your computer and use it in GitHub Desktop.
Save daviddoria/9693245 to your computer and use it in GitHub Desktop.
int vtkImageData::ComputeStructuredCoordinates( const double x[3], int ijk[3], double pcoords[3],
const int* extent,
const double* spacing,
const double* origin,
const double* bounds)
{
// tolerance is needed for 2D data (this is squared tolerance)
const double tol2 = 1e-12;
//
// Compute the ijk location
//
int isInBounds = 1;
for (int i = 0; i < 3; i++)
{
double d = x[i] - origin[i];
double doubleLoc = d / spacing[i];
// Floor for negative indexes.
ijk[i] = vtkMath::Floor(doubleLoc);
pcoords[i] = doubleLoc - static_cast<double>(ijk[i]);
int tmpInBounds = 0;
int minExt = extent[i*2];
int maxExt = extent[i*2 + 1];
// check if data is one pixel thick
if ( minExt == maxExt )
{
double dist = x[i] - bounds[2*i];
if (dist*dist <= spacing[i]*spacing[i]*tol2)
{
pcoords[i] = 0.0;
ijk[i] = minExt;
tmpInBounds = 1;
}
}
// low boundary check
else if ( ijk[i] < minExt)
{
if ( (spacing[i] >= 0 && x[i] >= bounds[i*2]) ||
(spacing[i] < 0 && x[i] <= bounds[i*2 + 1]) )
{
pcoords[i] = 0.0;
ijk[i] = minExt;
tmpInBounds = 1;
}
}
// high boundary check
else if ( ijk[i] >= maxExt )
{
if ( (spacing[i] >= 0 && x[i] <= bounds[i*2 + 1]) ||
(spacing[i] < 0 && x[i] >= bounds[i*2]) )
{
// make sure index is within the allowed cell index range
pcoords[i] = 1.0;
ijk[i] = maxExt - 1;
tmpInBounds = 1;
}
}
// else index is definitely within bounds
else
{
tmpInBounds = 1;
}
// clear isInBounds if out of bounds for this dimension
isInBounds = (isInBounds & tmpInBounds);
}
return isInBounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment