eos  0.7.1
LandmarkMapper.hpp
1 /*
2  * Eos - A 3D Morphable Model fitting library written in modern C++11/14.
3  *
4  * File: include/eos/core/LandmarkMapper.hpp
5  *
6  * Copyright 2014, 2015 Patrik Huber
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #pragma once
21 
22 #ifndef LANDMARKMAPPER_HPP_
23 #define LANDMARKMAPPER_HPP_
24 
25 #include "boost/optional.hpp"
26 #include "boost/filesystem/path.hpp"
27 #include "boost/property_tree/ptree.hpp"
28 #include "boost/property_tree/info_parser.hpp"
29 
30 #include <string>
31 #include <map>
32 
33 namespace eos {
34  namespace core {
35 
53 public:
58  LandmarkMapper() = default;
59 
71  LandmarkMapper(boost::filesystem::path filename)
72  {
73  using std::string;
74  using boost::property_tree::ptree;
75  ptree configtree;
76  try {
77  boost::property_tree::info_parser::read_info(filename.string(), configtree);
78  }
79  catch (const boost::property_tree::ptree_error& error) {
80  throw std::runtime_error(string("LandmarkMapper: Error reading landmark-mappings file: ") + error.what());
81  }
82 
83  try {
84  ptree pt_landmark_mappings = configtree.get_child("landmarkMappings");
85  for (auto&& mapping : pt_landmark_mappings) {
86  landmark_mappings.insert(make_pair(mapping.first, mapping.second.get_value<string>()));
87  }
88  }
89  catch (const boost::property_tree::ptree_error& error) {
90  throw std::runtime_error(string("LandmarkMapper: Error while parsing the mappings file: ") + error.what());
91  }
92  catch (const std::runtime_error& error) {
93  throw std::runtime_error(string("LandmarkMapper: Error while parsing the mappings file: ") + error.what());
94  }
95  };
96 
105  boost::optional<std::string> convert(std::string landmark_name) const
106  {
107  if (landmark_mappings.empty()) {
108  // perform identity mapping, i.e. return the input
109  return landmark_name;
110  }
111  else {
112  auto&& converted_landmark = landmark_mappings.find(landmark_name);
113  if (converted_landmark != std::end(landmark_mappings)) {
114  // landmark mapping found, return it
115  return converted_landmark->second;
116  }
117  else { // landmark_name does not match the key of any element in the map
118  return boost::none;
119  }
120  }
121  };
122 
128  auto size() const
129  {
130  return landmark_mappings.size();
131  };
132 
133 private:
134  std::map<std::string, std::string> landmark_mappings;
135 };
136 
137  } /* namespace core */
138 } /* namespace eos */
139 
140 #endif /* LANDMARKMAPPER_HPP_ */
LandmarkMapper()=default
Constructs a new landmark mapper that performs an identity mapping, that is, its output is the same a...
auto size() const
Returns the number of loaded landmark mappings.
Definition: LandmarkMapper.hpp:128
Namespace containing all of eos&#39;s 3D model fitting functionality.
boost::optional< std::string > convert(std::string landmark_name) const
Converts the given landmark name to the mapped name.
Definition: LandmarkMapper.hpp:105
LandmarkMapper(boost::filesystem::path filename)
Constructs a new landmark mapper from a file containing mappings from one set of landmark identifiers...
Definition: LandmarkMapper.hpp:71
Represents a mapping from one kind of landmarks to a different format (e.g. model vertices)...
Definition: LandmarkMapper.hpp:52