VTK-m  2.0
exec/CellLocatorRectilinearGrid.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 vtkm_exec_celllocatorrectilineargrid_h
11 #define vtkm_exec_celllocatorrectilineargrid_h
12 
13 #include <vtkm/Bounds.h>
15 #include <vtkm/Types.h>
17 
20 
21 #include <vtkm/exec/CellInside.h>
24 
25 namespace vtkm
26 {
27 
28 namespace exec
29 {
30 
32 {
33 private:
35  using RectilinearType =
39 
40  VTKM_CONT static vtkm::Id3&& ToId3(vtkm::Id3&& src) { return std::move(src); }
41  VTKM_CONT static vtkm::Id3 ToId3(vtkm::Id2&& src) { return vtkm::Id3(src[0], src[1], 1); }
42  VTKM_CONT static vtkm::Id3 ToId3(vtkm::Id&& src) { return vtkm::Id3(src, 1, 1); }
43 
44 public:
45  struct LastCell
46  {
47  };
48 
49  template <vtkm::IdComponent dimensions>
51  const vtkm::Id rowSize,
53  const RectilinearType& coords,
55  vtkm::cont::Token& token)
56  : PlaneSize(planeSize)
57  , RowSize(rowSize)
58  , PointDimensions(ToId3(cellSet.GetPointDimensions()))
59  , Dimensions(dimensions)
60  {
61  auto coordsContPortal = coords.ReadPortal();
62  RectilinearPortalType coordsExecPortal = coords.PrepareForInput(device, token);
63  this->AxisPortals[0] = coordsExecPortal.GetFirstPortal();
64  this->MinPoint[0] = coordsContPortal.GetFirstPortal().Get(0);
65  this->MaxPoint[0] = coordsContPortal.GetFirstPortal().Get(this->PointDimensions[0] - 1);
66 
67  this->AxisPortals[1] = coordsExecPortal.GetSecondPortal();
68  this->MinPoint[1] = coordsContPortal.GetSecondPortal().Get(0);
69  this->MaxPoint[1] = coordsContPortal.GetSecondPortal().Get(this->PointDimensions[1] - 1);
70  if (dimensions == 3)
71  {
72  this->AxisPortals[2] = coordsExecPortal.GetThirdPortal();
73  this->MinPoint[2] = coordsContPortal.GetThirdPortal().Get(0);
74  this->MaxPoint[2] = coordsContPortal.GetThirdPortal().Get(this->PointDimensions[2] - 1);
75  }
76  }
77 
78  VTKM_EXEC
79  inline bool IsInside(const vtkm::Vec3f& point) const
80  {
81  bool inside = true;
82  if (point[0] < this->MinPoint[0] || point[0] > this->MaxPoint[0])
83  inside = false;
84  if (point[1] < this->MinPoint[1] || point[1] > this->MaxPoint[1])
85  inside = false;
86  if (this->Dimensions == 3)
87  {
88  if (point[2] < this->MinPoint[2] || point[2] > this->MaxPoint[2])
89  inside = false;
90  }
91  return inside;
92  }
93 
94  VTKM_EXEC
96  vtkm::Id& cellId,
97  vtkm::Vec3f& parametric,
98  LastCell& vtkmNotUsed(lastCell)) const
99  {
100  return this->FindCell(point, cellId, parametric);
101  }
102 
103  VTKM_EXEC
105  vtkm::Id& cellId,
106  vtkm::Vec3f& parametric) const
107  {
108  if (!this->IsInside(point))
109  {
110  cellId = -1;
112  }
113 
114  // Get the Cell Id from the point.
115  vtkm::Id3 logicalCell(0, 0, 0);
116  for (vtkm::Int32 dim = 0; dim < this->Dimensions; ++dim)
117  {
118  //
119  // When searching for points, we consider the max value of the cell
120  // to be apart of the next cell. If the point falls on the boundary of the
121  // data set, then it is technically inside a cell. This checks for that case
122  //
123  if (point[dim] == MaxPoint[dim])
124  {
125  logicalCell[dim] = this->PointDimensions[dim] - 2;
126  parametric[dim] = static_cast<vtkm::FloatDefault>(1);
127  continue;
128  }
129 
130  vtkm::Id minIndex = 0;
131  vtkm::Id maxIndex = this->PointDimensions[dim] - 1;
132  vtkm::FloatDefault minVal;
133  vtkm::FloatDefault maxVal;
134  minVal = this->AxisPortals[dim].Get(minIndex);
135  maxVal = this->AxisPortals[dim].Get(maxIndex);
136  while (maxIndex > minIndex + 1)
137  {
138  vtkm::Id midIndex = (minIndex + maxIndex) / 2;
139  vtkm::FloatDefault midVal = this->AxisPortals[dim].Get(midIndex);
140  if (point[dim] <= midVal)
141  {
142  maxIndex = midIndex;
143  maxVal = midVal;
144  }
145  else
146  {
147  minIndex = midIndex;
148  minVal = midVal;
149  }
150  }
151  logicalCell[dim] = minIndex;
152  parametric[dim] = (point[dim] - minVal) / (maxVal - minVal);
153  }
154  // Get the actual cellId, from the logical cell index of the cell
155  cellId = logicalCell[2] * this->PlaneSize + logicalCell[1] * this->RowSize + logicalCell[0];
156 
158  }
159 
160 private:
163 
164  AxisPortalType AxisPortals[3];
169 };
170 } //namespace exec
171 } //namespace vtkm
172 
173 #endif //vtkm_exec_celllocatorrectilineargrid_h
vtkm::cont::ArrayHandle< vtkm::FloatDefault >
vtkm::ErrorCode
ErrorCode
Definition: ErrorCode.h:19
vtkm::exec::CellLocatorRectilinearGrid::FindCell
VTKM_EXEC vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &vtkmNotUsed(lastCell)) const
Definition: exec/CellLocatorRectilinearGrid.h:95
vtkm::exec::CellLocatorRectilinearGrid::AxisPortalType
typename AxisHandle::ReadPortalType AxisPortalType
Definition: exec/CellLocatorRectilinearGrid.h:37
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static VTKM_CONT vtkm::Id3 ToId3(vtkm::Id2 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:41
ConnectivityStructured.h
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::CellLocatorRectilinearGrid::CellLocatorRectilinearGrid
VTKM_CONT CellLocatorRectilinearGrid(const vtkm::Id planeSize, const vtkm::Id rowSize, const vtkm::cont::CellSetStructured< dimensions > &cellSet, const RectilinearType &coords, vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token)
Definition: exec/CellLocatorRectilinearGrid.h:50
Types.h
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static VTKM_CONT vtkm::Id3 && ToId3(vtkm::Id3 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:40
vtkm::cont::ArrayHandle::PrepareForInput
VTKM_CONT ReadPortalType PrepareForInput(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const
Prepares this array to be used as an input to an operation in the execution environment.
Definition: ArrayHandle.h:574
vtkm::exec::CellLocatorRectilinearGrid::FindCell
VTKM_EXEC vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorRectilinearGrid.h:104
vtkm::cont::CellSetStructured
Definition: CastAndCall.h:32
vtkm::ErrorCode::Success
@ Success
vtkm::cont::ArrayHandleCartesianProduct
ArrayHandleCartesianProduct is a specialization of ArrayHandle.
Definition: ArrayHandleCartesianProduct.h:326
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
VecFromPortalPermute.h
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
Bounds.h
CellInside.h
vtkm::exec::CellLocatorRectilinearGrid::Dimensions
vtkm::Id Dimensions
Definition: exec/CellLocatorRectilinearGrid.h:168
ArrayHandleCartesianProduct.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::exec::CellLocatorRectilinearGrid::PointDimensions
vtkm::Id3 PointDimensions
Definition: exec/CellLocatorRectilinearGrid.h:165
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::Id3
vtkm::Vec< vtkm::Id, 3 > Id3
Id3 corresponds to a 3-dimensional index for 3d arrays.
Definition: Types.h:1003
vtkm::ErrorCode::CellNotFound
@ CellNotFound
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::Vec< vtkm::Id, 3 >
vtkm::FloatDefault
vtkm::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:198
vtkm::exec::CellLocatorRectilinearGrid::MaxPoint
vtkm::Vec3f MaxPoint
Definition: exec/CellLocatorRectilinearGrid.h:167
vtkm::exec::CellLocatorRectilinearGrid::MinPoint
vtkm::Vec3f MinPoint
Definition: exec/CellLocatorRectilinearGrid.h:166
vtkm::cont::ArrayHandle::ReadPortal
VTKM_CONT ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:414
vtkm::Int32
int32_t Int32
Definition: Types.h:160
CellSetStructured.h
vtkm::exec::CellLocatorRectilinearGrid
Definition: exec/CellLocatorRectilinearGrid.h:31
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::exec::CellLocatorRectilinearGrid::RectilinearPortalType
typename RectilinearType::ReadPortalType RectilinearPortalType
Definition: exec/CellLocatorRectilinearGrid.h:38
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static VTKM_CONT vtkm::Id3 ToId3(vtkm::Id &&src)
Definition: exec/CellLocatorRectilinearGrid.h:42
ParametricCoordinates.h
vtkm::exec::CellLocatorRectilinearGrid::PlaneSize
vtkm::Id PlaneSize
Definition: exec/CellLocatorRectilinearGrid.h:161
vtkm::exec::CellLocatorRectilinearGrid::IsInside
VTKM_EXEC bool IsInside(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorRectilinearGrid.h:79
vtkm::exec::CellLocatorRectilinearGrid::LastCell
Definition: exec/CellLocatorRectilinearGrid.h:45
TopologyElementTag.h
vtkm::exec::CellLocatorRectilinearGrid::RowSize
vtkm::Id RowSize
Definition: exec/CellLocatorRectilinearGrid.h:162