<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#define GPU_INFO MITK_INFO("mapper.vr")
#define GPU_WARN MITK_WARN("mapper.vr")
#define GPU_ERROR MITK_ERROR("mapper.vr")

#include "mitkGPUVolumeMapper3D.h"

#include "mitkDataNode.h"

#include "mitkProperties.h"
#include "mitkLevelWindow.h"
#include "mitkColorProperty.h"
#include "mitkLevelWindowProperty.h"
#include "mitkLookupTableProperty.h"
#include "mitkTransferFunctionProperty.h"
#include "mitkTransferFunctionInitializer.h"
#include "mitkColorProperty.h"
#include "mitkVtkPropRenderer.h"
#include "mitkRenderingManager.h"

#include <vtkActor.h>
#include <vtkProperty.h>

#include <vtkVolumeTextureMapper2D.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkVolumeRayCastCompositeFunction.h>
#include <vtkVolumeRayCastMIPFunction.h>
#include <vtkFiniteDifferenceGradientEstimator.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageWriter.h>
#include <vtkImageData.h>
#include <vtkLODProp3D.h>
#include <vtkImageResample.h>
#include <vtkPlane.h>
#include <vtkImplicitPlaneWidget.h>
#include <vtkAssembly.h>

#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>

#include <itkMultiThreader.h>

// Only with VTK 5.6 or above
#if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION==5) && (VTK_MINOR_VERSION>=6) ))

#include "vtkMitkGPUVolumeRayCastMapper.h"

#endif

#include "vtkOpenGLGPUVolumeRayCastMapper.h"
#include "vtkMitkOpenGLVolumeTextureMapper3D.h"


const mitk::Image* mitk::GPUVolumeMapper3D::GetInput()
{
  return static_cast<const mitk::Image*> ( GetDataNode()->GetData() );
}

void mitk::GPUVolumeMapper3D::MitkRenderVolumetricGeometry(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  VtkMapper::MitkRenderVolumetricGeometry(renderer);

  if(ls->m_gpuInitialized)
    ls->m_MapperGPU->UpdateMTime();
}

bool mitk::GPUVolumeMapper3D::InitGPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_gpuInitialized)
    return ls->m_gpuSupported;

  GPU_INFO << "initializing gpu-slicing-vr (vtkMitkOpenGLVolumeTextureMapper3D)";

  ls->m_MapperGPU = vtkSmartPointer<vtkMitkOpenGLVolumeTextureMapper3D>::New();
  ls->m_MapperGPU->SetUseCompressedTexture(false);
  ls->m_MapperGPU->SetSampleDistance(1.0);

  ls->m_VolumePropertyGPU = vtkSmartPointer<vtkVolumeProperty>::New();
  ls->m_VolumePropertyGPU->ShadeOn();
  ls->m_VolumePropertyGPU->SetAmbient (0.25f); //0.05f
  ls->m_VolumePropertyGPU->SetDiffuse (0.50f); //0.45f
  ls->m_VolumePropertyGPU->SetSpecular(0.40f); //0.50f
  ls->m_VolumePropertyGPU->SetSpecularPower(16.0f);
  ls->m_VolumePropertyGPU->SetInterpolationTypeToLinear();

  ls->m_VolumeGPU = vtkSmartPointer<vtkVolume>::New();
  ls->m_VolumeGPU->SetMapper( ls->m_MapperGPU );
  ls->m_VolumeGPU->SetProperty( ls->m_VolumePropertyGPU );
  ls->m_VolumeGPU->VisibilityOn();

  ls->m_MapperGPU->SetInput( this->m_UnitSpacingImageFilter->GetOutput() );

  ls->m_gpuSupported = ls->m_MapperGPU->IsRenderSupported(renderer->GetVtkRenderer(),ls->m_VolumePropertyGPU);

  ls->m_gpuInitialized = true;

  return ls->m_gpuSupported;
}

void mitk::GPUVolumeMapper3D::InitCPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_cpuInitialized)
    return;

  int numThreads = itk::MultiThreader::GetGlobalDefaultNumberOfThreads();

  GPU_INFO << "initializing cpu-raycast-vr (vtkFixedPointVolumeRayCastMapper) (" << numThreads << " threads)";

  ls->m_MapperCPU = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
  ls->m_MapperCPU->SetSampleDistance(1.0);
