Utilities
Utilities header only library
IntrinsicParams.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 
7 #ifndef UTILS_INTRINSIC_PARAMS_H
8 #define UTILS_INTRINSIC_PARAMS_H
9 
10 #include <string>
11 #include <glm/common.hpp>
12 #include <glm/trigonometric.hpp>
13 
14 #include <Utils/API.h>
15 #include <Utils/SerializableJson.h>
16 
17 namespace utils
18 {
26  struct UTILS_API IntrinsicParams : public utils::SerializableJson
27  {
29  float fx{ 0.0f }, fy{ 0.0f };
31  float cx{ 0.0f }, cy{ 0.0f };
32 
33  IntrinsicParams() = default;
34  IntrinsicParams(float t_fx, float t_fy, float t_cx, float t_cy)
35  : fx(t_fx), fy(t_fy), cx(t_cx), cy(t_cy) {}
36 
38  float GetFovX() const
39  {
40  return 2.0f * glm::atan(cx / fx);
41  }
43  float GetFovY() const
44  {
45  return 2.0f * glm::atan(cy / fy);
46  }
48  virtual void AddToJsonNode(nlohmann::json &node) const override;
51  virtual bool ReadFromJsonNode(const nlohmann::json &node) override;
52  };
53 
55  inline glm::vec2 GetPrincipalPoint(float focal_length, float fovx, float fovy)
56  {
57  return glm::vec2(
58  focal_length * glm::tan(0.5f * fovx),
59  focal_length * glm::tan(0.5f * fovy)
60  );
61  }
62 
64  inline IntrinsicParams GetIntrinsicParamsFromResolution(const glm::uvec2 &resolution, float fovx, float fovy)
65  {
66  const auto cx = 0.5f * static_cast<float>(resolution.x);
67  const auto cy = 0.5f * static_cast<float>(resolution.y);
68  const auto fx = cx / glm::tan(0.5f * fovx);
69  const auto fy = cy / glm::tan(0.5f * fovy);
70  return IntrinsicParams(fx, fy, cx, cy);
71  }
72 
74  inline IntrinsicParams GetIntrinsicParamsFromDepth(float focal_length, float fovx, float fovy)
75  {
76  const auto c = GetPrincipalPoint(focal_length, fovx, fovy);
77  const auto fx = c.x / glm::tan(0.5f * fovx);
78  const auto fy = c.y / glm::tan(0.5f * fovy);
79  return IntrinsicParams(fx, fy, c.x, c.y);
80  }
81 
83  UTILS_API bool operator==(const IntrinsicParams &p1, const IntrinsicParams &p2);
85  UTILS_API bool operator!=(const IntrinsicParams &p1, const IntrinsicParams &p2);
86 
87 }
88 #endif /* !UTILS_INTRINSIC_PARAMS_H */
Holds camera intrinsic projection parameters.
Definition: IntrinsicParams.h:26
float GetFovY() const
Calculates vertical field of view. In radians.
Definition: IntrinsicParams.h:43
IntrinsicParams GetIntrinsicParamsFromDepth(float focal_length, float fovx, float fovy)
Calculates intrinsic projection parameters from projection depth and field of view angles...
Definition: IntrinsicParams.h:74
UTILS_API bool operator==(const IntrinsicParams &p1, const IntrinsicParams &p2)
Compare equality of two parameter structures.
float GetFovX() const
Calculates horizontal field of view. In radians.
Definition: IntrinsicParams.h:38
Abstration of a class that supports serialization to json strings.
Definition: SerializableJson.h:20
glm::vec2 GetPrincipalPoint(float focal_length, float fovx, float fovy)
Returns intrinsic cx, cy parameters for defined projection depth and field of view angles...
Definition: IntrinsicParams.h:55
UTILS_API bool operator!=(const IntrinsicParams &p1, const IntrinsicParams &p2)
Compare unequality of two parameter structures.
General namespace for utilities.
Definition: BulletConversions.h:9
IntrinsicParams GetIntrinsicParamsFromResolution(const glm::uvec2 &resolution, float fovx, float fovy)
Calculates intrinsic projection parameters from resolution and field of view angles.
Definition: IntrinsicParams.h:64