Skip to content

Commit

Permalink
new cameraProjectionMatrix with gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Nov 6, 2023
1 parent d01a0a1 commit 0d74031
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions WebARKit/WebARKitGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@ void arglCameraViewRHf(cv::Mat para, std::array<double, 16>& m_modelview, const
m_modelview[14] *= scale;
}
}

void cameraProjectionMatrix(const std::array<double, 9>& calibration, double nearPlane, double farPlane, int screenWidth, int screenHeight, std::array<double, 16>& projectionMatrix)
{
// Camera parameters
double f_x = calibration.at(0); // Focal length in x axis
double f_y = calibration.at(4); // Focal length in y axis (usually the same?)
double c_x = calibration.at(2); // Camera primary point x
double c_y = calibration.at(5); // Camera primary point y

projectionMatrix[0] = -2.0f * f_x / screenWidth;
projectionMatrix[1] = 0.0f;
projectionMatrix[2] = 0.0f;
projectionMatrix[3] = 0.0f;

projectionMatrix[4] = 0.0f;
projectionMatrix[5] = 2.0f * f_y / screenHeight;
projectionMatrix[6] = 0.0f;
projectionMatrix[7] = 0.0f;

projectionMatrix[8] = 2.0f * c_x / screenWidth - 1.0f;
projectionMatrix[9] = 2.0f * c_y / screenHeight - 1.0f;
projectionMatrix[10] = -( farPlane + nearPlane) / ( farPlane - nearPlane );
projectionMatrix[11] = -1.0f;

projectionMatrix[12] = 0.0f;
projectionMatrix[13] = 0.0f;
projectionMatrix[14] = -2.0f * farPlane * nearPlane / ( farPlane - nearPlane );
projectionMatrix[15] = 0.0f;
}
} // namespace webarkit
2 changes: 2 additions & 0 deletions WebARKit/include/WebARKitGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace webarkit {
void arglCameraViewRHf(float para[3][4], float m_modelview[16], const float scale);

void arglCameraViewRHf(cv::Mat para, std::array<double, 16>& m_modelview, const double scale);

void cameraProjectionMatrix(const std::array<double, 9>& calibration, double nearPlane, double farPlane, int screenWidth, int screenHeight, std::array<double, 16>& projectionMatrix);
} // namespace webarkit

#endif // WEBARKITGL_H
19 changes: 19 additions & 0 deletions tests/webarkit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <WebARKitManager.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitEnums.h>
#include <WebARKitCamera.h>
//#include <WebARKitGL.h>
#include <opencv2/imgcodecs.hpp>

class WebARKitEnumTest : public testing::TestWithParam<std::tuple<webarkit::TRACKER_TYPE, webarkit::ColorSpace>> {};
Expand Down Expand Up @@ -82,6 +83,24 @@ TEST(WebARKitCameraTest, TestCamera) {
camera.printSettings();
}

TEST(WebARKitGLTest, TestCameraProjectionMatrix) {
int width = 640;
int height = 480;
webarkit::WebARKitCamera camera;
camera.setupCamera(width, height);
std::array<double, 9> camera_mat = camera.getCameraData();
std::array<double, 16> projectionMatrix = {0.0};
webarkit::cameraProjectionMatrix(camera_mat, 0.01, 100.0, width, height, projectionMatrix);
/*for(auto i : projectionMatrix) {
std::cout << "proj Matrix: " << (double)i << std::endl;
}*/
EXPECT_EQ(projectionMatrix[0], -1.7851850084276433);
EXPECT_EQ(projectionMatrix[5], 2.3802466779035241);
EXPECT_EQ(projectionMatrix[10], -1.0002000200020003);
EXPECT_EQ(projectionMatrix[11], -1.0);
EXPECT_EQ(projectionMatrix[14], -0.020002000200020003);
}

// Check WebARKitManager initialisation.
TEST(WebARKitTest, InitialiseBaseAkazeTest) {
// Create a WebARKitManager object
Expand Down

0 comments on commit 0d74031

Please sign in to comment.