The inital scene is like the following image, here are an axes actor and an irregular 3D model.
We want to find all points that are on the model and face the plane which has normal (0, 1, 0) and origin (0, -10, 0).
My solution spend a lot time, maybe there is a better method.
We can visit every point and check if it pass through the surface of 3D model when it is projected to the big plane.
I create scalars for the found points, assign value 1 to show blue color, the rest point’s scalars have value 0 which means red.
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkAxesActor.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransform.h>
#include <vtkPlane.h>
#include <vtkProperty.h>
#include <vtkCylinderSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkTransformFilter.h>
#include <vtkOBBTree.h>
#include <vtkPointData.h>
#include "../share/tool.h"
#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();
using namespace std;
vtkPolyData *meshData = nullptr;
vtkSmartPointer<vtkOBBTree> obbTree;
void FindPointsOnSide( vtkSPtr<vtkPlane> plane )
{
vtkSPtrNew( scalars, vtkIntArray );
scalars->SetNumberOfTuples( meshData->GetNumberOfPoints() );
for( int i = 0 ; i < meshData->GetNumberOfPoints(); ++i )
{
PointStruct tmp( meshData->GetPoint( i ) );
PointStruct projectedPt;
plane->ProjectPoint( tmp.point, projectedPt.point );
vtkSPtrNew( intersectPts, vtkPoints );
obbTree->IntersectWithLine( tmp.point, projectedPt.point, intersectPts, nullptr );
if( intersectPts->GetNumberOfPoints() == 1 )
{
scalars->SetTuple1( i, 1 );
}
else {
scalars->SetTuple1( i, 0 );
}
}
meshData->GetPointData()->SetScalars( scalars );
meshData->GetPointData()->Modified();
}
int main()
{
vtkSPtrNew( reader, vtkXMLPolyDataReader );
reader->SetFileName( "/Users/weiyang/Desktop/example.vtp" );
reader->Update();
meshData = reader->GetOutput();
obbTree = vtkSmartPointer<vtkOBBTree>::New();
obbTree->SetDataSet( meshData );
obbTree->BuildLocator();
vtkSPtrNew( plane, vtkPlane );
plane->SetOrigin( 0, -10, 0 );
plane->SetNormal( 0, 1, 0 );
FindPointsOnSide( plane );
vtkSPtrNew( mapper, vtkPolyDataMapper );
mapper->SetInputData( meshData );
vtkSPtrNew( actor, vtkActor );
actor->SetMapper( mapper );
vtkSPtrNew( axesActor, vtkAxesActor );
vtkSPtrNew( renderer, vtkRenderer );
renderer->AddActor( actor );
renderer->AddActor( axesActor );
renderer->SetBackground( 0.2, 0.2, 0.2 );
vtkSPtrNew( renderWindow, vtkRenderWindow );
renderWindow->AddRenderer( renderer );
vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
renderWindowInteractor->SetRenderWindow( renderWindow );
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}