ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
image.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "base/image.h"
33 
34 #include "base/pose.h"
35 #include "base/projection.h"
36 
37 namespace colmap {
38 namespace {
39 
40 static const double kNaN = std::numeric_limits<double>::quiet_NaN();
41 
42 } // namespace
43 
45 
47  : image_id_(kInvalidImageId),
48  name_(""),
49  camera_id_(kInvalidCameraId),
50  registered_(false),
51  num_points3D_(0),
52  num_observations_(0),
53  num_correspondences_(0),
54  num_visible_points3D_(0),
55  qvec_(1.0, 0.0, 0.0, 0.0),
56  tvec_(0.0, 0.0, 0.0),
57  qvec_prior_(kNaN, kNaN, kNaN, kNaN),
58  tvec_prior_(kNaN, kNaN, kNaN) {}
59 
60 void Image::SetUp(const class Camera& camera) {
61  CHECK_EQ(camera_id_, camera.CameraId());
62  point3D_visibility_pyramid_ =
64  camera.Width(), camera.Height());
65 }
66 
68  point3D_visibility_pyramid_ = VisibilityPyramid(0, 0, 0);
69 }
70 
71 void Image::SetPoints2D(const std::vector<Eigen::Vector2d>& points) {
72  CHECK(points2D_.empty());
73  points2D_.resize(points.size());
74  num_correspondences_have_point3D_.resize(points.size(), 0);
75  for (point2D_t point2D_idx = 0; point2D_idx < points.size();
76  ++point2D_idx) {
77  points2D_[point2D_idx].SetXY(points[point2D_idx]);
78  }
79 }
80 
81 void Image::SetPoints2D(const std::vector<class Point2D>& points) {
82  CHECK(points2D_.empty());
83  points2D_ = points;
84  num_correspondences_have_point3D_.resize(points.size(), 0);
85 }
86 
87 void Image::SetPoint3DForPoint2D(const point2D_t point2D_idx,
88  const point3D_t point3D_id) {
89  CHECK_NE(point3D_id, kInvalidPoint3DId);
90  class Point2D& point2D = points2D_.at(point2D_idx);
91  if (!point2D.HasPoint3D()) {
92  num_points3D_ += 1;
93  }
94  point2D.SetPoint3DId(point3D_id);
95 }
96 
97 void Image::ResetPoint3DForPoint2D(const point2D_t point2D_idx) {
98  class Point2D& point2D = points2D_.at(point2D_idx);
99  if (point2D.HasPoint3D()) {
101  num_points3D_ -= 1;
102  }
103 }
104 
105 bool Image::HasPoint3D(const point3D_t point3D_id) const {
106  return std::find_if(points2D_.begin(), points2D_.end(),
107  [point3D_id](const class Point2D& point2D) {
108  return point2D.Point3DId() == point3D_id;
109  }) != points2D_.end();
110 }
111 
113  const class Point2D& point2D = points2D_.at(point2D_idx);
114 
115  num_correspondences_have_point3D_[point2D_idx] += 1;
116  if (num_correspondences_have_point3D_[point2D_idx] == 1) {
117  num_visible_points3D_ += 1;
118  }
119 
120  point3D_visibility_pyramid_.SetPoint(point2D.X(), point2D.Y());
121 
122  assert(num_visible_points3D_ <= num_observations_);
123 }
124 
126  const class Point2D& point2D = points2D_.at(point2D_idx);
127 
128  num_correspondences_have_point3D_[point2D_idx] -= 1;
129  if (num_correspondences_have_point3D_[point2D_idx] == 0) {
130  num_visible_points3D_ -= 1;
131  }
132 
133  point3D_visibility_pyramid_.ResetPoint(point2D.X(), point2D.Y());
134 
135  assert(num_visible_points3D_ <= num_observations_);
136 }
137 
138 void Image::NormalizeQvec() { qvec_ = NormalizeQuaternion(qvec_); }
139 
141  return ComposeProjectionMatrix(qvec_, tvec_);
142 }
143 
145  return InvertProjectionMatrix(ComposeProjectionMatrix(qvec_, tvec_));
146 }
147 
148 Eigen::Matrix3d Image::RotationMatrix() const {
149  return QuaternionToRotationMatrix(qvec_);
150 }
151 
152 Eigen::Vector3d Image::ProjectionCenter() const {
153  return ProjectionCenterFromPose(qvec_, tvec_);
154 }
155 
156 Eigen::Vector3d Image::ViewingDirection() const {
157  return RotationMatrix().row(2);
158 }
159 
160 } // namespace colmap
int points
std::string name_
Definition: FilePLY.cpp:33
camera_t CameraId() const
Definition: camera.h:154
size_t Width() const
Definition: camera.h:160
size_t Height() const
Definition: camera.h:162
void TearDown()
Definition: image.cc:67
static const int kNumPoint3DVisibilityPyramidLevels
Definition: image.h:182
Eigen::Matrix3x4d ProjectionMatrix() const
Definition: image.cc:140
void NormalizeQvec()
Definition: image.cc:138
void SetPoints2D(const std::vector< Eigen::Vector2d > &points)
Definition: image.cc:71
void ResetPoint3DForPoint2D(const point2D_t point2D_idx)
Definition: image.cc:97
void DecrementCorrespondenceHasPoint3D(const point2D_t point2D_idx)
Definition: image.cc:125
Eigen::Matrix3x4d InverseProjectionMatrix() const
Definition: image.cc:144
Eigen::Vector3d ViewingDirection() const
Definition: image.cc:156
bool HasPoint3D(const point3D_t point3D_id) const
Definition: image.cc:105
void SetUp(const Camera &camera)
Definition: image.cc:60
void IncrementCorrespondenceHasPoint3D(const point2D_t point2D_idx)
Definition: image.cc:112
Eigen::Matrix3d RotationMatrix() const
Definition: image.cc:148
Eigen::Vector3d ProjectionCenter() const
Definition: image.cc:152
void SetPoint3DForPoint2D(const point2D_t point2D_idx, const point3D_t point3D_id)
Definition: image.cc:87
double X() const
Definition: point2d.h:57
double Y() const
Definition: point2d.h:59
bool HasPoint3D() const
Definition: point2d.h:65
void SetPoint3DId(const point3D_t point3D_id)
Definition: point2d.h:67
void ResetPoint(const double x, const double y)
void SetPoint(const double x, const double y)
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
uint64_t point3D_t
Definition: types.h:72
Eigen::Vector4d NormalizeQuaternion(const Eigen::Vector4d &qvec)
Definition: pose.cc:82
const camera_t kInvalidCameraId
Definition: types.h:75
uint32_t point2D_t
Definition: types.h:67
Eigen::Matrix3x4d InvertProjectionMatrix(const Eigen::Matrix3x4d &proj_matrix)
Definition: projection.cc:55
const point3D_t kInvalidPoint3DId
Definition: types.h:80
Eigen::Vector3d ProjectionCenterFromPose(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: pose.cc:164
const image_t kInvalidImageId
Definition: types.h:76
Eigen::Matrix3x4d ComposeProjectionMatrix(const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec)
Definition: projection.cc:39
Eigen::Matrix3d QuaternionToRotationMatrix(const Eigen::Vector4d &qvec)
Definition: pose.cc:75