VTK
dox/Common/DataModel/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 "vtkCommonDataModelModule.h" // For export macro
00065 #include "vtkLocator.h"
00066 
00067 class vtkTimerLog;
00068 class vtkIdList;
00069 class vtkIdTypeArray;
00070 class vtkIntArray;
00071 class vtkPointSet;
00072 class vtkPoints;
00073 class vtkCellArray;
00074 class vtkCell;
00075 class vtkKdNode;
00076 class vtkBSPCuts;
00077 class vtkBSPIntersections;
00078 class vtkDataSetCollection;
00079 
00080 class VTKCOMMONDATAMODEL_EXPORT vtkKdTree : public vtkLocator
00081 {
00082 public:
00083   vtkTypeMacro(vtkKdTree, vtkLocator);
00084   void PrintSelf(ostream& os, vtkIndent indent);
00085 
00086   static vtkKdTree *New();
00087 
00089 
00090   vtkBooleanMacro(Timing, int);
00091   vtkSetMacro(Timing, int);
00092   vtkGetMacro(Timing, int);
00094 
00096 
00097   vtkSetMacro(MinCells, int);
00098   vtkGetMacro(MinCells, int);
00100 
00106   vtkGetMacro(NumberOfRegionsOrLess, int);
00107   vtkSetMacro(NumberOfRegionsOrLess, int);
00108 
00113   vtkGetMacro(NumberOfRegionsOrMore, int);
00114   vtkSetMacro(NumberOfRegionsOrMore, int);
00115 
00121   vtkGetMacro(FudgeFactor, double);
00122   vtkSetMacro(FudgeFactor, double);
00123 
00127   vtkGetObjectMacro(Cuts, vtkBSPCuts);
00128 
00133   void SetCuts(vtkBSPCuts *cuts);
00134 
00136   void OmitXPartitioning();
00137 
00139   void OmitYPartitioning();
00140 
00142   void OmitZPartitioning();
00143 
00145   void OmitXYPartitioning();
00146 
00148   void OmitYZPartitioning();
00149 
00151   void OmitZXPartitioning();
00152 
00154   void OmitNoPartitioning();
00155 
00165   virtual void SetDataSet(vtkDataSet *set);
00166 
00170   virtual void AddDataSet(vtkDataSet *set);
00171 
00173 
00174   virtual void RemoveDataSet(int index);
00175   virtual void RemoveDataSet(vtkDataSet *set);
00176   virtual void RemoveAllDataSets();
00178 
00180   int GetNumberOfDataSets();
00181 
00187   vtkDataSet *GetDataSet(int n);
00188 
00191   vtkDataSet *GetDataSet(){ return this->GetDataSet(0); }
00192 
00194 
00195   vtkGetObjectMacro(DataSets, vtkDataSetCollection);
00197 
00200   int GetDataSetIndex(vtkDataSet *set);
00201 
00204   void GetBounds(double *bounds);
00205 
00212   void SetNewBounds(double *bounds);
00213 
00215 
00216   vtkGetMacro(NumberOfRegions, int);
00218 
00220   void GetRegionBounds(int regionID, double bounds[6]);
00221 
00223   void GetRegionDataBounds(int regionID, double bounds[6]);
00224 
00226 
00227   void PrintTree();
00228   void PrintVerboseTree();
00230 
00232   void PrintRegion(int id);
00233 
00242   void CreateCellLists(int dataSetIndex, int *regionReqList,
00243                        int reqListSize);
00244   void CreateCellLists(vtkDataSet *set, int *regionReqList,
00245                        int reqListSize);
00246   void CreateCellLists(int *regionReqList, int listSize);
00247   void CreateCellLists();
00248 
00250 
00254   vtkSetMacro(IncludeRegionBoundaryCells, int);
00255   vtkGetMacro(IncludeRegionBoundaryCells, int);
00256   vtkBooleanMacro(IncludeRegionBoundaryCells, int);
00258 
00260   void DeleteCellLists();
00261 
00264   vtkIdList *GetCellList(int regionID);
00265 
00273   vtkIdList *GetBoundaryCellList(int regionID);
00274 
00276 
00290   vtkIdType GetCellLists(vtkIntArray *regions, int set,
00291                    vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00292   vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
00293             vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00294   vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
00295                                     vtkIdList *onBoundaryCells);
00297 
00299 
00302   int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
00303   int GetRegionContainingCell(int set, vtkIdType cellID);
00304   int GetRegionContainingCell(vtkIdType cellID);
00306 
00311   int *AllGetRegionContainingCell();
00312 
00314   int GetRegionContainingPoint(double x, double y, double z);
00315 
00319   void BuildLocator();
00320 
00332   int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
00333                                       double **convexRegionBounds);
00334 
00336 
00342   int ViewOrderAllRegionsInDirection(const double directionOfProjection[3],
00343                                      vtkIntArray *orderedList);
00345 
00347 
00352   int ViewOrderRegionsInDirection(vtkIntArray *regionIds,
00353                                   const double directionOfProjection[3],
00354                                   vtkIntArray *orderedList);
00356 
00358 
00364   int ViewOrderAllRegionsFromPosition(const double directionOfProjection[3],
00365                                       vtkIntArray *orderedList);
00367 
00369 
00374   int ViewOrderRegionsFromPosition(vtkIntArray *regionIds,
00375                                    const double directionOfProjection[3],
00376                                    vtkIntArray *orderedList);
00378 
00380 
00388   void BuildLocatorFromPoints(vtkPointSet *pointset);
00389   void BuildLocatorFromPoints(vtkPoints *ptArray);
00390   void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
00392 
00402   vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
00403 
00405 
00408   vtkIdType FindPoint(double *x);
00409   vtkIdType FindPoint(double x, double y, double z);
00411 
00413 
00416   vtkIdType FindClosestPoint(double *x, double &dist2);
00417   vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
00419 
00421 
00424   vtkIdType FindClosestPointWithinRadius(
00425     double radius, const double x[3], double& dist2);
00427 
00429 
00432   vtkIdType FindClosestPointInRegion(int regionId, double *x, double &dist2);
00433   vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z,
00434                                      double &dist2);
00436 
00441   void FindPointsWithinRadius(double R, const double x[3], vtkIdList *result);
00442 
00449   void FindClosestNPoints(int N, const double x[3], vtkIdList *result);
00450 
00453   vtkIdTypeArray *GetPointsInRegion(int regionId);
00454 
00457   void FreeSearchStructure();
00458 
00461   void GenerateRepresentation(int level, vtkPolyData *pd);
00462 
00465   void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
00466 
00468 
00472   vtkBooleanMacro(GenerateRepresentationUsingDataBounds, int);
00473   vtkSetMacro(GenerateRepresentationUsingDataBounds, int);
00474   vtkGetMacro(GenerateRepresentationUsingDataBounds, int);
00476 
00478   virtual void PrintTiming(ostream& os, vtkIndent indent);
00479 
00482   virtual int NewGeometry();
00483 
00486   virtual int NewGeometry(vtkDataSet **sets, int numDataSets);
00487 
00491   virtual void InvalidateGeometry();
00492 
00496   static vtkKdNode *CopyTree(vtkKdNode *kd);
00497 
00502   void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
00503 
00504 protected:
00505 
00506   vtkKdTree();
00507   ~vtkKdTree();
00508 
00509   vtkBSPIntersections *BSPCalculator;
00510   int UserDefinedCuts;
00511 
00512   void SetCalculator(vtkKdNode *kd);
00513 
00514   int ProcessUserDefinedCuts(double *bounds);
00515 
00516   void SetCuts(vtkBSPCuts *cuts, int userDefined);
00517 
00521   void UpdateBuildTime();
00522 
00528   int DivideTest(int numberOfPoints, int level);
00529 
00530 //BTX
00531   enum {
00532     XDIM = 0,  // don't change these values
00533     YDIM = 1,
00534     ZDIM = 2
00535   };
00536 //ETX
00537 
00538   int ValidDirections;
00539 
00540   vtkKdNode *Top;
00541   vtkKdNode **RegionList;      // indexed by region ID
00542 
00543   vtkTimerLog *TimerLog;
00544 
00545   static void DeleteAllDescendants(vtkKdNode *nd);
00546 
00547   void BuildRegionList();
00548   virtual int SelectCutDirection(vtkKdNode *kd);
00549   void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
00550 
00554   void GetRegionsAtLevel(int level, vtkKdNode **nodes);
00555 
00559   static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
00560 
00561 
00564   int GetNumberOfCells();
00565 
00568   int GetDataSetsNumberOfCells(int set1, int set2);
00569 
00574   void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
00575   void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
00576 
00583   float *ComputeCellCenters();
00584   float *ComputeCellCenters(int set);
00585   float *ComputeCellCenters(vtkDataSet *set);
00586 
00587   vtkDataSetCollection *DataSets;
00588 
00591   void UpdateProgress(double amount);
00592 
00594 
00595   vtkSetClampMacro(Progress,double,0.0,1.0);
00596   vtkGetMacro(Progress,double);
00598 
00599 protected:
00600   // So that each suboperation can report progress
00601   // in [0,1], yet we will be able to report a global
00602   // progress. Sub-operations must use UpdateSubOperationProgress()
00603   // for this to work.
00604   double ProgressScale;
00605   double ProgressOffset;
00606 
00607   // Update progress for a sub-operation. \c amount goes from 0.0 to 1.0.
00608   // Actual progress is given by
00609   // (this->ProgressOffset + this->ProgressScale* amount).
00610   void UpdateSubOperationProgress(double amount);
00611 
00612   static void _SetNewBounds(vtkKdNode *kd, double *b, int *fixDim);
00613   static void CopyChildNodes(vtkKdNode *to, vtkKdNode *from);
00614   static void CopyKdNode(vtkKdNode *to, vtkKdNode *from);
00615   static void SetDataBoundsToSpatialBounds(vtkKdNode *kd);
00616   static void ZeroNumberOfPoints(vtkKdNode *kd);
00617 
00618 //BTX
00619   // Recursive helper for public FindPointsWithinRadius
00620   void FindPointsWithinRadius(vtkKdNode* node, double R2,
00621                               const double x[3], vtkIdList* ids);
00622 
00623   // Recursive helper for public FindPointsWithinRadius
00624   void AddAllPointsInRegion(vtkKdNode* node, vtkIdList* ids);
00625 
00626   // Recursive helper for public FindPointsInArea
00627   void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
00628 
00629   // Recursive helper for public FindPointsInArea
00630   void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
00631 
00632   int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
00633 
00634   void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
00635 
00636   void SelfRegister(vtkKdNode *kd);
00637 
00638   struct _cellList{
00639     vtkDataSet *dataSet;        // cell lists for which data set
00640     int *regionIds;            // NULL if listing all regions
00641     int nRegions;
00642     vtkIdList **cells;
00643     vtkIdList **boundaryCells;
00644     vtkIdList *emptyList;
00645   };
00646 //ETX
00647 
00648   void InitializeCellLists();
00649   vtkIdList *GetList(int regionId, vtkIdList **which);
00650 
00651   void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
00652 
00653   void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
00654   void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
00655                                          vtkCellArray *polys, int level);
00656 
00657   void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
00658   void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
00659                                          vtkCellArray *polys, int level);
00660 
00661   void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
00662 
00663   void _printTree(int verbose);
00664 
00665   int SearchNeighborsForDuplicate(int regionId, float *point,
00666                                   int **pointsSoFar, int *len,
00667                                   float tolerance, float tolerance2);
00668 
00669   int SearchRegionForDuplicate(float *point, int *pointsSoFar,
00670                                int len, float tolerance2);
00671 
00672   int _FindClosestPointInRegion(int regionId,
00673                           double x, double y, double z, double &dist2);
00674 
00675   int FindClosestPointInSphere(double x, double y, double z, double radius,
00676                                int skipRegion, double &dist2);
00677 
00678   int _ViewOrderRegionsInDirection(vtkIntArray *IdsOfInterest,
00679                                    const double dop[3],
00680                                    vtkIntArray *orderedList);
00681 
00682   static int __ViewOrderRegionsInDirection(vtkKdNode *node, vtkIntArray *list,
00683                                            vtkIntArray *IdsOfInterest,
00684                                            const double dir[3], int nextId);
00685 
00686   int _ViewOrderRegionsFromPosition(vtkIntArray *IdsOfInterest,
00687                                     const double pos[3],
00688                                     vtkIntArray *orderedList);
00689 
00690   static int __ViewOrderRegionsFromPosition(vtkKdNode *node, vtkIntArray *list,
00691                                             vtkIntArray *IdsOfInterest,
00692                                             const double pos[3], int nextId);
00693 
00694   static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, vtkKdNode **nodes);
00695   static int FoundId(vtkIntArray *idArray, int id);
00696 
00697   void NewParitioningRequest(int req);
00698   void SetInputDataInfo(int i,
00699        int dims[3], double origin[3], double spacing[3]);
00700   int CheckInputDataInfo(int i,
00701        int dims[3], double origin[3], double spacing[3]);
00702   void ClearLastBuildCache();
00703 
00704 //BTX
00705   static void __printTree(vtkKdNode *kd, int depth, int verbose);
00706 //ETX
00707 
00708   static int MidValue(int dim, float *c1, int nvals, double &coord);
00709 
00710   static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
00711   static float FindMaxLeftHalf(int dim, float *c1, int K);
00712   static void _Select(int dim, float *X, int *ids, int L, int R, int K);
00713 
00714 //BTX
00715   static int ComputeLevel(vtkKdNode *kd);
00716   static int SelfOrder(int id, vtkKdNode *kd);
00717   static int findRegion(vtkKdNode *node, float x, float y, float z);
00718   static int findRegion(vtkKdNode *node, double x, double y, double z);
00719 //ETX
00720 
00721   static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes,
00722                                         vtkKdNode *kd);
00723 
00724   static void AddNewRegions(vtkKdNode *kd, float *c1,
00725                             int midpt, int dim, double coord);
00726 
00727   void NewPartitioningRequest(int req);
00728 
00729   int NumberOfRegionsOrLess;
00730   int NumberOfRegionsOrMore;
00731 
00732   int IncludeRegionBoundaryCells;
00733   double CellBoundsCache[6];       // to optimize IntersectsCell()
00734 
00735   int GenerateRepresentationUsingDataBounds;
00736 
00737 //BTX
00738   struct _cellList CellList;
00739 //ETX
00740 
00741   // Region Ids, by data set by cell id - this list is large (one
00742   // int per cell) but accelerates creation of cell lists
00743 
00744   int *CellRegionList;
00745 
00746   int MinCells;
00747   int NumberOfRegions;              // number of leaf nodes
00748 
00749   int Timing;
00750   double FudgeFactor;   // a very small distance, relative to the dataset's size
00751 
00752   // These instance variables are used by the special locator created
00753   // to find duplicate points. (BuildLocatorFromPoints)
00754 
00755   int NumberOfLocatorPoints;
00756   float *LocatorPoints;
00757   int *LocatorIds;
00758   int *LocatorRegionLocation;
00759 
00760   float MaxWidth;
00761 
00762   // These Last* values are here to save state so we can
00763   // determine later if k-d tree must be rebuilt.
00764 
00765   int LastNumDataSets;
00766   int LastDataCacheSize;
00767   vtkDataSet **LastInputDataSets;
00768   unsigned long *LastDataSetObserverTags;
00769   int *LastDataSetType;
00770   double *LastInputDataInfo;
00771   double *LastBounds;
00772   vtkIdType *LastNumPoints;
00773   vtkIdType *LastNumCells;
00774 
00775   vtkBSPCuts *Cuts;
00776   double Progress;
00777 
00778   vtkKdTree(const vtkKdTree&); // Not implemented
00779   void operator=(const vtkKdTree&); // Not implemented
00780 };
00781 #endif