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 
59 #ifndef vtkKdTree_h
60 #define vtkKdTree_h
61 
62 #include "vtkCommonDataModelModule.h" // For export macro
63 #include "vtkLocator.h"
64 
65 class vtkTimerLog;
66 class vtkIdList;
67 class vtkIdTypeArray;
68 class vtkIntArray;
69 class vtkPointSet;
70 class vtkPoints;
71 class vtkCellArray;
72 class vtkCell;
73 class vtkKdNode;
74 class vtkBSPCuts;
77 
78 class VTKCOMMONDATAMODEL_EXPORT vtkKdTree : public vtkLocator
79 {
80 public:
81  vtkTypeMacro(vtkKdTree, vtkLocator);
82  void PrintSelf(ostream& os, vtkIndent indent) override;
83 
84  static vtkKdTree* New();
85 
87 
90  vtkBooleanMacro(Timing, vtkTypeBool);
91  vtkSetMacro(Timing, vtkTypeBool);
92  vtkGetMacro(Timing, vtkTypeBool);
94 
96 
99  vtkSetMacro(MinCells, int);
100  vtkGetMacro(MinCells, int);
102 
110  vtkGetMacro(NumberOfRegionsOrLess, int);
111  vtkSetMacro(NumberOfRegionsOrLess, int);
112 
120  vtkGetMacro(NumberOfRegionsOrMore, int);
121  vtkSetMacro(NumberOfRegionsOrMore, int);
122 
130  vtkGetMacro(FudgeFactor, double);
131  vtkSetMacro(FudgeFactor, double);
132 
138  vtkGetObjectMacro(Cuts, vtkBSPCuts);
139 
146  void SetCuts(vtkBSPCuts* cuts);
147 
151  void OmitXPartitioning();
152 
156  void OmitYPartitioning();
157 
161  void OmitZPartitioning();
162 
166  void OmitXYPartitioning();
167 
171  void OmitYZPartitioning();
172 
176  void OmitZXPartitioning();
177 
181  void OmitNoPartitioning();
182 
197  void SetDataSet(vtkDataSet* set) override;
198 
203  virtual void AddDataSet(vtkDataSet* set);
204 
206 
209  virtual void RemoveDataSet(int index);
210  virtual void RemoveDataSet(vtkDataSet* set);
211  virtual void RemoveAllDataSets();
213 
217  int GetNumberOfDataSets();
218 
228  vtkDataSet* GetDataSet(int n);
229 
234  vtkDataSet* GetDataSet() override { return this->GetDataSet(0); }
235 
237 
240  vtkGetObjectMacro(DataSets, vtkDataSetCollection);
242 
247  int GetDataSetIndex(vtkDataSet* set);
248 
253  void GetBounds(double* bounds);
254 
263  void SetNewBounds(double* bounds);
264 
266 
269  vtkGetMacro(NumberOfRegions, int);
271 
275  void GetRegionBounds(int regionID, double bounds[6]);
276 
280  void GetRegionDataBounds(int regionID, double bounds[6]);
281 
283 
286  void PrintTree();
287  void PrintVerboseTree();
289 
293  void PrintRegion(int id);
294 
307  void CreateCellLists(int dataSetIndex, int* regionReqList, int reqListSize);
308  void CreateCellLists(vtkDataSet* set, int* regionReqList, int reqListSize);
309  void CreateCellLists(int* regionReqList, int listSize);
310  void CreateCellLists();
311 
313 
320  vtkSetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
321  vtkGetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
322  vtkBooleanMacro(IncludeRegionBoundaryCells, vtkTypeBool);
324 
328  void DeleteCellLists();
329 
334  vtkIdList* GetCellList(int regionID);
335 
346  vtkIdList* GetBoundaryCellList(int regionID);
347 
349 
369  vtkIdType GetCellLists(
370  vtkIntArray* regions, int set, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
371  vtkIdType GetCellLists(
372  vtkIntArray* regions, vtkDataSet* set, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
373  vtkIdType GetCellLists(
374  vtkIntArray* regions, vtkIdList* inRegionCells, vtkIdList* onBoundaryCells);
376 
378 
384  int GetRegionContainingCell(vtkDataSet* set, vtkIdType cellID);
385  int GetRegionContainingCell(int set, vtkIdType cellID);
386  int GetRegionContainingCell(vtkIdType cellID);
388 
397  int* AllGetRegionContainingCell();
398 
402  int GetRegionContainingPoint(double x, double y, double z);
403 
409  void BuildLocator() override;
410 
425  int MinimalNumberOfConvexSubRegions(vtkIntArray* regionIdList, double** convexRegionBounds);
426 
434  int ViewOrderAllRegionsInDirection(
435  const double directionOfProjection[3], vtkIntArray* orderedList);
436 
444  int ViewOrderRegionsInDirection(
445  vtkIntArray* regionIds, const double directionOfProjection[3], vtkIntArray* orderedList);
446 
454  int ViewOrderAllRegionsFromPosition(
455  const double directionOfProjection[3], vtkIntArray* orderedList);
456 
464  int ViewOrderRegionsFromPosition(
465  vtkIntArray* regionIds, const double directionOfProjection[3], vtkIntArray* orderedList);
466 
468 
481  void BuildLocatorFromPoints(vtkPointSet* pointset);
482  void BuildLocatorFromPoints(vtkPoints* ptArray);
483  void BuildLocatorFromPoints(vtkPoints** ptArray, int numPtArrays);
485 
500  vtkIdTypeArray* BuildMapForDuplicatePoints(float tolerance);
501 
503 
508  vtkIdType FindPoint(double* x);
509  vtkIdType FindPoint(double x, double y, double z);
511 
513 
518  vtkIdType FindClosestPoint(double* x, double& dist2);
519  vtkIdType FindClosestPoint(double x, double y, double z, double& dist2);
521 
527  vtkIdType FindClosestPointWithinRadius(double radius, const double x[3], double& dist2);
528 
530 
535  vtkIdType FindClosestPointInRegion(int regionId, double* x, double& dist2);
536  vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z, double& dist2);
538 
545  void FindPointsWithinRadius(double R, const double x[3], vtkIdList* result);
546 
555  void FindClosestNPoints(int N, const double x[3], vtkIdList* result);
556 
561  vtkIdTypeArray* GetPointsInRegion(int regionId);
562 
567  void FreeSearchStructure() override;
568 
574  void GenerateRepresentation(int level, vtkPolyData* pd) override;
575 
580  void GenerateRepresentation(int* regionList, int len, vtkPolyData* pd);
581 
583 
589  vtkBooleanMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
590  vtkSetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
591  vtkGetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
593 
597  virtual void PrintTiming(ostream& os, vtkIndent indent);
598 
603  virtual int NewGeometry();
604 
610  virtual int NewGeometry(vtkDataSet** sets, int numDataSets);
611 
617  virtual void InvalidateGeometry();
618 
624  static vtkKdNode* CopyTree(vtkKdNode* kd);
625 
632  void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
633 
634 protected:
635  vtkKdTree();
636  ~vtkKdTree() override;
637 
640 
641  void SetCalculator(vtkKdNode* kd);
642 
643  int ProcessUserDefinedCuts(double* bounds);
644 
645  void SetCuts(vtkBSPCuts* cuts, int userDefined);
646 
652  void UpdateBuildTime();
653 
661  int DivideTest(int numberOfPoints, int level);
662 
663  enum
664  {
665  XDIM = 0, // don't change these values
666  YDIM = 1,
667  ZDIM = 2
668  };
669 
671 
673  vtkKdNode** RegionList; // indexed by region ID
674 
676 
677  static void DeleteAllDescendants(vtkKdNode* nd);
678 
679  void BuildRegionList();
680  virtual int SelectCutDirection(vtkKdNode* kd);
681  void SetActualLevel() { this->Level = vtkKdTree::ComputeLevel(this->Top); }
682 
688  void GetRegionsAtLevel(int level, vtkKdNode** nodes);
689 
695  static void GetLeafNodeIds(vtkKdNode* node, vtkIntArray* ids);
696 
701  int GetNumberOfCells();
702 
708  int GetDataSetsNumberOfCells(int set1, int set2);
709 
716  void ComputeCellCenter(vtkDataSet* set, int cellId, float* center);
717  void ComputeCellCenter(vtkDataSet* set, int cellId, double* center);
718 
728  float* ComputeCellCenters();
729  float* ComputeCellCenters(int set);
730  float* ComputeCellCenters(vtkDataSet* set);
731 
733 
739  void UpdateProgress(double amount);
740 
742 
745  vtkSetClampMacro(Progress, double, 0.0, 1.0);
746  vtkGetMacro(Progress, double);
748 
749 protected:
750  // So that each suboperation can report progress
751  // in [0,1], yet we will be able to report a global
752  // progress. Sub-operations must use UpdateSubOperationProgress()
753  // for this to work.
754  double ProgressScale;
756 
757  // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
758  // Actual progress is given by
759  // (this->ProgressOffset + this->ProgressScale* amount).
760  void UpdateSubOperationProgress(double amount);
761 
762  static void _SetNewBounds(vtkKdNode* kd, double* b, int* fixDim);
763  static void CopyChildNodes(vtkKdNode* to, vtkKdNode* from);
764  static void CopyKdNode(vtkKdNode* to, vtkKdNode* from);
765  static void SetDataBoundsToSpatialBounds(vtkKdNode* kd);
766  static void ZeroNumberOfPoints(vtkKdNode* kd);
767 
768  // Recursive helper for public FindPointsWithinRadius
769  void FindPointsWithinRadius(vtkKdNode* node, double R2, const double x[3], vtkIdList* ids);
770 
771  // Recursive helper for public FindPointsWithinRadius
772  void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
773 
774  // Recursive helper for public FindPointsInArea
775  void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
776 
777  // Recursive helper for public FindPointsInArea
778  void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
779 
780  int DivideRegion(vtkKdNode* kd, float* c1, int* ids, int nlevels);
781 
782  void DoMedianFind(vtkKdNode* kd, float* c1, int* ids, int d1, int d2, int d3);
783 
784  void SelfRegister(vtkKdNode* kd);
785 
786  struct _cellList
787  {
788  vtkDataSet* dataSet; // cell lists for which data set
789  int* regionIds; // nullptr if listing all regions
790  int nRegions;
794  };
795 
796  void InitializeCellLists();
797  vtkIdList* GetList(int regionId, vtkIdList** which);
798 
799  void ComputeCellCenter(vtkCell* cell, double* center, double* weights);
800 
801  void GenerateRepresentationDataBounds(int level, vtkPolyData* pd);
802  void _generateRepresentationDataBounds(
803  vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys, int level);
804 
805  void GenerateRepresentationWholeSpace(int level, vtkPolyData* pd);
806  void _generateRepresentationWholeSpace(
807  vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys, int level);
808 
809  void AddPolys(vtkKdNode* kd, vtkPoints* pts, vtkCellArray* polys);
810 
811  void _printTree(int verbose);
812 
813  int SearchNeighborsForDuplicate(
814  int regionId, float* point, int** pointsSoFar, int* len, float tolerance, float tolerance2);
815 
816  int SearchRegionForDuplicate(float* point, int* pointsSoFar, int len, float tolerance2);
817 
818  int _FindClosestPointInRegion(int regionId, double x, double y, double z, double& dist2);
819 
820  int FindClosestPointInSphere(
821  double x, double y, double z, double radius, int skipRegion, double& dist2);
822 
823  int _ViewOrderRegionsInDirection(
824  vtkIntArray* IdsOfInterest, const double dop[3], vtkIntArray* orderedList);
825 
826  static int __ViewOrderRegionsInDirection(vtkKdNode* node, vtkIntArray* list,
827  vtkIntArray* IdsOfInterest, const double dir[3], int nextId);
828 
829  int _ViewOrderRegionsFromPosition(
830  vtkIntArray* IdsOfInterest, const double pos[3], vtkIntArray* orderedList);
831 
832  static int __ViewOrderRegionsFromPosition(vtkKdNode* node, vtkIntArray* list,
833  vtkIntArray* IdsOfInterest, const double pos[3], int nextId);
834 
835  static int __ConvexSubRegions(int* ids, int len, vtkKdNode* tree, vtkKdNode** nodes);
836  static int FoundId(vtkIntArray* idArray, int id);
837 
838  void SetInputDataInfo(int i, int dims[3], double origin[3], double spacing[3]);
839  int CheckInputDataInfo(int i, int dims[3], double origin[3], double spacing[3]);
840  void ClearLastBuildCache();
841 
842  static void __printTree(vtkKdNode* kd, int depth, int verbose);
843 
844  static int MidValue(int dim, float* c1, int nvals, double& coord);
845 
846  static int Select(int dim, float* c1, int* ids, int nvals, double& coord);
847  static float FindMaxLeftHalf(int dim, float* c1, int K);
848  static void _Select(int dim, float* X, int* ids, int L, int R, int K);
849 
850  static int ComputeLevel(vtkKdNode* kd);
851  static int SelfOrder(int id, vtkKdNode* kd);
852  static int findRegion(vtkKdNode* node, float x, float y, float z);
853  static int findRegion(vtkKdNode* node, double x, double y, double z);
854 
855  static vtkKdNode** _GetRegionsAtLevel(int level, vtkKdNode** nodes, vtkKdNode* kd);
856 
857  static void AddNewRegions(vtkKdNode* kd, float* c1, int midpt, int dim, double coord);
858 
859  void NewPartitioningRequest(int req);
860 
863 
865  double CellBoundsCache[6]; // to optimize IntersectsCell()
866 
868 
869  struct _cellList CellList;
870 
871  // Region Ids, by data set by cell id - this list is large (one
872  // int per cell) but accelerates creation of cell lists
873 
875 
876  int MinCells;
877  int NumberOfRegions; // number of leaf nodes
878 
880  double FudgeFactor; // a very small distance, relative to the dataset's size
881 
882  // These instance variables are used by the special locator created
883  // to find duplicate points. (BuildLocatorFromPoints)
884 
889 
890  float MaxWidth;
891 
892  // These Last* values are here to save state so we can
893  // determine later if k-d tree must be rebuilt.
894 
898  unsigned long* LastDataSetObserverTags;
901  double* LastBounds;
904 
906  double Progress;
907 
908  vtkKdTree(const vtkKdTree&) = delete;
909  void operator=(const vtkKdTree&) = delete;
910 };
911 #endif
vtkKdNode ** RegionList
Definition: vtkKdTree.h:673
virtual void BuildLocator()=0
Build the locator from the input dataset.
vtkTypeBool IncludeRegionBoundaryCells
Definition: vtkKdTree.h:864
vtkDataSet ** LastInputDataSets
Definition: vtkKdTree.h:897
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:672
float MaxWidth
Definition: vtkKdTree.h:890
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:895
int NumberOfLocatorPoints
Definition: vtkKdTree.h:885
static int ComputeLevel(vtkKdNode *kd)
int * LastDataSetType
Definition: vtkKdTree.h:899
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:861
abstract class for specifying dataset behavior
Definition: vtkPointSet.h:62
vtkDataSet * GetDataSet() override
Return the 0'th data set.
Definition: vtkKdTree.h:234
dynamic, self-adjusting array of vtkIdType
int UserDefinedCuts
Definition: vtkKdTree.h:639
int vtkIdType
Definition: vtkType.h:338
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:84
int LastDataCacheSize
Definition: vtkKdTree.h:896
virtual void FreeSearchStructure()=0
Free the memory required for the spatial data structure.
double ProgressScale
Definition: vtkKdTree.h:746
double * LastInputDataInfo
Definition: vtkKdTree.h:900
int vtkTypeBool
Definition: vtkABI.h:69
Timer support and logging.
Definition: vtkTimerLog.h:90
int * CellRegionList
Definition: vtkKdTree.h:874
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:898
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:39
void SetActualLevel()
Definition: vtkKdTree.h:681
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:905
list of point or cell ids
Definition: vtkIdList.h:30
vtkTimerLog * TimerLog
Definition: vtkKdTree.h:675
vtkDataSet * dataSet
Definition: vtkKdTree.h:788
double ProgressOffset
Definition: vtkKdTree.h:755
vtkIdType * LastNumPoints
Definition: vtkKdTree.h:902
int * LocatorIds
Definition: vtkKdTree.h:887
int MinCells
Definition: vtkKdTree.h:876
int NumberOfRegions
Definition: vtkKdTree.h:877
vtkTypeBool GenerateRepresentationUsingDataBounds
Definition: vtkKdTree.h:867
object to represent cell connectivity
Definition: vtkCellArray.h:179
double Progress
Definition: vtkKdTree.h:906
vtkIdList ** boundaryCells
Definition: vtkKdTree.h:792
vtkTypeBool Timing
Definition: vtkKdTree.h:879
double * LastBounds
Definition: vtkKdTree.h:901
a Kd-tree spatial decomposition of a set of points
Definition: vtkKdTree.h:78
float * LocatorPoints
Definition: vtkKdTree.h:886
vtkIdList ** cells
Definition: vtkKdTree.h:791
vtkIdList * emptyList
Definition: vtkKdTree.h:793
int ValidDirections
Definition: vtkKdTree.h:670
vtkIdType * LastNumCells
Definition: vtkKdTree.h:903
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
int * LocatorRegionLocation
Definition: vtkKdTree.h:888
vtkDataSetCollection * DataSets
Definition: vtkKdTree.h:732
virtual void GenerateRepresentation(int level, vtkPolyData *pd)=0
Method to build a representation at a particular level.
vtkBSPIntersections * BSPCalculator
Definition: vtkKdTree.h:638
represent and manipulate 3D points
Definition: vtkPoints.h:33
double FudgeFactor
Definition: vtkKdTree.h:880
int NumberOfRegionsOrMore
Definition: vtkKdTree.h:862