DGtal  0.9.2
NeighborhoodConfigurations.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file NeighborhoodConfigurations.ih
19  *
20  * @author Pablo Hernandez-Cerdan. Institute of Fundamental Sciences.
21  * Massey University. Palmerston North, New Zealand
22  *
23  * @date 2016/03/25
24  *
25  * Implementation of header NeighborhoodConfigurations.h
26  *
27  * This file is part of the DGtal library.
28  */
29 
30 #include <fstream>
31 #include "DGtal/kernel/SpaceND.h"
32 #include "DGtal/kernel/domains/HyperRectDomain.h"
33 
34 namespace DGtal{
35  namespace functions {
36 /*---------------------------------------------------------------------*/
37 
38  DGtal::CountedPtr< boost::dynamic_bitset<> >
39  loadTable(const std::string &input_filename, unsigned int known_size)
40  {
41  using ConfigMap = boost::dynamic_bitset<> ;
42  CountedPtr<ConfigMap> table(new ConfigMap(known_size));
43  try {
44  std::ifstream text_file(input_filename);
45  text_file >> *table ;
46  text_file.close();
47  } catch(std::exception &e) {
48  throw std::runtime_error("loadTable error in: " + input_filename + " with exception: " + e.what());
49  }
50 
51  return table ;
52  }
53 
54  template<unsigned int N>
55  inline
56  DGtal::CountedPtr< boost::dynamic_bitset<> >
57  loadTable(const std::string &input_filename)
58  {
59  if (N == 3) // Default
60  return loadTable(input_filename, 67108864);
61  if (N == 2)
62  return loadTable(input_filename, 256);
63  throw std::domain_error("loadTable<N> error, template parameter N = "
64  + std::to_string(N) + " is invalid (use N = 2 or N = 3)");
65 
66  }
67 
68 /*---------------------------------------------------------------------*/
69 
70  template<typename TPoint>
71  inline
72  DGtal::CountedPtr<
73  std::unordered_map<TPoint, NeighborhoodConfiguration > >
74  mapZeroPointNeighborhoodToConfigurationMask()
75  {
76  using Map = std::unordered_map<TPoint, NeighborhoodConfiguration> ;
77  NeighborhoodConfiguration mask = 1 ;
78  CountedPtr<Map> mapPtr(new Map);
79  Map& amap = *mapPtr;
80  auto p1 = TPoint::diagonal(-1);
81  auto p2 = TPoint::diagonal(1);
82  auto center = TPoint::diagonal(0);
83  // HyperRect Domain with lexicograpicOrder ensures same order.
84  using Space = SpaceND< TPoint::dimension , DGtal::int32_t>;
85  using Domain = HyperRectDomain< Space >;
86  Domain domain(p1,p2);
87  for (typename Domain::ConstIterator it = domain.begin(), itE = domain.end() ;
88  it!=itE ; ++it)
89  {
90  if (*it == center ) continue;
91  amap[*it] = mask;
92  mask <<= 1 ;
93  }
94 
95  return mapPtr;
96  }
97 
98  } // namespace functions
99 } // namespace DGtal