Skip to content

Commit

Permalink
Refactor remaining functions
Browse files Browse the repository at this point in the history
As indicated in #32 (review)
  • Loading branch information
cforgaci committed Dec 2, 2024
1 parent 0083463 commit ef9334e
Showing 1 changed file with 53 additions and 57 deletions.
110 changes: 53 additions & 57 deletions R/stroke.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,43 +110,49 @@ get_links <- function(segments) {
return(links)
}

#' @noRd
get_linked_segments <- function(segment_id, node_id, links) {
# find the segments connected to the given one via the given node
# 1. find all segments connected to the node
segs <- links[[node_id]]
# 2. exclude the given segment from the list
is_current_segment <- segs == segment_id
linked_segments <- segs[!is_current_segment]
return(linked_segments)
}

#' @noRd
get_linked_nodes <- function(node_id, segment_id, segments) {
# find the node connected to the given one via the given segment(s)
# 1. get the nodes that are part of the given segment(s)
nds <- segments[segment_id, ]
# 2. flatten the array row by row (i.e. along the node dimension)
nds <- as.vector(t(nds))
# 3. exclude the given node from the list
is_current_node <- nds %in% node_id
linked_nodes <- nds[!is_current_node]
return(linked_nodes)
}

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

Check warning on line 116 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=116,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
get_linked_segments <- function(segment_id, node_id) {
# find the segments connected to the given one via the given node
# 1. find all segments connected to the node
segs <- links[[node_id]]
# 2. exclude the given segment from the list
is_current_segment <- segs == segment_id
linked_segments <- segs[!is_current_segment]
return(linked_segments)
}

Check warning on line 126 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=126,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
get_linked_nodes <- function(node_id, segment_id) {
# find the node connected to the given one via the given segment(s)
# 1. get the nodes that are part of the given segment(s)
nds <- segments[segment_id, ]
# 2. flatten the array row by row (i.e. along the node dimension)
nds <- as.vector(t(nds))
# 3. exclude the given node from the list
is_current_node <- nds %in% node_id
linked_nodes <- nds[!is_current_node]
return(linked_nodes)
}

Check warning on line 137 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=137,col=4,[trailing_whitespace_linter] Trailing whitespace is superfluous.

Check warning on line 138 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=138,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
find_best_link <- function(node, opposite_node, current_segment) {
linked_segs <- get_linked_segments(current_segment, node, links)
linked_segs <- get_linked_segments(current_segment, node)

Check warning on line 141 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=141,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
get_link_on_same_edge <- function(current_segment, edge_ids) {
is_same_edge <- edge_ids[linked_segs] == edge_ids[current_segment]

Check warning on line 143 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=143,col=8,[indentation_linter] Indentation should be 6 spaces but is 8 spaces.
link_on_same_edge <- linked_segs[is_same_edge]
return(link_on_same_edge)
}

Check warning on line 147 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=147,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# if in flow mode, we look for a link on the same edge
if (flow_mode) {
best_link <- get_link_on_same_edge(linked_segs, current_segment, edge_ids)
best_link <- get_link_on_same_edge(current_segment, edge_ids)
}
# if not in flow mode or if no link is found on the same edge, we look for
# the best link by calculating the interior angles with all connections
if (length(best_link) == 0 || !flow_mode) {
linked_nodes <- get_linked_nodes(node, linked_segs, segments)
linked_nodes <- get_linked_nodes(node, linked_segs)
angles <- interior_angle(nodes[node, ],
nodes[opposite_node, , drop = FALSE],
nodes[linked_nodes, , drop = FALSE])
Expand All @@ -173,13 +179,6 @@ best_link <- function(nodes, segments, links, edge_ids, flow_mode,
return(best_links)
}

#' @noRd
get_link_on_same_edge <- function(links, current_segment, edge_ids) {
is_same_edge <- edge_ids[links] == edge_ids[current_segment]
link_on_same_edge <- links[is_same_edge]
return(link_on_same_edge)
}

#' @noRd
interior_angle <- function(v, p1, p2) {
# compute convex angle between three points:
Expand Down Expand Up @@ -236,27 +235,6 @@ cross_check_links <- function(best_links) {
return(links)
}

#' @noRd
get_next <- function(node, link, segments, links) {
# find the node and segment connected to the current ones via the given link
# 1. get the nodes and segments connected to the given link
nodes <- segments[link, ]
segs <- links[link, ]
# 2. identify the position of the current node in the arrays (the current
# segment will be in the same position
is_current <- nodes == node
# 3. exclude the current node and segment from the respective lists to find
# the new elements
return(list(node = nodes[!is_current], link = segs[!is_current]))
}

#' @noRd
to_linestring <- function(node_id, nodes) {
points <- nodes[node_id, ]
linestring <- sfheaders::sfc_linestring(points, x = "x", y = "y")
return(linestring)
}

merge_lines <- function(nodes, segments, links, edge_ids,
from_edge = NULL, attributes = FALSE, crs = NULL) {
is_segment_used <- array(FALSE, dim = nrow(segments))
Expand All @@ -272,11 +250,23 @@ merge_lines <- function(nodes, segments, links, edge_ids,
}

traverse_segments <- function(node, link, stroke_label) {
get_next <- function() {
# find the node and segment connected to the current ones via the given link

Check warning on line 254 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=254,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.
# 1. get the nodes and segments connected to the given link
nodes <- segments[link, ]
segs <- links[link, ]
# 2. identify the position of the current node in the arrays (the current
# segment will be in the same position
is_current <- nodes == node
# 3. exclude the current node and segment from the respective lists to find

Check warning on line 261 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=261,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 81 characters.
# the new elements
return(list(node = nodes[!is_current], link = segs[!is_current]))
}
stroke <- c()
while (TRUE) {
if (is.na(link) || (is_segment_used[link] && !can_reuse_segments)) break
stroke_labels[edge_ids[link]] <- stroke_label
new <- get_next(node, link, segments, links)
new <- get_next()
is_segment_used[link] <- TRUE
node <- new$node
link <- new$link
Expand All @@ -285,6 +275,12 @@ merge_lines <- function(nodes, segments, links, edge_ids,
return(list(stroke = stroke, is_segment_used = is_segment_used,
stroke_labels = stroke_labels))
}

Check warning on line 278 in R/stroke.R

View workflow job for this annotation

GitHub Actions / lint

file=R/stroke.R,line=278,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
to_linestring <- function(node_id) {
points <- nodes[node_id, ]
linestring <- sfheaders::sfc_linestring(points, x = "x", y = "y")
return(linestring)
}

istroke <- 1
for (iseg in segment_ids) {
Expand Down Expand Up @@ -312,7 +308,7 @@ merge_lines <- function(nodes, segments, links, edge_ids,

# combine strokes and add to results
stroke <- c(forward_stroke, stroke, backward_stroke)
strokes <- c(strokes, to_linestring(stroke, nodes))
strokes <- c(strokes, to_linestring(stroke))
istroke <- istroke + 1
}

Expand Down

0 comments on commit ef9334e

Please sign in to comment.