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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestValuePassFloatingPoint.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.
=========================================================================*/
// Description:
// Tests vtkValuePass in FLOATING_POINT mode. The test generates a 3-component
// float array ("elevationVector") using the loaded polygonal data (points and cells).
// Polygons are rendered with the ValuePass to its internal floating point frame-buffer.
// The rendered float image is then queried from the vtkValuePass and used to
// generate a color image using vtkLookupTable, the color image is rendered with
// an image actor on-screen. This is repeated for each component.
#include "vtkActor.h"
#include "vtkArrayCalculator.h"
#include "vtkCamera.h"
#include "vtkCameraPass.h"
#include "vtkCellData.h"
#include "vtkElevationFilter.h"
#include "vtkFloatArray.h"
#include "vtkImageActor.h"
#include "vtkImageData.h"
#include "vtkImageMapper3D.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkLookupTable.h"
#include "vtkOpenGLRenderer.h"
#include "vtkPointData.h"
#include "vtkPointDataToCellData.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderPassCollection.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSequencePass.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkTestUtilities.h"
#include "vtkValuePass.h"
void GenerateElevationArray(vtkSmartPointer<vtkPolyDataAlgorithm> source)
{
vtkPolyData* data = source->GetOutput();
double* bounds = data->GetBounds();
vtkSmartPointer<vtkElevationFilter> elevation =
vtkSmartPointer<vtkElevationFilter>::New();
elevation->SetInputConnection(source->GetOutputPort());
// Use vtkElevation to generate an array per component. vtkElevation generates
// a projected distance from each point in the dataset to the line, with respect to
// the LowPoint ([0, 1] in this case. This is different from having the actual
// coordinates of a given point.
for (int c = 0; c < 3; c++)
{
std::string name;
switch (c)
{
case 0:
name = "delta_x";
elevation->SetLowPoint(bounds[0], 0.0, 0.0);
elevation->SetHighPoint(bounds[1], 0.0, 0.0);
break;
case 1:
name = "delta_y";
elevation->SetLowPoint(0.0, bounds[2], 0.0);
elevation->SetHighPoint(0.0, bounds[3], 0.0);
break;
case 2:
name = "delta_z";
elevation->SetLowPoint(0.0, 0.0, bounds[4]);
elevation->SetHighPoint(0.0, 0.0, bounds[5]);
break;
}
elevation->Update();
vtkPolyData* result = vtkPolyData::SafeDownCast(elevation->GetOutput());
int outCellFlag;
// Enums defined in vtkAbstractMapper
vtkDataArray* elevArray = vtkAbstractMapper::GetScalars(result,
VTK_SCALAR_MODE_USE_POINT_FIELD_DATA, VTK_GET_ARRAY_BY_NAME/*acc mode*/,
0/*arr id*/, "Elevation"/*arr name*/, outCellFlag);
if (!elevArray)
{
std::cout << "->> Error: could not find array!" << std::endl;
return;
}
elevArray->SetName(name.c_str());
data->GetPointData()->AddArray(elevArray);
}
// Generate a 3-component vector array using the single components
// form elevation
// Point data
vtkSmartPointer<vtkArrayCalculator> calc =
vtkSmartPointer<vtkArrayCalculator>::New();
calc->SetInputConnection(source->GetOutputPort());
calc->SetAttributeModeToUsePointData();
calc->AddScalarArrayName("delta_x");
calc->AddScalarArrayName("delta_y");
calc->AddScalarArrayName("delta_z");
calc->SetFunction("delta_x * iHat + delta_y * jHat + delta_z * kHat");
calc->SetResultArrayName("elevationVector");
calc->Update();
// Cell data
vtkSmartPointer<vtkPointDataToCellData> p2c =
vtkSmartPointer<vtkPointDataToCellData>::New();
p2c->SetInputConnection(calc->GetOutputPort());
p2c->PassPointDataOn();
p2c->Update();
/// Include the elevation vector (point and cell data) in the original data
vtkPolyData* outputP2c = vtkPolyData::SafeDownCast(p2c->GetOutput());
data->GetPointData()->AddArray(calc->GetOutput()->GetPointData()->GetArray(
"elevationVector"));
data->GetCellData()->AddArray(outputP2c->GetCellData()->GetArray("elevationVector"));
};
//------------------------------------------------------------------------------
void RenderComponentImages(std::vector<vtkSmartPointer<vtkImageData> >& colorImOut,
vtkRenderWindow* window, vtkRenderer* renderer,
vtkValuePass* valuePass, int dataMode, char const* name)
{
valuePass->SetInputArrayToProcess(dataMode, name);
// Prepare a lut to map the floating point values
vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
lut->SetAlpha(1.0);
lut->Build();
// Render each component in a separate image
for(int c = 0; c < 3; c++)
{
valuePass->SetInputComponentToProcess(c);
window->Render();
/// Get the resulting values
vtkFloatArray* result = valuePass->GetFloatImageDataArray(renderer);
int* ext = valuePass->GetFloatImageExtents();
// Map the resulting float image to a color table
vtkUnsignedCharArray* colored = lut->MapScalars(result, VTK_COLOR_MODE_DEFAULT,
0/* single comp*/);
// Create an image dataset to render in a quad.
vtkSmartPointer<vtkImageData> colorIm = vtkSmartPointer<vtkImageData>::New();
colorIm->SetExtent(ext);
colorIm->GetPointData()->SetScalars(colored);
colorImOut.push_back(colorIm);
colored->Delete();
}
};
///////////////////////////////////////////////////////////////////////////////
int TestValuePassFloatingPoint(int argc, char *argv[])
{
// Load data
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
sphere->SetThetaResolution(8.0);
sphere->SetPhiResolution(8.0);
sphere->Update();
// Prepare a 3-component array (data will be appended to reader's output)
GenerateElevationArray(sphere);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(sphere->GetOutput());
mapper->ScalarVisibilityOn();
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Setup rendering and interaction
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
interactor->SetInteractorStyle(style);
vtkSmartPointer<vtkRenderWindow> window =
vtkSmartPointer<vtkRenderWindow>::New();
window->SetMultiSamples(0);
window->SetSize(640, 640);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
window->AddRenderer(renderer);
interactor->SetRenderWindow(window);
renderer->AddActor(actor);
renderer->SetBackground(0.2, 0.2, 0.5);
// Setup the value pass
//int const RenderingMode = vtkValuePass::INVERTIBLE_LUT;
int const RenderingMode = vtkValuePass::FLOATING_POINT;
int const comp = 0;
vtkSmartPointer<vtkValuePass> valuePass =
vtkSmartPointer<vtkValuePass>::New();
valuePass->SetRenderingMode(RenderingMode);
valuePass->SetInputComponentToProcess(comp);
// Initial data mode
valuePass->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA,
"elevationVector");
//valuePass->SetInputArrayToProcess(VTK_SCALAR_MODE_USE_CELL_FIELD_DATA,
// "elevationVector");
// 3. Add it to a sequence of passes
vtkSmartPointer<vtkRenderPassCollection> passes =
vtkSmartPointer<vtkRenderPassCollection>::New();
passes->AddItem(valuePass);
vtkSmartPointer<vtkSequencePass> sequence =
vtkSmartPointer<vtkSequencePass>::New();
sequence->SetPasses(passes);
vtkSmartPointer<vtkCameraPass> cameraPass =
vtkSmartPointer<vtkCameraPass>::New();
cameraPass->SetDelegatePass(sequence);
vtkOpenGLRenderer *glRenderer =
vtkOpenGLRenderer::SafeDownCast(renderer.GetPointer());
// Render the value pass
glRenderer->SetPass(cameraPass);
window->Render();
// Check whether the RenderingMode change (this could happen due to a lack of
// extension/context support
if (valuePass->GetRenderingMode() == vtkValuePass::FLOATING_POINT)
{
// Render point data images
std::vector<vtkSmartPointer<vtkImageData> > colorImagesPoint;
RenderComponentImages(colorImagesPoint, window, renderer, valuePass,
VTK_SCALAR_MODE_USE_POINT_FIELD_DATA, "elevationVector");
// Render cell data images
std::vector<vtkSmartPointer<vtkImageData> > colorImagesCell;
RenderComponentImages(colorImagesCell, window, renderer, valuePass,
VTK_SCALAR_MODE_USE_CELL_FIELD_DATA, "elevationVector");
////// Render results on-screen
renderer->RemoveActor(actor);
// Add image actors to display the point dataArray's componets
vtkSmartPointer<vtkImageActor> ia_x = vtkSmartPointer<vtkImageActor>::New();
ia_x->GetMapper()->SetInputData(colorImagesPoint.at(0));
renderer->AddActor(ia_x);
vtkSmartPointer<vtkImageActor> ia_y = vtkSmartPointer<vtkImageActor>::New();
ia_y->RotateX(90);
ia_y->GetMapper()->SetInputData(colorImagesPoint.at(1));
renderer->AddActor(ia_y);
vtkSmartPointer<vtkImageActor> ia_z = vtkSmartPointer<vtkImageActor>::New();
ia_z->RotateY(-90);
ia_z->GetMapper()->SetInputData(colorImagesPoint.at(2));
renderer->AddActor(ia_z);
// Add image actors to display cell dataArray's components
vtkSmartPointer<vtkImageActor> iacell_x = vtkSmartPointer<vtkImageActor>::New();
iacell_x->SetPosition(-500, 600, 600);
iacell_x->GetMapper()->SetInputData(colorImagesCell.at(0));
renderer->AddActor(iacell_x);
vtkSmartPointer<vtkImageActor> iacell_y = vtkSmartPointer<vtkImageActor>::New();
iacell_y->RotateX(90);
iacell_y->SetPosition(-500, 600, 600);
iacell_y->GetMapper()->SetInputData(colorImagesCell.at(1));
renderer->AddActor(iacell_y);
vtkSmartPointer<vtkImageActor> iacell_z = vtkSmartPointer<vtkImageActor>::New();
iacell_z->RotateY(-90);
iacell_z->SetPosition(-500, 600, 600);
iacell_z->GetMapper()->SetInputData(colorImagesCell.at(2));
renderer->AddActor(iacell_z);
// Adjust viewpoint
vtkCamera* cam = renderer->GetActiveCamera();
cam->SetPosition(2, 2, 2);
cam->SetFocalPoint(0, 0, 1);
renderer->ResetCamera();
// Use the default pass to render the colored image.
glRenderer->SetPass(NULL);
window->Render();
}
// initialize render loop
int retVal = vtkRegressionTestImage(window.GetPointer());
if( retVal == vtkRegressionTester::DO_INTERACTOR)
{
interactor->Start();
}
return !retVal;
}
|