-
Notifications
You must be signed in to change notification settings - Fork 4
/
step_3.R
66 lines (58 loc) · 2.34 KB
/
step_3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
source('merge.options.R')
step_3 <- function(df, step_2_res){
subs <- step_2_res[[1]]
adjacent <- step_2_res[[2]] %>% do.call(rbind,.)
adjacent <- adjacent[adjacent$Line != 0, ]
adjacent <- adjacent[order(adjacent$Line),2:1]
adjacent.list <- lapply(1:length(subs), function(x){
lins <- adjacent[adjacent$Polygon== x,1]
adjacent[adjacent$Line %in% lins & adjacent$Polygon != x,2]
})
subs.numbers <- subs.vertices <- subs.widths <- list()
for (n in 1:length(subs)) {
if (n == 1) {
subs.vertices[[1]] <- subs
subs.numbers[[1]] <- 1:length(subs) %>% as.list()
subs.widths[[1]] <- lapply(subs, function(x){width.function(x,F)[[2]]})
}else{
result.vertices <- list()
result.numbers <- list()
for (m in floor(n/2)) {
# print(c(round(n/length(subs),4), m))
if (m > length(subs.numbers) | (n-m)> length(subs.numbers)) {
next
}
if (is.null(subs.numbers[[n-m]])) {
next
}
if (is.null(subs.numbers[[m]])) {
next
}
temp <- merge.options(m,n-m,subs.numbers, subs.vertices, adjacent.list)
result.vertices <- list(result.vertices, temp[[1]]) %>% unlist(recursive = F)
result.numbers[[m]] <- temp[[2]]
}
if (is.null(result.numbers)| length(result.numbers) == 0) {
subs.vertices[[n]] <- NULL
subs.numbers[[n]] <- NULL
subs.widths[[n]] <- NULL
}else{
result.numbers <- result.numbers %>% do.call(rbind,.)
result.numbers <- result.numbers %>% apply(1, sort) %>% t()
result.vertices <- result.vertices[!(result.numbers %>% duplicated())]
result.numbers <- result.numbers[!(result.numbers %>% duplicated()),]
if (is.null(nrow(result.numbers))) {
subs.vertices[[n]] <- result.vertices
subs.numbers[[n]] <- list(result.numbers)
subs.widths[[n]] <- lapply(result.vertices, function(x){width.function(x,F)[[2]]})
}else{
result.numbers <- result.numbers %>% t() %>% as.data.frame(optional = T) %>% as.list.data.frame()
subs.vertices[[n]] <- result.vertices
subs.numbers[[n]] <- result.numbers
subs.widths[[n]] <- lapply(result.vertices, function(x){width.function(x,F)[[2]]})
}
}
}
}
return(list(subs.vertices, subs.numbers, subs.widths))
}