vtkPointAccumulator.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4 
5  Copyright (c) Kitware, Inc.
6  All rights reserved.
7  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
28 #ifndef vtkPointAccumulator_h
29 #define vtkPointAccumulator_h
30 
31 #include "vtkPoints.h"
32 #include <exception>
33 
34 template <typename T_CPP, class T_VTK>
36 {
37 public:
39  {
40  this->PtStore = 0;
41  this->NPts = 0;
42  }
43  ~vtkPointAccumulator() { this->Clear(); }
45 
48  void Clear()
49  {
50  if (this->PtStore != 0)
51  {
52  free(this->PtStore);
53  }
54  this->PtStore = 0;
55  this->NPts = 0;
56  }
58 
61  bool Empty() { return this->NPts == 0; }
63 
67  T_CPP* Expand(vtkIdType n)
68  {
69  const int bytesPerPoint = 3 * sizeof(T_CPP);
70  // extend
71  vtkIdType newNPts = this->NPts + n;
72  T_CPP* newPointStore = static_cast<T_CPP*>(realloc(this->PtStore, newNPts * bytesPerPoint));
73  if (newPointStore == 0)
74  {
75 #ifndef NDEBUG
76  abort();
77 #else
78  throw std::bad_alloc();
79 #endif
80  }
81  // mark begin of new
82  T_CPP* writePointer = newPointStore + 3 * this->NPts;
83  // update
84  this->PtStore = newPointStore;
85  this->NPts = newNPts;
87 
88  return writePointer;
89  }
91 
95  void Accumulate(T_CPP* pts, vtkIdType n)
96  {
97  // extend
98  T_CPP* writePointer = this->Expand(n);
99  // copy at end
100  const int bytesPerPoint = 3 * sizeof(T_CPP);
101  memcpy(writePointer, pts, n * bytesPerPoint);
102  }
104 
108  void Accumulate(T_VTK* pts) { this->Accumulate(pts->GetPointer(0), pts->GetNumberOfTuples()); }
110 
115  {
116  T_VTK* da = T_VTK::New();
117  da->SetNumberOfComponents(3);
118  da->SetArray(this->PtStore, 3 * this->NPts, 1);
119  vtkPoints* pts = vtkPoints::New();
120  pts->SetData(da);
121  da->Delete();
123 
124  return pts;
125  }
127 
132  void GetBounds(double bounds[6])
133  {
134  // Prepare
135  for (int q = 0; q < 3; ++q)
136  {
137  bounds[q] = static_cast<double>(this->PtStore[q]);
138  bounds[q + 1] = static_cast<double>(this->PtStore[q + 1]);
139  }
140  // Search
141  for (vtkIdType i = 1; i < this->NPts; ++i)
142  {
143  double pt[3];
144  vtkIdType ptIdx = 3 * i;
145  pt[0] = static_cast<double>(this->PtStore[ptIdx]);
146  pt[1] = static_cast<double>(this->PtStore[ptIdx + 1]);
147  pt[2] = static_cast<double>(this->PtStore[ptIdx + 2]);
148  if (pt[0] < bounds[0])
149  bounds[0] = pt[0];
150  if (pt[0] > bounds[1])
151  bounds[1] = pt[0];
152  if (pt[1] < bounds[2])
153  bounds[2] = pt[1];
154  if (pt[1] > bounds[3])
155  bounds[3] = pt[1];
156  if (pt[2] < bounds[4])
157  bounds[4] = pt[2];
158  if (pt[2] > bounds[5])
159  bounds[5] = pt[2];
160  }
161  }
163 
166  vtkIdType GetNumberOfPoints() { return this->NPts; }
168 
171  void Print()
172  {
173  T_CPP* pBuf = this->PtStore;
174  for (int i = 0; i < this->NPts; ++i)
175  {
176  cerr << i << " (" << pBuf[0];
177  for (int q = 1; q < 3; ++q)
178  {
179  cerr << ", " << pBuf[q];
180  }
181  cerr << ")" << endl;
182  pBuf += 3;
183  }
184  }
186 
187 private:
188  vtkPointAccumulator(const vtkPointAccumulator&) = delete;
189  vtkPointAccumulator& operator=(const vtkPointAccumulator&) = delete;
190 
191  T_CPP* PtStore;
192  vtkIdType NPts;
193 };
194 #endif
195 // VTK-HeaderTest-Exclude: vtkPointAccumulator.h
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)