VTK包围盒,AABB包围盒

2024-03-13 19:12
文章标签 包围 vtk aabb

本文主要是介绍VTK包围盒,AABB包围盒,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 概述

包围盒是指能够包含三维图形的长方体,常常用于模型的碰撞检测。包围盒可以分成:轴对齐包围盒(AABB),有向包围盒(OBB)和凸包(Convex Hull)

今天就来实践一下AABB包围盒

2.绘制一个锥体

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>#include "vtkStructuredData.h"
#include "vtkCellArray.h"int main()
{setbuf(stdout, nullptr);vtkSmartPointer<vtkConeSource> cone =vtkSmartPointer<vtkConeSource>::New();cone->SetDirection(1, 1, 0);cone->Update();vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();//mapper->SetInputData( polydata );mapper->SetInputData(cone->GetOutput());vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(actor);renderer->SetBackground(0, 0, 0);vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);renderWindowInteractor->SetRenderWindow(renderWindow);renderer->ResetCamera();renderWindow->Render();renderWindowInteractor->Start();return 0;
}

3.绘制包围盒,主要是利用了vtkOutlineFilter

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>#include "vtkStructuredData.h"
#include "vtkCellArray.h"int main()
{setbuf(stdout, nullptr);vtkSmartPointer<vtkConeSource> cone =vtkSmartPointer<vtkConeSource>::New();cone->SetDirection(1, 1, 0);cone->Update();vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();//mapper->SetInputData( polydata );mapper->SetInputData(cone->GetOutput());vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);// --------------------- start to draw bounding box ------------------------vtkSmartPointer<vtkOutlineFilter> outlineFilter =vtkSmartPointer<vtkOutlineFilter>::New();outlineFilter->SetInputData(cone->GetOutput());vtkSmartPointer<vtkPolyDataMapper> outLinemapper =vtkSmartPointer<vtkPolyDataMapper>::New();outLinemapper->SetInputConnection(outlineFilter->GetOutputPort());vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();outlineActor->SetMapper(outLinemapper);outlineActor->GetProperty()->SetColor(1, 1, 1);// --------------------- Drawing bounding box end------------------------vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(actor);renderer->AddActor(outlineActor);renderer->SetBackground(0, 0, 0);vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);renderWindowInteractor->SetRenderWindow(renderWindow);renderer->ResetCamera();renderWindow->Render();renderWindowInteractor->Start();return 0;
}

效果图:

3.修改包围盒的颜色

1.vtkBoundingBox 使用这个类,获取包围盒8个点的坐标

2.vtkPoints,vtkCellArray。设置8个点的坐标信息,拓扑信息,生成对应的vtkPolyData。

3.修改颜色成红色

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>#include "vtkStructuredData.h"
#include "vtkCellArray.h"typedef struct PointStruct_ {PointStruct_(double x, double y, double z){point[0] = x;point[1] = y;point[2] = z;}double point[3];//double pointY;//double pointZ;
}PointStruct;int main()
{setbuf(stdout, nullptr);vtkSmartPointer<vtkConeSource> cone =vtkSmartPointer<vtkConeSource>::New();cone->SetDirection(1, 1, 0);cone->Update();vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();//mapper->SetInputData( polydata );mapper->SetInputData(cone->GetOutput());vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);// --------------------- start to draw bounding box ------------------------vtkSmartPointer<vtkOutlineFilter> outlineFilter =vtkSmartPointer<vtkOutlineFilter>::New();outlineFilter->SetInputData(cone->GetOutput());vtkSmartPointer<vtkPolyDataMapper> outLinemapper =vtkSmartPointer<vtkPolyDataMapper>::New();outLinemapper->SetInputConnection(outlineFilter->GetOutputPort());vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();outlineActor->SetMapper(outLinemapper);outlineActor->GetProperty()->SetColor(1, 1, 1);// --------------------- Drawing bounding box end------------------------vtkBoundingBox boundingBox;for (int i = 0; i < cone->GetOutput()->GetNumberOfPoints(); ++i){boundingBox.AddPoint(cone->GetOutput()->GetPoint(i));}double bounds[6] = { 0 };boundingBox.GetBounds(bounds);std::vector<PointStruct> pts;for (int i = 0; i < 2; ++i){for (int j = 2; j < 4; ++j){for (int k = 4; k < 6; ++k){pts.push_back(PointStruct(bounds[i], bounds[j], bounds[k]));}}}for (auto it : pts){// cout << it;cout << std::to_string(it.point[0]) << "," << std::to_string(it.point[1]) << "," << std::to_string(it.point[2]) << endl;}vtkSmartPointer<vtkPolyData> boundsPolydata =vtkSmartPointer<vtkPolyData>::New();vtkSmartPointer<vtkPoints> boundsPoints =vtkSmartPointer<vtkPoints>::New();for (int i = 0; i < 8; ++i){boundsPoints->InsertNextPoint(pts[i].point);}boundsPolydata->SetPoints(boundsPoints);vtkSmartPointer<vtkCellArray> cells =vtkSmartPointer<vtkCellArray>::New();vtkIdType cell[2] = { 0, 1 };cells->InsertNextCell(2, cell);cell[0] = 0; cell[1] = 2;cells->InsertNextCell(2, cell);cell[0] = 3; cell[1] = 2;cells->InsertNextCell(2, cell);cell[0] = 3; cell[1] = 1;cells->InsertNextCell(2, cell);cell[0] = 4; cell[1] = 5;cells->InsertNextCell(2, cell);cell[0] = 4; cell[1] = 6;cells->InsertNextCell(2, cell);cell[0] = 7; cell[1] = 5;cells->InsertNextCell(2, cell);cell[0] = 7; cell[1] = 6;cells->InsertNextCell(2, cell);cell[0] = 1; cell[1] = 5;cells->InsertNextCell(2, cell);cell[0] = 0; cell[1] = 4;cells->InsertNextCell(2, cell);cell[0] = 2; cell[1] = 6;cells->InsertNextCell(2, cell);cell[0] = 3; cell[1] = 7;cells->InsertNextCell(2, cell);boundsPolydata->SetLines(cells);vtkSmartPointer<vtkPolyDataMapper> boundsMapper =vtkSmartPointer<vtkPolyDataMapper>::New();boundsMapper->SetInputData(boundsPolydata);vtkSmartPointer<vtkActor> boundsActor =vtkSmartPointer<vtkActor>::New();boundsActor->SetMapper(boundsMapper);boundsActor->GetProperty()->SetColor(1, 0, 0);vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(actor);renderer->AddActor(outlineActor);renderer->AddActor(boundsActor);renderer->SetBackground(0, 0, 0);vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);renderWindowInteractor->SetRenderWindow(renderWindow);renderer->ResetCamera();renderWindow->Render();renderWindowInteractor->Start();return 0;
}

