VTK  9.0.1
vtkKdTree.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkKdTree.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*----------------------------------------------------------------------------
16  Copyright (c) Sandia Corporation
17  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
18 ----------------------------------------------------------------------------*/
19 
56 #ifndef vtkKdTree_h
57 #define vtkKdTree_h
58 
59 #include "vtkCommonDataModelModule.h" // For export macro
60 #include "vtkLocator.h"
61 
62 class vtkTimerLog;
63 class vtkIdList;
64 class vtkIdTypeArray;
65 class vtkIntArray;
66 class vtkPointSet;
67 class vtkPoints;
68 class vtkCellArray;
69 class vtkCell;
70 class vtkKdNode;
71 class vtkBSPCuts;
74 
75 class VTKCOMMONDATAMODEL_EXPORT vtkKdTree : public vtkLocator
76 {
77 public:
78  vtkTypeMacro(vtkKdTree, vtkLocator);
79  void PrintSelf(ostream& os, vtkIndent indent) override;
80 
81  static vtkKdTree* New();
82 
84 
87  vtkBooleanMacro(Timing, vtkTypeBool);
88  vtkSetMacro(Timing, vtkTypeBool);
89  vtkGetMacro(Timing, vtkTypeBool);
91 
93 
96  vtkSetMacro(MinCells, int);
97  vtkGetMacro(MinCells, int);
99 
107  vtkGetMacro(NumberOfRegionsOrLess, int);
108  vtkSetMacro(NumberOfRegionsOrLess, int);
109 
117  vtkGetMacro(NumberOfRegionsOrMore, int);
118  vtkSetMacro(NumberOfRegionsOrMore, int);
119 
127  vtkGetMacro(FudgeFactor, double);
128  vtkSetMacro(FudgeFactor, double);
129 
135  vtkGetObjectMacro(Cuts, vtkBSPCuts);
136 
143  void SetCuts(vtkBSPCuts* cuts);
144 
148  void OmitXPartitioning();
149 
153  void OmitYPartitioning();
154 
158  void OmitZPartitioning();
159 
163  void OmitXYPartitioning();
164 
168  void OmitYZPartitioning();
169 
173  void OmitZXPartitioning();
174 
178  void OmitNoPartitioning();
179 
194  void SetDataSet(vtkDataSet* set) override;
195 
200  virtual void AddDataSet(vtkDataSet* set);
201 
203 
206  virtual void RemoveDataSet(int index);
207  virtual void RemoveDataSet(vtkDataSet* set);
208  virtual void RemoveAllDataSets();
210 
214  int GetNumberOfDataSets();
215 
225  vtkDataSet* GetDataSet(int n);
226 
231  vtkDataSet* GetDataSet() override { return this->GetDataSet(0); }
232 
234 
237  vtkGetObjectMacro(DataSets, vtkDataSetCollection);
239 
244  int GetDataSetIndex(vtkDataSet* set);
245 
250  void GetBounds(double* bounds);
251 
260  void SetNewBounds(double* bounds);
261 
263 
266  vtkGetMacro(NumberOfRegions, int);
268 
272  void GetRegionBounds(int regionID, double bounds[6]);
273 
277  void GetRegionDataBounds(int regionID, double bounds[6]);
278 
280 
283  void PrintTree();
284  void PrintVerboseTree();
286 
290  void PrintRegion(int id);
291 
304  void CreateCellLists(int dataSetIndex, int* regionReqList, int reqListSize);
305  void CreateCellLists(vtkDataSet* set, int* regionReqList, int reqListSize);
306  void CreateCellLists(int* regionReqList, int listSize);
307  void CreateCellLists();
308 
310 
317  vtkSetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
318  vtkGetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
319  vtkBooleanMacro(IncludeRegionBoundaryCells, vtkTypeBool);
321 
325  void DeleteCellLists();
326 
331  vtkIdList* GetCellList(int regionID);
332 
343  vtkIdList* GetBoundaryCellList(int regionID);
344 
346 
366  vtkIdType GetCellLists(
367  vtkIntArray* regions, int set, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
368  vtkIdType GetCellLists(
369  vtkIntArray* regions, vtkDataSet* set, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
370  vtkIdType GetCellLists(
371  vtkIntArray* regions, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
373 
375 
381  int GetRegionContainingCell(vtkDataSet* set, vtkIdType cellID);
382  int GetRegionContainingCell(int set, vtkIdType cellID);
383  int GetRegionContainingCell(vtkIdType cellID);
385 
394  int* AllGetRegionContainingCell();
395 
399  int GetRegionContainingPoint(double x, double y, double z);
400 
406  void BuildLocator() override;
407 
422  int MinimalNumberOfConvexSubRegions(vtkIntArray* regionIdList, double** convexRegionBounds);
423 
431  int ViewOrderAllRegionsInDirection(
432  const double directionOfProjection[3], vtkIntArray* orderedList);
433 
441  int ViewOrderRegionsInDirection(
442  vtkIntArray* regionIds, const double directionOfProjection[3], vtkIntArray* orderedList);
443 
451  int ViewOrderAllRegionsFromPosition(
452  const double directionOfProjection[3], vtkIntArray* orderedList);
453 
461  int ViewOrderRegionsFromPosition(
462  vtkIntArray* regionIds, const double directionOfProjection[3], vtkIntArray* orderedList);
463 
465 
478  void BuildLocatorFromPoints(vtkPointSet* pointset);
479  void BuildLocatorFromPoints(vtkPoints* ptArray);
480  void BuildLocatorFromPoints(vtkPoints** ptArray, int numPtArrays);
482 
497  vtkIdTypeArray* BuildMapForDuplicatePoints(float tolerance);
498 
500 
505  vtkIdType FindPoint(double* x);
506  vtkIdType FindPoint(double x, double y, double z);
508 
510 
515  vtkIdType FindClosestPoint(double* x, double& dist2);
516  vtkIdType FindClosestPoint(double x, double y, double z, double& dist2);
518 
524  vtkIdType FindClosestPointWithinRadius(double radius, const double x[3], double& dist2);
525 
527 
532  vtkIdType FindClosestPointInRegion(int regionId, double* x, double& dist2);
533  vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z, double& dist2);
535 
542  void FindPointsWithinRadius(double R, const double x[3], vtkIdList* result);
543 
552  void FindClosestNPoints(int N, const double x[3], vtkIdList* result);
553 
558  vtkIdTypeArray* GetPointsInRegion(int regionId);
559 
564  void FreeSearchStructure() override;
565 
571  void GenerateRepresentation(int level, vtkPolyData* pd) override;
572 
577  void GenerateRepresentation(int* regionList, int len, vtkPolyData* pd);
578 
580 
586  vtkBooleanMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
587  vtkSetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
588  vtkGetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
590 
594  virtual void PrintTiming(ostream& os, vtkIndent indent);
595 
600  virtual int NewGeometry();
601 
607  virtual int NewGeometry(vtkDataSet** sets, int numDataSets);
608 
614  virtual void InvalidateGeometry();
615 
621  static vtkKdNode* CopyTree(vtkKdNode* kd);
622 
629  void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
630 
631 protected:
632  vtkKdTree();
633  ~vtkKdTree() override;
634 
637 
638  void SetCalculator(vtkKdNode* kd);
639 
640  int ProcessUserDefinedCuts(double* bounds);
641 
642  void SetCuts(vtkBSPCuts* cuts, int userDefined);
643 
649  void UpdateBuildTime();
650 
658  int DivideTest(int numberOfPoints, int level);
659 
660  enum
661  {
662  XDIM = 0, // don't change these values
663  YDIM = 1,
664  ZDIM = 2
665  };
666 
668 
670  vtkKdNode** RegionList; // indexed by region ID
671 
673 
674  static void DeleteAllDescendants(vtkKdNode* nd);
675 
676  void BuildRegionList();
677  virtual int SelectCutDirection(vtkKdNode* kd);
678  void SetActualLevel() { this->Level = vtkKdTree::ComputeLevel(this->Top); }
679 
685  void GetRegionsAtLevel(int level, vtkKdNode** nodes);
686 
692  static void GetLeafNodeIds(vtkKdNode* node, vtkIntArray* ids);
693 
698  int GetNumberOfCells();
699 
705  int GetDataSetsNumberOfCells(int set1, int set2);
706 
713  void ComputeCellCenter(vtkDataSet* set, int cellId, float* center);
714  void ComputeCellCenter(vtkDataSet* set, int cellId, double* center);
715 
725  float* ComputeCellCenters();
726  float* ComputeCellCenters(int set);
727  float* ComputeCellCenters(vtkDataSet* set);
728 
730 
736  void UpdateProgress(double amount);
737 
739 
742  vtkSetClampMacro(Progress, double, 0.0, 1.0);
743  vtkGetMacro(Progress, double);
745 
746 protected:
747  // So that each suboperation can report progress
748  // in [0,1], yet we will be able to report a global
749  // progress. Sub-operations must use UpdateSubOperationProgress()
750  // for this to work.
751  double ProgressScale;
753 
754  // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
755  // Actual progress is given by
756  // (this->ProgressOffset + this->ProgressScale* amount).
757  void UpdateSubOperationProgress(double amount);
758 
759  static void _SetNewBounds(vtkKdNode* kd, double* b, int* fixDim);
760  static void CopyChildNodes(vtkKdNode* to, vtkKdNode* from);
761  static void CopyKdNode(vtkKdNode* to, vtkKdNode* from);
762  static void SetDataBoundsToSpatialBounds(vtkKdNode* kd);
763  static void ZeroNumberOfPoints(vtkKdNode* kd);
764 
765  // Recursive helper for public FindPointsWithinRadius
766  void FindPointsWithinRadius(vtkKdNode* node, double R2, const double x[3], vtkIdList* ids);
767 
768  // Recursive helper for public FindPointsWithinRadius
769  void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
770 
771  // Recursive helper for public FindPointsInArea
772  void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
773 
774  // Recursive helper for public FindPointsInArea
775  void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
776 
777  int DivideRegion(vtkKdNode* kd, float* c1, int* ids, int nlevels);
778 
779  void DoMedianFind(vtkKdNode* kd, float* c1, int* ids, int d1, int d2, int d3);
780 
781  void SelfRegister(vtkKdNode* kd);
782 
783  struct _cellList
784  {
785  vtkDataSet* dataSet; // cell lists for which data set
786  int* regionIds; // nullptr if listing all regions
787  int nRegions;
791  };
792 
793  void InitializeCellLists();
794  vtkIdList* GetList(int regionId, vtkIdList** which);
795 
796  void ComputeCellCenter(vtkCell* cell, double* center, double* weights);
797 
798  void GenerateRepresentationDataBounds(int level, vtkPolyData* pd);
799  void _generateRepresentationDataBounds(
800  vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys, int level);
801 
802  void GenerateRepresentationWholeSpace(int level, vtkPolyData* pd);
803  void _generateRepresentationWholeSpace(
804  vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys, int level);
805 
806  void AddPolys(vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys);
807 
808  void _printTree(int verbose);
809 
810  int SearchNeighborsForDuplicate(
811  int regionId, float* point, int** pointsSoFar, int* len, float tolerance, float tolerance2);
812 
813  int SearchRegionForDuplicate(float* point, int* pointsSoFar, int len, float tolerance2);
814 
815  int _FindClosestPointInRegion(int regionId, double x, double y, double z, double& dist2);
816 
817  int FindClosestPointInSphere(
818  double x, double y, double z, double radius, int skipRegion, double& dist2);
819 
820  int _ViewOrderRegionsInDirection(
821  vtkIntArray* IdsOfInterest, const double dop[3], vtkIntArray* orderedList);
822 
823  static int __ViewOrderRegionsInDirection(vtkKdNode* node, vtkIntArray* list,
824  vtkIntArray* IdsOfInterest, const double dir[3], int nextId);
825 
826  int _ViewOrderRegionsFromPosition(
827  vtkIntArray* IdsOfInterest, const double pos[3], vtkIntArray* orderedList);
828 
829  static int __ViewOrderRegionsFromPosition(vtkKdNode* node, vtkIntArray* list,
830  vtkIntArray* IdsOfInterest, const double pos[3], int nextId);
831 
832  static int __ConvexSubRegions(int* ids, int len, vtkKdNode* tree, vtkKdNode** nodes);
833  static int FoundId(vtkIntArray* idArray, int id);
834 
835  void SetInputDataInfo(int i, int dims[3], double origin[3], double spacing[3]);
836  int CheckInputDataInfo(int i, int dims[3], double origin[3], double spacing[3]);
837  void ClearLastBuildCache();
838 
839  static void __printTree(vtkKdNode* kd, int depth, int verbose);
840 
841  static int MidValue(int dim, float* c1, int nvals, double& coord);
842 
843  static int Select(int dim, float* c1, int* ids, int nvals, double& coord);
844  static float FindMaxLeftHalf(int dim, float* c1, int K);
845  static void _Select(int dim, float* X, int* ids, int L, int R, int K);
846 
847  static int ComputeLevel(vtkKdNode* kd);
848  static int SelfOrder(int id, vtkKdNode* kd);
849  static int findRegion(vtkKdNode* node, float x, float y, float z);
850  static int findRegion(vtkKdNode* node, double x, double y, double z);
851 
852  static vtkKdNode** _GetRegionsAtLevel(int level, vtkKdNode** nodes, vtkKdNode* kd);
853 
854  static void AddNewRegions(vtkKdNode* kd, float* c1, int midpt, int dim, double coord);
855 
856  void NewPartitioningRequest(int req);
857 
860 
862  double CellBoundsCache[6]; // to optimize IntersectsCell()
863 
865 
866  struct _cellList CellList;
867 
868  // Region Ids, by data set by cell id - this list is large (one
869  // int per cell) but accelerates creation of cell lists
870 
872 
873  int MinCells;
874  int NumberOfRegions; // number of leaf nodes
875 
877  double FudgeFactor; // a very small distance, relative to the dataset's size
878 
879  // These instance variables are used by the special locator created
880  // to find duplicate points. (BuildLocatorFromPoints)
881 
886 
887  float MaxWidth;
888 
889  // These Last* values are here to save state so we can
890  // determine later if k-d tree must be rebuilt.
891 
895  unsigned long* LastDataSetObserverTags;
898  double* LastBounds;
901 
903  double Progress;
904 
905  vtkKdTree(const vtkKdTree&) = delete;
906  void operator=(const vtkKdTree&) = delete;
907 };
908 #endif
vtkKdNode ** RegionList
Definition: vtkKdTree.h:670
virtual void BuildLocator()=0
Build the locator from the input dataset.
vtkTypeBool IncludeRegionBoundaryCells
Definition: vtkKdTree.h:861
vtkDataSet ** LastInputDataSets
Definition: vtkKdTree.h:894
maintain an unordered list of dataset objects
This class represents a single spatial region in an 3D axis aligned binary spatial partitioning...
Definition: vtkKdNode.h:42
Perform calculations (mostly intersection calculations) on regions of a 3D binary spatial partitionin...
vtkKdNode * Top
Definition: vtkKdTree.h:669
float MaxWidth
Definition: vtkKdTree.h:887
This class represents an axis-aligned Binary Spatial Partitioning of a 3D space.
Definition: vtkBSPCuts.h:44
abstract class to specify dataset behavior
Definition: vtkDataSet.h:56
int LastNumDataSets
Definition: vtkKdTree.h:892
int NumberOfLocatorPoints
Definition: vtkKdTree.h:882
static int ComputeLevel(vtkKdNode *kd)
int * LastDataSetType
Definition: vtkKdTree.h:896
abstract base class for objects that accelerate spatial searches
Definition: vtkLocator.h:69
void PrintSelf(ostream &os, vtkIndent indent) override
Standard type and print methods.
int NumberOfRegionsOrLess
Definition: vtkKdTree.h:858
abstract class for specifying dataset behavior
Definition: vtkPointSet.h:62
vtkDataSet * GetDataSet() override
Return the 0'th data set.
Definition: vtkKdTree.h:231
dynamic, self-adjusting array of vtkIdType
int UserDefinedCuts
Definition: vtkKdTree.h:636
int vtkIdType
Definition: vtkType.h:338
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:84
int LastDataCacheSize
Definition: vtkKdTree.h:893
virtual void FreeSearchStructure()=0
Free the memory required for the spatial data structure.
double ProgressScale
Definition: vtkKdTree.h:743
double * LastInputDataInfo
Definition: vtkKdTree.h:897
int vtkTypeBool
Definition: vtkABI.h:69
Timer support and logging.
Definition: vtkTimerLog.h:90
int * CellRegionList
Definition: vtkKdTree.h:871
virtual void SetDataSet(vtkDataSet *)
Build the locator from the points/cells defining this dataset.
abstract class to specify cell behavior
Definition: vtkCell.h:56
unsigned long * LastDataSetObserverTags
Definition: vtkKdTree.h:895
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:39
void SetActualLevel()
Definition: vtkKdTree.h:678
virtual vtkDataSet * GetDataSet()
Build the locator from the points/cells defining this dataset.
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkBSPCuts * Cuts
Definition: vtkKdTree.h:902
list of point or cell ids
Definition: vtkIdList.h:30
vtkTimerLog * TimerLog
Definition: vtkKdTree.h:672
vtkDataSet * dataSet
Definition: vtkKdTree.h:785
double ProgressOffset
Definition: vtkKdTree.h:752
vtkIdType * LastNumPoints
Definition: vtkKdTree.h:899
int * LocatorIds
Definition: vtkKdTree.h:884
int MinCells
Definition: vtkKdTree.h:873
int NumberOfRegions
Definition: vtkKdTree.h:874
vtkTypeBool GenerateRepresentationUsingDataBounds
Definition: vtkKdTree.h:864
object to represent cell connectivity
Definition: vtkCellArray.h:179
double Progress
Definition: vtkKdTree.h:903
vtkIdList ** boundaryCells
Definition: vtkKdTree.h:789
vtkTypeBool Timing
Definition: vtkKdTree.h:876
double * LastBounds
Definition: vtkKdTree.h:898
a Kd-tree spatial decomposition of a set of points
Definition: vtkKdTree.h:75
float * LocatorPoints
Definition: vtkKdTree.h:883
vtkIdList ** cells
Definition: vtkKdTree.h:788
vtkIdList * emptyList
Definition: vtkKdTree.h:790
int ValidDirections
Definition: vtkKdTree.h:667
vtkIdType * LastNumCells
Definition: vtkKdTree.h:900
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
int * LocatorRegionLocation
Definition: vtkKdTree.h:885
vtkDataSetCollection * DataSets
Definition: vtkKdTree.h:729
virtual void GenerateRepresentation(int level, vtkPolyData *pd)=0
Method to build a representation at a particular level.
vtkBSPIntersections * BSPCalculator
Definition: vtkKdTree.h:635
represent and manipulate 3D points
Definition: vtkPoints.h:33
double FudgeFactor
Definition: vtkKdTree.h:877
int NumberOfRegionsOrMore
Definition: vtkKdTree.h:859