farm-ng-core
orthographic.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
10 
11 #include "sophus/calculus/region.h"
14 
15 namespace sophus {
16 
17 template <class TScalar>
18 using OrthographicModelT =
20 
21 /// Returns orthographic camera model given bounding box and image size.
22 template <class TScalar>
24  Region2<TScalar> const& bounding_box, ImageSize image_size)
26  // (-0.5, -0.5) -> (min.x, min.y)
27  // (-0.5, h-0.5) -> (min.x, max.y)
28  // (w-0.5, -0.5) -> (max.x, min.y)
29  // (w-0.5, h-0.5) -> (max.x, max.y)
30  //
31  // Thus we have the following relationship:
32  // u = x*sx + ox
33  // v = y*sy + oy
34  //
35  // -0.5 = min.x * sx + ox => ox = -(0.5 + min.x * sx)
36  // (w-0.5) = max.x * sx + ox
37  //
38  // w = sx * (max.x - min.x)
39  // sx = w / (max.x - min.x)
40 
41  Eigen::Array<TScalar, 2, 1> const range = bounding_box.range();
42  Eigen::Array<TScalar, 2, 1> const scale =
43  Eigen::Array<TScalar, 2, 1>(image_size.width, image_size.height) / range;
44  Eigen::Array<TScalar, 2, 1> const offset =
45  -(0.5 + bounding_box.min().array() * scale);
46 
47  Eigen::Matrix<TScalar, 4, 1> const params(
48  scale.x(), scale.y(), offset.x(), offset.y());
49 
50  return OrthographicModelT<TScalar>(image_size, params);
51 }
52 
53 /// Returns 2d bounding box corresponding the the given orthographic camera
54 /// model.
55 template <class TScalar>
57  -> Region2<TScalar> {
58  Eigen::Vector<TScalar, 2> min = (-ortho_cam.principalPoint().array() - 0.5) /
59  ortho_cam.focalLength().array();
61  min,
62  min.array() + ortho_cam.imageSize().array().template cast<TScalar>() /
63  ortho_cam.focalLength().array());
64 }
65 
66 } // namespace sophus
sophus::CameraModelT
Camera model class template for pinhole-like camera projections.
Definition: camera_model.h:83
sophus::orthoCamFromBoundingBox
auto orthoCamFromBoundingBox(Region2< TScalar > const &bounding_box, ImageSize image_size) -> OrthographicModelT< TScalar >
Returns orthographic camera model given bounding box and image size.
Definition: orthographic.h:23
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::min
auto min(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:104
region.h
sophus::Region::fromMinMax
static auto fromMinMax(TPoint const &min, TPoint const &max) noexcept -> Region< TPoint >
Creates Region from two points, min and max.
Definition: region.h:104
sophus::boundingBoxFromOrthoCam
auto boundingBoxFromOrthoCam(OrthographicModelT< TScalar > const &ortho_cam) -> Region2< TScalar >
Returns 2d bounding box corresponding the the given orthographic camera model.
Definition: orthographic.h:56
camera_model.h
sophus::OrthographicModelT
CameraModelT< TScalar, AffineTransform, ProjectionOrtho > OrthographicModelT
Definition: orthographic.h:19
image_size.h
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::Region
A region is a closed interval [a, b] with a being the lower bound (=min) and b being the upper bound ...
Definition: region.h:19