Commit dc9186c6 authored by David C. Lonie's avatar David C. Lonie Committed by Code Review
Browse files

Merge topic 'selection-label-export' into master

78d211ed Add GL2PS handler for vtkDataLabelMapper.
parents 9dd47c76 78d211ed
......@@ -10,6 +10,7 @@ if(VTK_GHOSTSCRIPT_EXECUTABLE)
TestGL2PSExporterVector.cxx,NO_VALID
TestGL2PSExporterVolumeRaster.cxx,NO_VALID
TestGL2PSTextActor3D.cxx
TestGL2PSLabeledDataMapper.cxx
TestStackedPlotGL2PS.cxx
)
endif()
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestGL2PSLabeledDataMapper.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.
=========================================================================*/
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
#include "vtkGL2PSExporter.h"
#include "vtkActor.h"
#include "vtkActor2D.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkCellCenters.h"
#include "vtkIdFilter.h"
#include "vtkLabeledDataMapper.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSelectVisiblePoints.h"
#include "vtkSphereSource.h"
#include "vtkTestingInteractor.h"
#include "vtkTextProperty.h"
// This test is adapted from labeledMesh.py to test GL2PS exporting of selection
// labels.
int TestGL2PSLabeledDataMapper(int, char *[] )
{
// Selection rectangle:
double xmin = 100.;
double xmax = 400.;
double ymin = 100.;
double ymax = 400.;
vtkNew<vtkPoints> pts;
pts->InsertPoint(0, xmin, ymin, 0.);
pts->InsertPoint(1, xmax, ymin, 0.);
pts->InsertPoint(2, xmax, ymax, 0.);
pts->InsertPoint(3, xmin, ymax, 0.);
vtkNew<vtkCellArray> rect;
rect->InsertNextCell(5);
rect->InsertCellPoint(0);
rect->InsertCellPoint(1);
rect->InsertCellPoint(2);
rect->InsertCellPoint(3);
rect->InsertCellPoint(0);
vtkNew<vtkPolyData> selectRect;
selectRect->SetPoints(pts.GetPointer());
selectRect->SetLines(rect.GetPointer());
vtkNew<vtkPolyDataMapper2D> rectMapper;
vtkNew<vtkActor2D> rectActor;
rectMapper->SetInputData(selectRect.GetPointer());
rectActor->SetMapper(rectMapper.GetPointer());
// Create sphere
vtkNew<vtkSphereSource> sphere;
vtkNew<vtkPolyDataMapper> sphereMapper;
vtkNew<vtkActor> sphereActor;
sphereMapper->SetInputConnection(sphere->GetOutputPort());
sphereActor->SetMapper(sphereMapper.GetPointer());
// Generate ids for labeling
vtkNew<vtkIdFilter> ids;
ids->SetInputConnection(sphere->GetOutputPort());
ids->PointIdsOn();
ids->CellIdsOn();
ids->FieldDataOn();
// Create labels for points
vtkNew<vtkSelectVisiblePoints> visPts;
visPts->SetInputConnection(ids->GetOutputPort());
visPts->SelectionWindowOn();
visPts->SetSelection(xmin, xmax, ymin, ymax);
vtkNew<vtkLabeledDataMapper> ldm;
ldm->SetInputConnection(visPts->GetOutputPort());
ldm->SetLabelModeToLabelFieldData();
vtkNew<vtkActor2D> pointLabels;
pointLabels->SetMapper(ldm.GetPointer());
// Create labels for cells:
vtkNew<vtkCellCenters> cc;
cc->SetInputConnection(ids->GetOutputPort());
vtkNew<vtkSelectVisiblePoints> visCells;
visCells->SetInputConnection(cc->GetOutputPort());
visCells->SelectionWindowOn();
visCells->SetSelection(xmin, xmax, ymin, ymax);
vtkNew<vtkLabeledDataMapper> cellMapper;
cellMapper->SetInputConnection(visCells->GetOutputPort());
cellMapper->SetLabelModeToLabelFieldData();
cellMapper->GetLabelTextProperty()->SetColor(0., 1., 0.);
vtkNew<vtkActor2D> cellLabels;
cellLabels->SetMapper(cellMapper.GetPointer());
// Rendering setup
vtkNew<vtkRenderer> ren;
visPts->SetRenderer(ren.GetPointer());
visCells->SetRenderer(ren.GetPointer());
ren->AddActor(sphereActor.GetPointer());
ren->AddActor2D(rectActor.GetPointer());
ren->AddActor2D(pointLabels.GetPointer());
ren->AddActor2D(cellLabels.GetPointer());
ren->SetBackground(1., 1., 1.);
ren->GetActiveCamera()->Zoom(.55);
vtkNew<vtkRenderWindow> renWin;
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
renWin->AddRenderer(ren.GetPointer());
renWin->SetMultiSamples(0);
renWin->SetSize(500, 500);
renWin->Render();
vtkNew<vtkGL2PSExporter> exp;
exp->SetRenderWindow(renWin.GetPointer());
exp->SetFileFormatToPS();
exp->CompressOff();
exp->SetPS3Shading(0);
exp->SetSortToSimple();
exp->DrawBackgroundOn();
exp->Write3DPropsAsRasterImageOff();
exp->SetTextAsPath(true);
std::string fileprefix = vtkTestingInteractor::TempDirectory +
std::string("/TestGL2PSLabeledDataMapper");
exp->SetFilePrefix(fileprefix.c_str());
exp->Write();
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
......@@ -8,6 +8,7 @@ vtk_module(vtkIOExport
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PS
vtkRenderingLabel
vtkRenderingOpenGL
vtkImagingCore
vtkIOCore
......
......@@ -30,6 +30,7 @@
#include "vtkImageData.h"
#include "vtkImageShiftScale.h"
#include "vtkIntArray.h"
#include "vtkLabeledDataMapper.h"
#include "vtkMapper2D.h"
#include "vtkMathTextUtilities.h"
#include "vtkMatrix4x4.h"
......@@ -676,6 +677,11 @@ void vtkGL2PSExporter::HandleSpecialProp(vtkProp *prop, vtkRenderer *ren)
{
this->DrawTextMapper(textMap, act2d, ren);
}
else if (vtkLabeledDataMapper *ldm =
vtkLabeledDataMapper::SafeDownCast(map2d))
{
this->DrawLabeledDataMapper(ldm, ren);
}
else // Some other mapper2D
{
return;
......@@ -761,6 +767,39 @@ void vtkGL2PSExporter::DrawTextMapper(vtkTextMapper *textMap,
this->DrawViewportTextOverlay(string, tprop, coord, ren);
}
void vtkGL2PSExporter::DrawLabeledDataMapper(vtkLabeledDataMapper *mapper,
vtkRenderer *ren)
{
vtkNew<vtkCoordinate> coord;
coord->SetViewport(ren);
switch (mapper->GetCoordinateSystem())
{
case vtkLabeledDataMapper::WORLD:
coord->SetCoordinateSystem(VTK_WORLD);
break;
case vtkLabeledDataMapper::DISPLAY:
coord->SetCoordinateSystem(VTK_DISPLAY);
break;
default:
vtkWarningMacro("Unsupported coordinate system for exporting vtkLabeled"
"DataMapper. Some text may not be exported properly.");
return;
}
int numberOfLabels = mapper->GetNumberOfLabels();
const char *text;
double position[3];
for (int i = 0; i < numberOfLabels; ++i)
{
text = mapper->GetLabelText(i);
mapper->GetLabelPosition(i, position);
coord->SetValue(position);
this->DrawViewportTextOverlay(text, mapper->GetLabelTextProperty(),
coord.GetPointer(), ren);
}
}
void vtkGL2PSExporter::DrawScalarBarActor(vtkScalarBarActor *bar,
vtkRenderer *ren)
{
......
......@@ -85,6 +85,7 @@ class vtkCollection;
class vtkCoordinate;
class vtkImageData;
class vtkIntArray;
class vtkLabeledDataMapper;
class vtkMatrix4x4;
class vtkPath;
class vtkProp;
......@@ -311,6 +312,7 @@ protected:
void DrawTextActor3D(vtkTextActor3D *textAct, vtkRenderer *ren);
void DrawTextMapper(vtkTextMapper *textMap, vtkActor2D *textAct,
vtkRenderer *ren);
void DrawLabeledDataMapper(vtkLabeledDataMapper *mapper, vtkRenderer *ren);
void DrawScalarBarActor(vtkScalarBarActor *bar, vtkRenderer *ren);
void DrawViewportTextOverlay(const char *string, vtkTextProperty *tprop,
vtkCoordinate *coord, vtkRenderer *ren);
......
......@@ -96,7 +96,8 @@ int vtkActor2D::RenderOverlay(vtkViewport* viewport)
if (renderWindow->GetCapturingGL2PSSpecialProps())
{
if (this->IsA("vtkTextActor") ||
(this->Mapper && this->Mapper->IsA("vtkTextMapper")))
(this->Mapper && (this->Mapper->IsA("vtkTextMapper") ||
this->Mapper->IsA("vtkLabeledDataMapper"))))
{
renderer->CaptureGL2PSSpecialProp(this);
}
......
......@@ -791,6 +791,13 @@ vtkLabeledDataMapper::GetMTime()
return mtime;
}
// ----------------------------------------------------------------------
const char *vtkLabeledDataMapper::GetLabelText(int label)
{
assert("label index range" && label >= 0 && label < this->NumberOfLabels);
return this->TextMappers[label]->GetInput();
}
// ----------------------------------------------------------------------
void
vtkLabeledDataMapper::SetFieldDataName(const char *arrayName)
......
......@@ -46,6 +46,8 @@
#include "vtkRenderingLabelModule.h" // For export macro
#include "vtkMapper2D.h"
#include <cassert>
class vtkDataObject;
class vtkDataSet;
class vtkTextMapper;
......@@ -180,6 +182,24 @@ public:
// Return the modified time for this object.
virtual unsigned long GetMTime();
// Description:
// Return the number of labels rendered by the mapper.
vtkGetMacro(NumberOfLabels, int)
// Description:
// Return the position of the requested label.
void GetLabelPosition(int label, double pos[3])
{
assert("label index range" && label >= 0 && label < this->NumberOfLabels);
pos[0] = this->LabelPositions[3 * label];
pos[1] = this->LabelPositions[3 * label + 1];
pos[2] = this->LabelPositions[3 * label + 2];
}
// Description:
// Return the text for the requested label.
const char *GetLabelText(int label);
protected:
vtkLabeledDataMapper();
~vtkLabeledDataMapper();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment