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

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

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

#include "mitkRenderingManager.h"
#include "mitkRenderingManagerFactory.h"
#include "mitkBaseRenderer.h"

#include <vtkRenderWindow.h>
#include <vtkCallbackCommand.h>

#include <itkCommand.h>
#include <algorithm>

namespace mitk
{

RenderingManager::Pointer RenderingManager::s_Instance = 0;
RenderingManagerFactory *RenderingManager::s_RenderingManagerFactory = 0;


RenderingManager
::RenderingManager()
: m_UpdatePending( false ),
  m_MaxLOD( 2 ),
  m_LODIncreaseBlocked( false ),
  m_LODAbortMechanismEnabled( false ),
  m_ClippingPlaneEnabled( false ),
  m_TimeNavigationController( NULL )
{
  m_ShadingEnabled.assign( 3, false );
  m_ShadingValues.assign( 4, 0.0 );
}


RenderingManager
::~RenderingManager()
{
}


void
RenderingManager
::SetFactory( RenderingManagerFactory *factory )
{
  s_RenderingManagerFactory = factory;
}


const RenderingManagerFactory *
RenderingManager
::GetFactory()
{
  return s_RenderingManagerFactory;
}


bool
RenderingManager
::HasFactory()
{
  if ( RenderingManager::s_RenderingManagerFactory )
  {
    return true;
  }
  else
  {
    return false;
  }
}


RenderingManager::Pointer
RenderingManager
::New()
{
  const RenderingManagerFactory* factory = GetFactory();
  if(factory == NULL)
    return NULL;
  return factory->CreateRenderingManager();
}

RenderingManager *
RenderingManager
::GetInstance()
{
  if ( !RenderingManager::s_Instance )
  {
    if ( s_RenderingManagerFactory )
    {
      s_Instance = s_RenderingManagerFactory->CreateRenderingManager();
    }
  }

  return s_Instance;
}


bool
RenderingManager
::IsInstantiated()
{
  if ( RenderingManager::s_Instance )
    return true;
  else
    return false;
}


void
RenderingManager
::AddRenderWindow( vtkRenderWindow *renderWindow )
{
  if ( renderWindow
    && (m_RenderWindowList.find( renderWindow ) == m_RenderWindowList.end()) )
  {
    m_RenderWindowList[renderWindow] = RENDERING_INACTIVE;
    m_AllRenderWindows.push_back( renderWindow );

    typedef itk::MemberCommand< RenderingManager > MemberCommandType;

    // Add callbacks for rendering abort mechanism
    //BaseRenderer *renderer = BaseRenderer::GetInstance( renderWindow );
    vtkCallbackCommand *startCallbackCommand = vtkCallbackCommand::New();
    startCallbackCommand->SetCallback( 
      RenderingManager::RenderingStartCallback );
    renderWindow->AddObserver( vtkCommand::StartEvent, startCallbackCommand );

    vtkCallbackCommand *progressCallbackCommand = vtkCallbackCommand::New();
    progressCallbackCommand->SetCallback( 
      RenderingManager::RenderingProgressCallback );
    renderWindow->AddObserver( vtkCommand::AbortCheckEvent, progressCallbackCommand );

    vtkCallbackCommand *endCallbackCommand = vtkCallbackCommand::New();
    endCallbackCommand->SetCallback( 
      RenderingManager::RenderingEndCallback );
    renderWindow->AddObserver( vtkCommand::EndEvent, endCallbackCommand );
  }
}


void
RenderingManager
::RemoveRenderWindow( vtkRenderWindow *renderWindow )
{
  m_RenderWindowList.erase( renderWindow );

  RenderWindowVector::iterator thisRenderWindowsPosition = std::find( m_AllRenderWindows.begin(), m_AllRenderWindows.end(), renderWindow );
  if ( thisRenderWindowsPosition != m_AllRenderWindows.end() )
  {
    m_AllRenderWindows.erase( thisRenderWindowsPosition );
  }
}


const RenderingManager::RenderWindowVector&
RenderingManager
::GetAllRegisteredRenderWindows()
{
  return m_AllRenderWindows;
}


void
RenderingManager
::RequestUpdate( vtkRenderWindow *renderWindow )<commit_msg>ENH (#976): Register/Unregister vtkRenderWindows for proper reference counting<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

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

#include "mitkRenderingManager.h"
#include "mitkRenderingManagerFactory.h"
#include "mitkBaseRenderer.h"

#include <vtkRenderWindow.h>
#include <vtkCallbackCommand.h>

#include <itkCommand.h>
#include <algorithm>

namespace mitk
{

RenderingManager::Pointer RenderingManager::s_Instance = 0;
RenderingManagerFactory *RenderingManager::s_RenderingManagerFactory = 0;


RenderingManager
::RenderingManager()
: m_UpdatePending( false ),
  m_MaxLOD( 2 ),
  m_LODIncreaseBlocked( false ),
  m_LODAbortMechanismEnabled( false ),
  m_ClippingPlaneEnabled( false ),
  m_TimeNavigationController( NULL )
{
  m_ShadingEnabled.assign( 3, false );
  m_ShadingValues.assign( 4, 0.0 );
}


RenderingManager
::~RenderingManager()
{
  // Decrease reference counts of all registered vtkRenderWindows for 
  // proper destruction
  RenderWindowVector::iterator it;
  for ( it = m_AllRenderWindows.begin(); it != m_AllRenderWindows.end(); ++it )
  {
    (*it)->UnRegister( NULL );
  }
}


void
RenderingManager
::SetFactory( RenderingManagerFactory *factory )
{
  s_RenderingManagerFactory = factory;
}


const RenderingManagerFactory *
RenderingManager
::GetFactory()
{
  return s_RenderingManagerFactory;
}


bool
RenderingManager
::HasFactory()
{
  if ( RenderingManager::s_RenderingManagerFactory )
  {
    return true;
  }
  else
  {
    return false;
  }
}


RenderingManager::Pointer
RenderingManager
::New()
{
  const RenderingManagerFactory* factory = GetFactory();
  if(factory == NULL)
    return NULL;
  return factory->CreateRenderingManager();
}

RenderingManager *
RenderingManager
::GetInstance()
{
  if ( !RenderingManager::s_Instance )
  {
    if ( s_RenderingManagerFactory )
    {
      s_Instance = s_RenderingManagerFactory->CreateRenderingManager();
    }
  }

  return s_Instance;
}


bool
RenderingManager
::IsInstantiated()
{
  if ( RenderingManager::s_Instance )
    return true;
  else
    return false;
}


void
RenderingManager
::AddRenderWindow( vtkRenderWindow *renderWindow )
{
  if ( renderWindow
    && (m_RenderWindowList.find( renderWindow ) == m_RenderWindowList.end()) )
  {
    m_RenderWindowList[renderWindow] = RENDERING_INACTIVE;
    m_AllRenderWindows.push_back( renderWindow );

    // Register vtkRenderWindow instance
    renderWindow->Register( NULL );

    typedef itk::MemberCommand< RenderingManager > MemberCommandType;

    // Add callbacks for rendering abort mechanism
    //BaseRenderer *renderer = BaseRenderer::GetInstance( renderWindow );
    vtkCallbackCommand *startCallbackCommand = vtkCallbackCommand::New();
    startCallbackCommand->SetCallback( 
      RenderingManager::RenderingStartCallback );
    renderWindow->AddObserver( vtkCommand::StartEvent, startCallbackCommand );

    vtkCallbackCommand *progressCallbackCommand = vtkCallbackCommand::New();
    progressCallbackCommand->SetCallback( 
      RenderingManager::RenderingProgressCallback );
    renderWindow->AddObserver( vtkCommand::AbortCheckEvent, progressCallbackCommand );

    vtkCallbackCommand *endCallbackCommand = vtkCallbackCommand::New();
    endCallbackCommand->SetCallback( 
      RenderingManager::RenderingEndCallback );
    renderWindow->AddObserver( vtkCommand::EndEvent, endCallbackCommand );
  }
}


void
RenderingManager
::RemoveRenderWindow( vtkRenderWindow *renderWindow )
{
  m_RenderWindowList.erase( renderWindow );

  RenderWindowVector::iterator it = std::find( m_AllRenderWindows.begin(), m_AllRenderWindows.end(), renderWindow );
  if ( it != m_AllRenderWindows.end() )
  {
    vtkRenderWindow *renderWindow = *it;
    m_AllRenderWindows.erase( it );

    // Decrease reference count for proper destruction
    renderWindow->UnRegister( NULL );
  }
}


const RenderingManager::RenderWindowVector&
RenderingManager
::GetAllRegisteredRenderWindows()
{
  return m_AllRenderWindows;
}


void
RenderingManager
::RequestUpdate( vtkRenderWindow *renderWindow )
{'fraction_non_alphanumeric': 0.1253396739130435, 'fraction_numerical': 0.0019248188405797102, 'mean_word_length': 5.528455284552845, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 32, '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': '2545128', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2489, 'n_words': 533}