Last active
November 2, 2022 21:56
-
-
Save ekmett/d08eef0fa0d176fd4a3eb36465d291fe to your computer and use it in GitHub Desktop.
Computing Spherical Harmonics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <limits> | |
#define _USE_MATH_DEFINES | |
#include <math.h> | |
namespace framework { | |
namespace math_constexpr { | |
int constexpr abs(int x) { | |
return x < 0 ? -x : x; | |
} | |
float constexpr abs(float x) { | |
return x < 0 ? -x : x; | |
} | |
template <typename T> T constexpr cube(T x) { | |
return x*x*x; | |
} | |
namespace detail { | |
static constexpr float epsilon = 0.00001f; | |
// newton raphson | |
float constexpr sqrt_step(float x, float c, float p) { | |
return abs(c - p) < epsilon ? c : sqrt_step(x, 0.5f * (c + x / c), c); | |
} | |
// triple angle formula | |
float constexpr sin_step(float x) { | |
return x < epsilon ? x : 3 * sin_step(x / 3.f) - 4 * cube(sin_step(x / 3.f)); | |
} | |
} | |
float constexpr pow(float b, int e) { | |
return e < 0 ? 1.f / pow(b, -e) | |
: e == 0 ? 1.f | |
: b * pow(b, e - 1); | |
} | |
float constexpr sqrt(float x) { | |
return x >= 0 && x < std::numeric_limits<float>::infinity() ? detail::sqrt_step(x, x, 0) : std::numeric_limits<float>::quiet_NaN(); | |
} | |
float constexpr sin(float x) { | |
return detail::sin_step(x < 0 ? float(M_PI) - x : x); | |
} | |
float constexpr cos(float x) { | |
return sin(float(M_PI_2) - x); | |
} | |
int constexpr factorial_power(int x, int n, int h = 1) { | |
return (n > 0) ? x * factorial_power(x - h, n - 1, h) : 1; | |
} | |
int constexpr factorial(int x, int h = 1) { | |
return (x > 1) ? x * factorial(x - h, h) : 1; | |
} | |
float constexpr K(int l, int m) { | |
return sqrt((2 * l + 1) / (4 * float(M_PI) * factorial_power(l + abs(m), abs(m) + abs(m)))); | |
} | |
float constexpr legendre(int l, int m, float x) { | |
return l == m + 1 ? x * (2 * m + 1) * legendre(m, m, x) | |
: l == m ? pow(-1, m) * factorial(2 * m - 1, 2) * pow(1 - x*x, m / 2) | |
: (x * (2 * l - 1) * legendre(l - 1, m, x) - (l + m - 1) * legendre(l - 2, m, x)) / (l - m); | |
} | |
float constexpr spherical_harmonic(int l, int m, float theta, float phi) { | |
return m > 0 ? sqrt(2) * K(l, m) * cos(m*phi) * legendre(l, m, cos(theta)) | |
: m < 0 ? sqrt(2) * K(l, m) * sin(-m*phi) * legendre(l, -m, cos(theta)) | |
: K(l, m) * legendre(l, 0, cos(theta)); | |
} | |
} | |
} | |
/* | |
Copyright (c) 2016 Edward Kmett | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions | |
are met: | |
1. Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment