LOST  0.0.1
LOST: Open-source Star Tracker
camera.cpp
Go to the documentation of this file.
1 #include "camera.hpp"
2 
3 #include <math.h>
4 #include <assert.h>
5 
6 #include "attitude-utils.hpp"
7 #include "decimal.hpp"
8 
9 namespace lost {
10 
15 Vec2 Camera::SpatialToCamera(const Vec3 &vector) const {
16  // can't handle things behind the camera.
17  assert(vector.x > 0);
18  // TODO: is there any sort of accuracy problem when vector.y and vector.z are small?
19 
20  decimal focalFactor = focalLength/vector.x;
21 
22  decimal yPixel = vector.y*focalFactor;
23  decimal zPixel = vector.z*focalFactor;
24 
25  return { -yPixel + xCenter, -zPixel + yCenter };
26 }
27 
35 Vec3 Camera::CameraToSpatial(const Vec2 &vector) const {
36  assert(InSensor(vector));
37 
38  // isn't it interesting: To convert from center-based to left-corner-based coordinates is the
39  // same formula; f(x)=f^{-1}(x) !
40  decimal xPixel = -vector.x + xCenter;
41  decimal yPixel = -vector.y + yCenter;
42 
43  return {
44  1,
45  xPixel / focalLength,
46  yPixel / focalLength,
47  };
48 }
49 
51 bool Camera::InSensor(const Vec2 &vector) const {
52  // if vector.x == xResolution, then it is at the leftmost point of the pixel that's "hanging
53  // off" the edge of the image, so vector is still in the image.
54  return vector.x >= 0 && vector.x <= xResolution
55  && vector.y >= 0 && vector.y <= yResolution;
56 }
57 
59  return xResolution / DECIMAL(2.0) / DECIMAL_TAN(xFov/2);
60 }
61 
62 decimal FocalLengthToFov(decimal focalLength, decimal xResolution, decimal pixelSize) {
63  return DECIMAL_ATAN(xResolution/2 * pixelSize / focalLength) * 2;
64 }
65 
67  return FocalLengthToFov(focalLength, xResolution, 1.0);
68 }
69 
70 }
Vec2 SpatialToCamera(const Vec3 &) const
Converts from a 3D point in space to a 2D point on the camera sensor.
Definition: camera.cpp:15
bool InSensor(const Vec2 &vector) const
Returns whether a given pixel is actually in the camera's field of view.
Definition: camera.cpp:51
decimal Fov() const
Horizontal field of view in radians.
Definition: camera.cpp:66
Vec3 CameraToSpatial(const Vec2 &) const
Gives a point in 3d space that could correspond to the given vector, using the same coordinate system...
Definition: camera.cpp:35
Three dimensional vector with decimaling point components.
double decimal
Definition: decimal.hpp:11
#define DECIMAL_TAN(x)
Definition: decimal.hpp:54
#define DECIMAL(x)
Definition: decimal.hpp:21
#define DECIMAL_ATAN(x)
Definition: decimal.hpp:57
LOST starting point.
decimal FovToFocalLength(decimal xFov, decimal xResolution)
Definition: camera.cpp:58
decimal FocalLengthToFov(decimal focalLength, decimal xResolution, decimal pixelSize)
Definition: camera.cpp:62
A two dimensional vector with decimaling point components.