# TextureThreshold

VTKExamples/Cxx/Texture/TextureThreshold

### Description¶

Demonstrating texture thresholding applied to scalar data from a simulation of fluid flow.

There are three planes cutting the blunt fin with different thresholds set. From the left, the scalar threshold is set so that:

1) Only data with a scalar value greater than or equal to 1.5 is shown.

2) Only data with a scalar value less than or equal to 1.5 is shown.

3) Only data with a scalar value between 1.5 and 1.8 inclusive is shown.

Other Languages

See (Python)

Question

### Code¶

TextureThreshold.cxx

/*
Modified from VTK/Filters/Texture/Testing/Python/textureThreshold.py.

Demonstrating texture thresholding applied to scalar data from a simulation of
fluid flow->
There are three planes cutting the blunt fin with different thresholds set.
From the left, the scalar threshold is set so that:
1) Only data with a scalar value greater than or equal to 1.5 is shown.
2) Only data with a scalar value less than or equal to 1.5 is shown.
3) Only data with a scalar value between 1.5 and 1.8 inclusive is
shown.
*/

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredGrid.h>
#include <vtkStructuredGridGeometryFilter.h>
#include <vtkStructuredGridOutlineFilter.h>
#include <vtkTexture.h>
#include <vtkThresholdTextureCoords.h>

#include <array>
#include <string>
#include <vector>

int main(int argc, char* argv[])
{
if (argc < 4)
{
std::cout << "Usage: " << argv[0] << " filename1 filename2 filename3"
<< std::endl;
std::cout << "where: filename1 is bluntfinxyz.bin,\n"
<< "       filename2 is bluntfinq.bin and\n"
<< "       filename3 is texThres2.vtk." << std::endl;
return EXIT_FAILURE;
}

std::string dataFn1 = argv[1];
std::string dataFn2 = argv[2];
std::string textureFn = argv[3];

vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();

pl3d->SetXYZFileName(dataFn1.c_str());
pl3d->SetQFileName(dataFn2.c_str());
pl3d->SetScalarFunctionNumber(100); // Density
pl3d->SetVectorFunctionNumber(202); // Momentum
pl3d->Update();

vtkStructuredGrid* output =
dynamic_cast<vtkStructuredGrid*>(pl3d->GetOutput()->GetBlock(0));

// Make the wall (floor).
vtkSmartPointer<vtkStructuredGridGeometryFilter> wall =
vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
wall->SetInputData(output);
wall->SetExtent(0, 100, 0, 0, 0, 100);
vtkSmartPointer<vtkPolyDataMapper> wallMap =
vtkSmartPointer<vtkPolyDataMapper>::New();
wallMap->SetInputConnection(wall->GetOutputPort());
wallMap->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> wallActor = vtkSmartPointer<vtkActor>::New();
wallActor->SetMapper(wallMap);
wallActor->GetProperty()->SetColor(colors->GetColor3d("PeachPuff").GetData());

// Make the fin (rear wall).
vtkSmartPointer<vtkStructuredGridGeometryFilter> fin =
vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
fin->SetInputData(output);
fin->SetExtent(0, 100, 0, 100, 0, 0);
vtkSmartPointer<vtkPolyDataMapper> finMap =
vtkSmartPointer<vtkPolyDataMapper>::New();
finMap->SetInputConnection(fin->GetOutputPort());
finMap->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> finActor = vtkSmartPointer<vtkActor>::New();
finActor->SetMapper(finMap);
finActor->GetProperty()->SetColor(
colors->GetColor3d("DarkSlateGray").GetData());

// Get the texture.
tmap->SetFileName(textureFn.c_str());
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(tmap->GetOutputPort());
texture->InterpolateOff();
texture->RepeatOff();

// Create the rendering window, renderer, and interactive renderer.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);

// Make the planes to threshold and texture.
std::vector<vtkSmartPointer<vtkStructuredGridGeometryFilter>> plane;
std::vector<vtkSmartPointer<vtkThresholdTextureCoords>> thresh;
std::vector<vtkSmartPointer<vtkDataSetMapper>> planeMap;
std::vector<vtkSmartPointer<vtkActor>> planeActor;
// Define the extents of planes that we will use.
std::vector<std::array<int, 6>> planeExtents{{10, 10, 0, 100, 0, 100},
{30, 30, 0, 100, 0, 100},
{35, 35, 0, 100, 0, 100}};
// Now set up the pipeline.
for (unsigned int i = 0; i < planeExtents.size(); ++i)
{
plane.push_back(vtkSmartPointer<vtkStructuredGridGeometryFilter>::New());
plane[i]->SetInputData(output);
plane[i]->SetExtent(planeExtents[i].data());
thresh.push_back(vtkSmartPointer<vtkThresholdTextureCoords>::New());
thresh[i]->SetInputConnection(plane[i]->GetOutputPort());
thresh[i]->SetInputConnection(plane[i]->GetOutputPort());
// If you want an image similar to Fig 9-43(a) in the VTK textbook, set
// thresh[i]->ThresholdByUpper(1.5); for all planes.
switch (i)
{
case 0:
default:
thresh[i]->ThresholdByUpper(1.5);
break;
case 1:
thresh[i]->ThresholdByLower(1.5);
break;
case 2:
thresh[i]->ThresholdBetween(1.5, 1.8);
break;
}
planeMap.push_back(vtkSmartPointer<vtkDataSetMapper>::New());
planeMap[i]->SetInputConnection(thresh[i]->GetOutputPort());
planeMap[i]->SetScalarRange(output->GetScalarRange());
planeActor.push_back(vtkSmartPointer<vtkActor>::New());
planeActor[i]->SetMapper(planeMap[i]);
planeActor[i]->SetTexture(texture);
// The slight transparency gives a nice effect.
planeActor[i]->GetProperty()->SetOpacity(0.999);
}
// Get an outline of the data set for context.
vtkSmartPointer<vtkStructuredGridOutlineFilter> outline =
vtkSmartPointer<vtkStructuredGridOutlineFilter>::New();
outline->SetInputData(output);
vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();
outlineActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());
outlineActor->SetMapper(outlineMapper);

// Add the remaining actors to the renderer, set the background and size.
ren->SetBackground(colors->GetColor3d("MistyRose").GetData());
renWin->SetSize(512, 512);
vtkSmartPointer<vtkCamera> cam = vtkSmartPointer<vtkCamera>::New();
cam->SetClippingRange(1.51176, 75.5879);
cam->SetFocalPoint(2.33749, 2.96739, 3.61023);
cam->SetPosition(10.8787, 5.27346, 15.8687);
cam->SetViewAngle(30);
cam->SetViewUp(-0.0610856, 0.987798, -0.143262);
ren->SetActiveCamera(cam);

renWin->Render();
iren->Initialize();
iren->Start();

return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TextureThreshold)

find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonDataModel
vtkFiltersCore
vtkFiltersGeometry
vtkFiltersTexture
vtkIOLegacy
vtkIOParallel
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping TextureThreshold: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION:${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE}) add_executable(TextureThreshold MACOSX_BUNDLE TextureThreshold.cxx ) target_link_libraries(TextureThreshold PRIVATE${VTK_LIBRARIES})
else ()
# include all components
target_link_libraries(TextureThreshold PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS TextureThreshold MODULES${VTK_LIBRARIES}
)
endif ()


cd TextureThreshold/build


If VTK is installed:

cmake ..


If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..


Build the project:

make


and run it:

./TextureThreshold


WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.