首页 > 其他分享 >VTK 判断一个 点 是否在一个模型 stl 内部 vtk 点是否在内部 表面 寻找最近点

VTK 判断一个 点 是否在一个模型 stl 内部 vtk 点是否在内部 表面 寻找最近点

时间:2023-10-15 13:02:14浏览次数:29  
标签:stl 0.0 vtk points renderer double include VTK vtkNew


判断 一个点 ,判断是否在风格 stl 模型内部,或表面:

目录

1.方案一:使用vtkCellLocator  FindClosestPoint 找到模型上距离给定点最近的一点,计算两点的距离 ,小于某一阈值 则认为此点在模型上;

2.方案二 使用 vtkKdTreePointLocator

3.方案三 使用 vtkSelectEnclosedPoints


1.方案一:使用vtkCellLocator  FindClosestPoint 找到模型上距离给定点最近的一点,计算两点的距离 ,小于某一阈值 则认为此点在模型上;

vtkCellLocator本身是一个octree。典型的用法就是求最近的单元或者点

bool CheckPointInsidePolyData(vtkPolyData * poly, double* pt)
{
	bool inside=false; 
	auto cellLocator = vtkSmartPointer<vtkCellLocator>::New();
	cellLocator->SetDataSet(poly);
	cellLocator->BuildLocator();
	auto assistCell = vtkSmartPointer<vtkGenericCell>::New();
	double closestPoint[3];//the coordinates of the closest point will be returned here
	double closestPointDist2; //the squared distance to the closest point will be returned here
	vtkIdType cellId; //the cell id of the cell containing the closest point will be returned here
	int subId=-1;
	cellLocator->FindClosestPoint(point, closestPoint, assistCell, cellId, subId, closestPointDist2);
 
	if (-1!= subId&&closestPointDist2 < 0.001)
	{
		return true;
	}
	return inside;
}

2.方案二 使用 vtkKdTreePointLocator

但这个只能找到最新的点,还需要自己判断一下距离

官方样例:

#include <vtkIdList.h>
#include <vtkKdTreePointLocator.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

int main(int, char*[])
{
  // Setup point coordinates
  double x[3] = {1.0, 0.0, 0.0};
  double y[3] = {0.0, 1.0, 0.0};
  double z[3] = {0.0, 0.0, 1.0};

  vtkNew<vtkPoints> points;
  points->InsertNextPoint(x);
  points->InsertNextPoint(y);
  points->InsertNextPoint(z);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  // Create the tree
  vtkNew<vtkKdTreePointLocator> kDTree;
  kDTree->SetDataSet(polydata);
  kDTree->BuildLocator();

  double testPoint[3] = {2.0, 0.0, 0.0};

  // Find the closest points to TestPoint
  vtkIdType iD = kDTree->FindClosestPoint(testPoint);
  std::cout << "The closest point is point " << iD << std::endl;

  // Get the coordinates of the closest point
  double closestPoint[3];
  kDTree->GetDataSet()->GetPoint(iD, closestPoint);
  std::cout << "Coordinates: " << closestPoint[0] << " " << closestPoint[1]
            << " " << closestPoint[2] << std::endl;

  return EXIT_SUCCESS;
}

3.方案三 使用 vtkSelectEnclosedPoints

vtkSelectEnclosedPoints类可以判断标记点是否在封闭表面内。
vtkSelectEnclosedPoints是一个Filter,它计算所有输入点以确定它们是否位于封闭曲面中。过滤器生成一个(0,1)掩码(以vtkDataArray的形式),指示点是在提供的曲面的外部(掩码值=0)还是内部(掩码值=1)(输出vtkDataArray的名称是“SelectedPoints”。)
运行过滤器后,可以通过调用IsInside(ptId)方法来查询点是否在内部/外部。

样例:判断三个点是否在立方体内

注意:在立方体边上的点,不属于在立方体内部;

VTK 判断一个 点 是否在一个模型 stl 内部 vtk 点是否在内部 表面 寻找最近点_ide

 CODE

#include <vtkVersion.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkCubeSource.h>
#include <vtkSmartPointer.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkIntArray.h>
#include <vtkDataArray.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

