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 #include <exception> // for std::bad_alloc
22 
23 template <typename T_CPP, class T_VTK>
25 {
26 public:
28  {
29  this->PtStore = nullptr;
30  this->NPts = 0;
31  }
32  ~vtkPointAccumulator() { this->Clear(); }
34 
37  void Clear()
38  {
39  if (this->PtStore != nullptr)
40  {
41  free(this->PtStore);
42  }
43  this->PtStore = nullptr;
44  this->NPts = 0;
45  }
47 
50  bool Empty() { return this->NPts == 0; }
52 
56  T_CPP* Expand(vtkIdType n)
57  {
58  const int bytesPerPoint = 3 * sizeof(T_CPP);
59  // extend
60  vtkIdType newNPts = this->NPts + n;
61  T_CPP* newPointStore = static_cast<T_CPP*>(realloc(this->PtStore, newNPts * bytesPerPoint));
62  if (newPointStore == nullptr)
63  {
64 #ifndef NDEBUG
65  abort();
66 #else
67  throw std::bad_alloc();
68 #endif
69  }
70  // mark begin of new
71  T_CPP* writePointer = newPointStore + 3 * this->NPts;
72  // update
73  this->PtStore = newPointStore;
74  this->NPts = newNPts;
76 
77  return writePointer;
78  }
80 
84  void Accumulate(T_CPP* pts, vtkIdType n)
85  {
86  // extend
87  T_CPP* writePointer = this->Expand(n);
88  // copy at end
89  const int bytesPerPoint = 3 * sizeof(T_CPP);
90  memcpy(writePointer, pts, n * bytesPerPoint);
91  }
93 
97  void Accumulate(T_VTK* pts) { this->Accumulate(pts->GetPointer(0), pts->GetNumberOfTuples()); }
99 
104  {
105  T_VTK* da = T_VTK::New();
106  da->SetNumberOfComponents(3);
107  da->SetArray(this->PtStore, 3 * this->NPts, 1);
108  vtkPoints* pts = vtkPoints::New();
109  pts->SetData(da);
110  da->Delete();
112 
113  return pts;
114  }
116 
121  void GetBounds(double bounds[6])
122  {
123  // Prepare
124  for (int q = 0; q < 3; ++q)
125  {
126  bounds[q] = static_cast<double>(this->PtStore[q]);
127  bounds[q + 1] = static_cast<double>(this->PtStore[q + 1]);
128  }
129  // Search
130  for (vtkIdType i = 1; i < this->NPts; ++i)
131  {
132  double pt[3];
133  vtkIdType ptIdx = 3 * i;
134  pt[0] = static_cast<double>(this->PtStore[ptIdx]);
135  pt[1] = static_cast<double>(this->PtStore[ptIdx + 1]);
136  pt[2] = static_cast<double>(this->PtStore[ptIdx + 2]);
137  if (pt[0] < bounds[0])
138  bounds[0] = pt[0];
139  if (pt[0] > bounds[1])
140  bounds[1] = pt[0];
141  if (pt[1] < bounds[2])
142  bounds[2] = pt[1];
143  if (pt[1] > bounds[3])
144  bounds[3] = pt[1];
145  if (pt[2] < bounds[4])
146  bounds[4] = pt[2];
147  if (pt[2] > bounds[5])
148  bounds[5] = pt[2];
149  }
150  }
152 
155  vtkIdType GetNumberOfPoints() { return this->NPts; }
157 
160  void Print()
161  {
162  T_CPP* pBuf = this->PtStore;
163  for (int i = 0; i < this->NPts; ++i)
164  {
165  cerr << i << " (" << pBuf[0];
166  for (int q = 1; q < 3; ++q)
167  {
168  cerr << ", " << pBuf[q];
169  }
170  cerr << ")" << endl;
171  pBuf += 3;
172  }
173  }
175 
176 private:
177  vtkPointAccumulator(const vtkPointAccumulator&) = delete;
178  vtkPointAccumulator& operator=(const vtkPointAccumulator&) = delete;
179 
180  T_CPP* PtStore;
181  vtkIdType NPts;
182 };
183 #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)