KWWidgets/Projects/3DWidgets/Part2/Tests: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
Line 116: Line 116:


=== Anatomy of a test ===
=== Anatomy of a test ===
There are a number of differences between tests for different classes, but the basic use is identical.
==== Differences ====
''' RecordLog '''
Some test code includes a log file stored in an array of ''' char ''' before the main program. Whether this is inlcued in the code or not requires a difference later in the main program - whether to record user events to a log file, or to read them in from the array.
''' Callback '''
In some cases a Callback is created in order to provide specific user interactions.
''' Code order '''
In some cases the first two main program sections are inversed - for the rest the order is important.
==== Similarities ====
''' Includes ''' #include "xyz.h" All test code requires the inclusion of header of classes used in the test.
''' Main program ''' ''int TestXxxWidget (int argc, char *argv[])'' starts all main test programs.
*''' Create pipeline '''
*''' Create RenderWindow, Renderer and Actors '''
*''' Create Widget and its WidgetRepresentation '''
*''' Add Actors to Renderer, set Background colour and size '''
*''' Record/Read Events to/from log '''
*''' Render image '''
*''' Remove Oobservers to go interactive '''
*''' Delete objects '''


<pre>
<pre>

Revision as of 08:05, 24 October 2006

The Tests

Purpose

Tests are small programs used to see if a class can be correctly instantiated and used.

Example

TestBorderWidget

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

  Program:   Visualization Toolkit
  Module:    $RCSfile: TestBorderWidget.cxx,v $

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm 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 notice for more information.

=========================================================================*/
//
// This example tests the vtkBorderWidget.

// First include the required header files for the VTK classes we are using.
#include "vtkBorderWidget.h"
#include "vtkBorderRepresentation.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCommand.h"
#include "vtkInteractorEventRecorder.h"
#include "vtkRegressionTestImage.h"
#include "vtkDebugLeaks.h"

int TestBorderWidget( int argc, char *argv[] )
{
  // Create the RenderWindow, Renderer and both Actors
  //
  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  // Create a test pipeline
  //
  vtkSphereSource *ss = vtkSphereSource::New();
  vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
  mapper->SetInput(ss->GetOutput());
  vtkActor *actor = vtkActor::New();
  actor->SetMapper(mapper);

  // Create the widget and its representation
  vtkBorderRepresentation *rep = vtkBorderRepresentation::New();
  rep->ProportionalResizeOn();
  rep->SetShowBorderToOn();
//  rep->SetShowBorderToActive();
  
  vtkBorderWidget *widget = vtkBorderWidget::New();
  widget->SetInteractor(iren);
  widget->SetRepresentation(rep);
  widget->SelectableOff();

  // Add the actors to the renderer, set the background and size
  //
  ren1->AddActor(actor);
  ren1->SetBackground(0.1, 0.2, 0.4);
  renWin->SetSize(300, 300);

  // record events
  vtkInteractorEventRecorder *recorder = vtkInteractorEventRecorder::New();
  recorder->SetInteractor(iren);
  recorder->SetFileName("c:/record.log");
//  recorder->Record();
//  recorder->ReadFromInputStringOn();
//  recorder->SetInputString(eventLog);

  // render the image
  //
  iren->Initialize();
  renWin->Render();
  widget->On();
//  recorder->Play();

  // Remove the observers so we can go interactive. Without this the "-I"
  // testing option fails.
  recorder->Off();

  int retVal = vtkRegressionTestImage( renWin );
  if ( retVal == vtkRegressionTester::DO_INTERACTOR)
    {
    iren->Start();
    }

  ss->Delete();
  mapper->Delete();
  actor->Delete();
  widget->Off();
  widget->Delete();
  rep->Delete();
  iren->Delete();
  renWin->Delete();
  ren1->Delete();
  recorder->Delete();
  
  return !retVal;

}

Anatomy of a test

There are a number of differences between tests for different classes, but the basic use is identical.

Differences

RecordLog Some test code includes a log file stored in an array of char before the main program. Whether this is inlcued in the code or not requires a difference later in the main program - whether to record user events to a log file, or to read them in from the array.

Callback In some cases a Callback is created in order to provide specific user interactions.

Code order In some cases the first two main program sections are inversed - for the rest the order is important.

Similarities

Includes #include "xyz.h" All test code requires the inclusion of header of classes used in the test. Main program int TestXxxWidget (int argc, char *argv[]) starts all main test programs.

  • Create pipeline
  • Create RenderWindow, Renderer and Actors
  • Create Widget and its WidgetRepresentation
  • Add Actors to Renderer, set Background colour and size
  • Record/Read Events to/from log
  • Render image
  • Remove Oobservers to go interactive
  • Delete objects

//include headers of required classes
#include "" 

//Create a callback if needed
class vtkXxxCallback : public vtkCommand
{
public:
  static vtkXxxCallback *New() 
    { return new vtkXxxCallback ; }
  virtual void Execute(vtkObject *caller, unsigned long, void*);
  vtkXxxCallback():ImageActor(0),XxxRep(0) 
    {
      this->Transform = vtkTransform::New();
    }
  ~vtkXxxCallback()
    {
      this->Transform->Delete();
    }
  vtkImageActor *ImageActor;
  vtkXxxRepresentation2D *XxxRep;
  vtkTransform *Transform;
};

// Method re-positions the points using random perturbation
void vtkXxxCallback::Execute(vtkObject*, unsigned long, void*)
{
  this->XxxRep->GetTransform(this->Transform);
  this->ImageActor->SetUserTransform(this->Transform);
}


int TestXxxWidget (int argc, char *argv[])
{
  // Create the pipeline
  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");

  // Create the RenderWindow, Renderer and both Actors 

  // VTK widgets consist of two parts: the widget part that handles event processing;
  // and the widget representation that defines how the widget appears in the scene 
  // (i.e., matters pertaining to geometry).

  // Add the actors to the renderer, set the background and size

  // record events
  vtkInteractorEventRecorder *recorder = vtkInteractorEventRecorder::New();
  recorder->SetInteractor(iren);
  recorder->SetFileName("c:/record.log");

  // render the image

  / Remove the observers so we can go interactive. Without this the "-I"
  // testing option fails.
  recorder->Off();

  //Delete objects
}