summaryrefslogtreecommitdiff
path: root/includes/dynamic_depth/imaging_model.h
blob: 1175a66da3a112ee53f10708f88bb526d791c8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGING_MODEL_H_  // NOLINT
#define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGING_MODEL_H_  // NOLINT

#include <memory>
#include <unordered_map>

#include "dynamic_depth/dimension.h"
#include "dynamic_depth/element.h"
#include "dynamic_depth/point.h"
#include "xmpmeta/xml/deserializer.h"
#include "xmpmeta/xml/serializer.h"

namespace photos_editing_formats {
namespace dynamic_depth {
struct ImagingModelParams {
  // Required. The order of numbers is (x, y), in pixels.
  Point<double> focal_length;

  // Required. The order of numbers is (width, height), in pixels.
  Dimension image_size;

  // Optional. Set to (0.5, 0.5) if not present.
  // The order of numbers is (x, y).
  Point<double> principal_point;

  // Optional.
  std::vector<float> distortion;  // Distortion parameters.
  double skew;
  double pixel_aspect_ratio;

  ImagingModelParams(const Point<double>& focal_len,
                     const Dimension& image_size)
      : focal_length(focal_len),
        image_size(image_size),
        principal_point(Point<double>(0.5, 0.5)),
        distortion(std::vector<float>()),
        skew(0),
        pixel_aspect_ratio(1) {}

  inline bool operator==(const ImagingModelParams& other) const {
    return focal_length == other.focal_length &&
           image_size == other.image_size &&
           principal_point == other.principal_point &&
           distortion == other.distortion && skew == other.skew &&
           pixel_aspect_ratio == other.pixel_aspect_ratio;
  }

  inline bool operator!=(const ImagingModelParams& other) const {
    return !(*this == other);
  }
};

class ImagingModel : public Element {
 public:
  void GetNamespaces(
      std::unordered_map<string, string>* ns_name_href_map) override;

  bool Serialize(xml::Serializer* serializer) const override;

  // Creates an ImagingModel from the given params.
  static std::unique_ptr<ImagingModel> FromData(
      const ImagingModelParams& params);

  // Returns the deserialized equirect model, null if parsing fails.
  static std::unique_ptr<ImagingModel> FromDeserializer(
      const xml::Deserializer& parent_deserializer);

  // Getters.
  Point<double> GetFocalLength() const;
  Point<double> GetPrincipalPoint() const;
  Dimension GetImageSize() const;
  double GetSkew() const;
  double GetPixelAspectRatio() const;
  const std::vector<float>& GetDistortion() const;
  int GetDistortionCount() const;

  // Disallow copying.
  ImagingModel(const ImagingModel&) = delete;
  void operator=(const ImagingModel&) = delete;

 private:
  ImagingModel(const ImagingModelParams& params);

  ImagingModelParams params_;
};

}  // namespace dynamic_depth
}  // namespace photos_editing_formats

#endif // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_IMAGING_MODEL_H_  // NOLINT