Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

vtkKdTree.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkKdTree.h,v $
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 /*----------------------------------------------------------------------------
00016  Copyright (c) Sandia Corporation
00017  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
00018 ----------------------------------------------------------------------------*/
00019 
00078 #ifndef __vtkKdTree_h
00079 #define __vtkKdTree_h
00080 
00081 #include "vtkLocator.h"
00082 
00083 class vtkTimerLog;
00084 class vtkIdList;
00085 class vtkIdTypeArray;
00086 class vtkIntArray;
00087 class vtkPoints;
00088 class vtkCellArray;
00089 class vtkRenderer;
00090 class vtkPlanesIntersection;
00091 class vtkPlanes;
00092 class vtkCell;
00093 class vtkCamera;
00094 class vtkKdNode;
00095 
00096 class VTK_PARALLEL_EXPORT vtkKdTree : public vtkLocator
00097 {
00098 public:
00099   vtkTypeRevisionMacro(vtkKdTree, vtkLocator);
00100   void PrintSelf(ostream& os, vtkIndent indent);
00101 
00102   static vtkKdTree *New();
00103 
00105 
00106   vtkBooleanMacro(Timing, int);
00107   vtkSetMacro(Timing, int);
00108   vtkGetMacro(Timing, int);
00110 
00112 
00113   vtkSetMacro(MinCells, int);
00114   vtkGetMacro(MinCells, int);
00116   
00122   vtkGetMacro(FudgeFactor, double);
00123   vtkSetMacro(FudgeFactor, double);
00124 
00126   void OmitXPartitioning();
00127   
00129   void OmitYPartitioning();
00130 
00132   void OmitZPartitioning();
00133 
00135   void OmitXYPartitioning();
00136 
00138   void OmitYZPartitioning();
00139   
00141   void OmitZXPartitioning();
00142 
00144   void OmitNoPartitioning();
00145 
00153   void SetDataSet(vtkDataSet *set);
00154   void SetNthDataSet(int index, vtkDataSet *set);
00155   void RemoveDataSet(int index);
00156   void RemoveDataSet(vtkDataSet *set);
00157   void AddDataSet(vtkDataSet *set);
00158 
00160   int GetNumberOfDataSets(){return this->NumDataSets;};
00161 
00166   vtkDataSet *GetDataSet(int n);
00167   vtkDataSet *GetDataSet(){ return this->GetDataSet(0); }
00168 
00171   int GetDataSet(vtkDataSet *set);
00172 
00174 
00176   void GetBounds(float *bounds);
00177   void GetBounds(double *bounds);
00179 
00181   int GetNumberOfRegions(){ return this->NumRegions; }
00182 
00184 
00185   void GetRegionBounds(int regionID, float bounds[6]);
00186   void GetRegionBounds(int regionID, double bounds[6]);
00188 
00190 
00191   void GetRegionDataBounds(int regionID, float bounds[6]);
00192   void GetRegionDataBounds(int regionID, double bounds[6]);
00194 
00196 
00197   void PrintTree();
00198   void PrintVerboseTree();
00200   
00202   void PrintRegion(int id);
00203   
00212   void CreateCellLists(int DataSet, int *regionReqList, 
00213                        int reqListSize);
00214   void CreateCellLists(vtkDataSet *set, int *regionReqList,
00215                        int reqListSize);
00216   void CreateCellLists(int *regionReqList, int listSize);
00217   void CreateCellLists(); 
00218   
00220 
00224   vtkSetMacro(IncludeRegionBoundaryCells, int);
00225   vtkGetMacro(IncludeRegionBoundaryCells, int);
00226   vtkBooleanMacro(IncludeRegionBoundaryCells, int);
00228 
00230   void DeleteCellLists();
00231 
00234   vtkIdList *GetCellList(int regionID);
00235 
00243   vtkIdList *GetBoundaryCellList(int regionID);
00244 
00246 
00260   vtkIdType GetCellLists(vtkIntArray *regions, int set, 
00261                    vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00262   vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
00263             vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00264   vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
00265                                     vtkIdList *onBoundaryCells);
00267   
00269 
00272   int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
00273   int GetRegionContainingCell(int set, vtkIdType cellID);
00274   int GetRegionContainingCell(vtkIdType cellID);
00276 
00281   int *AllGetRegionContainingCell();
00282 
00284 
00285   int GetRegionContainingPoint(float x, float y, float z);
00286   int GetRegionContainingPoint(double x, double y, double z);
00288   
00295   int DepthOrderAllRegions(vtkCamera *camera, vtkIntArray *orderedList);
00296   
00298 
00302   int DepthOrderRegions(vtkIntArray *regionIds, vtkCamera *camera, 
00303                         vtkIntArray *orderedList);
00305 
00307 
00309   int IntersectsBox(int regionId, float *x); 
00310   int IntersectsBox(int regionId, double *x); 
00311   int IntersectsBox(int regionId, float xmin, float xmax, 
00312                     float ymin, float ymax, 
00313                     float zmin, float zmax); 
00314   int IntersectsBox(int regionId, double xmin, double xmax, 
00315                     double ymin, double ymax, 
00316                     double zmin, double zmax); 
00318 
00320 
00322   int IntersectsBox(int *ids, int len,  float *x); 
00323   int IntersectsBox(int *ids, int len,  double *x); 
00324   int IntersectsBox(int *ids, int len,  float x0, float x1, 
00325                     float y0, float y1, float z0, float z1); 
00326   int IntersectsBox(int *ids, int len,  double x0, double x1, 
00327                     double y0, double y1, double z0, double z1); 
00329 
00331 
00333   int IntersectsSphere2(int regionId, 
00334                         float x, float y, float z, float rSquared);
00335   int IntersectsSphere2(int regionId, 
00336                         double x, double y, double z, double rSquared);
00338 
00340 
00343   int IntersectsSphere2(int *ids, int len, 
00344                         float x, float y, float z, float rSquared);
00345   int IntersectsSphere2(int *ids, int len, 
00346                         double x, double y, double z, double rSquared);
00348 
00350 
00355   int IntersectsRegion(int regionId, vtkPlanes *planes);
00356   int IntersectsRegion(int regionId, vtkPlanes *planes, 
00357                         int nvertices, float *vertices);
00358   int IntersectsRegion(int regionId, vtkPlanes *planes, 
00359                         int nvertices, double *vertices);
00361 
00363 
00368   int IntersectsRegion(int *ids, int len, vtkPlanes *planes);
00369   int IntersectsRegion(int *ids, int len, vtkPlanes *planes, 
00370                        int nvertices, float *vertices);
00371   int IntersectsRegion(int *ids, int len, vtkPlanes *planes, 
00372                        int nvertices, double *vertices);
00374 
00376 
00382   int IntersectsCell(int regionId, vtkCell *cell, int cellRegion=-1);
00383   int IntersectsCell(int regionId, int cellId, int cellRegion=-1);
00384   int IntersectsCell(int regionId, vtkDataSet *Set, int cellId, int cellRegion=-1);
00386 
00388 
00393   int IntersectsCell(int *ids, int len, vtkCell *cell, int cellRegion=-1);
00394   int IntersectsCell(int *ids, int len, int cellId, int cellRegion=-1);
00395   int IntersectsCell(int *ids, int len, vtkDataSet *set, int cellId, int cellRegion=-1);
00397 
00399 
00403   int IntersectsFrustum(int regionId, vtkRenderer *ren, 
00404          float x0, float x1, float y0, float y1);
00405   int IntersectsFrustum(int regionId, vtkRenderer *ren, 
00406          double x0, double x1, double y0, double y1);
00408 
00409 
00411 
00414   int IntersectsFrustum(int *ids, int len,  vtkRenderer *ren, 
00415          float x0, float x1, float y0, float y1);
00416   int IntersectsFrustum(int *ids, int len,  vtkRenderer *ren, 
00417          double x0, double x1, double y0, double y1);
00419 
00421 
00431   int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
00432                                       double **convexRegionBounds);
00434   
00436 
00440   vtkBooleanMacro(ComputeIntersectionsUsingDataBounds, int);
00441   vtkSetMacro(ComputeIntersectionsUsingDataBounds, int);
00442   vtkGetMacro(ComputeIntersectionsUsingDataBounds, int);
00444 
00448   void BuildLocator();
00449 
00451 
00459   void BuildLocatorFromPoints(vtkPoints *ptArray);
00460   void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
00462   
00472   vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
00473 
00475 
00478   vtkIdType FindPoint(float *x);
00479   vtkIdType FindPoint(double *x);
00480   vtkIdType FindPoint(float x, float y, float z);
00481   vtkIdType FindPoint(double x, double y, double z);
00483 
00485 
00488   vtkIdType FindClosestPoint(float *x, float &dist2);
00489   vtkIdType FindClosestPoint(double *x, double &dist2);
00490   vtkIdType FindClosestPoint(float x, float y, float z, float &dist2);
00491   vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
00493 
00495 
00498   vtkIdType FindClosestPointInRegion(int regionId, float *x, float &dist2);
00499   vtkIdType FindClosestPointInRegion(int regionId, float x, float y, float z, 
00500                                      float &dist2);
00502 
00505   vtkIdTypeArray *GetPointsInRegion(int regionId);
00506 
00509   void FreeSearchStructure();
00510   
00513   void GenerateRepresentation(int level, vtkPolyData *pd);
00514   
00517   void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
00518 
00520 
00524   vtkBooleanMacro(GenerateRepresentationUsingDataBounds, int);
00525   vtkSetMacro(GenerateRepresentationUsingDataBounds, int);
00526   vtkGetMacro(GenerateRepresentationUsingDataBounds, int);
00528 
00530   virtual void PrintTiming(ostream& os, vtkIndent indent);
00531 
00534   int NewGeometry();
00535 
00538   int NewGeometry(vtkDataSet **sets, int numDataSets);
00539 
00543 //BTX                             
00544   static inline void SetCellBounds(vtkCell *cell, double *bounds);
00545 //ETX
00546 
00547 protected:
00548 
00549   vtkKdTree();
00550   ~vtkKdTree();
00551 
00555   void UpdateBuildTime();
00556 
00557 //BTX
00558   enum {
00559     XDIM = 0,  // don't change these values
00560     YDIM = 1,
00561     ZDIM = 2
00562   };
00563 //ETX
00564 
00565   int ValidDirections;
00566 
00567 //BTX
00568   vtkKdNode *Top;
00569   vtkKdNode **RegionList;      // indexed by region ID
00570 //ETX
00571 
00572   vtkTimerLog *TimerLog;
00573 
00574   static void DeleteNodes(vtkKdNode *nd);
00575 
00576   void BuildRegionList();
00577   virtual int SelectCutDirection(vtkKdNode *kd);
00578   void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
00579 
00583   void GetRegionsAtLevel(int level, vtkKdNode **nodes);
00584 
00588   static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
00589 
00592   int GetNumberOfCells();
00593 
00596   int GetDataSetsNumberOfCells(int set1, int set2);
00597 
00602   void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
00603   void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
00604 
00611   float *ComputeCellCenters();
00612   float *ComputeCellCenters(int set);
00613   float *ComputeCellCenters(vtkDataSet *set);
00614 
00615 private:
00616 
00617 //BTX
00618   int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
00619 
00620   void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
00621 
00622   void SelfRegister(vtkKdNode *kd);
00623 
00624   struct _cellList{
00625     vtkDataSet *dataSet;        // cell lists for which data set
00626     int *regionIds;            // NULL if listing all regions
00627     int nRegions;
00628     vtkIdList **cells;
00629     vtkIdList **boundaryCells;
00630     vtkIdList *emptyList;
00631   };
00632 //ETX
00633 
00634   void InitializeCellLists();
00635   vtkIdList *GetList(int regionId, vtkIdList **which);
00636 
00637   void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
00638 
00639 //BTX
00640   void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
00641   void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
00642                                          vtkCellArray *polys, int level);
00643 
00644   void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
00645   void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
00646                                          vtkCellArray *polys, int level);
00647 
00648   void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
00649 
00650   int _IntersectsBox(vtkKdNode *node, int *ids, int len,
00651                      double x0, double x1,
00652                      double y0, double y1, 
00653                      double z0, double z1);
00654 
00655   int _IntersectsSphere2(vtkKdNode *node, int *ids, int len,
00656                          double x, double y, double z, double rSquared);
00657 
00658   int _IntersectsRegion(vtkKdNode *node, int *ids, int len,
00659                         vtkPlanesIntersection *pi);
00660 
00661   int _IntersectsCell(vtkKdNode *node, int *ids, int len,
00662                       vtkCell *cell, int cellRegion=-1);
00663 //ETX
00664 
00665   void _printTree(int verbose);
00666 
00667   int _DepthOrderRegions(vtkIntArray *IdsOfInterest,
00668                          vtkCamera *camera, vtkIntArray *orderedList);
00669 
00670   int SearchNeighborsForDuplicate(int regionId, float *point,
00671                                   int **pointsSoFar, int *len, 
00672                                   float tolerance, float tolerance2);
00673 
00674   int SearchRegionForDuplicate(float *point, int *pointsSoFar, 
00675                                int len, float tolerance2);
00676 
00677   int _FindClosestPointInRegion(int regionId, 
00678                           float x, float y, float z, float &dist2);
00679 
00680   int FindClosestPointInSphere(float x, float y, float z, float radius,
00681                                int skipRegion, float &dist2);
00682 
00683   void NewParitioningRequest(int req);
00684   void SetInputDataInfo(int i, 
00685        int dims[3], double origin[3], double spacing[3]);
00686   int CheckInputDataInfo(int i, 
00687        int dims[3], double origin[3], double spacing[3]);
00688   void ClearLastBuildCache();
00689 
00690 //BTX
00691   static void __printTree(vtkKdNode *kd, int depth, int verbose);
00692 //ETX
00693 
00694   static int MidValue(int dim, float *c1, int nvals, double &coord);
00695 
00696   static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
00697   static float FindMaxLeftHalf(int dim, float *c1, int K);
00698   static void _Select(int dim, float *X, int *ids, int L, int R, int K);
00699   static int __DepthOrderRegions(vtkKdNode *node,
00700                                  vtkIntArray *list, vtkIntArray *IdsOfInterest,
00701                                  double *dir, int nextId);
00702   static int FindInSortedList(int *list, int size, int val);
00703   static int FoundId(vtkIntArray *ar, int val);
00704 
00705 //BTX
00706   static int ComputeLevel(vtkKdNode *kd);
00707   static int SelfOrder(int id, vtkKdNode *kd);
00708   static int findRegion(vtkKdNode *node, float x, float y, float z);
00709   static int findRegion(vtkKdNode *node, double x, double y, double z);
00710 //ETX
00711 
00712   static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes, 
00713                                         vtkKdNode *kd);
00714 
00715   static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, 
00716                                 vtkKdNode **nodes);
00717 
00718   static void AddNewRegions(vtkKdNode *kd, float *c1, 
00719                             int midpt, int dim, double coord);
00720 
00721   void NewPartitioningRequest(int req);
00722 
00723   int NumDataSetsAllocated;
00724 
00725   int IncludeRegionBoundaryCells;
00726   double CellBoundsCache[6];       // to optimize IntersectsCell()
00727 
00728   int GenerateRepresentationUsingDataBounds;
00729   int ComputeIntersectionsUsingDataBounds;
00730 
00731 //BTX
00732   struct _cellList CellList;
00733 //ETX
00734 
00735   // Region Ids, by data set by cell id - this list is large (one
00736   // int per cell) but accelerates creation of cell lists
00737 
00738   int *CellRegionList;
00739 
00740   int MinCells;
00741   int NumRegions;              // number of leaf nodes
00742 
00743   int Timing;
00744   double FudgeFactor;   // a very small distance, relative to the dataset's size
00745 
00746   vtkDataSet **DataSets;
00747   int NumDataSets;
00748 
00749   // These instance variables are used by the special locator created
00750   // to find duplicate points. (BuildLocatorFromPoints)
00751 
00752   int NumberOfLocatorPoints;
00753   float *LocatorPoints;
00754   int *LocatorIds;
00755   int *LocatorRegionLocation;
00756 
00757   float MaxWidth;
00758 
00759   // These Last* values are here to save state so we can
00760   // determine later if k-d tree must be rebuilt.
00761 
00762   int LastNumDataSets;
00763   int LastDataCacheSize;
00764   vtkDataSet **LastInputDataSets;
00765   int *LastDataSetType;
00766   double *LastInputDataInfo;
00767   double *LastBounds;
00768   int *LastNumPoints;
00769   int *LastNumCells;
00770 
00771   vtkKdTree(const vtkKdTree&); // Not implemented
00772   void operator=(const vtkKdTree&); // Not implemented
00773 };
00774 
00775 //BTX
00776 
00777 class VTK_PARALLEL_EXPORT vtkKdNode
00778 {
00779 public:
00780   vtkKdNode();
00781   ~vtkKdNode();
00782 
00783   void SetDim(int n) { this->Dim = n; }
00784   int  GetDim() { return this->Dim; }
00785 
00786   void SetNumberOfCells(int n) { this->NumCells = n; }
00787   int  GetNumberOfCells() { return this->NumCells; }
00788 
00789   void SetBounds(double x1,double x2,double y1,double y2,double z1,double z2);
00790   void GetBounds(double *b) const;
00791   void GetBounds(float *b) const;
00792 
00793 
00794   void SetDataBounds(double x1,double x2,double y1,double y2,double z1,double z2);
00795   void SetDataBounds(float *b);  
00796   void GetDataBounds(double *b) const;
00797   void GetDataBounds(float *b) const;
00798 
00799   void PrintNode(int depth);
00800   void PrintVerboseNode(int depth);
00801   void AddChildNodes(vtkKdNode *left, vtkKdNode *right);
00802 
00803   int IntersectsBox(float x1, float x2, float y1, float y2, float z1, float z2,
00804                   int useDataBounds);
00805   int IntersectsBox(double x1,double x2,double y1,double y2,double z1,double z2,
00806                   int useDataBounds);
00807   int IntersectsSphere2(float x, float y, float z, float rSquared,
00808                               int useDataBounds);
00809   int IntersectsSphere2(double x, double y, double z, double rSquared,
00810                               int useDataBounds);
00811   int IntersectsRegion(vtkPlanesIntersection *pi, int useDataBounds);
00812 
00813   int IntersectsCell(vtkCell *cell, int useDataBounds, int cellRegion=-1);
00814 
00815   int ContainsBox(float x1, float x2, float y1, float y2, float z1, float z2,
00816                    int useDataBounds);
00817   int ContainsBox(double x1,double x2,double y1,double y2,double z1,double z2,
00818                    int useDataBounds);
00819 
00820   int ContainsPoint(float x, float y, float z, int useDataBounds);
00821   int ContainsPoint(double x, double y, double z, int useDataBounds);
00822 
00823   float GetDistance2ToBoundary(float x, float y, float z, int useDataBounds);
00824   float GetDistance2ToBoundary(float x, float y, float z, float *boundaryPt,
00825                              int useDataBounds);
00826   float GetDistance2ToInnerBoundary(float x, float y, float z);
00827   float _GetDistance2ToBoundary(float x, float y, float z, float *boundaryPt,
00828                              int innerBoundaryOnly, int useDataBounds);
00829 
00830   static const char *LevelMarker[20];
00831 
00832   double Min[3], Max[3];       // spatial bounds of node
00833   double MinVal[3], MaxVal[3]; // spatial bounds of data
00834   int NumCells;
00835   
00836   vtkKdNode *Up;
00837 
00838   vtkKdNode *Left;
00839   vtkKdNode *Right;
00840 
00841   int Dim;
00842 
00843   int Id;        // region id
00844 
00845   int MinId;
00846   int MaxId;
00847 
00848   double *CellBoundsCache;  // to optimize IntersectsCell
00849 };
00850 //ETX
00851 
00852 #endif