int main(int, char*[])
{

	vtkNew<vtkCubeSource> cubeSource;
	cubeSource->Update();

	vtkPolyData* cube = cubeSource->GetOutput();

	double testInside[3] = { 0.0, 0.0, 0.0 };
	double testOutside[3] = { 0.7, 0.0, 0.0 };
	double testBorderOutside[3] = { 0.5, 0.0, 0.0 };
	vtkNew<vtkPoints> points;
	points->InsertNextPoint(testInside);
	points->InsertNextPoint(testOutside);
	points->InsertNextPoint(testBorderOutside);

	vtkNew<vtkPolyData> pointsPolydata;
	pointsPolydata->SetPoints(points);

	//Points inside test
	vtkNew<vtkSelectEnclosedPoints> selectEnclosedPoints;
	selectEnclosedPoints->SetInputData(pointsPolydata);
	selectEnclosedPoints->SetSurfaceData(cube);
	selectEnclosedPoints->Update();

	for (unsigned int i = 0; i < 3; i++) {
		std::cout << "Point " << i << ": " << selectEnclosedPoints->IsInside(i) << std::endl;
	}

	vtkDataArray* insideArray = vtkDataArray::SafeDownCast(selectEnclosedPoints->GetOutput()->GetPointData()->GetArray("SelectedPoints"));

	for (vtkIdType i = 0; i < insideArray->GetNumberOfTuples(); i++) {
		std::cout << i << " : " << insideArray->GetComponent(i, 0) << std::endl;
	}
	//Cube mapper, actor
	vtkNew<vtkPolyDataMapper> cubeMapper;
	cubeMapper->SetInputConnection(cubeSource->GetOutputPort());

	vtkNew<vtkActor> cubeActor;
	cubeActor->SetMapper(cubeMapper);
	cubeActor->GetProperty()->SetOpacity(0.5);

	//First, apply vtkVertexGlyphFilter to make cells around points, vtk only render cells.
	vtkNew<vtkVertexGlyphFilter> vertexGlyphFilter;

	vertexGlyphFilter->AddInputData(pointsPolydata);
	vertexGlyphFilter->Update();

	vtkNew<vtkPolyDataMapper> pointsMapper;
	pointsMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());

	vtkNew<vtkActor> pointsActor;
	pointsActor->SetMapper(pointsMapper);
	pointsActor->GetProperty()->SetPointSize(5); 
	pointsActor->GetProperty()->SetColor(1.0, 0.0, 0);

	//Create a renderer, render window, and interactor
	vtkNew<vtkRenderer> renderer;
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->AddRenderer(renderer);
	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

	// Add the actor to the scene
	renderer->AddActor(cubeActor);
	renderer->AddActor(pointsActor);
	renderer->SetBackground(.0, 1, .0);

	renderWindow->Render();
	renderWindowInteractor->Start();
	return EXIT_SUCCESS;
}

输出:

Point 0: 1
Point 1: 0
Point 2: 0
0 : 1
1 : 0
2 : 0

标签:stl,0.0,vtk,points,renderer,double,include,VTK,vtkNew
From: https://blog.51cto.com/u_12389088/7871389

相关文章

  • stl(c++)
    1.vector定义: a.size()a.empty()a.clear()vector<int>::iteratorit=a.begin()迭代器(可类比于指针)前开后闭a.begin()a.end()是开始迭代器和最后一个元素的下一个迭代器a[0]=*a.begin()a.back()最后一个元素a.push_back()O(1)加入元素到末尾a.pop_back()删除最后一......
  • 5381: C++实验:STL之search
    描述  使用STL中的search函数,判断一个序列是否是另一个序列的子序列。部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。  C++intmain(){vector<int>vec1,vec2;intn,m,a;cin>>n>>m;while(n--){cin>>a;......
  • 5383: C++实验:STL之multimap
    描述  使用STL中的multimap记录用户的所有电话号码,yuyu想查询用户有多少个电话号码,crq则想查询时输出所有的号码。部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。 C++intmain(){ multimap<string,string>sm; stringname,phone; intn; cin>>......
  • P1457 [USACO2.1] 城堡 The Castle 题解
    分析感觉没有蓝题难度一道bfs题目,相较于大部分bfs题,它较为复杂,但分析一下还是很好水过的。建立墙时,可以用三维数组,\(wall_{~i,~j,~pos}\)表示第\(i\)行第\(j\)列\(pos\)方向有墙。观察发现,\(8=2^3,4=2^2,2=2^1,1=2^1\),于是可以用位运算快速储存。这里给出......
  • C++ - STL算法
    5STL-常用算法 概述:算法主要是由头文件<algorithm><functional><numeric>组成。 <algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数<funct......
  • 基于Java Swing和BouncyCastle的证书生成工具
    "Almostnoonewillrememberwhathehadjustnotinterested."-Nobody“几乎没有人会记得他所丝毫不感兴趣的事情。”——佚名0x00大纲目录0x00大纲0x01前言0x02技术选型0x03需求分析目标用户用户故事功能需求安全需求兼容需求性能需求0x04原型设计主窗体RSA根......
  • STL总结
       STL(StandardTemplateLibrary)里有很多组成部分,但是主要有三个,容器、迭代器和算法   容器用来管理某个特定对象的集合。每一种容器都有自己的优点和缺点,在项目中根据不同的需求,使用不同的容器。容器可以是数组、链表或者类字典。   迭代器用于遍历对象集合的元素......
  • 【webapp】JSTL(JSP Standard Tag Library)
    JSTL(JSPStandardTagLibrary)是一个标准的JSP标签库,提供了一组用于处理常见任务的标签和函数,以简化JSP页面的开发。以下是关于JSTL的使用方法:引入JSTL标签库:在使用JSTL之前,首先需要在JSP页面中引入JSTL标签库。可以通过<%@taglib%>指令来完成引入。JSTL标......
  • 基于hash_table对STL unordered系列容器的封装 #C++
    概述本文对hash_table进行封装,以模仿SGISTL对unordered系列容器进行简单实现,旨在加深对C++封装与泛型技法的体会与理解。阅读本文之前,建议先对哈希表进行学习。unordered_map与map一样,unordered_map的所有元素类型都是pair,pair的第一个成员为Key,第二个成员为Value。因为Key在任何......
  • C++ STL快速入门方法
    在数月之前的机试中第一次体验到STL的威力,因为自己本来一直在用C语言做开发,很多数据结构都是自己造的,比如链表、队列等,第一次接触C++STL后发现这些数据结构都已经给我提供好了,我直接拿去调用就好了,真是超级方便。最近的项目中也遇到了STL一些容器,所以现在自己好好总结一下STL中......