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

## -----------------------------------------------------------------------------
respf <- function(x) {
     c1 <- x[, 1]^2 + 2*x[, 1]*x[, 2] + 2*(x[, 2]^2)
     c2 <- (x[, 1]-8)^2 + 2*(x[, 1]-8)*(x[, 2]-8) + 2*((x[, 2]-8)^2)
     resp <- ifelse(x[, 1] + 2*x[, 2] < 15, -1*c1, -1*c2)
     resp
}
feasf <- function(x) {
     (x[, 1] >= 0)
}

## -----------------------------------------------------------------------------
library(levelSets)
fobj <- fnObj(c("X1", "X2"), respfn=respf, feasfn=feasf)

## -----------------------------------------------------------------------------
pts <- rbind(c(2, 2), c(4, -1), c(-1, 4), c(3, 5))
respInfo(pts, fnobj=fobj, lsetthresh=-40)

## -----------------------------------------------------------------------------
rect1 <- inpRect(rbind(c(-5, -10), c(15, 12)), spec=fobj)

## -----------------------------------------------------------------------------
set.seed(1)
rays1 <- randomRays(50, origin=c(2, 1), rect=rect1)
plot(rays1, main="Random rays, position 1 defined by a rectangle")

## -----------------------------------------------------------------------------
segs1 <- bdryFromRays(fobj, rays=rays1, lsetthresh=-40)

## -----------------------------------------------------------------------------
plot(segs1, rect=rect1, main="Level set at threshold -40 (50 rays)")
summary(segs1)

## -----------------------------------------------------------------------------
chk1 <- lsetSegsCheck(segs1, fnobj=fobj, posns=(1:4)/5)
table(chk1$validIn)
table(chk1$validOut)
segs1 <- lsetSegs(chk1)  # all TRUE, so update 'segs1'
plot(segs1, rect=rect1, main="Level set at threshold -40 (invalid segs fixed)")

## -----------------------------------------------------------------------------
rays2 <- randomRays(50, origin=c(2, 1), rect=rect1)
segs2 <- bdryFromRays(fobj, rays=rays2, lsetthresh=-40, 
                      control=list(initPosns=(0:5)/5))

## -----------------------------------------------------------------------------
segs12 <- c(segs1, segs2)  # 'c' method for 'lsetSegs' objects
plot(segs12, rect=rect1, main="Level set at threshold -40 (100 rays)")
summary(segs12)

