-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node the GeometryCollectionShim and return a GeometryGraphShim
- Loading branch information
Showing
9 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
#include "generative/generative/cxxbridge/coord_ffi.rs.h" | ||
#include <generative/noding/geometry-graph.h> | ||
|
||
#include <rust/cxx.h> | ||
|
||
#include <vector> | ||
|
||
class GeometryGraphShim | ||
{ | ||
public: | ||
explicit GeometryGraphShim(generative::noding::GeometryGraph&& graph) : | ||
m_inner(std::move(graph)) | ||
{ | ||
} | ||
|
||
[[nodiscard]] rust::Vec<CoordShim> nodes() const noexcept | ||
{ | ||
const auto& cxx_nodes = m_inner.get_nodes(); | ||
rust::Vec<CoordShim> rust_nodes; | ||
rust_nodes.reserve(cxx_nodes.size()); | ||
|
||
for (const auto& node : cxx_nodes) | ||
{ | ||
auto rust_node = CoordShim{node.coord().x, node.coord().y}; | ||
rust_nodes.emplace_back(rust_node); | ||
} | ||
|
||
return rust_nodes; | ||
} | ||
|
||
[[nodiscard]] rust::Vec<GraphEdge> edges() const noexcept | ||
{ | ||
const auto& cxx_edges = m_inner.get_edge_pairs(); | ||
rust::Vec<GraphEdge> rust_edges; | ||
rust_edges.reserve(cxx_edges.size()); | ||
for (const auto& edge : cxx_edges) | ||
{ | ||
auto rust_edge = GraphEdge{edge.first.index, edge.second.index}; | ||
rust_edges.emplace_back(rust_edge); | ||
} | ||
|
||
return rust_edges; | ||
} | ||
|
||
private: | ||
generative::noding::GeometryGraph m_inner; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[cxx::bridge] | ||
pub mod ffi { | ||
unsafe extern "C++" { | ||
include!("geometry_graph.hpp"); | ||
|
||
type GeometryGraphShim; | ||
type CoordShim = crate::cxxbridge::CoordShim; | ||
type GraphEdge = crate::cxxbridge::GraphEdge; | ||
|
||
fn nodes(self: &GeometryGraphShim) -> Vec<CoordShim>; | ||
fn edges(self: &GeometryGraphShim) -> Vec<GraphEdge>; | ||
} | ||
|
||
impl UniquePtr<GeometryGraphShim> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
mod coord_ffi; | ||
mod geometry_collection; | ||
mod geometry_collection_ffi; | ||
mod geometry_graph_ffi; | ||
mod noder_ffi; | ||
|
||
pub use coord_ffi::ffi::{CoordShim, LineStringShim, PolygonShim}; | ||
pub use coord_ffi::ffi::{CoordShim, GraphEdge, LineStringShim, PolygonShim}; | ||
pub use geometry_collection::GeometryCollectionShim; | ||
pub use geometry_graph_ffi::ffi::GeometryGraphShim; | ||
pub use noder_ffi::ffi::node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
#pragma once | ||
#include "generative/generative/cxxbridge/geometry_collection_ffi.rs.h" | ||
#include "generative/generative/cxxbridge/geometry_graph_ffi.rs.h" | ||
#include "geometry_collection.hpp" | ||
#include <generative/noding/geometry-graph.h> | ||
#include <generative/noding/geometry-noder.h> | ||
|
||
inline void _compile_tester(const GeometryCollectionShim& rust_geoms) noexcept | ||
#include <geos/noding/snap/SnappingNoder.h> | ||
|
||
#include <memory> | ||
|
||
[[nodiscard]] inline std::unique_ptr<GeometryGraphShim> | ||
node(const GeometryCollectionShim& rust_geoms, double tolerance) noexcept | ||
{ | ||
const auto geos_geoms = copy_rust_collection_to_geos(rust_geoms); | ||
std::cerr << "Noding: " << geos_geoms->toString() << std::endl; | ||
|
||
auto noded = std::unique_ptr<geos::geom::Geometry>(nullptr); | ||
if (tolerance == 0.0) | ||
{ | ||
noded = generative::noding::GeometryNoder::node(*geos_geoms, nullptr); | ||
} else | ||
{ | ||
auto noder = std::make_unique<geos::noding::snap::SnappingNoder>(tolerance); | ||
noded = generative::noding::GeometryNoder::node(*geos_geoms, std::move(noder)); | ||
} | ||
std::cerr << "Noded: " << noded->toString() << std::endl; | ||
auto graph = generative::noding::GeometryGraph(*noded); | ||
|
||
auto graph_shim = std::make_unique<GeometryGraphShim>(std::move(graph)); | ||
|
||
return graph_shim; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,75 @@ | ||
#[cxx::bridge] | ||
mod ffi { | ||
pub mod ffi { | ||
unsafe extern "C++" { | ||
include!("noder.hpp"); | ||
|
||
type GeometryCollectionShim = crate::cxxbridge::GeometryCollectionShim; | ||
type GeometryGraphShim = crate::cxxbridge::GeometryGraphShim; | ||
|
||
fn _compile_tester(geoms: &GeometryCollectionShim); | ||
/// Node the given collection of geometries | ||
/// | ||
/// # Safety | ||
/// | ||
/// The noding is done by either a geos SnappingNoder or IteratedNoder, both of which have | ||
/// some gotchas. | ||
/// | ||
/// * The IteratedNoder can throw topology exceptions if it doesn't converge by MAX_ITERS | ||
/// * The SnappingNoder doesn't handle isolated POINTs | ||
unsafe fn node( | ||
geoms: &GeometryCollectionShim, | ||
tolerance: f64, | ||
) -> UniquePtr<GeometryGraphShim>; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::cxxbridge::{CoordShim, GraphEdge}; | ||
|
||
#[test] | ||
fn test_compile_tester() { | ||
let collection = geo::GeometryCollection::default(); | ||
let collection = crate::cxxbridge::GeometryCollectionShim(collection); | ||
ffi::_compile_tester(&collection); | ||
fn test_noder_doesnt_crash() { | ||
let empty = crate::cxxbridge::GeometryCollectionShim::new(Vec::new()); | ||
let tolerance = 0.001; | ||
let graph = unsafe { ffi::node(&empty, tolerance) }; | ||
|
||
assert!(!graph.is_null()); | ||
} | ||
|
||
#[test] | ||
fn test_noder_isolated_points() { | ||
let geoms = [geo::Point::new(0.0, 0.0), geo::Point::new(0.0, 1.0)]; | ||
let geoms: Vec<_> = geoms.into_iter().map(geo::Geometry::Point).collect(); | ||
let geoms = crate::cxxbridge::GeometryCollectionShim::new(geoms); | ||
|
||
let tolerance = 0.0; | ||
let graph = unsafe { ffi::node(&geoms, tolerance) }; | ||
assert!(!graph.is_null()); | ||
|
||
let nodes = graph.nodes(); | ||
let expected = [CoordShim { x: 0.0, y: 0.0 }, CoordShim { x: 0.0, y: 1.0 }]; | ||
assert_eq!(nodes, expected); | ||
|
||
let edges = graph.edges(); | ||
assert!(edges.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_noder_linestring() { | ||
let line: geo::LineString = vec![(0.0, 0.0), (0.00001, 0.0), (2.0, 0.0)].into(); | ||
let geoms = vec![geo::Geometry::LineString(line)]; | ||
let geoms = crate::cxxbridge::GeometryCollectionShim::new(geoms); | ||
|
||
let tolerance = 0.0001; | ||
let graph = unsafe { ffi::node(&geoms, tolerance) }; | ||
assert!(!graph.is_null()); | ||
|
||
let nodes = graph.nodes(); | ||
let expected = [CoordShim { x: 0.0, y: 0.0 }, CoordShim { x: 2.0, y: 0.0 }]; | ||
assert_eq!(nodes, expected); | ||
|
||
let edges = graph.edges(); | ||
let expected = [GraphEdge { src: 0, dst: 1 }]; | ||
assert_eq!(edges, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters