Viskores  1.0
Bounds.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 
19 #ifndef viskores_Bounds_h
20 #define viskores_Bounds_h
21 
22 #include <viskores/Range.h>
23 
24 namespace viskores
25 {
26 
37 struct Bounds
38 {
48 
52  Bounds() {}
53 
54  Bounds(const Bounds&) = default;
55 
58  Bounds(const viskores::Range& xRange,
59  const viskores::Range& yRange,
60  const viskores::Range& zRange)
61  : X(xRange)
62  , Y(yRange)
63  , Z(zRange)
64  {
65  }
66 
69  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
70  VISKORES_EXEC_CONT Bounds(const T1& minX,
71  const T2& maxX,
72  const T3& minY,
73  const T4& maxY,
74  const T5& minZ,
75  const T6& maxZ)
76  : X(viskores::Range(minX, maxX))
77  , Y(viskores::Range(minY, maxY))
78  , Z(viskores::Range(minZ, maxZ))
79  {
80  }
81 
85  template <typename T>
86  VISKORES_EXEC_CONT explicit Bounds(const T bounds[6])
87  : X(viskores::Range(bounds[0], bounds[1]))
88  , Y(viskores::Range(bounds[2], bounds[3]))
89  , Z(viskores::Range(bounds[4], bounds[5]))
90  {
91  }
92 
96  template <typename T>
98  const viskores::Vec<T, 3>& maxPoint)
99  : X(viskores::Range(minPoint[0], maxPoint[0]))
100  , Y(viskores::Range(minPoint[1], maxPoint[1]))
101  , Z(viskores::Range(minPoint[2], maxPoint[2]))
102  {
103  }
104 
105  viskores::Bounds& operator=(const viskores::Bounds& src) = default;
106 
114  bool IsNonEmpty() const
115  {
116  return (this->X.IsNonEmpty() && this->Y.IsNonEmpty() && this->Z.IsNonEmpty());
117  }
118 
121  template <typename T>
123  {
124  return (this->X.Contains(point[0]) && this->Y.Contains(point[1]) && this->Z.Contains(point[2]));
125  }
126 
134  {
135  if (this->IsNonEmpty())
136  {
137  return (this->X.Length() * this->Y.Length() * this->Z.Length());
138  }
139  else
140  {
141  return 0.0;
142  }
143  }
144 
152  {
153  if (this->IsNonEmpty())
154  {
155  return (this->X.Length() * this->Y.Length());
156  }
157  else
158  {
159  return 0.0;
160  }
161  }
162 
170  {
171  return viskores::Vec3f_64(this->X.Center(), this->Y.Center(), this->Z.Center());
172  }
173 
181  {
182  return viskores::Vec3f_64(this->X.Min, this->Y.Min, this->Z.Min);
183  }
184 
192  {
193  return viskores::Vec3f_64(this->X.Max, this->Y.Max, this->Z.Max);
194  }
195 
202  template <typename T>
204  {
205  this->X.Include(point[0]);
206  this->Y.Include(point[1]);
207  this->Z.Include(point[2]);
208  }
209 
216  void Include(const viskores::Bounds& bounds)
217  {
218  this->X.Include(bounds.X);
219  this->Y.Include(bounds.Y);
220  this->Z.Include(bounds.Z);
221  }
222 
228  viskores::Bounds Union(const viskores::Bounds& otherBounds) const
229  {
230  viskores::Bounds unionBounds(*this);
231  unionBounds.Include(otherBounds);
232  return unionBounds;
233  }
234 
239  {
240  return viskores::Bounds(this->X.Intersection(otherBounds.X),
241  this->Y.Intersection(otherBounds.Y),
242  this->Z.Intersection(otherBounds.Z));
243  }
244 
248  viskores::Bounds operator+(const viskores::Bounds& otherBounds) const
249  {
250  return this->Union(otherBounds);
251  }
252 
254  bool operator==(const viskores::Bounds& bounds) const
255  {
256  return ((this->X == bounds.X) && (this->Y == bounds.Y) && (this->Z == bounds.Z));
257  }
258 
260  bool operator!=(const viskores::Bounds& bounds) const
261  {
262  return ((this->X != bounds.X) || (this->Y != bounds.Y) || (this->Z != bounds.Z));
263  }
264 };
265 
268 inline VISKORES_CONT std::ostream& operator<<(std::ostream& stream, const viskores::Bounds& bounds)
269 {
270  return stream << "{ X:" << bounds.X << ", Y:" << bounds.Y << ", Z:" << bounds.Z << " }";
271 }
272 
273 template <>
274 struct VISKORES_NEVER_EXPORT VecTraits<viskores::Bounds>
275 {
278 
279  static constexpr viskores::IdComponent NUM_COMPONENTS = 3;
281  {
282  return NUM_COMPONENTS;
283  }
286 
288  static const ComponentType& GetComponent(const viskores::Bounds& bounds,
289  viskores::IdComponent component)
290  {
291  VISKORES_ASSERT((component >= 0) || (component < 3));
292  switch (component)
293  {
294  case 0:
295  return bounds.X;
296  case 1:
297  return bounds.Y;
298  case 2:
299  return bounds.Z;
300  default:
301  // Should never reach here
302  return bounds.X;
303  }
304  }
307  {
308  VISKORES_ASSERT((component >= 0) || (component < 3));
309  switch (component)
310  {
311  case 0:
312  return bounds.X;
313  case 1:
314  return bounds.Y;
315  case 2:
316  return bounds.Z;
317  default:
318  // Should never reach here
319  return bounds.X;
320  }
321  }
322 
324  static void SetComponent(viskores::Bounds& bounds,
325  viskores::IdComponent component,
326  const ComponentType& value)
327  {
328  VISKORES_ASSERT((component >= 0) || (component < 3));
329  switch (component)
330  {
331  case 0:
332  bounds.X = value;
333  break;
334  case 1:
335  bounds.Y = value;
336  break;
337  case 2:
338  bounds.Z = value;
339  break;
340  }
341  }
342 
343  template <typename NewComponentType>
345  template <typename NewComponentType>
347  viskores::Vec<NewComponentType,
349 
350  template <viskores::IdComponent destSize>
353  {
354  const viskores::IdComponent maxComponent =
355  (destSize < NUM_COMPONENTS) ? destSize : NUM_COMPONENTS;
356  for (viskores::IdComponent component = 0; component < maxComponent; ++component)
357  {
358  dest[component] = GetComponent(src, component);
359  }
360  }
361 };
362 
363 } // namespace viskores
364 
365 #endif //viskores_Bounds_h
viskores::VecTraits< viskores::Bounds >::SetComponent
static void SetComponent(viskores::Bounds &bounds, viskores::IdComponent component, const ComponentType &value)
Definition: Bounds.h:324
viskores::VecTraits< viskores::Bounds >::BaseComponentType
viskores::VecTraits< viskores::Range >::BaseComponentType BaseComponentType
Definition: Bounds.h:277
viskores::Range::Intersection
viskores::Range Intersection(const viskores::Range &otherRange) const
Return the intersection of this and another range.
Definition: Range.h:172
viskores::Range::Length
viskores::Float64 Length() const
Returns the length of the range.
Definition: Range.h:99
viskores::Range::Center
viskores::Float64 Center() const
Returns the center of the range.
Definition: Range.h:117
viskores::Bounds::Bounds
Bounds(const T1 &minX, const T2 &maxX, const T3 &minY, const T4 &maxY, const T5 &minZ, const T6 &maxZ)
Construct a bounds with the minimum and maximum coordinates in the x, y, and z directions.
Definition: Bounds.h:70
viskores::Vec3f_64
viskores::Vec< viskores::Float64, 3 > Vec3f_64
Vec3f_64 corresponds to a 3-dimensional vector of 64-bit floating point values.
Definition: Types.h:1076
viskores::Bounds::IsNonEmpty
bool IsNonEmpty() const
Determine if the bounds are valid (i.e.
Definition: Bounds.h:114
viskores::Bounds
Represent an axis-aligned 3D bounds in space.
Definition: Bounds.h:37
viskores::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:44
viskores::Bounds::MinCorner
viskores::Vec3f_64 MinCorner() const
Returns the min point of the bounds
Definition: Bounds.h:180
viskores::Range::Min
viskores::Float64 Min
The minumum value of the range (inclusive).
Definition: Range.h:42
viskores::Bounds::Union
viskores::Bounds Union(const viskores::Bounds &otherBounds) const
Return the union of this and another bounds.
Definition: Bounds.h:228
viskores::Bounds::operator!=
bool operator!=(const viskores::Bounds &bounds) const
Definition: Bounds.h:260
viskores::Bounds::MaxCorner
viskores::Vec3f_64 MaxCorner() const
Returns the max point of the bounds
Definition: Bounds.h:191
viskores::Bounds::Area
viskores::Float64 Area() const
Returns the area of the bounds in the X-Y-plane.
Definition: Bounds.h:151
viskores::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:31
viskores::Range::IsNonEmpty
bool IsNonEmpty() const
Determine if the range is valid (i.e.
Definition: Range.h:78
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::Bounds::operator+
viskores::Bounds operator+(const viskores::Bounds &otherBounds) const
Operator for union
Definition: Bounds.h:248
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::VecTraits< viskores::Bounds >::GetNumberOfComponents
static constexpr viskores::IdComponent GetNumberOfComponents(const viskores::Bounds &)
Definition: Bounds.h:280
viskores::Vec< T, 3 >
Definition: Types.h:1025
viskores::Range::Max
viskores::Float64 Max
Tha maximum value of the range (inclusive).
Definition: Range.h:44
viskores::Bounds::Intersection
viskores::Bounds Intersection(const viskores::Bounds &otherBounds) const
Return the intersection of this and another range.
Definition: Bounds.h:238
viskores::VecTraits< viskores::Bounds >::GetComponent
static ComponentType & GetComponent(viskores::Bounds &bounds, viskores::IdComponent component)
Definition: Bounds.h:306
viskores::Bounds::Contains
bool Contains(const viskores::Vec< T, 3 > &point) const
Determines if a point coordinate is within the bounds.
Definition: Bounds.h:122
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores::operator<<
std::ostream & operator<<(std::ostream &stream, const viskores::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:268
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::VecTraits< viskores::Bounds >::CopyInto
static void CopyInto(const viskores::Bounds &src, viskores::Vec< ComponentType, destSize > &dest)
Definition: Bounds.h:351
Range.h
viskores::Bounds::operator==
bool operator==(const viskores::Bounds &bounds) const
Definition: Bounds.h:254
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::Bounds::Center
viskores::Vec3f_64 Center() const
Returns the center of the range.
Definition: Bounds.h:169
viskores::Bounds::Z
viskores::Range Z
The range of values in the Z direction.
Definition: Bounds.h:47
viskores::Range
Represent a continuous scalar range of values.
Definition: Range.h:39
viskores::Bounds::Bounds
Bounds()
Construct an empty bounds.
Definition: Bounds.h:52
viskores::Bounds::Bounds
Bounds(const viskores::Range &xRange, const viskores::Range &yRange, const viskores::Range &zRange)
Construct a bounds with a given range in the x, y, and z dimensions.
Definition: Bounds.h:58
viskores::Bounds::Include
void Include(const viskores::Bounds &bounds)
Expand bounds to include other bounds.
Definition: Bounds.h:216
viskores::Bounds::Volume
viskores::Float64 Volume() const
Returns the volume of the bounds.
Definition: Bounds.h:133
viskores::Bounds::Y
viskores::Range Y
The range of values in the Y direction.
Definition: Bounds.h:44
viskores::Bounds::Include
void Include(const viskores::Vec< T, 3 > &point)
Expand bounds to include a point.
Definition: Bounds.h:203
viskores::Bounds::Bounds
Bounds(const viskores::Vec< T, 3 > &minPoint, const viskores::Vec< T, 3 > &maxPoint)
Initialize bounds with the minimum corner point and the maximum corner point.
Definition: Bounds.h:97
viskores::VecTraits< viskores::Bounds >::GetComponent
static const ComponentType & GetComponent(const viskores::Bounds &bounds, viskores::IdComponent component)
Definition: Bounds.h:288
viskores::Bounds::Bounds
Bounds(const T bounds[6])
Initialize bounds with an array of 6 values in the order xmin, xmax, ymin, ymax, zmin,...
Definition: Bounds.h:86
viskores::Bounds::operator=
viskores::Bounds & operator=(const viskores::Bounds &src)=default
viskores::Range::Include
void Include(const T &value)
Expand range to include a value.
Definition: Range.h:136
viskores::Range::Contains
bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:87
viskores::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:169
viskores::Vec< viskores::Float64, 3 >
viskores::Bounds::X
viskores::Range X
The range of values in the X direction.
Definition: Bounds.h:41