Optimized Belief Propagation (CPU and GPU)
BpFileHandling.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #include "BpFileHandling.h"
28 
35 std::filesystem::path BpFileHandling::StereoSetsPath() const
36 {
37  std::filesystem::path current_path = std::filesystem::current_path();
38  while (true)
39  {
40  //get iterator to belief prop directory in current path or filesystem end
41  //iterator if none found and then check if return from find_if at iterator
42  //end and therefore didn't find stereo sets directory; if that's the case
43  //continue to outer directory
44  //for now assuming stereo sets directory exists in some outer directory
45  //and program won't work without it
46  if (auto it =
47  std::find_if(
48  std::filesystem::begin(std::filesystem::directory_iterator(current_path)),
49  std::filesystem::end(std::filesystem::directory_iterator(current_path)),
50  [](const auto& p) {
51  return p.path().stem() == beliefprop::kBeliefPropDirectoryName; });
52  it == std::filesystem::end(std::filesystem::directory_iterator(current_path)))
53  {
54  //if current path same as parent path, then can't continue and throw error
55  if (current_path == current_path.parent_path()) {
56  throw std::filesystem::filesystem_error(
57  "Belief prop directory not found", std::error_code());
58  }
59  current_path = current_path.parent_path();
60  }
61  else {
62  if (const std::filesystem::path stereo_set_path =
64  std::filesystem::is_directory(stereo_set_path))
65  {
66  return stereo_set_path;
67  }
68  else {
69  throw std::filesystem::filesystem_error(
70  "Stereo set directory not found in belief prop directory",
71  std::error_code());
72  }
73  }
74  }
75 }
76 
83 std::filesystem::path BpFileHandling::RefImagePath() const
84 {
85  //check if ref image exists for each possible extension (currently pgm and
86  //ppm) and return path if so
87  for (const auto& extension : beliefprop::kInImagePossExtensions) {
88  if (std::filesystem::exists(
89  (stereo_set_path_ /
90  (std::string(beliefprop::kRefImageName) + "." +
91  std::string(extension))))) {
92  return stereo_set_path_ / (std::string(beliefprop::kRefImageName) +
93  "." + std::string(extension));
94  }
95  }
96 
97  throw std::filesystem::filesystem_error(
98  "Reference image not found",
99  std::error_code());
100 }
101 
108 std::filesystem::path BpFileHandling::TestImagePath() const
109 {
110  //check if test image exists for each possible extension (currently pgm and
111  //ppm) and return path if so
112  for (const auto& extension : beliefprop::kInImagePossExtensions) {
113  if (std::filesystem::exists(
114  (stereo_set_path_ /
115  (std::string(beliefprop::kTestImageName) + "." +
116  std::string(extension))))) {
117  return stereo_set_path_ / (std::string(beliefprop::kTestImageName) +
118  "." + std::string(extension));
119  }
120  }
121 
122  throw std::filesystem::filesystem_error(
123  "Test image not found",
124  std::error_code());
125 }
Declares class to retrieve path of stereo set files for reading and for output.
std::filesystem::path RefImagePath() const
Return path to reference image with valid extension if found, otherwise throw filesystem error.
std::filesystem::path TestImagePath() const
Return path to test image with valid extension if found, otherwise throw filesystem error.
constexpr std::string_view kRefImageName
constexpr std::string_view kInImagePossExtensions[]
constexpr std::string_view kStereoSetsDirectoryName
constexpr std::string_view kTestImageName
constexpr std::string_view kBeliefPropDirectoryName