File: ArrayBasics.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (90 lines) | stat: -rw-r--r-- 2,397 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <vtkArrayPrint.h>
#include <vtkDenseArray.h>
#include <vtkSparseArray.h>

int main(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
{
  ////////////////////////////////////////////////////////
  // Creating N-Way Arrays

  // Creating a dense array of 10 integers:
  vtkDenseArray<vtkIdType>* array = vtkDenseArray<vtkIdType>::New();
  array->Resize(10);

  // Creating a dense 20 x 30 matrix:
  vtkDenseArray<double>* matrix = vtkDenseArray<double>::New();
  matrix->Resize(20, 30);

  // Creating a sparse 10 x 20 x 30 x 40 tensor:
  vtkArrayExtents extents;
  extents.SetDimensions(4);
  extents[0] = vtkArrayRange(0, 10);
  extents[1] = vtkArrayRange(0, 20);
  extents[2] = vtkArrayRange(0, 30);
  extents[3] = vtkArrayRange(0, 40);
  vtkSparseArray<vtkIdType>* tensor = vtkSparseArray<vtkIdType>::New();
  tensor->Resize(extents);

  ////////////////////////////////////////////////////////
  // Initializing N-Way Arrays

  // Filling a dense array with ones:
  array->Fill(1);

  // Filling a dense matrix with zeros:
  matrix->Fill(0.0);

  // There's nothing to do for a sparse array - it's already empty.

  ////////////////////////////////////////////////////////
  // Assigning N-Way Array Values

  // Assign array value [5]:
  array->SetValue(5, 42);

  // Assign matrix value [4, 3]:
  matrix->SetValue(4, 3, 1970);

  // Assign tensor value [3, 7, 1, 2]:
  vtkArrayCoordinates coordinates;
  coordinates.SetDimensions(4);
  coordinates[0] = 3;
  coordinates[1] = 7;
  coordinates[2] = 1;
  coordinates[3] = 2;
  tensor->SetValue(coordinates, 38);

  ////////////////////////////////////////////////////////
  // Accessing N-Way Array Values

  // Access array value [5]:
  cout << "array[5]: " << array->GetValue(5) << "\n\n";

  // Access matrix value [4, 3]:
  cout << "matrix[4, 3]: " << matrix->GetValue(4, 3) << "\n\n";

  // Access tensor value [3, 7, 1, 2]:
  cout << "tensor[3, 7, 1, 2]: " << tensor->GetValue(coordinates) << "\n\n";

  ////////////////////////////////////////////////////////
  // Printing N-Way Arrays

  cout << "array:\n";
  vtkPrintVectorFormat(cout, array);
  cout << "\n";

  cout << "matrix:\n";
  vtkPrintMatrixFormat(cout, matrix);
  cout << "\n";

  cout << "tensor:\n";
  vtkPrintCoordinateFormat(cout, tensor);
  cout << "\n";

  // Cleanup array instances ...
  tensor->Delete();
  matrix->Delete();
  array->Delete();

  return 0;
}