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 
61 #ifndef vtkKdTree_h
62 #define vtkKdTree_h
63 
64 #include "vtkCommonDataModelModule.h" // For export macro
65 #include "vtkLocator.h"
66 
67 class vtkTimerLog;
68 class vtkIdList;
69 class vtkIdTypeArray;
70 class vtkIntArray;
71 class vtkPointSet;
72 class vtkPoints;
73 class vtkCellArray;
74 class vtkCell;
75 class vtkKdNode;
76 class vtkBSPCuts;
79 
81 {
82 public:
83  vtkTypeMacro(vtkKdTree, vtkLocator);
84  void PrintSelf(ostream& os, vtkIndent indent);
85 
86  static vtkKdTree *New();
87 
89 
90  vtkBooleanMacro(Timing, int);
91  vtkSetMacro(Timing, int);
92  vtkGetMacro(Timing, int);
94 
96 
97  vtkSetMacro(MinCells, int);
98  vtkGetMacro(MinCells, int);
100 
106  vtkGetMacro(NumberOfRegionsOrLess, int);
107  vtkSetMacro(NumberOfRegionsOrLess, int);
108 
113  vtkGetMacro(NumberOfRegionsOrMore, int);
114  vtkSetMacro(NumberOfRegionsOrMore, int);
115 
121  vtkGetMacro(FudgeFactor, double);
122  vtkSetMacro(FudgeFactor, double);
123 
127  vtkGetObjectMacro(Cuts, vtkBSPCuts);
128 
133  void SetCuts(vtkBSPCuts *cuts);
134 
136  void OmitXPartitioning();
137 
139  void OmitYPartitioning();
140 
142  void OmitZPartitioning();
143 
145  void OmitXYPartitioning();
146 
148  void OmitYZPartitioning();
149 
151  void OmitZXPartitioning();
152 
154  void OmitNoPartitioning();
155 
165  virtual void SetDataSet(vtkDataSet *set);
166 
170  virtual void AddDataSet(vtkDataSet *set);
171 
173 
174  virtual void RemoveDataSet(int index);
175  virtual void RemoveDataSet(vtkDataSet *set);
176  virtual void RemoveAllDataSets();
178 
180  int GetNumberOfDataSets();
181 
187  vtkDataSet *GetDataSet(int n);
188 
191  vtkDataSet *GetDataSet(){ return this->GetDataSet(0); }
192 
194 
195  vtkGetObjectMacro(DataSets, vtkDataSetCollection);
197 
200  int GetDataSetIndex(vtkDataSet *set);
201 
204  void GetBounds(double *bounds);
205 
212  void SetNewBounds(double *bounds);
213 
215 
216  vtkGetMacro(NumberOfRegions, int);
218 
220  void GetRegionBounds(int regionID, double bounds[6]);
221 
223  void GetRegionDataBounds(int regionID, double bounds[6]);
224 
226 
227  void PrintTree();
228  void PrintVerboseTree();
230 
232  void PrintRegion(int id);
233 
242  void CreateCellLists(int dataSetIndex, int *regionReqList,
243  int reqListSize);
244  void CreateCellLists(vtkDataSet *set, int *regionReqList,
245  int reqListSize);
246  void CreateCellLists(int *regionReqList, int listSize);
247  void CreateCellLists();
248 
250 
254  vtkSetMacro(IncludeRegionBoundaryCells, int);
255  vtkGetMacro(IncludeRegionBoundaryCells, int);
256  vtkBooleanMacro(IncludeRegionBoundaryCells, int);
258 
260  void DeleteCellLists();
261 
264  vtkIdList *GetCellList(int regionID);
265 
273  vtkIdList *GetBoundaryCellList(int regionID);
274 
276 
290  vtkIdType GetCellLists(vtkIntArray *regions, int set,
291  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
292  vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
293  vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
294  vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
295  vtkIdList *onBoundaryCells);
297 
299 
302  int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
303  int GetRegionContainingCell(int set, vtkIdType cellID);
304  int GetRegionContainingCell(vtkIdType cellID);
306 
311  int *AllGetRegionContainingCell();
312 
314  int GetRegionContainingPoint(double x, double y, double z);
315 
319  void BuildLocator();
320 
332  int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
333  double **convexRegionBounds);
334 
336 
342  int ViewOrderAllRegionsInDirection(const double directionOfProjection[3],
343  vtkIntArray *orderedList);
345 
347 
352  int ViewOrderRegionsInDirection(vtkIntArray *regionIds,
353  const double directionOfProjection[3],
354  vtkIntArray *orderedList);
356 
358 
364  int ViewOrderAllRegionsFromPosition(const double directionOfProjection[3],
365  vtkIntArray *orderedList);
367 
369 
374  int ViewOrderRegionsFromPosition(vtkIntArray *regionIds,
375  const double directionOfProjection[3],
376  vtkIntArray *orderedList);
378 
380 
388  void BuildLocatorFromPoints(vtkPointSet *pointset);
389  void BuildLocatorFromPoints(vtkPoints *ptArray);
390  void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
392 
402  vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
403 
405 
408  vtkIdType FindPoint(double *x);
409  vtkIdType FindPoint(double x, double y, double z);
411 
413 
416  vtkIdType FindClosestPoint(double *x, double &dist2);
417  vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
419 
421 
424  vtkIdType FindClosestPointWithinRadius(
425  double radius, const double x[3], double& dist2);
427 
429 
432  vtkIdType FindClosestPointInRegion(int regionId, double *x, double &dist2);
433  vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z,
434  double &dist2);
436 
441  void FindPointsWithinRadius(double R, const double x[3], vtkIdList *result);
442 
449  void FindClosestNPoints(int N, const double x[3], vtkIdList *result);
450 
453  vtkIdTypeArray *GetPointsInRegion(int regionId);
454 
457  void FreeSearchStructure();
458 
462 
465  void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
466 
468 
472  vtkBooleanMacro(GenerateRepresentationUsingDataBounds, int);
473  vtkSetMacro(GenerateRepresentationUsingDataBounds, int);
474  vtkGetMacro(GenerateRepresentationUsingDataBounds, int);
476 
478  virtual void PrintTiming(ostream& os, vtkIndent indent);
479 
482  virtual int NewGeometry();
483 
486  virtual int NewGeometry(vtkDataSet **sets, int numDataSets);
487 
491  virtual void InvalidateGeometry();
492 
496  static vtkKdNode *CopyTree(vtkKdNode *kd);
497 
502  void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
503 
504 protected:
505 
506  vtkKdTree();
507  ~vtkKdTree();
508 
511 
512  void SetCalculator(vtkKdNode *kd);
513 
514  int ProcessUserDefinedCuts(double *bounds);
515 
516  void SetCuts(vtkBSPCuts *cuts, int userDefined);
517 
521  void UpdateBuildTime();
522 
528  int DivideTest(int numberOfPoints, int level);
529 
530 //BTX
531  enum {
532  XDIM = 0, // don't change these values
533  YDIM = 1,
534  ZDIM = 2
535  };
536 //ETX
537 
539 
541  vtkKdNode **RegionList; // indexed by region ID
542 
544 
545  static void DeleteAllDescendants(vtkKdNode *nd);
546 
547  void BuildRegionList();
548  virtual int SelectCutDirection(vtkKdNode *kd);
549  void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
550 
554  void GetRegionsAtLevel(int level, vtkKdNode **nodes);
555 
559  static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
560 
561 
564  int GetNumberOfCells();
565 
568  int GetDataSetsNumberOfCells(int set1, int set2);
569 
574  void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
575  void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
576 
583  float *ComputeCellCenters();
584  float *ComputeCellCenters(int set);
585  float *ComputeCellCenters(vtkDataSet *set);
586 
588 
591  void UpdateProgress(double amount);
592 
594 
595  vtkSetClampMacro(Progress,double,0.0,1.0);
596  vtkGetMacro(Progress,double);
598 
599 protected:
600  // So that each suboperation can report progress
601  // in [0,1], yet we will be able to report a global
602  // progress. Sub-operations must use UpdateSubOperationProgress()
603  // for this to work.
604  double ProgressScale;
606 
607  // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
608  // Actual progress is given by
609  // (this->ProgressOffset + this->ProgressScale* amount).
610  void UpdateSubOperationProgress(double amount);
611 
612  static void _SetNewBounds(vtkKdNode *kd, double *b, int *fixDim);
613  static void CopyChildNodes(vtkKdNode *to, vtkKdNode *from);
614  static void CopyKdNode(vtkKdNode *to, vtkKdNode *from);
615  static void SetDataBoundsToSpatialBounds(vtkKdNode *kd);
616  static void ZeroNumberOfPoints(vtkKdNode *kd);
617 
618 //BTX
619  // Recursive helper for public FindPointsWithinRadius
620  void FindPointsWithinRadius(vtkKdNode* node, double R2,
621  const double x[3], vtkIdList* ids);
622 
623  // Recursive helper for public FindPointsWithinRadius
624  void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
625 
626  // Recursive helper for public FindPointsInArea
627  void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
628 
629  // Recursive helper for public FindPointsInArea
630  void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
631 
632  int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
633 
634  void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
635 
636  void SelfRegister(vtkKdNode *kd);
637 
638  struct _cellList{
639  vtkDataSet *dataSet; // cell lists for which data set
640  int *regionIds; // NULL if listing all regions
641  int nRegions;
645  };
646 //ETX
647 
648  void InitializeCellLists();
649  vtkIdList *GetList(int regionId, vtkIdList **which);
650 
651  void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
652 
653  void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
654  void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
655  vtkCellArray *polys, int level);
656 
657  void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
658  void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
659  vtkCellArray *polys, int level);
660 
661  void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
662 
663  void _printTree(int verbose);
664 
665  int SearchNeighborsForDuplicate(int regionId, float *point,
666  int **pointsSoFar, int *len,
667  float tolerance, float tolerance2);
668 
669  int SearchRegionForDuplicate(float *point, int *pointsSoFar,
670  int len, float tolerance2);
671 
672  int _FindClosestPointInRegion(int regionId,
673  double x, double y, double z, double &dist2);
674 
675  int FindClosestPointInSphere(double x, double y, double z, double radius,
676  int skipRegion, double &dist2);
677 
678  int _ViewOrderRegionsInDirection(vtkIntArray *IdsOfInterest,
679  const double dop[3],
680  vtkIntArray *orderedList);
681 
682  static int __ViewOrderRegionsInDirection(vtkKdNode *node, vtkIntArray *list,
683  vtkIntArray *IdsOfInterest,
684  const double dir[3], int nextId);
685 
686  int _ViewOrderRegionsFromPosition(vtkIntArray *IdsOfInterest,
687  const double pos[3],
688  vtkIntArray *orderedList);
689 
690  static int __ViewOrderRegionsFromPosition(vtkKdNode *node, vtkIntArray *list,
691  vtkIntArray *IdsOfInterest,
692  const double pos[3], int nextId);
693 
694  static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, vtkKdNode **nodes);
695  static int FoundId(vtkIntArray *idArray, int id);
696 
697  void NewParitioningRequest(int req);
698  void SetInputDataInfo(int i,
699  int dims[3], double origin[3], double spacing[3]);
700  int CheckInputDataInfo(int i,
701  int dims[3], double origin[3], double spacing[3]);
702  void ClearLastBuildCache();
703 
704 //BTX
705  static void __printTree(vtkKdNode *kd, int depth, int verbose);
706 //ETX
707 
708  static int MidValue(int dim, float *c1, int nvals, double &coord);
709 
710  static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
711  static float FindMaxLeftHalf(int dim, float *c1, int K);
712  static void _Select(int dim, float *X, int *ids, int L, int R, int K);
713 
714 //BTX
715  static int ComputeLevel(vtkKdNode *kd);
716  static int SelfOrder(int id, vtkKdNode *kd);
717  static int findRegion(vtkKdNode *node, float x, float y, float z);
718  static int findRegion(vtkKdNode *node, double x, double y, double z);
719 //ETX
720 
721  static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes,
722  vtkKdNode *kd);
723 
724  static void AddNewRegions(vtkKdNode *kd, float *c1,
725  int midpt, int dim, double coord);
726 
727  void NewPartitioningRequest(int req);
728 
731 
733  double CellBoundsCache[6]; // to optimize IntersectsCell()
734 
736 
737 //BTX
738  struct _cellList CellList;
739 //ETX
740 
741  // Region Ids, by data set by cell id - this list is large (one
742  // int per cell) but accelerates creation of cell lists
743 
745 
746  int MinCells;
747  int NumberOfRegions; // number of leaf nodes
748 
749  int Timing;
750  double FudgeFactor; // a very small distance, relative to the dataset's size
751 
752  // These instance variables are used by the special locator created
753  // to find duplicate points. (BuildLocatorFromPoints)
754 
759 
760  float MaxWidth;
761 
762  // These Last* values are here to save state so we can
763  // determine later if k-d tree must be rebuilt.
764 
768  unsigned long *LastDataSetObserverTags;
771  double *LastBounds;
774 
776  double Progress;
777 
778  vtkKdTree(const vtkKdTree&); // Not implemented
779  void operator=(const vtkKdTree&); // Not implemented
780 };
781 #endif
vtkKdNode ** RegionList
Definition: vtkKdTree.h:541
virtual void BuildLocator()=0
int Timing
Definition: vtkKdTree.h:749
vtkDataSet ** LastInputDataSets
Definition: vtkKdTree.h:767
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:44
Perform calculations (mostly intersection calculations) on regions of a 3D binary spatial partitionin...
vtkKdNode * Top
Definition: vtkKdTree.h:540
float MaxWidth
Definition: vtkKdTree.h:760
This class represents an axis-aligned Binary Spatial Partitioning of a 3D space.
Definition: vtkBSPCuts.h:43
abstract class to specify dataset behavior
Definition: vtkDataSet.h:61
int LastNumDataSets
Definition: vtkKdTree.h:765
int NumberOfLocatorPoints
Definition: vtkKdTree.h:755
static int ComputeLevel(vtkKdNode *kd)
int * LastDataSetType
Definition: vtkKdTree.h:769
abstract base class for objects that accelerate spatial searches
Definition: vtkLocator.h:61
int NumberOfRegionsOrLess
Definition: vtkKdTree.h:729
void PrintSelf(ostream &os, vtkIndent indent)
abstract class for specifying dataset behavior
Definition: vtkPointSet.h:44
dynamic, self-adjusting array of vtkIdType
int UserDefinedCuts
Definition: vtkKdTree.h:510
int vtkIdType
Definition: vtkType.h:275
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:83
int LastDataCacheSize
Definition: vtkKdTree.h:766
virtual void FreeSearchStructure()=0
double ProgressScale
Definition: vtkKdTree.h:596
double * LastInputDataInfo
Definition: vtkKdTree.h:770
Timer support and logging.
Definition: vtkTimerLog.h:81
int * CellRegionList
Definition: vtkKdTree.h:744
virtual void SetDataSet(vtkDataSet *)
abstract class to specify cell behavior
Definition: vtkCell.h:61
unsigned long * LastDataSetObserverTags
Definition: vtkKdTree.h:768
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:49
void SetActualLevel()
Definition: vtkKdTree.h:549
virtual vtkDataSet * GetDataSet()
a simple class to control print indentation
Definition: vtkIndent.h:38
vtkBSPCuts * Cuts
Definition: vtkKdTree.h:775
list of point or cell ids
Definition: vtkIdList.h:35
vtkTimerLog * TimerLog
Definition: vtkKdTree.h:543
vtkDataSet * dataSet
Definition: vtkKdTree.h:639
double ProgressOffset
Definition: vtkKdTree.h:605
vtkIdType * LastNumPoints
Definition: vtkKdTree.h:772
int * LocatorIds
Definition: vtkKdTree.h:757
int MinCells
Definition: vtkKdTree.h:746
int NumberOfRegions
Definition: vtkKdTree.h:747
object to represent cell connectivity
Definition: vtkCellArray.h:49
double Progress
Definition: vtkKdTree.h:776
vtkIdList ** boundaryCells
Definition: vtkKdTree.h:643
double * LastBounds
Definition: vtkKdTree.h:771
a Kd-tree spatial decomposition of a set of points
Definition: vtkKdTree.h:80
float * LocatorPoints
Definition: vtkKdTree.h:756
vtkIdList ** cells
Definition: vtkKdTree.h:642
vtkDataSet * GetDataSet()
Definition: vtkKdTree.h:191
vtkIdList * emptyList
Definition: vtkKdTree.h:644
int ValidDirections
Definition: vtkKdTree.h:538
int IncludeRegionBoundaryCells
Definition: vtkKdTree.h:732
vtkIdType * LastNumCells
Definition: vtkKdTree.h:773
static vtkObject * New()
int * LocatorRegionLocation
Definition: vtkKdTree.h:758
int GenerateRepresentationUsingDataBounds
Definition: vtkKdTree.h:735
vtkDataSetCollection * DataSets
Definition: vtkKdTree.h:587
virtual void GenerateRepresentation(int level, vtkPolyData *pd)=0
#define VTKCOMMONDATAMODEL_EXPORT
vtkBSPIntersections * BSPCalculator
Definition: vtkKdTree.h:509
represent and manipulate 3D points
Definition: vtkPoints.h:38
double FudgeFactor
Definition: vtkKdTree.h:750
int NumberOfRegionsOrMore
Definition: vtkKdTree.h:730