<commit_before>/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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 itkBlockMatchingOptimizingInterpolationDisplacementCalculator_hxx
#define itkBlockMatchingOptimizingInterpolationDisplacementCalculator_hxx


#include "itkImageRegionConstIteratorWithIndex.h"

namespace itk
{
namespace BlockMatching
{

template <class TMetricImage, class TDisplacementImage, class TCoordRep>
OptimizingInterpolationDisplacementCalculator<TMetricImage, TDisplacementImage, TCoordRep>::
  OptimizingInterpolationDisplacementCalculator()
{
  m_CostFunction = OptimizingInterpolationCostFunction::New();

  // @todo sensible default optimizer and interpolator
}


template <class TMetricImage, class TDisplacementImage, class TCoordRep>
void
OptimizingInterpolationDisplacementCalculator<TMetricImage, TDisplacementImage, TCoordRep>::SetMetricImagePixel(
  const PointType & centerPoint,
  const IndexType & displacementIndex,
  MetricImageType * metricImage)
{
  Superclass::SetMetricImagePixel(centerPoint, displacementIndex, metricImage);

  // Find index of the maximum value.
  PixelType max = NumericTraits<PixelType>::min();
  IndexType maxIndex;
  maxIndex.Fill(0);

  const typename MetricImageType::RegionType              region = metricImage->GetBufferedRegion();
  itk::ImageRegionConstIteratorWithIndex<MetricImageType> it(metricImage, region);
  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
  {
    if (it.Get() > max)
    {
      max = it.Get();
      maxIndex = it.GetIndex();
    }
  }

  // If the maxIndex is on the edge of the image we don't try interpolation.
  bool                                     onEdge = false;
  IndexType                                metricIndex = region.GetIndex();
  const typename MetricImageType::SizeType metricSize = region.GetSize();
  for (unsigned int i = 0; i < ImageDimension; ++i)
  {
    if (maxIndex[i] == metricIndex[i] ||
        maxIndex[i] == metricIndex[i] + static_cast<typename IndexType::IndexValueType>(metricSize[i]))
    {
      onEdge = true;
      break;
    }
  }

  PointType maxPoint;
  if (onEdge)
  {
    metricImage->TransformIndexToPhysicalPoint(maxIndex, maxPoint);
  }
  else
  {
    typename OptimizingInterpolationCostFunction::ParametersType parameters(ImageDimension);
    for (unsigned int i = 0; i < ImageDimension; ++i)
    {
      parameters[i] = static_cast<typename OptimizingInterpolationCostFunction::ParametersValueType>(maxIndex[i]);
    }
    m_Optimizer->SetInitialPosition(parameters);
    // Is this the right offset?
    for (unsigned int i = 0; i < ImageDimension; i++)
      parameters[i] += 0.1;
    m_CostFunction->Initialize(parameters);
    m_CostFunction->GetInterpolator()->SetInputImage(metricImage);
    m_Optimizer->StartOptimization();

    parameters = m_Optimizer->GetCurrentPosition();

    ContinuousIndexType continuousIndex;<commit_msg>COMP: Fix -fpermissive error in OptimizingInterpolationDisplacementCalculator<commit_after>/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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 itkBlockMatchingOptimizingInterpolationDisplacementCalculator_hxx
#define itkBlockMatchingOptimizingInterpolationDisplacementCalculator_hxx


#include "itkImageRegionConstIteratorWithIndex.h"

namespace itk
{
namespace BlockMatching
{

template <class TMetricImage, class TDisplacementImage, class TCoordRep>
OptimizingInterpolationDisplacementCalculator<TMetricImage, TDisplacementImage, TCoordRep>::
  OptimizingInterpolationDisplacementCalculator()
{
  m_CostFunction = OptimizingInterpolationCostFunction::New();

  // @todo sensible default optimizer and interpolator
}


template <class TMetricImage, class TDisplacementImage, class TCoordRep>
void
OptimizingInterpolationDisplacementCalculator<TMetricImage, TDisplacementImage, TCoordRep>::SetMetricImagePixel(
  const PointType & centerPoint,
  const IndexType & displacementIndex,
  MetricImageType * metricImage)
{
  Superclass::SetMetricImagePixel(centerPoint, displacementIndex, metricImage);

  // Find index of the maximum value.
  PixelType max = NumericTraits<PixelType>::min();
  IndexType maxIndex;
  maxIndex.Fill(0);

  const typename MetricImageType::RegionType              region = metricImage->GetBufferedRegion();
  itk::ImageRegionConstIteratorWithIndex<MetricImageType> it(metricImage, region);
  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
  {
    if (it.Get() > max)
    {
      max = it.Get();
      maxIndex = it.GetIndex();
    }
  }

  // If the maxIndex is on the edge of the image we don't try interpolation.
  bool                                     onEdge = false;
  IndexType                                metricIndex = region.GetIndex();
  const typename MetricImageType::SizeType metricSize = region.GetSize();
  for (unsigned int i = 0; i < ImageDimension; ++i)
  {
    if (maxIndex[i] == metricIndex[i] ||
        maxIndex[i] == metricIndex[i] + static_cast<typename IndexType::IndexValueType>(metricSize[i]))
    {
      onEdge = true;
      break;
    }
  }

  PointType maxPoint;
  if (onEdge)
  {
    metricImage->TransformIndexToPhysicalPoint(maxIndex, maxPoint);
  }
  else
  {
    typename OptimizingInterpolationCostFunction::ParametersType parameters(ImageDimension);
    for (unsigned int i = 0; i < ImageDimension; ++i)
    {
      parameters[i] = static_cast<typename OptimizingInterpolationCostFunction::ParametersValueType>(maxIndex[i]);
    }
    m_Optimizer->SetInitialPosition(parameters);
    // Is this the right offset?
    for (unsigned int i = 0; i < ImageDimension; i++)
      parameters[i] += 0.1;
    m_CostFunction->Initialize(parameters);
    const_cast<InterpolatorType*>(m_CostFunction->GetInterpolator())->SetInputImage(metricImage);
    m_Optimizer->StartOptimization();

    parameters = m_Optimizer->GetCurrentPosition();

    ContinuousIndexType continuousIndex;
{'fraction_non_alphanumeric': 0.1288752954261087, 'fraction_numerical': 0.0027804810232170164, 'mean_word_length': 4.421250941974378, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6543010', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1966, 'n_words': 504}