可视化法线向量

时间:2018-12-15 19:48:34

标签: c++ graphics computer-vision vtk point-clouds

我有一些.xyz格式的点云文件。这些文件中的每一个都包含大约一百万个点以及每个点对应的法向向量。 .xyz文件的每一行都属于一个点,并且每一行都包含笛卡尔坐标(x,y和z)和法线向量(nx,ny和nz)。我的目标是显示点及其法线向量。我可以成功读取.xyz文件并成功显示点。但是,我无法成功显示法线向量。 我尝试使用vtkGlyph3D类解决该问题,如Kitware的一些示例所示,但每次尝试对我来说都是失败的。我的代码如下所示。 您能帮我解决这个问题吗?我期待着您的回音。 预先感谢

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkDelimitedTextReader.h>
#include <vtkDoubleArray.h>
#include <vtkTable.h>
#include <vtkPointData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.h>

int main(int argc, char* argv[])
{
    vtkSmartPointer<vtkDelimitedTextReader> reader = vtkSmartPointer<vtkDelimitedTextReader>::New();
    reader->SetFileName("points_and_normals.xyz");
    reader->DetectNumericColumnsOn();
    reader->SetFieldDelimiterCharacters(" ");
    reader->Update();

    vtkTable* table = reader->GetOutput();
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkDoubleArray> normals = vtkSmartPointer<vtkDoubleArray>::New();
    normals->SetNumberOfComponents(3); //3d normals (ie x,y,z)

    std::cout << "Table has " << table->GetNumberOfRows()
            << " rows." << std::endl;
    std::cout << "Table has " << table->GetNumberOfColumns()
            << " columns." << std::endl;

    for (vtkIdType i = 0; i < table->GetNumberOfRows(); i++)
    {

            points->InsertNextPoint((table->GetValue(i, 0)).ToDouble(),
                    (table->GetValue(i, 1)).ToDouble(),
                    (table->GetValue(i, 2)).ToDouble());

            double n[3];
            n[0] = (table->GetValue(i, 3)).ToDouble();
            n[1] = (table->GetValue(i, 4)).ToDouble();
            n[2] = (table->GetValue(i, 5)).ToDouble();
            normals->InsertNextTuple(n);
    }

    std::cout << "There are " << points->GetNumberOfPoints()
            << " points." << std::endl;

    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(points);
    polydata->GetPointData()->SetNormals(normals);
    vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
        glyphFilter->SetInputConnection(polydata->GetProducerPort());
#else
        glyphFilter->SetInputData(polydata);
#endif
    glyphFilter->Update();
    // Visualize
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(glyphFilter->GetOutputPort());
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetPointSize(3);
    actor->GetProperty()->SetColor(0, 0, 1);
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderer->AddActor(actor);
    renderer->SetBackground(.5, .5, .5);
    renderWindow->Render();
    renderWindowInteractor->Start();
    return EXIT_SUCCESS;
} 

0 个答案:

没有答案