VTK/Examples/Cxx/Math/VectorDot

From KitwarePublic

Jump to: navigation, search

Note that the filter maps the values to a scalar range. In the example, the values of the dot products are

1, .707, 0

The filter outputs

1, .414, -1

because the default scalar range that the filter maps the values to is [-1,1].

VectorDot.cxx

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkVectorDot.h>
 
int main(int, char *[])
{
  // Generate data
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0,0,0);
  points->InsertNextPoint(1,0,0);
  points->InsertNextPoint(2,0,0);
 
  vtkSmartPointer<vtkPolyData> polydata =
    vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
 
  // Add normals
  vtkSmartPointer<vtkFloatArray> normals =
    vtkSmartPointer<vtkFloatArray>::New();
  normals->SetNumberOfComponents(3);
  normals->SetName("Normals");
 
  float n0[3] = {1,0,0};
  float n1[3] = {1,0,0};
  float n2[3] = {1,0,0};
  normals->InsertNextTupleValue(n0);
  normals->InsertNextTupleValue(n1);
  normals->InsertNextTupleValue(n2);
 
  polydata->GetPointData()->SetNormals(normals);
 
  // Add vectors
  vtkSmartPointer<vtkFloatArray> vectors =
    vtkSmartPointer<vtkFloatArray>::New();
  vectors->SetNumberOfComponents(3);
  vectors->SetName("Vectors");
 
  float v0[3] = {1,0,0};
  float v1[3] = {.707,.707,0};
  float v2[3] = {0,1,0};
  vectors->InsertNextTupleValue(v0);
  vectors->InsertNextTupleValue(v1);
  vectors->InsertNextTupleValue(v2);
 
  polydata->GetPointData()->SetVectors(vectors);
 
  // Compute the dot products between normals and vectors
  vtkSmartPointer<vtkVectorDot> vectorDot =
      vtkSmartPointer<vtkVectorDot>::New();
#if VTK_MAJOR_VERSION <= 5
  vectorDot->SetInputConnection(polydata->GetProducerPort());
#else
  vectorDot->SetInputData(polydata);
#endif
  vectorDot->Update();
 
  // Get the results
  vtkFloatArray* scalars = vtkFloatArray::SafeDownCast ( vectorDot->GetOutput()->GetPointData()->GetScalars() );
 
  // Output the results
  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(VectorDot)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(VectorDot VectorDot.cxx)
TARGET_LINK_LIBRARIES(VectorDot vtkHybrid)
Personal tools