DGtal 2.1.0
Loading...
Searching...
No Matches
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 2018/01/01
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#if defined(__GNUC__) || defined(__clang__)
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wall"
37#pragma GCC diagnostic ignored "-Wextra"
38#pragma GCC diagnostic ignored "-Wpedantic"
39#pragma GCC diagnostic ignored "-Wshadow"
40#pragma GCC diagnostic ignored "-Wtype-limits"
41#endif
42// zlib + boost for reading compressed tables
43#include <boost/iostreams/filtering_streambuf.hpp>
44#include <boost/iostreams/copy.hpp>
45#include <boost/iostreams/filter/zlib.hpp>
46#if defined(__GNUC__) || defined(__clang__)
47#pragma GCC diagnostic pop
48#endif
49
50namespace DGtal{
51 namespace functions {
52/*---------------------------------------------------------------------*/
53
54 DGtal::CountedPtr< boost::dynamic_bitset<> >
55 loadTable(const std::string &input_filename,
56 const unsigned int known_size,
57 const bool compressed)
58 {
59 using ConfigMap = boost::dynamic_bitset<> ;
60 CountedPtr<ConfigMap> table(new ConfigMap(known_size));
61 try {
62 if (compressed) {
63 std::ifstream in_file(input_filename, std::ios::binary);
64 namespace io = boost::iostreams ;
65 io::filtering_streambuf<io::input> filter;
66 filter.push(io::zlib_decompressor());
67 filter.push(in_file);
68 std::stringstream compressed_stream;
69 io::copy(filter,compressed_stream);
70 compressed_stream >> *table ;
71 } else {
72 std::ifstream in_file(input_filename);
73 in_file >> *table ;
74 }
75 } catch(std::exception &e) {
76 throw std::runtime_error("loadTable error in: " + input_filename + " with exception: " + e.what());
77 }
78
79 return table ;
80 }
81
82 template<unsigned int N>
83 inline
84 DGtal::CountedPtr< boost::dynamic_bitset<> >
85 loadTable(const std::string &input_filename, const bool compressed)
86 {
87 if (N == 3) // Default
88 return loadTable(input_filename, 67108864, compressed);
89 if (N == 2)
90 return loadTable(input_filename, 256, compressed);
91 throw std::domain_error("loadTable<N> error, template parameter N = "
92 + std::to_string(N) + " is invalid (use N = 2 or N = 3)");
93
94 }
95
96/*---------------------------------------------------------------------*/
97
98 template<typename TPoint>
99 inline
100 DGtal::CountedPtr<
101 std::unordered_map<TPoint, NeighborhoodConfiguration > >
102 mapZeroPointNeighborhoodToConfigurationMask()
103 {
104 using Map = std::unordered_map<TPoint, NeighborhoodConfiguration> ;
105 NeighborhoodConfiguration mask = 1 ;
106 CountedPtr<Map> mapPtr(new Map);
107 Map& amap = *mapPtr;
108 auto p1 = TPoint::diagonal(-1);
109 auto p2 = TPoint::diagonal(1);
110 auto center = TPoint::diagonal(0);
111 // HyperRect Domain with lexicograpicOrder ensures same order.
112 using Space = SpaceND< TPoint::dimension , DGtal::int32_t>;
113 using Domain = HyperRectDomain< Space >;
114 const Domain domain(p1, p2);
115 for ( auto it = domain.begin(), itE = domain.end() ;
116 it!=itE ; ++it)
117 {
118 if (*it == center ) continue;
119 amap[*it] = mask;
120 mask <<= 1 ;
121 }
122
123 return mapPtr;
124 }
125
126 } // namespace functions
127} // namespace DGtal