Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement flow mode #22

Merged
merged 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions R/stroke.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
}

if (attributes) stop("attribute mode not implemented.")
if (flow_mode) stop("flow mode not implemented.")
if (!is.null(from_edge) && (attributes || flow_mode)) {
stop("from_edge is not compatible with attributes or flow_mode")
}
Expand All @@ -61,11 +60,13 @@
links <- get_links(segments)

# calculate interior angles between segment pairs, identify best links
best_links <- best_link(nodes, segments, links, angle_threshold)
best_links <- best_link(
nodes, segments, links, edge_ids, flow_mode, angle_threshold
)

if (is.null(from_edge)) {
# verify that the best links identified fulfill input requirements
final_links <- cross_check_links(best_links, flow_mode)
final_links <- cross_check_links(best_links)
segments_ids <- seq_len(nrow(segments))

} else {
Expand All @@ -88,7 +89,7 @@
#' Find unique nodes and label them with IDs
#' @noRd
unique_nodes <- function(edge_points) {
nodes <- dplyr::distinct(edge_points, x, y)

Check warning on line 92 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=92,col=41,[object_usage_linter] no visible binding for global variable 'x'

Check warning on line 92 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=92,col=44,[object_usage_linter] no visible binding for global variable 'y'
nodes$node_id <- seq_len(nrow(nodes))
return(nodes)
}
Expand All @@ -114,7 +115,7 @@
get_links <- function(segments) {
nsegments <- nrow(segments)
links <- data.frame(node_id = as.vector(segments)) |>
dplyr::group_by(node_id) |>

Check warning on line 118 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=118,col=21,[object_usage_linter] no visible binding for global variable 'node_id'
dplyr::group_rows() |>
lapply(function(x) (x - 1) %% nsegments + 1)
return(links)
Expand Down Expand Up @@ -145,7 +146,9 @@
}

#' @noRd
best_link <- function(nodes, segments, links, angle_threshold = 0) {
best_link <- function(
nodes, segments, links, edge_ids, flow_mode, angle_threshold = 0
) {

# convert nodes to a matrix for faster indexing
nodes <- as.matrix(nodes[c("x", "y")])
Expand All @@ -158,23 +161,45 @@
for (iseg in seq_len(nrow(segments))) {
start_node <- segments[iseg, "start"]
end_node <- segments[iseg, "end"]
edge_id <- edge_ids[iseg]

# find angles formed with all segments linked at start point
linked_segs <- get_linked_segments(iseg, start_node, links)
linked_nodes <- get_linked_nodes(start_node, linked_segs, segments)
angles <- interior_angle(nodes[start_node, ],
nodes[end_node, , drop = FALSE],
nodes[linked_nodes, , drop = FALSE])
best_link <- get_best_link(angles, linked_segs, angle_threshold_rad)

# if flow_mode, we choose the link on the same edge
if (flow_mode) {
best_link <- get_link_on_same_edge(linked_segs, edge_ids, edge_id)
}
fnattino marked this conversation as resolved.
Show resolved Hide resolved

# if not flow_mode or no link on the same edge, we calculate the angle
if (length(best_link) == 0 || !flow_mode) {
linked_nodes <- get_linked_nodes(start_node, linked_segs, segments)
angles <- interior_angle(nodes[start_node, ],
nodes[end_node, , drop = FALSE],
nodes[linked_nodes, , drop = FALSE])
best_link <- get_best_link(angles, linked_segs, angle_threshold_rad)
}

if (length(best_link) > 0) best_links[iseg, "start"] <- best_link

# find angles formed with all segments linked at end point
linked_segs <- get_linked_segments(iseg, end_node, links)
linked_nodes <- get_linked_nodes(end_node, linked_segs, segments)
angles <- interior_angle(nodes[end_node, ],
nodes[start_node, , drop = FALSE],
nodes[linked_nodes, , drop = FALSE])
best_link <- get_best_link(angles, linked_segs, angle_threshold_rad)

# if flow_mode, we choose the link on the same edge
if (flow_mode) {
best_link <- get_link_on_same_edge(linked_segs, edge_ids, edge_id)
}

# if not flow_mode or no link on the same edge, we calculate the angle
if (length(best_link) == 0 || !flow_mode) {
linked_nodes <- get_linked_nodes(end_node, linked_segs, segments)
angles <- interior_angle(nodes[end_node, ],
nodes[start_node, , drop = FALSE],
nodes[linked_nodes, , drop = FALSE])
best_link <- get_best_link(angles, linked_segs, angle_threshold_rad)
}


if (length(best_link) > 0) best_links[iseg, "end"] <- best_link
}
return(best_links)
Expand Down Expand Up @@ -207,7 +232,14 @@
}

#' @noRd
cross_check_links <- function(best_links, flow_mode = FALSE) {
get_link_on_same_edge <- function(links, edge_ids, edge_id) {
is_same_edge <- edge_ids[links] == edge_id
link_on_same_edge <- links[is_same_edge]
return(link_on_same_edge)
}

#' @noRd
cross_check_links <- function(best_links) {

links <- array(integer(), dim = dim(best_links))
colnames(links) <- c("start", "end")
Expand Down Expand Up @@ -253,7 +285,7 @@
}

#' @noRd
merge_lines <- function(

Check warning on line 288 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=288,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 21.
nodes, segments, links, segments_ids, from_edge = NULL
) {
is_segment_used <- array(FALSE, dim = nrow(segments))
Expand Down
12 changes: 7 additions & 5 deletions tests/testthat/test-stroke.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,20 @@ test_that("attributes cannot be returned if not in flow mode", {
})

test_that("edges can be split if flow_mode is false", {
l1 <- sf::st_linestring(c(p1, p2, p5))
sfc <- sf::st_sfc(l1, l2)
new_l1 <- sf::st_linestring(c(p1, p2, p5))
sfc <- sf::st_sfc(new_l1, l2)
expected <- sf::st_sfc(sf::st_linestring(c(p1, p2, p3)),
sf::st_linestring(c(p2, p5)))
actual <- stroke(sfc, flow_mode = FALSE)
expect_setequal(actual, expected)
})

test_that("edges are not split if flow_mode is true", {
skip(message = "flow mode to be implemented")
l1 <- sf::st_linestring(c(p1, p2, p5))
sfc <- sf::st_sfc(l1, l2)
new_l1 <- sf::st_linestring(c(p1, p2, p5))
sfc <- sf::st_sfc(new_l1, l2)
# p1 - p2 - p3
# \
# p5
expected <- sfc
actual <- stroke(sfc, flow_mode = TRUE)
expect_setequal(actual, expected)
Expand Down
Loading