ArraySamplers1-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ArraySamplers1-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 1-D nearest array sampler class.
6 > Created Time: 2017/04/29
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_ARRAY_SAMPLERS1_IMPL_H
10 #define CUBBYFLOW_ARRAY_SAMPLERS1_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 namespace CubbyFlow
15 {
16  template <typename T, typename R>
18  const ConstArrayAccessor1<T>& accessor,
19  R gridSpacing,
20  R gridOrigin)
21  {
22  m_gridSpacing = gridSpacing;
23  m_origin = gridOrigin;
24  m_accessor = accessor;
25  }
26 
27  template <typename T, typename R>
29  {
30  m_gridSpacing = other.m_gridSpacing;
31  m_origin = other.m_origin;
32  m_accessor = other.m_accessor;
33  }
34 
35  template <typename T, typename R>
37  {
38  ssize_t i;
39  R fx;
40 
41  assert(m_gridSpacing > std::numeric_limits<R>::epsilon());
42 
43  const R normalizedX = (pt - m_origin) / m_gridSpacing;
44 
45  const ssize_t iSize = static_cast<ssize_t>(m_accessor.size());
46 
47  GetBarycentric(normalizedX, 0, iSize - 1, &i, &fx);
48 
49  i = std::min(static_cast<ssize_t>(i + fx + 0.5), iSize - 1);
50 
51  return m_accessor[i];
52  }
53 
54  template <typename T, typename R>
55  void NearestArraySampler<T, R, 1>::GetCoordinate(R pt, size_t* index) const
56  {
57  ssize_t i;
58  R fx;
59 
60  assert(m_gridSpacing > std::numeric_limits<R>::epsilon());
61 
62  const R normalizedX = (pt - m_origin) / m_gridSpacing;
63 
64  const ssize_t iSize = static_cast<ssize_t>(m_accessor.size());
65 
66  GetBarycentric(normalizedX, 0, iSize - 1, &i, &fx);
67 
68  *index = std::min(static_cast<ssize_t>(i + fx + 0.5), iSize - 1);
69  }
70 
71  template <typename T, typename R>
72  std::function<T(R)> NearestArraySampler<T, R, 1>::Functor() const
73  {
74  NearestArraySampler sampler(*this);
75  return std::bind(&NearestArraySampler::operator(), sampler, std::placeholders::_1);
76  }
77 
78  template <typename T, typename R>
80  const ConstArrayAccessor1<T>& accessor,
81  R gridSpacing,
82  R gridOrigin)
83  {
84  m_gridSpacing = gridSpacing;
85  m_origin = gridOrigin;
86  m_accessor = accessor;
87  }
88 
89  template <typename T, typename R>
91  {
92  m_gridSpacing = other.m_gridSpacing;
93  m_origin = other.m_origin;
94  m_accessor = other.m_accessor;
95  }
96 
97  template <typename T, typename R>
99  {
100  ssize_t i;
101  R fx;
102 
103  assert(m_gridSpacing > std::numeric_limits<R>::epsilon());
104 
105  const R normalizedX = (pt - m_origin) / m_gridSpacing;
106 
107  const ssize_t iSize = static_cast<ssize_t>(m_accessor.size());
108 
109  GetBarycentric(normalizedX, 0, iSize - 1, &i, &fx);
110 
111  const ssize_t ip1 = std::min(i + 1, iSize - 1);
112 
113  return Lerp(m_accessor[i], m_accessor[ip1], fx);
114  }
115 
116  template <typename T, typename R>
117  void LinearArraySampler<T, R, 1>::GetCoordinatesAndWeights(R pt, size_t* i0, size_t* i1, T* weight0, T* weight1) const
118  {
119  ssize_t i;
120  R fx;
121 
122  assert(m_gridSpacing > std::numeric_limits<R>::epsilon());
123 
124  const R normalizedX = (pt - m_origin) / m_gridSpacing;
125 
126  const ssize_t iSize = static_cast<ssize_t>(m_accessor.size());
127 
128  GetBarycentric(normalizedX, 0, iSize - 1, &i, &fx);
129 
130  const ssize_t ip1 = std::min(i + 1, iSize - 1);
131 
132  *i0 = i;
133  *i1 = ip1;
134  *weight0 = 1 - fx;
135  *weight1 = fx;
136  }
137 
138  template <typename T, typename R>
139  std::function<T(R)> LinearArraySampler<T, R, 1>::Functor() const
140  {
141  LinearArraySampler sampler(*this);
142  return std::bind(&LinearArraySampler::operator(), sampler, std::placeholders::_1);
143  }
144 
145  template <typename T, typename R>
147  const ConstArrayAccessor1<T>& accessor,
148  R gridSpacing,
149  R gridOrigin)
150  {
151  m_gridSpacing = gridSpacing;
152  m_origin = gridOrigin;
153  m_accessor = accessor;
154  }
155 
156  template <typename T, typename R>
158  {
159  m_gridSpacing = other.m_gridSpacing;
160  m_origin = other.m_origin;
161  m_accessor = other.m_accessor;
162  }
163 
164  template <typename T, typename R>
166  {
167  ssize_t i;
168  R fx;
169 
170  assert(m_gridSpacing > std::numeric_limits<R>::epsilon());
171 
172  const R normalizedX = (x - m_origin) / m_gridSpacing;
173 
174  const ssize_t iSize = static_cast<ssize_t>(m_accessor.size());
175 
176  GetBarycentric(normalizedX, 0, iSize - 1, &i, &fx);
177 
178  const ssize_t im1 = std::max(i - 1, ZERO_SSIZE);
179  const ssize_t ip1 = std::min(i + 1, iSize - 1);
180  const ssize_t ip2 = std::min(i + 2, iSize - 1);
181 
182  return MonotonicCatmullRom(
183  m_accessor[im1],
184  m_accessor[i],
185  m_accessor[ip1],
186  m_accessor[ip2],
187  fx);
188  }
189 
190  template <typename T, typename R>
191  std::function<T(R)> CubicArraySampler<T, R, 1>::Functor() const
192  {
193  CubicArraySampler sampler(*this);
194  return std::bind(&CubicArraySampler::operator(), sampler, std::placeholders::_1);
195  }
196 }
197 
198 #endif
S Lerp(const S &value0, const S &value1, T f)
Computes linear interpolation.
Definition: MathUtils-Impl.h:184
Generic N-D nearest array sampler class.
Definition: ArraySamplers.h:22
1-D read-only array accessor class.
Definition: ArrayAccessor1.h:185
Definition: pybind11Utils.h:24
Generic N-D cubic array sampler class.
Definition: ArraySamplers.h:50
constexpr ssize_t ZERO_SSIZE
Zero ssize_t.
Definition: Constants.h:20
T MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T f)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.h:228
Generic N-D linear array sampler class.
Definition: ArraySamplers.h:36
void GetBarycentric(T x, ssize_t iLow, ssize_t iHigh, ssize_t *i, T *f)
Gets the barycentric coordinate.
Definition: MathUtils-Impl.h:151