vtkPointAccumulator.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-License-Identifier: BSD-3-Clause
17 #ifndef vtkPointAccumulator_h
18 #define vtkPointAccumulator_h
19 
20 #include "vtkPoints.h" // for vtkPoints
21 #ifdef NDEBUG
22 #include <exception> // for std::bad_alloc
23 #endif
24 #include <iostream> // for std::cerr
25 
26 template <typename T_CPP, class T_VTK>
28 {
29 public:
31  {
32  this->PtStore = nullptr;
33  this->NPts = 0;
34  }
35  ~vtkPointAccumulator() { this->Clear(); }
37 
40  void Clear()
41  {
42  if (this->PtStore != nullptr)
43  {
44  free(this->PtStore);
45  }
46  this->PtStore = nullptr;
47  this->NPts = 0;
48  }
50 
53  bool Empty() { return this->NPts == 0; }
55 
59  T_CPP* Expand(vtkIdType n)
60  {
61  const int bytesPerPoint = 3 * sizeof(T_CPP);
62  // extend
63  vtkIdType newNPts = this->NPts + n;
64  T_CPP* newPointStore = static_cast<T_CPP*>(realloc(this->PtStore, newNPts * bytesPerPoint));
65  if (newPointStore == nullptr)
66  {
67 #ifndef NDEBUG
68  abort();
69 #else
70  throw std::bad_alloc();
71 #endif
72  }
73  // mark begin of new
74  T_CPP* writePointer = newPointStore + 3 * this->NPts;
75  // update
76  this->PtStore = newPointStore;
77  this->NPts = newNPts;
79 
80  return writePointer;
81  }
83 
87  void Accumulate(T_CPP* pts, vtkIdType n)
88  {
89  // extend
90  T_CPP* writePointer = this->Expand(n);
91  // copy at end
92  const int bytesPerPoint = 3 * sizeof(T_CPP);
93  memcpy(writePointer, pts, n * bytesPerPoint);
94  }
96 
100  void Accumulate(T_VTK* pts) { this->Accumulate(pts->GetPointer(0), pts->GetNumberOfTuples()); }
102 
107  {
108  T_VTK* da = T_VTK::New();
109  da->SetNumberOfComponents(3);
110  da->SetArray(this->PtStore, 3 * this->NPts, 1);
111  vtkPoints* pts = vtkPoints::New();
112  pts->SetData(da);
113  da->Delete();
115 
116  return pts;
117  }
119 
124  void GetBounds(double bounds[6])
125  {
126  // Prepare
127  for (int q = 0; q < 3; ++q)
128  {
129  bounds[q] = static_cast<double>(this->PtStore[q]);
130  bounds[q + 1] = static_cast<double>(this->PtStore[q + 1]);
131  }
132  // Search
133  for (vtkIdType i = 1; i < this->NPts; ++i)
134  {
135  double pt[3];
136  vtkIdType ptIdx = 3 * i;
137  pt[0] = static_cast<double>(this->PtStore[ptIdx]);
138  pt[1] = static_cast<double>(this->PtStore[ptIdx + 1]);
139  pt[2] = static_cast<double>(this->PtStore[ptIdx + 2]);
140  bounds[0] = std::min(bounds[0], pt[0]);
141  bounds[1] = std::max(bounds[1], pt[0]);
142  bounds[2] = std::min(bounds[2], pt[1]);
143  bounds[3] = std::max(bounds[3], pt[1]);
144  bounds[4] = std::min(bounds[4], pt[2]);
145  bounds[5] = std::max(bounds[5], pt[2]);
146  }
147  }
149 
152  vtkIdType GetNumberOfPoints() { return this->NPts; }
154 
157  void Print()
158  {
159  T_CPP* pBuf = this->PtStore;
160  for (int i = 0; i < this->NPts; ++i)
161  {
162  std::cerr << i << " (" << pBuf[0];
163  for (int q = 1; q < 3; ++q)
164  {
165  std::cerr << ", " << pBuf[q];
166  }
167  std::cerr << ")" << endl;
168  pBuf += 3;
169  }
170  }
172 
173 private:
174  vtkPointAccumulator(const vtkPointAccumulator&) = delete;
175  vtkPointAccumulator& operator=(const vtkPointAccumulator&) = delete;
176 
177  T_CPP* PtStore;
178  vtkIdType NPts;
179 };
180 #endif
static vtkPoints * New()
Container class that manages appending data arrays of points.
void Accumulate(T_CPP *pts, vtkIdType n)
Adds an array of points to the end of the internal store.
void GetBounds(double bounds[6])
Compute axis-aligned bounding box.
vtkIdType GetNumberOfPoints()
Return the number of points currently in the point store.
int vtkIdType
vtkPoints * BuildVtkPoints()
Creates a vtkPoints data structure from the internal store.
void Clear()
Free resources and mark as empty.
bool Empty()
Test if there is anything in the store.
void Accumulate(T_VTK *pts)
Adds an array of points at the end of the internal store.
virtual void SetData(vtkDataArray *)
T_CPP * Expand(vtkIdType n)
Extend the internal store and get a pointer to the newly added memory.
void Print()
Print the contents of the internal store.
VTKWRAPPINGJAVA_EXPORT jlong q(JNIEnv *env, jobject obj)