VTK vtkOBBTree 有向包围盒

news/2024/11/19 16:27:23/

vtkOBBTree  有向包围盒

vtkOBBTree是一个包围盒的树,它将体的每个cell分割到每个小的包围盒中,由SetNumberOfBuckets确定每个盒中放多少个 Cell。建立一个vtkOBBTree要先设定DataSet,SetDataSet。然后调用BuildLocator。包围盒精细程度由递归深度 (MaxLevel)以及预先设置的NumberOfBuckets决定。

使用场景:

可以用vtkOBBTree和直线、三角形甚至是另一个vtkOBBTree做相交检测、运算,碰撞检测;

可以用来,获得直线与多边形数据的交点;用来求最近点;等;

1.二个vtkOBBTree 求交:

     vtkOBBTree * targetTree = vtkOBBTree::New();targetTree->SetDataSet(targetPoly);targetTree->BuildLocator();vtkOBBTree * clipTree = vtkOBBTree::New();clipTree->SetDataSet(clipPoly);clipTree->BuildLocator();//使用包围盒求交 ,求交部分在OBBNodeIntersected中。NULL表示不做任何转换clipTree->IntersectWithOBBTree(targetTree,NULL,OBBNodeIntersected,this);//OBBNodeIntersected函数的定义:static int OBBNodeIntersected(vtkOBBNode *, vtkOBBNode *, vtkMatrix4x4 *,void *); 

2.官方样例:OBBTreeExtractCells.cxx

使用 vtkOBBTree 

IntersectWithLine

获取 线和 包围盒的 交点和 交点所在Cell;

vtkOBBTree 返回一条线和数据集的所有交点。 如果要最近的相交,则必须手 动找到它。 在此示例中,我们创建一个球体,并将其与一条直线相交。

这里其实也可以用于求,鼠标点击 Pick 到的点;鼠标的2D点,转成 世界坐标,加上相机的点,再延长;也是一条线,求出最近的点  ;也就是鼠标 Pick 到的点;

 CODE

#include <vtkExtractCells.h>
#include <vtkIdList.h>
#include <vtkLine.h>
#include <vtkLineSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOBBTree.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>int main(int, char*[])
{vtkNew<vtkNamedColors> colors;vtkNew<vtkSphereSource> sphereSource;sphereSource->SetPhiResolution(7);sphereSource->SetThetaResolution(15);sphereSource->Update();// Create the locatorvtkNew<vtkOBBTree> tree;tree->SetDataSet(sphereSource->GetOutput());tree->BuildLocator();// Intersect the locator with the linedouble lineP0[3] = {-0.6, -0.6, -0.6};double lineP1[3] = {.6, .6, .6};vtkNew<vtkPoints> intersectPoints;vtkNew<vtkIdList> intersectCells;double tol = 1.e-8;tree->SetTolerance(tol);tree->IntersectWithLine(lineP0, lineP1, intersectPoints, intersectCells);std::cout << "NumPoints: " << intersectPoints->GetNumberOfPoints()<< std::endl;// Display list of intersectionsdouble intersection[3];for (int i = 0; i < intersectPoints->GetNumberOfPoints(); i++){intersectPoints->GetPoint(i, intersection);std::cout << "\tPoint Intersection " << i << ": " << intersection[0] << ", "<< intersection[1] << ", " << intersection[2] << std::endl;}std::cout << "NumCells: " << intersectCells->GetNumberOfIds() << std::endl;vtkIdType cellId;for (int i = 0; i < intersectCells->GetNumberOfIds(); i++){cellId = intersectCells->GetId(i);std::cout << "\tCellId " << i << ": " << cellId << std::endl;}// Render the line, sphere and intersected cellsvtkNew<vtkLineSource> lineSource;lineSource->SetPoint1(lineP0);lineSource->SetPoint2(lineP1);vtkNew<vtkPolyDataMapper> lineMapper;lineMapper->SetInputConnection(lineSource->GetOutputPort());vtkNew<vtkActor> lineActor;lineActor->SetMapper(lineMapper);vtkNew<vtkPolyDataMapper> sphereMapper;sphereMapper->SetInputConnection(sphereSource->GetOutputPort());vtkNew<vtkActor> sphereActor;sphereActor->SetMapper(sphereMapper);sphereActor->GetProperty()->SetRepresentationToWireframe();sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());vtkNew<vtkExtractCells> cellSource;cellSource->SetInputConnection(sphereSource->GetOutputPort());cellSource->SetCellList(intersectCells);vtkNew<vtkDataSetMapper> cellMapper;cellMapper->SetInputConnection(cellSource->GetOutputPort());vtkNew<vtkActor> cellActor;cellActor->SetMapper(cellMapper);cellActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());vtkNew<vtkRenderer> renderer;vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(renderer);vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;renderWindowInteractor->SetRenderWindow(renderWindow);renderer->AddActor(lineActor);renderer->AddActor(sphereActor);renderer->AddActor(cellActor);renderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());renderWindow->SetWindowName("OBBTreeExtractCells");renderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;
}

