1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkAbstractCellLocator.cxx
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 #include "vtkAbstractCellLocator.h"
17 
18 #include "vtkCellArray.h"
19 #include "vtkDataSet.h"
20 #include "vtkGenericCell.h"
21 #include "vtkIdList.h"
22 #include "vtkMath.h"
23 #include "vtkObjectFactory.h"
24 #include "vtkPoints.h"
25 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
vtkAbstractCellLocator()27 vtkAbstractCellLocator::vtkAbstractCellLocator()
28 {
29   this->CacheCellBounds = 0;
30   this->CellBounds = nullptr;
31   this->MaxLevel = 8;
32   this->Level = 0;
33   this->RetainCellLists = 1;
34   this->NumberOfCellsPerNode = 32;
35   this->UseExistingSearchStructure = 0;
36   this->LazyEvaluation = 0;
37   this->GenericCell = vtkGenericCell::New();
38 }
39 //------------------------------------------------------------------------------
~vtkAbstractCellLocator()40 vtkAbstractCellLocator::~vtkAbstractCellLocator()
41 {
42   this->GenericCell->Delete();
43 }
44 //------------------------------------------------------------------------------
StoreCellBounds()45 bool vtkAbstractCellLocator::StoreCellBounds()
46 {
47   if (this->CellBounds)
48     return false;
49   if (!this->DataSet)
50     return false;
51   // Allocate space for cell bounds storage, then fill
52   vtkIdType numCells = this->DataSet->GetNumberOfCells();
53   this->CellBounds = new double[numCells][6];
54   for (vtkIdType j = 0; j < numCells; j++)
55   {
56     this->DataSet->GetCellBounds(j, CellBounds[j]);
57   }
58   return true;
59 }
60 //------------------------------------------------------------------------------
FreeCellBounds()61 void vtkAbstractCellLocator::FreeCellBounds()
62 {
63   delete[] this->CellBounds;
64   this->CellBounds = nullptr;
65 }
66 //------------------------------------------------------------------------------
IntersectWithLine(const double p1[3],const double p2[3],double tol,double & t,double x[3],double pcoords[3],int & subId)67 int vtkAbstractCellLocator::IntersectWithLine(const double p1[3], const double p2[3], double tol,
68   double& t, double x[3], double pcoords[3], int& subId)
69 {
70   vtkIdType cellId = -1;
71   return this->IntersectWithLine(p1, p2, tol, t, x, pcoords, subId, cellId);
72 }
73 //------------------------------------------------------------------------------
IntersectWithLine(const double p1[3],const double p2[3],double tol,double & t,double x[3],double pcoords[3],int & subId,vtkIdType & cellId)74 int vtkAbstractCellLocator::IntersectWithLine(const double p1[3], const double p2[3], double tol,
75   double& t, double x[3], double pcoords[3], int& subId, vtkIdType& cellId)
76 {
77   int returnVal;
78   returnVal = this->IntersectWithLine(p1, p2, tol, t, x, pcoords, subId, cellId, this->GenericCell);
79   return returnVal;
80 }
81 //------------------------------------------------------------------------------
IntersectWithLine(const double vtkNotUsed (p1)[3],const double vtkNotUsed (p2)[3],double vtkNotUsed (tol),double & vtkNotUsed (t),double vtkNotUsed (x)[3],double vtkNotUsed (pcoords)[3],int & vtkNotUsed (subId),vtkIdType & vtkNotUsed (cellId),vtkGenericCell * vtkNotUsed (cell))82 int vtkAbstractCellLocator::IntersectWithLine(const double vtkNotUsed(p1)[3],
83   const double vtkNotUsed(p2)[3], double vtkNotUsed(tol), double& vtkNotUsed(t),
84   double vtkNotUsed(x)[3], double vtkNotUsed(pcoords)[3], int& vtkNotUsed(subId),
85   vtkIdType& vtkNotUsed(cellId), vtkGenericCell* vtkNotUsed(cell))
86 {
87   vtkErrorMacro(<< "The locator class - " << this->GetClassName()
88                 << " does not yet support IntersectWithLine");
89   return 0;
90 }
91 //------------------------------------------------------------------------------
IntersectWithLine(const double vtkNotUsed (p1)[3],const double vtkNotUsed (p2)[3],vtkPoints * vtkNotUsed (points),vtkIdList * vtkNotUsed (cellIds))92 int vtkAbstractCellLocator::IntersectWithLine(const double vtkNotUsed(p1)[3],
93   const double vtkNotUsed(p2)[3], vtkPoints* vtkNotUsed(points), vtkIdList* vtkNotUsed(cellIds))
94 {
95   vtkErrorMacro(<< "The locator class - " << this->GetClassName()
96                 << " does not yet support this IntersectWithLine interface");
97   return 0;
98 }
99 //------------------------------------------------------------------------------
FindClosestPoint(const double x[3],double closestPoint[3],vtkIdType & cellId,int & subId,double & dist2)100 void vtkAbstractCellLocator::FindClosestPoint(
101   const double x[3], double closestPoint[3], vtkIdType& cellId, int& subId, double& dist2)
102 {
103   this->FindClosestPoint(x, closestPoint, this->GenericCell, cellId, subId, dist2);
104 }
105 //------------------------------------------------------------------------------
FindClosestPoint(const double vtkNotUsed (x)[3],double vtkNotUsed (closestPoint)[3],vtkGenericCell * vtkNotUsed (cell),vtkIdType & vtkNotUsed (cellId),int & vtkNotUsed (subId),double & vtkNotUsed (dist2))106 void vtkAbstractCellLocator::FindClosestPoint(const double vtkNotUsed(x)[3],
107   double vtkNotUsed(closestPoint)[3], vtkGenericCell* vtkNotUsed(cell),
108   vtkIdType& vtkNotUsed(cellId), int& vtkNotUsed(subId), double& vtkNotUsed(dist2))
109 {
110   vtkErrorMacro(<< "The locator class - " << this->GetClassName()
111                 << " does not yet support FindClosestPoint");
112 }
113 //------------------------------------------------------------------------------
FindClosestPointWithinRadius(double x[3],double radius,double closestPoint[3],vtkGenericCell * cell,vtkIdType & cellId,int & subId,double & dist2)114 vtkIdType vtkAbstractCellLocator::FindClosestPointWithinRadius(double x[3], double radius,
115   double closestPoint[3], vtkGenericCell* cell, vtkIdType& cellId, int& subId, double& dist2)
116 {
117   int inside;
118   return this->FindClosestPointWithinRadius(
119     x, radius, closestPoint, cell, cellId, subId, dist2, inside);
120 }
121 //------------------------------------------------------------------------------
FindClosestPointWithinRadius(double x[3],double radius,double closestPoint[3],vtkIdType & cellId,int & subId,double & dist2)122 vtkIdType vtkAbstractCellLocator::FindClosestPointWithinRadius(
123   double x[3], double radius, double closestPoint[3], vtkIdType& cellId, int& subId, double& dist2)
124 {
125   int inside;
126   return this->FindClosestPointWithinRadius(
127     x, radius, closestPoint, this->GenericCell, cellId, subId, dist2, inside);
128 }
129 //------------------------------------------------------------------------------
FindClosestPointWithinRadius(double vtkNotUsed (x)[3],double vtkNotUsed (radius),double vtkNotUsed (closestPoint)[3],vtkGenericCell * vtkNotUsed (cell),vtkIdType & vtkNotUsed (cellId),int & vtkNotUsed (subId),double & vtkNotUsed (dist2),int & vtkNotUsed (inside))130 vtkIdType vtkAbstractCellLocator::FindClosestPointWithinRadius(double vtkNotUsed(x)[3],
131   double vtkNotUsed(radius), double vtkNotUsed(closestPoint)[3], vtkGenericCell* vtkNotUsed(cell),
132   vtkIdType& vtkNotUsed(cellId), int& vtkNotUsed(subId), double& vtkNotUsed(dist2),
133   int& vtkNotUsed(inside))
134 {
135   vtkErrorMacro(<< "The locator class - " << this->GetClassName()
136                 << " does not yet support FindClosestPoint");
137   return 0;
138 }
139 //------------------------------------------------------------------------------
FindCellsWithinBounds(double * vtkNotUsed (bbox),vtkIdList * vtkNotUsed (cells))140 void vtkAbstractCellLocator::FindCellsWithinBounds(
141   double* vtkNotUsed(bbox), vtkIdList* vtkNotUsed(cells))
142 {
143   vtkErrorMacro(<< "The locator class - " << this->GetClassName()
144                 << " does not yet support FindCellsWithinBounds");
145 }
146 //------------------------------------------------------------------------------
FindCellsAlongLine(const double vtkNotUsed (p1)[3],const double vtkNotUsed (p2)[3],double vtkNotUsed (tolerance),vtkIdList * vtkNotUsed (cells))147 void vtkAbstractCellLocator::FindCellsAlongLine(const double vtkNotUsed(p1)[3],
148   const double vtkNotUsed(p2)[3], double vtkNotUsed(tolerance), vtkIdList* vtkNotUsed(cells))
149 {
150   vtkErrorMacro(<< "The locator " << this->GetClassName()
151                 << " does not yet support FindCellsAlongLine");
152 }
153 //------------------------------------------------------------------------------
FindCell(double x[3])154 vtkIdType vtkAbstractCellLocator::FindCell(double x[3])
155 {
156   //
157   double dist2 = 0, pcoords[3], weights[32];
158   return this->FindCell(x, dist2, this->GenericCell, pcoords, weights);
159 }
160 //------------------------------------------------------------------------------
FindCell(double x[3],double tol2,vtkGenericCell * GenCell,double pcoords[3],double * weights)161 vtkIdType vtkAbstractCellLocator::FindCell(
162   double x[3], double tol2, vtkGenericCell* GenCell, double pcoords[3], double* weights)
163 {
164   vtkIdType returnVal = -1;
165   int subId;
166   //
167   static bool warning_shown = false;
168   if (!warning_shown)
169   {
170     vtkWarningMacro(<< this->GetClassName() << " Does not implement FindCell"
171                     << " Reverting to slow DataSet implementation");
172     warning_shown = true;
173   }
174   //
175   if (this->DataSet)
176   {
177     returnVal = this->DataSet->FindCell(x, nullptr, GenCell, 0, tol2, subId, pcoords, weights);
178   }
179   return returnVal;
180 }
181 //------------------------------------------------------------------------------
InsideCellBounds(double x[3],vtkIdType cell_ID)182 bool vtkAbstractCellLocator::InsideCellBounds(double x[3], vtkIdType cell_ID)
183 {
184   double cellBounds[6], delta[3] = { 0.0, 0.0, 0.0 };
185   if (this->DataSet)
186   {
187     this->DataSet->GetCellBounds(cell_ID, cellBounds);
188     return vtkMath::PointIsWithinBounds(x, cellBounds, delta) != 0;
189   }
190   return false;
191 }
192 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)193 void vtkAbstractCellLocator::PrintSelf(ostream& os, vtkIndent indent)
194 {
195   this->Superclass::PrintSelf(os, indent);
196   os << indent << "Cache Cell Bounds: " << this->CacheCellBounds << "\n";
197   os << indent << "Retain Cell Lists: " << (this->RetainCellLists ? "On\n" : "Off\n");
198   os << indent << "Number of Cells Per Bucket: " << this->NumberOfCellsPerNode << "\n";
199   os << indent << "UseExistingSearchStructure: " << this->UseExistingSearchStructure << "\n";
200   os << indent << "LazyEvaluation: " << this->LazyEvaluation << "\n";
201 }
202 //------------------------------------------------------------------------------
203