ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
consistency_graph.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 "mvs/consistency_graph.h"
33 
34 #include <fstream>
35 #include <numeric>
36 
37 #include "util/logging.h"
38 #include "util/misc.h"
39 
40 namespace colmap {
41 namespace mvs {
42 
43 const int ConsistencyGraph::kNoConsistentImageIds = -1;
44 
46 
47 ConsistencyGraph::ConsistencyGraph(const size_t width, const size_t height,
48  const std::vector<int>& data)
49  : data_(data) {
50  InitializeMap(width, height);
51 }
52 
54  return (data_.size() + map_.size()) * sizeof(int);
55 }
56 
57 void ConsistencyGraph::GetImageIdxs(const int row, const int col,
58  int* num_images,
59  const int** image_idxs) const {
60  const int index = map_(row, col);
61  if (index == kNoConsistentImageIds) {
62  *num_images = 0;
63  *image_idxs = nullptr;
64  } else {
65  *num_images = data_.at(index);
66  *image_idxs = &data_.at(index + 1);
67  }
68 }
69 
70 void ConsistencyGraph::Read(const std::string& path) {
71  std::fstream text_file(path, std::ios::in | std::ios::binary);
72  CHECK(text_file.is_open()) << path;
73 
74  size_t width = 0;
75  size_t height = 0;
76  size_t depth = 0;
77  char unused_char;
78 
79  text_file >> width >> unused_char >> height >> unused_char >> depth >>
80  unused_char;
81  const std::streampos pos = text_file.tellg();
82  text_file.close();
83 
84  CHECK_GT(width, 0);
85  CHECK_GT(height, 0);
86  CHECK_GT(depth, 0);
87 
88  std::fstream binary_file(path, std::ios::in | std::ios::binary);
89  CHECK(binary_file.is_open()) << path;
90 
91  binary_file.seekg(0, std::ios::end);
92  const size_t num_bytes = binary_file.tellg() - pos;
93 
94  data_.resize(num_bytes / sizeof(int));
95 
96  binary_file.seekg(pos);
97  ReadBinaryLittleEndian<int>(&binary_file, &data_);
98  binary_file.close();
99 
100  InitializeMap(width, height);
101 }
102 
103 void ConsistencyGraph::Write(const std::string& path) const {
104  std::fstream text_file(path, std::ios::out);
105  CHECK(text_file.is_open()) << path;
106  text_file << map_.cols() << "&" << map_.rows() << "&" << 1 << "&";
107  text_file.close();
108 
109  std::fstream binary_file(path,
110  std::ios::out | std::ios::binary | std::ios::app);
111  CHECK(binary_file.is_open()) << path;
112  WriteBinaryLittleEndian<int>(&binary_file, data_);
113  binary_file.close();
114 }
115 
116 void ConsistencyGraph::InitializeMap(const size_t width, const size_t height) {
117  map_.resize(height, width);
118  map_.setConstant(kNoConsistentImageIds);
119  for (size_t i = 0; i < data_.size();) {
120  const int num_images = data_.at(i + 2);
121  if (num_images > 0) {
122  const int col = data_.at(i);
123  const int row = data_.at(i + 1);
124  map_(row, col) = i + 2;
125  }
126  i += 3 + num_images;
127  }
128 }
129 
130 } // namespace mvs
131 } // namespace colmap
int width
int height
void GetImageIdxs(const int row, const int col, int *num_images, const int **image_idxs) const
void Read(const std::string &path)
void Write(const std::string &path) const
GraphType data
Definition: graph_cut.cc:138
static const std::string path
Definition: PointCloud.cpp:59