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>
18 
21 #include <vtkm/VecTraits.h>
22 
23 namespace vtkm
24 {
25 namespace exec
26 {
27 
28 //--------------------------------------------------------------------
29 template <typename CellStructureType>
31 {
32  template <typename T>
34 
35  using CoordsPortalType =
36  typename vtkm::cont::CoordinateSystem::MultiplexerArrayType::ReadPortalType;
37 
40  using CellIdReadPortal =
42  CellIdOffsetArrayType>::ReadPortalType;
43 
44 public:
45  template <typename CellSetType>
47  const vtkm::Id3& cellDims,
48  const vtkm::Vec3f& origin,
49  const vtkm::Vec3f& maxPoint,
50  const vtkm::Vec3f& invSpacing,
51  const vtkm::Id3& maxCellIds,
53  const CellSetType& cellSet,
54  const vtkm::cont::CoordinateSystem& coords,
56  vtkm::cont::Token& token)
57  : CellDims(cellDims)
58  , Origin(origin)
59  , MaxPoint(maxPoint)
60  , InvSpacing(invSpacing)
61  , MaxCellIds(maxCellIds)
62  , CellIds(cellIds.PrepareForInput(device, token))
63  , CellSet(cellSet.PrepareForInput(device,
66  token))
67  , Coords(coords.GetDataAsMultiplexer().PrepareForInput(device, token))
68  {
69  }
70 
71  struct LastCell
72  {
73  vtkm::Id CellId = -1;
74  vtkm::Id BinIdx = -1;
75  };
76 
77  VTKM_EXEC
79  vtkm::Id& cellId,
80  vtkm::Vec3f& parametric) const
81  {
82  LastCell lastCell;
83  return this->FindCellImpl(point, cellId, parametric, lastCell);
84  }
85 
86  VTKM_EXEC
88  vtkm::Id& cellId,
89  vtkm::Vec3f& parametric,
90  LastCell& lastCell) const
91  {
92  vtkm::Id binIdx = this->FindBinIdx(point);
93 
94  if (binIdx == -1)
95  {
96  lastCell.CellId = -1;
97  lastCell.BinIdx = -1;
98  cellId = -1;
100  }
101  //See if the point is still in the same bin.
102  else if (binIdx == lastCell.BinIdx && this->LastCellValid(lastCell))
103  {
104  vtkm::Vec3f pc;
105  //Check the last cell first.
106  if (this->PointInCell(point, lastCell.CellId, pc))
107  {
108  parametric = pc;
109  cellId = lastCell.CellId;
111  }
112  //Otherwise, check cells in the bin, but skip lastCell.CellId
113  else if (this->PointInBin(point, lastCell.BinIdx, cellId, pc, lastCell.CellId))
114  {
115  parametric = pc;
117  }
118  }
119  //if cell still not found, drop to the general find cell below.
120 
121  //LastCell not initialized, or not in the same bin: do a full test.
122  //Since already computed the binIdx, re-use it.
123  return this->FindCellImpl(point, cellId, parametric, lastCell, binIdx);
124  }
125 
126  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
127  VTKM_EXEC CellLocatorUniformBins* operator->() { return this; }
128  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
129  VTKM_EXEC const CellLocatorUniformBins* operator->() const { return this; }
130 
131 private:
133  {
134  if (!this->IsInside(point))
135  return -1;
136 
137  vtkm::Vec3f temp;
138  temp = point - this->Origin;
139  temp = temp * this->InvSpacing;
140 
141  //make sure that if we border the upper edge, we sample the correct cell
142  vtkm::Id3 logicalCell = vtkm::Min(vtkm::Id3(temp), this->MaxCellIds);
143 
144  vtkm::Id binIdx =
145  (logicalCell[2] * this->CellDims[1] + logicalCell[1]) * this->CellDims[0] + logicalCell[0];
146 
147  return binIdx;
148  }
149 
150  VTKM_EXEC bool LastCellValid(const LastCell& lastCell) const
151  {
152  return lastCell.BinIdx >= 0 && lastCell.BinIdx < this->CellIds.GetNumberOfValues() &&
153  lastCell.CellId >= 0 && lastCell.CellId < this->CellSet.GetNumberOfElements();
154  }
155 
156  VTKM_EXEC bool IsInside(const vtkm::Vec3f& point) const
157  {
158  if (point[0] < this->Origin[0] || point[0] > this->MaxPoint[0])
159  return false;
160  if (point[1] < this->Origin[1] || point[1] > this->MaxPoint[1])
161  return false;
162  if (point[2] < this->Origin[2] || point[2] > this->MaxPoint[2])
163  return false;
164 
165  return true;
166  }
167 
168  VTKM_EXEC
170  vtkm::Id& cellId,
171  vtkm::Vec3f& parametric,
172  LastCell& lastCell,
173  vtkm::Id ptBinIdx = -1) const
174  {
175  lastCell.CellId = -1;
176  lastCell.BinIdx = -1;
177 
178  //if ptBinIdx is set, use it. Otherwise, compute the bin idx.
179  vtkm::Id binIdx = -1;
180  if (ptBinIdx == -1)
181  binIdx = this->FindBinIdx(point);
182  else
183  binIdx = ptBinIdx;
184 
185  //point not in a bin. return not found.
186  if (binIdx == -1)
187  {
188  cellId = -1;
190  }
191 
192  //search cells in the bin.
193  vtkm::Vec3f pc;
194  if (this->PointInBin(point, binIdx, cellId, pc))
195  {
196  parametric = pc;
197  lastCell.CellId = cellId;
198  lastCell.BinIdx = binIdx;
200  }
201 
203  }
204 
205  template <typename PointsVecType>
206  VTKM_EXEC vtkm::Bounds ComputeCellBounds(const PointsVecType& points) const
207  {
209 
210  vtkm::Bounds bounds;
211  for (vtkm::IdComponent i = 0; i < numPoints; ++i)
212  bounds.Include(points[i]);
213 
214  return bounds;
215  }
216 
217  // TODO: This function may return false positives for non 3D cells as the
218  // tests are done on the projection of the point on the cell. Extra checks
219  // should be added to test if the point actually falls on the cell.
220  template <typename CellShapeTag, typename CoordsType>
222  CellShapeTag cellShape,
223  CoordsType cellPoints,
224  vtkm::Vec3f& parametricCoordinates,
225  bool& inside) const
226  {
227  auto bounds = this->ComputeCellBounds(cellPoints);
228  if (bounds.Contains(point))
229  {
230  VTKM_RETURN_ON_ERROR(vtkm::exec::WorldCoordinatesToParametricCoordinates(
231  cellPoints, point, cellShape, parametricCoordinates));
232  inside = vtkm::exec::CellInside(parametricCoordinates, cellShape);
233  }
234  else
235  {
236  inside = false;
237  }
238  // Return success error code even point is not inside this cell
240  }
241 
242  VTKM_EXEC
243  bool PointInBin(const vtkm::Vec3f& point,
244  const vtkm::Id& binIdx,
245  vtkm::Id& cellId,
246  vtkm::Vec3f& parametric,
247  const vtkm::Id& skipCellId = -1) const
248  {
249  auto binIds = this->CellIds.Get(binIdx);
250 
251  vtkm::Vec3f pc;
252  for (vtkm::IdComponent i = 0; i < binIds.GetNumberOfComponents(); i++)
253  {
254  vtkm::Id cid = binIds[i];
255  if (cid != skipCellId && this->PointInCell(point, cid, pc))
256  {
257  cellId = cid;
258  parametric = pc;
259  return true;
260  }
261  }
262 
263  return false;
264  }
265 
266  VTKM_EXEC
267  bool PointInCell(const vtkm::Vec3f& point, const vtkm::Id& cid, vtkm::Vec3f& parametric) const
268  {
269  auto indices = this->CellSet.GetIndices(cid);
270  auto pts = vtkm::make_VecFromPortalPermute(&indices, this->Coords);
271  vtkm::Vec3f pc;
272  bool inside;
273  auto status = this->PointInsideCell(point, this->CellSet.GetCellShape(cid), pts, pc, inside);
274  if (status == vtkm::ErrorCode::Success && inside)
275  {
276  parametric = pc;
277  return true;
278  }
279 
280  return false;
281  }
282 
288 
290 
291  CellStructureType CellSet;
293 };
294 
295 }
296 } // vtkm::exec
297 
298 #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:74
vtkm::exec::CellLocatorUniformBins::InvSpacing
vtkm::Vec3f InvSpacing
Definition: exec/CellLocatorUniformBins.h:286
vtkm::exec::CellLocatorUniformBins::LastCell::CellId
vtkm::Id CellId
Definition: exec/CellLocatorUniformBins.h:73
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
Definition: exec/CellLocatorUniformBins.h:87
vtkm::exec::CellLocatorUniformBins::CellIds
CellIdReadPortal CellIds
Definition: exec/CellLocatorUniformBins.h:289
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:169
vtkm::exec::CellLocatorUniformBins::ComputeCellBounds
vtkm::Bounds ComputeCellBounds(const PointsVecType &points) const
Definition: exec/CellLocatorUniformBins.h:206
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:287
vtkm::exec::CellLocatorUniformBins::LastCellValid
bool LastCellValid(const LastCell &lastCell) const
Definition: exec/CellLocatorUniformBins.h:150
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
Definition: exec/CellLocatorUniformBins.h:71
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:284
vtkm::exec::CellLocatorUniformBins::CellIdReadPortal
typename vtkm::cont::ArrayHandleGroupVecVariable< CellIdArrayType, CellIdOffsetArrayType >::ReadPortalType CellIdReadPortal
Definition: exec/CellLocatorUniformBins.h:42
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:267
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:243
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:36
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:283
vtkm::exec::CellLocatorUniformBins::IsInside
bool IsInside(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorUniformBins.h:156
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:33
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:285
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:291
vtkm::exec::CellLocatorUniformBins::FindCell
vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorUniformBins.h:78
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:46
vtkm::exec::CellLocatorUniformBins
Definition: exec/CellLocatorUniformBins.h:30
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:132
vtkm::exec::CellLocatorUniformBins::PointInsideCell
vtkm::ErrorCode PointInsideCell(vtkm::Vec3f point, CellShapeTag cellShape, CoordsType cellPoints, vtkm::Vec3f &parametricCoordinates, bool &inside) const
Definition: exec/CellLocatorUniformBins.h:221
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
ParametricCoordinates.h
VecTraits.h
vtkm::exec::CellLocatorUniformBins::Coords
CoordsPortalType Coords
Definition: exec/CellLocatorUniformBins.h:292
TopologyElementTag.h