1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestXYPlotActor.cxx
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.
=========================================================================*/
// .SECTION Thanks
// This test was written by Philippe Pebay, Kitware SAS 2012
#include "vtkActor.h"
#include "vtkDoubleArray.h"
#include "vtkLegendBoxActor.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkProperty2D.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkTextProperty.h"
#include "vtkXYPlotActor.h"
#include "vtkTestUtilities.h"
int TestXYPlotActor( int argc, char* argv[] )
{
// Create container for points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
// Create containers for data
unsigned int nPlots = 4;
vtkStdString names[] =
{
"sqrt(x)",
"sqrt(x)sin(10ln(sqrt(x)))",
"sqrt(x)cos(x/10)",
"-sqrt(x)",
};
vtkSmartPointer<vtkDoubleArray>* data = new vtkSmartPointer<vtkDoubleArray>[nPlots];
for ( unsigned int i = 0; i < nPlots; ++ i )
{
data[i] = vtkSmartPointer<vtkDoubleArray>::New();
data[i]->SetNumberOfComponents( 1 );
data[i]->SetName( names[i].c_str() );
}
// Fill in points and data
unsigned int nSteps = 10;
unsigned int stepSize = 50;
unsigned int nVals = nSteps * stepSize + 1;
for ( unsigned int i = 0; i < nVals; ++ i )
{
points->InsertNextPoint( i, 0., 0. );
double val0 = sqrt( static_cast<double>( i ) );
data[0]->InsertNextValue( val0 );
double val1 = val0 * sin( 10*log( val0 ) );
data[1]->InsertNextValue( val1 );
double val2 = val0 * cos( 2. * val0 );
data[2]->InsertNextValue( val2 );
data[3]->InsertNextValue( -val0 );
}
// Determine extrema
double* rangeCurr = data[0]->GetRange();
double range[2];
range[0] = rangeCurr[0];
range[1] = rangeCurr[1];
for ( unsigned int i = 1; i < nPlots; ++ i )
{
rangeCurr = data[i]->GetRange();
range[0] = rangeCurr[0] < range[0] ? rangeCurr[0] : range[0];
range[1] = rangeCurr[1] > range[1] ? rangeCurr[1] : range[1];
}
// Create data sets with created points and data
vtkSmartPointer<vtkPolyData>* polydata = new vtkSmartPointer<vtkPolyData>[nPlots];
for ( unsigned int i = 0; i < nPlots; ++ i )
{
polydata[i] = vtkSmartPointer<vtkPolyData>::New();
polydata[i]->SetPoints( points );
polydata[i]->GetPointData()->SetScalars( data[i] );
}
// Set XY plot actor
double colors[] =
{
.54, .21, .06, // burnt sienna
1., .38, .01, // cadmium orange
.24, .57, .25, // cobalt green
0., 0., 0.502, // navy blue
};
vtkSmartPointer<vtkXYPlotActor> xyPlot = vtkSmartPointer<vtkXYPlotActor>::New();
for ( unsigned int i = 0; i < nPlots; ++ i )
{
xyPlot->AddDataSetInput( polydata[i] );
xyPlot->SetPlotColor( i, colors[3 * i], colors[3 * i + 1], colors[3 * i + 2] );
}
xyPlot->GetPositionCoordinate()->SetValue( .01, .01, .0 );
xyPlot->GetPosition2Coordinate()->SetValue( .99, .99, .0 );
xyPlot->SetLineWidth( 2 );
xyPlot->SetBorder( 10 );
// Title settings
xyPlot->SetTitleItalic( 0 );
xyPlot->SetTitleBold( 1 );
xyPlot->SetTitleFontFamily( VTK_ARIAL );
xyPlot->SetTitleColor( .9, .06, .02 );
xyPlot->SetTitle( "XY Plot Actor Test");
// Legend settings
xyPlot->SetLegend( 1 );
xyPlot->SetLegendPosition( .7, .6 );
xyPlot->SetLegendPosition2( .25, .2 );
xyPlot->SetLegendBorder( 1 );
xyPlot->SetLegendBox( 0 );
xyPlot->SetLegendUseBackground( 1 );
xyPlot->SetLegendBackgroundColor( .86, .86, .86 );
for ( unsigned int i = 0; i < nPlots; ++ i )
{
xyPlot->GetLegendActor()->SetEntryString( i, names[i] );
}
// Axes settings
xyPlot->SetAxisTitleFontFamily( VTK_TIMES );
xyPlot->SetAxisTitleColor( 0., 0., 1. );
xyPlot->SetYTitlePositionToVCenter();
xyPlot->SetXTitle( "x" );
xyPlot->SetYTitle( "f(x)" );
xyPlot->SetXValuesToIndex();
xyPlot->SetXRange( 0, nVals - 1 );
xyPlot->SetYRange( floor( range[0] ), ceil( range[1] ) );
xyPlot->SetXAxisColor( 0., 0., 0. );
xyPlot->SetYAxisColor( 0., 0., 0. );
// Label settings
xyPlot->SetAxisLabelFontFamily( VTK_COURIER );
xyPlot->SetAxisLabelColor( 0., 0., .9 );
xyPlot->SetLabelFormat("%g");
xyPlot->SetAdjustXLabels( 0 );
xyPlot->SetNumberOfXLabels( nSteps + 1 );
xyPlot->SetAdjustYLabels( 0 );
xyPlot->SetNumberOfYLabels( 3 );
// Set up rendering contraption
vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground( .99, 1., .94 ); // titanium white
ren1->AddActor( xyPlot );
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->SetMultiSamples( 0 );
renWin->AddRenderer( ren1 );
renWin->SetSize( 600, 400 );
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow( renWin );
int retVal = vtkRegressionTestImage( renWin.GetPointer() );
if ( retVal == vtkRegressionTester::DO_INTERACTOR )
{
iren->Start();
}
// Clean up
delete [] polydata;
delete [] data;
return !retVal;
}
|