# IntersectTubes

VTKExamples/Cxx/PolyData/IntersectTubes

### Code

IntersectTubes.cxx

    #include <vtkCamera.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkActor.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkBooleanOperationPolyDataFilter.h>
    #include <vtkActor.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyData.h>
    #include <vtkButterflySubdivisionFilter.h>
    #include <vtkTriangleFilter.h>
    #include <vtkTubeFilter.h>
    #include <vtkLineSource.h>
    #include <vtkPolyData.h>
    #include <vtkButterflySubdivisionFilter.h>
    #include <vtkTriangleFilter.h>
    #include <vtkTubeFilter.h>
    #include <vtkLineSource.h>

    int main(int, char* argv[])
    {
      double w = 50;
      double h = 50;
      vtkSmartPointer<vtkLineSource> lineSource1 =
        vtkSmartPointer<vtkLineSource>::New();
      lineSource1->SetPoint1(0.0, -h / 2, 0.0);
      lineSource1->SetPoint2(0.0, h / 2, 0.0);
      lineSource1->SetResolution(50);
      lineSource1->Update();

      vtkSmartPointer<vtkTubeFilter> tube1 =
        vtkSmartPointer<vtkTubeFilter>::New();
      tube1->SetNumberOfSides(40);
      tube1->SetRadius(w); //default is .5
      tube1->SidesShareVerticesOn();
      tube1->CappingOn();
      tube1->SetInputData(lineSource1->GetOutput());
      tube1->Update();

      vtkSmartPointer<vtkTriangleFilter> triangleFilter1 =
        vtkSmartPointer<vtkTriangleFilter>::New();
      triangleFilter1->SetInputData(tube1->GetOutput());
      triangleFilter1->Update();

      vtkSmartPointer<vtkButterflySubdivisionFilter> subdivide1 =
        vtkSmartPointer<vtkButterflySubdivisionFilter>::New();
      subdivide1->SetInputData(triangleFilter1->GetOutput());
      subdivide1->SetNumberOfSubdivisions(1);
      subdivide1->Update();

      vtkSmartPointer<vtkPolyData> input1 = vtkSmartPointer<vtkPolyData>::New();
      input1->ShallowCopy(subdivide1->GetOutput());

      double eps = .1;
      vtkSmartPointer<vtkLineSource> lineSource2 =
        vtkSmartPointer<vtkLineSource>::New();
      lineSource2->SetPoint1(eps, -h / 2 - eps, eps);
      lineSource2->SetPoint2(eps, h / 2 - eps, eps);
      lineSource2->Update();
      lineSource2->SetResolution(100);

      vtkSmartPointer<vtkTubeFilter> tube2 =
        vtkSmartPointer<vtkTubeFilter>::New();
      tube2->SetNumberOfSides(4);
      tube2->SetRadius(w);
      tube2->SidesShareVerticesOn();
      tube2->CappingOn();
      tube2->SetInputData(lineSource2->GetOutput());
      tube2->Update();

      vtkSmartPointer<vtkTriangleFilter> triangleFilter2 =
        vtkSmartPointer<vtkTriangleFilter>::New();
      triangleFilter2->SetInputData(tube2->GetOutput());
      triangleFilter2->Update();

      vtkSmartPointer<vtkButterflySubdivisionFilter> subdivide2 =
        vtkSmartPointer<vtkButterflySubdivisionFilter>::New();
      subdivide2->SetInputData(triangleFilter2->GetOutput());
      subdivide2->SetNumberOfSubdivisions(1);
      subdivide2->Update();

      vtkSmartPointer<vtkPolyData> input2 = vtkSmartPointer<vtkPolyData>::New();
      input2->ShallowCopy(subdivide2->GetOutput());

      std::string operation("difference");

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

      vtkSmartPointer<vtkPolyDataMapper> input1Mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      input1Mapper->SetInputData(input1);
      input1Mapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> input1Actor =
        vtkSmartPointer<vtkActor>::New();
      input1Actor->SetMapper(input1Mapper);
      input1Actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Tomato").GetData());
      input1Actor->GetProperty()->SetSpecular(.6);
      input1Actor->GetProperty()->SetSpecularPower(20);
      input1Actor->SetPosition(-10.0 * w / 4, 0, 0);

      vtkSmartPointer<vtkPolyDataMapper> input2Mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      input2Mapper->SetInputData(input2);
      input2Mapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> input2Actor =
        vtkSmartPointer<vtkActor>::New();
      input2Actor->SetMapper(input2Mapper);
      input2Actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Mint").GetData());
      input2Actor->GetProperty()->SetSpecular(.6);
      input2Actor->GetProperty()->SetSpecularPower(20);
      input2Actor->SetPosition(10.0 * w / 4, 0, 0);

      vtkSmartPointer<vtkBooleanOperationPolyDataFilter> booleanOperation =
        vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
      if (operation == "union")
      {
        booleanOperation->SetOperationToUnion();
      }
      else if (operation == "intersection")
      {
        booleanOperation->SetOperationToIntersection();
      }
      else if (operation == "difference")
      {
        booleanOperation->SetOperationToDifference();
      }
      else
      {
        std::cout << "Unknown operation: " << operation << std::endl;
        return EXIT_FAILURE;
      }
      booleanOperation->SetInputData(0, input1);
      booleanOperation->SetInputData(1, input2);

      vtkSmartPointer<vtkPolyDataMapper> booleanOperationMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      booleanOperationMapper->SetInputConnection(booleanOperation->GetOutputPort());
      booleanOperationMapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> booleanOperationActor =
        vtkSmartPointer<vtkActor>::New();
      booleanOperationActor->SetMapper(booleanOperationMapper);
      booleanOperationActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
      booleanOperationActor->GetProperty()->SetSpecular(.6);
      booleanOperationActor->GetProperty()->SetSpecularPower(20);

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->AddViewProp(input1Actor);
      renderer->AddViewProp(input2Actor);
      renderer->AddViewProp(booleanOperationActor);
      renderer->SetBackground(colors->GetColor3d("Silver").GetData());
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      renderWindow->SetSize(640, 480);
      double viewUp[3] = { 0.0, 0.0, 1.0 };
      double position[3] = { 0.0, -1.0, 0.0 };
      renderer->GetActiveCamera()->SetFocalPoint(0.0, 0.0, 0.0);
      renderer->GetActiveCamera()->SetViewUp(viewUp);
      renderer->GetActiveCamera()->SetPosition(position);
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Dolly(1.5);
      renderer->ResetCameraClippingRange();

      vtkSmartPointer<vtkRenderWindowInteractor> renWinInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renWinInteractor->SetRenderWindow(renderWindow);

      renderWindow->Render();
      renWinInteractor->Start();

      return 0;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(IntersectTubes)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(IntersectTubes MACOSX_BUNDLE IntersectTubes.cxx )

    target_link_libraries(IntersectTubes ${VTK_LIBRARIES})

### Download and Build IntersectTubes

Click here to download IntersectTubes and its CMakeLists.txt file. Once the tarball IntersectTubes.tar has been downloaded and extracted,

    cd IntersectTubes/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:

    ./IntersectTubes

WINDOWS USERS

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