Difference between revisions of "VTK/Examples/Cxx/PolyData/FieldData"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(FieldData.cxx)
(Replaced content with "= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/FieldData FieldData] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =")
 
Line 1: Line 1:
Three types of data can be stored in a vtkPolyData object - PointData, CellData, and FieldData. For PointData, there must be a piece of data associated with each point (e.g. a temperature data array with the temperature at each point). For CellData, there must be a piece of data associated with each cell (e.g. the area of each triangle). For data that does not align with either points or cells, FieldData should be used. This is typically data that describes the dataset as a whole. An example could be the name of the dataset, or the center of mass of the points, etc.
+
= '''See [https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/FieldData FieldData] on the new [https://lorensen.github.io/VTKExamples/site/ VTKExamples website].''' =
 
 
This demo adds general data to a polydata (not at each point or cell). In this example, the center of mass of the data is stored.
 
 
 
==FieldData.cxx==
 
<source lang="cpp">
 
#include <vtkSmartPointer.h>
 
#include <vtkCellData.h>
 
#include <vtkMath.h>
 
#include <vtkDoubleArray.h>
 
#include <vtkPoints.h>
 
#include <vtkPolyData.h>
 
#include <vtkPointData.h>
 
#include <vtkXMLPolyDataReader.h>
 
#include <vtkFieldData.h>
 
#include <vtkSphereSource.h>
 
 
int main(int, char *[])
 
{
 
  vtkSmartPointer<vtkSphereSource> source =
 
    vtkSmartPointer<vtkSphereSource>::New();
 
  source->Update();
 
 
  // Extract the polydata
 
  vtkSmartPointer<vtkPolyData> polydata =
 
    vtkSmartPointer<vtkPolyData>::New();
 
  polydata->ShallowCopy(source->GetOutput());
 
 
  vtkSmartPointer<vtkDoubleArray> location =
 
    vtkSmartPointer<vtkDoubleArray>::New();
 
 
  // Create the data to store (here we just use (0,0,0))
 
  double locationValue[3] = {0,0,0};
 
 
  location->SetNumberOfComponents(3);
 
  location->SetName("MyDoubleArray");
 
  location->InsertNextTuple(locationValue);
 
  // The data is added to FIELD data (rather than POINT data as usual)
 
  polydata->GetFieldData()->AddArray(location);
 
 
  vtkSmartPointer<vtkIntArray> intValue =
 
    vtkSmartPointer<vtkIntArray>::New();
 
  intValue->SetNumberOfComponents(1);
 
  intValue->SetName("MyIntValue");
 
  intValue->InsertNextValue(5);
 
 
 
  polydata->GetFieldData()->AddArray(intValue);
 
 
  // Get the data back out
 
  vtkIntArray* retrievedArray = vtkIntArray::SafeDownCast(polydata->GetFieldData()->GetAbstractArray("MyIntValue"));
 
 
 
  std::cout << retrievedArray->GetValue(0) << std::endl;
 
 
 
  return EXIT_SUCCESS;
 
}
 
 
 
</source>
 
 
 
{{VTKCMakeLists|{{SUBPAGENAME}}}}
 
 
 
[[Category:VTKNoVizRequired]]
 

Latest revision as of 17:58, 9 September 2019

See FieldData on the new VTKExamples website.