1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
//
// \author Denis P. Shamonin and Marius Staring. Division of Image Processing,
// Department of Radiology, Leiden, The Netherlands
//
// \note This work was funded by the Netherlands Organisation for
// Scientific Research (NWO NRG-2010.02 and NWO 639.021.124).
//
// OpenCL implementation of itk::BSplineTransform
//------------------------------------------------------------------------------
void set_weights( const float cindex,
const uint spline_order, const long startindex,
const uint offset, float * weights )
{
// The code below was taken from:
// elastix/src/Common/Transforms/itkBSplineKernelFunction2.h
// Compared to the ITK version this code assigns to the entire
// weights vector at once, instead of in a loop, thereby avoiding
// the excessive use of if statements.
const float u = cindex - (float)( startindex );
if( spline_order == 3 )
{
const float uu = pown(u, 2);
const float uuu = uu * u;
weights[offset ] = ( 8.0f - 12.0f * u + 6.0f * uu - uuu ) / 6.0f;
weights[offset + 1] = ( mad(21.0f, u, -5.0f) - 15.0f * uu + 3.0f * uuu ) / 6.0f;
weights[offset + 2] = ( 4.0f - 12.0f * u + 12.0f * uu - 3.0f * uuu ) / 6.0f;
weights[offset + 3] = ( mad(3.0f, u, -1.0f) - 3.0f * uu + uuu ) / 6.0f;
return;
}
else if( spline_order == 0 )
{
if( u < 0.5f ) { weights[offset + 0] = 1.0f; }
else { weights[offset] = 0.5f; }
return;
}
else if( spline_order == 1 )
{
weights[offset ] = 1.0f - u;
weights[offset + 1] = u - 1.0f;
return;
}
else if( spline_order == 2 )
{
const float uu = u * u;
weights[offset ] = ( 9.0f - 12.0f * u + 4.0f * uu ) / 8.0f;
weights[offset + 1] = mad(2.0f, u, -0.25f) - uu;
weights[offset + 2] = ( 1.0f - 4.0f * u + 4.0f * uu ) / 8.0f;
return;
}
}
//------------------------------------------------------------------------------
#ifdef DIM_1
bool inside_valid_region_1d( float * cindex, const uint spline_order,
uint coefficients_image_size_x )
{
const float min_limit = 0.5f * (float)( spline_order - 1 );
const float max_helper = 0.5f * (float)( spline_order - 1 ) + 1.0f;
const float eps = 1.0e-6f;
// x
float max_limit = coefficients_image_size_x - max_helper;
float cind = (*cindex); float diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex) -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
return true;
}
#endif // DIM_1
//------------------------------------------------------------------------------
#ifdef DIM_2
bool inside_valid_region_2d( float2 * cindex, const uint spline_order,
uint2 coefficients_image_size )
{
const float min_limit = 0.5f * (float)( spline_order - 1 );
const float max_helper = 0.5f * (float)( spline_order - 1 ) + 1.0f;
const float eps = 1.0e-6f;
// x
float max_limit = coefficients_image_size.x - max_helper;
float cind = (*cindex).x; float diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex).x -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
// y
max_limit = coefficients_image_size.y - max_helper;
cind = (*cindex).y; diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex).y -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
return true;
}
#endif // DIM_2
//------------------------------------------------------------------------------
#ifdef DIM_3
bool inside_valid_region_3d( float3 * cindex, const uint spline_order,
uint3 coefficients_image_size )
{
const float min_limit = 0.5f * (float)( spline_order - 1 );
const float max_helper = 0.5f * (float)( spline_order - 1 ) + 1.0f;
const float eps = 1.0e-6f;
// x
float max_limit = coefficients_image_size.x - max_helper;
float cind = (*cindex).x; float diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex).x -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
// y
max_limit = coefficients_image_size.y - max_helper;
cind = (*cindex).y; diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex).y -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
// z
max_limit = coefficients_image_size.z - max_helper;
cind = (*cindex).z; diff = cind - max_limit;
if( diff > 0.0f && diff < eps ){ (*cindex).z -= eps; }
else if( cind >= max_limit ) return false;
else if( cind < min_limit ) return false;
return true;
}
#endif // DIM_3
//------------------------------------------------------------------------------
#ifdef DIM_1
long evaluate_1d( const float index,
const uint spline_order, const uint support_size,
const uint number_of_weights, float * weights )
{
// find the starting index of the support region
const long startindex = (long)( floor( index - (float)( spline_order - 1 ) / 2.0f ) );
// number of elements in offset_to_index_table in 1d computed using formula spline_order + 1.
// we allocate the maximum to avoid using if's for all spline orders.
ulong offset_to_index_table[4];
for( uint i = 0; i < support_size; ++i )
{
offset_to_index_table[i] = i;
}
// number of weights1D in 1d computed using formula spline_order + 1.
// we allocate the maximum to avoid using if's for all spline orders.
float weights1D[4];
set_weights( index, spline_order, startindex, 0, weights1D );
// compute all possible products of the 1D weights
for( uint k = 0; k < number_of_weights; ++k )
{
weights[k] = weights1D[offset_to_index_table[k]];
}
// return start index
return startindex;
}
#endif // DIM_1
//------------------------------------------------------------------------------
#ifdef DIM_2
long2 evaluate_2d( const float2 cindex,
const uint spline_order, const uint support_size,
const uint number_of_weights, float * weights )
{
// find the starting index of the support region
long2 startIndex;
const float tmp = (float)( spline_order - 1 ) / 2.0f;
startIndex.x = (long)( floor( cindex.x - tmp ) );
startIndex.y = (long)( floor( cindex.y - tmp ) );
// number of weights1D in 2d computed using formula 2 * (spline_order + 1).
// we allocate the maximum to avoid using if's for all spline orders.
float weights1D[8];
set_weights( cindex.x, spline_order, startIndex.x, 0, weights1D );
set_weights( cindex.y, spline_order, startIndex.y, support_size, weights1D );
// compute all possible products of the 1D weights
uint x, y;
for( uint k = 0; k < number_of_weights; ++k )
{
x = k % support_size;
y = ( k / support_size ) % support_size;
weights[ k ] = weights1D[ x ] * weights1D[ 1 * support_size + y ];
}
// return start index
return startIndex;
}
#endif // DIM_2
//------------------------------------------------------------------------------
#ifdef DIM_3
long3 evaluate_3d( const float3 cindex,
const uint spline_order, const uint support_size,
const uint number_of_weights, float * weights )
{
// find the starting index of the support region
long3 startIndex;
const float tmp = (float)( spline_order - 1 ) / 2.0f;
startIndex.x = (long)( floor( cindex.x - tmp ) );
startIndex.y = (long)( floor( cindex.y - tmp ) );
startIndex.z = (long)( floor( cindex.z - tmp ) );
// number of weights1D in 3d computed using formula 3 * (spline_order + 1).
// we allocate the maximum to avoid using if's for all spline orders.
float weights1D[12];
set_weights( cindex.x, spline_order, startIndex.x, 0, weights1D );
set_weights( cindex.y, spline_order, startIndex.y, support_size, weights1D );
set_weights( cindex.z, spline_order, startIndex.z, support_size * 2, weights1D );
// compute all possible products of the 1D weights
uint x, y, z;
for( uint k = 0; k < number_of_weights; ++k )
{
x = k % support_size;
y = ( k / support_size ) % support_size;
z = ( k / support_size / support_size ) % support_size;
weights[ k ] = weights1D[ x ] * weights1D[ 1 * support_size + y ] * weights1D[ 2 * support_size + z ];
}
// return start index
return startIndex;
}
#endif // DIM_3
//------------------------------------------------------------------------------
#ifdef DIM_1
float bspline_transform_point_1d( const float point,
const uint spline_order,
__constant GPUImageBase1D *coefficients_image,
__global const float *coefficients )
{
float tpoint = 0;
float cindex;
// \todo:
// only needs PhysicalPointToIndex, Origin, Size
// not Direction, IndexToPhysicalPoint, Spacing
// Memory passing reduces to 8/18 x 100%.
transform_physical_point_to_continuous_index_1d( point, &cindex, coefficients_image );
const bool inside = inside_valid_region_1d( &cindex, spline_order, coefficients_image->size );
if ( !inside )
{
tpoint = point;
return tpoint;
}
// support region and coefficient image size, equals B-spline order + 1
const uint support_size = spline_order + 1;
// number of weights in 1d computed using formula pow(spline_order + 1, 1).
// we allocate the maximum to avoid using if's for all spline orders.
float weights[4];
const uint number_of_weights = support_size;
const long support_index = evaluate_1d( cindex, spline_order, support_size,
number_of_weights, weights );
const uint support_region = support_index + support_size;
// multiply weight with coefficient
uint counter = 0;
float c, w;
for ( uint i = (uint)( support_index ); i < support_region; ++i )
{
c = coefficients[i];
w = weights[counter];
tpoint = mad( c, w, tpoint );
++counter;
}
tpoint += point;
return tpoint;
}
#endif // DIM_1
//------------------------------------------------------------------------------
#ifdef DIM_2
float2 bspline_transform_point_2d( const float2 point,
const uint spline_order,
__constant GPUImageBase2D *coefficients_image,
__global const float *coefficients0,
__global const float *coefficients1 )
{
float2 tpoint = (float2)( 0, 0 );
float2 cindex;
// \todo:
// only needs PhysicalPointToIndex, Origin, Size
// not Direction, IndexToPhysicalPoint, Spacing
// Memory passing reduces to 8/18 x 100%.
transform_physical_point_to_continuous_index_2d( point, &cindex, coefficients_image );
const bool inside = inside_valid_region_2d( &cindex, spline_order, coefficients_image->size );
if( !inside ) return point;
// support region and coefficient image size, equals B-spline order + 1
const uint support_size = spline_order + 1;
// number of weights in 2d computed using formula pow(spline_order + 1, 2).
// we allocate the maximum to avoid using if's for all spline orders.
float weights[16];
const uint number_of_weights = support_size * support_size;
const long2 start_index = evaluate_2d( cindex, spline_order, support_size,
number_of_weights, weights );
// copy kernel parameter from const memory to local memory for speedup
const uint coefficients_image_size_x = coefficients_image->size.x;
// multiply weight with coefficient
float cx, cy, w;
uint x, y, gidx;
for( uint k = 0; k < number_of_weights; ++k )
{
// Get the index of the point corresponding to this weight
// Extra computation to avoid triple loop
x = start_index.x + ( k % support_size );
y = start_index.y + ( k / support_size ) % support_size;
// Get the global index of the coefficient image
gidx = mad24( coefficients_image_size_x, y, x );
// Get the coefficients and weight from memory
cx = coefficients0[ gidx ];
cy = coefficients1[ gidx ];
w = weights[ k ];
// Perform the multiplication and update the output point
tpoint.x = mad( cx, w, tpoint.x );
tpoint.y = mad( cy, w, tpoint.y );
}
// transformation = deformation + input point
tpoint += point;
return tpoint;
}
#endif // DIM_2
//------------------------------------------------------------------------------
#ifdef DIM_3
float3 bspline_transform_point_3d( const float3 point,
const uint spline_order,
__constant GPUImageBase3D *coefficients_image, // only partially needed
__global const float *coefficients0,
__global const float *coefficients1,
__global const float *coefficients2 )
{
float3 tpoint = (float3)( 0, 0, 0 );
// convert point to continuous index
float3 cindex = transform_physical_point_to_continuous_index_3d( point,
coefficients_image->physical_point_to_index, coefficients_image->origin );
// check if inside
const bool inside = inside_valid_region_3d( &cindex, spline_order, coefficients_image->size );
if( !inside ) return point;
// support region and coefficient image size, equals B-spline order + 1
const uint support_size = spline_order + 1;
// number of weights in 3d computed using formula pow(spline_order + 1, 3).
// we allocate the maximum to avoid using if's for all spline orders.
float weights[64];
const uint number_of_weights = support_size * support_size * support_size;
const long3 start_index = evaluate_3d( cindex, spline_order, support_size,
number_of_weights, weights );
// copy kernel parameter from const memory to local memory for speedup
const uint coefficients_image_size_x = coefficients_image->size.x;
const uint coefficients_image_size_y = coefficients_image->size.y;
// multiply weight with coefficient
float cx, cy, cz, w;
uint x, y, z, gidx;
for( uint k = 0; k < number_of_weights; ++k )
{
// Get the index of the point corresponding to this weight
// Extra computation to avoid triple loop
x = start_index.x + ( k % support_size );
y = start_index.y + ( k / support_size ) % support_size;
z = start_index.z + ( k / support_size / support_size ) % support_size;
// Get the global index of the coefficient image
gidx = mad24( coefficients_image_size_x,
mad24( z, coefficients_image_size_y, y ), x );
// Get the coefficients and weight from memory
cx = coefficients0[ gidx ];
cy = coefficients1[ gidx ];
cz = coefficients2[ gidx ];
w = weights[ k ];
// Perform the multiplication and update the output point
tpoint.x = mad( cx, w, tpoint.x );
tpoint.y = mad( cy, w, tpoint.y );
tpoint.z = mad( cz, w, tpoint.z );
}
// transformation = deformation + input point
tpoint += point;
return tpoint;
}
#endif // DIM_3
|