3.官方样例 包围盒可视化

 

 int maxLevel = 5;// Create the treevtkNew<vtkOBBTree> obbTree;obbTree->SetDataSet(polyData);obbTree->SetMaxLevel(maxLevel);obbTree->BuildLocator();double corner[3] = {0.0, 0.0, 0.0};double max[3] = {0.0, 0.0, 0.0};double mid[3] = {0.0, 0.0, 0.0};double min[3] = {0.0, 0.0, 0.0};double size[3] = {0.0, 0.0, 0.0};obbTree->ComputeOBB(polyData, corner, max, mid, min, size);// Initialize the representationvtkNew<vtkPolyData> polydata;obbTree->GenerateRepresentation(0, polydata);vtkNew<vtkPolyDataMapper> obbtreeMapper;obbtreeMapper->SetInputData(polydata);vtkNew<vtkActor> obbtreeActor;obbtreeActor->SetMapper(obbtreeMapper);obbtreeActor->GetProperty()->SetInterpolationToFlat();obbtreeActor->GetProperty()->SetOpacity(.5);obbtreeActor->GetProperty()->EdgeVisibilityOn();obbtreeActor->GetProperty()->SetColor(colors->GetColor4d("SpringGreen").GetData());


http://www.ppmy.cn/news/1039203.html

相关文章

04- 串口

串口 串口波特率的计算串口操作常用的库函数(省略函数入口参数)串口配置的一般步骤&#xff1a;串口的引脚配置时的模式&#xff1a;示例 串口 常用的寄存器&#xff1a;还有USART_CRx&#xff0c;x1/2/3&#xff08;控制寄存器&#xff0c;作用&#xff1a;相关的中断使能&…

JVM参数配置推荐

以下是一些常见的JVM参数配置推荐&#xff0c;适用于大多数应用程序&#xff1a; -Xms256m: 设置JVM初始分配的堆内存大小为256MB。 -Xmx1024m: 设置JVM最大可分配的堆内存大小为1024MB。 -Xmn512m: 设置新生代的大小为512MB。 -Xss1024k: 设置每个线程的堆栈大小为1024KB。 -…

pprof 三把刀

pprof 三把刀 看内存 go tool pprof http://127.0.0.1:6060/debug/pprof/heap?seconds30 看cpu go tool pprof http://127.0.0.1:6060/debug/pprof/profile?seconds30 看协程 go tool pprof http://localhost:6060/debug/pprof/goroutine 端口是自定义的&#xff0c;看看…

PAT 1013 Battle Over Cities

个人学习记录&#xff0c;代码难免不尽人意。 It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair a…

golang github.com/spf13/cast 库识别不了 自定义数据类型

以下代码运行不会是10&#xff0c;而是返回 0 package mainimport ("fmt""github.com/spf13/cast" )type UserNum int32func main() {var uNum UserNumuNum 10uNumint64 : cast.ToInt64(uNum)uNumint64E, err : cast.ToInt64E(uNum)fmt.Println(uNumin…

两个案例熟悉String的基本操作

1、第一个案例 Java语言规范要求完全相同的字符串字面量&#xff0c;应该包含同样的Unicode字符序列&#xff08;包含同一份码点序列的常量&#xff09;&#xff0c;并且必须是指向同一个String类实例。 package string; public class StringTest4 {public static void main(St…

【计算机网络篇】UDP协议

✅作者简介&#xff1a;大家好&#xff0c;我是小杨 &#x1f4c3;个人主页&#xff1a;「小杨」的csdn博客 &#x1f433;希望大家多多支持&#x1f970;一起进步呀&#xff01; UDP协议 1&#xff0c;UDP 简介 UDP&#xff08;User Datagram Protocol&#xff09;是一种无连…

探秘分布式大数据:融合专业洞见,燃起趣味火花,启迪玄幻思维

文章目录 一 数据导论二 大数据的诞生三 大数据概论3.1 大数据的5V特征3.2 大数据的工作核心 四 大数据软件生态4.1 数据存储软件4.2 数据计算软件4.3 数据传输软件 五 Apache Hadoop概述5.1 Apache Hadoop框架5.2 Hadoop的功能5.3 Hadoop的发展5.4 Hadoop发行版本 一 数据导论…