# torus_demo

    library(ashapesampler)
    library(alphashape3d)
    #> Loading required package: geometry
    library(alphahull)
    library(doParallel)
    library(parallel)
    cores <- min(2L, detectCores())
    library(rgl)
    options(rgl.useNULL = TRUE)

In this document, we demonstrate the \(\alpha\)-shape sampler pipeline by simulating the process of learning a set of three-dimensional shapes (in this case, tori) and simulating a new shape from that. This vignette requires the packages `alphashape3d`, `alphahull`, `rgl`, `parallel`, and `doParallel` in addition to `ashapesampler`.

Within vignettes, we are unable to set the number of cores above 2L. However, we highly recommend using as many cores on the machine as possible to parallelize the code and expedite computation, especially with 3D calculations.

We begin by setting the parameters for our simulation. We will fix \(\alpha=0.15\) and \(n=1000\), and draw 20 shapes for our data set. Our true underlying manifold will be the torus with major radius 0.5 and minor radius 0.15.

    N=10
    tau_vec <- vector("numeric", N)
    my_alpha <- 0.15
    n <- 1000
    r_maj <- 0.5
    r_min <- 0.15

Next we will draw the shapes themselves.

    torus_list <- list()
    complex_torus_list <- list()

    for (k in 1:N){
      torus_pts <- rtorus(n, r_min, r_maj)
      temp_torus <- ashape3d(torus_pts, my_alpha)
      torus_list[[k]] <- temp_torus
      complex_torus_list[[k]] <- get_alpha_complex(torus_pts, my_alpha)
      tau_vec[k] <- tau_bound(torus_list[[k]]$x, complex_torus_list[[k]])
    }

Now that we have the shapes generated and imported, we can learn the underlying manifold. First we will put together the entire point cloud into one two column matrix.

    choose_2 <- sample(N,2)
    point_cloud = rbind(torus_list[[choose_2[1]]]$x, torus_list[[choose_2[[2]]]]$x)

Then we will have our \(\tau\) bound be a summary statistic of the \(\tau\) found for each input shape. Here, we will use mean, but one can tweak this to see different results. Note that if \(\tau\) is too small, then the random walk won’t be able to execute around the point cloud, but if \(\tau\) is too big, then we risk losing geometric and topological information in the reconstruction.

    tau = min(tau_vec[choose_2[1]], tau_vec[choose_2[2]])

Now we can take the parameters and generate a new shape and plot it. Note we assume `k_min=3` as we are in three dimensions.

    new_torus <- generate_ashape3d(point_cloud, J=2, tau=tau, cores=cores)
    #> [1] "Acceptance Rate is 0.853"
    plot(new_torus, indexAlpha="all")
    #> Device  1  : alpha =  0.2459555
    rglwidget()