VTK-m  2.2
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  // NOLINTNEXTLINE(performance-move-const-arg)
41  VTKM_CONT static vtkm::Id3&& ToId3(vtkm::Id3&& src) { return std::move(src); }
42  VTKM_CONT static vtkm::Id3 ToId3(vtkm::Id2&& src) { return vtkm::Id3(src[0], src[1], 1); }
43  VTKM_CONT static vtkm::Id3 ToId3(vtkm::Id&& src) { return vtkm::Id3(src, 1, 1); }
44 
45 public:
46  struct LastCell
47  {
48  };
49 
50  template <vtkm::IdComponent dimensions>
52  const vtkm::Id rowSize,
54  const RectilinearType& coords,
56  vtkm::cont::Token& token)
57  : PlaneSize(planeSize)
58  , RowSize(rowSize)
59  , PointDimensions(ToId3(cellSet.GetPointDimensions()))
60  , Dimensions(dimensions)
61  {
62  auto coordsContPortal = coords.ReadPortal();
63  RectilinearPortalType coordsExecPortal = coords.PrepareForInput(device, token);
64  this->AxisPortals[0] = coordsExecPortal.GetFirstPortal();
65  this->MinPoint[0] = coordsContPortal.GetFirstPortal().Get(0);
66  this->MaxPoint[0] = coordsContPortal.GetFirstPortal().Get(this->PointDimensions[0] - 1);
67 
68  this->AxisPortals[1] = coordsExecPortal.GetSecondPortal();
69  this->MinPoint[1] = coordsContPortal.GetSecondPortal().Get(0);
70  this->MaxPoint[1] = coordsContPortal.GetSecondPortal().Get(this->PointDimensions[1] - 1);
71  if (dimensions == 3)
72  {
73  this->AxisPortals[2] = coordsExecPortal.GetThirdPortal();
74  this->MinPoint[2] = coordsContPortal.GetThirdPortal().Get(0);
75  this->MaxPoint[2] = coordsContPortal.GetThirdPortal().Get(this->PointDimensions[2] - 1);
76  }
77  }
78 
79  VTKM_EXEC
80  inline bool IsInside(const vtkm::Vec3f& point) const
81  {
82  bool inside = true;
83  if (point[0] < this->MinPoint[0] || point[0] > this->MaxPoint[0])
84  inside = false;
85  if (point[1] < this->MinPoint[1] || point[1] > this->MaxPoint[1])
86  inside = false;
87  if (this->Dimensions == 3)
88  {
89  if (point[2] < this->MinPoint[2] || point[2] > this->MaxPoint[2])
90  inside = false;
91  }
92  return inside;
93  }
94 
95  VTKM_EXEC
97  vtkm::Id& cellId,
98  vtkm::Vec3f& parametric,
99  LastCell& vtkmNotUsed(lastCell)) const
100  {
101  return this->FindCell(point, cellId, parametric);
102  }
103 
104  VTKM_EXEC
106  vtkm::Id& cellId,
107  vtkm::Vec3f& parametric) const
108  {
109  if (!this->IsInside(point))
110  {
111  cellId = -1;
113  }
114 
115  // Get the Cell Id from the point.
116  vtkm::Id3 logicalCell(0, 0, 0);
117  for (vtkm::Int32 dim = 0; dim < this->Dimensions; ++dim)
118  {
119  //
120  // When searching for points, we consider the max value of the cell
121  // to be apart of the next cell. If the point falls on the boundary of the
122  // data set, then it is technically inside a cell. This checks for that case
123  //
124  if (point[dim] == MaxPoint[dim])
125  {
126  logicalCell[dim] = this->PointDimensions[dim] - 2;
127  parametric[dim] = static_cast<vtkm::FloatDefault>(1);
128  continue;
129  }
130 
131  vtkm::Id minIndex = 0;
132  vtkm::Id maxIndex = this->PointDimensions[dim] - 1;
133  vtkm::FloatDefault minVal;
134  vtkm::FloatDefault maxVal;
135  minVal = this->AxisPortals[dim].Get(minIndex);
136  maxVal = this->AxisPortals[dim].Get(maxIndex);
137  while (maxIndex > minIndex + 1)
138  {
139  vtkm::Id midIndex = (minIndex + maxIndex) / 2;
140  vtkm::FloatDefault midVal = this->AxisPortals[dim].Get(midIndex);
141  if (point[dim] <= midVal)
142  {
143  maxIndex = midIndex;
144  maxVal = midVal;
145  }
146  else
147  {
148  minIndex = midIndex;
149  minVal = midVal;
150  }
151  }
152  logicalCell[dim] = minIndex;
153  parametric[dim] = (point[dim] - minVal) / (maxVal - minVal);
154  }
155  // Get the actual cellId, from the logical cell index of the cell
156  cellId = logicalCell[2] * this->PlaneSize + logicalCell[1] * this->RowSize + logicalCell[0];
157 
159  }
160 
161 private:
164 
165  AxisPortalType AxisPortals[3];
170 };
171 } //namespace exec
172 } //namespace vtkm
173 
174 #endif //vtkm_exec_celllocatorrectilineargrid_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
vtkm::ErrorCode
ErrorCode
Identifies whether an operation was successful or what type of error it had.
Definition: ErrorCode.h:28
vtkm::exec::CellLocatorRectilinearGrid::AxisPortalType
typename AxisHandle::ReadPortalType AxisPortalType
Definition: exec/CellLocatorRectilinearGrid.h:37
ConnectivityStructured.h
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static vtkm::Id3 && ToId3(vtkm::Id3 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:41
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::cont::CellSetStructured
Defines a 1-, 2-, or 3-dimensional structured grid of points.
Definition: CastAndCall.h:32
vtkm::ErrorCode::Success
@ Success
A successful operation.
vtkm::cont::ArrayHandleCartesianProduct
ArrayHandleCartesianProduct is a specialization of ArrayHandle.
Definition: ArrayHandleCartesianProduct.h:334
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
VecFromPortalPermute.h
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::exec::CellLocatorRectilinearGrid::CellLocatorRectilinearGrid
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:51
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static vtkm::Id3 ToId3(vtkm::Id2 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:42
vtkm::exec::CellLocatorRectilinearGrid::FindCell
vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric, LastCell &) const
Definition: exec/CellLocatorRectilinearGrid.h:96
Bounds.h
CellInside.h
vtkm::exec::CellLocatorRectilinearGrid::IsInside
bool IsInside(const vtkm::Vec3f &point) const
Definition: exec/CellLocatorRectilinearGrid.h:80
vtkm::exec::CellLocatorRectilinearGrid::Dimensions
vtkm::Id Dimensions
Definition: exec/CellLocatorRectilinearGrid.h:169
vtkm::cont::ArrayHandleCartesianProduct::ReadPortalType
typename Superclass::ReadPortalType ReadPortalType
Definition: ArrayHandleCartesianProduct.h:351
ArrayHandleCartesianProduct.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::exec::CellLocatorRectilinearGrid::PointDimensions
vtkm::Id3 PointDimensions
Definition: exec/CellLocatorRectilinearGrid.h:166
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
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:1044
vtkm::ErrorCode::CellNotFound
@ CellNotFound
A cell matching some given criteria could not be found.
vtkm::exec::CellLocatorRectilinearGrid::ToId3
static vtkm::Id3 ToId3(vtkm::Id &&src)
Definition: exec/CellLocatorRectilinearGrid.h:43
vtkm::cont::ArrayHandle::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:433
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::Vec< vtkm::Id, 3 >
vtkm::exec::CellLocatorRectilinearGrid::FindCell
vtkm::ErrorCode FindCell(const vtkm::Vec3f &point, vtkm::Id &cellId, vtkm::Vec3f &parametric) const
Definition: exec/CellLocatorRectilinearGrid.h:105
vtkm::FloatDefault
vtkm::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:236
vtkm::exec::CellLocatorRectilinearGrid::MaxPoint
vtkm::Vec3f MaxPoint
Definition: exec/CellLocatorRectilinearGrid.h:168
vtkm::exec::CellLocatorRectilinearGrid::MinPoint
vtkm::Vec3f MinPoint
Definition: exec/CellLocatorRectilinearGrid.h:167
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::cont::ArrayHandle::PrepareForInput
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:599
CellSetStructured.h
vtkm::exec::CellLocatorRectilinearGrid
Definition: exec/CellLocatorRectilinearGrid.h:31
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::exec::CellLocatorRectilinearGrid::RectilinearPortalType
typename RectilinearType::ReadPortalType RectilinearPortalType
Definition: exec/CellLocatorRectilinearGrid.h:38
ParametricCoordinates.h
vtkm::exec::CellLocatorRectilinearGrid::PlaneSize
vtkm::Id PlaneSize
Definition: exec/CellLocatorRectilinearGrid.h:162
vtkm::exec::CellLocatorRectilinearGrid::LastCell
Definition: exec/CellLocatorRectilinearGrid.h:46
TopologyElementTag.h
vtkm::exec::CellLocatorRectilinearGrid::RowSize
vtkm::Id RowSize
Definition: exec/CellLocatorRectilinearGrid.h:163