## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(echo=TRUE, fig.align="center")
has_ggplot <- requireNamespace("ggplot2", quietly=TRUE)

## ----echo=FALSE, eval=has_ggplot----------------------------------------------
ggplot2::theme_update(plot.background=ggplot2::element_rect(fill=NA))

## -----------------------------------------------------------------------------
respf <- function(x, inclGrad=FALSE) {
  y <- -1 * (100*((x[, 2] - x[, 1]^2)^2) + (1 - x[, 1])^2)
  if (inclGrad) {
    grad <- cbind(400*(x[, 1]*(x[, 2] - x[, 1]^2)) + 2*(1 - x[, 1]), 
                  -200*(x[, 2] - x[, 1]^2))
    attr(y, "gradient") <- grad
  }
  y
}
thresh <- -50

## -----------------------------------------------------------------------------
feasf <- function(x) {
     (x[, 1] >= -2)
}

## -----------------------------------------------------------------------------
library(levelSets)
fobj <- fnObj(c("x1", "x2"), respfn=respf, feasfn=feasf, hasgrad=TRUE)

## -----------------------------------------------------------------------------
str(lsetPkgOpt(c("inptol", "resptol")))

## -----------------------------------------------------------------------------
rect0 <- inpRect(cbind(c(-3, 3), c(-3, 6)), spec=fobj)

## ----out.width='45%'----------------------------------------------------------
set.seed(1)
rays0 <- randomRays(25, origin=c(1, 1), rect=rect0)
plot(rays0, main="Random rays, position 1 defined by 'rect0'")

## -----------------------------------------------------------------------------
fspec <- fnSpec(inpnames=c("x1", "x2"))
rect1 <- inpRect(rbind(c(0.5, 3), c(5.5, 3.5)), spec=fspec)

## ----fig.show='hold', out.width='45%'-----------------------------------------
set.seed(1)
# Rays with uniformly distributed directions and equal position 1 lengths:
rays_u <- randomRays(50, origin=c(1, 3.25), spec=fspec)
# Position 1 redefined by intersection with a hyper-rectangle:
rays1 <- update(rays_u, rect=rect1)
plot(rays1, main="Random rays, no dimension scaling")
# Specify scaling for input space dimensions:
scl2 <- inpScale(rectSize(rect1), spec=fspec)
# Same rectangle, but ray generation now reflects input dimension scaling:
set.seed(1)
rays2 <- randomRays(50, origin=c(1, 3.25), scale=scl2, rect=rect1)
plot(rays2, main="Random rays, dimensions scaled by 'rect' sides")

## -----------------------------------------------------------------------------
rays3 <- axisRays(degree=2, origin=c(1, 1), rect=rect0)
prof3 <- respProfiles(fobj, rays=rays3, lsetthresh=-50, posns=(-10:10)/10)

## -----------------------------------------------------------------------------
summary(prof3)
if (requireNamespace("ggplot2", quietly=TRUE))  plot(prof3, groupBy=names(rays3))

## -----------------------------------------------------------------------------
head(respInfo(prof3))
head(ptCoord(prof3))

## ----out.width='45%', fig.show='hold', echo=FALSE-----------------------------
set.seed(1)
# Origin in the level set.
rays1 <- randomRays(24, origin=c(1, 1), rect=rect0)
segs1 <- bdryFromRays(fobj, rays=rays1, lsetthresh=thresh)
  # Level set segment found for all rays (although some are in fact invalid).
chk <- lsetSegsCheck(segs1, fnobj=fobj, posns=(1:4)/5)
#table(chk$validIn)  # 2 of 24 invalid
#table(chk$validOut)  # all TRUE
segs1 <- lsetSegs(chk)
plot(segs1, rect=rect0, main="Origin inside the level set", pt0=c(1, 1))

# Origin outside the level set.
rays2 <- update(rays1, origin=c(-1, -2))
segs2 <- bdryFromRays(fobj, rays=rays2, lsetthresh=thresh)
chk <- lsetSegsCheck(segs2, fnobj=fobj, posns=(1:4)/5)
#table(chk$validIn)  #  0 of 6 invalid
#table(chk$validOut)  # all TRUE
segs2 <- lsetSegs(chk)
plot(segs2, rect=rect0, main="Origin outside the level set", pt0=c(-1, -2))
  # Much less informative, only 6 segs found.

## ----out.width='45%', fig.show='hold', echo=FALSE-----------------------------
# Initial origin in the level set.
srch1b <- bdrySearch(fobj, lsetthresh=thresh, rect=rect0, initOrigin=c(1, 1), 
                     control=list(axisDegree=2, maxSteps=6))  # 24 rays
segs1b <- srch1b$segs
chk <- lsetSegsCheck(segs1b, fnobj=fobj, posns=(1:4)/5)
#table(chk$validIn)  # 1 of 24 invalid
#table(chk$validOut)  # all TRUE
segs1b <- chk$segs
plot(segs1b, rect=rect0, main="Initial origin inside the level set", 
     pt0=c(1, 1))

# Initial origin outside the level set.
srch2b <- bdrySearch(fobj, lsetthresh=thresh, rect=rect0, initOrigin=c(-1, -2), 
                     control=list(axisDegree=2, maxSteps=6))  # 24 rays
segs2b <- srch2b$segs
chk <- lsetSegsCheck(segs2b, fnobj=fobj, posns=(1:4)/5)
#table(chk$validIn)  #  0 of 23 invalid
#table(chk$validOut)  # all TRUE
segs2b <- chk$segs
plot(segs2b, rect=rect0, main="Initial origin outside the level set", pt0=c(-1, -2))

## ----eval=FALSE, echo=TRUE----------------------------------------------------
# slices <- rbind(c(1, NA, NA), c(2, NA, 3), c(NA, NA, NA))

