We often subtract mesh by other object to get the result we want.
The scene looks like the following image.

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkClipPolyData.h>
#include <vtkImplicitBoolean.h>
#include <vtkPlanes.h>
#include <vtkDataArray.h>
#include <vtkDoubleArray.h>
#include <vtkProperty.h>
#include <vtkBooleanOperationPolyDataFilter.h>

using namespace std;

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();

int main()
{
    vtkSPtrNew( sphere, vtkSphereSource );
    sphere->Update();

    vtkSPtrNew( cone, vtkConeSource );
    cone->Update();

    vtkSPtrNew( boolean, vtkBooleanOperationPolyDataFilter );
    boolean->SetInputData( 0, sphere->GetOutput() );
    boolean->SetInputData( 1, cone->GetOutput() );
    boolean->SetOperationToDifference();
    boolean->Update();

    vtkSPtrNew( mapper, vtkPolyDataMapper );
    mapper->SetInputData( boolean->GetOutput() );
    mapper->SetScalarVisibility( false );

    vtkSPtrNew( actor, vtkActor );
    actor->SetMapper( mapper );

    vtkSPtrNew( renderer, vtkRenderer );
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSPtrNew( renderWindow, vtkRenderWindow );
    renderWindow->AddRenderer( renderer );

    vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

There may will be a hole on the surface of mesh due to the subtraction operation.

If we make the sphere bigger to contains the cone perfectly, the subtraction will not works and the program gives us the error information.

Change the size of sphere.

    sphere->SetRadius( 5 );

The height(length_ of cone is 1 by default.

2021-09-20 12:48:02.146 (   0.020s) [           3578D]    vtkPointLocator.cxx:845    ERR| vtkPointLocator (0x7fea5f624150): No points to subdivide
2021-09-20 12:48:02.147 (   0.021s) [           3578D]vtkIntersectionPolyData:2410  WARN| No Intersection between objects 
2021-09-20 12:48:02.148 (   0.022s) [           3578D]vtkDistancePolyDataFilt:82     ERR| vtkDistancePolyDataFilter (0x7fea5f61ae50): No points/cells to operate on

Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

3D Model Viewer: add grid plane and convex hull.
Add google translate tool at right-bottom position.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x