Stride Reference Manual  - generated for commit 9643b11
GeoGrid.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, 2019, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #pragma once
17 
18 #include "contact/ContactType.h"
20 
21 #include <set>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace stride {
26 class ContactPool;
27 class Population;
28 } // namespace stride
29 
30 namespace geopop {
31 
32 class Location;
33 
34 template <typename Policy, typename... F>
35 class GeoAggregator;
36 
41 class GeoGrid
42 {
43 public:
45  explicit GeoGrid(stride::Population* population);
46 
48  GeoGrid(const GeoGrid&) = delete;
49 
51  GeoGrid operator=(const GeoGrid&) = delete;
52 
54  void AddLocation(std::shared_ptr<Location> location);
55 
57  void Finalize();
58 
60  std::shared_ptr<Location> GetById(unsigned int id) const { return m_locations[m_id_to_index.at(id)]; }
61 
64 
66  std::vector<const Location*> LocationsInRadius(const Location& start, double radius) const;
67 
71  std::vector<stride::ContactPool*> GetNearbyPools(stride::ContactType::Id id, const Location& start,
72  double startRadius = 10.0) const;
73 
84  std::set<const Location*> LocationsInBox(double long1, double lat1, double long2, double lat2) const;
85 
87  std::set<const Location*> LocationsInBox(Location* loc1, Location* loc2) const;
88 
90  std::vector<Location*> TopK(size_t k) const;
91 
92 public:
94  template <typename Policy, typename F>
95  GeoAggregator<Policy, F> BuildAggregator(F functor, typename Policy::Args&& args) const;
96 
98  template <typename Policy>
99  GeoAggregator<Policy> BuildAggregator(typename Policy::Args&& args) const;
100 
101 public:
102  using iterator = std::vector<std::shared_ptr<Location>>::iterator;
103  using const_iterator = std::vector<std::shared_ptr<Location>>::const_iterator;
104 
106  iterator begin() { return m_locations.begin(); }
107 
109  iterator end() { return m_locations.end(); }
110 
112  const_iterator cbegin() const { return m_locations.cbegin(); }
113 
115  const_iterator cend() const { return m_locations.cend(); }
116 
118  std::shared_ptr<Location>& operator[](size_t index) { return m_locations[index]; }
119 
121  const std::shared_ptr<Location>& operator[](size_t index) const { return m_locations[index]; }
122 
124  size_t size() const { return m_locations.size(); }
125 
126 private:
128  void CheckFinalized(const std::string& functionName) const;
129 
130 private:
132  std::vector<std::shared_ptr<Location>> m_locations;
133 
135  std::unordered_map<unsigned int, unsigned int> m_id_to_index;
136 
139 
142 
145 };
146 
147 } // namespace geopop
GeoGridKdTree m_tree
Definition: GeoGrid.h:144
std::vector< const Location * > LocationsInRadius(const Location &start, double radius) const
Search for locations in radius (in km) around start.
Definition: GeoGrid.cpp:98
std::vector< std::shared_ptr< Location >>::iterator iterator
Definition: GeoGrid.h:102
Id
Enumerates the ContactPool types.
Definition: ContactType.h:34
stride::Population * GetPopulation() const
Get the Population associated with this GeoGrid.
Definition: GeoGrid.h:63
A GeoAggregator that has to be called with a functor.
Definition: GeoAggregator.h:85
GeoGrid operator=(const GeoGrid &)=delete
No copy assignment.
iterator end()
Iterator to the end of the Location storage.
Definition: GeoGrid.h:109
std::vector< std::shared_ptr< Location >>::const_iterator const_iterator
Definition: GeoGrid.h:103
bool m_finalized
Internal KdTree for quick spatial lookup.
Definition: GeoGrid.h:141
const_iterator cbegin() const
Const Iterator to first Location.
Definition: GeoGrid.h:112
iterator begin()
Iterator to first Location.
Definition: GeoGrid.h:106
void AddLocation(std::shared_ptr< Location > location)
Adds a location to this GeoGrid.
Definition: GeoGrid.cpp:39
GeoGrid(stride::Population *population)
GeoGrid and associated Population.
Definition: GeoGrid.cpp:34
std::vector< std::shared_ptr< Location > > m_locations
< Container for Locations in GeoGrid.
Definition: GeoGrid.h:132
std::set< const Location * > LocationsInBox(double long1, double lat1, double long2, double lat2) const
Gets the locations in a rectangle determined by the two coordinates (long1, lat1) and (long2...
Definition: GeoGrid.cpp:77
void Finalize()
Disables the addLocation method and builds the kdtree.
Definition: GeoGrid.cpp:67
A Geographic grid of simulation region contains Locations that in turn contain an index to the Contac...
Definition: GeoGrid.h:41
std::shared_ptr< Location > & operator[](size_t index)
Gets a Location by index, doesn&#39;t performs a range check.
Definition: GeoGrid.h:118
std::vector< Location * > TopK(size_t k) const
Gets the K biggest (in population count) locations of this GeoGrid.
Definition: GeoGrid.cpp:129
std::unordered_map< unsigned int, unsigned int > m_id_to_index
Stores pointer to Popluation, but does not take ownership.
Definition: GeoGrid.h:135
Key Data structure: container for (a) all individuals in the population (b) the ContactPoolSys wchich...
Definition: Population.h:47
size_t size() const
Gets current size of Location storage.
Definition: GeoGrid.h:124
Definition of ContactPool Id Type.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
GeoAggregator< Policy, F > BuildAggregator(F functor, typename Policy::Args &&args) const
Build a GeoAggregator with a predefined functor and given args for the Policy.
Definition: GeoGrid.cpp:49
std::shared_ptr< Location > GetById(unsigned int id) const
Gets a Location by Id and check if the Id exists.
Definition: GeoGrid.h:60
A GeoAggregator constructed with a functor.
std::vector< stride::ContactPool * > GetNearbyPools(stride::ContactType::Id id, const Location &start, double startRadius=10.0) const
Find contactpools in startRadius (in km) around start and, if none are found, double the radius and s...
Definition: GeoGrid.cpp:111
void CheckFinalized(const std::string &functionName) const
< Checks whether the GeoGrid is finalized i.e. certain operations can(not) be used.
Definition: GeoGrid.cpp:60
stride::Population * m_population
Is the GeoGrid finalized (ready for use) yet?
Definition: GeoGrid.h:138
Location for use within the GeoGrid, contains Coordinate and index to ContactPools.
Definition: Location.h:41
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
const std::shared_ptr< Location > & operator[](size_t index) const
Gets a Location by index, doesn&#39;t performs a range check.
Definition: GeoGrid.h:121
const_iterator cend() const
Const iterator to the end of the Location storage.
Definition: GeoGrid.h:115