Nori  23
common.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #if !defined(__NORI_COMMON_H)
20 #define __NORI_COMMON_H
21 
22 #if defined(_MSC_VER)
23 /* Disable some warnings on MSVC++ */
24 #pragma warning(disable : 4127 4702 4100 4515 4800 4146 4512)
25 #define WIN32_LEAN_AND_MEAN /* Don't ever include MFC on Windows */
26 #define NOMINMAX /* Don't override min/max */
27 #endif
28 
29 /* Include the basics needed by any Nori file */
30 #include <iostream>
31 #include <algorithm>
32 #include <vector>
33 #include <Eigen/Core>
34 #include <stdint.h>
35 #include <ImathPlatform.h>
36 #include <tinyformat.h>
37 
38 /* Convenience definitions */
39 #define NORI_NAMESPACE_BEGIN namespace nori {
40 #define NORI_NAMESPACE_END }
41 
42 #if defined(__NORI_APPLE__NORI_)
43 #define PLATFORM_MACOS
44 #elif defined(__NORI_linux__NORI_)
45 #define PLATFORM_LINUX
46 #elif defined(WIN32)
47 #define PLATFORM_WINDOWS
48 #endif
49 
50 /* "Ray epsilon": relative error threshold for ray intersection computations */
51 #define Epsilon 1e-4f
52 
53 /* A few useful constants */
54 #undef M_PI
55 
56 #define M_PI 3.14159265358979323846f
57 #define INV_PI 0.31830988618379067154f
58 #define INV_TWOPI 0.15915494309189533577f
59 #define INV_FOURPI 0.07957747154594766788f
60 #define SQRT_TWO 1.41421356237309504880f
61 #define INV_SQRT_TWO 0.70710678118654752440f
62 
63 /* Forward declarations */
64 namespace filesystem {
65  class path;
66  class resolver;
67 };
68 
69 NORI_NAMESPACE_BEGIN
70 
71 /* Forward declarations */
72 template <typename Scalar, int Dimension> struct TVector;
73 template <typename Scalar, int Dimension> struct TPoint;
74 template <typename Point, typename Vector> struct TRay;
75 template <typename Point> struct TBoundingBox;
76 
77 /* Basic Nori data structures (vectors, points, rays, bounding boxes,
78  kd-trees) are oblivious to the underlying data type and dimension.
79  The following list of typedefs establishes some convenient aliases
80  for specific types. */
100 typedef TPoint<double, 4> Point4d;
101 typedef TPoint<int, 1> Point1i;
102 typedef TPoint<int, 2> Point2i;
103 typedef TPoint<int, 3> Point3i;
104 typedef TPoint<int, 4> Point4i;
119 
121 class BSDF;
122 class Bitmap;
123 class BlockGenerator;
124 class Camera;
125 class ImageBlock;
126 class Integrator;
127 class KDTree;
128 class Emitter;
129 struct EmitterQueryRecord;
130 class Shape;
131 class NoriObject;
132 class NoriObjectFactory;
133 class NoriScreen;
134 class PhaseFunction;
136 class Sampler;
137 class Scene;
138 
140 using std::cout;
141 using std::cerr;
142 using std::endl;
143 
144 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> MatrixXf;
145 typedef Eigen::Matrix<uint32_t, Eigen::Dynamic, Eigen::Dynamic> MatrixXu;
146 
148 class NoriException : public std::runtime_error {
149 public:
151  template <typename... Args> NoriException(const char *fmt, const Args &... args)
152  : std::runtime_error(tfm::format(fmt, args...)) { }
153 };
154 
156 extern int getCoreCount();
157 
159 extern std::string indent(const std::string &string, int amount = 2);
160 
162 extern std::string toLower(const std::string &value);
163 
165 extern bool toBool(const std::string &str);
166 
168 extern int toInt(const std::string &str);
169 
171 extern unsigned int toUInt(const std::string &str);
172 
174 extern float toFloat(const std::string &str);
175 
177 extern size_t vectorSize(const std::string &str);
178 
180 extern Eigen::Vector2f toVector2f(const std::string &str);
182 extern Eigen::Vector3f toVector3f(const std::string &str);
183 
185 extern std::vector<std::string> tokenize(const std::string &s, const std::string &delim = ", ", bool includeEmpty = false);
186 
188 extern bool endsWith(const std::string &value, const std::string &ending);
189 
191 extern std::string timeString(double time, bool precise = false);
192 
194 extern std::string memString(size_t size, bool precise = false);
195 
197 enum EMeasure {
198  EUnknownMeasure = 0,
199  ESolidAngle,
200  EDiscrete
201 };
202 
204 inline float radToDeg(float value) { return value * (180.0f / M_PI); }
205 
207 inline float degToRad(float value) { return value * (M_PI / 180.0f); }
208 
209 #if !defined(_GNU_SOURCE)
211  inline void sincosf(float theta, float *_sin, float *_cos) {
212  *_sin = sinf(theta);
213  *_cos = cosf(theta);
214  }
215 #endif
216 
218 inline float clamp(float value, float min, float max) {
219  if (value < min)
220  return min;
221  else if (value > max)
222  return max;
223  else return value;
224 }
225 
227 inline int clamp(int value, int min, int max) {
228  if (value < min)
229  return min;
230  else if (value > max)
231  return max;
232  else return value;
233 }
234 
236 inline float lerp(float t, float v1, float v2) {
237  return ((float) 1 - t) * v1 + t * v2;
238 }
239 
241 inline int mod(int a, int b) {
242  int r = a % b;
243  return (r < 0) ? r+b : r;
244 }
245 
247 extern Vector3f sphericalDirection(float theta, float phi);
248 
250 extern Point2f sphericalCoordinates(const Vector3f &dir);
251 
264 extern float fresnel(float cosThetaI, float extIOR, float intIOR);
265 
272 extern filesystem::resolver *getFileResolver();
273 
274 NORI_NAMESPACE_END
275 
276 #endif /* __NORI_COMMON_H */
Superclass of all bidirectional scattering distribution functions.
Definition: bsdf.h:63
Stores a RGB high dynamic-range bitmap.
Definition: bitmap.h:32
Spiraling block generator.
Definition: block.h:137
Generic camera interface.
Definition: camera.h:35
Superclass of all emitters.
Definition: emitter.h:64
Weighted pixel storage for a rectangular subregion of an image.
Definition: block.h:48
Abstract integrator (i.e. a rendering technique)
Definition: integrator.h:35
Simple exception class, which stores a human-readable error description.
Definition: common.h:148
NoriException(const char *fmt, const Args &... args)
Variadic template constructor to support printf-style arguments.
Definition: common.h:151
Factory for Nori objects.
Definition: object.h:128
Base class of all objects.
Definition: object.h:32
Definition: gui.h:47
Generic radially symmetric image reconstruction filter.
Definition: rfilter.h:41
Abstract sample generator.
Definition: sampler.h:63
Main scene data structure.
Definition: scene.h:34
Superclass of all shapes.
Definition: shape.h:97
Data record for conveniently querying and sampling the direct illumination technique implemented by a...
Definition: emitter.h:31
Generic n-dimensional bounding box data structure.
Definition: bbox.h:42
Generic N-dimensional point data structure based on Eigen::Matrix.
Definition: vector.h:85
Simple n-dimensional ray segment data structure.
Definition: ray.h:38
Generic N-dimensional vector data structure based on Eigen::Matrix.
Definition: vector.h:37