KWHelloWorldExample-alex.cxx

From KitwarePublic
Jump to navigationJump to search
#include "vtkKWApplication.h"
#include "vtkKWWindowBase.h"
#include "vtkKWLabel.h"
#include "vtkKWFrame.h"
#include "vtkKWRenderWidget.h"
#include "vtkKWHistogram.h"
#include "vtkKWPiecewiseFunctionEditor.h"

#include <vtksys/SystemTools.hxx>
#include <vtksys/CommandLineArguments.hxx>

#include "itkImageToVTKImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"

#include "vtkRenderer.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolume.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkCamera.h"
#include "vtkPointData.h"

int my_main(int argc, char *argv[])
{
  // Initialize Tcl

  Tcl_Interp *interp = vtkKWApplication::InitializeTcl(argc, argv, &cerr);
  if (!interp)
    {
    cerr << "Error: InitializeTcl failed" << endl ;
    return 1;
    }

  // Load the Image via ITK

  const unsigned int Dimension = 3;
  typedef unsigned char InputPixelType;

  typedef itk::Image<InputPixelType, Dimension> InputImageType;
  typedef itk::ImageFileReader<InputImageType> ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
//  reader->SetFileName( "/home/alex/interp/61457548-roi-8bit.tif" );
  reader->SetFileName( "/home/alex/interp/61457548-roi-8bit-x4-ga-40-0.05-10-no_edges.tif" );

  try
    {
        reader->Update();
    }
  catch( itk::ExceptionObject & excep )
     {
        std::cerr << "Exception while reading input!"<< std::endl;
        std::cerr << excep << std::endl;
     }

  typedef itk::ImageToVTKImageFilter<InputImageType> FilterType;
  FilterType::Pointer imtoimfilt = FilterType::New();
  imtoimfilt->SetInput( reader->GetOutput() );

  try
    {
            imtoimfilt->Update();
    }
  catch( itk::ExceptionObject & except)
    {
            std::cerr << "Exception while converting!"<< std::endl;
            std::cerr << except << std::endl;
    }

  // Create transfer mapping scalar value to opacity
  vtkPiecewiseFunction *opacTransFunc = vtkPiecewiseFunction::New();
    opacTransFunc->AddPoint(0, 1.0);
    opacTransFunc->AddPoint(100, 0.0);
    opacTransFunc->AddPoint(255, 0.0);

  // The property describes how the data will look
  vtkVolumeProperty *vp = vtkVolumeProperty::New();
    vp->SetScalarOpacity(opacTransFunc);
    vp->ShadeOn();
    vp->SetInterpolationTypeToLinear();

  vtkFixedPointVolumeRayCastMapper *rcm = vtkFixedPointVolumeRayCastMapper::New();
    rcm->SetInput( imtoimfilt->GetOutput());
  // The volume holds the mapper and the property and
  // can be used to position/orient the volume
  vtkVolume *volHead= vtkVolume::New();
  volHead->SetMapper(rcm);
  volHead->SetProperty(vp);

  // Create the application
  // If --test was provided, ignore all registry settings, and exit silently
  // Restore the settings that have been saved to the registry, like
  // the geometry of the user interface so far.

  vtkKWApplication *app = vtkKWApplication::New();
  app->SetName("KWHelloWorldExample-alex");
  app->RestoreApplicationSettingsFromRegistry();

  // Set a help link. Can be a remote link (URL), or a local file
  // vtksys::SystemTools::GetFilenamePath(__FILE__) + "/help.html";
  app->SetHelpDialogStartingPage("http://www.kwwidgets.org");

  // Add a window
  // Set 'SupportHelp' to automatically add a menu entry for the help link

  vtkKWWindowBase *win = vtkKWWindowBase::New();
  win->SupportHelpOn();
  app->AddWindow(win);
  win->Create();

  vtkKWRenderWidget *hello_renderwidget = vtkKWRenderWidget::New();
  hello_renderwidget->SetParent(win->GetViewFrame());
  hello_renderwidget->Create();
  
  vtkRenderer *hello_renderer = hello_renderwidget->GetRenderer();
  hello_renderer->AddViewProp(volHead); 
  hello_renderer->SetBackground(0.3, 0.6, 1.0);
  hello_renderer->GetActiveCamera()->ParallelProjectionOff();
  hello_renderer->ResetCamera();
  
  app->Script("pack %s -side left -fill both -anchor c -expand y",
                  hello_renderwidget->GetWidgetName());
  hello_renderwidget->Delete();
  
 // build an histogram of the data, it will be used inside the editor
 // as if we were trying to tune a tfunc based on the real values
 
   vtkKWHistogram *pfed_hist = vtkKWHistogram::New();
   pfed_hist->BuildHistogram( 
     imtoimfilt->GetOutput()->GetPointData()->GetScalars(), 0);
   double *range = pfed_hist->GetRange();
  
  // Assign our tfunc to the editor
  // Make sure we show the whole range of the tfunc
  // Use an histogram

  vtkKWPiecewiseFunctionEditor *pfed_tfunc2_editor = 
    vtkKWPiecewiseFunctionEditor::New();
  pfed_tfunc2_editor->SetParent(win->GetViewFrame());
  pfed_tfunc2_editor->Create();
  pfed_tfunc2_editor->SetBorderWidth(2);
  pfed_tfunc2_editor->SetReliefToGroove();
  pfed_tfunc2_editor->SetPadX(2);
  pfed_tfunc2_editor->SetPadY(2);
  pfed_tfunc2_editor->ExpandCanvasWidthOff();
  pfed_tfunc2_editor->SetCanvasWidth(250);
  pfed_tfunc2_editor->SetCanvasHeight(150);
  pfed_tfunc2_editor->SetLabelText("Opacity Function Editor");
  pfed_tfunc2_editor->SetBalloonHelpString(
    "Piecewise transfer function editor. Guidelines are dispayed "
    "for each midpoint, ticks are displayed in the "
    "parameter space at the bottom, the width is set explicitly. "
    "The range and histogram are based on a real image data.");

  pfed_tfunc2_editor->SetPiecewiseFunction(opacTransFunc);
  pfed_tfunc2_editor->SetWholeParameterRangeToFunctionRange();
  pfed_tfunc2_editor->SetVisibleParameterRangeToWholeParameterRange();

//  pfed_tfunc2_editor->PointIndexVisibilityOff();
//  pfed_tfunc2_editor->SelectedPointIndexVisibilityOn();
//  pfed_tfunc2_editor->MidPointVisibilityOn();
//  pfed_tfunc2_editor->PointGuidelineVisibilityOff();
//  pfed_tfunc2_editor->MidPointGuidelineVisibilityOn();
//  pfed_tfunc2_editor->MidPointGuidelineValueVisibilityOn();
//  pfed_tfunc2_editor->SetMidPointGuidelineValueFormat("%-#6.0f");
//  pfed_tfunc2_editor->MidPointEntryVisibilityOn();
//  pfed_tfunc2_editor->SharpnessEntryVisibilityOn();
  pfed_tfunc2_editor->ValueRangeVisibilityOff();
  pfed_tfunc2_editor->ParameterRangeVisibilityOff();
  pfed_tfunc2_editor->SetLabelPositionToTop();
  pfed_tfunc2_editor->LockEndPointsParameterOn();

  pfed_tfunc2_editor->SetHistogram(pfed_hist);

  pfed_tfunc2_editor->ParameterTicksVisibilityOn();
  pfed_tfunc2_editor->ComputeValueTicksFromHistogramOn();
//  pfed_tfunc2_editor->SetParameterTicksFormat(
//    pfed_tfunc2_editor->GetMidPointGuidelineValueFormat());

  app->Script(
    "pack %s -side top -anchor nw -expand n -padx 2 -pady 20", 
    pfed_tfunc2_editor->GetWidgetName());
  
  pfed_tfunc2_editor->Delete();
  opacTransFunc->Delete();
  pfed_hist->Delete();

  /*
  // Add a label, attach it to the view frame, and pack
  
  vtkKWLabel *hello_label = vtkKWLabel::New();
  hello_label->SetParent(win->GetViewFrame());
  hello_label->Create();
  hello_label->SetText("Hello, World!");
  app->Script("pack %s -side left -anchor c -expand y", 
              hello_label->GetWidgetName());
  hello_label->Delete();
*/
  
  // Start the application
  // If --test was provided, do not enter the event loop and run this example
  // as a non-interactive test for software quality purposes.

  int ret = 0;
  win->Display();
  app->Start(argc, argv);
  ret = app->GetExitStatus();
  win->Close();

  // Deallocate and exit

  win->Delete();
  app->Delete();
  
  return ret;
}

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int)
{
  int argc;
  char **argv;
  vtksys::SystemTools::ConvertWindowsCommandLineToUnixArguments(
    lpCmdLine, &argc, &argv);
  int ret = my_main(argc, argv);
  for (int i = 0; i < argc; i++) { delete [] argv[i]; }
  delete [] argv;
  return ret;
}
#else
int main(int argc, char *argv[])
{
  return my_main(argc, argv);
}
#endif



Complete Setup: [Welcome | Site Map]