VTK/Examples/Cxx/Math/VectorNorm

From KitwarePublic

Jump to: navigation, search

VectorNorm.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkVectorNorm.h>
 
int main(int, char *[])
{
  vtkSmartPointer<vtkPoints> points = 
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(1,2,3);
  points->InsertNextPoint(4,5,6);
 
  vtkSmartPointer<vtkPolyData> polydata = 
    vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
 
  vtkSmartPointer<vtkFloatArray> distances =
    vtkSmartPointer<vtkFloatArray>::New();
  distances->SetNumberOfComponents(3);
  distances->SetName("Distances");
 
  float v1[3] = {1,2,3};
  float v2[3] = {4,5,6};
  distances->InsertNextTupleValue(v1);
  distances->InsertNextTupleValue(v2);
 
  polydata->GetPointData()->SetVectors(distances);
 
  vtkSmartPointer<vtkVectorNorm> vectorNorm = 
    vtkSmartPointer<vtkVectorNorm>::New();
#if VTK_MAJOR_VERSION <= 5
  vectorNorm->SetInputConnection(polydata->GetProducerPort());
#else
  vectorNorm->SetInputData(polydata);
#endif
  vectorNorm->Update();
 
  vtkFloatArray* scalars = vtkFloatArray::SafeDownCast ( vectorNorm->GetOutput()->GetPointData()->GetScalars() );
 
  for(vtkIdType i = 0; i < scalars->GetNumberOfTuples(); i++)
    {
    std::cout << "Value " << i << " : " << scalars->GetValue(i) << std::endl;
    } 
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(VectorNorm)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(VectorNorm VectorNorm.cxx)
TARGET_LINK_LIBRARIES(VectorNorm vtkHybrid)
Personal tools