File size: 24,939 Bytes
34a4bcb |
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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
/*
Copyright (c) MONAI Consortium
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
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.
*/
// adapted from https://github.com/balbasty/nitorch
#pragma once
// This file contains static functions for handling (0-7 order)
// interpolation weights.
// It also defines an enumerated types that encodes each boundary type.
// The entry points are:
// . monai::interpolation::weight -> node weight based on distance
// . monai::interpolation::fastweight -> same, assuming x lies in support
// . monai::interpolation::grad -> weight derivative // oriented distance
// . monai::interpolation::fastgrad -> same, assuming x lies in support
// . monai::interpolation::hess -> weight 2nd derivative // oriented distance
// . monai::interpolation::fasthess -> same, assuming x lies in support
// . monai::interpolation::bounds -> min/max nodes
// NOTE:
// 1st derivatives used to be implemented with a recursive call, e.g.:
// scalar_t grad2(scalar_t x) {
// if (x < 0) return -grad2(-x);
// ...
// }
// However, this prevents nvcc to statically determine the stack size
// and leads to memory errors (because the allocated stack is too small).
// I now use a slightly less compact implementation that gets rid of
// recursive calls.
// TODO:
// . second order derivatives [5/6/7]
// ? other types of basis functions (gauss, sinc)
#include "utils/resample_utils.h"
namespace monai {
namespace _interpolation {
// --- order 0 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight0(scalar_t x) {
x = std::fabs(x);
return x < 0.5 ? static_cast<scalar_t>(1) : static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight0(scalar_t x) {
x = std::fabs(x);
return static_cast<scalar_t>(1);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad0(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad0(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess0(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess0(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds0(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::round(x));
upp = low;
}
// --- order 1 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight1(scalar_t x) {
x = std::fabs(x);
return x < 1 ? static_cast<scalar_t>(1) - x : static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight1(scalar_t x) {
return static_cast<scalar_t>(1) - std::fabs(x);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad1(scalar_t x) {
if (std::fabs(x) >= 1)
return static_cast<scalar_t>(0);
return fastgrad1(x);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad1(scalar_t x) {
return x < static_cast<scalar_t>(0) ? static_cast<scalar_t>(1) : static_cast<scalar_t>(-1);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess1(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess1(scalar_t x) {
return static_cast<scalar_t>(0);
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds1(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x));
upp = low + 1;
}
// --- order 2 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight2(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return 0.75 - x * x;
} else if (x < 1.5) {
x = 1.5 - x;
return 0.5 * x * x;
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight2(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return 0.75 - x * x;
} else {
x = 1.5 - x;
return 0.5 * x * x;
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad2(scalar_t x) {
bool neg = x < 0;
if (x < 0.5) {
x = -2. * x;
} else if (x < 1.5) {
x = x - 1.5;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad2(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 0.5) {
x = -2. * x;
} else {
x = x - 1.5;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess2(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return static_cast<scalar_t>(-2.);
} else if (x < 1.5) {
return static_cast<scalar_t>(1.);
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess2(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return static_cast<scalar_t>(-2.);
} else {
return static_cast<scalar_t>(1.);
}
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds2(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - .5));
upp = low + 2;
}
// --- order 3 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight3(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
return (x * x * (x - 2.) * 3. + 4.) / 6.;
} else if (x < 2.) {
x = 2. - x;
return (x * x * x) / 6.;
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight3(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
return (x * x * (x - 2.) * 3. + 4.) / 6.;
} else {
x = 2. - x;
return (x * x * x) / 6.;
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad3(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
x = x * (x * 1.5 - 2.);
} else if (x < 2.) {
x = 2. - x;
x = -(x * x) * 0.5;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad3(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
x = x * (x * 1.5 - 2.);
} else {
x = 2. - x;
x = -(x * x) * 0.5;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess3(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
return x * 3. - 2.;
} else if (x < 2.) {
return 2. - x;
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess3(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
return x * 3. - 2.;
} else {
return 2. - x;
}
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds3(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - 1.));
upp = low + 3;
}
// --- order 4 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight4(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
x *= x;
return x * (x * 0.25 - 0.625) + 115. / 192.;
} else if (x < 1.5) {
return x * (x * (x * (5. - x) / 6. - 1.25) + 5. / 24.) + 55. / 96.;
} else if (x < 2.5) {
x -= 2.5;
x *= x;
return (x * x) / 24.;
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight4(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
x *= x;
return x * (x * 0.25 - 0.625) + 115. / 192.;
} else if (x < 1.5) {
return x * (x * (x * (5. - x) / 6. - 1.25) + 5. / 24.) + 55. / 96.;
} else {
x -= 2.5;
x *= x;
return (x * x) / 24.;
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad4(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 0.5) {
x = x * (x * x - 1.25);
} else if (x < 1.5) {
x = x * (x * (x * (-2. / 3.) + 2.5) - 2.5) + 5. / 24.;
} else if (x < 2.5) {
x = x * 2. - 5.;
x = (x * x * x) / 48.;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad4(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 0.5) {
x = x * (x * x - 1.25);
} else if (x < 1.5) {
x = x * (x * (x * (-2. / 3.) + 2.5) - 2.5) + 5. / 24.;
} else {
x = x * 2. - 5.;
x = (x * x * x) / 48.;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess4(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return (x * x) * 3. - 1.25;
} else if (x < 1.5) {
return x * (x * (-2.) + 5.) - 2.5;
} else if (x < 2.5) {
x = x * 2. - 5.;
return (x * x) / 8.;
} else {
return static_cast<scalar_t>(0);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess4(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
return (x * x) * 3. - 1.25;
} else if (x < 1.5) {
return x * (x * (-2.) + 5.) - 2.5;
} else {
x = x * 2. - 5.;
return (x * x) / 8.;
}
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds4(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - 1.5));
upp = low + 4;
}
// --- order 5 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight5(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
scalar_t f = x * x;
return f * (f * (0.25 - x * (1. / 12.)) - 0.5) + 0.55;
} else if (x < 2.) {
return x * (x * (x * (x * (x * (1. / 24.) - 0.375) + 1.25) - 1.75) + 0.625) + 0.425;
} else if (x < 3.) {
scalar_t f = 3. - x;
x = f * f;
return f * x * x * (1. / 120.);
} else
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight5(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
scalar_t f = x * x;
return f * (f * (0.25 - x * (1. / 12.)) - 0.5) + 0.55;
} else if (x < 2.) {
return x * (x * (x * (x * (x * (1. / 24.) - 0.375) + 1.25) - 1.75) + 0.625) + 0.425;
} else {
scalar_t f = 3. - x;
x = f * f;
return f * x * x * (1. / 120.);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad5(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
x = x * (x * (x * (x * (-5. / 12.) + 1.)) - 1.);
} else if (x < 2.) {
x = x * (x * (x * (x * (5. / 24.) - 1.5) + 3.75) - 3.5) + 0.625;
} else if (x < 3.) {
x -= 3.;
x *= x;
x = -(x * x) / 24.;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad5(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
x = x * (x * (x * (x * (-5. / 12.) + 1.)) - 1.);
} else if (x < 2.) {
x = x * (x * (x * (x * (5. / 24.) - 1.5) + 3.75) - 3.5) + 0.625;
} else {
x -= 3.;
x *= x;
x = -(x * x) / 24.;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds5(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - 2.));
upp = low + 5;
}
// --- order 6 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight6(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
x *= x;
return x * (x * (7. / 48. - x * (1. / 36.)) - 77. / 192.) + 5887. / 11520.0;
} else if (x < 1.5) {
return x * (x * (x * (x * (x * (x * (1. / 48.) - 7. / 48.) + 0.328125) - 35. / 288.) - 91. / 256.) - 7. / 768.) +
7861. / 15360.0;
} else if (x < 2.5) {
return x * (x * (x * (x * (x * (7. / 60. - x * (1. / 120.)) - 0.65625) + 133. / 72.) - 2.5703125) + 1267. / 960.) +
1379. / 7680.0;
} else if (x < 3.5) {
x -= 3.5;
x *= x * x;
return x * x * (1. / 720.);
} else
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight6(scalar_t x) {
x = std::fabs(x);
if (x < 0.5) {
x *= x;
return x * (x * (7. / 48. - x * (1. / 36.)) - 77. / 192.) + 5887. / 11520.0;
} else if (x < 1.5) {
return x * (x * (x * (x * (x * (x * (1. / 48.) - 7. / 48.) + 0.328125) - 35. / 288.) - 91. / 256.) - 7. / 768.) +
7861. / 15360.0;
} else if (x < 2.5) {
return x * (x * (x * (x * (x * (7. / 60. - x * (1. / 120.)) - 0.65625) + 133. / 72.) - 2.5703125) + 1267. / 960.) +
1379. / 7680.0;
} else {
x -= 3.5;
x *= x * x;
return x * x * (1. / 720.);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad6(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < .5) {
scalar_t x2 = x * x;
x = x * (x2 * (7. / 12.) - (x2 * x2) / 6. - 77. / 96.);
} else if (x < 1.5) {
x = x * (x * (x * (x * (x * 0.125 - 35. / 48.) + 1.3125) - 35. / 96.) - 0.7109375) - 7.0 / 768.0;
} else if (x < 2.5) {
x = x * (x * (x * (x * (x * (-1. / 20.) + 7. / 12.) - 2.625) + 133. / 24.) - 5.140625) + 1267. / 960.;
} else if (x < 3.5) {
x *= 2.;
x -= 7.;
scalar_t x2 = x * x;
x = (x2 * x2 * x) / 3840.;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad6(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < .5) {
scalar_t x2 = x * x;
x = x * (x2 * (7. / 12.) - (x2 * x2) / 6. - 77. / 96.);
} else if (x < 1.5) {
x = x * (x * (x * (x * (x * 0.125 - 35. / 48.) + 1.3125) - 35. / 96.) - 0.7109375) - 7.0 / 768.0;
} else if (x < 2.5) {
x = x * (x * (x * (x * (x * (-1. / 20.) + 7. / 12.) - 2.625) + 133. / 24.) - 5.140625) + 1267. / 960.;
} else {
x *= 2.;
x -= 7.;
scalar_t x2 = x * x;
x = (x2 * x2 * x) / 3840.;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds6(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - 2.5));
upp = low + 6;
}
// --- order 7 -------------------------------------------------------
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight7(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
scalar_t f = x * x;
return f * (f * (f * (x * (1. / 144.) - 1. / 36.) + 1. / 9.) - 1. / 3.) + 151. / 315.0;
} else if (x < 2.) {
return x * (x * (x * (x * (x * (x * (0.05 - x * (1. / 240.)) - 7. / 30.) + 0.5) - 7. / 18.) - 0.1) - 7. / 90.) +
103. / 210.0;
} else if (x < 3.) {
return x *
(x * (x * (x * (x * (x * (x * (1. / 720.) - 1. / 36.) + 7. / 30.) - 19. / 18.) + 49. / 18.) - 23. / 6.) +
217. / 90.) -
139. / 630.0;
} else if (x < 4.) {
scalar_t f = 4. - x;
x = f * f * f;
return (x * x * f) / 5040.;
} else
return static_cast<scalar_t>(0);
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight7(scalar_t x) {
x = std::fabs(x);
if (x < 1.) {
scalar_t f = x * x;
return f * (f * (f * (x * (1. / 144.) - 1. / 36.) + 1. / 9.) - 1. / 3.) + 151. / 315.0;
} else if (x < 2.) {
return x * (x * (x * (x * (x * (x * (0.05 - x * (1. / 240.)) - 7. / 30.) + 0.5) - 7. / 18.) - 0.1) - 7. / 90.) +
103. / 210.0;
} else if (x < 3.) {
return x *
(x * (x * (x * (x * (x * (x * (1. / 720.) - 1. / 36.) + 7. / 30.) - 19. / 18.) + 49. / 18.) - 23. / 6.) +
217. / 90.) -
139. / 630.0;
} else {
scalar_t f = 4. - x;
x = f * f * f;
return (x * x * f) / 5040.;
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad7(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
scalar_t x2 = x * x;
x = x * (x2 * (x2 * (x * (7. / 144.) - 1. / 6.) + 4. / 9.) - 2. / 3.);
} else if (x < 2.) {
x = x * (x * (x * (x * (x * (x * (-7. / 240.) + 3. / 10.) - 7. / 6.) + 2.) - 7. / 6.) - 1. / 5.) - 7. / 90.;
} else if (x < 3.) {
x = x * (x * (x * (x * (x * (x * (7. / 720.) - 1. / 6.) + 7. / 6.) - 38. / 9.) + 49. / 6.) - 23. / 3.) + 217. / 90.;
} else if (x < 4.) {
x -= 4;
x *= x * x;
x *= x;
x = -x / 720.;
} else {
return static_cast<scalar_t>(0);
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad7(scalar_t x) {
bool neg = x < 0;
if (neg)
x = -x;
if (x < 1.) {
scalar_t x2 = x * x;
x = x * (x2 * (x2 * (x * (7. / 144.) - 1. / 6.) + 4. / 9.) - 2. / 3.);
} else if (x < 2.) {
x = x * (x * (x * (x * (x * (x * (-7. / 240.) + 3. / 10.) - 7. / 6.) + 2.) - 7. / 6.) - 1. / 5.) - 7. / 90.;
} else if (x < 3.) {
x = x * (x * (x * (x * (x * (x * (7. / 720.) - 1. / 6.) + 7. / 6.) - 38. / 9.) + 49. / 6.) - 23. / 3.) + 217. / 90.;
} else {
x -= 4;
x *= x * x;
x *= x;
x = -x / 720.;
}
if (neg)
x = -x;
return x;
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds7(scalar_t x, offset_t& low, offset_t& upp) {
low = static_cast<offset_t>(std::floor(x - 3.));
upp = low + 7;
}
} // namespace _interpolation
namespace interpolation {
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t weight(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::weight0(x);
case InterpolationType::Linear:
return _interpolation::weight1(x);
case InterpolationType::Quadratic:
return _interpolation::weight2(x);
case InterpolationType::Cubic:
return _interpolation::weight3(x);
case InterpolationType::FourthOrder:
return _interpolation::weight4(x);
case InterpolationType::FifthOrder:
return _interpolation::weight5(x);
case InterpolationType::SixthOrder:
return _interpolation::weight6(x);
case InterpolationType::SeventhOrder:
return _interpolation::weight7(x);
default:
return _interpolation::weight1(x);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastweight(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::fastweight0(x);
case InterpolationType::Linear:
return _interpolation::fastweight1(x);
case InterpolationType::Quadratic:
return _interpolation::fastweight2(x);
case InterpolationType::Cubic:
return _interpolation::fastweight3(x);
case InterpolationType::FourthOrder:
return _interpolation::fastweight4(x);
case InterpolationType::FifthOrder:
return _interpolation::fastweight5(x);
case InterpolationType::SixthOrder:
return _interpolation::fastweight6(x);
case InterpolationType::SeventhOrder:
return _interpolation::fastweight7(x);
default:
return _interpolation::fastweight1(x);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t grad(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::grad0(x);
case InterpolationType::Linear:
return _interpolation::grad1(x);
case InterpolationType::Quadratic:
return _interpolation::grad2(x);
case InterpolationType::Cubic:
return _interpolation::grad3(x);
case InterpolationType::FourthOrder:
return _interpolation::grad4(x);
case InterpolationType::FifthOrder:
return _interpolation::grad5(x);
case InterpolationType::SixthOrder:
return _interpolation::grad6(x);
case InterpolationType::SeventhOrder:
return _interpolation::grad7(x);
default:
return _interpolation::grad1(x);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fastgrad(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::fastgrad0(x);
case InterpolationType::Linear:
return _interpolation::fastgrad1(x);
case InterpolationType::Quadratic:
return _interpolation::fastgrad2(x);
case InterpolationType::Cubic:
return _interpolation::fastgrad3(x);
case InterpolationType::FourthOrder:
return _interpolation::fastgrad4(x);
case InterpolationType::FifthOrder:
return _interpolation::fastgrad5(x);
case InterpolationType::SixthOrder:
return _interpolation::fastgrad6(x);
case InterpolationType::SeventhOrder:
return _interpolation::fastgrad7(x);
default:
return _interpolation::fastgrad1(x);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t hess(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::hess0(x);
case InterpolationType::Linear:
return _interpolation::hess1(x);
case InterpolationType::Quadratic:
return _interpolation::hess2(x);
case InterpolationType::Cubic:
return _interpolation::hess3(x);
case InterpolationType::FourthOrder:
return _interpolation::hess4(x);
case InterpolationType::FifthOrder:
return _interpolation::hess0(x); // notimplemented
case InterpolationType::SixthOrder:
return _interpolation::hess0(x); // notimplemented
case InterpolationType::SeventhOrder:
return _interpolation::hess0(x); // notimplemented
default:
return _interpolation::grad1(x);
}
}
template <typename scalar_t>
static MONAI_INLINE MONAI_DEVICE scalar_t fasthess(InterpolationType interpolation_type, scalar_t x) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::fasthess0(x);
case InterpolationType::Linear:
return _interpolation::fasthess1(x);
case InterpolationType::Quadratic:
return _interpolation::fasthess2(x);
case InterpolationType::Cubic:
return _interpolation::fasthess3(x);
case InterpolationType::FourthOrder:
return _interpolation::fasthess4(x);
case InterpolationType::FifthOrder:
return _interpolation::fasthess0(x); // notimplemented
case InterpolationType::SixthOrder:
return _interpolation::fasthess0(x); // notimplemented
case InterpolationType::SeventhOrder:
return _interpolation::fasthess0(x); // notimplemented
default:
return _interpolation::fasthess1(x);
}
}
template <typename scalar_t, typename offset_t>
static MONAI_INLINE MONAI_DEVICE void bounds(
InterpolationType interpolation_type,
scalar_t x,
offset_t& low,
offset_t& upp) {
switch (interpolation_type) {
case InterpolationType::Nearest:
return _interpolation::bounds0(x, low, upp);
case InterpolationType::Linear:
return _interpolation::bounds1(x, low, upp);
case InterpolationType::Quadratic:
return _interpolation::bounds2(x, low, upp);
case InterpolationType::Cubic:
return _interpolation::bounds3(x, low, upp);
case InterpolationType::FourthOrder:
return _interpolation::bounds4(x, low, upp);
case InterpolationType::FifthOrder:
return _interpolation::bounds5(x, low, upp);
case InterpolationType::SixthOrder:
return _interpolation::bounds6(x, low, upp);
case InterpolationType::SeventhOrder:
return _interpolation::bounds7(x, low, upp);
default:
return _interpolation::bounds1(x, low, upp);
}
}
} // namespace interpolation
} // namespace monai
|