<commit_before>/*=========================================================================
 *
 *  Copyright Insight Software 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.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 itkImageMaskSpatialObject_hxx
#define itkImageMaskSpatialObject_hxx

#include "itkMath.h"
#include "itkImageMaskSpatialObject.h"
#include "itkImageRegionRange.h"

#include <cstdint> // For uintmax_t.

namespace itk
{
/** Constructor */
template <unsigned int TDimension, typename TPixel>
ImageMaskSpatialObject<TDimension, TPixel>::ImageMaskSpatialObject()
{
  this->SetTypeName("ImageMaskSpatialObject");
}

/** Test whether a point is inside or outside the object
 *  For computational speed purposes, it is faster if the method does not
 *  check the name of the class and the current depth */
template <unsigned int TDimension, typename TPixel>
bool
ImageMaskSpatialObject<TDimension, TPixel>::IsInsideInObjectSpace(const PointType & point) const
{
  typename Superclass::InterpolatorType::ContinuousIndexType index;
  if (this->GetImage()->TransformPhysicalPointToContinuousIndex(point, index))
  {
    using InterpolatorOutputType = typename InterpolatorType::OutputType;
    bool insideMask = (Math::NotExactlyEquals(DefaultConvertPixelTraits<InterpolatorOutputType>::GetScalarValue(
                                                this->GetInterpolator()->EvaluateAtContinuousIndex(index)),
                                              NumericTraits<PixelType>::ZeroValue()));
    if (insideMask)
    {
      return true;
    }
  }

  return false;
}


template <unsigned int TDimension, typename TPixel>
void
ImageMaskSpatialObject<TDimension, TPixel>::ComputeMyBoundingBox()
{
  const ImageType * const image = this->GetImage();
  itkAssertOrThrowMacro(image != nullptr, "Ensure that SetImage has been called!");

  const RegionType boundingBoxInIndexSpace{ this->ComputeMyBoundingBoxInIndexSpace() };

  BoundingBoxType * const boundingBoxInObjectSpace = this->GetModifiableMyBoundingBoxInObjectSpace();

  // Assert should never fail as SpatialObject takes care of creating the BoundingBox.
  assert(boundingBoxInObjectSpace != nullptr);

  if (boundingBoxInIndexSpace.GetNumberOfPixels() == 0)
  {
    boundingBoxInObjectSpace->SetMinimum(PointType());
    boundingBoxInObjectSpace->SetMaximum(PointType());
  }
  else
  {
    using ContinuousIndexType = typename Superclass::ContinuousIndexType;
    using VectorType = typename SpatialObject<TDimension>::VectorType;

    const IndexType minIndex = boundingBoxInIndexSpace.GetIndex();

    ContinuousIndexType minContinuousIndex{ minIndex };
    ContinuousIndexType maxContinuousIndex{ minIndex + boundingBoxInIndexSpace.GetSize() };

    // Allow a margin of half a pixel in each direction.
    minContinuousIndex -= VectorType{ 0.5 };
    maxContinuousIndex -= VectorType{ 0.5 };

    // Initially set the corner point corresponding to the minimum index as
    // both the minimum and maximum of the bounding box (in object space).
    // Afterwards, all other corners are considered.
    PointType firstPoint;
    image->TransformContinuousIndexToPhysicalPoint(minContinuousIndex, firstPoint);
    boundingBoxInObjectSpace->SetMinimum(firstPoint);
    boundingBoxInObjectSpace->SetMaximum(firstPoint);

    // The total number of corner points of the bounding box.
    constexpr auto numberOfCorners = std::uintmax_t{ 1 } << TDimension;

    for (std::uintmax_t cornerNumber{ 1 }; cornerNumber < numberOfCorners; ++cornerNumber)
    {
      // For each corner, estimate the n-dimensional index.<commit_msg>ENH: Shadowed typedef warnings removed<commit_after>/*=========================================================================
 *
 *  Copyright Insight Software 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.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 itkImageMaskSpatialObject_hxx
#define itkImageMaskSpatialObject_hxx

#include "itkMath.h"
#include "itkImageMaskSpatialObject.h"
#include "itkImageRegionRange.h"

#include <cstdint> // For uintmax_t.

namespace itk
{
/** Constructor */
template <unsigned int TDimension, typename TPixel>
ImageMaskSpatialObject<TDimension, TPixel>::ImageMaskSpatialObject()
{
  this->SetTypeName("ImageMaskSpatialObject");
}

/** Test whether a point is inside or outside the object
 *  For computational speed purposes, it is faster if the method does not
 *  check the name of the class and the current depth */
template <unsigned int TDimension, typename TPixel>
bool
ImageMaskSpatialObject<TDimension, TPixel>::IsInsideInObjectSpace(const PointType & point) const
{
  typename Superclass::InterpolatorType::ContinuousIndexType index;
  if (this->GetImage()->TransformPhysicalPointToContinuousIndex(point, index))
  {
    using InterpolatorOutputType = typename InterpolatorType::OutputType;
    bool insideMask = (Math::NotExactlyEquals(DefaultConvertPixelTraits<InterpolatorOutputType>::GetScalarValue(
                                                this->GetInterpolator()->EvaluateAtContinuousIndex(index)),
                                              NumericTraits<PixelType>::ZeroValue()));
    if (insideMask)
    {
      return true;
    }
  }

  return false;
}


template <unsigned int TDimension, typename TPixel>
void
ImageMaskSpatialObject<TDimension, TPixel>::ComputeMyBoundingBox()
{
  const ImageType * const image = this->GetImage();
  itkAssertOrThrowMacro(image != nullptr, "Ensure that SetImage has been called!");

  const RegionType boundingBoxInIndexSpace{ this->ComputeMyBoundingBoxInIndexSpace() };

  BoundingBoxType * const boundingBoxInObjectSpace = this->GetModifiableMyBoundingBoxInObjectSpace();

  // Assert should never fail as SpatialObject takes care of creating the BoundingBox.
  assert(boundingBoxInObjectSpace != nullptr);

  if (boundingBoxInIndexSpace.GetNumberOfPixels() == 0)
  {
    boundingBoxInObjectSpace->SetMinimum(PointType());
    boundingBoxInObjectSpace->SetMaximum(PointType());
  }
  else
  {
    const IndexType minIndex = boundingBoxInIndexSpace.GetIndex();

    typename Superclass::ContinuousIndexType minContinuousIndex{ minIndex };
    typename Superclass::ContinuousIndexType maxContinuousIndex{ minIndex + boundingBoxInIndexSpace.GetSize() };

    // Allow a margin of half a pixel in each direction.
    const typename SpatialObject<TDimension>::VectorType half_pixel_size{ 0.5 };
    minContinuousIndex -= half_pixel_size;
    maxContinuousIndex -= half_pixel_size;

    // Initially set the corner point corresponding to the minimum index as
    // both the minimum and maximum of the bounding box (in object space).
    // Afterwards, all other corners are considered.
    PointType firstPoint;
    image->TransformContinuousIndexToPhysicalPoint(minContinuousIndex, firstPoint);
    boundingBoxInObjectSpace->SetMinimum(firstPoint);
    boundingBoxInObjectSpace->SetMaximum(firstPoint);

    // The total number of corner points of the bounding box.
    constexpr auto numberOfCorners = std::uintmax_t{ 1 } << TDimension;

    for (std::uintmax_t cornerNumber{ 1 }; cornerNumber < numberOfCorners; ++cornerNumber)
    {
      // For each corner, estimate the n-dimensional index.
{'fraction_non_alphanumeric': 0.11613135186960691, 'fraction_numerical': 0.0023969319271332696, 'mean_word_length': 4.537491705374917, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5325184', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2172, 'n_words': 648}