forked from moonlovelj/nori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
325 lines (265 loc) · 8.59 KB
/
common.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
This file is part of Nori, a simple educational ray tracer
Copyright (c) 2015 by Wenzel Jakob
*/
#include <nori/object.h>
#include <nori/frame.h>
#include <Eigen/Geometry>
#include <Eigen/LU>
#include <filesystem/resolver.h>
#include <iomanip>
#if defined(PLATFORM_LINUX)
#include <malloc.h>
#endif
#if defined(PLATFORM_WINDOWS)
#include <windows.h>
#endif
#if defined(PLATFORM_MACOS)
#include <sys/sysctl.h>
#endif
NORI_NAMESPACE_BEGIN
std::string indent(const std::string &string, int amount) {
/* This could probably be done faster (it's not
really speed-critical though) */
std::istringstream iss(string);
std::ostringstream oss;
std::string spacer(amount, ' ');
bool firstLine = true;
for (std::string line; std::getline(iss, line); ) {
if (!firstLine)
oss << spacer;
oss << line;
if (!iss.eof())
oss << endl;
firstLine = false;
}
return oss.str();
}
bool endsWith(const std::string &value, const std::string &ending) {
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
std::string toLower(const std::string &value) {
std::string result;
result.resize(value.size());
std::transform(value.begin(), value.end(), result.begin(), ::tolower);
return result;
}
bool toBool(const std::string &str) {
std::string value = toLower(str);
if (value == "false")
return false;
else if (value == "true")
return true;
else
throw NoriException("Could not parse boolean value \"%s\"", str);
}
int toInt(const std::string &str) {
char *end_ptr = nullptr;
int result = (int) strtol(str.c_str(), &end_ptr, 10);
if (*end_ptr != '\0')
throw NoriException("Could not parse integer value \"%s\"", str);
return result;
}
unsigned int toUInt(const std::string &str) {
char *end_ptr = nullptr;
unsigned int result = (int) strtoul(str.c_str(), &end_ptr, 10);
if (*end_ptr != '\0')
throw NoriException("Could not parse integer value \"%s\"", str);
return result;
}
float toFloat(const std::string &str) {
char *end_ptr = nullptr;
float result = (float) strtof(str.c_str(), &end_ptr);
if (*end_ptr != '\0')
throw NoriException("Could not parse floating point value \"%s\"", str);
return result;
}
Eigen::Vector3f toVector3f(const std::string &str) {
std::vector<std::string> tokens = tokenize(str);
if (tokens.size() != 3)
throw NoriException("Expected 3 values");
Eigen::Vector3f result;
for (int i=0; i<3; ++i)
result[i] = toFloat(tokens[i]);
return result;
}
std::vector<std::string> tokenize(const std::string &string, const std::string &delim, bool includeEmpty) {
std::string::size_type lastPos = 0, pos = string.find_first_of(delim, lastPos);
std::vector<std::string> tokens;
while (lastPos != std::string::npos) {
if (pos != lastPos || includeEmpty)
tokens.push_back(string.substr(lastPos, pos - lastPos));
lastPos = pos;
if (lastPos != std::string::npos) {
lastPos += 1;
pos = string.find_first_of(delim, lastPos);
}
}
return tokens;
}
std::string timeString(double time, bool precise) {
if (std::isnan(time) || std::isinf(time))
return "inf";
std::string suffix = "ms";
if (time > 1000) {
time /= 1000; suffix = "s";
if (time > 60) {
time /= 60; suffix = "m";
if (time > 60) {
time /= 60; suffix = "h";
if (time > 12) {
time /= 12; suffix = "d";
}
}
}
}
std::ostringstream os;
os << std::setprecision(precise ? 4 : 1)
<< std::fixed << time << suffix;
return os.str();
}
std::string memString(size_t size, bool precise) {
double value = (double) size;
const char *suffixes[] = {
"B", "KiB", "MiB", "GiB", "TiB", "PiB"
};
int suffix = 0;
while (suffix < 5 && value > 1024.0f) {
value /= 1024.0f; ++suffix;
}
std::ostringstream os;
os << std::setprecision(suffix == 0 ? 0 : (precise ? 4 : 1))
<< std::fixed << value << " " << suffixes[suffix];
return os.str();
}
filesystem::resolver *getFileResolver() {
static filesystem::resolver *resolver = new filesystem::resolver();
return resolver;
}
Color3f Color3f::toSRGB() const {
Color3f result;
for (int i=0; i<3; ++i) {
float value = coeff(i);
if (value <= 0.0031308f)
result[i] = 12.92f * value;
else
result[i] = (1.0f + 0.055f)
* std::pow(value, 1.0f/2.4f) - 0.055f;
}
return result;
}
Color3f Color3f::toLinearRGB() const {
Color3f result;
for (int i=0; i<3; ++i) {
float value = coeff(i);
if (value <= 0.04045f)
result[i] = value * (1.0f / 12.92f);
else
result[i] = std::pow((value + 0.055f)
* (1.0f / 1.055f), 2.4f);
}
return result;
}
bool Color3f::isValid() const {
for (int i=0; i<3; ++i) {
float value = coeff(i);
if (value < 0 || !std::isfinite(value))
return false;
}
return true;
}
float Color3f::getLuminance() const {
return coeff(0) * 0.212671f + coeff(1) * 0.715160f + coeff(2) * 0.072169f;
}
Transform::Transform(const Eigen::Matrix4f &trafo)
: m_transform(trafo), m_inverse(trafo.inverse()) { }
std::string Transform::toString() const {
std::ostringstream oss;
oss << m_transform.format(Eigen::IOFormat(4, 0, ", ", ";\n", "", "", "[", "]"));
return oss.str();
}
Transform Transform::operator*(const Transform &t) const {
return Transform(m_transform * t.m_transform,
t.m_inverse * m_inverse);
}
Vector3f sphericalDirection(float theta, float phi) {
float sinTheta, cosTheta, sinPhi, cosPhi;
sincosf(theta, &sinTheta, &cosTheta);
sincosf(phi, &sinPhi, &cosPhi);
return Vector3f(
sinTheta * cosPhi,
sinTheta * sinPhi,
cosTheta
);
}
Point2f sphericalCoordinates(const Vector3f &v) {
Point2f result(
std::acos(v.z()),
std::atan2(v.y(), v.x())
);
if (result.y() < 0)
result.y() += 2*M_PI;
return result;
}
extern Vector3f reflect(const Vector3f &wi, const Vector3f &normal) {
return 2 * wi.dot(normal)*normal - wi;
}
void coordinateSystem(const Vector3f &a, Vector3f &b, Vector3f &c) {
if (std::abs(a.x()) > std::abs(a.y())) {
float invLen = 1.0f / std::sqrt(a.x() * a.x() + a.z() * a.z());
c = Vector3f(a.z() * invLen, 0.0f, -a.x() * invLen);
} else {
float invLen = 1.0f / std::sqrt(a.y() * a.y() + a.z() * a.z());
c = Vector3f(0.0f, a.z() * invLen, -a.y() * invLen);
}
b = c.cross(a);
}
float fresnel(float cosThetaI, float extIOR, float intIOR) {
float etaI = extIOR, etaT = intIOR;
if (extIOR == intIOR)
return 0.0f;
/* Swap the indices of refraction if the interaction starts
at the inside of the object */
if (cosThetaI < 0.0f) {
std::swap(etaI, etaT);
cosThetaI = -cosThetaI;
}
/* Using Snell's law, calculate the squared sine of the
angle between the normal and the transmitted ray */
float eta = etaI / etaT,
sinThetaTSqr = eta*eta * (1-cosThetaI*cosThetaI);
if (sinThetaTSqr > 1.0f)
return 1.0f; /* Total internal reflection! */
float cosThetaT = std::sqrt(1.0f - sinThetaTSqr);
float Rs = (etaI * cosThetaI - etaT * cosThetaT)
/ (etaI * cosThetaI + etaT * cosThetaT);
float Rp = (etaT * cosThetaI - etaI * cosThetaT)
/ (etaT * cosThetaI + etaI * cosThetaT);
return (Rs * Rs + Rp * Rp) / 2.0f;
}
Vector3f refract(const Vector3f &n, const Vector3f &wi, float extIOR, float intIOR) {
float etaI = extIOR, etaT = intIOR;
if (extIOR == intIOR) return Vector3f(0.0f);
auto cosThetaI = n.dot(wi);
/* Swap the indices of refraction if the interaction starts
at the inside of the object */
Vector3f N = n;
if (cosThetaI < 0.0f) {
std::swap(etaI, etaT);
cosThetaI = -cosThetaI;
N = -N;
}
/* Using Snell's law, calculate the squared sine of the
angle between the normal and the transmitted ray */
float eta = etaI / etaT;
float sinThetaTSqr = eta*eta * (1-cosThetaI*cosThetaI);
if (sinThetaTSqr > 1.0f) return Vector3f(0.0f);; /* Total internal reflection! */
auto cosThetaTSqr = 1.f - sinThetaTSqr;
return -eta * wi + (eta * cosThetaI - sqrtf(cosThetaTSqr)) * N;
}
bool sameHemisphere(const Vector3f &w, const Vector3f &wp) {
return w.z() * wp.z() > 0;
}
NORI_NAMESPACE_END