这篇关于VTK包围盒,AABB包围盒的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805917

相关文章

初步了解VTK装配体

VTK还不太了解,根据资料, vtk.vtkAssembly 是 VTK库中的一个重要类,允许通过将多个vtkActor对象组合在一起来创建复杂的3D模型。 import vtkimport mathfrom vtk.util.colors import *filenames = ["cylinder.stl","sphere.stl","torus.stl"]dt = 1.0renW

005:VTK世界坐标系中的相机和物体

VTK医学图像处理---世界坐标系中的相机和物体 左侧是成像结果                                                    右侧是世界坐标系中的相机与被观察物体 目录 VTK医学图像处理---世界坐标系中的相机和物体 简介 1 在三维空间中添加坐标系 2 世界坐标系中的相机 3 世界坐标系中vtkImageData的参数 总结:

004: VTK读入数据---vtkImageData详细说明

VTK医学图像处理---vtkImageData类 目录 VTK医学图像处理---vtkImageData类 简介: 1 Mricro软件的安装和使用 (1) Mricro安装 (2) Mricro转换DICOM为裸数据  2 从硬盘读取数据到vtkImageData 3 vtkImageData转RGB或RGBA格式 4 练习 总结 简介:         对于医

001:VTK的学习资料与方法

VTK 医学图像处理---VTK学习资料 简介:       本节主要介绍学习VTK的一些资料和学习方法,仅供参考,可以根据自己的实际情况来调整。学习资料主要以VTK官网提供的资料为主,不管对于入门还是深入研究都足够了;但为了让入手VTK的难度更低一点,所以在有了本系列博文。 VTK 医学图像处理---VTK学习资料 简介: 1 官网免费电子书(好像有中文翻译版,建议看原版)

VTK平面切割

文章目录 一、vtkClipPolyData二、CapClip三、SolidClip四、vtkClipClosedSurface 本文的主要内容:简单介绍VTK中通过平面切割模型的相关功能。 哪些人适合阅读本文:有一定VTK基础的人。 一、vtkClipPolyData VTK官网描述: vtkClipPolyData使用用户指定的隐函数剪切多边形数据,如果使用vtkP

用CMake构建CloudCompare工程出现VTK找不到的问题

勾选了QPCL_Plugin就可能会出现这个问题 这个问题是因为VTK的环境变量没配置好,仔细读CMake的提示 添加一个VTK_DIR的环境变量,值为包含VTKConfig.cmake的那个目录,哪个目录呢?你在VTK的目录下搜一下就知道啦, 最重要的一步,重启一下电脑,嘻嘻,就OK了.

PyQt创建VTK

1.定义和调用全局函数来创建VTK图形 import sysimport vtkfrom PyQt4 import QtCore, QtGuifrom vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractorclass MainWindow(QtGui.QMainWindow):def __init__

QT+VTK+Visual Studio 2010联合开发

QT+VTK+Visual Studio 2010联合开发 由于开发VTK程序是需要的GUI环境需求比较苛刻,传统的MFC框架在开发简单的GUI程序时还行,稍微复杂一点的程序就显得生硬。因此在开源社区里,开发VTK的GUI程序时,普遍采用QT。以下简单描述这三者的关系。 准备工作,这三者应该提前安装,建议遵循安装顺序为先Visual Studio 2010,再按装QT,再按装CMake,最后安

《完蛋,我被美女包围了》国产游戏的崛起与挑战:技术创新如何引领未来!!!

近年来,国产游戏行业迎来了快速发展和崛起的黄金时期。随着技术水平的不断提升,越来越多的国产游戏在国际市场上取得了显著的成绩。从画面渲染的精细程度到物理引擎的复杂度,再到AI技术的应用与服务器架构的优化,中国游戏开发者在多个领域展现出了强大的技术实力。以《完蛋我被美女包围了》为例,这款游戏通过独特的玩法和创新技术,在国产游戏市场上引发了广泛关注,成为近期的一大热门。然而,面对全球游戏市场的激

机器之心 | 五倍吞吐量,性能全面包围Transformer:新架构Mamba引爆AI圈

本文来源公众号“机器之心”,仅用于学术分享,侵权删,干货满满。 原文链接:五倍吞吐量,性能全面包围Transformer:新架构Mamba引爆AI圈 屹立不倒的 Transformer 迎来了一个强劲竞争者。 在别的领域,如果你想形容一个东西非常重要,你可能将其形容为「撑起了某领域的半壁江山」。但在 AI 大模型领域,Transformer 架构不能这么形容,因为它几乎撑起了「整个江山