Skip to content

Commit

Permalink
rewrited setupCamera code with new setFocalLength
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Oct 29, 2023
1 parent 690fcad commit be9c775
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
15 changes: 11 additions & 4 deletions WebARKit/WebARKitCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ bool WebARKitCamera::setupCamera(int width, int height) {
}
xsize = width;
ysize = height;
// simple routine to calculate focal length from diagonal field of view, and convert to camera matrix.
diagonal_image_size = std::pow(std::pow(xsize, 2.0) + std::pow(ysize, 2.0), 0.5);
diagonal_fov_radians = diagonal_fov_degrees * m_pi / 180.0;
focal_length = 0.5 * diagonal_image_size / std::tan(0.5 * diagonal_fov_radians);

setFocalLength(xsize, ysize);

cmat.at(0) = focal_length;
cmat.at(2) = 0.5 * xsize;
Expand All @@ -42,4 +40,13 @@ std::array<double, 9> WebARKitCamera::getCameraData() const {
std::array<double, 6> WebARKitCamera::getDistortionCoefficients() const {
return kc;
}

void WebARKitCamera::setFocalLength(int width, int height) {
double diagonal_image_size;
double diagonal_fov_radians;
// simple routine to calculate focal length from diagonal field of view, and convert to camera matrix.
diagonal_image_size = std::pow(std::pow(width, 2.0) + std::pow(height, 2.0), 0.5);
diagonal_fov_radians = diagonal_fov_degrees * m_pi / 180.0;
focal_length = 0.5 * diagonal_image_size / std::tan(0.5 * diagonal_fov_radians);
}
} // namespace webarkit
6 changes: 4 additions & 2 deletions WebARKit/include/WebARKitCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class WebARKitCamera {

std::array<double, 6> getDistortionCoefficients() const;

double getFocalLength() const { return focal_length; }

private:
int xsize, ysize;
std::array<double, 9> cmat;
std::array<double, 6> kc;
double focal_length;
double diagonal_image_size;
double diagonal_fov_degrees;
double diagonal_fov_radians;

void setFocalLength(int width, int height);
};
} // namespace webarkit

Expand Down
1 change: 1 addition & 0 deletions tests/webarkit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ TEST(WebARKitCameraTest, TestCamera) {
EXPECT_EQ(camera_mat[4], 571.25920269684582);
EXPECT_EQ(camera_mat[5], 240.0);
EXPECT_EQ(camera_mat[8], 1.0);
EXPECT_EQ(camera.getFocalLength(), 571.25920269684582);
camera.printSettings();
}

Expand Down

0 comments on commit be9c775

Please sign in to comment.