1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkArrayPrint.txx
5 
6 -------------------------------------------------------------------------
7   Copyright 2008 Sandia Corporation.
8   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9   the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13   All rights reserved.
14   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16      This software is distributed WITHOUT ANY WARRANTY; without even
17      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18      PURPOSE.  See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
22 #ifndef vtkArrayPrint_txx
23 #define vtkArrayPrint_txx
24 
25 #include "vtkArrayCoordinates.h"
26 #include <algorithm>
27 #include <iterator>
28 
29 template <typename T>
vtkPrintCoordinateFormat(ostream & stream,vtkTypedArray<T> * array)30 void vtkPrintCoordinateFormat(ostream& stream, vtkTypedArray<T>* array)
31 {
32   if (!array)
33   {
34     vtkGenericWarningMacro(<< "vtkPrintCoordinateFormat() requires a non-nullptr array as input.");
35     return;
36   }
37 
38   const vtkArrayExtents extents = array->GetExtents();
39   const vtkIdType dimensions = array->GetDimensions();
40   const vtkIdType non_null_size = array->GetNonNullSize();
41 
42   for (vtkIdType i = 0; i != dimensions; ++i)
43     stream << extents[i] << " ";
44   stream << array->GetNonNullSize() << "\n";
45 
46   vtkArrayCoordinates coordinates;
47   for (vtkIdType n = 0; n != non_null_size; ++n)
48   {
49     array->GetCoordinatesN(n, coordinates);
50     for (vtkIdType i = 0; i != dimensions; ++i)
51       stream << coordinates[i] << " ";
52     stream << array->GetValueN(n) << "\n";
53   }
54 }
55 
56 template <typename T>
vtkPrintMatrixFormat(ostream & stream,vtkTypedArray<T> * matrix)57 void vtkPrintMatrixFormat(ostream& stream, vtkTypedArray<T>* matrix)
58 {
59   if (!matrix)
60   {
61     vtkGenericWarningMacro(<< "vtkPrintMatrixFormat() requires a non-nullptr array as input.");
62     return;
63   }
64 
65   if (matrix->GetDimensions() != 2)
66   {
67     vtkGenericWarningMacro(<< "vtkPrintMatrixFormat() requires a matrix (2-way array) as input.");
68     return;
69   }
70 
71   const vtkArrayRange rows = matrix->GetExtent(0);
72   const vtkArrayRange columns = matrix->GetExtent(1);
73 
74   for (vtkIdType row = rows.GetBegin(); row != rows.GetEnd(); ++row)
75   {
76     for (vtkIdType column = columns.GetBegin(); column != columns.GetEnd(); ++column)
77     {
78       stream << matrix->GetValue(vtkArrayCoordinates(row, column)) << " ";
79     }
80     stream << "\n";
81   }
82 }
83 
84 template <typename T>
vtkPrintVectorFormat(ostream & stream,vtkTypedArray<T> * vector)85 void vtkPrintVectorFormat(ostream& stream, vtkTypedArray<T>* vector)
86 {
87   if (!vector)
88   {
89     vtkGenericWarningMacro(<< "vtkPrintVectorFormat() requires a non-nullptr array as input.");
90     return;
91   }
92 
93   if (vector->GetDimensions() != 1)
94   {
95     vtkGenericWarningMacro(<< "vtkPrintVectorFormat() requires a vector (1-way array) as input.");
96     return;
97   }
98 
99   const vtkArrayRange rows = vector->GetExtent(0);
100 
101   for (vtkIdType row = rows.GetBegin(); row != rows.GetEnd(); ++row)
102   {
103     stream << vector->GetValue(vtkArrayCoordinates(row)) << "\n";
104   }
105 }
106 
107 #endif
108