vtkTestHull将多个平面围成一个凸面体

1、vtkHull 

produce an n-sided convex hull

vtkHull is a filter which will produce an n-sided convex hull given a set of n planes. (The convex hull bounds the input polygonal data.) The hull is generated by squeezing the planes towards the input vtkPolyData, until the planes just touch the vtkPolyData. Then, the resulting planes are used to generate a polyhedron (i.e., hull) that is represented by triangles.

The n planes can be defined in a number of ways including 1) manually specifying each plane; 2) choosing the six face planes of the input's bounding box; 3) choosing the eight vertex planes of the input's bounding box; 4) choosing the twelve edge planes of the input's bounding box; and/or 5) using a recursively subdivided octahedron. Note that when specifying planes, the plane normals should point outside of the convex region.

The output of this filter can be used in combination with vtkLODActor to represent a levels-of-detail in the LOD hierarchy. Another use of this class is to manually specify the planes, and then generate the polyhedron from the planes (without squeezing the planes towards the input). The method GenerateHull() is used to do this.

Tests:
vtkHull (Tests)

下面分别是有50个和150个随机面围裹成的两个凸多面体

    

示例代码:

#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include <vtkVersion.h>
#include <vtkActor.h>
#include <vtkLine.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkFloatArray.h>

#include <vtkMath.h>
#include <vtkPlanes.h>
#include <vtkHull.h>

int main()
{
    vtkSmartPointer<vtkMath> mathObj=vtkSmartPointer<vtkMath>::New();
    vtkSmartPointer<vtkPoints> points=vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkFloatArray> normals=vtkSmartPointer<vtkFloatArray>::New();
    normals->SetNumberOfComponents(3);
    float radius,theta,phi,x,y,z;
    int numberOfPlanes=150;
///创建numberOfPlanes个随机平面,每个平面中心点坐标为(x,y,z),其法向量为(x,y,z),
///无论怎样随机,这些平面都是与半径为radius的球面相切,并包裹着该球面
    for(int i=0;i<numberOfPlanes;i++)
    {
         radius=1;
        theta=mathObj->Random(0,mathObj->Pi()*2);
        phi=mathObj->Random(0,mathObj->Pi());
        x=radius*sin(phi)*cos(theta);
        y=radius*sin(phi)*sin(theta);
        z=radius*cos(phi);
        points->InsertPoint(i,x,y,z);
        normals->InsertTuple3(i,x,y,z);
    }
    vtkSmartPointer<vtkPlanes>planes=vtkSmartPointer<vtkPlanes>::New();
    planes->SetPoints(points);
    planes->SetNormals(normals);
std::cout<<planes->GetNumberOfPlanes()<<std::endl;
    vtkSmartPointer<vtkHull>hull=vtkSmartPointer<vtkHull>::New();
    hull->SetPlanes(planes);
   vtkSmartPointer<vtkPolyData> pd=vtkSmartPointer<vtkPolyData>::New();
    hull->GenerateHull(pd,-1,1,-1,1,-1,1);

    vtkSmartPointer<vtkPolyDataMapper>hullMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
    hullMapper->SetInputData(pd);

    vtkSmartPointer<vtkActor>hullActor=vtkSmartPointer<vtkActor>::New();
    hullActor->SetMapper(hullMapper);
    //创建图形对象
    //创建显示窗口
    vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin=vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    ren1->AddActor(hullActor);
    renWin->AddRenderer(ren1);
    iren->SetRenderWindow(renWin);

    ren1->ResetCamera();
    iren->Initialize();
    iren->Start();

    return 0;
}

 

转载于:https://www.cnblogs.com/phoenixdsg/p/6274911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值