File: vtkToConstantArrayStrategy.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (136 lines) | stat: -rw-r--r-- 3,636 bytes parent folder | download | duplicates (7)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkToConstantArrayStrategy.h"

#include "vtkArrayDispatch.h"
#include "vtkConstantArray.h"
#include "vtkDataArrayRange.h"
#include "vtkObjectFactory.h"
#include "vtkSMPTools.h"
#include "vtkSmartPointer.h"

namespace
{
using Dispatch = vtkArrayDispatch::DispatchByArray<vtkArrayDispatch::AllArrays>;

template <typename ValueType>
struct ThreadedCheckingWorklet
{

  bool IsConstant = true;
  ValueType Value;
  double Tolerance;

  ThreadedCheckingWorklet(ValueType val, double tol)
    : Value(val)
    , Tolerance(tol)
  {
  }

  template <class Iterator>
  void operator()(Iterator begin, Iterator end)
  {
    for (auto it = begin; it != end; ++it)
    {
      if (std::abs(static_cast<double>(*it - this->Value)) > this->Tolerance)
      {
        this->IsConstant = false;
        break;
      }
    }
  }
};

struct CheckConstantWorklet
{
  template <typename ArrayT>
  void operator()(ArrayT* arr, double tol, bool& isConstant) const
  {
    using VType = vtk::GetAPIType<ArrayT>;
    auto range = vtk::DataArrayValueRange(arr);

    if (!range.size())
    {
      isConstant = false;
      return;
    }

    ThreadedCheckingWorklet<VType> tcWorker(range[0], tol);
    vtkSMPTools::For(range.begin(), range.end(), tcWorker);
    isConstant = tcWorker.IsConstant;
  }
};

struct GenerateConstantWorklet
{
  template <typename ArrayT>
  void operator()(ArrayT* arr, vtkSmartPointer<vtkDataArray>& cArr) const
  {
    using VType = vtk::GetAPIType<ArrayT>;
    vtkNew<vtkConstantArray<VType>> constant;
    auto range = vtk::DataArrayValueRange(arr);
    constant->SetBackend(std::make_shared<vtkConstantImplicitBackend<VType>>(range[0]));
    constant->SetNumberOfComponents(arr->GetNumberOfComponents());
    constant->SetNumberOfTuples(arr->GetNumberOfTuples());
    constant->SetName(arr->GetName());
    cArr = constant;
  }
};
}

VTK_ABI_NAMESPACE_BEGIN
//-------------------------------------------------------------------------
vtkObjectFactoryNewMacro(vtkToConstantArrayStrategy);

//-------------------------------------------------------------------------
void vtkToConstantArrayStrategy::PrintSelf(std::ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << std::flush;
}

//-------------------------------------------------------------------------
vtkToImplicitStrategy::Optional vtkToConstantArrayStrategy::EstimateReduction(vtkDataArray* arr)
{
  if (!arr)
  {
    vtkWarningMacro("Cannot transform nullptr to constant array.");
    return vtkToImplicitStrategy::Optional();
  }
  vtkIdType nVals = arr->GetNumberOfValues();
  if (!nVals)
  {
    return vtkToImplicitStrategy::Optional();
  }
  bool isConstant = false;
  ::CheckConstantWorklet worker;
  if (!::Dispatch::Execute(arr, worker, this->Tolerance, isConstant))
  {
    worker(arr, this->Tolerance, isConstant);
  }
  return isConstant ? vtkToImplicitStrategy::Optional(1.0 / nVals)
                    : vtkToImplicitStrategy::Optional();
}

//-------------------------------------------------------------------------
vtkSmartPointer<vtkDataArray> vtkToConstantArrayStrategy::Reduce(vtkDataArray* arr)
{
  vtkSmartPointer<vtkDataArray> res = nullptr;
  if (!arr)
  {
    vtkWarningMacro("Cannot transform nullptr to constant array.");
    return res;
  }
  vtkIdType nVals = arr->GetNumberOfValues();
  if (!nVals)
  {
    return res;
  }
  ::GenerateConstantWorklet worker;
  if (!::Dispatch::Execute(arr, worker, res))
  {
    worker(arr, res);
  }
  return res;
}
VTK_ABI_NAMESPACE_END