00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef LUX_LUX_H
00024 #define LUX_LUX_H
00025
00026
00027
00028 #include <cmath>
00029 #ifdef __CYGWIN__
00030 #include <ieeefp.h>
00031 #endif
00032
00033 #define BOOST_NO_INTRINSIC_INT64_T
00034 #include <boost/archive/text_oarchive.hpp>
00035 #include <boost/archive/text_iarchive.hpp>
00036 #include <boost/serialization/vector.hpp>
00037 #include <boost/serialization/string.hpp>
00038 #include <boost/serialization/split_member.hpp>
00039
00040 #if defined (__INTEL_COMPILER) && !defined(WIN32)
00041
00042 inline float expf(float a) { return exp(a); }
00043 #endif
00044
00045 #if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
00046 # include <malloc.h>
00047 # if !defined(WIN32) || defined(__CYGWIN__)
00048 # include <alloca.h>
00049 # endif
00050 #endif
00051 #if defined(__FreeBSD__)
00052 # define memalign(A,B) malloc(B)
00053 #endif
00054 #if defined(WIN32) && !defined(__CYGWIN__)
00055 # include <float.h>
00056 # pragma warning (disable: 4244) // conversion from double to float (VS2005) - Radiance
00057 # pragma warning (disable: 4305) // truncation from double to float (VS2005) - Radiance
00058 # pragma warning (disable: 4996) // deprecated functions (VS2005) - Radiance
00059 # pragma warning (disable: 4267) // conversion from 'size_t' [asio\detail\socket_ops.hpp; boost\serialization\collections_save_imp.hpp] - zcott
00060 # pragma warning (disable: 4311) // pointer truncation from 'void *' to 'long' [Fl_Widget.H; Fl_Menu_Item.H;; asio\detail\win_iocp_socket_service.hpp] - zcott
00061 # pragma warning(disable : 4312) // conversion from 'long' to 'void *' of greater size [Fl_Widget.H; Fl_Menu_Item.H; asio\detail\win_iocp_socket_service.hpp] - zcott
00062
00063 # pragma warning (disable: 4267 4251 4065 4102)
00064 # pragma warning (disable: 4190) // extern "C" nonsense when returning a template
00065
00066 # include <windows.h>
00067 #endif
00068 #include <stdlib.h>
00069 #define _GNU_SOURCE 1 //NOBOOK
00070 #include <stdio.h>
00071 #include <string.h>
00072 #include <string>
00073 using std::string;
00074 #include <vector>
00075 using std::vector;
00076 #include <iostream>
00077 using std::ostream;
00078 #include <algorithm>
00079 using std::min;
00080 using std::max;
00081 using std::swap;
00082 using std::sort;
00083 #include <assert.h>
00084
00085
00086 #if defined(WIN32) && !defined(__CYGWIN__)
00087 # define memalign(a,b) _aligned_malloc(b, a)
00088 # define alloca _alloca
00089 # define isnan _isnan
00090 # define isinf(f) (!_finite((f)))
00091 #elif defined(__APPLE__)
00092 # define memalign(a,b) valloc(b)
00093 # define powf pow
00094 # define sinf sin
00095 # define cosf cos
00096 # define tanf tan
00097 # define asinf asin
00098 # define acosf acos
00099 # define atanf atan
00100 # define atan2f atan2
00101 # define logf log
00102 # define log10f log10
00103
00104
00105 # define sqrtf sqrt
00106 # if (__GNUC__ == 3) || (__GNUC__ == 4)
00107 extern "C" {
00108 int isinf(double);
00109 int isnan(double);
00110 }
00111 # endif // ONLY GCC 3
00112 #elif defined(__OpenBSD__)
00113 # define memalign(a,b) malloc(b)
00114 #elif defined(sgi)
00115 # define for if (0) ; else for
00116 #endif
00117
00118 #include "randomgen.h"
00119
00120
00121 typedef double StatsCounterType;
00122 typedef unsigned char u_char;
00123 typedef unsigned short u_short;
00124 typedef unsigned int u_int;
00125 typedef unsigned long u_long;
00126 #define BC_GRID_SIZE 40
00127 typedef vector<int> SampleGrid[BC_GRID_SIZE][BC_GRID_SIZE];
00128 #define GRID(v) (int((v) * BC_GRID_SIZE))
00129
00130
00131 class Timer;
00132 class MemoryArena;
00133 template<class T, int logBlockSize = 2> class BlockedArray;
00134 class ToneMap;
00135 class ProgressReporter;
00136 class StatsCounter;
00137 class StatsRatio;
00138 class StatsPercentage;
00139
00140 namespace lux
00141 {
00142 class Matrix4x4;
00143 class ParamSet;
00144 template <class T> struct ParamSetItem;
00145 class Vector;
00146 class Point;
00147 class Normal;
00148 class Ray;
00149 class RayDifferential;
00150 class BBox;
00151 class Transform;
00152 class DifferentialGeometry;
00153 class TextureParams;
00154 class Shape;
00155 class Scene;
00156 class Primitive;
00157 class Intersection;
00158 class GeometricPrimitive;
00159 class Spectrum;
00160 class ImageData;
00161 class SWCSpectrum;
00162 class Color;
00163 class RGBColor;
00164 class XYZColor;
00165 class SPD;
00166 class Camera;
00167 class ProjectiveCamera;
00168 class Sampler;
00169 class IntegrationSampler;
00170 class Sample;
00171 class Filter;
00172 class Film;
00173 class BxDF;
00174 class BRDF;
00175 class BTDF;
00176 class Fresnel;
00177 class FresnelConductor;
00178 class FresnelDielectric;
00179 class FresnelNoOp;
00180 class SpecularReflection;
00181 class SpecularTransmission;
00182 class Lambertian;
00183 class OrenNayar;
00184 class Microfacet;
00185 class MicrofacetDistribution;
00186 class BSDF;
00187 class Material;
00188 class TextureMapping2D;
00189 class UVMapping2D;
00190 class SphericalMapping2D;
00191 class CylindricalMapping2D;
00192 class PlanarMapping2D;
00193 class TextureMapping3D;
00194 class IdentityMapping3D;
00195 class TriangleMesh;
00196 class PlyMesh;
00197 template <class T> class Texture;
00198 class VolumeRegion;
00199 class Light;
00200 struct VisibilityTester;
00201 class AreaLight;
00202 class ShapeSet;
00203 class Integrator;
00204 class SurfaceIntegrator;
00205 class VolumeIntegrator;
00206 }
00207
00208
00209 #ifdef M_PI
00210 # undef M_PI
00211 #endif
00212 #define M_PI 3.14159265358979323846f
00213 #define INV_PI 0.31830988618379067154f
00214 #define INV_TWOPI 0.15915494309189533577f
00215 #ifndef INFINITY
00216 # define INFINITY HUGE_VAL
00217
00218 #endif
00219 #define LUX_VERSION 0.5
00220 #define LUX_VERSION_STRING "0.5 (CVS)"
00221 #define RAY_EPSILON 1e-3f
00222 #define COLOR_SAMPLES 3
00223 #if defined(WIN32) && !defined(__CYGWIN__)
00224 # define LUX_PATH_SEP ";"
00225 #else
00226 # define LUX_PATH_SEP ":"
00227 #endif
00228
00229
00230 extern "C" void luxInit();
00231 extern "C" void luxCleanup();
00232 bool ParseFile(const char *filename);
00233 namespace lux
00234 {
00235
00236 unsigned int DJBHash(const std::string& str);
00237
00238 bool SolveLinearSystem2x2(const float A[2][2], const float B[2], float x[2]);
00239
00240 ImageData *ReadImage(const string &name);
00241 void WriteRGBAImage(const string &name,
00242 float *pixels, float *alpha, int XRes, int YRes,
00243 int totalXRes, int totalYRes, int xOffset, int yOffset);
00244 void WriteRGBAImageFloat(const string &name,
00245 float *pixels, float *alpha, int XRes, int YRes,
00246 int totalXRes, int totalYRes, int xOffset, int yOffset);
00247 void WriteIgiImage(const string &name,
00248 float *pixels, float *alpha, int XRes, int YRes,
00249 int totalXRes, int totalYRes, int xOffset, int yOffset);
00250 }
00251
00252
00253
00254
00255 #include <boost/shared_ptr.hpp>
00256 #include <boost/enable_shared_from_this.hpp>
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 template<class T> inline T Lerp(float t, T v1, T v2) {
00327 return (1.f - t) * v1 + t * v2;
00328 }
00329 template<class T> inline T Clamp(T val, T low, T high) {
00330 return val > low ? (val < high ? val : high) : low;
00331 }
00332 inline int Mod(int a, int b) {
00333
00334 if(a==0) a=1;
00335 if(b==0) b=1;
00336
00337 int n = int(a/b);
00338 a -= n*b;
00339 if (a < 0)
00340 a += b;
00341 return a;
00342 }
00343 inline float Radians(float deg) {
00344 return ((float)M_PI/180.f) * deg;
00345 }
00346 inline float Degrees(float rad) {
00347 return (180.f/(float)M_PI) * rad;
00348 }
00349 inline float Log2(float x) {
00350 return logf(x) / logf(2.f);
00351 }
00352 inline int Log2Int(float v) {
00353 #if 0
00354 return ((*reinterpret_cast<int *>(&v)) >> 23) - 127;
00355 #else
00356 #define _doublemagicroundeps (.5-1.4e-11)
00357 return int(Log2(v) + _doublemagicroundeps);
00358 #endif
00359 }
00360 inline bool IsPowerOf2(int v) {
00361 return (v & (v - 1)) == 0;
00362 }
00363 inline u_int RoundUpPow2(u_int v) {
00364 v--;
00365 v |= v >> 1;
00366 v |= v >> 2;
00367 v |= v >> 4;
00368 v |= v >> 8;
00369 v |= v >> 16;
00370 return v+1;
00371 }
00372 #if (defined(__linux__) && defined(__i386__)) || defined(WIN32)
00373
00374 #endif
00375 #define _doublemagicroundeps (.5-1.4e-11)
00376
00377 inline int Round2Int(double val) {
00378 #ifdef FAST_INT
00379 #define _doublemagic double (6755399441055744.0)
00380
00381 val = val + _doublemagic;
00382 return (reinterpret_cast<long*>(&val))[0];
00383 #else
00384 return int (val+_doublemagicroundeps);
00385 #endif
00386 }
00387 template<class T> inline int Float2Int(T val) {
00388 #ifdef FAST_INT
00389 return (val<0) ? Round2Int(val+_doublemagicroundeps) :
00390 Round2Int(val-_doublemagicroundeps);
00391 #else
00392 return (int)val;
00393 #endif
00394 }
00395 template<class T> inline int Floor2Int(T val) {
00396 #ifdef FAST_INT
00397 return Round2Int(val - _doublemagicroundeps);
00398 #else
00399 return (int)floor(val);
00400 #endif
00401 }
00402 template<class T> inline int Ceil2Int(T val) {
00403 #ifdef FAST_INT
00404 return Round2Int(val + _doublemagicroundeps);
00405 #else
00406 return (int)ceil(val);
00407 #endif
00408 }
00409 inline bool Quadratic(float A, float B, float C, float *t0,
00410 float *t1) {
00411
00412 float discrim = B * B - 4.f * A * C;
00413 if (discrim < 0.) return false;
00414 float rootDiscrim = sqrtf(discrim);
00415
00416 float q;
00417 if (B < 0) q = -.5f * (B - rootDiscrim);
00418 else q = -.5f * (B + rootDiscrim);
00419 *t0 = q / A;
00420 *t1 = C / q;
00421 if (*t0 > *t1) swap(*t0, *t1);
00422 return true;
00423 }
00424 inline float SmoothStep(float min, float max, float value) {
00425 float v = Clamp((value - min) / (max - min), 0.f, 1.f);
00426 return v * v * (-2.f * v + 3.f);
00427 }
00428
00429 #endif // LUX_LUX_H