25 template<
class InputIt,
typename T>
34 if (cdf_begin[i] > u) {
break; }
43 template<
class InputIt,
typename T>
49 InputIt u_i = std::upper_bound(cdf_begin, cdf_begin+res, u);
51 return SYSmax(
int(0),
int(u_i-cdf_begin));
57 template<
class InputIt,
typename T>
65 return SYSmax(
int(0),
int(u_i-cdf_begin));
74 static inline int eval(
const int *res,
84 static inline int eval(
const int *res,
94 static inline int eval(
const int *res,
97 return i[1] * res[0] + i[0];
104 static inline int eval(
const int *res,
107 return (i[0] * res[1] + i[1]) * res[2] + i[2];
114 template<
class F,
typename T>
128 template<
class F,
typename T>
136 for ( ; i[0] < res[0]; ++i[0])
148 template<
class F,
typename T>
156 for (i[0] = 0; i[0] < res[0]; ++i[0])
158 for (i[1] = 0; i[1] < res[1]; ++i[1])
171 template<
class F,
typename T>
177 int i[
N] = {0, 0, 0};
179 for (i[0] = 0; i[0] < res[0]; ++i[0])
181 for (i[1] = 0; i[1] < res[1]; ++i[1])
183 for (i[2] = 0; i[2] < res[2]; ++i[2])
201 return myVoxels->operator()(i[0], i[1], i[2]);
208 template<
class OutputIt>
212 if (res == 0) {
return; }
214 output_t
s = 1.0F / res;
215 for (
int i = 0; i < res; ++i)
223 template<
class S,
class InputIt,
typename T>
232 template<
class S,
class InputIt,
typename T>
241 dval *= cdf_begin[res-1];
242 idx = UTsampleCdf<S>(cdf_begin, res, dval);
244 range = cdf_begin[idx];
248 dval -= cdf_begin[idx-1];
249 range -= cdf_begin[idx-1];
254 pdf = res*range / cdf_begin[res-1];
261 template<
class InputIt,
typename T>
263 UTsampleCdf(InputIt cdf_begin,
const int &res,
const int &i, T &pdf)
269 range -= cdf_begin[i-1];
273 pdf = res*range / cdf_begin[res-1];
294 template<
class InputIt,
class OutputIt>
305 std::for_each(pdf_begin, pdf_begin + (values_end-values_begin),
306 [&sum](
const output_t &
v)
311 output_t
area = output_t(1.0) / sum;
312 std::transform(pdf_begin, pdf_begin + (values_end-values_begin), pdf_begin,
313 [&area](
const output_t &v) {
return v *
area; });
317 template<
class InputIt,
class OutputIt>
324 while (values_begin != values_end)
326 *cdf_begin = *values_begin;
327 if (i > 0) { *cdf_begin += *(cdf_begin-1); }
340 static_assert((
N > 0 &&
N < 4),
"unsupported number of dimensions");
354 const bool ©data);
361 inline UT_Cdf(
const F &ftor,
369 inline UT_Cdf(
const int *res);
378 inline void resize(
const int *res);
383 inline void setRawArray(
T *
data,
384 const bool ©data);
389 inline void setRawArray(
const F &ftor);
403 inline void sample(
const T *u,
T *dval)
const;
404 inline void sample(
const T *u,
T *dval,
T &pdf)
const;
407 inline void sample(
const T *u,
int *didx)
const;
412 inline void sample(
const T *u,
int *didx,
T *doff,
T &pdf)
const;
415 inline void evaluatePdf(
const T *u,
T &pdf)
const;
418 inline const T &
getSum()
const {
return myCdf[0][myRes[0]-1]; }
426 inline const int *
getRes()
const {
return myRes; }
427 inline const int &
getRes(
int dim)
const {
return myRes[dim]; }
429 inline void dump()
const;
433 size_t mem =
sizeof(*this);
437 for (
int i = 0; i <
N; ++i)
440 mem +=
sizeof(
T)*cap;
448 inline void realloc(
const int *res);
461 template<
int N,
typename T,
class S>
468 template<
int N,
typename T,
class S>
473 const bool ©data)
476 myOwnData = copydata;
478 setRawArray(data, copydata);
482 template<
int N,
typename T,
class S>
494 template<
int N,
typename T,
class S>
502 template<
int N,
typename T,
class S>
509 template<
int N,
typename T,
class S>
513 for (
int i = 0; i <
N; ++i)
527 template<
int N,
typename T,
class S>
531 if (!myOwnData) {
return; }
532 for (
int i = 0; i <
N; ++i)
543 template<
int N,
typename T,
class S>
547 if (!myOwnData) {
return; }
550 for (
int i = 0; i <
N; ++i)
552 myCapacity *= res[i];
553 myCdf[i] =
new T[myCapacity];
557 template<
int N,
typename T,
class S>
562 for (
int i = 0; i <
N; ++i)
565 myIRes[i] = 1.0F / myRes[i];
568 if (mySize > myCapacity) { realloc(res); }
571 template<
int N,
typename T,
class S>
575 const bool ©data)
577 if (myOwnData && !copydata)
581 myOwnData = copydata;
584 memcpy(myCdf[
N-1], data, mySize*
sizeof(
T));
592 template<
int N,
typename T,
class S>
598 static_assert(
F::N ==
N,
"unsupported number of dimensions");
604 template<
int N,
typename T,
class S>
610 for (
int i =
N; i-- > 0; )
619 myCdf[i][idx] =
value;
624 for (
exint k = 1; k < myRes[i]; ++k, ++idx)
626 value =
SYSmax(myCdf[i][idx], 0.0F);
632 myCdf[i-1][
j] = myCdf[i][idx-1];
639 for (
int i = 0; i <
N; ++i)
640 myScale /= (
T)myRes[i];
642 myISum = 1.0F / getSum();
643 myIScale = 1.0F / getScaling();
646 template<
int N,
typename T,
class S>
652 sample(u, dval, pdf);
655 template<
int N,
typename T,
class S>
661 sample(u, didx, dval, pdf);
662 for (
int i = 0; i <
N; ++i)
665 dval[i] *= myIRes[i];
669 template<
int N,
typename T,
class S>
675 for (
int i = 0; i <
N; ++i)
679 const T *cdf = myCdf[i] + off;
681 idx = didx[i] = UTsampleCdf<S>(
682 cdf, myRes[i], u[i] * cdf[myRes[i]-1]);
686 template<
int N,
typename T,
class S>
694 for (
int i = 0; i <
N; ++i)
698 const T *cdf = myCdf[i] + off;
700 idx = didx[i] = UTsampleCdf<S>(
701 cdf, myRes[i], u[i], doff[i], onepdf);
706 template<
int N,
typename T,
class S>
713 for (
int i = 0; i <
N; ++i)
716 idx = (
int)(u[i]*myRes[i]);
719 const T *cdf = myCdf[i] + off;
726 onepdf = myRes[i] * range / cdf[myRes[i]-1];
733 template<
int N,
typename T,
class S>
737 for (
int dim = 0; dim <
N; ++dim)
739 fprintf(stderr,
"dim %d:\n", dim);
740 for (
int i = 0; i < myRes[dim]; ++i)
741 fprintf(stderr,
"%d: %f\n", i, myCdf[dim][i]);
const T & getSum() const
Get the sum over all image values.
void clear()
Clear associated storage.
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
static void eval(T *cdf, const int *res, const F &ftor)
static int eval(const int *res, const int *i)
void evaluatePdf(const T *u, T &pdf) const
Evaluate the pdf at the given index.
T UTsampleTrapezoidPdf(T u, T d0, T d1)
const T & getScaling() const
Get the average image value.
void setRawArray(T *data, const bool ©data)
GLsizei const GLfloat * value
static void eval(T *cdf, const int *res, const F &ftor)
static int eval(const int *res, const int *i)
float operator()(const int *i) const
void UTfillCdfIdentity(OutputIt cdf, const int res)
Fill a linear cdf with res entries of a linear gradient.
UT_CdfStdBinarySearch UT_Cdf_default_search_t
static int eval(const int *res, const int *i)
static int eval(InputIt cdf_begin, const int &res, const T &u)
const T & getIScaling() const
void sample(const T *u, T *dval) const
IMATH_NAMESPACE::V2f float
const int & getRes(int dim) const
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
static int eval(InputIt cdf_begin, const int &res, const T &u)
UT_CdfValueVoxelArrayF(const UT_VoxelArrayF *voxels)
HUSD_API bool eval(VtValue &val, T &ret_val)
static int eval(const int *res, const int *i)
size_t getMemoryUsage() const
GA_API const UT_StringHolder transform
static int eval(InputIt cdf_begin, const int &res, const T &u)
const T & getISum() const
void UTcreateCdf(InputIt values_begin, InputIt values_end, OutputIt cdf_begin)
Create a CDF from a range of values.
auto fprintf(std::FILE *f, const S &fmt, const T &...args) -> int
void UTcreatePdf(InputIt values_begin, InputIt values_end, OutputIt pdf_begin)
Create a PDF from a range of values.
void UTnormalizeArray(InputIt begin, InputIt end, OutputIt d_begin)
Normalize an array.
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
static void eval(T *cdf, const int *res, const F &ftor)
const UT_VoxelArrayF * myVoxels
GA_API const UT_StringHolder N
const int * getRes() const
int UTsampleCdf(InputIt cdf_begin, const int &res, const T &u)
static int quadratic(T a, T b, T c, T &v0, T &v1)
ForwardIt UTfastUpperBound(ForwardIt first, ForwardIt last, const T &value)
void resize(const int *res)
GA_API const UT_StringHolder area
static void eval(T *cdf, const int *res, const F &ftor)