Skip to content

Commit

Permalink
Use our own constants instead of M_PI, M_*, etc. (#1733)
Browse files Browse the repository at this point in the history
These constants are not part of the standard. We instead use our own
constexpr definitions in the filament::math namespace, as part of
the scalar.h include.
  • Loading branch information
romainguy committed Oct 2, 2019
1 parent 7ea7d1d commit a4a1bf0
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 192 deletions.
2 changes: 1 addition & 1 deletion filament/src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ FCamera::FCamera(FEngine& engine, Entity e)
void UTILS_NOINLINE FCamera::setProjection(double fov, double aspect, double near, double far,
Camera::Fov direction) noexcept {
double w, h;
double s = std::tan(fov * (M_PI / 360.0)) * near;
double s = std::tan(fov * (F_PI / 360.0)) * near;
if (direction == Fov::VERTICAL) {
w = s * aspect;
h = s;
Expand Down
39 changes: 19 additions & 20 deletions filament/src/IndirectLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@

#include "FilamentAPI-impl.h"

#include <utils/Panic.h>

#include <backend/DriverEnums.h>
#include <filament/IndirectLight.h>
#include <utils/Log.h>

#include <utils/Panic.h>

#include <math/scalar.h>

Expand Down Expand Up @@ -90,21 +89,21 @@ IndirectLight::Builder& IndirectLight::Builder::radiance(uint8_t bands, float3 c
// To save math in the shader, we pre-multiply our SH coefficient by the A[i] factors.
// Additionally, we include the lambertian diffuse BRDF 1/pi and truncated cos.

constexpr float M_SQRT_PI = 1.7724538509f;
constexpr float M_SQRT_3 = 1.7320508076f;
constexpr float M_SQRT_5 = 2.2360679775f;
constexpr float M_SQRT_15 = 3.8729833462f;
constexpr float C[] = { M_PI, 2.0943951f, 0.785398f }; // <cos>
constexpr float F_SQRT_PI = 1.7724538509f;
constexpr float F_SQRT_3 = 1.7320508076f;
constexpr float F_SQRT_5 = 2.2360679775f;
constexpr float F_SQRT_15 = 3.8729833462f;
constexpr float C[] = { F_PI, 2.0943951f, 0.785398f }; // <cos>
constexpr float A[] = {
1.0f / (2.0f * M_SQRT_PI) * C[0] * M_1_PI, // 0 0
-M_SQRT_3 / (2.0f * M_SQRT_PI) * C[1] * M_1_PI, // 1 -1
M_SQRT_3 / (2.0f * M_SQRT_PI) * C[1] * M_1_PI, // 1 0
-M_SQRT_3 / (2.0f * M_SQRT_PI) * C[1] * M_1_PI, // 1 1
M_SQRT_15 / (2.0f * M_SQRT_PI) * C[2] * M_1_PI, // 2 -2
-M_SQRT_15 / (2.0f * M_SQRT_PI) * C[2] * M_1_PI, // 3 -1
M_SQRT_5 / (4.0f * M_SQRT_PI) * C[2] * M_1_PI, // 3 0
-M_SQRT_15 / (2.0f * M_SQRT_PI) * C[2] * M_1_PI, // 3 1
M_SQRT_15 / (4.0f * M_SQRT_PI) * C[2] * M_1_PI // 3 2
1.0f / (2.0f * F_SQRT_PI) * C[0] * F_1_PI, // 0 0
-F_SQRT_3 / (2.0f * F_SQRT_PI) * C[1] * F_1_PI, // 1 -1
F_SQRT_3 / (2.0f * F_SQRT_PI) * C[1] * F_1_PI, // 1 0
-F_SQRT_3 / (2.0f * F_SQRT_PI) * C[1] * F_1_PI, // 1 1
F_SQRT_15 / (2.0f * F_SQRT_PI) * C[2] * F_1_PI, // 2 -2
-F_SQRT_15 / (2.0f * F_SQRT_PI) * C[2] * F_1_PI, // 3 -1
F_SQRT_5 / (4.0f * F_SQRT_PI) * C[2] * F_1_PI, // 3 0
-F_SQRT_15 / (2.0f * F_SQRT_PI) * C[2] * F_1_PI, // 3 1
F_SQRT_15 / (4.0f * F_SQRT_PI) * C[2] * F_1_PI // 3 2
};

// this is a way to "document" the actual value of these coefficients and at the same
Expand Down Expand Up @@ -251,12 +250,12 @@ float4 FIndirectLight::getColorEstimate(float3 direction) const noexcept {

// The scale factor below is explained in the gamasutra article above, however it seems
// to cause the intensity of the light to be too low.
// constexpr float c = (16.0f * M_PI / 17.0f);
// constexpr float LdSquared = (9.0f / (4.0f * M_PI)) * c * c;
// constexpr float c = (16.0f * F_PI / 17.0f);
// constexpr float LdSquared = (9.0f / (4.0f * F_PI)) * c * c;
// LdDotLe *= c / LdSquared; // Note the final coefficient is 17/36

// We multiply by PI because our SH coefficients contain the 1/PI lambertian BRDF.
LdDotLe *= M_PI;
LdDotLe *= F_PI;

// Make sure we don't have negative intensities
LdDotLe = max(LdDotLe, float3{0});
Expand Down
22 changes: 12 additions & 10 deletions filament/src/components/LightManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

#include "details/Engine.h"

#include <filament/LightManager.h>

#include <math/fast.h>
#include <math/scalar.h>
#include <filament/LightManager.h>

#include <assert.h>

using namespace filament::math;
using namespace utils;
Expand All @@ -43,7 +45,7 @@ struct LightManager::BuilderDetails {
LinearColor mColor = LinearColor{ 1.0f };
float mIntensity = 100000.0f;
float3 mDirection = { 0.0f, -1.0f, 0.0f };
float2 mSpotInnerOuter = { (float)M_PI, (float)M_PI };
float2 mSpotInnerOuter = { (float) F_PI, (float) F_PI };
float mSunAngle = 0.00951f; // 0.545° in radians
float mSunHaloSize = 10.0f;
float mSunHaloFalloff = 80.0f;
Expand Down Expand Up @@ -249,7 +251,7 @@ void FLightManager::setIntensity(Instance i, float intensity) noexcept {

case Type::POINT:
// li = lp / (4*pi)
luminousIntensity = luminousPower * float(M_1_PI) * 0.25f;
luminousIntensity = luminousPower * float(F_1_PI) * 0.25f;
break;

case Type::FOCUSED_SPOT: {
Expand All @@ -258,13 +260,13 @@ void FLightManager::setIntensity(Instance i, float intensity) noexcept {
float2 scaleOffset = spotParams.scaleOffset;
float cosOuter = -scaleOffset.y / scaleOffset.x;
float cosHalfOuter = std::sqrt((1.0f + cosOuter) * 0.5f); // half-angle identities
luminousIntensity = luminousPower / (2.0f * float(M_PI) * (1.0f - cosHalfOuter));
luminousIntensity = luminousPower / (2.0f * float(F_PI) * (1.0f - cosHalfOuter));
spotParams.luminousPower = luminousPower;
break;
}
case Type::SPOT:
// li = lp / pi
luminousIntensity = luminousPower * float(M_1_PI);
luminousIntensity = luminousPower * float(F_1_PI);
break;
}
manager[i].intensity = luminousIntensity;
Expand All @@ -285,8 +287,8 @@ void FLightManager::setSpotLightCone(Instance i, float inner, float outer) noexc
auto& manager = mManager;
if (i && isSpotLight(i)) {
// clamp the inner/outer angles to pi
float outerClamped = std::min(std::abs(outer), float(M_PI));
float innerClamped = std::min(std::abs(inner), float(M_PI));
float outerClamped = std::min(std::abs(outer), float(F_PI));
float innerClamped = std::min(std::abs(inner), float(F_PI));

// inner must always be smaller than outer
innerClamped = std::min(innerClamped, outerClamped);
Expand All @@ -307,7 +309,7 @@ void FLightManager::setSpotLightCone(Instance i, float inner, float outer) noexc
if (type == Type::FOCUSED_SPOT) {
float luminousPower = spotParams.luminousPower;
float cosHalfOuter = std::sqrt((1.0f + cosOuter) * 0.5f); // half-angle identities
float luminousIntensity = luminousPower / (2.0f * float(M_PI) * (1.0f - cosHalfOuter));
float luminousIntensity = luminousPower / (2.0f * float(F_PI) * (1.0f - cosHalfOuter));
manager[i].intensity = luminousIntensity;
}
}
Expand All @@ -316,7 +318,7 @@ void FLightManager::setSpotLightCone(Instance i, float inner, float outer) noexc
void FLightManager::setSunAngularRadius(Instance i, float angularRadius) noexcept {
if (i && isSunLight(i)) {
angularRadius = clamp(angularRadius, 0.25f, 20.0f);
mManager[i].sunAngularRadius = angularRadius * float(M_PI / 180.0);
mManager[i].sunAngularRadius = angularRadius * float(F_PI / 180.0);
}
}

Expand Down Expand Up @@ -411,7 +413,7 @@ void LightManager::setSunAngularRadius(Instance i, float angularRadius) noexcept

float LightManager::getSunAngularRadius(Instance i) const noexcept {
float radius = upcast(this)->getSunAngularRadius(i);
return radius * float(180.0 / M_PI);
return radius * float(180.0 / F_PI);
}

void LightManager::setSunHaloSize(Instance i, float haloSize) noexcept {
Expand Down
34 changes: 17 additions & 17 deletions filament/test/filament_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ TEST(FilamentTest, FroxelData) {
Froxel f = froxelData.getFroxelAt(0,0,0);

// 45-deg plane, with normal pointing outward to the left
EXPECT_FLOAT_EQ(-M_SQRT2/2, f.planes[Froxel::LEFT].x);
EXPECT_FLOAT_EQ(-F_SQRT2/2, f.planes[Froxel::LEFT].x);
EXPECT_FLOAT_EQ( 0, f.planes[Froxel::LEFT].y);
EXPECT_FLOAT_EQ( M_SQRT2/2, f.planes[Froxel::LEFT].z);
EXPECT_FLOAT_EQ( F_SQRT2/2, f.planes[Froxel::LEFT].z);

// the right side of froxel 1 is near 45-deg plane pointing outward to the right
EXPECT_TRUE(f.planes[Froxel::RIGHT].x > 0);
Expand All @@ -500,9 +500,9 @@ TEST(FilamentTest, FroxelData) {

// right side of last horizontal froxel is 45-deg plane pointing outward to the right
Froxel g = froxelData.getFroxelAt(froxelData.getFroxelCountX()-1,0,0);
EXPECT_FLOAT_EQ(M_SQRT2/2, g.planes[Froxel::RIGHT].x);
EXPECT_FLOAT_EQ(F_SQRT2/2, g.planes[Froxel::RIGHT].x);
EXPECT_FLOAT_EQ( 0, g.planes[Froxel::RIGHT].y);
EXPECT_FLOAT_EQ(M_SQRT2/2, g.planes[Froxel::RIGHT].z);
EXPECT_FLOAT_EQ(F_SQRT2/2, g.planes[Froxel::RIGHT].z);

// first froxel near plane facing us
EXPECT_FLOAT_EQ( 0, f.planes[Froxel::NEAR].x);
Expand Down Expand Up @@ -677,21 +677,21 @@ TEST(FilamentTest, Bones) {
Test::check(mat4f::scaling(float3{ 4, -2, 3 }));
Test::check(mat4f::scaling(float3{ 4, 2, -3 }));

Test::check(mat4f::rotation(M_PI_2, float3{ 0, 0, 1 }));
Test::check(mat4f::rotation(M_PI_2, float3{ 0, 1, 0 }));
Test::check(mat4f::rotation(M_PI_2, float3{ 1, 0, 0 }));
Test::check(mat4f::rotation(M_PI_2, float3{ 0, 1, 1 }));
Test::check(mat4f::rotation(M_PI_2, float3{ 1, 0, 1 }));
Test::check(mat4f::rotation(M_PI_2, float3{ 1, 1, 0 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 0, 0, 1 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 0, 1, 0 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 1, 0, 0 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 0, 1, 1 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 1, 0, 1 }));
Test::check(mat4f::rotation(-M_PI_2, float3{ 1, 1, 0 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 0, 0, 1 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 0, 1, 0 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 1, 0, 0 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 0, 1, 1 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 1, 0, 1 }));
Test::check(mat4f::rotation(F_PI_2, float3{ 1, 1, 0 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 0, 0, 1 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 0, 1, 0 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 1, 0, 0 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 0, 1, 1 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 1, 0, 1 }));
Test::check(mat4f::rotation(-F_PI_2, float3{ 1, 1, 0 }));

mat4f m = mat4f::translation(float3{ 1, 2, 3 }) *
mat4f::rotation(-M_PI_2, float3{ 1, 1, 0 }) *
mat4f::rotation(-F_PI_2, float3{ 1, 1, 0 }) *
mat4f::scaling(float3{ -2, 3, 0.04 });

Test::check(m);
Expand Down
41 changes: 20 additions & 21 deletions filament/tools/ssaogen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
* limitations under the License.
*/

#include <math/scalar.h>
#include <math/vec3.h>

#include <random>
#include <iostream>

using namespace filament::math;


static float lerp(float a, float b, float f) {
return a + f * (b - a);
}

int main(int argc, char** argv) {

std::uniform_real_distribution<float> random(0.0, 1.0);
std::default_random_engine generator;

Expand All @@ -44,11 +43,11 @@ int main(int argc, char** argv) {
};
d = normalize(d);
d = d * r * lerp(0.1f, 1.0f, s * s);
if (!(i & 1)) {
if (!(i & 1u)) {
std::cout << " ";
}
std::cout << " vec3(" << d.x << ", " << d.y << ", " << d.z << "),";
if (i & 1) {
if (i & 1u) {
std::cout << std::endl;
}
}
Expand All @@ -65,11 +64,11 @@ int main(int argc, char** argv) {
random(generator) * 2 - 1,
};
d = normalize(d);
if ((i & 0x1) == 0) {
if ((i & 0x1u) == 0) {
std::cout << " ";
}
std::cout << " vec3(" << d.x << ", " << d.y << ", " << d.z << "),";
if ((i & 0x1) == 0x1) {
if ((i & 0x1u) == 0x1) {
std::cout << std::endl;
}
}
Expand Down Expand Up @@ -105,9 +104,9 @@ int main(int argc, char** argv) {
* Unfortunately, because angle depends on radius^2, it's not possible to separate phi and i,
* as the final expression is:
*
* g(phi) = phi^2 * 2.0 * M_PI * kSpiralTurns * K
* + phi * 2.0 * M_PI * (1.0 + kSpiralTurns * K)
* + i * phi * 4.0 * M_PI * kSpiralTurns * K; // K is a constant
* g(phi) = phi^2 * 2.0 * F_PI * kSpiralTurns * K
* + phi * 2.0 * F_PI * (1.0 + kSpiralTurns * K)
* + i * phi * 4.0 * F_PI * kSpiralTurns * K; // K is a constant
*
* g(phi) has a term in "i * phi" which links both expressions.
*
Expand All @@ -122,12 +121,12 @@ int main(int argc, char** argv) {
const float dalpha = 1.0f / (spiralSampleCount - 0.5f);
for (size_t i = 0; i < spiralSampleCount; i++) {
float radius = (i + 0.5f) * dalpha;
float angle = radius * radius * (2 * M_PI * kSpiralTurns);
if ((i & 0x1) == 0) {
float angle = float(radius * radius * (2 * F_PI * kSpiralTurns));
if ((i & 0x1u) == 0) {
std::cout << " ";
}
std::cout << " vec3(" << std::cos(angle) << ", " << std::sin(angle) << ", " << radius << "),";
if ((i & 0x1) == 0x1) {
if ((i & 0x1u) == 0x1) {
std::cout << std::endl;
}
}
Expand All @@ -140,15 +139,15 @@ int main(int argc, char** argv) {
for (size_t i = 0; i < trigNoiseSampleCount; i++) {
float phi = random(generator);
float dr = phi * dalpha;
float dphi = 2.0 * M_PI * kSpiralTurns * phi * phi * dalpha * dalpha
+ phi * 2.0 * M_PI * (1.0 + kSpiralTurns * dalpha * dalpha)
+ phi * 4.0 * M_PI * kSpiralTurns * dalpha * dalpha;
float dphi = float(2.0 * F_PI * kSpiralTurns * phi * phi * dalpha * dalpha
+ phi * 2.0 * F_PI * (1.0 + kSpiralTurns * dalpha * dalpha)
+ phi * 4.0 * F_PI * kSpiralTurns * dalpha * dalpha);
float3 d = { std::cos(dphi), std::sin(dphi), dr };
if ((i & 0x1) == 0) {
if ((i & 0x1u) == 0) {
std::cout << " ";
}
std::cout << " vec3(" << d.x << ", " << d.y << ", " << d.z << "),";
if ((i & 0x1) == 0x1) {
if ((i & 0x1u) == 0x1) {
std::cout << std::endl;
}
}
Expand All @@ -171,15 +170,15 @@ int main(int argc, char** argv) {
// Cut-off frequency definition:
// fc = 1.1774 / (2pi * q) (half power frequency or 0.707 amplitude)

float q = (gaussianWidth + 1) / 6.0; // ~1.667 for 9 taps
float g = (1.0 / (std::sqrt(2.0 * M_PI) * q)) * std::exp(-(x * x) / (2.0 * q * q));
float q = (gaussianWidth + 1.0f) / 6.0f; // ~1.667 for 9 taps
float g = (1.0 / (std::sqrt(2.0 * F_PI) * q)) * std::exp(-(x * x) / (2.0 * q * q));
weightSum += g * (i == 0 ? 1.0f : 2.0f);

if ((i & 0x7) == 0) {
if ((i & 0x7u) == 0) {
std::cout << " ";
}
std::cout << g << ", ";
if ((i & 0x7) == 0x7) {
if ((i & 0x7u) == 0x7) {
std::cout << std::endl;
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/bluevk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include_directories(${PUBLIC_HDR_DIR})

add_library(${TARGET} STATIC ${PUBLIC_HDRS} ${SRCS})

target_link_libraries(${TARGET} utils)
target_link_libraries(${TARGET} utils math)

target_include_directories(${TARGET} PUBLIC ${PUBLIC_HDR_DIR})

Expand Down
9 changes: 7 additions & 2 deletions libs/bluevk/tests/test_bluevk_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
#include "SDL_vulkan.h"

#include <bluevk/BlueVK.h>

#include <utils/Log.h>

#include <math/scalar.h>

using namespace filament::math;

struct VulkanDriver {
VkInstance instance;
VkDevice device;
Expand Down Expand Up @@ -720,8 +725,8 @@ static SDL_bool render() {
}
currentTime = (double) SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
clearColor.float32[0] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime));
clearColor.float32[1] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + M_PI * 2 / 3));
clearColor.float32[2] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + M_PI * 4 / 3));
clearColor.float32[1] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + F_PI * 2 / 3));
clearColor.float32[2] = (float)(0.5 + 0.5 * SDL_sin(SPEED * currentTime + F_PI * 4 / 3));
clearColor.float32[3] = 1;
rerecordCommandBuffer(frameIndex, &clearColor);
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Expand Down
4 changes: 2 additions & 2 deletions libs/filagui/src/ImGuiExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ void ArrowWidget::createArrow() {

float x0, x1, y0, y1, z0, z1, a0, a1, nx, nn;
for (int i = 0; i < SUBDIV; ++i) {
a0 = 2.0f*float(M_PI)*(float(i)) / SUBDIV;
a1 = 2.0f*float(M_PI)*(float(i + 1)) / SUBDIV;
a0 = 2.0f*float(F_PI)*(float(i)) / SUBDIV;
a1 = 2.0f*float(F_PI)*(float(i + 1)) / SUBDIV;
x0 = ARROW_BGN;
x1 = ARROW_END - CONE_LENGTH;
y0 = cosf(a0);
Expand Down
Loading

0 comments on commit a4a1bf0

Please sign in to comment.