VTK  9.6.20260613
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
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
18VTK_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;
35 std::string FormatString;
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 {
68 auto result = vtk::format_to_n(formatedString, sizeof(formatedString), 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), FormatString,
87 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(
110 formatedString, sizeof(formatedString), 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), FormatString,
140 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
179VTK_ABI_NAMESPACE_END
180#endif
dynamic, self-adjusting array of int
draw text labels at dataset points
virtual char * GetLabelFormat()
Set/Get the std::format or printf style format with which to print the labels.
virtual char GetComponentSeparator()
Set/Get the separator between components.
a vtkAbstractArray subclass for strings
const ValueType & GetValue(vtkIdType id) const
Read-access of string at a particular index.
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.
typename detail::GetAPITypeImpl< ArrayType, ForceValueTypeForVtkDataArray >::APIType GetAPIType
Resolved label input for one dataset/graph, produced by vtkLabeledDataMapper::ResolveLabeledFormatter...
void operator()(TArray *array, int numComp, const std::string &FormatString, NumericVector)
void operator()(TArray *array, int activeComp, const std::string &FormatString, NumericComponent)
void operator()(vtkStringArray *array, const std::string &FormatString)
virtual void SetFormattedString(int i, const char *text)=0
vtkLabeledFormatterInterface(vtkLabeledDataMapper *self, vtkIntArray *typeArr, int numCurLabels)
void operator()(const std::string &FormatString)
void Dispatch(const vtkLabeledFormatterInput &input)
virtual ~vtkLabeledFormatterInterface()=default
#define vtkDataArray
STL-compatible iterable ranges that provide access to vtkDataArray elements.
Optimized C++ utilities for formatting values to strings and files.
#define VTK_FORMAT_IF_ERROR_RETURN(formatCommand, returnValue)