| 15 | |
| 16 | namespace generative::noding { |
| 17 | class GeometryGraph |
| 18 | { |
| 19 | public: |
| 20 | struct Node |
| 21 | { |
| 22 | //! @brief The index into the GeometryGraph::get_nodes() array. |
| 23 | const std::size_t index; |
| 24 | //! @brief the point at which this node is located. |
| 25 | std::unique_ptr<geos::geom::Point> point; |
| 26 | //! @brief All of the Node is's adjacent to this node. |
| 27 | std::unordered_set<std::size_t> adjacencies; |
| 28 | |
| 29 | Node(std::size_t _id, std::unique_ptr<geos::geom::Point> _point) : |
| 30 | index(_id), point(std::move(_point)) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | geos::geom::CoordinateXY coord() const |
| 35 | { |
| 36 | if (point) |
| 37 | { |
| 38 | const auto* coord = point->getCoordinate(); |
| 39 | if (coord != nullptr) |
| 40 | { |
| 41 | return *coord; |
| 42 | } |
| 43 | } |
| 44 | return geos::geom::CoordinateXY::getNull(); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | //! @brief Create an empty graph. |
| 49 | //! @see GeometryGraph::build() to generate the graph from a geometry. |
| 50 | //! @see GeometryGraph::set_nodes() and GeometryGraph::add_edge() to build a graph yourself. |
| 51 | explicit GeometryGraph(const geos::geom::GeometryFactory& factory) : m_factory(factory) {} |
| 52 | |
| 53 | //! @brief Create and build the graph from the given geometry. |
| 54 | //! @note The geometry must be fully noded. |
| 55 | //! @param multilinestring - A fully noded collection of linestrings to build the graph from. |
| 56 | explicit GeometryGraph(const geos::geom::Geometry& geometrycollection); |
| 57 | |
| 58 | //! @brief Create a graph known a priori using the given factory. |
| 59 | GeometryGraph(std::vector<Node>&& nodes, const geos::geom::GeometryFactory& factory); |
| 60 | |
| 61 | //! @brief Build the graph from the given geoemtry. |
| 62 | //! @note The geometry must be fully noded. |
| 63 | void build(const geos::geom::Geometry& geometry); |
| 64 | |
| 65 | //! @brief Get the constructed graph. |
| 66 | [[nodiscard]] const std::vector<Node>& get_nodes() const { return m_nodes; } |
| 67 | void set_nodes(std::vector<Node>&& nodes) { m_nodes = std::move(nodes); } |
| 68 | |
| 69 | //! @brief Add the given edge to the graph. |
| 70 | //! @note The nodes at the indices @p src and @p dst must exist. |
| 71 | void add_edge(std::size_t src, std::size_t dst); |
| 72 | |
| 73 | //! @brief Add the given node to the graph. |
| 74 | //! @returns the index of the created node. |