---
title: "elife-71858_executable"
output: html_document
date: '2022-04-15'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(include = FALSE)
options(rgl.useNULL = TRUE) # Suppress the separate window.
```

```{r, init}
library(natverse)
library(tidyverse)
library(RColorBrewer)

knitr::knit_hooks$set(webgl = hook_webgl) #new hook setup

conn=catmaid_login(server="https://fafb.catmaid.virtualflybrain.org") #server info
```


```{r functions}
#' Rigid/Euclidean transform neurons via PCA matrix
#' @param n nat neuronlist 
#' @param Rm rotation matrix
#' @param Tm center as new origin
#'
#' @return neuronlist in the rotated and recentered coordinate
xEucl_neu <- function(n, Rm, Tm){
  n_xform <- n
  for (j in 1:length(n)) {
    xyz <- n[[j]]$d[, c("X","Y","Z")]
    xyz_xform <- sweep(as.matrix(xyz), 2, Tm, '-') %*% Rm
    n_xform[[j]]$d[, c("X","Y","Z")] <- xyz_xform
  }
  return(n_xform)
}

```

```{r, plot-setup}
pal_syn <- brewer.pal(9,"Set1")[c(4,3)]
names(pal_syn) <- c("R7","R8")
xAng <- -105 #rotation angle around x-axis
```

1. Choose the skeleton ids (skids) of interest (Not run)
```{r eval=FALSE, echo=TRUE}
skid = c() #manual input skeleton ids of interest
neu = read.neurons.catmaid(skids = skid, .progress='text', conn = conn) #read neuron from server
```

```{r load saved data}
load('data_exec.rda')
# me_pca -- used for transforming to central-eye coordinate
# cp1, cp2, cy1, cy2
# layers_side
# skid_pR7, skid_yR7, skid_pR8, skid_yR8

load('data_exec_temp.rda')
# neu
# skid
# p/y R7/8
```

```{r transform}
pR7_xform <-  xEucl_neu(pR7, me_pca$rotation, me_pca$center)
pR8_xform <-  xEucl_neu(pR8, me_pca$rotation, me_pca$center)
yR7_xform <-  xEucl_neu(yR7, me_pca$rotation, me_pca$center)
yR8_xform <-  xEucl_neu(yR8, me_pca$rotation, me_pca$center)
neu_xform <- xEucl_neu(neu, me_pca$rotation, me_pca$center)
```

2. 3D interactive plots
```{r, webgl=TRUE, include=TRUE}
# nopen3d()
ii <- 1
plot3d(neu_xform[[ii]], col='black', lwd=2, soma = T, WithNodes = F, lit=F) 
plot3d(pR8_xform[[1]], lwd=3, col=pal_syn['R8'])
shade3d(cp1, col='#9ecae1', alpha = .1, lit=F)
shade3d(cp2, col='#9ecae1', alpha = .1, lit=F)
shade3d(cy1, col='#fec44f', alpha = .1, lit=F)
shade3d(cy2, col='#fec44f', alpha = .1, lit=F)
segments3d(layers_side, lwd=1)
rgl.viewpoint(fov=0,zoom=0.4, userMatrix= rotationMatrix(90/180*pi,0,0,1) %*% rotationMatrix(xAng/180*pi,1,0,0))


# rglwidget()
```

```{r, eval=FALSE}
# # separate R7 and R8 input
# conn_in_R7 <- catmaid_get_connectors_between(pre_skids = c(skid_pR7,skid_yR7), post_skids = skid[ii])
# if (!is.null(conn_in_R7)) {
#   conn_in_R7 <- conn_in_R7[, c("post_node_x", "post_node_y", "post_node_z")]
#   conn_in_R7_xform <- sweep(as.matrix(conn_in_R7), 2, me_pca$center) %*% me_pca$rotation
#   pch3d(conn_in_R7_xform, pch=16, radius=1000, alpha=0.7, col=pal_syn['R7'])
# }
# conn_in_R8 <- catmaid_get_connectors_between(pre_skids = c(skid_pR8, skid_yR8), post_skids = skid[ii])
# if (!is.null(conn_in_R8)) {
#   conn_in_R8 <- conn_in_R8[, c("post_node_x", "post_node_y", "post_node_z")]
#   conn_in_R8_xform <- sweep(as.matrix(conn_in_R8), 2, me_pca$center) %*% me_pca$rotation
#   pch3d(conn_in_R8_xform, pch=16, radius=1000, alpha=0.8, col=pal_syn['R8'])
# }
```