Skip to content

Instantly share code, notes, and snippets.

@dwilliamson
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwilliamson/3a65ab8076f95ce9b374 to your computer and use it in GitHub Desktop.
Save dwilliamson/3a65ab8076f95ce9b374 to your computer and use it in GitHub Desktop.
Generating SH constants from polynomial form in C-style comments
//
// Uses pycgen: https://github.com/Celtoys/pycgen
// Generates code for C, HLSL, CUDA and OpenCL
//
void SH_Y2(float3 n, cmp_out float y[9])
{
/*$pycgen
from math import pi
from math import sqrt
k_2sqrtpi = 2 * sqrt(pi)
k_4sqrtpi = 4 * sqrt(pi)
# Stupid SH Tricks, Appendix A2 Polynomial Forms of SH Basis
y_00 = 1.0 / k_2sqrtpi
y_1_1 = -sqrt(3) / k_2sqrtpi
y_10 = sqrt(3) / k_2sqrtpi
y_11 = -sqrt(3) / k_2sqrtpi
y_2_2 = sqrt(15) / k_2sqrtpi
y_2_1 = -sqrt(15) / k_2sqrtpi
y_20_m = sqrt(5) * 3 / k_4sqrtpi
y_20_a = sqrt(5) / k_4sqrtpi
y_21 = -sqrt(15) / k_2sqrtpi
y_22 = sqrt(15) / k_4sqrtpi
code = """
y[0] = {y_00}f;
y[1] = {y_1_1}f * n.y;
y[2] = {y_10}f * n.z;
y[3] = {y_11}f * n.x;
y[4] = {y_2_2}f * n.x * n.y;
y[5] = {y_2_1}f * n.y * n.z;
y[6] = {y_20_m}f * n.z * nz - {y_20_a}f;
y[7] = {y_21}f * n.x * n.z;
y[8] = {y_22}f * (n.x * n.x - ny.y * n.y);
"""
EmitFmt(code)
*/
//$pycgen-begin
y[0] = 0.282094791774f;
y[1] = -0.488602511903f * n.y;
y[2] = 0.488602511903f * n.z;
y[3] = -0.488602511903f * n.x;
y[4] = 1.09254843059f * n.x * n.y;
y[5] = -1.09254843059f * n.y * n.z;
y[6] = 0.946174695758f * n.z * nz - 0.315391565253f;
y[7] = -1.09254843059f * n.x * n.z;
y[8] = 0.546274215296f * (n.x * n.x - ny.y * n.y);
//$pycgen-end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment