getIndexForPosition method

int getIndexForPosition(
  1. Vector3 position
)

Computes the partition index for the given position vector.

Implementation

int getIndexForPosition(Vector3 position ) {
	clampedPosition.copy( position ).clamp( _min, _max );

	int indexX = ( ( ( cellsX * ( clampedPosition.x + _halfWidth ) ) / width ).floor() ).abs();
	int indexY = ( ( ( cellsY * ( clampedPosition.y + _halfHeight ) ) / height ).floor() ).abs();
	int indexZ = ( ( ( cellsZ * ( clampedPosition.z + _halfDepth ) ) / depth ).floor() ).abs();

	// handle index overflow
	if ( indexX == cellsX ) indexX = cellsX - 1;
	if ( indexY == cellsY ) indexY = cellsY - 1;
	if ( indexZ == cellsZ ) indexZ = cellsZ - 1;

	// calculate final index
	return ( indexX * cellsY * cellsZ ) + ( indexY * cellsZ ) + indexZ;
}