VTK-m  2.0
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  //See if point is inside the last cell.
93  vtkm::Vec3f pc;
94  if ((lastCell.CellId >= 0) && (lastCell.CellId < this->CellSet.GetNumberOfElements()) &&
95  this->PointInCell(point, lastCell.CellId, pc) == vtkm::ErrorCode::Success)
96  {
97  parametric = pc;
98  cellId = lastCell.CellId;
100  }
101 
102  //See if it's in the last bin.
103  if ((lastCell.BinIdx >= 0) && (lastCell.BinIdx < this->CellIds.GetNumberOfValues()) &&
104  this->PointInBin(point, lastCell.BinIdx, cellId, pc) == vtkm::ErrorCode::Success)
105  {
106  parametric = pc;
107  lastCell.CellId = cellId;
109  }
110 
111  return this->FindCellImpl(point, cellId, parametric, lastCell);
112  }
113 
114  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
115  VTKM_EXEC CellLocatorUniformBins* operator->() { return this; }
116  VTKM_DEPRECATED(1.6, "Locators are no longer pointers. Use . operator.")
117  VTKM_EXEC const CellLocatorUniformBins* operator->() const { return this; }
118 
119 private:
120  VTKM_EXEC bool IsInside(const vtkm::Vec3f& point) const
121  {
122  if (point[0] < this->Origin[0] || point[0] > this->MaxPoint[0])
123  return false;
124  if (point[1] < this->Origin[1] || point[1] > this->MaxPoint[1])
125  return false;
126  if (point[2] < this->Origin[2] || point[2] > this->MaxPoint[2])
127  return false;
128 
129  return true;
130  }
131 
132  VTKM_EXEC
134  vtkm::Id& cellId,
135  vtkm::Vec3f& parametric,
136  LastCell& lastCell) const
137  {
138  lastCell.CellId = -1;
139  lastCell.BinIdx = -1;
140 
141  if (!this->IsInside(point))
142  {
143  cellId = -1;
145  }
146 
147  //Find the bin containing the point.
148  vtkm::Id3 logicalCell(0, 0, 0);
149 
150  vtkm::Vec3f temp;
151  temp = point - this->Origin;
152  temp = temp * this->InvSpacing;
153 
154  //make sure that if we border the upper edge, we sample the correct cell
155  logicalCell = vtkm::Min(vtkm::Id3(temp), this->MaxCellIds);
156 
157  vtkm::Id binIdx =
158  (logicalCell[2] * this->CellDims[1] + logicalCell[1]) * this->CellDims[0] + logicalCell[0];
159 
160  vtkm::Vec3f pc;
161  if (this->PointInBin(point, binIdx, cellId, pc) == vtkm::ErrorCode::Success)
162  {
163  parametric = pc;
164  lastCell.CellId = cellId;
165  lastCell.BinIdx = binIdx;
167  }
168 
170  }
171 
172  template <typename PointsVecType>
173  VTKM_EXEC vtkm::Bounds ComputeCellBounds(const PointsVecType& points) const
174  {
176 
177  vtkm::Bounds bounds;
178  for (vtkm::IdComponent i = 0; i < numPoints; ++i)
179  bounds.Include(points[i]);
180 
181  return bounds;
182  }
183 
184  // TODO: This function may return false positives for non 3D cells as the
185  // tests are done on the projection of the point on the cell. Extra checks
186  // should be added to test if the point actually falls on the cell.
187  template <typename CellShapeTag, typename CoordsType>
189  CellShapeTag cellShape,
190  CoordsType cellPoints,
191  vtkm::Vec3f& parametricCoordinates,
192  bool& inside) const
193  {
194  auto bounds = this->ComputeCellBounds(cellPoints);
195  if (bounds.Contains(point))
196  {
197  VTKM_RETURN_ON_ERROR(vtkm::exec::WorldCoordinatesToParametricCoordinates(
198  cellPoints, point, cellShape, parametricCoordinates));
199  inside = vtkm::exec::CellInside(parametricCoordinates, cellShape);
200  }
201  else
202  {
203  inside = false;
204  }
205  // Return success error code even point is not inside this cell
207  }
208 
209  VTKM_EXEC
211  const vtkm::Id& binIdx,
212  vtkm::Id& cellId,
213  vtkm::Vec3f& parametric) const
214  {
215  auto binIds = this->CellIds.Get(binIdx);
216 
217  for (vtkm::IdComponent i = 0; i < binIds.GetNumberOfComponents(); i++)
218  {
219  vtkm::Id cid = binIds[i];
220  vtkm::Vec3f pc;
221  if (this->PointInCell(point, cid, pc) == vtkm::ErrorCode::Success)
222  {
223  cellId = cid;
224  parametric = pc;
226  }
227  }
228 
230  }
231 
232  VTKM_EXEC
234  const vtkm::Id& cid,
235  vtkm::Vec3f& parametric) const
236  {
237  auto indices = this->CellSet.GetIndices(cid);
238  auto pts = vtkm::make_VecFromPortalPermute(&indices, this->Coords);
239  vtkm::Vec3f pc;
240  bool inside;
241  auto status = this->PointInsideCell(point, this->CellSet.GetCellShape(cid), pts, pc, inside);
242  if (status == vtkm::ErrorCode::Success && inside)
243  {
244  parametric = pc;
246  }
247 
249  }
250 
256 
258 
259  CellStructureType CellSet;
261 };
262 
263 }
264 } // vtkm::exec
265 
266 #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
Definition: ErrorCode.h:19
vtkm::exec::CellLocatorUniformBins::LastCell::BinIdx
vtkm::Id BinIdx
Definition: exec/CellLocatorUniformBins.h:74
vtkm::exec::CellLocatorUniformBins::CellLocatorUniformBins
VTKM_CONT 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::InvSpacing
vtkm::Vec3f InvSpacing
Definition: exec/CellLocatorUniformBins.h:254
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::make_VecFromPortalPermute
VTKM_EXEC VecFromPortalPermute< IndexVecType, PortalType > make_VecFromPortalPermute(const IndexVecType *index, const PortalType &portal)
Definition: VecFromPortalPermute.h:166
vtkm::exec::CellLocatorUniformBins::CellIds
CellIdReadPortal CellIds
Definition: exec/CellLocatorUniformBins.h:257
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::exec::CellLocatorUniformBins::FindCell
VTKM_EXEC vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &lastCell) const
Definition: exec/CellLocatorUniformBins.h:87
vtkm::cont::ArrayHandleGroupVecVariable
Fancy array handle that groups values into vectors of different sizes.
Definition: ArrayHandleGroupVecVariable.h:255
vtkm::ErrorCode::Success
@ Success
vtkm::exec::CellLocatorUniformBins::MaxCellIds
vtkm::Id3 MaxCellIds
Definition: exec/CellLocatorUniformBins.h:255
vtkm::exec::CellLocatorUniformBins::IsInside
VTKM_EXEC bool IsInside(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorUniformBins.h:120
CoordinateSystem.h
vtkm::cont::ArrayHandle::ReadPortalType
typename StorageType::ReadPortalType ReadPortalType
Definition: ArrayHandle.h:294
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::CellLocatorUniformBins::LastCell
Definition: exec/CellLocatorUniformBins.h:71
VecFromPortalPermute.h
vtkm::cont::CoordinateSystem
Definition: CoordinateSystem.h:25
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::exec::CellLocatorUniformBins::PointInCell
VTKM_EXEC vtkm::ErrorCode PointInCell(const vtkm::Vec3f &point, const vtkm::Id &cid, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorUniformBins.h:233
vtkm::exec::CellLocatorUniformBins::Origin
vtkm::Vec3f Origin
Definition: exec/CellLocatorUniformBins.h:252
vtkm::exec::CellLocatorUniformBins::CellIdReadPortal
typename vtkm::cont::ArrayHandleGroupVecVariable< CellIdArrayType, CellIdOffsetArrayType >::ReadPortalType CellIdReadPortal
Definition: exec/CellLocatorUniformBins.h:42
vtkm::Bounds::Include
VTKM_EXEC_CONT void Include(const vtkm::Vec< T, 3 > &point)
Expand bounds to include a point.
Definition: Bounds.h:175
CellInside.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
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:251
vtkm::exec::CellLocatorUniformBins::FindCellImpl
VTKM_EXEC vtkm::ErrorCode FindCellImpl(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &lastCell) const
Definition: exec/CellLocatorUniformBins.h:133
vtkm::ErrorCode::CellNotFound
@ CellNotFound
vtkm::exec::CellLocatorUniformBins::FindCell
VTKM_EXEC vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorUniformBins.h:78
vtkm::exec::CellLocatorUniformBins::ReadPortal
typename vtkm::cont::ArrayHandle< T >::ReadPortalType ReadPortal
Definition: exec/CellLocatorUniformBins.h:33
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::exec::CellLocatorUniformBins::PointInBin
VTKM_EXEC vtkm::ErrorCode PointInBin(const vtkm::Vec3f &point, const vtkm::Id &binIdx, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorUniformBins.h:210
vtkm::exec::CellLocatorUniformBins::MaxPoint
vtkm::Vec3f MaxPoint
Definition: exec/CellLocatorUniformBins.h:253
vtkm::Vec< vtkm::Id, 3 >
vtkm::exec::CellLocatorUniformBins::PointInsideCell
VTKM_EXEC vtkm::ErrorCode PointInsideCell(vtkm::Vec3f point, CellShapeTag cellShape, CoordsType cellPoints, vtkm::Vec3f &parametricCoordinates, bool &inside) const
Definition: exec/CellLocatorUniformBins.h:188
VTKM_RETURN_ON_ERROR
#define VTKM_RETURN_ON_ERROR(call)
Definition: ErrorCode.h:111
vtkm::exec::CellLocatorUniformBins::CellSet
CellStructureType CellSet
Definition: exec/CellLocatorUniformBins.h:259
vtkm::TopologyElementTagCell
A tag used to identify the cell elements in a topology.
Definition: TopologyElementTag.h:24
vtkm::exec::CellLocatorUniformBins
Definition: exec/CellLocatorUniformBins.h:30
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
ParametricCoordinates.h
vtkm::VecTraits::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const VecType &vec)
Number of components in the given vector.
VecTraits.h
vtkm::exec::CellLocatorUniformBins::Coords
CoordsPortalType Coords
Definition: exec/CellLocatorUniformBins.h:260
vtkm::exec::CellLocatorUniformBins::ComputeCellBounds
VTKM_EXEC vtkm::Bounds ComputeCellBounds(const PointsVecType &points) const
Definition: exec/CellLocatorUniformBins.h:173
TopologyElementTag.h