Library of Assembled Shared Sources
inverse_transform_sampling.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_INVERSE_TRANSFORM_SAMPLING_H
44#define LASS_GUARDIAN_OF_INCLUSION_NUM_INVERSE_TRANSFORM_SAMPLING_H
45
46#include "num_common.h"
47#include "../prim/point_2d.h"
48
49#include <cstddef>
50
51namespace lass
52{
53namespace num
54{
55namespace impl
56{
57
58/**
59 * @param [first, last) [in]
60 * @param sample [in] value between 0 and 1, inclusive
61 * @param pdf [out] probability density of returned sample
62 * @param index [out]
63 * @return value between 0 and 1.
64 */
65template <typename RandomIterator, typename T>
66T sampleCdf1D(RandomIterator first, RandomIterator last, T sample, T& pdf, size_t& index)
67{
68 LASS_ASSERT(first != last);
69 const T n = static_cast<T>(last - first);
70 const RandomIterator it = std::lower_bound(first, last, sample);
71 if (it == first)
72 {
73 pdf = n * *first;
74 index = 0;
75 return sample / pdf;
76 }
77 if (it == last)
78 {
79 pdf = n * (*(last - 1) - *(last - 2));
80 index = static_cast<size_t>(last - first) - 1;
81 return 1;
82 }
83 const RandomIterator prev = it - 1;
84 index = static_cast<size_t>(it - first);
85 pdf = n * (*it - *prev);
86 const T x0 = static_cast<T>(index) / n;
87 return x0 + (sample - *prev) / pdf;
88}
89
90}
91
92template <typename T>
93class InverseTransformSampling2D
94{
95public:
96 typedef T TValue;
97 typedef prim::Point2D<T> TSample;
98 typedef prim::Point2D<size_t> TIndex;
99
100 InverseTransformSampling2D(): uSize_(0), vSize_(0) {}
101
102 template <typename InputIterator>
103 InverseTransformSampling2D(InputIterator first, InputIterator last, size_t uSize, size_t vSize)
104 {
105 reset(first, last, uSize, vSize);
106 }
107
108 template <typename InputIterator>
109 void reset(InputIterator first, InputIterator last, size_t uSize, size_t vSize)
110 {
111 LASS_ENFORCE(uSize > 0 && vSize > 0);
112 uSize_ = static_cast<std::ptrdiff_t>(uSize);
113 vSize_ = static_cast<std::ptrdiff_t>(vSize);
114 condCdfV_.resize(uSize * vSize);
115 margCdfU_.resize(uSize);
116 std::copy(first, last, condCdfV_.begin());
117 buildCdf();
118 }
119
120 TSample operator()(const TSample& in, TValue& pdf, TIndex& index) const
121 {
122 LASS_ASSERT(!margCdfU_.empty());
123 TValue pdfU, pdfV;
124 const TValue u = impl::sampleCdf1D(margCdfU_.begin(), margCdfU_.end(), in.x, pdfU, index.x);
125 const typename TValues::const_iterator cdfV = condCdfV_.begin() + static_cast<std::ptrdiff_t>(index.x) * vSize_;
126 const TValue v = impl::sampleCdf1D(cdfV, cdfV + vSize_, in.y, pdfV, index.y);
127 pdf = pdfU * pdfV;
128 return TSample(u, v);
129 }
130
131private:
132 typedef std::vector<TValue> TValues;
133
134 void buildCdf()
135 {
136 LASS_ASSERT(condCdfV_.size() == static_cast<size_t>(uSize_ * vSize_));
137 const typename TValues::iterator margCdfU = margCdfU_.begin();
138 for (std::ptrdiff_t i = 0; i < uSize_; ++i)
139 {
140 const typename TValues::iterator firstCdfV = condCdfV_.begin() + i * vSize_;
141 const typename TValues::iterator lastCdfV = firstCdfV + vSize_;
142 std::partial_sum(firstCdfV, lastCdfV, firstCdfV);
143 const TValue maxCdfV = *(lastCdfV - 1);
144 *(margCdfU + i) = maxCdfV;
145 std::transform(firstCdfV, lastCdfV, firstCdfV, [maxCdfV](TValue cdf) { return cdf / maxCdfV; });
146 }
147 std::partial_sum(margCdfU_.begin(), margCdfU_.end(), margCdfU_.begin());
148 const TValue maxCdfU = margCdfU_.back();
149 std::transform(margCdfU_.begin(), margCdfU_.end(), margCdfU_.begin(), [maxCdfU](TValue cdf) { return cdf / maxCdfU; });
150 }
151
152 TValues condCdfV_;
153 TValues margCdfU_;
154 std::ptrdiff_t uSize_;
155 std::ptrdiff_t vSize_;
156};
157
158
159}
160}
161
162#endif
T sampleCdf1D(RandomIterator first, RandomIterator last, T sample, T &pdf, size_t &index)
numeric types and traits.
Definition basic_ops.h:70
ColorRGBA in(const ColorRGBA &a, const ColorRGBA &b)
part of a inside b.
Library for Assembled Shared Sources.
Definition config.h:53