PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/core/sh.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 #pragma once
00034 #endif
00035 
00036 #ifndef PBRT_CORE_SH_H
00037 #define PBRT_CORE_SH_H
00038 
00039 // core/sh.h*
00040 #include "pbrt.h"
00041 #include "geometry.h"
00042 #include "spectrum.h"
00043 
00044 // Spherical Harmonics Declarations
00045 inline int SHTerms(int lmax) {
00046     return (lmax + 1) * (lmax + 1);
00047 }
00048 
00049 
00050 inline int SHIndex(int l, int m) {
00051     return l*l + l + m;
00052 }
00053 
00054 
00055 void SHEvaluate(const Vector &v, int lmax, float *out);
00056 void SHWriteImage(const char *filename, const Spectrum *c, int lmax, int yres);
00057 template <typename Func>
00058 void SHProjectCube(Func func, const Point &p, int res, int lmax,
00059                    Spectrum *coeffs) {
00060     float *Ylm = ALLOCA(float, SHTerms(lmax));
00061     for (int u = 0; u < res; ++u) {
00062         float fu = -1.f + 2.f * (float(u) + 0.5f) / float(res);
00063         for (int v = 0; v < res; ++v) {
00064             float fv = -1.f + 2.f * (float(v) + 0.5f) / float(res);
00065             // Incorporate results from $+z$ face to coefficients
00066             Vector w(fu, fv, 1);
00067             SHEvaluate(Normalize(w), lmax, Ylm);
00068             Spectrum f = func(u, v, p, w);
00069             float dA = 1.f / powf(Dot(w, w), 3.f/2.f);
00070             for (int k = 0; k < SHTerms(lmax); ++k)
00071                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00072 
00073             // Incorporate results from other faces to coefficients
00074             w = Vector(fu, fv, -1);
00075             SHEvaluate(Normalize(w), lmax, Ylm);
00076             f = func(u, v, p, w);
00077             for (int k = 0; k < SHTerms(lmax); ++k)
00078                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00079             w = Vector(fu, 1, fv);
00080             SHEvaluate(Normalize(w), lmax, Ylm);
00081             f = func(u, v, p, w);
00082             for (int k = 0; k < SHTerms(lmax); ++k)
00083                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00084             w = Vector(fu, -1, fv);
00085             SHEvaluate(Normalize(w), lmax, Ylm);
00086             f = func(u, v, p, w);
00087             for (int k = 0; k < SHTerms(lmax); ++k)
00088                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00089             w = Vector(1, fu, fv);
00090             SHEvaluate(Normalize(w), lmax, Ylm);
00091             f = func(u, v, p, w);
00092             for (int k = 0; k < SHTerms(lmax); ++k)
00093                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00094             w = Vector(-1, fu, fv);
00095             SHEvaluate(Normalize(w), lmax, Ylm);
00096             f = func(u, v, p, w);
00097             for (int k = 0; k < SHTerms(lmax); ++k)
00098                 coeffs[k] += f * Ylm[k] * dA * (4.f / (res * res));
00099         }
00100     }
00101 }
00102 
00103 
00104 void SHProjectIncidentDirectRadiance(const Point &p, float pEpsilon, float time,
00105     MemoryArena &arena, const Scene *scene, bool computeLightVisibility,
00106     int lmax, RNG &rng, Spectrum *c_d);
00107 void SHProjectIncidentIndirectRadiance(const Point &p, float pEpsilon,
00108     float time, const Renderer *renderer, Sample *origSample,
00109     const Scene *scene, int lmax, RNG &rng, int nSamples, Spectrum *c_i);
00110 void SHReduceRinging(Spectrum *c, int lmax, float lambda = .005f);
00111 void SHRotate(const Spectrum *c_in, Spectrum *c_out, const Matrix4x4 &m,
00112               int lmax, MemoryArena &arena);
00113 void SHRotateZ(const Spectrum *c_in, Spectrum *c_out, float alpha, int lmax);
00114 void SHRotateXMinus(const Spectrum *c_in, Spectrum *c_out, int lmax);
00115 void SHRotateXPlus(const Spectrum *c_in, Spectrum *c_out, int lmax);
00116 //void SHSwapYZ(const Spectrum *c_in, Spectrum *c_out, int lmax);
00117 void SHConvolveCosTheta(int lmax, const Spectrum *c_in, Spectrum *c_out);
00118 void SHConvolvePhong(int lmax, float n, const Spectrum *c_in, Spectrum *c_out);
00119 void SHComputeDiffuseTransfer(const Point &p, const Normal &n, float rayEpsilon,
00120     const Scene *scene, RNG &rng, int nSamples, int lmax, Spectrum *c_transfer);
00121 void SHComputeTransferMatrix(const Point &p, float rayEpsilon,
00122     const Scene *scene, RNG &rng, int nSamples, int lmax, Spectrum *T);
00123 void SHComputeBSDFMatrix(const Spectrum &Kd, const Spectrum &Ks,
00124     float roughness, RNG &rng, int nSamples, int lmax, Spectrum *B);
00125 void SHMatrixVectorMultiply(const Spectrum *M, const Spectrum *v,
00126                             Spectrum *vout, int lmax);
00127 
00128 #endif // PBRT_CORE_SH_H