VTK
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,
308  int reqListSize);
309  void CreateCellLists(vtkDataSet *set, int *regionReqList,
310  int reqListSize);
311  void CreateCellLists(int *regionReqList, int listSize);
312  void CreateCellLists();
313 
315 
322  vtkSetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
323  vtkGetMacro(IncludeRegionBoundaryCells, vtkTypeBool);
324  vtkBooleanMacro(IncludeRegionBoundaryCells, vtkTypeBool);
326 
330  void DeleteCellLists();
331 
336  vtkIdList *GetCellList(int regionID);
337 
348  vtkIdList *GetBoundaryCellList(int regionID);
349 
351 
371  vtkIdType GetCellLists(vtkIntArray *regions, int set,
372  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
373  vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
374  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
375  vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
376  vtkIdList *onBoundaryCells);
378 
380 
386  int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
387  int GetRegionContainingCell(int set, vtkIdType cellID);
388  int GetRegionContainingCell(vtkIdType cellID);
390 
399  int *AllGetRegionContainingCell();
400 
404  int GetRegionContainingPoint(double x, double y, double z);
405 
411  void BuildLocator() override;
412 
427  int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
428  double **convexRegionBounds);
429 
437  int ViewOrderAllRegionsInDirection(const double directionOfProjection[3],
438  vtkIntArray *orderedList);
439 
447  int ViewOrderRegionsInDirection(vtkIntArray *regionIds,
448  const double directionOfProjection[3],
449  vtkIntArray *orderedList);
450 
458  int ViewOrderAllRegionsFromPosition(const double directionOfProjection[3],
459  vtkIntArray *orderedList);
460 
468  int ViewOrderRegionsFromPosition(vtkIntArray *regionIds,
469  const double directionOfProjection[3],
470  vtkIntArray *orderedList);
471 
473 
486  void BuildLocatorFromPoints(vtkPointSet *pointset);
487  void BuildLocatorFromPoints(vtkPoints *ptArray);
488  void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
490 
505  vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
506 
508 
513  vtkIdType FindPoint(double *x);
514  vtkIdType FindPoint(double x, double y, double z);
516 
518 
523  vtkIdType FindClosestPoint(double *x, double &dist2);
524  vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
526 
532  vtkIdType FindClosestPointWithinRadius(
533  double radius, const double x[3], double& dist2);
534 
536 
541  vtkIdType FindClosestPointInRegion(int regionId, double *x, double &dist2);
542  vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z,
543  double &dist2);
545 
552  void FindPointsWithinRadius(double R, const double x[3], vtkIdList *result);
553 
562  void FindClosestNPoints(int N, const double x[3], vtkIdList *result);
563 
568  vtkIdTypeArray *GetPointsInRegion(int regionId);
569 
574  void FreeSearchStructure() override;
575 
581  void GenerateRepresentation(int level, vtkPolyData *pd) override;
582 
587  void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
588 
590 
596  vtkBooleanMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
597  vtkSetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
598  vtkGetMacro(GenerateRepresentationUsingDataBounds, vtkTypeBool);
600 
604  virtual void PrintTiming(ostream& os, vtkIndent indent);
605 
610  virtual int NewGeometry();
611 
617  virtual int NewGeometry(vtkDataSet **sets, int numDataSets);
618 
624  virtual void InvalidateGeometry();
625 
631  static vtkKdNode *CopyTree(vtkKdNode *kd);
632 
639  void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
640 
641 protected:
642 
643  vtkKdTree();
644  ~vtkKdTree() override;
645 
648 
649  void SetCalculator(vtkKdNode *kd);
650 
651  int ProcessUserDefinedCuts(double *bounds);
652 
653  void SetCuts(vtkBSPCuts *cuts, int userDefined);
654 
660  void UpdateBuildTime();
661 
669  int DivideTest(int numberOfPoints, int level);
670 
671  enum {
672  XDIM = 0, // don't change these values
673  YDIM = 1,
674  ZDIM = 2
675  };
676 
678 
680  vtkKdNode **RegionList; // indexed by region ID
681 
683 
684  static void DeleteAllDescendants(vtkKdNode *nd);
685 
686  void BuildRegionList();
687  virtual int SelectCutDirection(vtkKdNode *kd);
688  void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
689 
695  void GetRegionsAtLevel(int level, vtkKdNode **nodes);
696 
702  static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
703 
704 
709  int GetNumberOfCells();
710 
716  int GetDataSetsNumberOfCells(int set1, int set2);
717 
724  void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
725  void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
726 
736  float *ComputeCellCenters();
737  float *ComputeCellCenters(int set);
738  float *ComputeCellCenters(vtkDataSet *set);
739 
741 
747  void UpdateProgress(double amount);
748 
750 
753  vtkSetClampMacro(Progress,double,0.0,1.0);
754  vtkGetMacro(Progress,double);
756 
757 protected:
758  // So that each suboperation can report progress
759  // in [0,1], yet we will be able to report a global
760  // progress. Sub-operations must use UpdateSubOperationProgress()
761  // for this to work.
762  double ProgressScale;
764 
765  // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
766  // Actual progress is given by
767  // (this->ProgressOffset + this->ProgressScale* amount).
768  void UpdateSubOperationProgress(double amount);
769 
770  static void _SetNewBounds(vtkKdNode *kd, double *b, int *fixDim);
771  static void CopyChildNodes(vtkKdNode *to, vtkKdNode *from);
772  static void CopyKdNode(vtkKdNode *to, vtkKdNode *from);
773  static void SetDataBoundsToSpatialBounds(vtkKdNode *kd);
774  static void ZeroNumberOfPoints(vtkKdNode *kd);
775 
776  // Recursive helper for public FindPointsWithinRadius
777  void FindPointsWithinRadius(vtkKdNode* node, double R2,
778  const double x[3], vtkIdList* ids);
779 
780  // Recursive helper for public FindPointsWithinRadius
781  void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
782 
783  // Recursive helper for public FindPointsInArea
784  void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
785 
786  // Recursive helper for public FindPointsInArea
787  void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
788 
789  int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
790 
791  void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
792 
793  void SelfRegister(vtkKdNode *kd);
794 
795  struct _cellList{
796  vtkDataSet *dataSet; // cell lists for which data set
797  int *regionIds; // nullptr if listing all regions
798  int nRegions;
802  };
803 
804  void InitializeCellLists();
805  vtkIdList *GetList(int regionId, vtkIdList **which);
806 
807  void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
808 
809  void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
810  void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
811  vtkCellArray *polys, int level);
812 
813  void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
814  void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
815  vtkCellArray *polys, int level);
816 
817  void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
818 
819  void _printTree(int verbose);
820 
821  int SearchNeighborsForDuplicate(int regionId, float *point,
822  int **pointsSoFar, int *len,
823  float tolerance, float tolerance2);
824 
825  int SearchRegionForDuplicate(float *point, int *pointsSoFar,
826  int len, float tolerance2);
827 
828  int _FindClosestPointInRegion(int regionId,
829  double x, double y, double z, double &dist2);
830 
831  int FindClosestPointInSphere(double x, double y, double z, double radius,
832  int skipRegion, double &dist2);
833 
834  int _ViewOrderRegionsInDirection(vtkIntArray *IdsOfInterest,
835  const double dop[3],
836  vtkIntArray *orderedList);
837 
838  static int __ViewOrderRegionsInDirection(vtkKdNode *node, vtkIntArray *list,
839  vtkIntArray *IdsOfInterest,
840  const double dir[3], int nextId);
841 
842  int _ViewOrderRegionsFromPosition(vtkIntArray *IdsOfInterest,
843  const double pos[3],
844  vtkIntArray *orderedList);
845 
846  static int __ViewOrderRegionsFromPosition(vtkKdNode *node, vtkIntArray *list,
847  vtkIntArray *IdsOfInterest,
848  const double pos[3], int nextId);
849 
850  static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, vtkKdNode **nodes);
851  static int FoundId(vtkIntArray *idArray, int id);
852 
853  void SetInputDataInfo(int i,
854  int dims[3], double origin[3], double spacing[3]);
855  int CheckInputDataInfo(int i,
856  int dims[3], double origin[3], double spacing[3]);
857  void ClearLastBuildCache();
858 
859  static void __printTree(vtkKdNode *kd, int depth, int verbose);
860 
861  static int MidValue(int dim, float *c1, int nvals, double &coord);
862 
863  static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
864  static float FindMaxLeftHalf(int dim, float *c1, int K);
865  static void _Select(int dim, float *X, int *ids, int L, int R, int K);
866 
867  static int ComputeLevel(vtkKdNode *kd);
868  static int SelfOrder(int id, vtkKdNode *kd);
869  static int findRegion(vtkKdNode *node, float x, float y, float z);
870  static int findRegion(vtkKdNode *node, double x, double y, double z);
871 
872  static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes,
873  vtkKdNode *kd);
874 
875  static void AddNewRegions(vtkKdNode *kd, float *c1,
876  int midpt, int dim, double coord);
877 
878  void NewPartitioningRequest(int req);
879 
882 
884  double CellBoundsCache[6]; // to optimize IntersectsCell()
885 
887 
888  struct _cellList CellList;
889 
890  // Region Ids, by data set by cell id - this list is large (one
891  // int per cell) but accelerates creation of cell lists
892 
894 
895  int MinCells;
896  int NumberOfRegions; // number of leaf nodes
897 
899  double FudgeFactor; // a very small distance, relative to the dataset's size
900 
901  // These instance variables are used by the special locator created
902  // to find duplicate points. (BuildLocatorFromPoints)
903 
908 
909  float MaxWidth;
910 
911  // These Last* values are here to save state so we can
912  // determine later if k-d tree must be rebuilt.
913 
917  unsigned long *LastDataSetObserverTags;
920  double *LastBounds;
923 
925  double Progress;
926 
927  vtkKdTree(const vtkKdTree&) = delete;
928  void operator=(const vtkKdTree&) = delete;
929 };
930 #endif
vtkKdNode ** RegionList
Definition: vtkKdTree.h:680
virtual void BuildLocator()=0
Build the locator from the input dataset.
vtkTypeBool IncludeRegionBoundaryCells
Definition: vtkKdTree.h:883
vtkDataSet ** LastInputDataSets
Definition: vtkKdTree.h:916
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:45
Perform calculations (mostly intersection calculations) on regions of a 3D binary spatial partitionin...
vtkKdNode * Top
Definition: vtkKdTree.h:679
float MaxWidth
Definition: vtkKdTree.h:909
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:62
int LastNumDataSets
Definition: vtkKdTree.h:914
int NumberOfLocatorPoints
Definition: vtkKdTree.h:904
static int ComputeLevel(vtkKdNode *kd)
int * LastDataSetType
Definition: vtkKdTree.h:918
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:880
abstract class for specifying dataset behavior
Definition: vtkPointSet.h:42
vtkDataSet * GetDataSet() override
Return the 0'th data set.
Definition: vtkKdTree.h:234
dynamic, self-adjusting array of vtkIdType
int UserDefinedCuts
Definition: vtkKdTree.h:647
int vtkIdType
Definition: vtkType.h:347
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:85
int LastDataCacheSize
Definition: vtkKdTree.h:915
virtual void FreeSearchStructure()=0
Free the memory required for the spatial data structure.
double ProgressScale
Definition: vtkKdTree.h:754
double * LastInputDataInfo
Definition: vtkKdTree.h:919
int vtkTypeBool
Definition: vtkABI.h:69
Timer support and logging.
Definition: vtkTimerLog.h:91
int * CellRegionList
Definition: vtkKdTree.h:893
virtual void SetDataSet(vtkDataSet *)
Build the locator from the points/cells defining this dataset.
abstract class to specify cell behavior
Definition: vtkCell.h:59
unsigned long * LastDataSetObserverTags
Definition: vtkKdTree.h:917
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:45
void SetActualLevel()
Definition: vtkKdTree.h:688
virtual vtkDataSet * GetDataSet()
Build the locator from the points/cells defining this dataset.
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkBSPCuts * Cuts
Definition: vtkKdTree.h:924
list of point or cell ids
Definition: vtkIdList.h:36
vtkTimerLog * TimerLog
Definition: vtkKdTree.h:682
vtkDataSet * dataSet
Definition: vtkKdTree.h:796
double ProgressOffset
Definition: vtkKdTree.h:763
vtkIdType * LastNumPoints
Definition: vtkKdTree.h:921
int * LocatorIds
Definition: vtkKdTree.h:906
int MinCells
Definition: vtkKdTree.h:895
int NumberOfRegions
Definition: vtkKdTree.h:896
vtkTypeBool GenerateRepresentationUsingDataBounds
Definition: vtkKdTree.h:886
object to represent cell connectivity
Definition: vtkCellArray.h:50
double Progress
Definition: vtkKdTree.h:925
vtkIdList ** boundaryCells
Definition: vtkKdTree.h:800
vtkTypeBool Timing
Definition: vtkKdTree.h:898
double * LastBounds
Definition: vtkKdTree.h:920
a Kd-tree spatial decomposition of a set of points
Definition: vtkKdTree.h:78
float * LocatorPoints
Definition: vtkKdTree.h:905
vtkIdList ** cells
Definition: vtkKdTree.h:799
vtkIdList * emptyList
Definition: vtkKdTree.h:801
int ValidDirections
Definition: vtkKdTree.h:677
vtkIdType * LastNumCells
Definition: vtkKdTree.h:922
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
int * LocatorRegionLocation
Definition: vtkKdTree.h:907
vtkDataSetCollection * DataSets
Definition: vtkKdTree.h:740
virtual void GenerateRepresentation(int level, vtkPolyData *pd)=0
Method to build a representation at a particular level.
vtkBSPIntersections * BSPCalculator
Definition: vtkKdTree.h:646
represent and manipulate 3D points
Definition: vtkPoints.h:39
double FudgeFactor
Definition: vtkKdTree.h:899
int NumberOfRegionsOrMore
Definition: vtkKdTree.h:881