Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rangeutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_RANGEUTILS_H_
19 #define ION_MATH_RANGEUTILS_H_
20 
25 
26 #include <algorithm>
27 
28 #include "ion/math/range.h"
29 
30 namespace ion {
31 namespace math {
32 
36 template <int Dimension, typename T>
38  const Range<Dimension, T> &r1) {
39  Range<Dimension, T> result(r0);
40  result.ExtendByRange(r1);
41  return result;
42 }
43 
47 template <int Dimension, typename T>
49  const Range<Dimension, T> &r1) {
50  if (r0.IsEmpty() || r1.IsEmpty()) {
51  return Range<Dimension, T>();
52  } else {
53  typename Range<Dimension, T>::Endpoint result_min;
54  typename Range<Dimension, T>::Endpoint result_max;
55  for (int i = 0; i < Dimension; ++i) {
56  result_min[i] = std::max(r0.GetMinPoint()[i], r1.GetMinPoint()[i]);
57  result_max[i] = std::min(r0.GetMaxPoint()[i], r1.GetMaxPoint()[i]);
59  if (result_min[i] > result_max[i])
60  return Range<Dimension, T>();
61  }
62  return Range<Dimension, T>(result_min, result_max);
63  }
64 }
65 
68 template <int Dimension, typename T>
70  if (r.IsEmpty()) {
71  return T(0);
72  } else {
73  T result(1);
74  typename Range<Dimension, T>::Size edges = r.GetSize();
75  for (int i = 0; i < Dimension; ++i) {
76  result *= edges[i];
77  }
78  return result;
79  }
80 }
81 
84 template <int Dimension, typename T>
86  const Range<Dimension, T>& r1,
87  const T threshold) {
88  if (r0.IsEmpty() || r1.IsEmpty())
89  return false;
90  const typename Range<Dimension, T>::Endpoint& r0_min = r0.GetMinPoint();
91  const typename Range<Dimension, T>::Endpoint& r0_max = r0.GetMaxPoint();
92  const typename Range<Dimension, T>::Endpoint& r1_min = r1.GetMinPoint();
93  const typename Range<Dimension, T>::Endpoint& r1_max = r1.GetMaxPoint();
94 
95  for (int i = 0; i < Dimension; ++i) {
96  if (Abs(r0_min[i] - r1_min[i]) > Abs(threshold) ||
97  Abs(r0_max[i] - r1_max[i]) > Abs(threshold))
98  return false;
99  }
100  return true;
101 }
102 
106 template <int Dimension, typename T>
108  T scale_factor) {
109  typedef Range<Dimension, T> RangeType;
110  RangeType scaled;
111  if (!r.IsEmpty() && scale_factor > static_cast<T>(0)) {
112  const typename RangeType::Endpoint center = r.GetCenter();
113  scaled.Set(center + scale_factor * (r.GetMinPoint() - center),
114  center + scale_factor * (r.GetMaxPoint() - center));
115  }
116  return scaled;
117 }
118 
122 template <int Dimension, typename T>
124  const Range<Dimension, T>& r, const Vector<Dimension, T> scale_factors) {
125  typedef Range<Dimension, T> RangeType;
126  RangeType scaled;
127  if (!r.IsEmpty()) {
128  const typename RangeType::Endpoint center = r.GetCenter();
129  typename RangeType::Endpoint min_pt = r.GetMinPoint();
130  typename RangeType::Endpoint max_pt = r.GetMaxPoint();
131  for (int i = 0; i < Dimension; ++i) {
132  if (scale_factors[i] <= static_cast<T>(0))
133  return scaled;
134  min_pt[i] = center[i] + scale_factors[i] * (min_pt[i] - center[i]);
135  max_pt[i] = center[i] + scale_factors[i] * (max_pt[i] - center[i]);
136  }
137  scaled.Set(min_pt, max_pt);
138  }
139  return scaled;
140 }
141 
148 template <int Dimension, typename T1, typename T2>
150  const Range<Dimension, T1>& r, const Vector<Dimension, T2> modulation) {
151  typedef Range<Dimension, T1> RangeType;
152  RangeType modulated;
153  if (!r.IsEmpty()) {
154  typename RangeType::Endpoint min_pt = r.GetMinPoint();
155  typename RangeType::Endpoint max_pt = r.GetMaxPoint();
156  for (int i = 0; i < Dimension; ++i) {
157  if (modulation[i] <= static_cast<T2>(0))
158  return modulated;
159  min_pt[i] = static_cast<T1>(static_cast<T2>(min_pt[i]) * modulation[i]);
160  max_pt[i] = static_cast<T1>(static_cast<T2>(max_pt[i]) * modulation[i]);
161  }
162  modulated.Set(min_pt, max_pt);
163  }
164  return modulated;
165 }
166 
167 } // namespace math
168 } // namespace ion
169 
170 #endif // ION_MATH_RANGEUTILS_H_
const Endpoint & GetMinPoint() const
Returns the minimum or maximum endpoint.
Definition: range.h:135
const Size GetSize() const
Returns the size of the range, or the zero vector if the Range is empty.
Definition: range.h:161
The Range class defines an N-dimensional interval defined by minimum and maximum N-dimensional endpoi...
Definition: range.h:100
T NVolume(const Range< Dimension, T > &r)
Returns the NVolume of a Range, which is the product of its sizes in all dimensions.
Definition: rangeutils.h:69
const Endpoint GetCenter() const
Returns the point at the center of the range, or the origin if the range is empty.
Definition: range.h:167
const Range< Dimension, T > ScaleRange(const Range< Dimension, T > &r, T scale_factor)
Returns a Range that is the input Range scaled uniformly about its center by the given factor...
Definition: rangeutils.h:107
void Set(T e0)
Sets the vector values.
Definition: vector.h:464
const Range< Dimension, T1 > ModulateRange(const Range< Dimension, T1 > &r, const Vector< Dimension, T2 > modulation)
Modulate a Range by a Vector.
Definition: rangeutils.h:149
const Range< Dimension, T > RangeUnion(const Range< Dimension, T > &r0, const Range< Dimension, T > &r1)
Returns the union of two Range instances.
Definition: rangeutils.h:37
bool IsEmpty() const
Returns true if the Range is empty, meaning that the minimum value is strictly greater than the maxim...
Definition: range.h:264
Vector.
Definition: vector.h:180
bool RangesAlmostEqual(const Range< Dimension, T > &r0, const Range< Dimension, T > &r1, const T threshold)
Returns true if all dimensions of the two ranges are equal within the threshold.
Definition: rangeutils.h:85
Point.
Definition: vector.h:296
const Range< Dimension, T > RangeIntersection(const Range< Dimension, T > &r0, const Range< Dimension, T > &r1)
Returns the intersection of two Range instances.
Definition: rangeutils.h:48
const Endpoint & GetMaxPoint() const
Definition: range.h:136
void ExtendByRange(const Range &r)
Extends the Range if necessary to contain the given Range.
Definition: range.h:299
const T Abs(const T &val)
Returns the absolute value of a number in a type-safe way.
Definition: utils.h:42
const Range< Dimension, T > ScaleRangeNonUniformly(const Range< Dimension, T > &r, const Vector< Dimension, T > scale_factors)
Returns a Range that is the input Range scaled nonuniformly about its center by the given per-dimensi...
Definition: rangeutils.h:123