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 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#ifndef _itkCombinationImageToImageMetric_hxx
#define _itkCombinationImageToImageMetric_hxx
#include "itkCombinationImageToImageMetric.h"
#include "itkTimeProbe.h"
#include "itkMath.h"
/** Macros to reduce some copy-paste work.
* These macros provide the implementation of
* all Set/GetFixedImage, Set/GetInterpolator etc methods
*
* The macros are undef'ed at the end of this file
*/
/** For setting objects, implement two methods */
#define itkImplementationSetObjectMacro2(_name, _type1, _type2) \
template <class TFixedImage, class TMovingImage> \
void CombinationImageToImageMetric<TFixedImage, TMovingImage>::Set##_name(_type1 _type2 * _arg, unsigned int pos) \
{ \
if (pos == 0) \
{ \
this->Superclass::Set##_name(_arg); \
} \
ImageMetricType * testPtr1 = dynamic_cast<ImageMetricType *>(this->GetMetric(pos)); \
PointSetMetricType * testPtr2 = dynamic_cast<PointSetMetricType *>(this->GetMetric(pos)); \
if (testPtr1) \
{ \
testPtr1->Set##_name(_arg); \
} \
else if (testPtr2) \
{ \
testPtr2->Set##_name(_arg); \
} \
} \
template <class TFixedImage, class TMovingImage> \
void CombinationImageToImageMetric<TFixedImage, TMovingImage>::Set##_name(_type1 _type2 * _arg) \
{ \
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i) \
{ \
this->Set##_name(_arg, i); \
} \
} // comments for allowing ; after calling the macro
#define itkImplementationSetObjectMacro1(_name, _type1, _type2) \
template <class TFixedImage, class TMovingImage> \
void CombinationImageToImageMetric<TFixedImage, TMovingImage>::Set##_name(_type1 _type2 * _arg, unsigned int pos) \
{ \
if (pos == 0) \
{ \
this->Superclass::Set##_name(_arg); \
} \
ImageMetricType * testPtr1 = dynamic_cast<ImageMetricType *>(this->GetMetric(pos)); \
if (testPtr1) \
{ \
testPtr1->Set##_name(_arg); \
} \
} \
template <class TFixedImage, class TMovingImage> \
void CombinationImageToImageMetric<TFixedImage, TMovingImage>::Set##_name(_type1 _type2 * _arg) \
{ \
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i) \
{ \
this->Set##_name(_arg, i); \
} \
} // comments for allowing ; after calling the macro
/** for getting const object, implement one method */
#define itkImplementationGetConstObjectMacro1(_name, _type) \
template <class TFixedImage, class TMovingImage> \
auto CombinationImageToImageMetric<TFixedImage, TMovingImage>::Get##_name(unsigned int pos) const->const _type * \
{ \
const ImageMetricType * testPtr1 = dynamic_cast<const ImageMetricType *>(this->GetMetric(pos)); \
if (testPtr1) \
{ \
return testPtr1->Get##_name(); \
} \
else \
{ \
return 0; \
} \
} // comments for allowing ; after calling the macro
#define itkImplementationGetConstObjectMacro2(_name, _type) \
template <class TFixedImage, class TMovingImage> \
auto CombinationImageToImageMetric<TFixedImage, TMovingImage>::Get##_name(unsigned int pos) const->const _type * \
{ \
const ImageMetricType * testPtr1 = dynamic_cast<const ImageMetricType *>(this->GetMetric(pos)); \
const PointSetMetricType * testPtr2 = dynamic_cast<const PointSetMetricType *>(this->GetMetric(pos)); \
if (testPtr1) \
{ \
return testPtr1->Get##_name(); \
} \
else if (testPtr2) \
{ \
return testPtr2->Get##_name(); \
} \
else \
{ \
return 0; \
} \
} // comments for allowing ; after calling the macro
namespace itk
{
itkImplementationSetObjectMacro2(Transform, , TransformType);
itkImplementationSetObjectMacro1(Interpolator, , InterpolatorType);
itkImplementationSetObjectMacro2(FixedImageMask, const, FixedImageMaskType);
itkImplementationSetObjectMacro2(MovingImageMask, const, MovingImageMaskType);
itkImplementationSetObjectMacro1(FixedImage, const, FixedImageType);
itkImplementationSetObjectMacro1(MovingImage, const, MovingImageType);
itkImplementationGetConstObjectMacro2(Transform, TransformType);
itkImplementationGetConstObjectMacro1(Interpolator, InterpolatorType);
itkImplementationGetConstObjectMacro2(FixedImageMask, FixedImageMaskType);
itkImplementationGetConstObjectMacro2(MovingImageMask, MovingImageMaskType);
itkImplementationGetConstObjectMacro1(FixedImage, FixedImageType);
itkImplementationGetConstObjectMacro1(MovingImage, MovingImageType);
/**
* ********************* Constructor ****************************
*/
template <class TFixedImage, class TMovingImage>
CombinationImageToImageMetric<TFixedImage, TMovingImage>::CombinationImageToImageMetric()
{
this->ComputeGradientOff();
} // end Constructor
/**
* ********************* PrintSelf ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::PrintSelf(std::ostream & os, Indent indent) const
{
/** Call the superclass' PrintSelf. */
Superclass::PrintSelf(os, indent);
/** Add debugging information. */
os << "NumberOfMetrics: " << this->m_NumberOfMetrics << std::endl;
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
os << "Metric " << i << ":\n";
os << indent << "MetricPointer: " << this->m_Metrics[i].GetPointer() << "\n";
os << indent << "MetricWeight: " << this->m_MetricWeights[i] << "\n";
os << indent << "MetricRelativeWeight: " << this->m_MetricRelativeWeights[i] << "\n";
os << indent << "UseRelativeWeights: " << (this->m_UseRelativeWeights ? "true\n" : "false\n");
os << indent << "MetricValue: " << this->m_MetricValues[i] << "\n";
os << indent << "MetricDerivativesMagnitude: " << this->m_MetricDerivativesMagnitude[i] << "\n";
os << indent << "UseMetric: " << (this->m_UseMetric[i] ? "true\n" : "false\n");
os << indent << "MetricComputationTime: " << this->m_MetricComputationTime[i] << "\n";
}
} // end PrintSelf()
/**
* ******************** SetFixedImageRegion ************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetFixedImageRegion(const FixedImageRegionType _arg,
unsigned int pos)
{
if (pos == 0)
{
this->Superclass::SetFixedImageRegion(_arg);
}
ImageMetricType * testPtr = dynamic_cast<ImageMetricType *>(this->GetMetric(pos));
if (testPtr)
{
testPtr->SetFixedImageRegion(_arg);
}
} // end SetFixedImageRegion()
/**
* ******************** SetFixedImageRegion ************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetFixedImageRegion(const FixedImageRegionType _arg)
{
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i)
{
this->SetFixedImageRegion(_arg, i);
}
} // end SetFixedImageRegion()
/**
* ******************** GetFixedImageRegion ************************
*/
template <class TFixedImage, class TMovingImage>
auto
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetFixedImageRegion(unsigned int pos) const
-> const FixedImageRegionType &
{
const ImageMetricType * testPtr = dynamic_cast<const ImageMetricType *>(this->GetMetric(pos));
if (testPtr)
{
return testPtr->GetFixedImageRegion();
}
else
{
return this->m_NullFixedImageRegion;
}
} // end GetFixedImageRegion()
/**
* ********************* SetNumberOfMetrics ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetNumberOfMetrics(unsigned int count)
{
if (count != this->m_Metrics.size())
{
this->m_NumberOfMetrics = count;
this->m_Metrics.resize(count);
this->m_MetricWeights.resize(count);
this->m_MetricRelativeWeights.resize(count);
this->m_UseMetric.resize(count);
this->m_MetricValues.resize(count);
this->m_MetricDerivatives.resize(count);
this->m_MetricDerivativesMagnitude.resize(count);
this->m_MetricComputationTime.resize(count);
this->Modified();
}
} // end SetNumberOfMetrics()
/**
* ********************* SetMetric ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetMetric(SingleValuedCostFunctionType * metric,
unsigned int pos)
{
if (pos >= this->GetNumberOfMetrics())
{
this->SetNumberOfMetrics(pos + 1);
}
if (metric != this->m_Metrics[pos])
{
this->m_Metrics[pos] = metric;
this->Modified();
}
} // end SetMetric()
/**
* ********************* GetMetric ****************************
*/
template <class TFixedImage, class TMovingImage>
auto
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetric(unsigned int pos) const
-> SingleValuedCostFunctionType *
{
if (pos >= this->GetNumberOfMetrics())
{
return nullptr;
}
else
{
return this->m_Metrics[pos];
}
} // end GetMetric()
/**
* ********************* SetMetricWeight ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetMetricWeight(double weight, unsigned int pos)
{
if (pos >= this->GetNumberOfMetrics())
{
this->SetNumberOfMetrics(pos + 1);
}
if (weight != this->m_MetricWeights[pos])
{
this->m_MetricWeights[pos] = weight;
this->Modified();
}
} // end SetMetricWeight()
/**
* ********************* GetMetricWeight ****************************
*/
template <class TFixedImage, class TMovingImage>
double
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricWeight(unsigned int pos) const
{
if (pos >= this->GetNumberOfMetrics())
{
return 0.0;
}
else
{
return this->m_MetricWeights[pos];
}
} // end GetMetricWeight()
/**
* ********************* SetMetricRelativeWeight ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetMetricRelativeWeight(double weight, unsigned int pos)
{
if (pos >= this->GetNumberOfMetrics())
{
this->SetNumberOfMetrics(pos + 1);
}
if (weight != this->m_MetricRelativeWeights[pos])
{
this->m_MetricRelativeWeights[pos] = weight;
this->Modified();
}
} // end SetMetricRelativeWeight()
/**
* ********************* GetMetricRelativeWeight ****************************
*/
template <class TFixedImage, class TMovingImage>
double
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricRelativeWeight(unsigned int pos) const
{
if (pos >= this->GetNumberOfMetrics())
{
return 0.0;
}
else
{
return this->m_MetricRelativeWeights[pos];
}
} // end GetMetricRelativeWeight()
/**
* ********************* SetUseMetric ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetUseMetric(const bool use, const unsigned int pos)
{
if (pos >= this->GetNumberOfMetrics())
{
this->SetNumberOfMetrics(pos + 1);
}
if (use != this->m_UseMetric[pos])
{
this->m_UseMetric[pos] = use;
this->Modified();
}
} // end SetUseMetric()
/**
* ********************* SetUseAllMetrics ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetUseAllMetrics()
{
for (unsigned int pos = 0; pos < this->GetNumberOfMetrics(); ++pos)
{
if (!this->m_UseMetric[pos])
{
this->m_UseMetric[pos] = true;
this->Modified();
}
}
} // end SetUseAllMetrics()
/**
* ********************* GetUseMetric ****************************
*/
template <class TFixedImage, class TMovingImage>
bool
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetUseMetric(unsigned int pos) const
{
if (pos >= this->GetNumberOfMetrics())
{
return false;
}
else
{
return this->m_UseMetric[pos];
}
} // end GetUseMetric()
/**
* ********************* GetMetricValue ****************************
*/
template <class TFixedImage, class TMovingImage>
auto
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricValue(unsigned int pos) const -> MeasureType
{
if (pos >= this->GetNumberOfMetrics())
{
return 0.0;
}
else
{
return this->m_MetricValues[pos];
}
} // end GetMetricValue()
/**
* ********************* GetMetricDerivative ****************************
*/
template <class TFixedImage, class TMovingImage>
auto
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricDerivative(unsigned int pos) const
-> const DerivativeType &
{
if (pos >= this->GetNumberOfMetrics())
{
return this->m_NullDerivative;
}
else
{
return this->m_MetricDerivatives[pos];
}
} // end GetMetricDerivative()
/**
* ********************* GetMetricDerivativeMagnitude ****************************
*/
template <class TFixedImage, class TMovingImage>
double
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricDerivativeMagnitude(unsigned int pos) const
{
if (pos >= this->GetNumberOfMetrics())
{
return 0.0;
}
else
{
return this->m_MetricDerivativesMagnitude[pos];
}
} // end GetMetricDerivativeMagnitude()
/**
* ********************* GetMetricComputationTime ****************************
*/
template <class TFixedImage, class TMovingImage>
double
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMetricComputationTime(unsigned int pos) const
{
if (pos >= this->GetNumberOfMetrics())
{
return 0;
}
else
{
return this->m_MetricComputationTime[pos];
}
} // end GetMetricComputationTime()
/**
* **************** GetNumberOfPixelsCounted ************************
*/
template <class TFixedImage, class TMovingImage>
const SizeValueType &
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetNumberOfPixelsCounted() const
{
unsigned long sum = 0;
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i)
{
const ImageMetricType * testPtr = dynamic_cast<const ImageMetricType *>(this->GetMetric(i));
if (testPtr)
{
sum += testPtr->GetNumberOfPixelsCounted();
}
}
Superclass::m_NumberOfPixelsCounted = sum;
return Superclass::m_NumberOfPixelsCounted;
} // end GetNumberOfPixelsCounted()
/**
* ********************* Initialize ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::Initialize()
{
/** Check if transform, interpolator have been set. Effectively this
* method checks if the first sub metric is set up completely.
* This implicitly means that the first sub metric is an ImageToImageMetric,
* which is a reasonable demand.
*/
this->Superclass::Initialize();
/** Check if at least one (image)metric is provided */
if (this->GetNumberOfMetrics() == 0)
{
itkExceptionMacro("At least one metric should be set!");
}
/** Call Initialize for all metrics. */
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i)
{
SingleValuedCostFunctionType * costfunc = this->GetMetric(i);
if (!costfunc)
{
itkExceptionMacro("Metric " << i << " has not been set!");
}
ImageMetricType * testPtr1 = dynamic_cast<ImageMetricType *>(this->GetMetric(i));
PointSetMetricType * testPtr2 = dynamic_cast<PointSetMetricType *>(this->GetMetric(i));
if (testPtr1)
{
// The NumberOfThreadsPerMetric is changed after Initialize() so we save it before and then
// set it on.
unsigned nrOfThreadsPerMetric = this->GetNumberOfWorkUnits();
testPtr1->Initialize();
testPtr1->SetNumberOfWorkUnits(nrOfThreadsPerMetric);
}
else if (testPtr2)
{
testPtr2->Initialize();
}
}
} // end Initialize()
/**
* ******************* InitializeThreadingParameters *******************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::InitializeThreadingParameters() const
{
/** Initialize the derivatives. */
for (ThreadIdType i = 0; i < this->GetNumberOfMetrics(); ++i)
{
this->m_MetricDerivatives[i].SetSize(this->GetNumberOfParameters());
}
} // end InitializeThreadingParameters()
/**
* ******************* GetFinalMetricWeight *******************
*/
template <class TFixedImage, class TMovingImage>
double
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetFinalMetricWeight(unsigned int pos) const
{
double weight = 1.0;
if (!this->m_UseRelativeWeights)
{
weight = this->m_MetricWeights[pos];
}
else
{
/** The relative weight of metric i is such that the
* magnitude of the derivative of metric i is rescaled
* to be a fraction of that of metric 0; the fraction is
* defined by the fraction of the two relative weights.
* Note that this weight is different in each iteration.
*/
if (this->m_MetricDerivativesMagnitude[pos] > 1e-10)
{
weight = this->m_MetricRelativeWeights[pos] * this->m_MetricDerivativesMagnitude[0] /
this->m_MetricDerivativesMagnitude[pos];
}
}
return weight;
} // end GetFinalMetricWeight()
/**
* ********************* GetValue ****************************
*/
template <class TFixedImage, class TMovingImage>
auto
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetValue(const ParametersType & parameters) const
-> MeasureType
{
/** Initialise. */
MeasureType measure{};
/** Compute, store and combine all metric values. */
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
/** Time the computation per metric. */
itk::TimeProbe timer;
timer.Start();
/** Compute ... */
MeasureType tmpValue = this->m_Metrics[i]->GetValue(parameters);
timer.Stop();
/** store ... */
this->m_MetricValues[i] = tmpValue;
this->m_MetricComputationTime[i] = timer.GetMean() * 1000.0;
/** and combine. */
if (this->m_UseMetric[i])
{
if (!this->m_UseRelativeWeights)
{
measure += this->m_MetricWeights[i] * this->m_MetricValues[i];
}
else
{
/** The relative weight of metric i is such that the
* value of metric i is rescaled
* to be a fraction of that of metric 0; the fraction is
* defined by the fraction of the two relative weights.
* Note that this weight is different in each iteration.
*/
double weight = 1.0;
if (this->m_MetricValues[i] > 1e-10)
{
weight = this->m_MetricRelativeWeights[i] * this->m_MetricValues[0] / this->m_MetricValues[i];
measure += weight * this->m_MetricValues[i];
}
}
}
}
/** Return a value. */
return measure;
} // end GetValue()
/**
* ********************* GetDerivative ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetDerivative(const ParametersType & parameters,
DerivativeType & derivative) const
{
/** Initialise. */
DerivativeType tmpDerivative(this->GetNumberOfParameters());
derivative.set_size(this->GetNumberOfParameters());
derivative.Fill(MeasureType{});
/** Compute, store and combine all metric derivatives. */
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
/** Time the computation per metric. */
itk::TimeProbe timer;
timer.Start();
/** Compute ... */
tmpDerivative.Fill(MeasureType{});
this->m_Metrics[i]->GetDerivative(parameters, tmpDerivative);
timer.Stop();
/** store ... */
this->m_MetricDerivatives[i] = tmpDerivative;
this->m_MetricDerivativesMagnitude[i] = tmpDerivative.magnitude();
this->m_MetricComputationTime[i] = timer.GetMean() * 1000.0;
/** and combine. */
if (this->m_UseMetric[i])
{
if (!this->m_UseRelativeWeights)
{
derivative += this->m_MetricWeights[i] * this->m_MetricDerivatives[i];
}
else
{
/** The relative weight of metric i is such that the
* magnitude of the derivative of metric i is rescaled
* to be a fraction of that of metric 0; the fraction is
* defined by the fraction of the two relative weights.
* Note that this weight is different in each iteration.
*/
double weight = 1.0;
if (this->m_MetricDerivativesMagnitude[i] > 1e-10)
{
weight = this->m_MetricRelativeWeights[i] * this->m_MetricDerivativesMagnitude[0] /
this->m_MetricDerivativesMagnitude[i];
derivative += weight * this->m_MetricDerivatives[i];
}
}
}
}
} // end GetDerivative()
/**
* ********************* GetValueAndDerivative ****************************
*/
template <class TFixedImage, class TMovingImage>
void
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetValueAndDerivative(const ParametersType & parameters,
MeasureType & value,
DerivativeType & derivative) const
{
/** Declare timer. */
itk::TimeProbe timer;
/** This function must be called before the multi-threaded code.
* It calls all the non thread-safe stuff.
*/
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
ImageMetricType * testPtr1 = dynamic_cast<ImageMetricType *>(this->GetMetric(i));
PointSetMetricType * testPtr2 = dynamic_cast<PointSetMetricType *>(this->GetMetric(i));
if (testPtr1)
{
testPtr1->SetUseMetricSingleThreaded(true);
testPtr1->BeforeThreadedGetValueAndDerivative(parameters);
testPtr1->SetUseMetricSingleThreaded(false);
}
if (testPtr2)
{
testPtr2->SetUseMetricSingleThreaded(true);
testPtr2->BeforeThreadedGetValueAndDerivative(parameters);
testPtr2->SetUseMetricSingleThreaded(false);
}
}
/** Initialize some threading related parameters. */
this->InitializeThreadingParameters();
/** Compute all metric values and derivatives. */
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
/** Compute ... */
timer.Reset();
timer.Start();
this->m_Metrics[i]->GetValueAndDerivative(parameters, this->m_MetricValues[i], this->m_MetricDerivatives[i]);
timer.Stop();
/** Store computation time. */
this->m_MetricComputationTime[i] = timer.GetMean() * 1000.0;
}
/** Compute the derivative magnitude. */
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
this->m_MetricDerivativesMagnitude[i] = this->m_MetricDerivatives[i].magnitude();
}
/** Combine the metric values. */
value = MeasureType{};
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
if (this->m_UseMetric[i])
{
const double weight = this->GetFinalMetricWeight(i);
value += weight * this->m_MetricValues[i];
}
}
/** Combine the metric derivatives. First, the first derivative. */
if (this->m_UseMetric[0])
{
const double weight = this->GetFinalMetricWeight(0);
derivative = weight * this->m_MetricDerivatives[0];
}
else
{
derivative.Fill(0);
}
/** Then the remaining derivatives. */
for (unsigned int i = 1; i < this->m_NumberOfMetrics; ++i)
{
if (this->m_UseMetric[i])
{
const double weight = this->GetFinalMetricWeight(i);
derivative += weight * this->m_MetricDerivatives[i];
}
}
} // end GetValueAndDerivative()
/**
* ********************* GetMTime ****************************
*/
template <class TFixedImage, class TMovingImage>
ModifiedTimeType
CombinationImageToImageMetric<TFixedImage, TMovingImage>::GetMTime() const
{
ModifiedTimeType mtime = this->Superclass::GetMTime();
ModifiedTimeType m;
// Some of the following should be removed once this 'ivars' are put in the
// input and output lists
/** Check the modified time of the sub metrics */
for (unsigned int i = 0; i < this->GetNumberOfMetrics(); ++i)
{
SingleValuedCostFunctionPointer metric = this->GetMetric(i);
if (metric.IsNotNull())
{
m = metric->GetMTime();
mtime = (m > mtime ? m : mtime);
}
}
return mtime;
} // end GetMTime()
} // end namespace itk
#undef itkImplementationSetObjectMacro1
#undef itkImplementationSetObjectMacro2
#undef itkImplementationGetConstObjectMacro1
#undef itkImplementationGetConstObjectMacro2
#endif // end #ifndef _itkCombinationImageToImageMetric_hxx
|