Cycles: Make all #include statements relative to cycles source directory
[blender.git] / intern / cycles / util / util_math_cdf.cpp
1 /*
2  * Copyright 2011-2015 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "util/util_math_cdf.h"
18
19 #include "util/util_algorithm.h"
20 #include "util/util_math.h"
21
22 CCL_NAMESPACE_BEGIN
23
24 /* Invert pre-calculated CDF function. */
25 void util_cdf_invert(const int resolution,
26                      const float from,
27                      const float to,
28                      const vector<float> &cdf,
29                      const bool make_symmetric,
30                      vector<float> &inv_cdf) {
31         const float inv_resolution = 1.0f / (float)resolution;
32         const float range = to - from;
33         inv_cdf.resize(resolution);
34         if(make_symmetric) {
35                 const int half_size = (resolution - 1) / 2;
36                 for(int i = 0; i <= half_size; i++) {
37                         float x = i / (float)half_size;
38                         int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
39                         float t;
40                         if(index < cdf.size() - 1) {
41                                 t = (x - cdf[index])/(cdf[index+1] - cdf[index]);
42                         } else {
43                                 t = 0.0f;
44                                 index = cdf.size() - 1;
45                         }
46                         float y = ((index + t) / (resolution - 1)) * (2.0f * range);
47                         inv_cdf[half_size+i] = 0.5f*(1.0f + y);
48                         inv_cdf[half_size-i] = 0.5f*(1.0f - y);
49                 }
50         }
51         else {
52                 for(int i = 0; i < resolution; i++) {
53                         float x = from + range * (float)i * inv_resolution;
54                         int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
55                         float t;
56                         if(index < cdf.size() - 1) {
57                                 t = (x - cdf[index])/(cdf[index+1] - cdf[index]);
58                         } else {
59                                 t = 0.0f;
60                                 index = resolution;
61                         }
62                         inv_cdf[i] = (index + t) * inv_resolution;
63                 }
64         }
65 }
66
67 CCL_NAMESPACE_END