summaryrefslogtreecommitdiff
path: root/internal/dynamic_depth/point_cloud.cc
blob: f35e9c1b441261df1081ef2774113c449f9d171a (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "dynamic_depth/point_cloud.h"

#include "android-base/logging.h"
#include "dynamic_depth/const.h"
#include "strings/numbers.h"
#include "xmpmeta/base64.h"
#include "xmpmeta/xml/utils.h"

using photos_editing_formats::xml::Deserializer;
using photos_editing_formats::xml::Serializer;

namespace photos_editing_formats {
namespace dynamic_depth {
namespace {

const char kPropertyPrefix[] = "PointCloud";
const char kPointCount[] = "PointCount";
const char kPoints[] = "Points";
const char kMetric[] = "Metric";

const char kNamespaceHref[] = "http://ns.google.com/photos/dd/1.0/pointcloud/";

}  // namespace

// Private constructor.
PointCloud::PointCloud() : metric_(false) {}

// Public methods.
void PointCloud::GetNamespaces(
    std::unordered_map<string, string>* ns_name_href_map) {
  if (ns_name_href_map == nullptr) {
    LOG(ERROR) << "Namespace list or own namespace is null";
    return;
  }
  ns_name_href_map->insert(
      std::pair<string, string>(kPropertyPrefix, kNamespaceHref));
}

std::unique_ptr<PointCloud> PointCloud::FromData(
    const std::vector<float>& points, bool metric) {
  if (points.empty()) {
    LOG(ERROR) << "No point data given";
    return nullptr;
  }

  if (points.size() % 3 != 0) {
    LOG(ERROR) << "Points must be (x, y, z) tuples, so the size must be "
               << "divisible by 3, got " << points.size();
    return nullptr;
  }

  std::unique_ptr<PointCloud> point_cloud(new PointCloud());
  point_cloud->points_ = points;
  point_cloud->metric_ = metric;
  return point_cloud;
}

std::unique_ptr<PointCloud> PointCloud::FromDeserializer(
    const Deserializer& parent_deserializer) {
  std::unique_ptr<Deserializer> deserializer =
      parent_deserializer.CreateDeserializer(
          DynamicDepthConst::Namespace(kPropertyPrefix), kPropertyPrefix);
  if (deserializer == nullptr) {
    return nullptr;
  }

  std::unique_ptr<PointCloud> point_cloud(new PointCloud());
  if (!point_cloud->ParseFields(*deserializer)) {
    return nullptr;
  }
  return point_cloud;
}

int PointCloud::GetPointCount() const {
  return static_cast<int>(points_.size() / 3);
}

const std::vector<float>& PointCloud::GetPoints() const { return points_; }
bool PointCloud::GetMetric() const { return metric_; }

bool PointCloud::Serialize(Serializer* serializer) const {
  if (serializer == nullptr) {
    LOG(ERROR) << "Serializer is null";
    return false;
  }

  if (points_.empty()) {
    LOG(ERROR) << "No points in the PointCloud to serialize";
    return false;
  }

  // No error checking (e.g. points_.size() % 3 == 0), because serialization
  // shouldn't be blocked by this.
  string base64_encoded_points;
  if (!EncodeFloatArrayBase64(points_, &base64_encoded_points)) {
    LOG(ERROR) << "Points encoding failed";
    return false;
  }

  // Write required fields.
  int point_count = static_cast<int>(points_.size() / 3);
  if (!serializer->WriteProperty(
          DynamicDepthConst::PointCloud(), kPointCount,
          ::dynamic_depth::strings::SimpleItoa(point_count))) {
    return false;
  }

  if (!serializer->WriteProperty(DynamicDepthConst::PointCloud(), kPoints,
                                 base64_encoded_points)) {
    return false;
  }

  // Write optional fields.
  serializer->WriteBoolProperty(DynamicDepthConst::PointCloud(), kMetric,
                                metric_);
  return true;
}

// Private methods.
bool PointCloud::ParseFields(const Deserializer& deserializer) {
  // Required fields.
  std::vector<float> points;
  if (!deserializer.ParseFloatArrayBase64(DynamicDepthConst::PointCloud(),
                                          kPoints, &points)) {
    return false;
  }

  int point_count;
  if (!deserializer.ParseInt(DynamicDepthConst::PointCloud(), kPointCount,
                             &point_count)) {
    return false;
  }

  if (points.size() % 3 != 0) {
    LOG(ERROR) << "Parsed " << points.size() << " values but expected the size "
               << "to be divisible by 3 for (x, y, z) tuple representation";
    return false;
  }

  int parsed_points_count = static_cast<int>(points.size() / 3);
  if (parsed_points_count != point_count) {
    LOG(ERROR) << "Parsed PointCount = " << point_count << " but "
               << parsed_points_count << " points were found";
    return false;
  }

  // We know that point_count == points.size() now.
  points_ = points;

  // Optional fields.
  bool metric;
  if (!deserializer.ParseBoolean(DynamicDepthConst::PointCloud(), kMetric,
                                 &metric)) {
    // Set it to the default value.
    metric_ = false;
  } else {
    metric_ = metric;
  }

  return true;
}

}  // namespace dynamic_depth
}  // namespace photos_editing_formats