Visualizing normal vectors

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Visualizing normal vectors

mozendi
Dear VTK Users,

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
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers