SMACC2
Loading...
Searching...
No Matches
geometry.cpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <cl_rgb/geometry.hpp>
16
17namespace cl_rgb
18{
19
20cv::Vec3d PinholeCamera::rayDirection(double u, double v) const
21{
22 return rotation * cv::Vec3d((u - cx) / fx, (v - cy) / fy, 1.0);
23}
24
25std::optional<cv::Point2d> PinholeCamera::project(const cv::Vec3d & point) const
26{
27 const cv::Vec3d p = rotation.t() * (point - position);
28 if (p[2] <= 1e-9)
29 {
30 return std::nullopt;
31 }
32 return cv::Point2d(fx * p[0] / p[2] + cx, fy * p[1] / p[2] + cy);
33}
34
35std::optional<cv::Vec3d> PinholeCamera::pixelToPlane(double u, double v, double z) const
36{
37 const cv::Vec3d d = rayDirection(u, v);
38 if (std::abs(d[2]) < 1e-12)
39 {
40 return std::nullopt;
41 }
42 const double s = (z - position[2]) / d[2];
43 if (s <= 0.0)
44 {
45 return std::nullopt;
46 }
47 return position + s * d;
48}
49
50cv::Matx33d PinholeCamera::imageToPlaneHomography(double z) const
51{
52 // With m = R K^-1 (u, v, 1), the plane point is x = ox + (z - oz) m1 / m3 and the same for y.
53 const cv::Matx33d inverseK(1.0 / fx, 0.0, -cx / fx, 0.0, 1.0 / fy, -cy / fy, 0.0, 0.0, 1.0);
54 const double h = z - position[2];
55 const cv::Matx33d toPlane(h, 0.0, position[0], 0.0, h, position[1], 0.0, 0.0, 1.0);
56 return toPlane * rotation * inverseK;
57}
58
59} // namespace cl_rgb
cv::Matx33d rotation
Definition geometry.hpp:32
std::optional< cv::Vec3d > pixelToPlane(double u, double v, double z) const
Definition geometry.cpp:35
cv::Matx33d imageToPlaneHomography(double z) const
Definition geometry.cpp:50
cv::Vec3d rayDirection(double u, double v) const
Definition geometry.cpp:20
std::optional< cv::Point2d > project(const cv::Vec3d &point) const
Definition geometry.cpp:25