diff --git a/openVulkanoCpp/Math/CameraIntrinsic.hpp b/openVulkanoCpp/Math/CameraIntrinsic.hpp new file mode 100644 index 0000000..17a730d --- /dev/null +++ b/openVulkanoCpp/Math/CameraIntrinsic.hpp @@ -0,0 +1,101 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Math.hpp" + +namespace openVulkanoCpp::Math +{ + class CameraIntrinsic + { + public: + const Math::Matrix3f cameraMatrix; + + CameraIntrinsic(const Math::Matrix3f& camMat) : cameraMatrix(camMat) + {} + + CameraIntrinsic(float fx, float fy, float cx, float cy, float skew = 0) + : cameraMatrix(fx, 0, 0, skew, fy, 0, cx, cy, 0) + {} + + [[nodiscard]] CameraIntrinsic Scale(float scale) const + { + Math::Matrix3f scaled = cameraMatrix; + scaled[0][0] *= scale; + scaled[2][0] *= scale; + scaled[1][1] *= scale; + scaled[2][1] *= scale; + return { scaled }; + } + + [[nodiscard]] CameraIntrinsic Scale(Math::Vector2f scale) const + { + Math::Matrix3f scaled = cameraMatrix; + scaled[0][0] *= scale.x; + scaled[2][0] *= scale.x; + scaled[1][1] *= scale.y; + scaled[2][1] *= scale.y; + scaled[1][0] *= scale.x / scale.y; // Scale skew + return { scaled }; + } + + [[nodiscard]] float Fx() const + { + return cameraMatrix[0][0]; + } + + [[nodiscard]] float Fy() const + { + return cameraMatrix[1][1]; + } + + [[nodiscard]] float Cx() const + { + return cameraMatrix[2][0]; + } + + [[nodiscard]] float Cy() const + { + return cameraMatrix[2][1]; + } + + [[nodiscard]] float Skew() const + { + return cameraMatrix[1][0]; + } + + [[nodiscard]] float PixelAspectRatio() const + { + return Fy() / Fx(); + } + + [[nodiscard]] Math::Vector3f ProjectTo3D(Math::Vector2i pixelCoordinates, float depth) const + { + return ProjectTo3D(Math::Vector2f(pixelCoordinates), depth); + } + + [[nodiscard]] Math::Vector3f ProjectTo3D(Math::Vector2f pixelCoordinates, float depth) const + { + // TODO handle skew + return { + (pixelCoordinates.x - Cx()) * depth / Fx(), + (pixelCoordinates.y - Cy()) * depth / Fy(), + depth + }; + } + + CameraIntrinsic operator * (const float scale) const + { + return Scale(scale); + } + + CameraIntrinsic operator * (const Math::Vector2f& scale) const + { + return Scale(scale); + } + }; +}