//  ls->m_MapperCPU->LockSampleDistanceToInputSpacingOn();
  ls->m_MapperCPU->SetImageSampleDistance(1.0);
  ls->m_MapperCPU->IntermixIntersectingGeometryOn();
  ls->m_MapperCPU->SetAutoAdjustSampleDistances(0);

  ls->m_MapperCPU->SetNumberOfThreads( numThreads );

  ls->m_VolumePropertyCPU = vtkSmartPointer<vtkVolumeProperty>::New();
  ls->m_VolumePropertyCPU->ShadeOn();
  ls->m_VolumePropertyCPU->SetAmbient (0.10f); //0.05f
  ls->m_VolumePropertyCPU->SetDiffuse (0.50f); //0.45f
  ls->m_VolumePropertyCPU->SetSpecular(0.40f); //0.50f
  ls->m_VolumePropertyCPU->SetSpecularPower(16.0f);
  ls->m_VolumePropertyCPU->SetInterpolationTypeToLinear();

  ls->m_VolumeCPU = vtkSmartPointer<vtkVolume>::New();
  ls->m_VolumeCPU->SetMapper( ls->m_MapperCPU );
  ls->m_VolumeCPU->SetProperty( ls->m_VolumePropertyCPU );
  ls->m_VolumeCPU->VisibilityOn();

  ls->m_MapperCPU->SetInput( m_UnitSpacingImageFilter->GetOutput() );//m_Resampler->GetOutput());

  ls->m_cpuInitialized=true;
}

void mitk::GPUVolumeMapper3D::DeinitGPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_gpuInitialized)
  {
    GPU_INFO << "deinitializing gpu-slicing-vr";

    ls->m_VolumePropertyGPU = NULL;
    ls->m_MapperGPU = NULL;
    ls->m_VolumeGPU = NULL;
    ls->m_gpuInitialized=false;
  }
}

void mitk::GPUVolumeMapper3D::DeinitCPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);<commit_msg>releasing gpu memory in volumerendering when the gpu slicer is deinitialized<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#define GPU_INFO MITK_INFO("mapper.vr")
#define GPU_WARN MITK_WARN("mapper.vr")
#define GPU_ERROR MITK_ERROR("mapper.vr")

#include "mitkGPUVolumeMapper3D.h"

#include "mitkDataNode.h"

#include "mitkProperties.h"
#include "mitkLevelWindow.h"
#include "mitkColorProperty.h"
#include "mitkLevelWindowProperty.h"
#include "mitkLookupTableProperty.h"
#include "mitkTransferFunctionProperty.h"
#include "mitkTransferFunctionInitializer.h"
#include "mitkColorProperty.h"
#include "mitkVtkPropRenderer.h"
#include "mitkRenderingManager.h"

#include <vtkActor.h>
#include <vtkProperty.h>

#include <vtkVolumeTextureMapper2D.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkVolumeRayCastCompositeFunction.h>
#include <vtkVolumeRayCastMIPFunction.h>
#include <vtkFiniteDifferenceGradientEstimator.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageWriter.h>
#include <vtkImageData.h>
#include <vtkLODProp3D.h>
#include <vtkImageResample.h>
#include <vtkPlane.h>
#include <vtkImplicitPlaneWidget.h>
#include <vtkAssembly.h>

#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>

#include <itkMultiThreader.h>

// Only with VTK 5.6 or above
#if ((VTK_MAJOR_VERSION > 5) || ((VTK_MAJOR_VERSION==5) && (VTK_MINOR_VERSION>=6) ))

#include "vtkMitkGPUVolumeRayCastMapper.h"

#endif

#include "vtkOpenGLGPUVolumeRayCastMapper.h"
#include "vtkMitkOpenGLVolumeTextureMapper3D.h"


const mitk::Image* mitk::GPUVolumeMapper3D::GetInput()
{
  return static_cast<const mitk::Image*> ( GetDataNode()->GetData() );
}

void mitk::GPUVolumeMapper3D::MitkRenderVolumetricGeometry(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  VtkMapper::MitkRenderVolumetricGeometry(renderer);

  if(ls->m_gpuInitialized)
    ls->m_MapperGPU->UpdateMTime();
}

