Skip to content

Commit

Permalink
VOXEL: added new tests and fixed raycastFaceDetection
Browse files Browse the repository at this point in the history
see issue #500
  • Loading branch information
mgerhardy committed Aug 5, 2024
1 parent 6217cf4 commit 5988113
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/modules/voxel/Face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include "Face.h"
#include "core/Common.h"
#include <glm/ext/scalar_constants.hpp>
#include <math.h>

#include <glm/ext/scalar_constants.hpp>
#include <glm/geometric.hpp>
#include <glm/vec3.hpp>
#include <glm/common.hpp>
Expand Down Expand Up @@ -50,7 +51,7 @@ FaceNames raycastFaceDetection(const glm::vec3& rayOrigin,

float tmin;
float tmax;
const float divx = glm::abs(rayDirection.x) <= glm::epsilon<float>() ? 1.0f : 1.0f / rayDirection.x;
const float divx = glm::abs(rayDirection.x) <= glm::epsilon<float>() ? (rayDirection.x < 0.0f ? -INFINITY : INFINITY) : 1.0f / rayDirection.x;
if (divx >= 0.0f) {
tmin = (mins.x - rayOrigin.x) * divx;
tmax = (maxs.x - rayOrigin.x) * divx;
Expand All @@ -61,7 +62,7 @@ FaceNames raycastFaceDetection(const glm::vec3& rayOrigin,

float tymin;
float tymax;
const float divy = glm::abs(rayDirection.y) <= glm::epsilon<float>() ? 1.0f : 1.0f / rayDirection.y;
const float divy = glm::abs(rayDirection.y) <= glm::epsilon<float>() ? (rayDirection.y < 0.0f ? -INFINITY : INFINITY) : 1.0f / rayDirection.y;
if (divy >= 0.0f) {
tymin = (mins.y - rayOrigin.y) * divy;
tymax = (maxs.y - rayOrigin.y) * divy;
Expand All @@ -84,7 +85,7 @@ FaceNames raycastFaceDetection(const glm::vec3& rayOrigin,

float tzmin;
float tzmax;
const float divz = glm::abs(rayDirection.z) <= glm::epsilon<float>() ? 1.0f : 1.0f / rayDirection.z;
const float divz = glm::abs(rayDirection.z) <= glm::epsilon<float>() ? (rayDirection.z < 0.0f ? -INFINITY : INFINITY) : 1.0f / rayDirection.z;
if (divz >= 0.0f) {
tzmin = (mins.z - rayOrigin.z) * divz;
tzmax = (maxs.z - rayOrigin.z) * divz;
Expand Down
24 changes: 24 additions & 0 deletions src/modules/voxel/tests/FaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ inline ::std::ostream& operator<<(::std::ostream& os, const voxel::FaceBits& fac
class FaceTest: public app::AbstractTest {
};

TEST_F(FaceTest, testNegativeXStraightOffsets) {
glm::vec3 rayOrigin { 0.0f, 0.0f, 0.0f };
glm::vec3 hitPos { 14.0f, 0.0f, 0.0f };
const FaceNames name = raycastFaceDetection(rayOrigin, hitPos, 0.0f, 1.0f);
ASSERT_EQ(FaceNames::NegativeX, name)
<< "Ray did not hit the expected face. Face: " << (int) name;
}

TEST_F(FaceTest, testPositiveXStraight) {
glm::vec3 rayOrigin { 0.0f, 0.0f, 0.0f };
glm::vec3 hitPos { -14.0f, 0.0f, 0.0f };
const FaceNames name = raycastFaceDetection(rayOrigin, hitPos);
ASSERT_EQ(FaceNames::PositiveX, name)
<< "Ray did not hit the expected face. Face: " << (int) name;
}

TEST_F(FaceTest, testNegativeXStraight) {
glm::vec3 rayOrigin { 0.0f, 0.0f, 0.0f };
glm::vec3 hitPos { 14.0f, 0.0f, 0.0f };
const FaceNames name = raycastFaceDetection(rayOrigin, hitPos);
ASSERT_EQ(FaceNames::NegativeX, name)
<< "Ray did not hit the expected face. Face: " << (int) name;
}

TEST_F(FaceTest, testNegativeX) {
glm::vec3 rayOrigin { 0.0f };
glm::vec3 hitPos { 14.0f };
Expand Down

0 comments on commit 5988113

Please sign in to comment.