Viskores  1.0
exec/CellLocatorRectilinearGrid.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_exec_celllocatorrectilineargrid_h
19 #define viskores_exec_celllocatorrectilineargrid_h
20 
21 #include <viskores/Bounds.h>
23 #include <viskores/Types.h>
25 
28 
32 
33 namespace viskores
34 {
35 
36 namespace exec
37 {
38 
48 class VISKORES_ALWAYS_EXPORT CellLocatorRectilinearGrid
49 {
50 private:
52  using RectilinearType =
56 
57  // NOLINTNEXTLINE(performance-move-const-arg)
58  VISKORES_CONT static viskores::Id3&& ToId3(viskores::Id3&& src) { return std::move(src); }
60  {
61  return viskores::Id3(src[0], src[1], 1);
62  }
63  VISKORES_CONT static viskores::Id3 ToId3(viskores::Id&& src) { return viskores::Id3(src, 1, 1); }
64 
65 public:
67  struct LastCell
68  {
69  };
70 
71  template <viskores::IdComponent dimensions>
73  const viskores::Id planeSize,
74  const viskores::Id rowSize,
76  const RectilinearType& coords,
78  viskores::cont::Token& token)
79  : PlaneSize(planeSize)
80  , RowSize(rowSize)
81  , PointDimensions(ToId3(cellSet.GetPointDimensions()))
82  , Dimensions(dimensions)
83  {
84  auto coordsContPortal = coords.ReadPortal();
85  RectilinearPortalType coordsExecPortal = coords.PrepareForInput(device, token);
86  this->AxisPortals[0] = coordsExecPortal.GetFirstPortal();
87  this->MinPoint[0] = coordsContPortal.GetFirstPortal().Get(0);
88  this->MaxPoint[0] = coordsContPortal.GetFirstPortal().Get(this->PointDimensions[0] - 1);
89 
90  this->AxisPortals[1] = coordsExecPortal.GetSecondPortal();
91  this->MinPoint[1] = coordsContPortal.GetSecondPortal().Get(0);
92  this->MaxPoint[1] = coordsContPortal.GetSecondPortal().Get(this->PointDimensions[1] - 1);
93  if (dimensions == 3)
94  {
95  this->AxisPortals[2] = coordsExecPortal.GetThirdPortal();
96  this->MinPoint[2] = coordsContPortal.GetThirdPortal().Get(0);
97  this->MaxPoint[2] = coordsContPortal.GetThirdPortal().Get(this->PointDimensions[2] - 1);
98  }
99  }
100 
102  inline bool IsInside(const viskores::Vec3f& point) const
103  {
104  bool inside = true;
105  if (point[0] < this->MinPoint[0] || point[0] > this->MaxPoint[0])
106  inside = false;
107  if (point[1] < this->MinPoint[1] || point[1] > this->MaxPoint[1])
108  inside = false;
109  if (this->Dimensions == 3)
110  {
111  if (point[2] < this->MinPoint[2] || point[2] > this->MaxPoint[2])
112  inside = false;
113  }
114  return inside;
115  }
116 
119  viskores::Id& cellId,
120  viskores::Vec3f& parametric,
121  LastCell& viskoresNotUsed(lastCell)) const
122  {
123  return this->FindCell(point, cellId, parametric);
124  }
125 
128  viskores::Id& cellId,
129  viskores::Vec3f& parametric) const
130  {
131  if (!this->IsInside(point))
132  {
133  cellId = -1;
135  }
136 
137  // Get the Cell Id from the point.
138  viskores::Id3 logicalCell(0, 0, 0);
139  for (viskores::Int32 dim = 0; dim < this->Dimensions; ++dim)
140  {
141  //
142  // When searching for points, we consider the max value of the cell
143  // to be apart of the next cell. If the point falls on the boundary of the
144  // data set, then it is technically inside a cell. This checks for that case
145  //
146  if (point[dim] == MaxPoint[dim])
147  {
148  logicalCell[dim] = this->PointDimensions[dim] - 2;
149  parametric[dim] = static_cast<viskores::FloatDefault>(1);
150  continue;
151  }
152 
153  viskores::Id minIndex = 0;
154  viskores::Id maxIndex = this->PointDimensions[dim] - 1;
155  viskores::FloatDefault minVal;
156  viskores::FloatDefault maxVal;
157  minVal = this->AxisPortals[dim].Get(minIndex);
158  maxVal = this->AxisPortals[dim].Get(maxIndex);
159  while (maxIndex > minIndex + 1)
160  {
161  viskores::Id midIndex = (minIndex + maxIndex) / 2;
162  viskores::FloatDefault midVal = this->AxisPortals[dim].Get(midIndex);
163  if (point[dim] <= midVal)
164  {
165  maxIndex = midIndex;
166  maxVal = midVal;
167  }
168  else
169  {
170  minIndex = midIndex;
171  minVal = midVal;
172  }
173  }
174  logicalCell[dim] = minIndex;
175  parametric[dim] = (point[dim] - minVal) / (maxVal - minVal);
176  }
177  // Get the actual cellId, from the logical cell index of the cell
178  cellId = logicalCell[2] * this->PlaneSize + logicalCell[1] * this->RowSize + logicalCell[0];
179 
181  }
182 
183 private:
186 
187  AxisPortalType AxisPortals[3];
192 };
193 } //namespace exec
194 } //namespace viskores
195 
196 #endif //viskores_exec_celllocatorrectilineargrid_h
viskores::exec::CellLocatorRectilinearGrid::RectilinearPortalType
typename RectilinearType::ReadPortalType RectilinearPortalType
Definition: exec/CellLocatorRectilinearGrid.h:55
viskores::exec::CellLocatorRectilinearGrid::RowSize
viskores::Id RowSize
Definition: exec/CellLocatorRectilinearGrid.h:185
viskores::cont::ArrayHandle::ReadPortal
ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:447
ConnectivityStructured.h
viskores::exec::CellLocatorRectilinearGrid::ToId3
static viskores::Id3 && ToId3(viskores::Id3 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:58
Types.h
viskores::cont::ArrayHandle::PrepareForInput
ReadPortalType PrepareForInput(viskores::cont::DeviceAdapterId device, viskores::cont::Token &token) const
Prepares this array to be used as an input to an operation in the execution environment.
Definition: ArrayHandle.h:615
viskores::cont::ArrayHandleCartesianProduct::ReadPortalType
typename Superclass::ReadPortalType ReadPortalType
Definition: ArrayHandleCartesianProduct.h:362
viskores::exec::CellLocatorRectilinearGrid::PlaneSize
viskores::Id PlaneSize
Definition: exec/CellLocatorRectilinearGrid.h:184
viskoresNotUsed
#define viskoresNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:136
viskores::ErrorCode
ErrorCode
Identifies whether an operation was successful or what type of error it had.
Definition: ErrorCode.h:36
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::exec::CellLocatorRectilinearGrid::CellLocatorRectilinearGrid
CellLocatorRectilinearGrid(const viskores::Id planeSize, const viskores::Id rowSize, const viskores::cont::CellSetStructured< dimensions > &cellSet, const RectilinearType &coords, viskores::cont::DeviceAdapterId device, viskores::cont::Token &token)
Definition: exec/CellLocatorRectilinearGrid.h:72
viskores::cont::ArrayHandle::ReadPortalType
typename StorageType::ReadPortalType ReadPortalType
The type of portal used when accessing data in a read-only mode.
Definition: ArrayHandle.h:325
viskores::exec::CellLocatorRectilinearGrid::IsInside
bool IsInside(const viskores::Vec3f &point) const
Definition: exec/CellLocatorRectilinearGrid.h:102
VecFromPortalPermute.h
viskores::exec::CellLocatorRectilinearGrid::LastCell
Structure capturing the location of a cell in the search structure.
Definition: exec/CellLocatorRectilinearGrid.h:67
viskores::exec::CellLocatorRectilinearGrid::MinPoint
viskores::Vec3f MinPoint
Definition: exec/CellLocatorRectilinearGrid.h:189
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores::exec::CellLocatorRectilinearGrid::AxisPortalType
typename AxisHandle::ReadPortalType AxisPortalType
Definition: exec/CellLocatorRectilinearGrid.h:54
viskores::exec::CellLocatorRectilinearGrid::PointDimensions
viskores::Id3 PointDimensions
Definition: exec/CellLocatorRectilinearGrid.h:188
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Bounds.h
CellInside.h
viskores::exec::CellLocatorRectilinearGrid::Dimensions
viskores::Id Dimensions
Definition: exec/CellLocatorRectilinearGrid.h:191
viskores::exec::CellLocatorRectilinearGrid::FindCell
viskores::ErrorCode FindCell(const viskores::Vec3f &point, viskores::Id &cellId, viskores::Vec3f &parametric) const
Locate the cell containing the provided point.
Definition: exec/CellLocatorRectilinearGrid.h:127
ArrayHandleCartesianProduct.h
viskores::Id3
viskores::Vec< viskores::Id, 3 > Id3
Id3 corresponds to a 3-dimensional index for 3d arrays.
Definition: Types.h:1053
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
viskores::cont::CellSetStructured
Defines a 1-, 2-, or 3-dimensional structured grid of points.
Definition: CastAndCall.h:40
viskores::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:189
viskores::ErrorCode::CellNotFound
@ CellNotFound
A cell matching some given criteria could not be found.
viskores::exec::CellLocatorRectilinearGrid::ToId3
static viskores::Id3 ToId3(viskores::Id &&src)
Definition: exec/CellLocatorRectilinearGrid.h:63
viskores::ErrorCode::Success
@ Success
A successful operation.
viskores::FloatDefault
viskores::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:244
CellSetStructured.h
viskores::exec::CellLocatorRectilinearGrid::FindCell
viskores::ErrorCode FindCell(const viskores::Vec3f &point, viskores::Id &cellId, viskores::Vec3f &parametric, LastCell &) const
Locate the cell containing the provided point.
Definition: exec/CellLocatorRectilinearGrid.h:118
viskores::cont::ArrayHandleCartesianProduct
ArrayHandleCartesianProduct is a specialization of ArrayHandle.
Definition: ArrayHandleCartesianProduct.h:345
viskores::exec::CellLocatorRectilinearGrid
Structure for locating cells.
Definition: exec/CellLocatorRectilinearGrid.h:48
viskores::exec::CellLocatorRectilinearGrid::MaxPoint
viskores::Vec3f MaxPoint
Definition: exec/CellLocatorRectilinearGrid.h:190
viskores::exec::CellLocatorRectilinearGrid::ToId3
static viskores::Id3 ToId3(viskores::Id2 &&src)
Definition: exec/CellLocatorRectilinearGrid.h:59
ParametricCoordinates.h
viskores::Vec< viskores::Id, 3 >
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
VISKORES_EXEC
#define VISKORES_EXEC
Definition: ExportMacros.h:59
TopologyElementTag.h