]> git.sesse.net Git - movit/blob - util.cpp
Run include-what-you-use over all of movit. Some hand tuning.
[movit] / util.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <Eigen/Core>
8
9 #include "init.h"
10 #include "util.h"
11
12 extern std::string *movit_data_directory;
13
14 void hsv2rgb(float h, float s, float v, float *r, float *g, float *b)
15 {
16         if (h < 0.0f) {
17                 h += 2.0f * M_PI;
18         }
19         float c = v * s;
20         float hp = (h * 180.0 / M_PI) / 60.0;
21         float x = c * (1 - fabs(fmod(hp, 2.0f) - 1.0f));
22
23         if (hp >= 0 && hp < 1) {
24                 *r = c;
25                 *g = x;
26                 *b = 0.0f;
27         } else if (hp >= 1 && hp < 2) {
28                 *r = x;
29                 *g = c;
30                 *b = 0.0f;
31         } else if (hp >= 2 && hp < 3) {
32                 *r = 0.0f;
33                 *g = c;
34                 *b = x;
35         } else if (hp >= 3 && hp < 4) {
36                 *r = 0.0f;
37                 *g = x;
38                 *b = c;
39         } else if (hp >= 4 && hp < 5) {
40                 *r = x;
41                 *g = 0.0f;
42                 *b = c;
43         } else {
44                 *r = c;
45                 *g = 0.0f;
46                 *b = x;
47         }
48
49         float m = v - c;
50         *r += m;
51         *g += m;
52         *b += m;
53 }
54
55 void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b)
56 {
57         float ref_r, ref_g, ref_b;
58         hsv2rgb(h, s, v, r, g, b);
59         hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b);
60         float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b;
61         float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b;
62         if (lum > 1e-3) {
63                 float fac = ref_lum / lum;
64                 *r *= fac;
65                 *g *= fac;
66                 *b *= fac;
67         }
68 }
69
70 std::string read_file(const std::string &filename)
71 {
72         const std::string full_pathname = *movit_data_directory + "/" + filename;
73
74         static char buf[131072];
75         FILE *fp = fopen(full_pathname.c_str(), "r");
76         if (fp == NULL) {
77                 perror(full_pathname.c_str());
78                 exit(1);
79         }
80
81         int len = fread(buf, 1, sizeof(buf), fp);
82         fclose(fp);
83
84         return std::string(buf, len);
85 }
86
87 GLuint compile_shader(const std::string &shader_src, GLenum type)
88 {
89         GLuint obj = glCreateShader(type);
90         const GLchar* source[] = { shader_src.data() };
91         const GLint length[] = { (GLint)shader_src.size() };
92         glShaderSource(obj, 1, source, length);
93         glCompileShader(obj);
94
95         GLchar info_log[4096];
96         GLsizei log_length = sizeof(info_log) - 1;
97         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
98         info_log[log_length] = 0; 
99         if (strlen(info_log) > 0) {
100                 printf("shader compile log: %s\n", info_log);
101         }
102
103         GLint status;
104         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
105         if (status == GL_FALSE) {
106                 exit(1);
107         }
108
109         return obj;
110 }
111
112 void print_3x3_matrix(const Eigen::Matrix3d& m)
113 {
114         printf("%6.4f %6.4f %6.4f\n", m(0,0), m(0,1), m(0,2));
115         printf("%6.4f %6.4f %6.4f\n", m(1,0), m(1,1), m(1,2));
116         printf("%6.4f %6.4f %6.4f\n", m(2,0), m(2,1), m(2,2));
117         printf("\n");
118 }
119
120 std::string output_glsl_mat3(const std::string &name, const Eigen::Matrix3d &m)
121 {
122         char buf[1024];
123         sprintf(buf,
124                 "const mat3 %s = mat3(\n"
125                 "    %.8f, %.8f, %.8f,\n"
126                 "    %.8f, %.8f, %.8f,\n"
127                 "    %.8f, %.8f, %.8f);\n\n",
128                 name.c_str(),
129                 m(0,0), m(1,0), m(2,0),
130                 m(0,1), m(1,1), m(2,1),
131                 m(0,2), m(1,2), m(2,2));
132         return buf;
133 }
134
135 void combine_two_samples(float w1, float w2, float *offset, float *total_weight, float *sum_sq_error)
136 {
137         assert(movit_initialized);
138         assert(w1 * w2 >= 0.0f);  // Should not have differing signs.
139         float z;  // Just a shorter name for offset.
140         if (fabs(w1 + w2) < 1e-6) {
141                 z = 0.5f;
142         } else {
143                 z = w2 / (w1 + w2);
144         }
145
146         // Round to the minimum number of bits we have measured earlier.
147         // The card will do this for us anyway, but if we know what the real z
148         // is, we can pick a better total_weight below.
149         z = lrintf(z / movit_texel_subpixel_precision) * movit_texel_subpixel_precision;
150         
151         // Choose total weight w so that we minimize total squared error
152         // for the effective weights:
153         //
154         //   e = (w(1-z) - a)² + (wz - b)²
155         //
156         // Differentiating by w and setting equal to zero:
157         //
158         //   2(w(1-z) - a)(1-z) + 2(wz - b)z = 0
159         //   w(1-z)² - a(1-z) + wz² - bz = 0
160         //   w((1-z)² + z²) = a(1-z) + bz
161         //   w = (a(1-z) + bz) / ((1-z)² + z²)
162         //
163         // If z had infinite precision, this would simply reduce to w = w1 + w2.
164         *total_weight = (w1 * (1 - z) + w2 * z) / (z * z + (1 - z) * (1 - z));
165         *offset = z;
166
167         if (sum_sq_error != NULL) {
168                 float err1 = *total_weight * (1 - z) - w1;
169                 float err2 = *total_weight * z - w2;
170                 *sum_sq_error = err1 * err1 + err2 * err2;
171         }
172
173         assert(*offset >= 0.0f);
174         assert(*offset <= 1.0f);
175 }