ModifiedBSPTreeExtractCells
Repository source: ModifiedBSPTreeExtractCells
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ModifiedBSPTreeExtractCells.cxx
#include <vtkExtractCells.h>
#include <vtkIdList.h>
#include <vtkLineSource.h>
#include <vtkModifiedBSPTree.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetPhiResolution(7);
sphereSource->SetThetaResolution(15);
sphereSource->Update();
// Create the locator
vtkNew<vtkModifiedBSPTree> tree;
tree->SetDataSet(sphereSource->GetOutput());
// Intersect the locator with the line
double lineP0[3] = {-0.6, -0.6, -0.6};
double lineP1[3] = {0.6, 0.6, 0.6};
vtkNew<vtkPoints> intersectPoints;
vtkNew<vtkIdList> intersectCells;
double tol = 1.0e-8;
tree->IntersectWithLine(lineP0, lineP1, tol, intersectPoints, intersectCells);
std::cout << "NumPoints: " << intersectPoints->GetNumberOfPoints()
<< std::endl;
// Display list of intersections
double intersection[3];
for (int i = 0; i < intersectPoints->GetNumberOfPoints(); i++)
{
intersectPoints->GetPoint(i, intersection);
std::cout << "\tPoint Intersection " << i << ": " << intersection[0] << ", "
<< intersection[1] << ", " << intersection[2] << std::endl;
}
std::cout << "NumCells: " << intersectCells->GetNumberOfIds() << std::endl;
vtkIdType cellId;
for (int i = 0; i < intersectCells->GetNumberOfIds(); i++)
{
cellId = intersectCells->GetId(i);
std::cout << "\tCellId " << i << ": " << cellId << std::endl;
}
// Render the line, sphere and intersected cells
vtkNew<vtkLineSource> lineSource;
lineSource->SetPoint1(lineP0);
lineSource->SetPoint2(lineP1);
vtkNew<vtkPolyDataMapper> lineMapper;
lineMapper->SetInputConnection(lineSource->GetOutputPort());
vtkNew<vtkActor> lineActor;
lineActor->SetMapper(lineMapper);
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetRepresentationToWireframe();
sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
vtkNew<vtkExtractCells> cellSource;
cellSource->SetInputConnection(sphereSource->GetOutputPort());
cellSource->SetCellList(intersectCells);
vtkNew<vtkDataSetMapper> cellMapper;
cellMapper->SetInputConnection(cellSource->GetOutputPort());
vtkNew<vtkActor> cellActor;
cellActor->SetMapper(cellMapper);
cellActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(lineActor);
renderer->AddActor(sphereActor);
renderer->AddActor(cellActor);
renderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());
renderWindow->SetWindowName("ModifiedBSPTreeExtractCells");
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ModifiedBSPTreeExtractCells)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersCore
FiltersFlowPaths
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ModifiedBSPTreeExtractCells: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ModifiedBSPTreeExtractCells MACOSX_BUNDLE ModifiedBSPTreeExtractCells.cxx )
target_link_libraries(ModifiedBSPTreeExtractCells PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ModifiedBSPTreeExtractCells
MODULES ${VTK_LIBRARIES}
)
Download and Build ModifiedBSPTreeExtractCells¶
Click here to download ModifiedBSPTreeExtractCells and its CMakeLists.txt file. Once the tarball ModifiedBSPTreeExtractCells.tar has been downloaded and extracted,
cd ModifiedBSPTreeExtractCells/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:
./ModifiedBSPTreeExtractCells
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.