---
title: "Spacekime Analytics (Time Complexity and Inferential Uncertainty)"
subtitle: "TCIU: Interactive Kime-Surface Parametric Plot"
author: "SOCR Team"
date: "`r format(Sys.time(),'%m/%d/%Y')`"
output: 
  html_document:
    theme: spacelab
    highlight: tango
    includes:
      before_body: TCIU_header.html
  fig_width: 8 
  fig_height: 8 
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r error=F, message=F, warning=F, echo=F}
library(plotly)
phi <- seq(from = 0, to = 2*pi, length.out = 200)
theta <- seq(from = 0, to = pi/3, length.out = 200)

r1 = 1 + sin(5*phi) %o% (sin(7*theta)/5) # 2 + sin(3 * phi + 5 * theta)
x1 = r1 * (cos(phi) %o% sin(theta))    # x = r*cos(phi)*sin(theta)
y1 = r1 * (sin(phi) %o% sin(theta))     # y = r*sin(phi)*sin(theta)
z1 = r1 * (cos(theta) %o% rep(1,200))  # z = r*cos(theta)
surf_color1= phi %o% rep(1,200)
colorscale = cbind(seq(0, 1, by=1/(length(phi) - 1)), rainbow(length(phi)))

# Add Angular and Radial (polar-mesh) traces
# phi <- seq(from = 0, to = 2*pi, length.out = 10)
# theta <- seq(from = 0, to = pi/3, length.out = 8)
r = 1 + sin(5*phi) %o% (sin(7*theta)/5) # 2 + sin(3 * phi + 5 * theta)
x = r * (cos(phi) %o% sin(theta))    # x = r*cos(phi)*sin(theta)
y = r * (sin(phi) %o% sin(theta))     # y = r*sin(phi)*sin(theta)
z = r * (cos(theta) %o% rep(1,200))  # z = r*cos(theta)

# Radial (Time) mesh curves
count <- 50
phi <- seq(from = 0, to = 2*pi, length.out = count)
theta <- seq(from = 0, to = pi/3, length.out = count)
x <- array(1, dim=c(count*count))
y <- array(1, dim=c(count*count))
z <- array(1, dim=c(count*count))
r <- array(1, dim=c(count*count))
opaque <-  array(1, dim=c(count*count))

for (i in 1:count) {  # phi
  for (j in 1:count) { # theta
    r[(i-1)*count+j] <- 1 + sin(5*phi[i]) * (sin(7*theta[j])/5)
    x[(i-1)*count+j] <- r[(i-1)*count+j] * (cos(phi[i]) * sin(theta[j]))
    y[(i-1)*count+j] <- r[(i-1)*count+j] * (sin(phi[i]) * sin(theta[j]))
    z[(i-1)*count+j] <- r[(i-1)*count+j] * cos(theta[j])
    opaque[(i-1)*count+j] <- ifelse(j==10, 0, 1)
  }
}

# Angular (Phase) mesh curves
count <- 50 # theta
phi <- seq(from = 0, to = 2*pi, length.out = count)
theta <- seq(from = 0, to = pi/3, length.out = count)
x2 <- array(1, dim=c(count*count))
y2 <- array(1, dim=c(count*count))
z2 <- array(1, dim=c(count*count))
r2 <- array(1, dim=c(count*count))
for (i in 1:count) {  # phi
  for (j in 1:count) { # theta
    r2[(i-1)*count+j] <- 1 + sin(5*phi[i]) * (sin(7*theta[j])/5)
    x2[(i-1)*count+j] <- r2[(i-1)*count+j] * (cos(phi[i]) * sin(theta[j]))
    y2[(i-1)*count+j] <- r2[(i-1)*count+j] * (sin(phi[i]) * sin(theta[j]))
    z2[(i-1)*count+j] <- r2[(i-1)*count+j] * cos(theta[j])
  }
}
# length(x2); length(y2); length(r2); dim(z2)
phase_seq5 <- seq(from=25, to=count^2, by=count)  # phase mesh indexing patterns
phase_seq15 <- seq(from=40, to=count^2, by=count)
epsilon=0.03   # slight offset (up shift) of contour meshes

p <- plot_ly(hoverinfo="none", showscale = FALSE) %>%
    add_trace(x = ~x1, y = ~y1, z = ~r1,
              surfacecolor=~surf_color1, colorscale=colorscale, #Phase-based color
              type = 'surface', opacity=1, visible=T,
              contour=list(x = list(highlight = FALSE),
                           y = list(highlight = FALSE),
                           z = list( highlight = TRUE, highlightcolor = "blue"),
                           color="#000", width=15, lwd=10,
                           opacity=1.0, hoverinfo="none")) %>%
    # Add a few Radial Traces
    add_trace(x = x[(count+1):(2*count)], y = y[(count+1):(2*count)], 
              z = r[(count+1):(2*count)]+epsilon, 
            type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
            line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
    add_trace(x = x[(10*count+1):(11*count)], y = y[(10*count+1):(11*count)], 
              z = r[(10*count+1):(11*count)]+epsilon, 
            type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
            line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
    add_trace(x = x[(32*count+1):(33*count)], y = y[(32*count+1):(33*count)], 
              z = r[(32*count+1):(33*count)]+epsilon, 
            type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
            line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
    
    # Add a few Phase (Angular) Traces
    add_trace(x = x2[phase_seq5], y = y2[phase_seq5], z = r2[phase_seq5]+epsilon, 
            type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
            line = list(color="darkgray", width=7, lwd=7), name = 'Phase') %>%
    add_trace(x = x2[phase_seq15], y = y2[phase_seq15], 
              z = r2[phase_seq15]+epsilon, 
            type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
            line = list(color="darkgray", width=7, lwd=7), name = 'Phase') %>%
  
    # Configure the scene layout
    layout(showlegend = FALSE,
           scene = list(aspectmode = "manual", 
                        aspectratio = list(x=1, y=1, z=0.3))) # 1:1:1 aspect ratio
p

#### For high-resolution kimesurface rendering suppress the cardinal plances and axes
# hide_axes_grid <- list(
#   title = "",
#   zeroline = FALSE,
#   showline = FALSE,
#   showticklabels = FALSE,
#   showgrid = FALSE
# )
# 
# p1 <- plot_ly(hoverinfo="none", showscale = FALSE) %>%
#     add_trace(x = ~x1, y = ~y1, z = ~r1,
#               surfacecolor=~surf_color1, colorscale=colorscale, #Phase-based color
#               type = 'surface', opacity=1, visible=T,
#               contour=list(x = list(highlight = FALSE),
#                            y = list(highlight = FALSE),
#                            z = list( highlight = TRUE, highlightcolor = "blue"),
#                            color="#000", width=15, lwd=10,
#                            opacity=1.0, hoverinfo="none")) %>%
#     # Add a few Radial Traces
#     add_trace(x = x[(count+1):(2*count)], y = y[(count+1):(2*count)], 
#               z = r[(count+1):(2*count)]+epsilon, 
#             type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
#             line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
#     add_trace(x = x[(10*count+1):(11*count)], y = y[(10*count+1):(11*count)], 
#               z = r[(10*count+1):(11*count)]+epsilon, 
#             type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
#             line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
#     add_trace(x = x[(32*count+1):(33*count)], y = y[(32*count+1):(33*count)], 
#               z = r[(32*count+1):(33*count)]+epsilon, 
#             type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
#             line = list(color="#000", width=7, lwd=7), name = 'Time') %>%
#     
#     # Add a few Phase (Angular) Traces
#     add_trace(x = x2[phase_seq5], y = y2[phase_seq5], z = r2[phase_seq5]+epsilon, 
#             type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
#             line = list(color="darkgray", width=7, lwd=7), name = 'Phase') %>%
#     add_trace(x = x2[phase_seq15], y = y2[phase_seq15], 
#               z = r2[phase_seq15]+epsilon, 
#             type = 'scatter3d', mode = 'lines', opacity = 1, # opaque, 
#             line = list(color="darkgray", width=7, lwd=7), name = 'Phase') %>%
#   
#     # Configure the scene layout
#     layout(showlegend = FALSE,
#            scene = list(aspectmode = "manual", 
#                         aspectratio = list(x=1, y=1, z=0.3),
#                         xaxis = hide_axes_grid, yaxis = hide_axes_grid, 
#                         zaxis = hide_axes_grid)
#            ) # 1:1:1 aspect ratio
# p1
```

<!--html_preserve-->
<div>
    	<footer><center>
			<a href="http://www.socr.umich.edu/">SOCR Resource</a>
				Visitor number 
				<img class="statcounter"
								src="https://c.statcounter.com/5714596/0/038e9ac4/0/" 
								alt="Web Analytics" align="middle" border="0">
								
				<script type="text/javascript">
					var d = new Date();
					document.write(" | " + d.getFullYear() + " | ");
				</script> 
				<a href="http://socr.umich.edu/img/SOCR_Email.png"><img alt="SOCR Email"
	 			title="SOCR Email" src="http://socr.umich.edu/img/SOCR_Email.png"
	 			style="border: 0px solid ;"></a>
	 		 </center>
	 	</footer>

	<!-- Start of StatCounter Code -->
		<script type="text/javascript">
			var sc_project=5714596; 
			var sc_invisible=1; 
			var sc_partition=71; 
			var sc_click_stat=1; 
			var sc_security="038e9ac4"; 
		</script>
		
		<script type="text/javascript" src="https://www.statcounter.com/counter/counter.js"></script>
	<!-- End of StatCounter Code -->
	
	<!-- GoogleAnalytics -->
		<script src="https://www.google-analytics.com/urchin.js" type="text/javascript"> </script>
		<script type="text/javascript"> _uacct = "UA-676559-1"; urchinTracker(); </script>
	<!-- End of GoogleAnalytics Code -->
</div>
<!--/html_preserve-->

