[vtkusers] Visualizing normal vectors

mozendi mozendi at gmail.com
Sat Dec 15 15:02:58 EST 2018


I have some point cloud files in .xyz format. Each of these files contain
approximately 1million points and corresponding normal vectors for each
point. Each line of .xyz files belongs to one point and each line contains
cartesian coordinates (x,y, and z) and normal vetor (nx,ny, and nz). My
objective is to show points and their normal vectors. I can read .xyz files
successfully and show points successfully. However, I can not show normal
vectors successfully. I tried to solve the problem using vtkGlyph3D class as
shown in some examples of Kitware but every attempt was failure for me. My
code is shown below. 
Could you please help me about solving this problem? I am looking forward to
hearing from you. Thanks in advance.

#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;
} 



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list