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.
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.
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/>.
18 * @file NeighborhoodConfigurations.ih
20 * @author Pablo Hernandez-Cerdan. Institute of Fundamental Sciences.
21 * Massey University. Palmerston North, New Zealand
25 * Implementation of header NeighborhoodConfigurations.h
27 * This file is part of the DGtal library.
31#include "DGtal/kernel/SpaceND.h"
32#include "DGtal/kernel/domains/HyperRectDomain.h"
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"
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
52/*---------------------------------------------------------------------*/
54 DGtal::CountedPtr< boost::dynamic_bitset<> >
55 loadTable(const std::string &input_filename,
56 const unsigned int known_size,
57 const bool compressed)
59 using ConfigMap = boost::dynamic_bitset<> ;
60 CountedPtr<ConfigMap> table(new ConfigMap(known_size));
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());
68 std::stringstream compressed_stream;
69 io::copy(filter,compressed_stream);
70 compressed_stream >> *table ;
72 std::ifstream in_file(input_filename);
75 } catch(std::exception &e) {
76 throw std::runtime_error("loadTable error in: " + input_filename + " with exception: " + e.what());
82 template<unsigned int N>
84 DGtal::CountedPtr< boost::dynamic_bitset<> >
85 loadTable(const std::string &input_filename, const bool compressed)
87 if (N == 3) // Default
88 return loadTable(input_filename, 67108864, compressed);
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)");
96/*---------------------------------------------------------------------*/
98 template<typename TPoint>
101 std::unordered_map<TPoint, NeighborhoodConfiguration > >
102 mapZeroPointNeighborhoodToConfigurationMask()
104 using Map = std::unordered_map<TPoint, NeighborhoodConfiguration> ;
105 NeighborhoodConfiguration mask = 1 ;
106 CountedPtr<Map> mapPtr(new Map);
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() ;
118 if (*it == center ) continue;
126 } // namespace functions