library(plotly)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.5.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# ============================================================
# CONCEPTUAL 3D PILOT FARM
# Designated Area + 3-NOP Feeder + Methane Measurement Lab
# ============================================================


# ------------------------------------------------------------
# FARM DIMENSIONS
# ------------------------------------------------------------

farm_width  <- 100
farm_depth  <- 70

# Proposed designated pilot area
pilot_x1 <- 10
pilot_x2 <- 90
pilot_y1 <- 10
pilot_y2 <- 60


# ------------------------------------------------------------
# FUNCTION TO CREATE A 3D BOX
# ------------------------------------------------------------

box3d <- function(x1, x2, y1, y2, z1, z2) {

  x <- c(
    x1, x2, x2, x1,
    x1, x2, x2, x1
  )

  y <- c(
    y1, y1, y2, y2,
    y1, y1, y2, y2
  )

  z <- c(
    z1, z1, z1, z1,
    z2, z2, z2, z2
  )

  list(
    x = x,
    y = y,
    z = z,
    i = c(
      0, 0, 0,
      4, 4, 4,
      0, 0, 3,
      1, 2, 1,
      5, 6, 5
    ),
    j = c(
      1, 2, 3,
      5, 6, 7,
      4, 5, 7,
      2, 3, 6,
      6, 7, 2
    ),
    k = c(
      2, 3, 1,
      6, 7, 5,
      5, 1, 4,
      6, 7, 5,
      5, 4, 3
    )
  )
}


# ------------------------------------------------------------
# DESIGNATED PILOT AREA
# ------------------------------------------------------------

pilot <- box3d(
  pilot_x1,
  pilot_x2,
  pilot_y1,
  pilot_y2,
  0,
  1
)


# ------------------------------------------------------------
# 3-NOP FEEDING BUILDING
# ------------------------------------------------------------

nop_building <- box3d(
  20, 38,
  18, 34,
  1,
  12
)


# ------------------------------------------------------------
# METHANE MEASUREMENT LAB
# ------------------------------------------------------------

methane_lab <- box3d(
  62, 82,
  38, 55,
  1,
  14
)


# ------------------------------------------------------------
# CATTLE AREA
# ------------------------------------------------------------

cattle_area <- box3d(
  42, 58,
  17, 52,
  1,
  3
)


# ------------------------------------------------------------
# CREATE FIGURE
# ------------------------------------------------------------

fig <- plot_ly()


# ------------------------------------------------------------
# DESIGNATED PILOT AREA
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = pilot$x,
    y = pilot$y,
    z = pilot$z,

    i = pilot$i,
    j = pilot$j,
    k = pilot$k,

    type = "mesh3d",

    opacity = 0.20,

    name = "Designated pilot area",

    hovertemplate =
      "<b>DESIGNATED PILOT AREA</b><br>" %+%
      "Proposed area for the methane-reduction pilot" %+%
      "<extra></extra>"
  )