bool mitk::GPUVolumeMapper3D::InitGPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_gpuInitialized)
    return ls->m_gpuSupported;

  GPU_INFO << "initializing gpu-slicing-vr (vtkMitkOpenGLVolumeTextureMapper3D)";

  ls->m_MapperGPU = vtkSmartPointer<vtkMitkOpenGLVolumeTextureMapper3D>::New();
  ls->m_MapperGPU->SetUseCompressedTexture(false);
  ls->m_MapperGPU->SetSampleDistance(1.0);

  ls->m_VolumePropertyGPU = vtkSmartPointer<vtkVolumeProperty>::New();
  ls->m_VolumePropertyGPU->ShadeOn();
  ls->m_VolumePropertyGPU->SetAmbient (0.25f); //0.05f
  ls->m_VolumePropertyGPU->SetDiffuse (0.50f); //0.45f
  ls->m_VolumePropertyGPU->SetSpecular(0.40f); //0.50f
  ls->m_VolumePropertyGPU->SetSpecularPower(16.0f);
  ls->m_VolumePropertyGPU->SetInterpolationTypeToLinear();

  ls->m_VolumeGPU = vtkSmartPointer<vtkVolume>::New();
  ls->m_VolumeGPU->SetMapper( ls->m_MapperGPU );
  ls->m_VolumeGPU->SetProperty( ls->m_VolumePropertyGPU );
  ls->m_VolumeGPU->VisibilityOn();

  ls->m_MapperGPU->SetInput( this->m_UnitSpacingImageFilter->GetOutput() );

  ls->m_gpuSupported = ls->m_MapperGPU->IsRenderSupported(renderer->GetVtkRenderer(),ls->m_VolumePropertyGPU);

  ls->m_gpuInitialized = true;

  return ls->m_gpuSupported;
}

void mitk::GPUVolumeMapper3D::InitCPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_cpuInitialized)
    return;

  int numThreads = itk::MultiThreader::GetGlobalDefaultNumberOfThreads();

  GPU_INFO << "initializing cpu-raycast-vr (vtkFixedPointVolumeRayCastMapper) (" << numThreads << " threads)";

  ls->m_MapperCPU = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
  ls->m_MapperCPU->SetSampleDistance(1.0);
//  ls->m_MapperCPU->LockSampleDistanceToInputSpacingOn();
  ls->m_MapperCPU->SetImageSampleDistance(1.0);
  ls->m_MapperCPU->IntermixIntersectingGeometryOn();
  ls->m_MapperCPU->SetAutoAdjustSampleDistances(0);

  ls->m_MapperCPU->SetNumberOfThreads( numThreads );

  ls->m_VolumePropertyCPU = vtkSmartPointer<vtkVolumeProperty>::New();
  ls->m_VolumePropertyCPU->ShadeOn();
  ls->m_VolumePropertyCPU->SetAmbient (0.10f); //0.05f
  ls->m_VolumePropertyCPU->SetDiffuse (0.50f); //0.45f
  ls->m_VolumePropertyCPU->SetSpecular(0.40f); //0.50f
  ls->m_VolumePropertyCPU->SetSpecularPower(16.0f);
  ls->m_VolumePropertyCPU->SetInterpolationTypeToLinear();

  ls->m_VolumeCPU = vtkSmartPointer<vtkVolume>::New();
  ls->m_VolumeCPU->SetMapper( ls->m_MapperCPU );
  ls->m_VolumeCPU->SetProperty( ls->m_VolumePropertyCPU );
  ls->m_VolumeCPU->VisibilityOn();

  ls->m_MapperCPU->SetInput( m_UnitSpacingImageFilter->GetOutput() );//m_Resampler->GetOutput());

  ls->m_cpuInitialized=true;
}

void mitk::GPUVolumeMapper3D::DeinitGPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);

  if(ls->m_gpuInitialized)
  {
    GPU_INFO << "deinitializing gpu-slicing-vr";
    // deinit renderwindow, this is needed to release the memory allocated on the gpu
    // to prevent leaking memory on the gpu
    ls->m_MapperGPU->ReleaseGraphicsResources(renderer->GetVtkRenderer()->GetVTKWindow());
    ls->m_VolumePropertyGPU = NULL;
    ls->m_MapperGPU = NULL;
    ls->m_VolumeGPU = NULL;
    ls->m_gpuInitialized=false;
  }
}

void mitk::GPUVolumeMapper3D::DeinitCPU(mitk::BaseRenderer* renderer)
{
  LocalStorage *ls = m_LSH.GetLocalStorage(renderer);
{'fraction_non_alphanumeric': 0.1557810738138607, 'fraction_numerical': 0.011449388498568826, 'mean_word_length': 6.9243986254295535, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 249, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5006365', 'n_tokens_mistral': 4211, 'n_tokens_neox': 4013, 'n_words': 584}