PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/core/pbrt.h
00001 
00002 /*
00003     pbrt source code Copyright(c) 1998-2012 Matt Pharr and Greg Humphreys.
00004 
00005     This file is part of pbrt.
00006 
00007     Redistribution and use in source and binary forms, with or without
00008     modification, are permitted provided that the following conditions are
00009     met:
00010 
00011     - Redistributions of source code must retain the above copyright
00012       notice, this list of conditions and the following disclaimer.
00013 
00014     - Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in the
00016       documentation and/or other materials provided with the distribution.
00017 
00018     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00019     IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00021     PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030  */
00031 
00032 #if defined(_MSC_VER)
00033 #define NOMINMAX 
00034 #pragma once
00035 #endif
00036 
00037 #ifndef PBRT_CORE_PBRT_H
00038 #define PBRT_CORE_PBRT_H
00039 
00040 // core/pbrt.h*
00041 
00042 #if defined(_WIN32) || defined(_WIN64)
00043 #define PBRT_IS_WINDOWS
00044 #elif defined(__linux__)
00045 #define PBRT_IS_LINUX
00046 #elif defined(__APPLE__)
00047   #define PBRT_IS_APPLE
00048   #if !(defined(__i386__) || defined(__amd64__))
00049   #define PBRT_IS_APPLE_PPC
00050   #else
00051   #define PBRT_IS_APPLE_X86
00052   #endif
00053 #elif defined(__OpenBSD__)
00054 #define PBRT_IS_OPENBSD
00055 #endif
00056 
00057 // Global Include Files
00058 #include <math.h>
00059 #include <stdlib.h>
00060 #define _GNU_SOURCE 1
00061 #include <stdio.h>
00062 #include <string.h>
00063 #include <string>
00064 using std::string;
00065 #include <vector>
00066 using std::vector;
00067 #include "error.h"
00068 #if !defined(PBRT_IS_APPLE) && !defined(PBRT_IS_OPENBSD)
00069 #include <malloc.h> // for _alloca, memalign
00070 #endif
00071 #if !defined(PBRT_IS_WINDOWS) && !defined(PBRT_IS_APPLE) && !defined(PBRT_IS_OPENBSD)
00072 #include <alloca.h>
00073 #endif
00074 #include <assert.h>
00075 #include <algorithm>
00076 using std::min;
00077 using std::max;
00078 using std::swap;
00079 using std::sort;
00080 
00081 // Platform-specific definitions
00082 #if defined(PBRT_IS_WINDOWS)
00083 #include <float.h>
00084 #define isnan _isnan
00085 #define isinf(f) (!_finite((f)))
00086 #define int8_t __int8
00087 #define uint8_t unsigned __int8
00088 #define int16_t __int16
00089 #define uint16_t unsigned __int16
00090 #define int32_t __int32
00091 #define uint32_t unsigned __int32
00092 #define int64_t __int64
00093 #define uint64_t unsigned __int64
00094 #pragma warning (disable : 4305) // double constant assigned to float
00095 #pragma warning (disable : 4244) // int -> float conversion
00096 #pragma warning (disable : 4267) // size_t -> unsigned int conversion
00097 #endif
00098 
00099 #ifdef PBRT_IS_LINUX
00100 #include <stdint.h>
00101 #endif // PBRT_IS_LINUX
00102 #if defined(PBRT_IS_WINDOWS)
00103 #define isnan _isnan
00104 #define isinf(f) (!_finite((f)))
00105 #endif
00106 
00107 // Global Macros
00108 #define ALLOCA(TYPE, COUNT) (TYPE *)alloca((COUNT) * sizeof(TYPE))
00109 
00110 // Global Forward Declarations
00111 class RNG;
00112 class Timer;
00113 class ProgressReporter;
00114 class MemoryArena;
00115 template <typename T, int logBlockSize = 2> class BlockedArray;
00116 struct Matrix4x4;
00117 class Mutex;
00118 class RWMutex;
00119 class Shape;
00120 class ParamSet;
00121 template <typename T> struct ParamSetItem;
00122 struct Options {
00123     Options() { nCores = 0;
00124                 quickRender = quiet = openWindow = verbose = false;
00125                 imageFile = ""; }
00126     int nCores;
00127     bool quickRender;
00128     bool quiet, verbose;
00129     bool openWindow;
00130     string imageFile;
00131 };
00132 
00133 
00134 extern Options PbrtOptions;
00135 class TextureParams;
00136 class Scene;
00137 class Renderer;
00138 class Vector;
00139 class Point;
00140 class Normal;
00141 class Ray;
00142 class RayDifferential;
00143 class BBox;
00144 class Transform;
00145 struct DifferentialGeometry;
00146 class Primitive;
00147 struct Intersection;
00148 class GeometricPrimitive;
00149 template <int nSamples> class CoefficientSpectrum;
00150 class RGBSpectrum;
00151 class SampledSpectrum;
00152 typedef RGBSpectrum Spectrum;
00153 // typedef SampledSpectrum Spectrum;
00154 class Camera;
00155 class ProjectiveCamera;
00156 class Sampler;
00157 struct CameraSample;
00158 struct Sample;
00159 class Filter;
00160 class Film;
00161 class BxDF;
00162 class BRDF;
00163 class BTDF;
00164 class BSDF;
00165 class Material;
00166 template <typename T> class Texture;
00167 class VolumeRegion;
00168 class BSSRDF;
00169 class Light;
00170 struct VisibilityTester;
00171 class AreaLight;
00172 struct Distribution1D;
00173 struct Distribution2D;
00174 struct BSDFSample;
00175 struct BSDFSampleOffsets;
00176 struct LightSample;
00177 struct LightSampleOffsets;
00178 class SurfaceIntegrator;
00179 class Integrator;
00180 class VolumeIntegrator;
00181 
00182 // Global Constants
00183 #define PBRT_VERSION "2.0.0"
00184 #ifdef M_PI
00185 #undef M_PI
00186 #endif
00187 #define M_PI       3.14159265358979323846f
00188 #define INV_PI     0.31830988618379067154f
00189 #define INV_TWOPI  0.15915494309189533577f
00190 #define INV_FOURPI 0.07957747154594766788f
00191 #ifndef INFINITY
00192 #define INFINITY FLT_MAX
00193 #endif
00194 #if defined(PBRT_IS_WINDOWS)
00195 #define alloca _alloca
00196 #endif
00197 #ifndef PBRT_L1_CACHE_LINE_SIZE
00198 #define PBRT_L1_CACHE_LINE_SIZE 64
00199 #endif
00200 #ifndef PBRT_POINTER_SIZE
00201 #if defined(__amd64__) || defined(_M_X64)
00202 #define PBRT_POINTER_SIZE 8
00203 #elif defined(__i386__) || defined(_M_IX86)
00204 #define PBRT_POINTER_SIZE 4
00205 #endif
00206 #endif
00207 #ifndef PBRT_HAS_64_BIT_ATOMICS
00208 #if (PBRT_POINTER_SIZE == 8)
00209 #define PBRT_HAS_64_BIT_ATOMICS
00210 #endif
00211 #endif // PBRT_HAS_64_BIT_ATOMICS
00212 
00213 // Global Inline Functions
00214 inline float Lerp(float t, float v1, float v2) {
00215     return (1.f - t) * v1 + t * v2;
00216 }
00217 
00218 
00219 inline float Clamp(float val, float low, float high) {
00220     if (val < low) return low;
00221     else if (val > high) return high;
00222     else return val;
00223 }
00224 
00225 
00226 inline int Clamp(int val, int low, int high) {
00227     if (val < low) return low;
00228     else if (val > high) return high;
00229     else return val;
00230 }
00231 
00232 
00233 inline int Mod(int a, int b) {
00234     int n = int(a/b);
00235     a -= n*b;
00236     if (a < 0) a += b;
00237     return a;
00238 }
00239 
00240 
00241 inline float Radians(float deg) {
00242     return ((float)M_PI/180.f) * deg;
00243 }
00244 
00245 
00246 inline float Degrees(float rad) {
00247     return (180.f/(float)M_PI) * rad;
00248 }
00249 
00250 
00251 inline float Log2(float x) {
00252     static float invLog2 = 1.f / logf(2.f);
00253     return logf(x) * invLog2;
00254 }
00255 
00256 
00257 inline int Floor2Int(float val);
00258 inline int Log2Int(float v) {
00259     return Floor2Int(Log2(v));
00260 }
00261 
00262 
00263 inline bool IsPowerOf2(int v) {
00264     return (v & (v - 1)) == 0;
00265 }
00266 
00267 
00268 inline uint32_t RoundUpPow2(uint32_t v) {
00269     v--;
00270     v |= v >> 1;    v |= v >> 2;
00271     v |= v >> 4;    v |= v >> 8;
00272     v |= v >> 16;
00273     return v+1;
00274 }
00275 
00276 
00277 inline int Floor2Int(float val) {
00278     return (int)floorf(val);
00279 }
00280 
00281 
00282 inline int Round2Int(float val) {
00283     return Floor2Int(val + 0.5f);
00284 }
00285 
00286 
00287 inline int Float2Int(float val) {
00288     return (int)val;
00289 }
00290 
00291 
00292 inline int Ceil2Int(float val) {
00293     return (int)ceilf(val);
00294 }
00295 
00296 
00297 #ifdef NDEBUG
00298 #define Assert(expr) ((void)0)
00299 #else
00300 #define Assert(expr) \
00301     ((expr) ? (void)0 : \
00302         Severe("Assertion \"%s\" failed in %s, line %d", \
00303                #expr, __FILE__, __LINE__))
00304 #endif // NDEBUG
00305 inline bool Quadratic(float A, float B, float C, float *t0, float *t1) {
00306     // Find quadratic discriminant
00307     float discrim = B * B - 4.f * A * C;
00308     if (discrim <= 0.) return false;
00309     float rootDiscrim = sqrtf(discrim);
00310 
00311     // Compute quadratic _t_ values
00312     float q;
00313     if (B < 0) q = -.5f * (B - rootDiscrim);
00314     else       q = -.5f * (B + rootDiscrim);
00315     *t0 = q / A;
00316     *t1 = C / q;
00317     if (*t0 > *t1) swap(*t0, *t1);
00318     return true;
00319 }
00320 
00321 
00322 
00323 #endif // PBRT_CORE_PBRT_H