## Warning: <ggplot> %+% x was deprecated in ggplot2 4.0.0.
## ℹ Please use <ggplot> + x instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# ------------------------------------------------------------
# 3-NOP FEEDER
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = nop_building$x,
    y = nop_building$y,
    z = nop_building$z,

    i = nop_building$i,
    j = nop_building$j,
    k = nop_building$k,

    type = "mesh3d",

    opacity = 0.90,

    name = "3-NOP Feed Station",

    hovertemplate =
      "<b>3-NOP FEED STATION</b><br><br>" %+%
      "Proposed facility for preparing and delivering " %+%
      "3-NOP through cattle feed.<br><br>" %+%
      "<b>Purpose:</b> Reduce methane produced during digestion." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# METHANE MEASUREMENT LAB
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = methane_lab$x,
    y = methane_lab$y,
    z = methane_lab$z,

    i = methane_lab$i,
    j = methane_lab$j,
    k = methane_lab$k,

    type = "mesh3d",

    opacity = 0.90,

    name = "Methane Measurement Lab",

    hovertemplate =
      "<b>METHANE MEASUREMENT LAB</b><br><br>" %+%
      "Proposed monitoring and research facility.<br><br>" %+%
      "<b>Purpose:</b> Measure methane emissions and " %+%
      "evaluate whether the 3-NOP intervention is working." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# CATTLE AREA
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = cattle_area$x,
    y = cattle_area$y,
    z = cattle_area$z,

    i = cattle_area$i,
    j = cattle_area$j,
    k = cattle_area$k,

    type = "mesh3d",

    opacity = 0.45,

    name = "Cattle area",

    hovertemplate =
      "<b>CATTLE AREA</b><br>" %+%
      "Existing cattle production area within the conceptual pilot layout." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# BUILDING LABELS
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = c(
      29,
      72
    ),

    y = c(
      26,
      47
    ),

    z = c(
      15,
      17
    ),

    type = "scatter3d",

    mode = "text",

    text = c(
      "<b>3-NOP FEED STATION</b>",
      "<b>METHANE MEASUREMENT LAB</b>"
    ),

    textposition = "middle center",

    showlegend = FALSE,

    hoverinfo = "skip"
  )


# ------------------------------------------------------------
# PILOT AREA LABEL
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = 50,
    y = 8,
    z = 2,

    type = "scatter3d",

    mode = "text",

    text = "DESIGNATED PILOT AREA",

    textposition = "middle center",

    showlegend = FALSE,

    hoverinfo = "skip"
  )


# ------------------------------------------------------------
# 3-NOP → CATTLE ARROW
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = c(38, 42),
    y = c(26, 30),
    z = c(7, 4),

    type = "scatter3d",

    mode = "lines+markers",

    line = list(
      width = 8
    ),

    marker = list(
      size = 5
    ),

    name = "3-NOP feed pathway",

    hovertemplate =
      "<b>3-NOP FEED PATHWAY</b><br>" %+%
      "Proposed delivery of 3-NOP-treated feed to cattle." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# CATTLE → METHANE LAB PATHWAY
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = c(58, 62),
    y = c(35, 46),
    z = c(4, 8),

    type = "scatter3d",

    mode = "lines+markers",

    line = list(
      width = 8
    ),

    marker = list(
      size = 5
    ),

    name = "Methane monitoring pathway",

    hovertemplate =
      "<b>METHANE MONITORING</b><br>" %+%
      "Proposed collection of methane measurements and " %+%
      "transfer of data to the monitoring lab." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# X-AXIS / FARM AREA LABEL
# ------------------------------------------------------------

fig <- fig %>%

  add_trace(

    x = c(0, 100),

    y = c(0, 0),

    z = c(0, 0),

    type = "scatter3d",

    mode = "lines",

    line = list(
      width = 5
    ),

    name = "Farm area",

    hovertemplate =
      "<b>FARM AREA</b><br>" %+%
      "Conceptual dimensions of the visualization." %+%
      "<extra></extra>"
  )


# ------------------------------------------------------------
# FINAL LAYOUT
# ------------------------------------------------------------

fig <- fig %>%

  layout(

    title = list(
      text =
        "Conceptual 3D Design of a Cattle Methane Reduction Pilot"
    ),

    scene = list(

      xaxis = list(
        title = "Farm Area — X Axis",
        range = c(0, 100),
        showgrid = TRUE
      ),

      yaxis = list(
        title = "Farm Area — Y Axis",
        range = c(0, 70),
        showgrid = TRUE
      ),

      zaxis = list(
        title = "Structure Height",
        range = c(0, 20),
        showgrid = TRUE
      ),

      camera = list(
        eye = list(
          x = 1.5,
          y = 1.5,
          z = 1.15
        )
      )
    ),

    legend = list(
      x = 0.02,
      y = 0.98,
      bgcolor = "rgba(255,255,255,0.90)"
    ),

    margin = list(
      l = 0,
      r = 0,
      b = 0,
      t = 60
    )
  )


fig