<commit_before>// Test the vtkDICOMReader by displaying an image.

#include "vtkDICOMMetaData.h"
#include "vtkDICOMFileSorter.h"
#include "vtkDICOMReader.h"
#include "vtkDICOMCTRectifier.h"
#include "vtkDICOMApplyPalette.h"

#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkImageData.h"
#include "vtkImageReslice.h"
#include "vtkImageResliceMapper.h"
#include "vtkImageProperty.h"
#include "vtkImageSlice.h"
#include "vtkImageReader2.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkIntArray.h"
#include "vtkMatrix4x4.h"
#include "vtkMath.h"
#include "vtkErrorCode.h"

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
  style->SetInteractionModeToImage3D();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  iren->SetRenderWindow(renWin);
  iren->SetInteractorStyle(style);

  vtkSmartPointer<vtkDICOMFileSorter> sorter =
    vtkSmartPointer<vtkDICOMFileSorter>::New();

  vtkSmartPointer<vtkStringArray> files =
    vtkSmartPointer<vtkStringArray>::New();

  const char *stackID = 0;

  for (int i = 1; i < argc; i++)
    {
    if (strcmp(argv[i], "--stack") == 0)
      {
      if (i+1 < argc)
        {
        stackID = argv[++i];
        }
      }
    else
      {
      files->InsertNextValue(argv[i]);
      }
    }

  sorter->SetInputFileNames(files);
  sorter->Update();

#if 0
  // find the series with the largest number of files
  int m = sorter->GetNumberOfStudies();
  int studyIdx = 0;
  int seriesIdx = 0;
  int kmax = 0;
  for (int i = 0; i < m; i++)
    {
    int fj = sorter->GetFirstSeriesForStudy(i);
    int lj = sorter->GetLastSeriesForStudy(i);
    for (int j = fj; j <= lj; j++)
      {
      int k = sorter->GetFileNamesForSeries(j)->GetNumberOfValues();
      if (k > kmax)
        {
        kmax = k;
        seriesIdx = j;
        studyIdx = i;
        }
      }
    }

  // display the longest series
  //vtkStringArray *a = sorter->GetFileNamesForSeries(seriesIdx);
#endif

  // trust the user and display all the files, even if multiple series
  vtkStringArray *a = sorter->GetOutputFileNames();
  vtkSmartPointer<vtkImageReslice> reslice =
    vtkSmartPointer<vtkImageReslice>::New();
  vtkSmartPointer<vtkDICOMReader> reader =
    vtkSmartPointer<vtkDICOMReader>::New();
  reader->SetMemoryRowOrderToFileNative();
  //reader->TimeAsVectorOn();
  //reader->SetDesiredTimeIndex(5);
  if (stackID)
    {
    reader->SetDesiredStackID(stackID);
    }
  reader->SetFileNames(a);

  double range[2];
  int extent[6];
  reader->Update();

  if (reader->GetErrorCode() != vtkErrorCode::NoError)
    {
    return 1;
    }

  reader->GetOutput()->GetScalarRange(range);
  reader->GetOutput()->GetExtent(extent);

  // get the output port to connect to the display pipeline
  vtkAlgorithmOutput *portToDisplay = reader->GetOutputPort();

  vtkSmartPointer<vtkDICOMApplyPalette> palette =
    vtkSmartPointer<vtkDICOMApplyPalette>::New();
  palette->SetInputConnection(reader->GetOutputPort());
  palette->Update();
  palette->GetOutput()->GetScalarRange(range);
  portToDisplay = palette->GetOutputPort();

#if 0
  // check if the matrix indicates a tilted CT gantry
  vtkSmartPointer<vtkMatrix4x4> pmat =
    vtkSmartPointer<vtkMatrix4x4>::New();
  pmat->DeepCopy(reader->GetPatientMatrix());
  double xvec[4] = { 1.0, 0.0, 0.0, 0.0 };
  double yvec[4] = { 0.0, 1.0, 0.0, 0.0 };
  double zvec[4] = { 0.0, 0.0, 1.0, 0.0 };
  pmat->MultiplyPoint(xvec, xvec);
  pmat->MultiplyPoint(yvec, yvec);
  pmat->MultiplyPoint(zvec, zvec);

  // create slice normal and compare to zvec
  double normal[3];
  vtkMath::Cross(xvec, yvec, normal);
  double checkvec[3];
  vtkMath::Cross(zvec, normal, checkvec);
  if (vtkMath::Norm(checkvec) > 1e-3)
    {
    // definite gantry tilt, the volume is trapezoid-shaped
    vtkSmartPointer<vtkMatrix4x4> rmat =
      vtkSmartPointer<vtkMatrix4x4>::New();
    rmat->DeepCopy(pmat);
    rmat->SetElement(0, 2, normal[0]);
    rmat->SetElement(1, 2, normal[1]);
    rmat->SetElement(2, 2, normal[2]);
    pmat->Invert();
    vtkMatrix4x4::Multiply4x4(pmat, rmat, rmat);

    // pure shear matrix will have only one element that is different
    // from the identity matrix:
    double shear = rmat->GetElement(1, 2);
    rmat->Identity();
    rmat->SetElement(1, 2, shear);

    double origin[3], spacing[3];
    reader->GetOutput()->GetOrigin(origin);
    reader->GetOutput()->GetSpacing(spacing);
    // adjust the origin to centre the new volume on the old trapezoid
    origin[1] -= shear*0.5*spacing[2]*(extent[5] - extent[4]);

    // use vtkImageReslice to eliminate any shear
    reslice->SetOutputOrigin(origin);
    reslice->SetOutputSpacing(spacing);
    reslice->SetOutputExtent(extent);
    reslice->SetResliceAxes(rmat);
    reslice->SetInputConnection(reader->GetOutputPort());
    reslice->SetInterpolationModeToLinear();
    reslice->Update();

    // specify the port that the display pipeline will use
    portToDisplay = reslice->GetOutputPort();
    }
#else
  vtkSmartPointer<vtkDICOMCTRectifier> rect =
    vtkSmartPointer<vtkDICOMCTRectifier>::New();
  rect->SetVolumeMatrix(reader->GetPatientMatrix());
  rect->SetInputConnection(portToDisplay);
  rect->Update();
  portToDisplay = rect->GetOutputPort();
#endif

  static double viewport[3][4] = {
    { 0.67, 0.0, 1.0, 0.5 },
    { 0.67, 0.5, 1.0, 1.0 },
    { 0.0, 0.0, 0.67, 1.0 },
  };

  // check if image is 2D
  bool imageIs3D = (extent[5] > extent[4]);

  for (int i = 2*(imageIs3D == 0); i < 3; i++)
    {
    vtkSmartPointer<vtkImageResliceMapper> imageMapper =
      vtkSmartPointer<vtkImageResliceMapper>::New();<commit_msg>Allow TestDICOMDisplay to use a directory.<commit_after>// Test the vtkDICOMReader by displaying an image.

#include "vtkDICOMMetaData.h"
#include "vtkDICOMDirectory.h"
#include "vtkDICOMReader.h"
#include "vtkDICOMCTRectifier.h"
#include "vtkDICOMApplyPalette.h"

#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkImageData.h"
#include "vtkImageReslice.h"
#include "vtkImageResliceMapper.h"
#include "vtkImageProperty.h"
#include "vtkImageSlice.h"
#include "vtkImageReader2.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkIntArray.h"
#include "vtkMatrix4x4.h"
#include "vtkMath.h"
#include "vtkErrorCode.h"

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
  style->SetInteractionModeToImage3D();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  iren->SetRenderWindow(renWin);
  iren->SetInteractorStyle(style);

  vtkSmartPointer<vtkStringArray> files =
    vtkSmartPointer<vtkStringArray>::New();

  const char *stackID = 0;

  for (int i = 1; i < argc; i++)
    {
    if (strcmp(argv[i], "--stack") == 0)
      {
      if (i+1 < argc)
        {
        stackID = argv[++i];
        }
      }
    else
      {
      files->InsertNextValue(argv[i]);
      }
    }

  // find all DICOM files supplied by the user
  vtkSmartPointer<vtkDICOMDirectory> sorter =
    vtkSmartPointer<vtkDICOMDirectory>::New();
  sorter->RequirePixelDataOn();
  sorter->SetScanDepth(1);
  sorter->SetInputFileNames(files);
  sorter->Update();

  // find the series with the largest number of files
  int m = sorter->GetNumberOfStudies();
  int studyIdx = 0;
  int seriesIdx = 0;
  int kmax = 0;
  for (int i = 0; i < m; i++)
    {
    int fj = sorter->GetFirstSeriesForStudy(i);
    int lj = sorter->GetLastSeriesForStudy(i);
    for (int j = fj; j <= lj; j++)
      {
      int k = sorter->GetFileNamesForSeries(j)->GetNumberOfValues();
      if (k > kmax)
        {
        kmax = k;
        seriesIdx = j;
        studyIdx = i;
        }
      }
    }

  // display the longest series
  vtkStringArray *a = sorter->GetFileNamesForSeries(seriesIdx);
  vtkSmartPointer<vtkImageReslice> reslice =
    vtkSmartPointer<vtkImageReslice>::New();
  vtkSmartPointer<vtkDICOMReader> reader =
    vtkSmartPointer<vtkDICOMReader>::New();
  reader->SetMemoryRowOrderToFileNative();
  //reader->TimeAsVectorOn();
  //reader->SetDesiredTimeIndex(5);
  if (stackID)
    {
    reader->SetDesiredStackID(stackID);
    }
  reader->SetFileNames(a);

  double range[2];
  int extent[6];
  reader->Update();

  if (reader->GetErrorCode() != vtkErrorCode::NoError)
    {
    return 1;
    }

  reader->GetOutput()->GetScalarRange(range);
  reader->GetOutput()->GetExtent(extent);

  // get the output port to connect to the display pipeline
  vtkAlgorithmOutput *portToDisplay = reader->GetOutputPort();

  vtkSmartPointer<vtkDICOMApplyPalette> palette =
    vtkSmartPointer<vtkDICOMApplyPalette>::New();
  palette->SetInputConnection(reader->GetOutputPort());
  palette->Update();
  palette->GetOutput()->GetScalarRange(range);
  portToDisplay = palette->GetOutputPort();

  vtkSmartPointer<vtkDICOMCTRectifier> rect =
    vtkSmartPointer<vtkDICOMCTRectifier>::New();
  rect->SetVolumeMatrix(reader->GetPatientMatrix());
  rect->SetInputConnection(portToDisplay);
  rect->Update();
  portToDisplay = rect->GetOutputPort();

  static double viewport[3][4] = {
    { 0.67, 0.0, 1.0, 0.5 },
    { 0.67, 0.5, 1.0, 1.0 },
    { 0.0, 0.0, 0.67, 1.0 },
  };

  // check if image is 2D
  bool imageIs3D = (extent[5] > extent[4]);

  for (int i = 2*(imageIs3D == 0); i < 3; i++)
    {
    vtkSmartPointer<vtkImageResliceMapper> imageMapper =
      vtkSmartPointer<vtkImageResliceMapper>::New();
{'fraction_non_alphanumeric': 0.14261340826977117, 'fraction_numerical': 0.017362505018065032, 'mean_word_length': 3.975037443834249, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4124740', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3570, 'n_words': 680}