00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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_GRAPHICS_EXPORT vtkKdTree : public vtkLocator
00080 {
00081 public:
00082 vtkTypeRevisionMacro(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
00104 vtkGetMacro(NumberOfRegionsOrLess, int);
00105 vtkSetMacro(NumberOfRegionsOrLess, int);
00106
00111 vtkGetMacro(NumberOfRegionsOrMore, int);
00112 vtkSetMacro(NumberOfRegionsOrMore, int);
00113
00119 vtkGetMacro(FudgeFactor, double);
00120 vtkSetMacro(FudgeFactor, double);
00121
00125 vtkGetObjectMacro(Cuts, vtkBSPCuts);
00126
00131 void SetCuts(vtkBSPCuts *cuts);
00132
00134 void OmitXPartitioning();
00135
00137 void OmitYPartitioning();
00138
00140 void OmitZPartitioning();
00141
00143 void OmitXYPartitioning();
00144
00146 void OmitYZPartitioning();
00147
00149 void OmitZXPartitioning();
00150
00152 void OmitNoPartitioning();
00153
00163 virtual void SetDataSet(vtkDataSet *set);
00164
00168 virtual void AddDataSet(vtkDataSet *set);
00169
00171
00172 virtual void RemoveDataSet(int index);
00173 virtual void RemoveDataSet(vtkDataSet *set);
00174 virtual void RemoveAllDataSets();
00176
00178 int GetNumberOfDataSets();
00179
00185 vtkDataSet *GetDataSet(int n);
00186
00189 vtkDataSet *GetDataSet(){ return this->GetDataSet(0); }
00190
00192
00193 vtkGetObjectMacro(DataSets, vtkDataSetCollection);
00195
00198 int GetDataSetIndex(vtkDataSet *set);
00199
00202 void GetBounds(double *bounds);
00203
00210 void SetNewBounds(double *bounds);
00211
00213
00214 vtkGetMacro(NumberOfRegions, int);
00216
00218 void GetRegionBounds(int regionID, double bounds[6]);
00219
00221 void GetRegionDataBounds(int regionID, double bounds[6]);
00222
00224
00225 void PrintTree();
00226 void PrintVerboseTree();
00228
00230 void PrintRegion(int id);
00231
00240 void CreateCellLists(int dataSetIndex, int *regionReqList,
00241 int reqListSize);
00242 void CreateCellLists(vtkDataSet *set, int *regionReqList,
00243 int reqListSize);
00244 void CreateCellLists(int *regionReqList, int listSize);
00245 void CreateCellLists();
00246
00248
00252 vtkSetMacro(IncludeRegionBoundaryCells, int);
00253 vtkGetMacro(IncludeRegionBoundaryCells, int);
00254 vtkBooleanMacro(IncludeRegionBoundaryCells, int);
00256
00258 void DeleteCellLists();
00259
00262 vtkIdList *GetCellList(int regionID);
00263
00271 vtkIdList *GetBoundaryCellList(int regionID);
00272
00274
00288 vtkIdType GetCellLists(vtkIntArray *regions, int set,
00289 vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00290 vtkIdType GetCellLists(vtkIntArray *regions, vtkDataSet *set,
00291 vtkIdList *inRegionCells, vtkIdList *onBoundaryCells);
00292 vtkIdType GetCellLists(vtkIntArray *regions, vtkIdList *inRegionCells,
00293 vtkIdList *onBoundaryCells);
00295
00297
00300 int GetRegionContainingCell(vtkDataSet *set, vtkIdType cellID);
00301 int GetRegionContainingCell(int set, vtkIdType cellID);
00302 int GetRegionContainingCell(vtkIdType cellID);
00304
00309 int *AllGetRegionContainingCell();
00310
00312 int GetRegionContainingPoint(double x, double y, double z);
00313
00317 void BuildLocator();
00318
00330 int MinimalNumberOfConvexSubRegions(vtkIntArray *regionIdList,
00331 double **convexRegionBounds);
00332
00335 VTK_LEGACY(int DepthOrderAllRegions(double *dop, vtkIntArray *orderedList));
00336
00338
00340 VTK_LEGACY(int DepthOrderRegions(vtkIntArray *regionIds, double *dop,
00341 vtkIntArray *orderedList));
00343
00345
00351 int ViewOrderAllRegionsInDirection(const double directionOfProjection[3],
00352 vtkIntArray *orderedList);
00354
00356
00361 int ViewOrderRegionsInDirection(vtkIntArray *regionIds,
00362 const double directionOfProjection[3],
00363 vtkIntArray *orderedList);
00365
00367
00373 int ViewOrderAllRegionsFromPosition(const double directionOfProjection[3],
00374 vtkIntArray *orderedList);
00376
00378
00383 int ViewOrderRegionsFromPosition(vtkIntArray *regionIds,
00384 const double directionOfProjection[3],
00385 vtkIntArray *orderedList);
00387
00389
00397 void BuildLocatorFromPoints(vtkPointSet *pointset);
00398 void BuildLocatorFromPoints(vtkPoints *ptArray);
00399 void BuildLocatorFromPoints(vtkPoints **ptArray, int numPtArrays);
00401
00411 vtkIdTypeArray *BuildMapForDuplicatePoints(float tolerance);
00412
00414
00417 vtkIdType FindPoint(double *x);
00418 vtkIdType FindPoint(double x, double y, double z);
00420
00422
00425 vtkIdType FindClosestPoint(double *x, double &dist2);
00426 vtkIdType FindClosestPoint(double x, double y, double z, double &dist2);
00428
00430
00433 vtkIdType FindClosestPointInRegion(int regionId, double *x, double &dist2);
00434 vtkIdType FindClosestPointInRegion(int regionId, double x, double y, double z,
00435 double &dist2);
00437
00440 vtkIdTypeArray *GetPointsInRegion(int regionId);
00441
00444 void FreeSearchStructure();
00445
00448 void GenerateRepresentation(int level, vtkPolyData *pd);
00449
00452 void GenerateRepresentation(int *regionList, int len, vtkPolyData *pd);
00453
00455
00459 vtkBooleanMacro(GenerateRepresentationUsingDataBounds, int);
00460 vtkSetMacro(GenerateRepresentationUsingDataBounds, int);
00461 vtkGetMacro(GenerateRepresentationUsingDataBounds, int);
00463
00465 virtual void PrintTiming(ostream& os, vtkIndent indent);
00466
00469 virtual int NewGeometry();
00470
00473 virtual int NewGeometry(vtkDataSet **sets, int numDataSets);
00474
00478 virtual void InvalidateGeometry();
00479
00483 static vtkKdNode *CopyTree(vtkKdNode *kd);
00484
00489 void FindPointsInArea(double* area, vtkIdTypeArray* ids, bool clearArray = true);
00490
00491 protected:
00492
00493 vtkKdTree();
00494 ~vtkKdTree();
00495
00496 vtkBSPIntersections *BSPCalculator;
00497 int UserDefinedCuts;
00498
00499 void SetCalculator(vtkKdNode *kd);
00500
00501 int ProcessUserDefinedCuts(double *bounds);
00502
00503 void SetCuts(vtkBSPCuts *cuts, int userDefined);
00504
00508 void UpdateBuildTime();
00509
00515 int DivideTest(int numberOfPoints, int level);
00516
00517
00518 enum {
00519 XDIM = 0,
00520 YDIM = 1,
00521 ZDIM = 2
00522 };
00523
00524
00525 int ValidDirections;
00526
00527 vtkKdNode *Top;
00528 vtkKdNode **RegionList;
00529
00530 vtkTimerLog *TimerLog;
00531
00532 static void DeleteAllDescendants(vtkKdNode *nd);
00533
00534 void BuildRegionList();
00535 virtual int SelectCutDirection(vtkKdNode *kd);
00536 void SetActualLevel(){this->Level = vtkKdTree::ComputeLevel(this->Top);}
00537
00541 void GetRegionsAtLevel(int level, vtkKdNode **nodes);
00542
00546 static void GetLeafNodeIds(vtkKdNode *node, vtkIntArray *ids);
00547
00548
00551 int GetNumberOfCells();
00552
00555 int GetDataSetsNumberOfCells(int set1, int set2);
00556
00561 void ComputeCellCenter(vtkDataSet *set, int cellId, float *center);
00562 void ComputeCellCenter(vtkDataSet *set, int cellId, double *center);
00563
00570 float *ComputeCellCenters();
00571 float *ComputeCellCenters(int set);
00572 float *ComputeCellCenters(vtkDataSet *set);
00573
00574 vtkDataSetCollection *DataSets;
00575
00576 virtual void ReportReferences(vtkGarbageCollector*);
00577
00580 void UpdateProgress(double amount);
00581
00583
00584 vtkSetClampMacro(Progress,double,0.0,1.0);
00585 vtkGetMacro(Progress,double);
00587
00588 protected:
00589
00590
00591
00592
00593 double ProgressScale;
00594 double ProgressOffset;
00595
00596
00597
00598
00599 void UpdateSubOperationProgress(double amount);
00600
00601 static void _SetNewBounds(vtkKdNode *kd, double *b, int *fixDim);
00602 static void CopyChildNodes(vtkKdNode *to, vtkKdNode *from);
00603 static void CopyKdNode(vtkKdNode *to, vtkKdNode *from);
00604 static void SetDataBoundsToSpatialBounds(vtkKdNode *kd);
00605 static void ZeroNumberOfPoints(vtkKdNode *kd);
00606
00607
00608
00609 void FindPointsInArea(vtkKdNode* node, double* area, vtkIdTypeArray* ids);
00610
00611
00612 void AddAllPointsInRegion(vtkKdNode* node, vtkIdTypeArray* ids);
00613
00614 int DivideRegion(vtkKdNode *kd, float *c1, int *ids, int nlevels);
00615
00616 void DoMedianFind(vtkKdNode *kd, float *c1, int *ids, int d1, int d2, int d3);
00617
00618 void SelfRegister(vtkKdNode *kd);
00619
00620 struct _cellList{
00621 vtkDataSet *dataSet;
00622 int *regionIds;
00623 int nRegions;
00624 vtkIdList **cells;
00625 vtkIdList **boundaryCells;
00626 vtkIdList *emptyList;
00627 };
00628
00629
00630 void InitializeCellLists();
00631 vtkIdList *GetList(int regionId, vtkIdList **which);
00632
00633 void ComputeCellCenter(vtkCell* cell, double *center, double *weights);
00634
00635 void GenerateRepresentationDataBounds(int level, vtkPolyData *pd);
00636 void _generateRepresentationDataBounds(vtkKdNode *kd, vtkPoints *pts,
00637 vtkCellArray *polys, int level);
00638
00639 void GenerateRepresentationWholeSpace(int level, vtkPolyData *pd);
00640 void _generateRepresentationWholeSpace(vtkKdNode *kd, vtkPoints *pts,
00641 vtkCellArray *polys, int level);
00642
00643 void AddPolys(vtkKdNode *kd, vtkPoints *pts, vtkCellArray *polys);
00644
00645 void _printTree(int verbose);
00646
00647 int SearchNeighborsForDuplicate(int regionId, float *point,
00648 int **pointsSoFar, int *len,
00649 float tolerance, float tolerance2);
00650
00651 int SearchRegionForDuplicate(float *point, int *pointsSoFar,
00652 int len, float tolerance2);
00653
00654 int _FindClosestPointInRegion(int regionId,
00655 double x, double y, double z, double &dist2);
00656
00657 int FindClosestPointInSphere(double x, double y, double z, double radius,
00658 int skipRegion, double &dist2);
00659
00660 int _ViewOrderRegionsInDirection(vtkIntArray *IdsOfInterest,
00661 const double dop[3],
00662 vtkIntArray *orderedList);
00663
00664 static int __ViewOrderRegionsInDirection(vtkKdNode *node, vtkIntArray *list,
00665 vtkIntArray *IdsOfInterest,
00666 const double dir[3], int nextId);
00667
00668 int _ViewOrderRegionsFromPosition(vtkIntArray *IdsOfInterest,
00669 const double pos[3],
00670 vtkIntArray *orderedList);
00671
00672 static int __ViewOrderRegionsFromPosition(vtkKdNode *node, vtkIntArray *list,
00673 vtkIntArray *IdsOfInterest,
00674 const double pos[3], int nextId);
00675
00676 static int __ConvexSubRegions(int *ids, int len, vtkKdNode *tree, vtkKdNode **nodes);
00677 static int FoundId(vtkIntArray *idArray, int id);
00678
00679 void NewParitioningRequest(int req);
00680 void SetInputDataInfo(int i,
00681 int dims[3], double origin[3], double spacing[3]);
00682 int CheckInputDataInfo(int i,
00683 int dims[3], double origin[3], double spacing[3]);
00684 void ClearLastBuildCache();
00685
00686
00687 static void __printTree(vtkKdNode *kd, int depth, int verbose);
00688
00689
00690 static int MidValue(int dim, float *c1, int nvals, double &coord);
00691
00692 static int Select(int dim, float *c1, int *ids, int nvals, double &coord);
00693 static float FindMaxLeftHalf(int dim, float *c1, int K);
00694 static void _Select(int dim, float *X, int *ids, int L, int R, int K);
00695
00696
00697 static int ComputeLevel(vtkKdNode *kd);
00698 static int SelfOrder(int id, vtkKdNode *kd);
00699 static int findRegion(vtkKdNode *node, float x, float y, float z);
00700 static int findRegion(vtkKdNode *node, double x, double y, double z);
00701
00702
00703 static vtkKdNode **_GetRegionsAtLevel(int level, vtkKdNode **nodes,
00704 vtkKdNode *kd);
00705
00706 static void AddNewRegions(vtkKdNode *kd, float *c1,
00707 int midpt, int dim, double coord);
00708
00709 void NewPartitioningRequest(int req);
00710
00711 int NumberOfRegionsOrLess;
00712 int NumberOfRegionsOrMore;
00713
00714 int IncludeRegionBoundaryCells;
00715 double CellBoundsCache[6];
00716
00717 int GenerateRepresentationUsingDataBounds;
00718
00719
00720 struct _cellList CellList;
00721
00722
00723
00724
00725
00726 int *CellRegionList;
00727
00728 int MinCells;
00729 int NumberOfRegions;
00730
00731 int Timing;
00732 double FudgeFactor;
00733
00734
00735
00736
00737 int NumberOfLocatorPoints;
00738 float *LocatorPoints;
00739 int *LocatorIds;
00740 int *LocatorRegionLocation;
00741
00742 float MaxWidth;
00743
00744
00745
00746
00747 int LastNumDataSets;
00748 int LastDataCacheSize;
00749 vtkDataSet **LastInputDataSets;
00750 unsigned long *LastDataSetObserverTags;
00751 int *LastDataSetType;
00752 double *LastInputDataInfo;
00753 double *LastBounds;
00754 vtkIdType *LastNumPoints;
00755 vtkIdType *LastNumCells;
00756
00757 vtkBSPCuts *Cuts;
00758 double Progress;
00759
00760 vtkKdTree(const vtkKdTree&);
00761 void operator=(const vtkKdTree&);
00762 };
00763 #endif