Commit 833e1cc7 authored by Scott Wittenburg's avatar Scott Wittenburg Committed by Kitware Robot

Merge topic 'add-labeled-contour-polydataitem'

54a59288 Add a test for labeled contours
c5f9bca2 Create a context item for labeled contour polydata
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !4699
parents 8efcc553 54a59288
......@@ -17,6 +17,7 @@ set(module_SRCS
vtkContextTransform.cxx
vtkImageItem.cxx
vtkMarkerUtilities.cxx
vtkLabeledContourPolyDataItem.cxx
vtkPen.cxx
# vtkPiecewisePointHandleItem.cxx
vtkPolyDataItem.cxx
......
311e976241e016a6bba7b7e0db6d2e0684e68f1b1adab4b435f3580dd3085c1f7f8bf0cd315a545f93c451229526101caa6aef78c020bb22c12c52678c377d4a
028bdb18d48b99a27a227b2733a5d46f357edc3c5252528e6b6d0a57798a283b3733aea47fa64d9f483208ee52e5013c5671ef7f2b87125576646940f18d11e4
vtk_add_test_python(
labeledContours.py
multiPolyDataItems.py
polyDataItemDrawHints.py
)
#!/usr/bin/env python
import sys
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
contourValues = [
84.0,
105.0,
126.0,
148.0,
169.0,
191.0,
212.0,
233.0,
255.0,
276.0
]
wavelet = vtk.vtkRTAnalyticSource()
plane = vtk.vtkPlane()
plane.SetOrigin(0.0, 0.0, 0.0)
plane.SetNormal(0.0, 0.0, 1.0)
planeCut = vtk.vtkCutter()
planeCut.SetInputConnection(wavelet.GetOutputPort())
planeCut.SetCutFunction(plane)
contours = vtk.vtkContourFilter()
contours.SetInputConnection(planeCut.GetOutputPort())
contours.SetNumberOfContours(len(contourValues))
contours.SetComputeScalars(True)
for idx in range(len(contourValues)):
contours.SetValue(idx, contourValues[idx])
stripper = vtk.vtkStripper()
stripper.SetInputConnection(contours.GetOutputPort())
stripper.Update()
pd = stripper.GetOutput()
pdbounds = pd.GetBounds()
lut = vtk.vtkLookupTable()
lut.SetNumberOfColors(len(contourValues))
lut.SetRange(84.0, 277.0)
lut.Build()
tprops = vtk.vtkTextPropertyCollection()
for i in range(len(contourValues)):
textProp = vtk.vtkTextProperty()
col = [0.0, 0.0, 0.0]
lut.GetColor(contourValues[i], col)
textProp.SetColor(*col)
textProp.SetBackgroundColor((1.0, 1.0, 1.0))
textProp.SetFrame(0)
textProp.SetFontSize(16)
textProp.SetBold(0)
textProp.SetItalic(0)
textProp.SetShadow(0)
textProp.SetJustification(0)
textProp.SetBackgroundOpacity(1.0)
textProp.SetVerticalJustification(1)
textProp.SetUseTightBoundingBox(0)
textProp.SetOrientation(0.0)
textProp.SetLineSpacing(1.1)
textProp.SetLineOffset(0.0)
tprops.AddItem(textProp)
tpropMap = vtk.vtkDoubleArray()
tpropMap.SetNumberOfComponents(1)
for i in range(len(contourValues)):
tpropMap.InsertNextTypedTuple([contourValues[i]])
mappedColors = vtk.vtkUnsignedCharArray()
mappedColors.SetNumberOfComponents(4)
for i in range(pd.GetNumberOfPoints()):
mappedColors.InsertNextTypedTuple([0, 0, 0, 255])
item = vtk.vtkLabeledContourPolyDataItem()
item.SetPolyData(pd)
item.SetTextProperties(tprops)
item.SetTextPropertyMapping(tpropMap)
item.SetLabelVisibility(1)
item.SetSkipDistance(20.0)
item.SetScalarMode(vtk.VTK_SCALAR_MODE_USE_POINT_DATA)
item.SetMappedColors(mappedColors)
width = 600
height = 600
view = vtk.vtkContextView()
renWin = view.GetRenderWindow()
renWin.SetSize(width, height)
area = vtk.vtkInteractiveArea()
view.GetScene().AddItem(area)
xmin = pdbounds[0]
ymin = pdbounds[2]
dataWidth = pdbounds[1] - pdbounds[0]
dataHeight = pdbounds[3] - pdbounds[2]
drawAreaBounds = vtk.vtkRectd(xmin, ymin, dataWidth, dataHeight)
vp = [0.0, 1.0, 0.0, 1.0]
screenGeometry = vtk.vtkRecti(int(vp[0] * width),
int(vp[2] * height),
int((vp[1] - vp[0]) * width),
int((vp[3] - vp[2]) * height))
area.SetDrawAreaBounds(drawAreaBounds)
area.SetGeometry(screenGeometry)
area.SetFillViewport(False)
area.SetShowGrid(False)
axisLeft = area.GetAxis(vtk.vtkAxis.LEFT)
axisRight = area.GetAxis(vtk.vtkAxis.RIGHT)
axisBottom = area.GetAxis(vtk.vtkAxis.BOTTOM)
axisTop = area.GetAxis(vtk.vtkAxis.TOP)
axisTop.SetVisible(False)
axisRight.SetVisible(False)
axisLeft.SetVisible(False)
axisBottom.SetVisible(False)
axisTop.SetMargins(0, 0)
axisRight.SetMargins(0, 0)
axisLeft.SetMargins(0, 0)
axisBottom.SetMargins(0, 0)
renWin.Render()
area.GetDrawAreaItem().AddItem(item)
renWin.Render()
......@@ -9,6 +9,7 @@ vtk_module(vtkRenderingContext2D
KIT
vtkRendering
DEPENDS
vtkCommonSystem
vtkCommonCore
vtkCommonDataModel
vtkCommonExecutionModel
......
This diff is collapsed.
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLabeledContourPolyDataItem.h
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.
=========================================================================*/
/**
* @class vtkLabeledContourPolyDataItem
* @brief Filter that translate a vtkPolyData 2D mesh into vtkContextItems.
*
* @warning
* The input vtkPolyData should be a 2D mesh.
*
*/
#ifndef vtkLabeledContourPolyDataItem_h
#define vtkLabeledContourPolyDataItem_h
#include "vtkRect.h" // For vtkRect/vtkVector/vtkTuple
#include "vtkRenderingContext2DModule.h" // For export macro
#include "vtkPolyDataItem.h"
#include "vtkSmartPointer.h" // For vtkSmartPointer
class vtkActor;
class vtkContext2D;
class vtkDoubleArray;
class vtkRenderer;
class vtkTextActor3D;
class vtkTextProperty;
class vtkTextPropertyCollection;
struct LabelHelper;
class VTKRENDERINGCONTEXT2D_EXPORT vtkLabeledContourPolyDataItem : public vtkPolyDataItem
{
public:
vtkTypeMacro(vtkLabeledContourPolyDataItem, vtkPolyDataItem);
void PrintSelf(ostream& os, vtkIndent indent) override;
static vtkLabeledContourPolyDataItem *New();
/**
* Paint event for the item.
*/
bool Paint(vtkContext2D* painter) override;
/**
* The text property used to label the lines. Note that both vertical and
* horizontal justifications will be reset to "Centered" prior to rendering.
* @note This is a convenience method that clears TextProperties and inserts
* the argument as the only property in the collection.
* @sa SetTextProperties
*/
virtual void SetTextProperty(vtkTextProperty *tprop);
//@{
/**
* The text properties used to label the lines. Note that both vertical and
* horizontal justifications will be reset to "Centered" prior to rendering.
* If the TextPropertyMapping array exists, then it is used to identify which
* text property to use for each label as follows: If the scalar value of a
* line is found in the mapping, the index of the value in mapping is used to
* lookup the text property in the collection. If there are more mapping
* values than properties, the properties are looped through until the
* mapping is exhausted.
* Lines with scalar values missing from the mapping are assigned text
* properties in a round-robin fashion starting from the beginning of the
* collection, repeating from the start of the collection as necessary.
* @sa SetTextProperty
* @sa SetTextPropertyMapping
*/
virtual void SetTextProperties(vtkTextPropertyCollection *coll);
virtual vtkTextPropertyCollection *GetTextProperties();
//@}
//@{
/**
* Values in this array correspond to vtkTextProperty objects in the
* TextProperties collection. If a contour line's scalar value exists in
* this array, the corresponding text property is used for the label.
* See SetTextProperties for more information.
*/
virtual vtkDoubleArray* GetTextPropertyMapping();
virtual void SetTextPropertyMapping(vtkDoubleArray *mapping);
//@}
//@{
/**
* If true, labels will be placed and drawn during rendering. Otherwise,
* only the mapper returned by GetPolyDataMapper() will be rendered.
* The default is to draw labels.
*/
vtkSetMacro(LabelVisibility, bool)
vtkGetMacro(LabelVisibility, bool)
vtkBooleanMacro(LabelVisibility, bool)
//@}
//@{
/**
* Ensure that there are at least SkipDistance pixels between labels. This
* is only enforced on labels along the same line. The default is 0.
*/
vtkSetMacro(SkipDistance, double)
vtkGetMacro(SkipDistance, double)
//@}
protected:
vtkLabeledContourPolyDataItem();
~vtkLabeledContourPolyDataItem() override;
virtual void ComputeBounds();
void Reset();
bool CheckInputs();
bool CheckRebuild();
bool PrepareRender();
bool PlaceLabels();
bool ResolveLabels();
virtual bool CreateLabels();
bool RenderBackgrounds(vtkContext2D* painter);
bool RenderLabels(vtkContext2D* painter);
bool AllocateTextActors(vtkIdType num);
bool FreeTextActors();
double SkipDistance;
bool LabelVisibility;
vtkIdType NumberOfTextActors;
vtkIdType NumberOfUsedTextActors;
vtkTextActor3D **TextActors;
LabelHelper** LabelHelpers;
vtkSmartPointer<vtkTextPropertyCollection> TextProperties;
vtkSmartPointer<vtkDoubleArray> TextPropertyMapping;
vtkTimeStamp LabelBuildTime;
private:
vtkLabeledContourPolyDataItem(const vtkLabeledContourPolyDataItem&) = delete;
void operator=(const vtkLabeledContourPolyDataItem&) = delete;
struct Private;
Private *Internal;
};
#endif
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