VTK  9.7.1
vtkVoronoiCore2D.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
106 #ifndef vtkVoronoiCore2D_h
107 #define vtkVoronoiCore2D_h
108 
109 #include "vtkAnnularBinIterator.h" // Iteration over the bins of the locator
110 #include "vtkLocatorInterface.h" // Interface to spatial locators
111 #include "vtkPoints.h" // for input points
112 #include "vtkSMPTools.h" // SMP parallel processing
113 #include "vtkStaticPointLocator2D.h" // for locating nearby points
114 #include "vtkVoronoiCore.h" // Core includes
115 #include "vtkVoronoiTile.h" // Generate Voronoi convex tiles
116 
117 VTK_ABI_NAMESPACE_BEGIN
118 
147 {
148  // Optional regions ids for point classification.
149  const int* Regions;
150 
151  // Constructors
153  : Regions(nullptr)
154  {
155  }
156  vtkVoronoiClassifier2D(const int* regions)
157  : Regions(regions)
158  {
159  }
160 
161  // Method required by vtkVoronoiCore2D.
163  {
164  if (c)
165  {
166  this->Regions = c->Regions;
167  }
168  }
169 
170  // Method required by vtkVoronoiCore2D.
172  vtkVoronoiSpokesType& spokes, int& numSpokes, int& maxPoints);
173 
174  // Method required by vtkVoronoiCore2D. By default, any region id >= 0 is
175  // considered a valid inside region (<0 region values are reserved for
176  // algorithm use). If no region ids have been specified, and the point id
177  // is ptId >=0, then the point ptId is inside an interior region.
179  {
180  if (!this->Regions)
181  {
182  return (ptId >= 0);
183  }
184  else
185  {
186  return (ptId >= 0 && this->Regions[ptId] >= 0);
187  }
188  }
189 
190  // Method required by vtkVoronoiCore2D. Determine if the two points
191  // ptId and neiId (which form a spoke) are in the same region. It is
192  // assumed that both (ptId,neiId >= 0), i.e., inside.
193  bool IsSameRegion(vtkIdType ptId, vtkIdType neiId)
194  {
195  return (!this->Regions || this->Regions[ptId] == this->Regions[neiId]);
196  }
197 }; // vtkVoronoiClassifier2D
198 
203 template <class TCompositor, class TClassifier>
205 {
206  vtkIdType ThreadId; // assign a thread id [0,NumThreadsUsed)
207  int MaxPoints; // the maximum number of points in any tile
208  int NumPrunes; // total number of pruning operations
209  vtkBatchIdsType LocalBatches; // list of batches processed by this thread
210  vtkVoronoiSpokesType LocalSpokes; // connecting edges/spokes for each tile
211  vtkAnnularBinIterator BIter; // iterator over static point locator bins
212  vtkVoronoiTile Tile; // computational 2D Voronoi tile algorithm
213  typename TCompositor::LocalData Compositor; // gather data from compositing operations
214  TClassifier Classifier; // Used to classify spokes (based on regions)
215 
217  : ThreadId(-1)
218  , MaxPoints(0)
219  , NumPrunes(0)
220  {
221  this->LocalBatches.reserve(2048);
222  this->LocalSpokes.reserve(2048);
223  }
224 }; // vtkVoronoi2DLocalData
225 
230 template <class TCompositor, class TClassifier>
231 using ThreadMapType = std::vector<vtkVoronoi2DLocalData<TCompositor, TClassifier>*>;
232 
245 template <class TCompositor, class TClassifier = vtkVoronoiClassifier2D>
247 {
248 public:
265  static std::unique_ptr<vtkVoronoiCore2D<TCompositor, TClassifier>> Execute(vtkAlgorithm* filter,
266  unsigned int batchSize, vtkStaticPointLocator2D* loc, vtkPoints* inPts, double padding,
267  vtkIdType maxClips, bool validate, double pruneTol, TCompositor* comp, TClassifier* cl);
268 
270 
275  int GetNumberOfThreads() { return this->NumberOfThreads; }
277  {
278  return this->ThreadMap[threadNum];
279  }
281 
283 
289  int GetMaximumNumberOfPoints() { return this->MaximumNumberOfPoints; }
290  int GetNumberOfPrunes() { return this->NumberOfPrunes; }
292 
298 
304  vtkStaticPointLocator2D* loc, vtkPoints* inPts, double padding, vtkIdType maxClips,
305  bool validate, double pruneTol, TCompositor* comp, TClassifier* c);
306 
308 
315  void Initialize();
316  void operator()(vtkIdType batchId, vtkIdType endBatchId);
317  void Reduce();
319 
327  TCompositor Compositor;
328 
334  TClassifier Classifier;
335 
342 
349 
354  vtkIdType GetNumberOfPoints() { return this->NPts; }
355  const double* GetPoints() { return this->Points; }
356 
362 
363 private:
364  vtkIdType NPts; // The number of input points
365  vtkPoints* InPoints; // Input points as data array
366  const double* Points; // Input points as pointer to x-y-z AOS doubles
367  vtkStaticPointLocator2D* Locator; // Used to (quickly) find nearby points
368  double Padding; // The padding distance around the bounding box
369  double Bounds[6]; // locator bounds
370  double PaddedBounds[6]; // the domain over which Voronoi is calculated
371  vtkIdType MaxClips; // Limit the number of half-space clips
372 
373  // Enable pruning of spokes (equivalent to deletion of a degenerate tile edges)
374  bool Validate; // Indicate whether to explicitly validate the mesh
375  vtkIdType NumberOfPrunes; // If pruning is on, keep track of the number of prunes
376  double PruneTolerance; // Specify the square spoke prune tolerance
377 
378  // High-level information captured during processing
379  int NumberOfThreads; // Keep track of the number of threads used durinf processing
380  int MaximumNumberOfPoints; // Maximum number of points in a generated Voronoi tile (this
381  // equals the maximum number of edges).
382 
383  // Storage local to each thread, as well as working/scratch arrays. We
384  // don't want to allocate working arrays on every thread invocation. Thread
385  // local storage saves lots of new/delete (e.g. the locator tuples).
388 
394  bool BuildTile(vtkVoronoiTile& tile, vtkBinIterator* biter, const double* pts, vtkIdType maxClips,
395  vtkDist2TupleArray& results, int& numPrunes);
396 
397 public:
407  {
410  void operator()(vtkIdType threadId, vtkIdType endThreadId);
411 
412  // Invoke the production of wheels and spokes
414  }; // ProduceWheelsAndSpokes
415 
429  {
432 
433  vtkMergeTuples2DType MergeTuples; // temporary array for merging points
434  vtkMergeMapType MergeMap; // maps tile/hull point ids to merged point ids
435  vtkIdType NumMergedPts; // after merging, the number of points remaining
436 
444 
445  // Core Voronoi methods in support of vtkSMPTools
446  void Initialize() {}
447  void operator()(vtkIdType threadId, vtkIdType endThreadId);
448  void Reduce();
449 
450  // Execute the topological point merge to produce a merge map.
451  static std::unique_ptr<TopologicalMerge> Execute(
453  }; // TopologicalMerge
454 
455 }; // vtkVoronoiCore2D
456 
470 {
475  void Initialize(vtkIdType vtkNotUsed(numPts), vtkEmptyVoronoi2DCompositor*) {}
476  void Finalize() {}
477 
481  struct LocalData
482  {
484  void AddData(vtkVoronoiTile&, int vtkNotUsed(numSpokes), const vtkVoronoiSpoke*) {}
485  };
486 }; // vtkEmptyVoronoi2DCompositor
487 
488 // Minimal classifier - just records the tile's number of points and edges. It
489 // still supports region classification.
491 {
492  // Optional regions ids for point classification.
493  const int* Regions;
494 
495  // Constructor
497  : Regions(nullptr)
498  {
499  }
500  vtkEmptyVoronoi2DClassifier(const int* regions)
501  : Regions(regions)
502  {
503  }
504 
505  // Initialize
507  {
508  if (c)
509  {
510  this->Regions = c->Regions;
511  }
512  }
513 
514  // Method required by vtkVoronoiCore2D. This vtkEmptyClassifier provides
515  // the minimum information needed.
517  vtkVoronoiSpokesType& vtkNotUsed(spokes), int& numSpokes, int& maxPoints)
518  {
519  int numPts = tile.GetNumberOfPoints();
520  wheels[tile.GetGeneratorPointId()] = numPts; // numEdges == numSpokes == numPts
521  maxPoints = (numPts > maxPoints ? numPts : maxPoints);
522  numSpokes = 0;
523  return nullptr;
524  }
525 
526  // Method required by vtkVoronoiCore2D. By default, any region id >= 0 is
527  // considered a valid inside region (<0 region values are reserved for
528  // algorithm use). If no region ids have been specified, and the point id
529  // is ptId >=0, then the point ptId is inside an interior region.
531  {
532  if (!this->Regions)
533  {
534  return (ptId >= 0);
535  }
536  else
537  {
538  return (ptId >= 0 && this->Regions[ptId] >= 0);
539  }
540  }
541 
542  // Method required by vtkVoronoiCore2D. Determine if the two points
543  // ptId and neiId (which form a spoke) are in the same region. It is
544  // assumed that both (ptId,neiId >= 0), i.e., inside.
545  bool IsSameRegion(vtkIdType ptId, vtkIdType neiId)
546  {
547  return (!this->Regions || this->Regions[ptId] == this->Regions[neiId]);
548  }
549 }; // vtkEmptyVoronoi2DClassifier
550 
551 VTK_ABI_NAMESPACE_END
552 #include "vtkVoronoiCore2D.txx"
553 
554 #endif
555 // VTK-HeaderTest-Exclude: vtkVoronoiCore2D.h
int GetMaximumNumberOfPoints()
Obtain information about the execution of the Voronoi algorithm.
static std::unique_ptr< vtkVoronoiCore2D< TCompositor, TClassifier > > Execute(vtkAlgorithm *filter, unsigned int batchSize, vtkStaticPointLocator2D *loc, vtkPoints *inPts, double padding, vtkIdType maxClips, bool validate, double pruneTol, TCompositor *comp, TClassifier *cl)
A factory method to conveniently instantiate and execute the algorithm.
void operator()(vtkIdType threadId, vtkIdType endThreadId)
std::vector< vtkIdType > vtkBatchIdsType
Keep track of batches of generating points.
void Initialize(vtkIdType numPts, vtkEmptyVoronoi2DCompositor *)
Prepare to accumulate compositing information: specify the total number of generating points to be pr...
std::vector< vtkVoronoiMergeTuple2D > vtkMergeTuples2DType
vtkVoronoiCore2D< TCompositor, TClassifier > * VC
bool IsInsideRegion(vtkIdType ptId)
void Initialize(vtkVoronoiClassifier2D *c)
vtkIdType GetNumberOfPoints()
bool IsInsideRegion(vtkIdType ptId)
vtkVoronoiBatchManager Batcher
Controls processing of batches of generating points.
TCompositor Compositor
The compositor enables this vtkVoronoiCore2D templated class to be used in different applications...
vtkVoronoiSpokesType LocalSpokes
const vtkVoronoiSpoke * AddAdjacencyInformation(vtkVoronoiTile &tile, vtkVoronoiWheelsType &wheels, vtkVoronoiSpokesType &spokes, int &numSpokes, int &maxPoints)
========================================================================= The templated, core Voronoi class.
void Initialize(vtkEmptyVoronoi2DCompositor *)
Implementation note about the compositor.
bool IsSameRegion(vtkIdType ptId, vtkIdType neiId)
vtkVoronoiCore2D(vtkAlgorithm *filter, vtkVoronoiBatchManager &batcher, vtkStaticPointLocator2D *loc, vtkPoints *inPts, double padding, vtkIdType maxClips, bool validate, double pruneTol, TCompositor *comp, TClassifier *c)
Constructor.
int GetNumberOfPrunes()
Obtain information about the execution of the Voronoi algorithm.
Functor class used to topologically merge (nearly) coincident points.
vtkVoronoiAdjacencyGraph Graph
This is used to create the spokes and wheels adjacency graph used to validate the tessellation and pr...
int vtkIdType
Definition: vtkType.h:363
void operator()(vtkIdType batchId, vtkIdType endBatchId)
Core vtkSMPTools methods to process tiles in parallel, and produce thread local output data...
std::vector< vtkIdType > vtkVoronoiWheelsType
std::vector< vtkIdType > vtkMergeMapType
When merging points, the merge map is a vector that maps global tile/hull vertex ids (which contain d...
The convex polygon tile class proper.
vtkIdType GetNumberOfMergedPoints()
Methods related to merging coincident points.
Superclass for all sources, filters, and sinks in VTK.
Definition: vtkAlgorithm.h:53
Typedefs and classes in support of the adjacency graph.
vtkBatchIdsType LocalBatches
The following thread local data is used to process and keep track of information on a per-thread basi...
void operator()(vtkIdType threadId, vtkIdType endThreadId)
vtkIdType GetNumberOfPoints()
Convenience methods to retrieve the number of input points, and the raw double* points array...
const vtkVoronoiSpoke * AddAdjacencyInformation(vtkVoronoiTile &tile, vtkVoronoiWheelsType &wheels, vtkVoronoiSpokesType &spokes, int &numSpokes, int &maxPoints)
vtkIdType GetGeneratorPointId()
Obtain information about the generated tile.
vtkEmptyVoronoi2DClassifier(const int *regions)
void Initialize()
Core vtkSMPTools methods to process tiles in parallel, and produce thread local output data...
ThreadMapType< TCompositor, TClassifier > ThreadMap
vtkAnnularBinIterator BIter
The adjacency graph, a collection of wheels and spokes, is a topological construct that connects Voro...
void Initialize(vtkEmptyVoronoi2DClassifier *c)
vtkVoronoiCore2D< TCompositor, TClassifier > * VC
vtkVoronoi2DLocalData< TCompositor, TClassifier > * GetThreadData(int threadNum)
Access the local thread data produced by execution of the filter.
Thread local data may be needed.
static std::unique_ptr< TopologicalMerge > Execute(vtkVoronoiCore2D< TCompositor, TClassifier > *vc)
vtkVoronoiClassifier2D(const int *regions)
void Reduce()
Core vtkSMPTools methods to process tiles in parallel, and produce thread local output data...
vtkAlgorithm * Filter
Used for controlling filter abort and accessing filter information.
TCompositor::LocalData Compositor
Represent an array of vtkDist2Tuples.
TClassifier Classifier
This templated class is used to extend the API of this vtkVoronoiCore2D class, to implement the spoke...
void AddData(vtkVoronoiTile &, int numSpokes, const vtkVoronoiSpoke *)
std::vector< vtkVoronoiSpoke > vtkVoronoiSpokesType
The vtkVoronoiWheelsType vector is used to keep track of the number of spokes (and equivalently...
int GetNumberOfThreads()
Access the local thread data produced by execution of the filter.
Class to manage batches of points.
bool IsSameRegion(vtkIdType ptId, vtkIdType neiId)
These are convenience/demonstration classes for configuring the templated 2D Voronoi classes...
const double * GetPoints()
Produce the global adjacency graph / wheels and spokes data structure.
TopologicalMerge(vtkVoronoiCore2D< TCompositor, TClassifier > *vc)
std::vector< vtkVoronoi2DLocalData< TCompositor, TClassifier > * > ThreadMapType
The thread map keeps track of the thread local data across all computing threads. ...
static void Execute(vtkVoronoiCore2D< TCompositor, TClassifier > *vc)
represent and manipulate 3D points
Definition: vtkPoints.h:30
ProduceWheelsAndSpokes(vtkVoronoiCore2D< TCompositor, TClassifier > *vc)
A fast, lightweight class for iterating over the bins of a 2D vtkStaticPointLocator2D.
quickly locate points in 2-space
vtkVoronoiAdjacencyGraph & GetAdjacencyGraph()
Obtain the adjacency graph (wheel & spokes data structure).