VTK
dox/Filtering/vtkKdTree.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkKdTree.h
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 
00061 #ifndef __vtkKdTree_h
00062 #define __vtkKdTree_h
00063 
00064 #include "vtkLocator.h"
00065 
00066 class vtkTimerLog;
00067 class vtkIdList;
00068 class vtkIdTypeArray;
00069 class vtkIntArray;
00070 class vtkPointSet;
00071 class vtkPoints;
00072 class vtkCellArray;
00073 class vtkCell;
00074 class vtkKdNode;
00075 class vtkBSPCuts;
00076 class vtkBSPIntersections;
00077 class vtkDataSetCollection;
00078 
00079 class VTK_FILTERING_EXPORT vtkKdTree : public vtkLocator
00080 {
00081 public:
00082   vtkTypeMacro(vtkKdTree, vtkLocator);
00083   void PrintSelf(ostream& os, vtkIndent indent);
00084 
00085   static vtkKdTree *New();
00086 
00088 
00089   vtkBooleanMacro(Timing, int);
00090   vtkSetMacro(Timing, int);
00091   vtkGetMacro(Timing, int);
00093 
00095 
00096   vtkSetMacro(MinCells, int);
00097   vtkGetMacro(MinCells, int);
00099 
00105   vtkGetMacro(NumberOfRegionsOrLess, int);
00106   vtkSetMacro(NumberOfRegionsOrLess, int);
00107 
00112   vtkGetMacro(NumberOfRegionsOrMore, int);
00113   vtkSetMacro(NumberOfRegionsOrMore, int);
00114   
00120   vtkGetMacro(FudgeFactor, double);
00121   vtkSetMacro(FudgeFactor, double);
00122 
00126   vtkGetObjectMacro(Cuts, vtkBSPCuts);
00127 
00132   void SetCuts(vtkBSPCuts *cuts);
00133 
00135   void OmitXPartitioning();
00136   
00138   void OmitYPartitioning();
00139 
00141   void OmitZPartitioning();
00142 
00144   void OmitXYPartitioning();
00145 
00147   void OmitYZPartitioning();
00148   
00150   void OmitZXPartitioning();
00151 
00153   void OmitNoPartitioning();
00154 
00164   virtual void SetDataSet(vtkDataSet *set);
00165 
00169   virtual void AddDataSet(vtkDataSet *set);
00170 
00172 
00173   virtual void RemoveDataSet(int index);
00174   virtual void RemoveDataSet(vtkDataSet *set);
00175   virtual void RemoveAllDataSets();
00177 
00179   int GetNumberOfDataSets();
00180 
00186   vtkDataSet *GetDataSet(int n);
00187 
00190   vtkDataSet *GetDataSet(){ return this->GetDataSet(0); }
00191 
00193 
00194   vtkGetObjectMacro(DataSets, vtkDataSetCollection);
00196 
00199   int GetDataSetIndex(vtkDataSet *set);
00200 
00203   void GetBounds(double *bounds);
00204 
00211   void SetNewBounds(double *bounds);
00212 
00214 
00215   vtkGetMacro(NumberOfRegions, int);
00217 
00219   void GetRegionBounds(int regionID, double bounds[6]);
00220 
00222   void GetRegionDataBounds(int regionID, double bounds[6]);
00223 
00225 
00226   void PrintTree();
00227   void PrintVerboseTree();
00229   
00231   void PrintRegion(int id);
00232   
00241   void CreateCellLists(int dataSetIndex, int *regionReqList, 
00242                        int reqListSize);
00243   void CreateCellLists(vtkDataSet *set, int *regionReqList,
00244                        int reqListSize);
00245   void CreateCellLists(int *regionReqList, int listSize);
00246   void CreateCellLists(); 
00247   
00249 
00253   vtkSetMacro(IncludeRegionBoundaryCells, int);
00254   vtkGetMacro(IncludeRegionBoundaryCells, int);
00255   vtkBooleanMacro(IncludeRegionBoundaryCells, int);
00257 
00259   void DeleteCellLists();
00260 
00263   vtkIdList *GetCellList(int regionID);
00264 
00272   vtkIdList *GetBoundaryCellList(int regionID);
00273 
00275 
00289   vtkIdType GetCellLists(vtkIntArray *regions, int set, 
00290                    vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00291   vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
00292             vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00293   vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
00294                                     vtkIdList *onBoundaryCells);
00296   
00298 
00301   int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
00302   int GetRegionContainingCell(int set, vtkIdType cellID);
00303   int GetRegionContainingCell(vtkIdType cellID);
00305 
00310   int *AllGetRegionContainingCell();
00311 
00313   int GetRegionContainingPoint(double x, double y, double z);
00314   
00318   void BuildLocator();
00319 
00331   int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
00332                                       double **convexRegionBounds);
00333 
00336   VTK_LEGACY(int DepthOrderAllRegions(double *dop, vtkIntArray *orderedList));
00337 
00339 
00341   VTK_LEGACY(int DepthOrderRegions(vtkIntArray *regionIds, double *dop,
00342                                    vtkIntArray *orderedList));
00344 
00346 
00352   int ViewOrderAllRegionsInDirection(const double directionOfProjection[3],
00353                                      vtkIntArray *orderedList);
00355 
00357 
00362   int ViewOrderRegionsInDirection(vtkIntArray *regionIds,
00363                                   const double directionOfProjection[3],
00364                                   vtkIntArray *orderedList);
00366 
00368 
00374   int ViewOrderAllRegionsFromPosition(const double directionOfProjection[3],
00375                                       vtkIntArray *orderedList);
00377 
00379 
00384   int ViewOrderRegionsFromPosition(vtkIntArray *regionIds,
00385                                    const double directionOfProjection[3],
00386                                    vtkIntArray *orderedList);
00388 
00390 
00398   void BuildLocatorFromPoints(vtkPointSet *pointset);
00399   void BuildLocatorFromPoints(vtkPoints *ptArray);
00400   void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
00402   
00412   vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
00413 
00415 
00418   vtkIdType FindPoint(double *x);
00419   vtkIdType FindPoint(double x, double y, double z);
00421 
00423 
00426   vtkIdType FindClosestPoint(double *x, double &dist2);
00427   vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
00429 
00431 
00434   vtkIdType FindClosestPointWithinRadius(
00435     double radius, const double x[3], double& dist2);
00437 
00439 
00442   vtkIdType FindClosestPointInRegion(int regionId, double *x, double &dist2);
00443   vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z, 
00444                                      double &dist2);
00446 
00451   void FindPointsWithinRadius(double R, const double x[3], vtkIdList *result);
00452 
00459   void FindClosestNPoints(int N, const double x[3], vtkIdList *result);
00460 
00463   vtkIdTypeArray *GetPointsInRegion(int regionId);
00464 
00467   void FreeSearchStructure();
00468   
00471   void GenerateRepresentation(int level, vtkPolyData *pd);
00472   
00475   void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
00476 
00478 
00482   vtkBooleanMacro(GenerateRepresentationUsingDataBounds, int);
00483   vtkSetMacro(GenerateRepresentationUsingDataBounds, int);
00484   vtkGetMacro(GenerateRepresentationUsingDataBounds, int);
00486 
00488   virtual void PrintTiming(ostream& os, vtkIndent indent);
00489 
00492   virtual int NewGeometry();
00493 
00496   virtual int NewGeometry(vtkDataSet **sets, int numDataSets);
00497 
00501   virtual void InvalidateGeometry();
00502 
00506   static vtkKdNode *CopyTree(vtkKdNode *kd);
00507 
00512   void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
00513 
00514 protected:
00515 
00516   vtkKdTree();
00517   ~vtkKdTree();
00518 
00519   vtkBSPIntersections *BSPCalculator;
00520   int UserDefinedCuts;
00521 
00522   void SetCalculator(vtkKdNode *kd);
00523 
00524   int ProcessUserDefinedCuts(double *bounds);
00525 
00526   void SetCuts(vtkBSPCuts *cuts, int userDefined);
00527 
00531   void UpdateBuildTime();
00532 
00538   int DivideTest(int numberOfPoints, int level);
00539 
00540 //BTX
00541   enum {
00542     XDIM = 0,  // don't change these values
00543     YDIM = 1,
00544     ZDIM = 2
00545   };
00546 //ETX
00547 
00548   int ValidDirections;
00549 
00550   vtkKdNode *Top;
00551   vtkKdNode **RegionList;      // indexed by region ID
00552 
00553   vtkTimerLog *TimerLog;
00554 
00555   static void DeleteAllDescendants(vtkKdNode *nd);
00556 
00557   void BuildRegionList();
00558   virtual int SelectCutDirection(vtkKdNode *kd);
00559   void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
00560 
00564   void GetRegionsAtLevel(int level, vtkKdNode **nodes);
00565 
00569   static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
00570 
00571 
00574   int GetNumberOfCells();
00575 
00578   int GetDataSetsNumberOfCells(int set1, int set2);
00579 
00584   void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
00585   void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
00586 
00593   float *ComputeCellCenters();
00594   float *ComputeCellCenters(int set);
00595   float *ComputeCellCenters(vtkDataSet *set);
00596 
00597   vtkDataSetCollection *DataSets;
00598 
00599   virtual void ReportReferences(vtkGarbageCollector*);
00600 
00603   void UpdateProgress(double amount);
00604 
00606 
00607   vtkSetClampMacro(Progress,double,0.0,1.0);
00608   vtkGetMacro(Progress,double);
00610 
00611 protected:
00612   // So that each suboperation can report progress
00613   // in [0,1], yet we will be able to report a global 
00614   // progress. Sub-operations must use UpdateSubOperationProgress()
00615   // for this to work.
00616   double ProgressScale;
00617   double ProgressOffset;
00618   
00619   // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
00620   // Actual progress is given by 
00621   // (this->ProgressOffset + this->ProgressScale* amount).
00622   void UpdateSubOperationProgress(double amount);
00623 
00624   static void _SetNewBounds(vtkKdNode *kd, double *b, int *fixDim);
00625   static void CopyChildNodes(vtkKdNode *to, vtkKdNode *from);
00626   static void CopyKdNode(vtkKdNode *to, vtkKdNode *from);
00627   static void SetDataBoundsToSpatialBounds(vtkKdNode *kd);
00628   static void ZeroNumberOfPoints(vtkKdNode *kd);
00629 
00630 //BTX
00631   // Recursive helper for public FindPointsWithinRadius
00632   void FindPointsWithinRadius(vtkKdNode* node, double R2, 
00633                               const double x[3], vtkIdList* ids);  
00634 
00635   // Recursive helper for public FindPointsWithinRadius
00636   void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
00637 
00638   // Recursive helper for public FindPointsInArea
00639   void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
00640 
00641   // Recursive helper for public FindPointsInArea
00642   void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
00643 
00644   int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
00645 
00646   void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
00647 
00648   void SelfRegister(vtkKdNode *kd);
00649 
00650   struct _cellList{
00651     vtkDataSet *dataSet;        // cell lists for which data set
00652     int *regionIds;            // NULL if listing all regions
00653     int nRegions;
00654     vtkIdList **cells;
00655     vtkIdList **boundaryCells;
00656     vtkIdList *emptyList;
00657   };
00658 //ETX
00659 
00660   void InitializeCellLists();
00661   vtkIdList *GetList(int regionId, vtkIdList **which);
00662 
00663   void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
00664 
00665   void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
00666   void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
00667                                          vtkCellArray *polys, int level);
00668 
00669   void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
00670   void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
00671                                          vtkCellArray *polys, int level);
00672 
00673   void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
00674 
00675   void _printTree(int verbose);
00676 
00677   int SearchNeighborsForDuplicate(int regionId, float *point,
00678                                   int **pointsSoFar, int *len, 
00679                                   float tolerance, float tolerance2);
00680 
00681   int SearchRegionForDuplicate(float *point, int *pointsSoFar, 
00682                                int len, float tolerance2);
00683 
00684   int _FindClosestPointInRegion(int regionId, 
00685                           double x, double y, double z, double &dist2);
00686 
00687   int FindClosestPointInSphere(double x, double y, double z, double radius,
00688                                int skipRegion, double &dist2);
00689 
00690   int _ViewOrderRegionsInDirection(vtkIntArray *IdsOfInterest,
00691                                    const double dop[3],
00692                                    vtkIntArray *orderedList);
00693 
00694   static int __ViewOrderRegionsInDirection(vtkKdNode *node, vtkIntArray *list, 
00695                                            vtkIntArray *IdsOfInterest,
00696                                            const double dir[3], int nextId);
00697 
00698   int _ViewOrderRegionsFromPosition(vtkIntArray *IdsOfInterest,
00699                                     const double pos[3],
00700                                     vtkIntArray *orderedList);
00701 
00702   static int __ViewOrderRegionsFromPosition(vtkKdNode *node, vtkIntArray *list, 
00703                                             vtkIntArray *IdsOfInterest,
00704                                             const double pos[3], int nextId);
00705 
00706   static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, vtkKdNode **nodes);
00707   static int FoundId(vtkIntArray *idArray, int id);
00708 
00709   void NewParitioningRequest(int req);
00710   void SetInputDataInfo(int i, 
00711        int dims[3], double origin[3], double spacing[3]);
00712   int CheckInputDataInfo(int i, 
00713        int dims[3], double origin[3], double spacing[3]);
00714   void ClearLastBuildCache();
00715 
00716 //BTX
00717   static void __printTree(vtkKdNode *kd, int depth, int verbose);
00718 //ETX
00719 
00720   static int MidValue(int dim, float *c1, int nvals, double &coord);
00721 
00722   static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
00723   static float FindMaxLeftHalf(int dim, float *c1, int K);
00724   static void _Select(int dim, float *X, int *ids, int L, int R, int K);
00725 
00726 //BTX
00727   static int ComputeLevel(vtkKdNode *kd);
00728   static int SelfOrder(int id, vtkKdNode *kd);
00729   static int findRegion(vtkKdNode *node, float x, float y, float z);
00730   static int findRegion(vtkKdNode *node, double x, double y, double z);
00731 //ETX
00732 
00733   static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes, 
00734                                         vtkKdNode *kd);
00735 
00736   static void AddNewRegions(vtkKdNode *kd, float *c1, 
00737                             int midpt, int dim, double coord);
00738 
00739   void NewPartitioningRequest(int req);
00740 
00741   int NumberOfRegionsOrLess;
00742   int NumberOfRegionsOrMore;
00743 
00744   int IncludeRegionBoundaryCells;
00745   double CellBoundsCache[6];       // to optimize IntersectsCell()
00746 
00747   int GenerateRepresentationUsingDataBounds;
00748 
00749 //BTX
00750   struct _cellList CellList;
00751 //ETX
00752 
00753   // Region Ids, by data set by cell id - this list is large (one
00754   // int per cell) but accelerates creation of cell lists
00755 
00756   int *CellRegionList;
00757 
00758   int MinCells;
00759   int NumberOfRegions;              // number of leaf nodes
00760 
00761   int Timing;
00762   double FudgeFactor;   // a very small distance, relative to the dataset's size
00763 
00764   // These instance variables are used by the special locator created
00765   // to find duplicate points. (BuildLocatorFromPoints)
00766 
00767   int NumberOfLocatorPoints;
00768   float *LocatorPoints;
00769   int *LocatorIds;
00770   int *LocatorRegionLocation;
00771 
00772   float MaxWidth;
00773 
00774   // These Last* values are here to save state so we can
00775   // determine later if k-d tree must be rebuilt.
00776 
00777   int LastNumDataSets;
00778   int LastDataCacheSize;
00779   vtkDataSet **LastInputDataSets;
00780   unsigned long *LastDataSetObserverTags;
00781   int *LastDataSetType;
00782   double *LastInputDataInfo;
00783   double *LastBounds;
00784   vtkIdType *LastNumPoints;
00785   vtkIdType *LastNumCells;
00786 
00787   vtkBSPCuts *Cuts;
00788   double Progress;
00789 
00790   vtkKdTree(const vtkKdTree&); // Not implemented
00791   void operator=(const vtkKdTree&); // Not implemented
00792 };
00793 #endif