VTK-m  2.2
exec/CellLocatorUniformBins.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_exec_CellLocatorUniformBins_h
11 #define vtk_m_exec_CellLocatorUniformBins_h
12 
13 #include <vtkm/exec/CellInside.h>
15 
16 #include <vtkm/cont/ArrayHandle.h>
19 
22 #include <vtkm/VecTraits.h>
23 
24 namespace vtkm
25 {
26 namespace exec
27 {
28 
29 //--------------------------------------------------------------------
30 
40 template <typename CellStructureType>
42 {
43  template <typename T>
45 
46  using CoordsPortalType =
47  typename vtkm::cont::CoordinateSystem::MultiplexerArrayType::ReadPortalType;
48 
51  using CellIdReadPortal =
53  CellIdOffsetArrayType>::ReadPortalType;
54 
55 public:
56  template <typename CellSetType>
58  const vtkm::Id3& cellDims,
59  const vtkm::Vec3f& origin,
60  const vtkm::Vec3f& maxPoint,
61  const vtkm::Vec3f& invSpacing,
62  const vtkm::Id3& maxCellIds,
64  const CellSetType& cellSet,
65  const vtkm::cont::CoordinateSystem& coords,
67  vtkm::cont::Token& token)
68  : CellDims(cellDims)
69  , Origin(origin)
70  , MaxPoint(maxPoint)
71  , InvSpacing(invSpacing)
72  , MaxCellIds(maxCellIds)
73  , CellIds(cellIds.PrepareForInput(device, token))
74  , CellSet(cellSet.PrepareForInput(device,
77  token))
78  , Coords(coords.GetDataAsMultiplexer().PrepareForInput(device, token))
79  {
80  }
81 
83  struct LastCell
84  {
85  vtkm::Id CellId = -1;
86  vtkm::Id BinIdx = -1;
87  };
88 
91  vtkm::Id& cellId,
92  vtkm::Vec3f& parametric) const
93  {
94  LastCell lastCell;
95  return this->FindCellImpl(point, cellId, parametric, lastCell);
96  }
97 
100  vtkm::Id& cellId,
101  vtkm::Vec3f& parametric,
102  LastCell& lastCell) const
103  {
104  vtkm::Id binIdx = this->FindBinIdx(point);
105 
106  if (binIdx == -1)
107  {
108  lastCell.CellId = -1;
109  lastCell.BinIdx = -1;
110  cellId = -1;
112  }
113  //See if the point is still in the same bin.
114  else if (binIdx == lastCell.BinIdx && this->LastCellValid(lastCell))
115  {
116  vtkm::Vec3f pc;
117  //Check the last cell first.
118  if (this->PointInCell(point, lastCell.CellId, pc))
119  {
120  parametric = pc;
121  cellId = lastCell.CellId;
123  }
124  //Otherwise, check cells in the bin, but skip lastCell.CellId
125  else if (this->PointInBin(point, lastCell.BinIdx, cellId, pc, lastCell.CellId))
126  {
127  parametric = pc;
129  }
130  }
131  //if cell still not found, drop to the general find cell below.
132 
133  //LastCell not initialized, or not in the same bin: do a full test.
134  //Since already computed the binIdx, re-use it.
135  return this->FindCellImpl(point, cellId, parametric, lastCell, binIdx);
136  }
137 
138  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
139  VTKM_EXEC CellLocatorUniformBins* operator->() { return this; }
140  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
141  VTKM_EXEC const CellLocatorUniformBins* operator->() const { return this; }
142 
143 private:
145  {
146  if (!this->IsInside(point))
147  return -1;
148 
149  vtkm::Vec3f temp;
150  temp = point - this->Origin;
151  temp = temp * this->InvSpacing;
152 
153  //make sure that if we border the upper edge, we sample the correct cell
154  vtkm::Id3 logicalCell = vtkm::Min(vtkm::Id3(temp), this->MaxCellIds);
155 
156  vtkm::Id binIdx =
157  (logicalCell[2] * this->CellDims[1] + logicalCell[1]) * this->CellDims[0] + logicalCell[0];
158 
159  return binIdx;
160  }
161 
162  VTKM_EXEC bool LastCellValid(const LastCell& lastCell) const
163  {
164  return lastCell.BinIdx >= 0 && lastCell.BinIdx < this->CellIds.GetNumberOfValues() &&
165  lastCell.CellId >= 0 && lastCell.CellId < this->CellSet.GetNumberOfElements();
166  }
167 
168  VTKM_EXEC bool IsInside(const vtkm::Vec3f& point) const
169  {
170  if (point[0] < this->Origin[0] || point[0] > this->MaxPoint[0])
171  return false;
172  if (point[1] < this->Origin[1] || point[1] > this->MaxPoint[1])
173  return false;
174  if (point[2] < this->Origin[2] || point[2] > this->MaxPoint[2])
175  return false;
176 
177  return true;
178  }
179 
180  VTKM_EXEC
182  vtkm::Id& cellId,
183  vtkm::Vec3f& parametric,
184  LastCell& lastCell,
185  vtkm::Id ptBinIdx = -1) const
186  {
187  lastCell.CellId = -1;
188  lastCell.BinIdx = -1;
189 
190  //if ptBinIdx is set, use it. Otherwise, compute the bin idx.
191  vtkm::Id binIdx = -1;
192  if (ptBinIdx == -1)
193  binIdx = this->FindBinIdx(point);
194  else
195  binIdx = ptBinIdx;
196 
197  //point not in a bin. return not found.
198  if (binIdx == -1)
199  {
200  cellId = -1;
202  }
203 
204  //search cells in the bin.
205  vtkm::Vec3f pc;
206  if (this->PointInBin(point, binIdx, cellId, pc))
207  {
208  parametric = pc;
209  lastCell.CellId = cellId;
210  lastCell.BinIdx = binIdx;
212  }
213 
215  }
216 
217  template <typename PointsVecType>
218  VTKM_EXEC vtkm::Bounds ComputeCellBounds(const PointsVecType& points) const
219  {
221 
222  vtkm::Bounds bounds;
223  for (vtkm::IdComponent i = 0; i < numPoints; ++i)
224  bounds.Include(points[i]);
225 
226  return bounds;
227  }
228 
229  // TODO: This function may return false positives for non 3D cells as the
230  // tests are done on the projection of the point on the cell. Extra checks
231  // should be added to test if the point actually falls on the cell.
232  template <typename CellShapeTag, typename CoordsType>
234  CellShapeTag cellShape,
235  CoordsType cellPoints,
236  vtkm::Vec3f& parametricCoordinates,
237  bool& inside) const
238  {
239  auto bounds = this->ComputeCellBounds(cellPoints);
240  if (bounds.Contains(point))
241  {
242  VTKM_RETURN_ON_ERROR(vtkm::exec::WorldCoordinatesToParametricCoordinates(
243  cellPoints, point, cellShape, parametricCoordinates));
244  inside = vtkm::exec::CellInside(parametricCoordinates, cellShape);
245  }
246  else
247  {
248  inside = false;
249  }
250  // Return success error code even point is not inside this cell
252  }
253 
254  VTKM_EXEC
255  bool PointInBin(const vtkm::Vec3f& point,
256  const vtkm::Id& binIdx,
257  vtkm::Id& cellId,
258  vtkm::Vec3f& parametric,
259  const vtkm::Id& skipCellId = -1) const
260  {
261  auto binIds = this->CellIds.Get(binIdx);
262 
263  vtkm::Vec3f pc;
264  for (vtkm::IdComponent i = 0; i < binIds.GetNumberOfComponents(); i++)
265  {
266  vtkm::Id cid = binIds[i];
267  if (cid != skipCellId && this->PointInCell(point, cid, pc))
268  {
269  cellId = cid;
270  parametric = pc;
271  return true;
272  }
273  }
274 
275  return false;
276  }
277 
278  VTKM_EXEC
279  bool PointInCell(const vtkm::Vec3f& point, const vtkm::Id& cid, vtkm::Vec3f& parametric) const
280  {
281  auto indices = this->CellSet.GetIndices(cid);
282  auto pts = vtkm::make_VecFromPortalPermute(&indices, this->Coords);
283  vtkm::Vec3f pc;
284  bool inside;
285  auto status = this->PointInsideCell(point, this->CellSet.GetCellShape(cid), pts, pc, inside);
286  if (status == vtkm::ErrorCode::Success && inside)
287  {
288  parametric = pc;
289  return true;
290  }
291 
292  return false;
293  }
294 
300 
302 
303  CellStructureType CellSet;
305 };
306 
307 }
308 } // vtkm::exec
309 
310 #endif //vtk_m_exec_CellLocatorUniformBins_h
vtkm::TopologyElementTagPoint
A tag used to identify the point elements in a topology.
Definition: TopologyElementTag.h:34
vtkm::cont::ArrayHandle< vtkm::Id >
ArrayHandle.h
vtkm::ErrorCode
ErrorCode
Identifies whether an operation was successful or what type of error it had.
Definition: ErrorCode.h:28
vtkm::exec::CellLocatorUniformBins::LastCell::BinIdx
vtkm::Id BinIdx
Definition: exec/CellLocatorUniformBins.h:86
vtkm::exec::CellLocatorUniformBins::InvSpacing
vtkm::Vec3f InvSpacing
Definition: exec/CellLocatorUniformBins.h:298
vtkm::exec::CellLocatorUniformBins::LastCell::CellId
vtkm::Id CellId
Definition: exec/CellLocatorUniformBins.h:85
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::exec::CellLocatorUniformBins::FindCell
vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &lastCell) const
Locate the cell containing the provided point.
Definition: exec/CellLocatorUniformBins.h:99
vtkm::exec::CellLocatorUniformBins::CellIds
CellIdReadPortal CellIds
Definition: exec/CellLocatorUniformBins.h:301
vtkm::exec::CellLocatorUniformBins::FindCellImpl
vtkm::ErrorCode FindCellImpl(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &lastCell, vtkm::Id ptBinIdx=-1) const
Definition: exec/CellLocatorUniformBins.h:181
vtkm::exec::CellLocatorUniformBins::ComputeCellBounds
vtkm::Bounds ComputeCellBounds(const PointsVecType &points) const
Definition: exec/CellLocatorUniformBins.h:218
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleGroupVecVariable
Fancy array handle that groups values into vectors of different sizes.
Definition: ArrayHandleGroupVecVariable.h:271
vtkm::ErrorCode::Success
@ Success
A successful operation.
vtkm::exec::CellLocatorUniformBins::MaxCellIds
vtkm::Id3 MaxCellIds
Definition: exec/CellLocatorUniformBins.h:299
vtkm::exec::CellLocatorUniformBins::LastCellValid
bool LastCellValid(const LastCell &lastCell) const
Definition: exec/CellLocatorUniformBins.h:162
CoordinateSystem.h
vtkm::cont::ArrayHandle::ReadPortalType
typename StorageType::ReadPortalType ReadPortalType
The type of portal used when accessing data in a read-only mode.
Definition: ArrayHandle.h:312
vtkm::exec::CellLocatorUniformBins::LastCell
Structure capturing the location of a cell in the search structure.
Definition: exec/CellLocatorUniformBins.h:83
VecFromPortalPermute.h
vtkm::cont::CoordinateSystem
Manages a coordinate system for a DataSet.
Definition: CoordinateSystem.h:30
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::exec::CellLocatorUniformBins::Origin
vtkm::Vec3f Origin
Definition: exec/CellLocatorUniformBins.h:296
vtkm::exec::CellLocatorUniformBins::CellIdReadPortal
typename vtkm::cont::ArrayHandleGroupVecVariable< CellIdArrayType, CellIdOffsetArrayType >::ReadPortalType CellIdReadPortal
Definition: exec/CellLocatorUniformBins.h:53
vtkm::VecTraits::GetNumberOfComponents
static constexpr vtkm::IdComponent GetNumberOfComponents(const T &)
Returns the number of components in the given vector.
Definition: VecTraits.h:94
CellInside.h
vtkm::exec::CellLocatorUniformBins::PointInCell
bool PointInCell(const vtkm::Vec3f &point, const vtkm::Id &cid, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorUniformBins.h:279
vtkm::exec::CellLocatorUniformBins::PointInBin
bool PointInBin(const vtkm::Vec3f &point, const vtkm::Id &binIdx, vtkm::Id &cellId, vtkm::Vec3f &parametric, const vtkm::Id &skipCellId=-1) const
Definition: exec/CellLocatorUniformBins.h:255
vtkm::make_VecFromPortalPermute
VecFromPortalPermute< IndexVecType, PortalType > make_VecFromPortalPermute(const IndexVecType *index, const PortalType &portal)
Definition: VecFromPortalPermute.h:166
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::exec::CellLocatorUniformBins::CoordsPortalType
typename vtkm::cont::CoordinateSystem::MultiplexerArrayType::ReadPortalType CoordsPortalType
Definition: exec/CellLocatorUniformBins.h:47
vtkm::Bounds
Represent an axis-aligned 3D bounds in space.
Definition: Bounds.h:29
vtkm::exec::CellLocatorUniformBins::CellDims
vtkm::Id3 CellDims
Definition: exec/CellLocatorUniformBins.h:295
vtkm::exec::CellLocatorUniformBins::IsInside
bool IsInside(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorUniformBins.h:168
vtkm::ErrorCode::CellNotFound
@ CellNotFound
A cell matching some given criteria could not be found.
vtkm::exec::CellLocatorUniformBins::ReadPortal
typename vtkm::cont::ArrayHandle< T >::ReadPortalType ReadPortal
Definition: exec/CellLocatorUniformBins.h:44
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::exec::CellLocatorUniformBins::MaxPoint
vtkm::Vec3f MaxPoint
Definition: exec/CellLocatorUniformBins.h:297
vtkm::Vec< vtkm::Id, 3 >
VTKM_RETURN_ON_ERROR
#define VTKM_RETURN_ON_ERROR(call)
Definition: ErrorCode.h:202
vtkm::exec::CellLocatorUniformBins::CellSet
CellStructureType CellSet
Definition: exec/CellLocatorUniformBins.h:303
vtkm::exec::CellLocatorUniformBins::FindCell
vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Locate the cell containing the provided point.
Definition: exec/CellLocatorUniformBins.h:90
ArrayHandleGroupVecVariable.h
vtkm::TopologyElementTagCell
A tag used to identify the cell elements in a topology.
Definition: TopologyElementTag.h:24
vtkm::Bounds::Include
void Include(const vtkm::Vec< T, 3 > &point)
Expand bounds to include a point.
Definition: Bounds.h:186
vtkm::exec::CellLocatorUniformBins::CellLocatorUniformBins
CellLocatorUniformBins(const vtkm::Id3 &cellDims, const vtkm::Vec3f &origin, const vtkm::Vec3f &maxPoint, const vtkm::Vec3f &invSpacing, const vtkm::Id3 &maxCellIds, const vtkm::cont::ArrayHandleGroupVecVariable< CellIdArrayType, CellIdOffsetArrayType > &cellIds, const CellSetType &cellSet, const vtkm::cont::CoordinateSystem &coords, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: exec/CellLocatorUniformBins.h:57
vtkm::exec::CellLocatorUniformBins
Structure for locating cells.
Definition: exec/CellLocatorUniformBins.h:41
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::exec::CellLocatorUniformBins::FindBinIdx
vtkm::Id FindBinIdx(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorUniformBins.h:144
vtkm::exec::CellLocatorUniformBins::PointInsideCell
vtkm::ErrorCode PointInsideCell(vtkm::Vec3f point, CellShapeTag cellShape, CoordsType cellPoints, vtkm::Vec3f &parametricCoordinates, bool &inside) const
Definition: exec/CellLocatorUniformBins.h:233
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
ParametricCoordinates.h
VecTraits.h
vtkm::exec::CellLocatorUniformBins::Coords
CoordsPortalType Coords
Definition: exec/CellLocatorUniformBins.h:304
TopologyElementTag.h