VTK  9.7.1
vtkLabeledFormatter.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Private implementation header — not installed.
4 
5 #ifndef vtkLabeledFormatter_h
6 #define vtkLabeledFormatter_h
7 
8 #include "../vtkLabeledDataMapper.h"
9 #include "vtkArrayDispatch.h"
10 #include "vtkDataArray.h"
11 #include "vtkDataArrayRange.h"
12 #include "vtkIntArray.h"
13 #include "vtkStringArray.h"
14 #include "vtkStringFormatter.h"
15 
16 #include <string>
17 
18 VTK_ABI_NAMESPACE_BEGIN
19 
26 {
27  bool Valid = true;
28  vtkIntArray* TypeArr = nullptr;
29  int NumCurLabels = 0;
32  int NumComp = 0;
33  int ActiveComp = 0;
34  bool PointIdLabels = false;
36 };
37 
46 {
50 
52  : Self(self)
53  , TypeArr(typeArr)
54  , NumCurLabels(numCurLabels)
55  {
56  }
57 
58  virtual ~vtkLabeledFormatterInterface() = default;
59 
60  virtual void SetFormattedString(int i, const char* text) = 0;
61 
62  void operator()(const std::string& FormatString)
63  {
64  char formatedString[1024];
65  for (int i = 0; i < this->NumCurLabels; i++)
66  {
67  VTK_FORMAT_IF_ERROR_RETURN(auto result = vtk::format_to_n(formatedString,
68  sizeof(formatedString), vtk::runtime(FormatString), i);
69  *result.out = '\0', );
70  this->SetFormattedString(i, formatedString);
71  }
72  }
73 
75  {
76  };
77  template <class TArray>
78  void operator()(TArray* array, int activeComp, const std::string& FormatString, NumericComponent)
79  {
80  char formatedString[1024];
81  auto a = vtk::DataArrayTupleRange(array);
82  using ValueType = vtk::GetAPIType<TArray>;
83  for (int i = 0; i < this->NumCurLabels; i++)
84  {
86  auto result = vtk::format_to_n(formatedString, sizeof(formatedString),
87  vtk::runtime(FormatString), static_cast<ValueType>(a[i][activeComp]));
88  *result.out = '\0', );
89  this->SetFormattedString(i, formatedString);
90  }
91  }
92 
94  {
95  };
96  template <class TArray>
97  void operator()(TArray* array, int numComp, const std::string& FormatString, NumericVector)
98  {
99  char formatedString[1024];
100  std::string ResultString;
101  auto a = vtk::DataArrayTupleRange(array);
102  using ValueType = vtk::GetAPIType<TArray>;
103  for (int i = 0; i < this->NumCurLabels; i++)
104  {
105  ResultString = "(";
106  for (int j = 0; j < numComp; ++j)
107  {
109  auto result = vtk::format_to_n(formatedString, sizeof(formatedString),
110  vtk::runtime(FormatString), static_cast<ValueType>(a[i][j]));
111  *result.out = '\0', );
112  ResultString += formatedString;
113  if (j < (numComp - 1))
114  {
115  ResultString += this->Self->GetComponentSeparator();
116  }
117  else
118  {
119  ResultString += ')';
120  }
121  }
122  this->SetFormattedString(i, ResultString.c_str());
123  }
124  }
125 
126  void operator()(vtkStringArray* array, const std::string& FormatString)
127  {
128  char formatedString[1024];
129  for (int i = 0; i < this->NumCurLabels; i++)
130  {
131  const char* labelFormat = this->Self->GetLabelFormat();
132  if (!labelFormat || std::string_view(labelFormat).empty())
133  {
134  this->SetFormattedString(i, array->GetValue(i).c_str());
135  }
136  else
137  {
139  auto result = vtk::format_to_n(formatedString, sizeof(formatedString),
140  vtk::runtime(FormatString), static_cast<std::string&>(array->GetValue(i)));
141  *result.out = '\0', );
142  this->SetFormattedString(i, formatedString);
143  }
144  }
145  }
146 
148  {
149  if (input.PointIdLabels)
150  {
151  (*this)(input.FormatString);
152  }
153  else if (input.NumericData)
154  {
155  if (input.NumComp == 1)
156  {
157  if (!vtkArrayDispatch::Dispatch::Execute(
158  input.NumericData, *this, input.ActiveComp, input.FormatString, NumericComponent{}))
159  {
160  (*this)(input.NumericData, input.ActiveComp, input.FormatString, NumericComponent{});
161  }
162  }
163  else
164  {
165  if (!vtkArrayDispatch::Dispatch::Execute(
166  input.NumericData, *this, input.NumComp, input.FormatString, NumericVector{}))
167  {
168  (*this)(input.NumericData, input.NumComp, input.FormatString, NumericVector{});
169  }
170  }
171  }
172  else
173  {
174  (*this)(input.StringData, input.FormatString);
175  }
176  }
177 };
178 
179 VTK_ABI_NAMESPACE_END
180 #endif
Resolved label input for one dataset/graph, produced by vtkLabeledDataMapper::ResolveLabeledFormatter...
typename detail::GetAPITypeImpl< ArrayType, ForceValueTypeForVtkDataArray >::APIType GetAPIType
void operator()(TArray *array, int numComp, const std::string &FormatString, NumericVector)
virtual char * GetLabelFormat()
Set/Get the std::format or printf style format with which to print the labels.
a vtkAbstractArray subclass for strings
VTK_ITER_INLINE auto DataArrayTupleRange(const ArrayTypePtr &array, TupleIdType start=-1, TupleIdType end=-1) -> typename detail::SelectTupleRange< ArrayTypePtr, TupleSize >::type
Generate an stl and for-range compatible range of tuple iterators from a vtkDataArray.
vtkLabeledDataMapper * Self
void operator()(vtkStringArray *array, const std::string &FormatString)
virtual ~vtkLabeledFormatterInterface()=default
draw text labels at dataset points
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:35
Base class for label formatters.
void Dispatch(const vtkLabeledFormatterInput &input)
const ValueType & GetValue(vtkIdType id) const
Read-access of string at a particular index.
virtual void SetFormattedString(int i, const char *text)=0
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
void operator()(TArray *array, int activeComp, const std::string &FormatString, NumericComponent)
#define VTK_FORMAT_IF_ERROR_RETURN(formatCommand, returnValue)
vtkLabeledFormatterInterface(vtkLabeledDataMapper *self, vtkIntArray *typeArr, int numCurLabels)
void operator()(const std::string &FormatString)
virtual char GetComponentSeparator()
Set/Get the separator between components.
Optimized C++ utilities for formatting values to strings and files.
STL-compatible iterable ranges that provide access to vtkDataArray elements.