Lesson 17.1: Point Membership Classification with OpenCascade + VTK

//Dear OCC VTK enthusiasts
//A simple example that is the same as shown in Lesson 17, but in VTK.

[Lesson 17.1: Point Membership Classification with OpenCascade - YouTube]

//You will see A Cone from AIS-BRepPrimAPI_MakeCone represented in TopoDS_Shape
//You will see A Sphere from Import iges represented in TopoDS_Shape
//You will see A Box Import step represented in TopoDS_Shape
//You can choose which one will be boxed with points
// 
//Good luck  ^^...

#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeCone.hxx>
#include <IVtkTools_ShapeDataSource.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include<vtkAutoInit.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkInteractorStyleTrackballCamera.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkPolyDataMapper.h>
#include <IGESControl_Reader.hxx> 
#include <STEPCAFControl_Reader.hxx>
//Lesson 17
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <vtkCubeSource.h>
#include <vtkEllipseArcSource.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkAppendPolyData.h >
#include <vtkCleanPolyData.h >
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkBoundingBox.h>
//#include <vtkPointSource.h>
//VTK_MODULE_INIT(vtkRenderingOpenGl2); i didnt use this..
VTK_MODULE_INIT(vtkInteractionStyle);
int main()
{
	//TopoDS_ShapeSphere
	gp_Ax2 sphere_origin(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
	BRepPrimAPI_MakeCone mkCone(sphere_origin, 1.0,0.01,1);
	const TopoDS_Shape& TopoDS_ShapemkConeTMP = mkCone.Shape();
	TopoDS_Shape TopoDS_ShapeCONE = static_cast<TopoDS_Shape>(TopoDS_ShapemkConeTMP);
	gp_Trsf TSPHERE;
	TSPHERE.SetScaleFactor(0.9);
	TSPHERE.SetTranslationPart(gp_Vec(0, 0, 0));
	BRepBuilderAPI_Transform stepBRepTransformationTOPBOX(TopoDS_ShapeCONE, TSPHERE, Standard_True);
	TopoDS_ShapeCONE = stepBRepTransformationTOPBOX.Shape();

	//IGES Import
	IGESControl_Reader ReaderIGES;
	IFSelect_ReturnStatus statIGES = ReaderIGES.ReadFile("C:\\Users\\lilos\\Desktop\\WorkSpace\\sphere.iges");
	IFSelect_PrintCount modeIGES = IFSelect_ListByItem;
	ReaderIGES.PrintCheckLoad(Standard_True, modeIGES);
	ReaderIGES.TransferRoots();
	TopoDS_Shape TopoDS_ShapeIGES = ReaderIGES.OneShape();
	gp_Trsf igesTM;
	igesTM.SetScaleFactor(2);
	igesTM.SetTranslationPart(gp_Vec(0, 3, 0));
	BRepBuilderAPI_Transform stepBRepTransformationIGES(TopoDS_ShapeIGES, igesTM, Standard_True);
	TopoDS_ShapeIGES = stepBRepTransformationIGES.Shape();

	//STEP Import
	STEPControl_Reader readerSTEP;
	IFSelect_ReturnStatus statSTEP = readerSTEP.ReadFile("C:\\Users\\lilos\\Desktop\\WorkSpace\\cylinder.step");
	IFSelect_PrintCount modeSTEP = IFSelect_ListByItem;
	readerSTEP.PrintCheckLoad(Standard_False, modeSTEP);
	readerSTEP.TransferRoots();
	TopoDS_Shape TopoDS_ShapeSTEP = readerSTEP.OneShape();
	gp_Trsf stepTM;
	stepTM.SetScaleFactor(2);
	stepTM.SetTranslationPart(gp_Vec(0, -3, 0));
	BRepBuilderAPI_Transform stepBRepTransformationSTEP(TopoDS_ShapeSTEP, stepTM, Standard_True);
	TopoDS_ShapeSTEP = stepBRepTransformationSTEP.Shape();


	//VTKpointOnMeshes
	vtkNew<vtkPoints> VTKpointsOnMeshes;
	vtkNew<vtkCellArray> verticesForVTKpointsOnMeshes;
	vtkIdType pid[1];

	//vtkPolyData creation
	vtkNew<vtkPolyData> PolyDataVTKPointsOnMeshes;

	//vtkPolyData initialization
	PolyDataVTKPointsOnMeshes->SetPoints(VTKpointsOnMeshes);
	PolyDataVTKPointsOnMeshes->SetVerts(verticesForVTKpointsOnMeshes);
	
	//VTKpoint IGES, STEP TopoDS_ShapeBOX CALC   
	Bnd_Box aabb;
	//AABB ALGO Per TopoDS_Shape mesh  , Please choose the source you like to run and put it in next line
	BRepBndLib::Add(TopoDS_ShapeSTEP, aabb, true); //TopoDS_ShapeIGES  //TopoDS_ShapeSTEP //TopoDS_ShapeCONE
	//VTKpointIGES CALC per TopoDS_Shape mesh
	int density = 20;
	gp_XYZ Pmin = aabb.CornerMin().XYZ();
	gp_XYZ Pmax = aabb.CornerMax().XYZ();
	gp_XYZ D = Pmax - Pmin;
	double dim[3] = { D.X(),D.Y(),D.Z() };
	double mind = Min(dim[0], Min(dim[1], dim[2]));
	const double d = mind / density;
	int nslice[3] = {
		int(Round(dim[0] / d)) + 1 ,
		int(Round(dim[1] / d)) + 1 ,
		int(Round(dim[2] / d)) + 1 };
	for (int i = 0; i < nslice[0]; ++i)
		for (int j = 0; j < nslice[1]; ++j)
			for (int k = 0; k < nslice[2]; ++k)
			{
				gp_XYZ p = Pmin
					+ gp_XYZ(d * i, 0, 0)
					+ gp_XYZ(0, d * j, 0)
					+ gp_XYZ(0, 0, d * k);
				pid[0] = VTKpointsOnMeshes->InsertNextPoint(p.X(), p.Y(), p.Z());
				verticesForVTKpointsOnMeshes->InsertNextCell(1, pid);
			};
	
	//Append the meshes
	vtkNew<vtkAppendPolyData> appendFilter;
	appendFilter->AddInputData(PolyDataVTKPointsOnMeshes);

	//Remove any duplicate points.
	vtkNew<vtkCleanPolyData> cleanFilterForMapperVTKShapes;
	cleanFilterForMapperVTKShapes->SetInputConnection(appendFilter->GetOutputPort());
	cleanFilterForMapperVTKShapes->Update();

	//Create mappers
	vtkNew<vtkPolyDataMapper> mapperVTKShapes;
	vtkNew<vtkPolyDataMapper> mapperIGES;
	vtkNew<vtkPolyDataMapper> mapperSTEP;
	vtkNew<vtkPolyDataMapper> mapperTOPOCONE;

	//Create Iges Step Topo DataSource
	vtkNew<IVtkTools_ShapeDataSource> IGESSource;
	IGESSource->SetShape(new IVtkOCC_Shape(TopoDS_ShapeIGES));
	vtkNew<IVtkTools_ShapeDataSource> STEPSource;
	STEPSource->SetShape(new IVtkOCC_Shape(TopoDS_ShapeSTEP));
	vtkNew<IVtkTools_ShapeDataSource> TOPOCONESource;
	TOPOCONESource->SetShape(new IVtkOCC_Shape(TopoDS_ShapeCONE));

	//Mappers Intialize
	mapperVTKShapes->SetInputConnection(cleanFilterForMapperVTKShapes->GetOutputPort());
	mapperIGES->SetInputConnection(IGESSource->GetOutputPort());
	mapperSTEP->SetInputConnection(STEPSource->GetOutputPort());
	mapperTOPOCONE->SetInputConnection(TOPOCONESource->GetOutputPort());

	//Create Actors
	vtkNew<vtkActor> actorVTKShapes;
	vtkNew<vtkActor> actorIGES;
	vtkNew<vtkActor> actorSTEP;
	vtkNew<vtkActor> actorTOPOCONE;

	//Actors Intialize
	actorVTKShapes->SetMapper(mapperVTKShapes);
	actorIGES->SetMapper(mapperIGES);
	actorSTEP->SetMapper(mapperSTEP);
	actorTOPOCONE->SetMapper(mapperTOPOCONE);

	//////////////////////////////////////
	vtkNew<vtkRenderWindow> renwin;
	vtkNew<vtkRenderer> ren;
	renwin->AddRenderer(ren);
	vtkNew<vtkInteractorStyleTrackballCamera> istyle;
	vtkNew<vtkRenderWindowInteractor> iren;
	iren->SetRenderWindow(renwin);
	iren->SetInteractorStyle(istyle);

	/////////////////////////////
	ren->AddActor(actorVTKShapes);
	ren->AddActor(actorIGES);
	ren->AddActor(actorSTEP);
	ren->AddActor(actorTOPOCONE);

	/////////////////////////////
	renwin->Render();
	iren->Start();
	return 0;
}
1 Like