vtk 根据3点坐标创建三角平面

#include <vtkProperty.h>
#include <vtkAutoInit.h>
#include <vtkObject.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#include <QDebug>
#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 <vtkActor2D.h>
#include <vtkRendererCollection.h>

using namespace std;

int main(int argc, char *argv[])
{
    vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray> cellArray = vtkSmartPointer<vtkCellArray>::New();

    vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    //-----------start create plane------------------
    points->InsertPoint(0, 0, 0, 0);
    points->InsertPoint(1, 1, 0, 0);
    points->InsertPoint(2, 0, 1, 0);
    polyData->SetPoints( points );
    vtkIdType cellId1[3] = { 0, 1, 2 };
    //Create a cell by specifying the number of points and an array of pointid's.  Return the cell id of the cell.
    cellArray->InsertNextCell(3, cellId1);
    polyData->SetPolys( cellArray );
    planeMapper->SetInputData( polyData );
    planeActor->SetMapper( planeMapper );
    planeActor->GetProperty()->SetColor( 1,0,0 );

    renderer->AddActor( planeActor );
    //-----------create plane finished------------------

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    renderer->GetActiveCamera()->ParallelProjectionOn();
    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();

    return 0;
}



VTK (Visualization Toolkit) 中,要获取STL模型的几何中心以及计算其表法向量,可以按照以下步骤操作: ### 获取 STL 模型的中心 为了得到整个 STL 模型的几何中心(质心),你可以利用 `vtkMassProperties` 类来帮助你。以下是基本流程: ```cpp // 加载 STL 文件到 polydata vtkNew<vtkSTLReader> reader; reader->SetFileName("your_model.stl"); reader->Update(); // 创建一个用于计算质量属性的对象 vtkNew<vtkMassProperties> massProps; massProps->SetInputConnection(reader->GetOutputPort()); massProps->Update(); double centerOfMass[3]; massProps->GetCenter(centerOfMass); // 获得重心位置 ``` ### 计算法向量 对于每个片(triangle)的单位法线向量,则可以通过 `vtkPolyDataNormals` 进行处理: ```cpp // 使用vtkPolyDataNormals生成所有顶处平滑后的平均法向量 vtkNew<vtkPolyDataNormals> normalsGenerator; normalsGenerator->SetInputConnection(reader->GetOutputPort()); normalsGenerator->ComputePointNormalsOn(); // 开启法线计算 normalsGenerator->ComputeCellNormalsOff(); // 关闭单元格(三角形)法线计算 (这里选择一种) normalsGenerator->Update(); // 如果需要获取某个特定三角形(cell) 的原始未加权平均的法线值可以直接访问cells数据结构并提取 vtkSmartPointer<vtkDoubleArray> cellNormals = vtkDoubleArray::SafeDownCast( normalsGenerator->GetOutput()->GetCellData()->GetNormals()); if (!cellNormals) { std::cerr << "No Cell Normals found!" << std::endl; } else { double normalVector[3]; for(vtkIdType i=0; i < normalsGenerator->GetOutput()->GetNumberOfCells(); ++i){ cellNormals->GetTuple(i, normalVector); // 现在这里normalVector就是第i个三角形单元对应的法向量了... } } // 若想获得单一位处经过插值得来的光滑化过后的近似“局部”方向信息则应查询points关联的数据集 vtkSmartPointer<vtkDoubleArray> pointNormals = vtkDoubleArray::SafeDownCast( normalsGenerator->GetOutput()->GetPointData()->GetNormals()); if(!pointNormals) { std::cerr << "No Point Normals found!" << std::endl; } else { double pointNormalAtVertex[3]; pointNormals->GetTuple(vertexIndexHere, pointNormalAtVertex); // 根据vertexIndexHere指定索引来取对应节上估计出来的朝向 } ``` 以上代码展示了如何通过 VTK 库读入 STL 数据,并分别求出物体的整体中心及其表上各元素(无论是单独考虑各个小平面还是综合考量临近影响下的某附近环境特性)的方向特征。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值