Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Created November 30, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyhorsman/3dc73e57d8bcb60d739fe33313b917e3 to your computer and use it in GitHub Desktop.
Save emilyhorsman/3dc73e57d8bcb60d739fe33313b917e3 to your computer and use it in GitHub Desktop.
TEST_CASE("Refraction straight through center from outside") {
Vec3f rayDirection({ 0, 0, -1 });
Vec3f normal({ 0, 0, 1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.2f, isTotalInternalReflection);
REQUIRE(refraction[0] == 0);
REQUIRE(refraction[1] == 0);
REQUIRE(refraction[2] == -1);
REQUIRE(!isTotalInternalReflection);
}
TEST_CASE("Refraction straight through center from inside") {
Vec3f rayDirection({ 0, 0, -1 });
Vec3f normal({ 0, 0, -1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.2f, isTotalInternalReflection);
REQUIRE(refraction[0] == 0);
REQUIRE(refraction[1] == 0);
REQUIRE(refraction[2] == -1);
REQUIRE(!isTotalInternalReflection);
}
TEST_CASE("Refraction at 45 degrees through glass slab") {
Vec3f rayDirection = normalize(Vec3f({ 0, -sinf(M_PI / 4.0f), -cosf(M_PI / 4.0f) }));
Vec3f normal({ 0, 0, 1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
REQUIRE(refraction[0] == 0);
REQUIRE(refraction[1] == Approx(-sinf(M_PI / 6.0f)).margin(0.03f));
REQUIRE(refraction[2] == Approx(-cosf(M_PI / 6.0f)).margin(0.03f));
REQUIRE(!isTotalInternalReflection);
refraction = refractionDir(refraction, multiply(normal, -1), 1.5f, isTotalInternalReflection);
REQUIRE(refraction[0] == Approx(rayDirection[0]));
REQUIRE(refraction[1] == Approx(rayDirection[1]));
REQUIRE(refraction[2] == Approx(rayDirection[2]));
REQUIRE(!isTotalInternalReflection);
}
TEST_CASE("Refraction at 75 degrees from normal through glass slab") {
Vec3f rayDirection = normalize(Vec3f({ 0, -sinf(5.0f * M_PI / 12.0f), -cosf(5.0f * M_PI / 12.0f) }));
Vec3f normal({ 0, 0, 1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
REQUIRE(refraction[0] == 0);
REQUIRE(refraction[1] == Approx(-sinf(40.0f * M_PI / 180.0f)).margin(0.03f));
REQUIRE(refraction[2] == Approx(-cosf(40.0f * M_PI / 180.0f)).margin(0.03f));
REQUIRE(!isTotalInternalReflection);
refraction = refractionDir(refraction, multiply(normal, -1), 1.5f, isTotalInternalReflection);
REQUIRE(refraction[0] == Approx(rayDirection[0]));
REQUIRE(refraction[1] == Approx(rayDirection[1]));
REQUIRE(refraction[2] == Approx(rayDirection[2]));
REQUIRE(!isTotalInternalReflection);
}
TEST_CASE("Refraction at 45 degrees from normal below glass slab") {
Vec3f rayDirection = normalize(Vec3f({ 0, sinf(M_PI / 4.0f), -cosf(M_PI / 4.0f) }));
printf("ray direction: %f %f %f\n", REST(rayDirection));
Vec3f normal({ 0, 0, 1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
printf("RESULT: %f %f %f %d %f\n", REST(refraction), isTotalInternalReflection, norm(refraction));
REQUIRE(refraction[0] == 0);
REQUIRE(refraction[1] == Approx(sinf(M_PI / 6.0f)).margin(0.03f));
REQUIRE(refraction[2] == Approx(-cosf(M_PI / 6.0f)).margin(0.03f));
REQUIRE(!isTotalInternalReflection);
refraction = refractionDir(refraction, multiply(normal, -1), 1.5f, isTotalInternalReflection);
REQUIRE(refraction[0] == Approx(rayDirection[0]));
REQUIRE(refraction[1] == Approx(rayDirection[1]));
REQUIRE(refraction[2] == Approx(rayDirection[2]));
REQUIRE(!isTotalInternalReflection);
}
TEST_CASE("Total internal reflection") {
Vec3f rayDirection = normalize(Vec3f({ 0, sinf(M_PI / 4.0f), -cosf(M_PI / 4.0f) }));
Vec3f normal({ 0, 0, -1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
REQUIRE(isTotalInternalReflection);
rayDirection = normalize(Vec3f({ 0, sinf(M_PI / 3.0f), -cosf(M_PI / 3.0f) }));
isTotalInternalReflection = false;
refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
REQUIRE(isTotalInternalReflection);
}
TEST_CASE("Just before total internal reflection") {
Vec3f rayDirection = normalize(Vec3f({ 0, sinf(M_PI / 4.5f), -cosf(M_PI / 4.5f) }));
Vec3f normal({ 0, 0, -1 });
bool isTotalInternalReflection;
Vec3f refraction = refractionDir(rayDirection, normal, 1.5f, isTotalInternalReflection);
REQUIRE(!isTotalInternalReflection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment