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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#ifndef vtkXMLUnstructuredPolyReader_txx
#define vtkXMLUnstructuredPolyReader_txx
#include "vtkArrayDispatch.h"
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkDataArrayRange.h"
#include "vtkMath.h"
#include "vtkMathUtilities.h"
#include "vtkSMPThreadLocal.h"
#include "vtkSMPTools.h"
#include "vtkTypeTraits.h"
#include <algorithm>
#include <array>
#include <cassert> // for assert()
#include <limits>
#include <tuple>
#include <utility>
#include <vector>
namespace vtkXMLUnstructuredDataReaderPrivate
{
VTK_ABI_NAMESPACE_BEGIN
template <typename ArrayT, typename APIType = typename vtk::GetAPIType<ArrayT>>
class FaceIdMinAndMax
{
private:
ArrayT* Array;
APIType ReducedRange[2];
vtkSMPThreadLocal<std::array<APIType, 2>> TLRange;
public:
FaceIdMinAndMax(ArrayT* array)
: Array(array)
{
this->ReducedRange[0] = vtkTypeTraits<APIType>::Max();
this->ReducedRange[1] = vtkTypeTraits<APIType>::Min();
}
// Help vtkSMPTools find Initialize() and Reduce()
void Initialize()
{
auto& range = this->TLRange.Local();
range[0] = vtkTypeTraits<APIType>::Max();
range[1] = vtkTypeTraits<APIType>::Min();
}
void Reduce()
{
for (auto itr = this->TLRange.begin(); itr != this->TLRange.end(); ++itr)
{
auto& range = *itr;
this->ReducedRange[0] = vtkMath::Min(this->ReducedRange[0], range[0]);
this->ReducedRange[1] = vtkMath::Max(this->ReducedRange[1], range[1]);
}
}
template <typename T>
void CopyRanges(T* ranges)
{
ranges[0] = static_cast<T>(this->ReducedRange[0]);
ranges[1] = static_cast<T>(this->ReducedRange[1]);
}
void operator()(vtkIdType begin, vtkIdType end)
{
const auto tuples = vtk::DataArrayValueRange<1>(this->Array, begin, end);
auto& range = FaceIdMinAndMax::TLRange.Local();
for (const APIType value : tuples)
{
if (value < 0)
{
// ignored for now
// but can be needed for compact storage in the future.
continue;
}
else
{
vtkMathUtilities::UpdateRange(range[0], range[1], value);
}
}
}
};
struct FaceIdRangeDispatchWrapper
{
bool Successful;
vtkIdType* Range;
FaceIdRangeDispatchWrapper(vtkIdType* range)
: Successful(false)
, Range(range)
{
}
template <typename ArrayT>
void operator()(ArrayT* array)
{
using T = vtk::GetAPIType<ArrayT>;
T range[2];
const vtkIdType numTuples = array->GetNumberOfTuples();
range[0] = vtkTypeTraits<T>::Max();
range[1] = vtkTypeTraits<T>::Min();
// do this after we make sure range is max to min
if (numTuples == 0)
{
this->Successful = false;
this->Range[0] = static_cast<vtkIdType>(range[0]);
this->Range[1] = static_cast<vtkIdType>(range[1]);
return;
}
FaceIdMinAndMax<ArrayT> MinAndMax(array);
vtkSMPTools::For(0, numTuples, MinAndMax);
MinAndMax.CopyRanges(range);
this->Range[0] = static_cast<vtkIdType>(range[0]);
this->Range[1] = static_cast<vtkIdType>(range[1]);
this->Successful = true;
}
};
bool FindPolyFaceRange(vtkIdType* ranges, vtkDataArray* polyhedron_faces)
{
using Dispatcher = vtkArrayDispatch::DispatchByArray<vtkCellArray::StorageArrayList>;
FaceIdRangeDispatchWrapper worker(ranges);
if (!Dispatcher::Execute(polyhedron_faces, worker))
{
worker(polyhedron_faces);
}
return worker.Successful;
}
template <typename ArrayT, typename APIType = typename vtk::GetAPIType<ArrayT>>
class Offsetter
{
private:
ArrayT* Array;
APIType Offset;
vtkSMPThreadLocal<std::array<APIType, 2>> TLRange;
public:
Offsetter(ArrayT* array, APIType value)
: Array(array)
, Offset(value)
{
}
void operator()(vtkIdType begin, vtkIdType end)
{
auto range = vtk::DataArrayValueRange<1>(this->Array, begin, end);
using RefType = typename decltype(range)::ReferenceType;
APIType val = this->Offset;
for (RefType value : range)
{
value -= val;
}
}
};
struct OffsettingWrapper
{
bool Successful;
bool UseStartValue;
vtkIdType Value;
OffsettingWrapper(vtkIdType val)
: Successful(false)
, UseStartValue(false)
, Value(val)
{
}
template <typename ArrayT>
vtk::GetAPIType<ArrayT> InitOffsetWithFirstValue(ArrayT* array)
{
return array->GetValue(0);
}
template <typename ArrayT>
void operator()(ArrayT* array)
{
using T = vtk::GetAPIType<ArrayT>;
T offsetValue;
const vtkIdType numTuples = array->GetNumberOfTuples();
if (numTuples == 0)
{
this->Successful = true;
return;
}
if (this->UseStartValue)
{
offsetValue = static_cast<T>(this->InitOffsetWithFirstValue(array));
this->Value = static_cast<vtkIdType>(offsetValue);
}
else
{
offsetValue = static_cast<T>(this->Value);
}
if (offsetValue == 0)
{
this->Successful = true;
return;
}
Offsetter<ArrayT> translator(array, offsetValue);
vtkSMPTools::For(0, numTuples, translator);
this->Successful = true;
}
};
template <>
vtk::GetAPIType<vtkDataArray> OffsettingWrapper::InitOffsetWithFirstValue(vtkDataArray* array)
{
using T = vtk::GetAPIType<vtkDataArray>;
T startValue;
startValue = static_cast<T>(array->GetTuple1(0));
return startValue;
}
bool RebaseOffset(vtkDataArray* arr)
{
using Dispatcher = vtkArrayDispatch::DispatchByArray<vtkCellArray::StorageArrayList>;
OffsettingWrapper worker(0);
worker.UseStartValue = true;
if (!Dispatcher::Execute(arr, worker))
{
worker(arr);
}
if (!Dispatcher::Execute(arr, worker))
{
worker(arr);
}
return worker.Successful;
}
bool RebasePolyFaces(vtkDataArray* arr, vtkIdType offset)
{
using Dispatcher = vtkArrayDispatch::DispatchByArray<vtkCellArray::StorageArrayList>;
OffsettingWrapper worker(offset);
worker.UseStartValue = false;
if (!Dispatcher::Execute(arr, worker))
{
worker(arr);
}
return worker.Successful;
}
VTK_ABI_NAMESPACE_END
} // end namespace vtkXMLUnstructuredDataReaderPrivate
#endif
// VTK-HeaderTest-Exclude: vtkXMLUnstructuredPolyReader.txx
|