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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestLabelPlacementMapper.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.
=========================================================================*/
// .NAME Test of vtkLabelPlacementMapper
// .SECTION Description
// this program tests vtkLabelPlacementMapper which uses a sophisticated algorithm to
// prune labels/icons preventing them from overlapping.
#include "vtkActor.h"
#include "vtkActor2D.h"
#include "vtkCamera.h"
#include "vtkImageData.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkLabelHierarchy.h"
#include "vtkLabelPlacementMapper.h"
#include "vtkLabeledDataMapper.h"
#include "vtkPointData.h"
#include "vtkPointSetToLabelHierarchy.h"
#include "vtkPointSource.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkProperty.h"
#include "vtkRectilinearGrid.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkStringArray.h"
#include "vtkStructuredGrid.h"
#include "vtkTextProperty.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXMLPolyDataReader.h"
#include "vtkXMLPolyDataWriter.h"
#include <vtkRegressionTestImage.h>
#include <vtkTestUtilities.h>
int TestLabelPlacementMapper(int argc, char* argv[])
{
// use non-unit aspect ratio to capture more potential errors
int windowSize[2] = { 200, 600 };
vtkNew<vtkRenderWindow> renWin;
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindowInteractor> iren;
vtkInteractorStyleSwitch::SafeDownCast(iren->GetInteractorStyle())
->SetCurrentStyleToTrackballCamera();
renWin->SetSize(200, 600);
renWin->AddRenderer(renderer);
renderer->SetBackground(0.0, 0.0, 0.0);
iren->SetRenderWindow(renWin);
vtkNew<vtkTextProperty> tprop;
tprop->SetFontSize(12);
tprop->SetFontFamily(vtkTextProperty::GetFontFamilyFromString("Arial"));
tprop->SetColor(0.0, 0.8, 0.2);
// Test display if anchor is defined in
// World coordinate system
{
int maxLevels = 5;
int targetLabels = 32;
double labelRatio = 0.05;
char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/uniform-001371-5x5x5.vtp");
// int iteratorType = vtkLabelHierarchy::FULL_SORT;
int iteratorType = vtkLabelHierarchy::QUEUE;
// int iteratorType = vtkLabelHierarchy::DEPTH_FIRST;
double center[3] = { 12.0, 8.0, 30.0 };
vtkNew<vtkSphereSource> sphere;
sphere->SetRadius(5.0);
// view will be centered around this centerpoint, thereby shifting normalized view coordinate
// system from world coordinate system (to test if label display works with acnhors defined in
// arbitrary coordinate systems).
sphere->SetCenter(center);
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphere->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
renderer->AddActor(sphereActor);
vtkNew<vtkXMLPolyDataReader> xmlPolyDataReader;
xmlPolyDataReader->SetFileName(fname);
delete[] fname;
vtkNew<vtkTransformPolyDataFilter> transformToCenter;
transformToCenter->SetInputConnection(xmlPolyDataReader->GetOutputPort());
vtkNew<vtkTransform> transformToCenterTransform;
transformToCenterTransform->Translate(center);
transformToCenter->SetTransform(transformToCenterTransform);
vtkNew<vtkPointSetToLabelHierarchy> pointSetToLabelHierarchy;
pointSetToLabelHierarchy->SetTextProperty(tprop);
pointSetToLabelHierarchy->AddInputConnection(transformToCenter->GetOutputPort());
pointSetToLabelHierarchy->SetPriorityArrayName("Priority");
pointSetToLabelHierarchy->SetLabelArrayName("PlaceNames");
pointSetToLabelHierarchy->SetMaximumDepth(maxLevels);
pointSetToLabelHierarchy->SetTargetLabelCount(targetLabels);
vtkNew<vtkLabelPlacementMapper> labelPlacer;
labelPlacer->SetInputConnection(pointSetToLabelHierarchy->GetOutputPort());
labelPlacer->SetIteratorType(iteratorType);
labelPlacer->SetMaximumLabelFraction(labelRatio);
labelPlacer->UseDepthBufferOn();
vtkNew<vtkActor2D> textActor;
textActor->SetMapper(labelPlacer);
renderer->AddActor(textActor);
}
// Test display if anchor is defined in
// NormalizedViewport coordinate system
{
vtkNew<vtkPolyData> labeledPoints;
vtkNew<vtkPoints> points;
points->InsertNextPoint(0.05, 0.25, 0);
points->InsertNextPoint(0.75, 0.75, 0);
points->InsertNextPoint(0.50, 0.05, 0);
points->InsertNextPoint(0.50, 0.95, 0);
labeledPoints->SetPoints(points);
vtkNew<vtkStringArray> labels;
labels->SetName("labels");
labels->InsertNextValue("NV-left");
labels->InsertNextValue("NV-right");
labels->InsertNextValue("NV-bottom");
labels->InsertNextValue("NV-top");
vtkNew<vtkStringArray> labelsPriority;
labelsPriority->SetName("priority");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labeledPoints->GetPointData()->AddArray(labels);
labeledPoints->GetPointData()->AddArray(labelsPriority);
vtkNew<vtkPointSetToLabelHierarchy> pointSetToLabelHierarchy;
pointSetToLabelHierarchy->SetTextProperty(tprop);
pointSetToLabelHierarchy->AddInputData(labeledPoints);
pointSetToLabelHierarchy->SetPriorityArrayName("priority");
pointSetToLabelHierarchy->SetLabelArrayName("labels");
vtkNew<vtkLabelPlacementMapper> labelPlacer;
labelPlacer->SetInputConnection(pointSetToLabelHierarchy->GetOutputPort());
labelPlacer->PlaceAllLabelsOn();
labelPlacer->GetAnchorTransform()->SetCoordinateSystemToNormalizedViewport();
labelPlacer->UseDepthBufferOff();
vtkNew<vtkActor2D> textActor;
textActor->SetMapper(labelPlacer);
renderer->AddActor(textActor);
}
// Test display if anchor is defined in
// Display coordinate system
{
vtkNew<vtkPolyData> labeledPoints;
vtkNew<vtkPoints> points;
points->InsertNextPoint(windowSize[0] * 0.01, windowSize[1] * 0.01, 0);
points->InsertNextPoint(windowSize[0] * 0.90, windowSize[1] * 0.01, 0);
points->InsertNextPoint(windowSize[0] * 0.01, windowSize[1] * 0.97, 0);
points->InsertNextPoint(windowSize[0] * 0.90, windowSize[1] * 0.97, 0);
labeledPoints->SetPoints(points);
vtkNew<vtkStringArray> labels;
labels->SetName("labels");
labels->InsertNextValue("D-bottom-left");
labels->InsertNextValue("D-bottom-right");
labels->InsertNextValue("D-top-left");
labels->InsertNextValue("D-top-right");
vtkNew<vtkStringArray> labelsPriority;
labelsPriority->SetName("priority");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labelsPriority->InsertNextValue("1");
labeledPoints->GetPointData()->AddArray(labels);
labeledPoints->GetPointData()->AddArray(labelsPriority);
vtkNew<vtkPointSetToLabelHierarchy> pointSetToLabelHierarchy;
pointSetToLabelHierarchy->SetTextProperty(tprop);
pointSetToLabelHierarchy->AddInputData(labeledPoints);
pointSetToLabelHierarchy->SetPriorityArrayName("priority");
pointSetToLabelHierarchy->SetLabelArrayName("labels");
vtkNew<vtkLabelPlacementMapper> labelPlacer;
labelPlacer->SetInputConnection(pointSetToLabelHierarchy->GetOutputPort());
labelPlacer->PlaceAllLabelsOn();
labelPlacer->GetAnchorTransform()->SetCoordinateSystemToDisplay();
labelPlacer->UseDepthBufferOff();
vtkNew<vtkActor2D> textActor;
textActor->SetMapper(labelPlacer);
renderer->AddActor(textActor);
}
///
renWin->Render();
// renderer->GetActiveCamera()->ParallelProjectionOn();
renderer->ResetCamera();
renderer->ResetCamera();
renderer->ResetCamera();
int retVal = vtkRegressionTestImage(renWin);
if (retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
|