-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.h
234 lines (164 loc) · 5.37 KB
/
Utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef UTILS_H
#define UTILS_H
#ifdef __INTEL_COMPILER
// Currently there is no way to detect C++0x mode on Windows.
// You can force specific features by manually defining COMPILER_HAS_XXX.
#endif
#if (defined(_MSC_VER) && !defined(__INTEL_COMPILER))
#if _MSC_VER >= 1600
#define COMPILER_HAS_LAMBDAS
#define COMPILER_HAS_RVALUE_REFERENCES
#endif
#endif
#if (defined(__GNUC__) && !defined(__INTEL_COMPILER))
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4)) && defined(__GXX_EXPERIMENTAL_CXX0X__)
#define COMPILER_HAS_LAMBDAS
#define COMPILER_HAS_RVALUE_REFERENCES
#define COMPILER_HAS_INITIALIZER_LISTS
#endif
#endif
#include "Vec.h"
#include "Mat.h"
#include "Array.h"
#include "Array2.h"
#include <cstdio>
float lerp(const float a,const float b,const float t);
template<int N> Vec<N,float> lerp(const Vec<N,float>& a,const Vec<N,float>& b,const float t)
{
return (1.0f-t)*a+t*b;
}
template<int N> Vec<N,float> lerp(const Vec<N,float>& a,const Vec<N,float>& b,const Vec<N,float>& t)
{
return (1.0f-t)*a+t*b;
}
template<unsigned int N> Vec<N,float> abs(const Vec<N,float>& x)
{
Vec<N,float> out;
for(unsigned int i=0;i<N;i++) out(i) = fabs(x(i));
return out;
}
template<unsigned int N> Vec<N,float> floor(const Vec<N,float>& x)
{
Vec<N,float> out;
for(unsigned int i=0;i<N;i++) out(i) = floorf(x(i));
return out;
}
template<unsigned int N> Vec<N,float> ceil(const Vec<N,float>& x)
{
Vec<N,float> out;
for(unsigned int i=0;i<N;i++) out(i) = ceilf(x(i));
return out;
}
template<unsigned int N> Vec<N,float> exp(const Vec<N,float>& x)
{
Vec<N,float> out;
for(unsigned int i=0;i<N;i++) out(i) = expf(x(i));
return out;
}
Vec2f cart2pol(const Vec2f& u);
Vec2f pol2cart(const Vec2f& u);
Vec2f perp(const Vec2f& u);
/*
TODO
----
float gausspdf(float mu,float sigma,const float x)
template<int N> Vec<N,float> gausspdf(const Vec<N,float>& mean,const Mat<N,N,float>& cov,const Vec<N,float>& x)
{
}
template<int N> Vec<N,float> reflect(const Vec<N,float>& I,const Vec<N,float>& N)
{
}
// ma smysl i v jiny dimenzi nez 3 ?
template<int N> Vec<N,float> refract(const Vec<N,float>& I,const Vec<N,float>& N,const float eta)
{
}
template<int N> Vec<N,float> refract(const Vec<N,float>& I,const Vec<N,float>& N,const float eta,bool* tir)
{
}
*/
template<typename T> bool arrayWrite(const char* fileName,const Array<T>& array)
{
const int n = array.length();
const T* d = array.data();
const int s = sizeof(T);
if (d==0) return false;
FILE* f = fopen(fileName,"wb");
if (!f) return false;
fwrite(&n,sizeof(n),1,f);
fwrite(&s,sizeof(s),1,f);
fwrite(d,sizeof(T)*n,1,f);
fclose(f);
return true;
}
template<typename T> bool arrayRead(const char* fileName,Array<T>* array)
{
if (array==0) return false;
FILE* f = fopen(fileName,"rb");
if (!f) return false;
int n,s;
fread(&n,sizeof(n),1,f);
fread(&s,sizeof(s),1,f);
if (n<=0 || s!=sizeof(T)) { fclose(f); return false; }
(*array) = Array<T>(n);
fread(array->data(),sizeof(T)*n,1,f);
fclose(f);
return true;
}
template<typename T> Array<T> arrayRead(const char* fileName)
{
Array<T> array;
arrayRead(fileName,&array);
return array;
}
template<typename T> bool arrayWrite(const char* fileName,const Array2<T>& array)
{
const int w = array.width();
const int h = array.height();
const T* d = array.data();
const int s = sizeof(T);
if (d==0) return false;
FILE* f = fopen(fileName,"wb");
if (!f) return false;
fwrite(&w,sizeof(w),1,f);
fwrite(&h,sizeof(h),1,f);
fwrite(&s,sizeof(s),1,f);
fwrite(d,sizeof(T)*w*h,1,f);
fclose(f);
return true;
}
template<typename T> bool arrayRead(const char* fileName,Array2<T>* array)
{
if (array==0) return false;
FILE* f = fopen(fileName,"rb");
if (!f) return false;
int w,h,s;
fread(&w,sizeof(w),1,f);
fread(&h,sizeof(h),1,f);
fread(&s,sizeof(s),1,f);
if (w<=0 || h<=0 || s!=sizeof(T)) { fclose(f); return false; }
(*array) = Array2<T>(w,h);
fread(array->data(),sizeof(T)*w*h,1,f);
fclose(f);
return true;
}
template<typename T> Array2<T> arrayRead(const char* fileName)
{
Array2<T> array;
arrayRead(fileName,&array);
return array;
}
bool imageRead(const char* fileName,Array2<unsigned char>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec2uc>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec3uc>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec4uc>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<float>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec2f>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec3f>* image,float scale=1.0f,float gamma=1.0f);
bool imageRead(const char* fileName,Array2<Vec4f>* image,float scale=1.0f,float gamma=1.0f);
template<typename T> Array2<T> imageRead(const char* fileName,float scale=1.0,float gamma=1.0f)
{
Array2<T> image;
imageRead(fileName,&image,scale,gamma);
return image;
}
#endif