VTK-m  2.0
Bounds.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 
11 #ifndef vtk_m_Bounds_h
12 #define vtk_m_Bounds_h
13 
14 #include <vtkm/Range.h>
15 
16 namespace vtkm
17 {
18 
29 struct Bounds
30 {
34 
36  Bounds() {}
37 
38  Bounds(const Bounds&) = default;
39 
41  Bounds(const vtkm::Range& xRange, const vtkm::Range& yRange, const vtkm::Range& zRange)
42  : X(xRange)
43  , Y(yRange)
44  , Z(zRange)
45  {
46  }
47 
48  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
49  VTKM_EXEC_CONT Bounds(const T1& minX,
50  const T2& maxX,
51  const T3& minY,
52  const T4& maxY,
53  const T5& minZ,
54  const T6& maxZ)
55  : X(vtkm::Range(minX, maxX))
56  , Y(vtkm::Range(minY, maxY))
57  , Z(vtkm::Range(minZ, maxZ))
58  {
59  }
60 
64  template <typename T>
65  VTKM_EXEC_CONT explicit Bounds(const T bounds[6])
66  : X(vtkm::Range(bounds[0], bounds[1]))
67  , Y(vtkm::Range(bounds[2], bounds[3]))
68  , Z(vtkm::Range(bounds[4], bounds[5]))
69  {
70  }
71 
75  template <typename T>
76  VTKM_EXEC_CONT Bounds(const vtkm::Vec<T, 3>& minPoint, const vtkm::Vec<T, 3>& maxPoint)
77  : X(vtkm::Range(minPoint[0], maxPoint[0]))
78  , Y(vtkm::Range(minPoint[1], maxPoint[1]))
79  , Z(vtkm::Range(minPoint[2], maxPoint[2]))
80  {
81  }
82 
83  vtkm::Bounds& operator=(const vtkm::Bounds& src) = default;
84 
92  bool IsNonEmpty() const
93  {
94  return (this->X.IsNonEmpty() && this->Y.IsNonEmpty() && this->Z.IsNonEmpty());
95  }
96 
99  template <typename T>
100  VTKM_EXEC_CONT bool Contains(const vtkm::Vec<T, 3>& point) const
101  {
102  return (this->X.Contains(point[0]) && this->Y.Contains(point[1]) && this->Z.Contains(point[2]));
103  }
104 
112  {
113  if (this->IsNonEmpty())
114  {
115  return (this->X.Length() * this->Y.Length() * this->Z.Length());
116  }
117  else
118  {
119  return 0.0;
120  }
121  }
122 
130  {
131  if (this->IsNonEmpty())
132  {
133  return (this->X.Length() * this->Y.Length());
134  }
135  else
136  {
137  return 0.0;
138  }
139  }
140 
148  {
149  return vtkm::Vec3f_64(this->X.Center(), this->Y.Center(), this->Z.Center());
150  }
151 
158  vtkm::Vec3f_64 MinCorner() const { return vtkm::Vec3f_64(this->X.Min, this->Y.Min, this->Z.Min); }
159 
166  vtkm::Vec3f_64 MaxCorner() const { return vtkm::Vec3f_64(this->X.Max, this->Y.Max, this->Z.Max); }
167 
174  template <typename T>
176  {
177  this->X.Include(point[0]);
178  this->Y.Include(point[1]);
179  this->Z.Include(point[2]);
180  }
181 
188  void Include(const vtkm::Bounds& bounds)
189  {
190  this->X.Include(bounds.X);
191  this->Y.Include(bounds.Y);
192  this->Z.Include(bounds.Z);
193  }
194 
200  vtkm::Bounds Union(const vtkm::Bounds& otherBounds) const
201  {
202  vtkm::Bounds unionBounds(*this);
203  unionBounds.Include(otherBounds);
204  return unionBounds;
205  }
206 
210  vtkm::Bounds Intersection(const vtkm::Bounds& otherBounds) const
211  {
212  return vtkm::Bounds(this->X.Intersection(otherBounds.X),
213  this->Y.Intersection(otherBounds.Y),
214  this->Z.Intersection(otherBounds.Z));
215  }
216 
220  vtkm::Bounds operator+(const vtkm::Bounds& otherBounds) const { return this->Union(otherBounds); }
221 
223  bool operator==(const vtkm::Bounds& bounds) const
224  {
225  return ((this->X == bounds.X) && (this->Y == bounds.Y) && (this->Z == bounds.Z));
226  }
227 
229  bool operator!=(const vtkm::Bounds& bounds) const
230  {
231  return ((this->X != bounds.X) || (this->Y != bounds.Y) || (this->Z != bounds.Z));
232  }
233 };
234 
237 inline VTKM_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Bounds& bounds)
238 {
239  return stream << "{ X:" << bounds.X << ", Y:" << bounds.Y << ", Z:" << bounds.Z << " }";
240 }
241 
242 template <>
244 {
247 
248  static constexpr vtkm::IdComponent NUM_COMPONENTS = 3;
250  {
251  return NUM_COMPONENTS;
252  }
255 
257  static const ComponentType& GetComponent(const vtkm::Bounds& bounds, vtkm::IdComponent component)
258  {
259  VTKM_ASSERT((component >= 0) || (component < 3));
260  switch (component)
261  {
262  case 0:
263  return bounds.X;
264  case 1:
265  return bounds.Y;
266  case 2:
267  return bounds.Z;
268  default:
269  // Should never reach here
270  return bounds.X;
271  }
272  }
275  {
276  VTKM_ASSERT((component >= 0) || (component < 3));
277  switch (component)
278  {
279  case 0:
280  return bounds.X;
281  case 1:
282  return bounds.Y;
283  case 2:
284  return bounds.Z;
285  default:
286  // Should never reach here
287  return bounds.X;
288  }
289  }
290 
292  static void SetComponent(vtkm::Bounds& bounds,
293  vtkm::IdComponent component,
294  const ComponentType& value)
295  {
296  VTKM_ASSERT((component >= 0) || (component < 3));
297  switch (component)
298  {
299  case 0:
300  bounds.X = value;
301  break;
302  case 1:
303  bounds.Y = value;
304  break;
305  case 2:
306  bounds.Z = value;
307  break;
308  }
309  }
310 
311  template <typename NewComponentType>
313  template <typename NewComponentType>
316 
317  template <vtkm::IdComponent destSize>
318  VTKM_EXEC_CONT static void CopyInto(const vtkm::Bounds& src,
320  {
321  const vtkm::IdComponent maxComponent = (destSize < NUM_COMPONENTS) ? destSize : NUM_COMPONENTS;
322  for (vtkm::IdComponent component = 0; component < maxComponent; ++component)
323  {
324  dest[component] = GetComponent(src, component);
325  }
326  }
327 };
328 
329 } // namespace vtkm
330 
331 #endif //vtk_m_Bounds_h
vtkm::VecTraits< vtkm::Bounds >::CopyInto
static VTKM_EXEC_CONT void CopyInto(const vtkm::Bounds &src, vtkm::Vec< ComponentType, destSize > &dest)
Definition: Bounds.h:318
vtkm::Bounds::Center
VTKM_EXEC_CONT vtkm::Vec3f_64 Center() const
Returns the center of the range.
Definition: Bounds.h:147
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:21
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
vtkm::Bounds::Include
VTKM_EXEC_CONT void Include(const vtkm::Bounds &bounds)
Expand bounds to include other bounds.
Definition: Bounds.h:188
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Bounds::Area
VTKM_EXEC_CONT vtkm::Float64 Area() const
Returns the area of the bounds in the X-Y-plane.
Definition: Bounds.h:129
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::Bounds::Union
VTKM_EXEC_CONT vtkm::Bounds Union(const vtkm::Bounds &otherBounds) const
Return the union of this and another bounds.
Definition: Bounds.h:200
vtkm::VecTraits< vtkm::Bounds >::SetComponent
static VTKM_EXEC_CONT void SetComponent(vtkm::Bounds &bounds, vtkm::IdComponent component, const ComponentType &value)
Definition: Bounds.h:292
vtkm::Range::Include
VTKM_EXEC_CONT void Include(const T &value)
Expand range to include a value.
Definition: Range.h:124
vtkm::Bounds::Bounds
VTKM_EXEC_CONT Bounds(const vtkm::Range &xRange, const vtkm::Range &yRange, const vtkm::Range &zRange)
Definition: Bounds.h:41
vtkm::VecTraits< vtkm::Bounds >::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const vtkm::Bounds &bounds, vtkm::IdComponent component)
Definition: Bounds.h:257
vtkm::Bounds::MaxCorner
VTKM_EXEC_CONT vtkm::Vec3f_64 MaxCorner() const
Returns the max point of the bounds
Definition: Bounds.h:166
vtkm::VecTraits< vtkm::Bounds >::GetNumberOfComponents
static constexpr vtkm::IdComponent GetNumberOfComponents(const vtkm::Bounds &)
Definition: Bounds.h:249
vtkm::Bounds::Contains
VTKM_EXEC_CONT bool Contains(const vtkm::Vec< T, 3 > &point) const
Determines if a point coordinate is within the bounds.
Definition: Bounds.h:100
vtkm::Range::Center
VTKM_EXEC_CONT vtkm::Float64 Center() const
Returns the center of the range.
Definition: Range.h:105
vtkm::Range::Length
VTKM_EXEC_CONT vtkm::Float64 Length() const
Returns the length of the range.
Definition: Range.h:87
vtkm::Bounds::operator==
VTKM_EXEC_CONT bool operator==(const vtkm::Bounds &bounds) const
Definition: Bounds.h:223
vtkm::VecTraits< vtkm::Bounds >::GetComponent
static VTKM_EXEC_CONT ComponentType & GetComponent(vtkm::Bounds &bounds, vtkm::IdComponent component)
Definition: Bounds.h:274
vtkm::VecTraits< vtkm::Bounds >::BaseComponentType
vtkm::VecTraits< vtkm::Range >::BaseComponentType BaseComponentType
Definition: Bounds.h:246
vtkm::Bounds::Intersection
VTKM_EXEC_CONT vtkm::Bounds Intersection(const vtkm::Bounds &otherBounds) const
Return the intersection of this and another range.
Definition: Bounds.h:210
vtkm::Vec3f_64
vtkm::Vec< vtkm::Float64, 3 > Vec3f_64
Vec3f_64 corresponds to a 3-dimensional vector of 64-bit floating point values.
Definition: Types.h:1026
vtkm::Bounds::Include
VTKM_EXEC_CONT void Include(const vtkm::Vec< T, 3 > &point)
Expand bounds to include a point.
Definition: Bounds.h:175
vtkm::Range::Intersection
VTKM_EXEC_CONT vtkm::Range Intersection(const vtkm::Range &otherRange) const
Return the intersection of this and another range.
Definition: Range.h:160
vtkm::Bounds::Bounds
VTKM_EXEC_CONT Bounds(const T1 &minX, const T2 &maxX, const T3 &minY, const T4 &maxY, const T5 &minZ, const T6 &maxZ)
Definition: Bounds.h:49
Range.h
vtkm::operator<<
VTKM_CONT std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:237
vtkm::Bounds::Z
vtkm::Range Z
Definition: Bounds.h:33
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Range::IsNonEmpty
VTKM_EXEC_CONT bool IsNonEmpty() const
Determine if the range is valid (i.e.
Definition: Range.h:66
vtkm::Bounds::operator!=
VTKM_EXEC_CONT bool operator!=(const vtkm::Bounds &bounds) const
Definition: Bounds.h:229
vtkm::Bounds
Represent an axis-aligned 3D bounds in space.
Definition: Bounds.h:29
vtkm::Bounds::operator=
vtkm::Bounds & operator=(const vtkm::Bounds &src)=default
vtkm::Bounds::operator+
VTKM_EXEC_CONT vtkm::Bounds operator+(const vtkm::Bounds &otherBounds) const
Operator for union
Definition: Bounds.h:220
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Vec< vtkm::Float64, 3 >
vtkm::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:34
vtkm::Range::Contains
VTKM_EXEC_CONT bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:75
vtkm::Range::Min
vtkm::Float64 Min
Definition: Range.h:33
vtkm::Bounds::Bounds
VTKM_EXEC_CONT Bounds()
Definition: Bounds.h:36
vtkm::Bounds::IsNonEmpty
VTKM_EXEC_CONT bool IsNonEmpty() const
Determine if the bounds are valid (i.e.
Definition: Bounds.h:92
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::Bounds::X
vtkm::Range X
Definition: Bounds.h:31
vtkm::Bounds::Y
vtkm::Range Y
Definition: Bounds.h:32
VTKM_NEVER_EXPORT
#define VTKM_NEVER_EXPORT
Definition: ExportMacros.h:93
vtkm::Range::Max
vtkm::Float64 Max
Definition: Range.h:34
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
vtkm::Bounds::MinCorner
VTKM_EXEC_CONT vtkm::Vec3f_64 MinCorner() const
Returns the min point of the bounds
Definition: Bounds.h:158
vtkm::Bounds::Volume
VTKM_EXEC_CONT vtkm::Float64 Volume() const
Returns the volume of the bounds.
Definition: Bounds.h:111
vtkm::Bounds::Bounds
VTKM_EXEC_CONT Bounds(const vtkm::Vec< T, 3 > &minPoint, const vtkm::Vec< T, 3 > &maxPoint)
Initialize bounds with the minimum corner point and the maximum corner point.
Definition: Bounds.h:76
vtkm::Bounds::Bounds
VTKM_EXEC_CONT Bounds(const T bounds[6])
Initialize bounds with an array of 6 values in the order xmin, xmax, ymin, ymax, zmin,...
Definition: Bounds.h:65
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31