|
| 1 | +cpp_simplify<-function(Graph,keep=NULL,new_edges=FALSE,rm_loop=TRUE,iterate=FALSE,silent=TRUE){ |
| 2 | + |
| 3 | + #Nodes to keep |
| 4 | + to_keep<-rep(0,Graph$nbnode) |
| 5 | + if (!is.null(keep)) { |
| 6 | + to_keep[Graph$dict$ref %in% keep]<-1 |
| 7 | + } |
| 8 | + |
| 9 | + |
| 10 | + simp<-Simplify2(Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,loop=rm_loop,keep = to_keep,dict = Graph$dict$ref) |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + if (new_edges==TRUE)edges<-list(simp[[3]]) |
| 15 | + else edges<-NULL |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + #Because removing nodes can create other nodes to remove |
| 21 | + counter<-1 |
| 22 | + while(iterate==TRUE){ |
| 23 | + if (counter==1 & silent==FALSE) message(paste(" iteration :",counter,"-",Graph$nbnode-simp[[2]],"nodes removed")) |
| 24 | + |
| 25 | + if (simp[[2]]==Graph$nbnode) break |
| 26 | + count<-simp[[2]] |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + rd<-Remove_duplicate(simp[[1]][,1],simp[[1]][,2],simp[[1]][,3],Graph$nbnode) |
| 31 | + simp<-Simplify2(rd[,1],rd[,2],rd[,3],Graph$nbnode,loop=rm_loop,keep = to_keep,dict = Graph$dict$ref) |
| 32 | + |
| 33 | + counter<-counter+1 |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + if (count == simp[[2]]) break |
| 39 | + |
| 40 | + if(silent==FALSE) message(paste(" iteration :",counter,"-",count-simp[[2]],"nodes removed")) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + if (new_edges==TRUE) edges[[length(edges)+1]]<-simp[[3]] |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + rd<-Remove_duplicate(simp[[1]][,1],simp[[1]][,2],simp[[1]][,3],Graph$nbnode) |
| 49 | + |
| 50 | + |
| 51 | + simp<-rd |
| 52 | + if (nrow(simp)==0) stop("All nodes have been removed") |
| 53 | + |
| 54 | + Nodes=unique(c(simp[,1],simp[,2])) |
| 55 | + |
| 56 | + |
| 57 | + dict<-Graph$dict[Graph$dict$id %in% Nodes,] |
| 58 | + dict$idnew<-0:(nrow(dict)-1) |
| 59 | + simp[,1]<-dict$idnew[match(simp[,1],dict$id)] |
| 60 | + simp[,2]<-dict$idnew[match(simp[,2],dict$id)] |
| 61 | + simp<-as.data.frame(simp) |
| 62 | + simp[,1]<-as.integer(simp[,1]) |
| 63 | + simp[,2]<-as.integer(simp[,2]) |
| 64 | + colnames(simp)<-c("from","to","dist") |
| 65 | + if (!is.null(Graph$coords)){ |
| 66 | + coords<-Graph$coords |
| 67 | + coords<-coords[match(dict$id,Graph$dict$id),] |
| 68 | + } |
| 69 | + else coords=NULL |
| 70 | + |
| 71 | + dict<-dict[,-2] |
| 72 | + colnames(dict)<-c("ref","id") |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + return(list(graph=list(data=simp, |
| 78 | + coords=coords, |
| 79 | + nbnode=length(Nodes), |
| 80 | + dict=dict), |
| 81 | + new_edges=edges)) |
| 82 | + |
| 83 | + } |
0 commit comments