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

From KitwarePublic
Jump to navigationJump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
= The Tests =
= The Tests =
[http://www.vtk.org/doc/nightly/html/c2_vtk_t_0.html All vtk tests]


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


=== Example ===
=== Examples ===
[http://public.kitware.com/cgi-bin/viewcvs.cgi/*checkout*/Widgets/Testing/Cxx/TestAffineWidget.cxx?root=VTK&content-type=text/plain TestAffineWidget]
[http://public.kitware.com/cgi-bin/viewcvs.cgi/*checkout*/Widgets/Testing/Cxx/TestBorderWidget.cxx?root=VTK&content-type=text/plain TestBorderWidget]
[http://public.kitware.com/cgi-bin/viewcvs.cgi/*checkout*/Widgets/Testing/Cxx/TestBorderWidget.cxx?root=VTK&content-type=text/plain TestBorderWidget]
[http://public.kitware.com/cgi-bin/viewcvs.cgi/*checkout*/Widgets/Testing/Cxx/TestDistanceWidget.cxx?root=VTK&content-type=text/plain TestDistanceWidget]
<pre>
<pre>
/*=========================================================================
/*=========================================================================
Line 117: Line 122:
=== 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.
<pre>
char TestLineWidget2EventLog[] =
"# StreamVersion 1\n"
"CharEvent 185 179 0 0 105 1 i\n"
"KeyReleaseEvent 185 179 0 0 105 1 i\n"
"MouseMoveEvent 185 178 0 0 0 0 i\n"
</pre>
''' Callback '''
In some cases a Callback is created in order to provide specific user interactions.
<pre>
<pre>
//include headers of required classes
#include ""
//Create a callback if needed
class vtkXxxCallback : public vtkCommand
class vtkXxxCallback : public vtkCommand
{
{
Line 128: Line 142:
   static vtkXxxCallback *New()  
   static vtkXxxCallback *New()  
     { return new vtkXxxCallback ; }
     { return new vtkXxxCallback ; }
   virtual void Execute(vtkObject *caller, unsigned long, void*);
   virtual void Execute(vtkObject *caller, unsigned long, void*)
  {//specific action here
  }
   vtkXxxCallback():ImageActor(0),XxxRep(0)  
   vtkXxxCallback():ImageActor(0),XxxRep(0)  
     {
     { this->Transform = vtkTransform::New(); }
      this->Transform = vtkTransform::New();
    }
   ~vtkXxxCallback()
   ~vtkXxxCallback()
     {
     { this->Transform->Delete(); }
      this->Transform->Delete();
    }
   vtkImageActor *ImageActor;
   vtkImageActor *ImageActor;
   vtkXxxRepresentation2D *XxxRep;
   vtkXxxRepresentation2D *XxxRep;
   vtkTransform *Transform;
   vtkTransform *Transform;
};
};
</pre>
''' Code order '''
In some cases the first two main program sections (create pipeline and create RenderWindow) are inversed - for the rest the order is important.


// Method re-positions the points using random perturbation
==== Similarities ====
void vtkXxxCallback::Execute(vtkObject*, unsigned long, void*)
''' Includes ''' #include "xyz.h" All test code requires the inclusion of header of classes used in the test.
{
<pre>
  this->XxxRep->GetTransform(this->Transform);
//include headers of required classes
  this->ImageActor->SetUserTransform(this->Transform);
#include ""
}
</pre>


''' Main program '''
This starts all main test programs.
<pre>int TestXxxWidget (int argc, char *argv[])</pre>


int TestXxxWidget (int argc, char *argv[])
*''' Create pipeline '''
{
<pre>
  // Create the pipeline
   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
   char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
  vtkVolume16Reader* v16 = vtkVolume16Reader::New();
  v16->SetDataDimensions(64, 64);
  v16->SetDataByteOrderToLittleEndian();
  v16->SetImageRange(1, 93);
  v16->SetDataSpacing(3.2, 3.2, 1.5);
  v16->SetFilePrefix(fname);
  v16->ReleaseDataFlagOn();
  v16->SetDataMask(0x7fff);
  v16->Update();
  delete[] fname;
</pre>
*''' Create RenderWindow, Renderer and Actors '''
<pre>
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkInteractorStyleImage *style = vtkInteractorStyleImage::New();
iren->SetInteractorStyle(style);
</pre>


   // Create the RenderWindow, Renderer and both Actors
*''' Create Widget and its WidgetRepresentation '''
<pre>
double p[3];
vtkLineRepresentation *rep = vtkLineRepresentation::New();
p[0] = 0.0; p[1] = -1.0; p[2] = 0.0;
rep->SetPoint1WorldPosition(p);
p[0] = 0.0; p[1] =  1.0; p[2] = 0.0;
rep->SetPoint2WorldPosition(p);
rep->PlaceWidget(pl3d->GetOutput()->GetBounds());
rep->GetPolyData(seeds);
    
vtkLineWidget2 *lineWidget = vtkLineWidget2::New();
lineWidget->SetInteractor(iren);
lineWidget->SetRepresentation(rep);
lineWidget->AddObserver(vtkCommand::InteractionEvent,myCallback);


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


  // Add the actors to the renderer, set the background and size
*''' Add Actors to Renderer, set Background colour and size '''
<pre>
ren1->SetBackground(0.1, 0.2, 0.4);
renWin->SetSize(300, 300);
</pre>
 
*''' Record/Read Events to/from log '''
Required:
<pre>
vtkInteractorEventRecorder *recorder = vtkInteractorEventRecorder::New();
recorder->SetInteractor(iren);
</pre>
Used to record events to a log file:
<pre>
recorder->SetFileName("c:/record.log");
recorder->Record();
</pre>
Used to read events from log:
<pre>
recorder->ReadFromInputStringOn();
recorder->SetInputString(eventLog);
</pre>


  // record events
*''' Render image '''
  vtkInteractorEventRecorder *recorder = vtkInteractorEventRecorder::New();
<pre>
  recorder->SetInteractor(iren);
iren->Initialize();
  recorder->SetFileName("c:/record.log");
renWin->Render();
</pre>


  // render the image
Add this line only if we are reading from a log!
<pre>
recorder->Play();
</pre>


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


  //Delete objects
*''' Delete objects '''
}
<pre>
v16->Delete();
shifter->Delete();
imageActor->Delete();
widget->Off();
widget->Delete();
rep->Delete();
style->Delete();
iren->Delete();
renWin->Delete();
ren1->Delete();
recorder->Delete();
</pre>
</pre>

Latest revision as of 09:33, 24 October 2006

The Tests

All vtk tests


Purpose

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

Examples

TestAffineWidget TestBorderWidget TestDistanceWidget

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

  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.

char TestLineWidget2EventLog[] =
"# StreamVersion 1\n"
"CharEvent 185 179 0 0 105 1 i\n"
"KeyReleaseEvent 185 179 0 0 105 1 i\n"
"MouseMoveEvent 185 178 0 0 0 0 i\n"

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

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

Code order In some cases the first two main program sections (create pipeline and create RenderWindow) 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.

//include headers of required classes
#include "" 

Main program This starts all main test programs.

int TestXxxWidget (int argc, char *argv[])
  • Create pipeline
  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter");
  vtkVolume16Reader* v16 = vtkVolume16Reader::New();
  v16->SetDataDimensions(64, 64);
  v16->SetDataByteOrderToLittleEndian();
  v16->SetImageRange(1, 93);
  v16->SetDataSpacing(3.2, 3.2, 1.5);
  v16->SetFilePrefix(fname);
  v16->ReleaseDataFlagOn();
  v16->SetDataMask(0x7fff);
  v16->Update();
  delete[] fname;
  • Create RenderWindow, Renderer and Actors
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);

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

vtkInteractorStyleImage *style = vtkInteractorStyleImage::New();
iren->SetInteractorStyle(style);
  • Create Widget and its WidgetRepresentation
double p[3];
vtkLineRepresentation *rep = vtkLineRepresentation::New();
p[0] = 0.0; p[1] = -1.0; p[2] = 0.0;
rep->SetPoint1WorldPosition(p);
p[0] = 0.0; p[1] =  1.0; p[2] = 0.0;
rep->SetPoint2WorldPosition(p);
rep->PlaceWidget(pl3d->GetOutput()->GetBounds());
rep->GetPolyData(seeds);
  
vtkLineWidget2 *lineWidget = vtkLineWidget2::New();
lineWidget->SetInteractor(iren);
lineWidget->SetRepresentation(rep);
lineWidget->AddObserver(vtkCommand::InteractionEvent,myCallback);

ren1->AddActor(streamline);
ren1->AddActor(outlineActor);
  • Add Actors to Renderer, set Background colour and size
ren1->SetBackground(0.1, 0.2, 0.4);
renWin->SetSize(300, 300);
  • Record/Read Events to/from log

Required:

vtkInteractorEventRecorder *recorder = vtkInteractorEventRecorder::New();
recorder->SetInteractor(iren);

Used to record events to a log file:

recorder->SetFileName("c:/record.log");
recorder->Record();

Used to read events from log:

recorder->ReadFromInputStringOn();
recorder->SetInputString(eventLog);
  • Render image
iren->Initialize();
renWin->Render();

Add this line only if we are reading from a log!

recorder->Play();
  • Remove Observers to go interactive
recorder->Off();
  • Delete objects
v16->Delete();
shifter->Delete();
imageActor->Delete();
widget->Off();
widget->Delete();
rep->Delete();
style->Delete();
iren->Delete();
renWin->Delete();
ren1->Delete();
recorder->Delete();