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

speed up TRI0 for a path-type #93

Closed
mdsumner opened this issue May 7, 2019 · 0 comments
Closed

speed up TRI0 for a path-type #93

mdsumner opened this issue May 7, 2019 · 0 comments

Comments

@mdsumner
Copy link
Member

mdsumner commented May 7, 2019

This is a draft replacement for the triangulate_0 core:

x <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf", mustWork = TRUE))
#x <- sf::st_sf(a = 1, geom = sf::st_sfc(sfzoo[[6]]))
library(silicate)
library(dplyr)
tri_faster <- function(x) {
  p <- PATH0(x)
  #pp <- tidyr::unnest(p$object["path_"], cols = c(path_)) %>% 
  pp <-  
    dplyr::transmute(dplyr::bind_rows(p[["object"]][["path_"]]), 
                     vertex_, object_, path_, tri_object = paste(object_, subobject, sep = "-"))
  path <- gibble::gibble(x)
  path$tri_object <- paste(path$object, path$subobject, sep = "-")
  ## have object
  ## we are subobject
  usub <- unique(pp$tri_object)
  coord <- p$vertex[pp$vertex_, ]
  trilist <- vector("list", length(usub)) 
  vertsbefore <- 0
  
  for (i in seq_along(usub)) {
    vertsnow <- path$nrow[path$tri_object == usub[i]]
    rowindex <- (vertsbefore + 1):(vertsbefore + sum(vertsnow))
    verts <- coord[rowindex, ]
    holes <- 0
    if (length(vertsnow) > 1) holes <- head(cumsum(vertsnow), -1L) + 1
    
    trindex <- decido::earcut(cbind(verts[["x_"]], verts[["y_"]]), holes)
    trimat <- matrix(trindex, ncol = 3L, byrow = TRUE)
    windex <- pp$vertex_[rowindex]
    #browser()
    trilist[[i]] <- tibble::tibble(.vx0 = pp$vertex_[windex[trimat[,1L]]],
                                   .vx1 = pp$vertex_[windex[trimat[,2L]]],
                                   .vx2 = pp$vertex_[windex[trimat[,3L]]],
                                   #path_ = pp$path_[rowindex],
                                   object_ = pp$object_[rowindex][1])
    
    
    vertsbefore <- vertsbefore + sum(vertsnow)
  }
  bind_rows(trilist)
  
}


system.time({
  tri <- tri_faster(x)
coord <- sc_coord(x)
plot(coord, pch = ".")
polygon(coord[rbind(tri$.vx0, tri$.vx1, tri$.vx2, NA), ])
})

 user  system elapsed 
  0.250   0.004   0.254 

library(quadmesh)
## the thing we want sped up
system.time(mesh_plot(TRI0(x)))
 user  system elapsed 
  0.970   0.016   0.986 

The key parts are maintaining the link between vertex and coordinate index (done with tortuous joining previously, and direct bind_rows on the object$path_ rather than unnesting).

Looks about 10x faster - motivation is to make mesh_plot(TRI0(x)) much faster.

@mdsumner mdsumner changed the title speed up TRI0 speed up TRI0 for a path-type May 7, 2019
@mdsumner mdsumner mentioned this issue Oct 3, 2019
@mdsumner mdsumner closed this as completed Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant