<commit_before>#include "antsUtilities.h"
#include <algorithm>

#include "itkBSplineScatteredDataPointSetToImageFilter.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkPointSet.h"
#include "itkTimeProbe.h"
#include "itkVector.h"
#include "itkVectorIndexSelectionCastImageFilter.h"

#include "ReadWriteData.h"

namespace ants
{
template <unsigned int ImageDimension>
int SuperResolution( unsigned int argc, char *argv[] )
{

  typedef float                                   RealType;
  typedef itk::Image<RealType, ImageDimension>    ImageType;

  typedef itk::Vector<RealType, 1>                         ScalarType;
  typedef itk::Image<ScalarType, ImageDimension>           ScalarImageType;
  typedef itk::PointSet<ScalarType, ImageDimension>        PointSetType;
  typedef itk::BSplineScatteredDataPointSetToImageFilter
    <PointSetType, ScalarImageType>                        BSplineFilterType;

  typename ImageType::Pointer domainImage = ITK_NULLPTR;
  ReadImage<ImageType>( domainImage, argv[3] );

  typename BSplineFilterType::Pointer bspliner = BSplineFilterType::New();

  typename PointSetType::Pointer bsplinePoints = PointSetType::New();
  bsplinePoints->Initialize();

  typename BSplineFilterType::WeightsContainerType::Pointer weights =
    BSplineFilterType::WeightsContainerType::New();
  weights->Initialize();

  unsigned int splineOrder = 3;
  typename BSplineFilterType::ArrayType numberOfLevels;
  typename BSplineFilterType::ArrayType ncps;

  std::vector<unsigned int> nlevels = ConvertVector<unsigned int>( std::string( argv[5] ) );
  if ( nlevels.size() == 1 )
    {
    numberOfLevels.Fill( nlevels[0] );
    }
  else if ( nlevels.size() == ImageDimension )
    {
    for ( unsigned int d = 0; d < ImageDimension; d++ )
      {
      numberOfLevels[d] = nlevels[d];
      }
    }
  else
    {
    std::cerr << "Invalid nlevels format." << std::endl;
    return EXIT_FAILURE;
    }

  std::vector<unsigned int> meshSize = ConvertVector<unsigned int>( std::string( argv[4] ) );
  if ( meshSize.size() == 1 )
    {
    ncps.Fill( meshSize[0] + splineOrder );
    }
  else if ( meshSize.size() == ImageDimension )
    {
    for ( unsigned int d = 0; d < ImageDimension; d++ )
      {
      ncps[d] = meshSize[d] + splineOrder;
      }
    }
  else
    {
    std::cerr << "Invalid ncps format." << std::endl;
    return EXIT_FAILURE;
    }

  unsigned int N = 0;
  for( unsigned int n = 6; n < argc; n++ )
    {
    typename ImageType::Pointer inputImage = ITK_NULLPTR;
    ReadImage<ImageType>( inputImage, argv[n] );

    itk::ImageRegionConstIteratorWithIndex<ImageType> It( inputImage, inputImage->GetRequestedRegion() );
    for( It.GoToBegin(); !It.IsAtEnd(); ++It )
      {
      typename ImageType::PointType imagePoint;
      inputImage->TransformIndexToPhysicalPoint( It.GetIndex(), imagePoint );

      typename ImageType::IndexType index;
      bool isInside = domainImage->TransformPhysicalPointToIndex( imagePoint, index );

      if( !isInside )
        {
        continue;
        }

      ScalarType scalar;
      scalar[0] = It.Get();

      bsplinePoints->SetPointData( N, scalar );
      bsplinePoints->SetPoint( N, imagePoint );
      weights->InsertElement( N, 1 );

      N++;
      }
    }

  itk::TimeProbe timer;
  timer.Start();

  bspliner->SetOrigin( domainImage->GetOrigin() );
  bspliner->SetSpacing( domainImage->GetSpacing() );
  bspliner->SetSize( domainImage->GetRequestedRegion().GetSize() );
  bspliner->SetGenerateOutputImage( true );
  bspliner->SetNumberOfLevels( numberOfLevels );
  bspliner->SetSplineOrder( splineOrder );
  bspliner->SetNumberOfControlPoints( ncps );
  bspliner->SetInput( bsplinePoints );
  bspliner->SetPointWeights( weights );
  bspliner->Update();

  timer.Stop();

  std::cout << "Elapsed Time:  " << timer.GetMean() << std::endl;

  typedef itk::VectorIndexSelectionCastImageFilter<ScalarImageType, ImageType> SelectorType;
  typename SelectorType::Pointer selector = SelectorType::New();
  selector->SetInput( bspliner->GetOutput() );
  selector->SetIndex( 0 );
  selector->Update();
<commit_msg>BUG:  Didn't get phyiscal domain correct.<commit_after>#include "antsUtilities.h"
#include <algorithm>

#include "itkBSplineScatteredDataPointSetToImageFilter.h"
#include "itkContinuousIndex.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkImportImageFilter.h"
#include "itkPointSet.h"
#include "itkTimeProbe.h"
#include "itkVector.h"
#include "itkVectorIndexSelectionCastImageFilter.h"

#include "ReadWriteData.h"

namespace ants
{
template <unsigned int ImageDimension>
int SuperResolution( unsigned int argc, char *argv[] )
{

  typedef float                                   RealType;
  typedef itk::Image<RealType, ImageDimension>    ImageType;

  typedef itk::Vector<RealType, 1>                         ScalarType;
  typedef itk::Image<ScalarType, ImageDimension>           ScalarImageType;
  typedef itk::PointSet<ScalarType, ImageDimension>        PointSetType;
  typedef itk::BSplineScatteredDataPointSetToImageFilter
    <PointSetType, ScalarImageType>                        BSplineFilterType;

  typename ImageType::Pointer domainImage = ITK_NULLPTR;
  ReadImage<ImageType>( domainImage, argv[3] );

  typename BSplineFilterType::Pointer bspliner = BSplineFilterType::New();

  typename PointSetType::Pointer bsplinePoints = PointSetType::New();
  bsplinePoints->Initialize();

  typename BSplineFilterType::WeightsContainerType::Pointer weights =
    BSplineFilterType::WeightsContainerType::New();
  weights->Initialize();

  unsigned int splineOrder = 3;
  typename BSplineFilterType::ArrayType numberOfLevels;
  typename BSplineFilterType::ArrayType ncps;

  std::vector<unsigned int> nlevels = ConvertVector<unsigned int>( std::string( argv[5] ) );
  if ( nlevels.size() == 1 )
    {
    numberOfLevels.Fill( nlevels[0] );
    }
  else if ( nlevels.size() == ImageDimension )
    {
    for ( unsigned int d = 0; d < ImageDimension; d++ )
      {
      numberOfLevels[d] = nlevels[d];
      }
    }
  else
    {
    std::cerr << "Invalid nlevels format." << std::endl;
    return EXIT_FAILURE;
    }

  std::vector<unsigned int> meshSize = ConvertVector<unsigned int>( std::string( argv[4] ) );
  if ( meshSize.size() == 1 )
    {
    ncps.Fill( meshSize[0] + splineOrder );
    }
  else if ( meshSize.size() == ImageDimension )
    {
    for ( unsigned int d = 0; d < ImageDimension; d++ )
      {
      ncps[d] = meshSize[d] + splineOrder;
      }
    }
  else
    {
    std::cerr << "Invalid ncps format." << std::endl;
    return EXIT_FAILURE;
    }

  typename ScalarImageType::DirectionType identity;
  identity.SetIdentity();

  const typename ScalarImageType::RegionType & bufferedRegion = domainImage->GetBufferedRegion();
  const itk::SizeValueType numberOfPixels = bufferedRegion.GetNumberOfPixels();
  const bool filterHandlesMemory = false;

  typedef itk::ImportImageFilter<RealType, ImageDimension> ImporterType;
  typename ImporterType::Pointer importer = ImporterType::New();
  importer->SetImportPointer( domainImage->GetBufferPointer(), numberOfPixels, filterHandlesMemory );
  importer->SetRegion( domainImage->GetBufferedRegion() );
  importer->SetOrigin( domainImage->GetOrigin() );
  importer->SetSpacing( domainImage->GetSpacing() );
  importer->SetDirection( identity );
  importer->Update();

  typename ImageType::IndexType domainBeginIndex = domainImage->GetRequestedRegion().GetIndex();
  typename ImageType::IndexType domainEndIndex;
  for( unsigned int d = 0; d < ImageDimension; d++ )
    {
    domainEndIndex[d] = domainBeginIndex[d] + domainImage->GetRequestedRegion().GetSize()[d] - 1;
    }

  const typename ImporterType::OutputImageType * parametricDomainImage = importer->GetOutput();

  unsigned int N = 0;
  for( unsigned int n = 6; n < argc; n++ )
    {
    typename ImageType::Pointer inputImage = ITK_NULLPTR;
    ReadImage<ImageType>( inputImage, argv[n] );

    itk::ImageRegionConstIteratorWithIndex<ImageType> It( inputImage, inputImage->GetRequestedRegion() );
    for( It.GoToBegin(); !It.IsAtEnd(); ++It )
      {
      typename ImageType::PointType imagePoint;
      inputImage->TransformIndexToPhysicalPoint( It.GetIndex(), imagePoint );

      itk::ContinuousIndex<RealType, ImageDimension> cidx;
      bool isInside = domainImage->TransformPhysicalPointToContinuousIndex( imagePoint, cidx );

      if( isInside )
        {
        for( unsigned int d = 0; d < ImageDimension; d++ )
          {
          if( cidx[d] <= domainBeginIndex[d] || cidx[d] >= domainEndIndex[d] )
            {
            isInside = false;
            break;
            }
          }
        }

      if( !isInside )
        {
        continue;
        }

//       domainImage->SetPixel( index, 1 );

      parametricDomainImage->TransformContinuousIndexToPhysicalPoint( cidx, imagePoint );

      ScalarType scalar;
      scalar[0] = It.Get();

      bsplinePoints->SetPointData( N, scalar );
      bsplinePoints->SetPoint( N, imagePoint );
      weights->InsertElement( N, 1 );

      N++;
      }
    }

  std::cout << bsplinePoints->GetNumberOfPoints() << std::endl;

  itk::TimeProbe timer;
  timer.Start();

  typename ScalarImageType::PointType parametricOrigin = domainImage->GetOrigin();
  for( unsigned int d = 0; d < ImageDimension; d++ )
    {
    parametricOrigin[d] += (
        domainImage->GetSpacing()[d] *
        domainImage->GetLargestPossibleRegion().GetIndex()[d] );
    }

  bspliner->SetOrigin( parametricOrigin );
  bspliner->SetSpacing( domainImage->GetSpacing() );
  bspliner->SetSize( domainImage->GetRequestedRegion().GetSize() );
  bspliner->SetDirection( domainImage->GetDirection() );
  bspliner->SetGenerateOutputImage( true );
  bspliner->SetNumberOfLevels( numberOfLevels );
  bspliner->SetSplineOrder( splineOrder );
  bspliner->SetNumberOfControlPoints( ncps );
  bspliner->SetInput( bsplinePoints );
  bspliner->SetPointWeights( weights );
  bspliner->Update();

  timer.Stop();

  std::cout << "Elapsed Time:  " << timer.GetMean() << std::endl;

  typedef itk::VectorIndexSelectionCastImageFilter<ScalarImageType, ImageType> SelectorType;
  typename SelectorType::Pointer selector = SelectorType::New();
  selector->SetInput( bspliner->GetOutput() );
  selector->SetIndex( 0 );
  selector->Update();

{'fraction_non_alphanumeric': 0.12497576110141555, 'fraction_numerical': 0.0035873569904983516, 'mean_word_length': 3.795443979544398, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239343', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3170, 'n_words': 625}