Skip to content

magnetica-studio/efficient-spherical-harmonic-evaluation

Repository files navigation

Efficient Spherical Harmonic Evaluation with constexpr.

A header only library to compute spherical harmonic function.

The algorithm is taken from the article below.

Peter-Pike Sloan, Efficient Spherical Harmonic Evaluation, Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 2, 84-90, 2013 Available online http://jcgt.org/published/0002/02/06/

The original code included in the article uses meta-programming technique which generates C source code as characters by a program written in C++.

ConstexprSHEval.hpp uses compile-time calculation in modern C++ feature and it can be called directly from other C++ codes without compilation in advance.

Use

#include "ConstexprSHEval.hpp"

namespace cshe =  novonotes::constexprsheval

constexpr int ORDER = 7;
auto res = cshe::SHEvalExec<ORDER>(x,y,z);

A necessary compile option

  • --std=gnu++17 is needed to use constexpr feature.
  • -fconstexpr-depth=-1 -fconstexpr-steps=-1 are also necessary to disable a limit of maximum loop in computation (To compute sqrt on compile-time, Newton-Method is used to approximate).

Limitation

SIMD-compatible version, which is originally contained is not implemented yet.

Folder Structure

  • codegen - contains original implementation using meta-programming
  • generated - contains actual evaluation C codes generated by SHEvalCodeGen
  • test
  • minimaltest.cpp - simple test program for ConstExprSHEval.hpp to inspect assembly or LLVM IR
  • ConstexprSHEvalTest.cpp - test program which compares the functions between Codegen and Constexpr.
  • ConstexprSHEval.hpp - A main library code.

Modification

  • removed unused windows dependency
  • uses std::filesystem to open files
  • added sign flip option to match an axis coordinate which is used in IEM Plugin Suite.
  • added cmake configuration

Build test program

The test programs (and original code generator program) can be compiled with CMake.

mkdir build && cd build
cmake ..
cmake --build . -j

SIMD code generation option in the original code

uncomment the line below in CMakeLists.txt

# set if you want to use SIMD for x86 arch code
# target_compile_definitions(shevalcodegen PRIVATE GENSSE)

License

Copyright (c) [2020] [MAGNETICA studio co. ltd.]