cgalPolygonWithHoles: R6 class to represent a CGAL polygon with holes

cgalPolygonWithHolesR Documentation

R6 class to represent a CGAL polygon with holes

Description

R6 class to represent a CGAL polygon with holes.

Methods

Public methods


Method new()

Creates a new cgalpolygonWithHoles object.

Usage
cgalPolygonWithHoles$new(outerVertices, holes = list())
Arguments
outerVertices

a numeric matrix with two columns, the vertices of the outer polygon

holes

a list of numeric matrices, each representing the vertices of a hole; an empty list is allowed

Returns

A cgalPolygonWithHoles object.

Examples
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh

Method area()

Area of the polygon with holes.

Usage
cgalPolygonWithHoles$area()
Returns

A positive number, the area of the polygon.

Examples
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$area() # should be 12

Method boundingBox()

Bounding box of the polygon with holes.

Usage
cgalPolygonWithHoles$boundingBox()
Returns

A 2x2 matrix giving the lower corner of the bounding box in its first row and the upper corner in its second row.

Examples
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$boundingBox()

Method convexParts()

Decomposition into convex parts of the polygon with holes. The outer polygon as well as the holes must be simple.

Usage
cgalPolygonWithHoles$convexParts(method = "triangle")
Arguments
method

the method used: "triangle" or "vertical"

Returns

A list of matrices; each matrix has two columns and represents a convex polygon.

Examples
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
cxparts <- pwh$convexParts()
pwh$plot(list(), density = 10)
invisible(
  lapply(cxparts, function(cxpart) {
    polygon(cxpart, lwd = 2)
  })
)

Method getVertices()

Get the vertices of the polygon.

Usage
cgalPolygonWithHoles$getVertices()
Returns

A named list with two fields: "outer", the vertices of the outer polygon in a matrix, and "holes", the vertices of the holes in a list of matrices.


Method intersection()

Intersection of the polygon with another polygon.

Usage
cgalPolygonWithHoles$intersection(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# intersection
plgList <- plg1$intersection(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
  NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1, 
  xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg2$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg$plot(lwd = 4, col = "red", new = FALSE)
par(opar)

Method minkowskiSum()

Minkowski sum of the polygon and another polygon.

Usage
cgalPolygonWithHoles$minkowskiSum(plg2, method = "convolution")
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object, the polygon to add to the reference polygon

method

the method used: "convolution", "triangle", "vertical" or "optimal" (the method should not change the result)

Returns

Either a cgalPolygonWithHoles object, or, in the case if there is no hole in the Minkowski sum, a cgalPolygon object.

Examples
library(cgalPolygons)
plg1 <- cgalPolygonWithHoles$new(decagram)
plg2 <- cgalPolygonWithHoles$new(star)
minko <- plg1$minkowskiSum(plg2)
minko$plot(lwd = 2, col = "limegreen")

Method plot()

Plot the polygon with holes.

Usage
cgalPolygonWithHoles$plot(
  outerpars = list(),
  ...,
  new = TRUE,
  outerfirst = TRUE
)
Arguments
outerpars

named list of arguments passed to polygon for the outer polygon

...

arguments passed to polygon for the holes

new

Boolean, whether to create a new plot

outerfirst

Boolean, whether to print the outer polygon first

Returns

No returned value, called for side-effect.

Examples
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$plot(
  outerpars = list(lwd = 2), density = 10
)

Method print()

Print the cgalPolygonWithHoles object.

Usage
cgalPolygonWithHoles$print(...)
Arguments
...

ignored

Returns

No value, just prints some information about the polygon.


Method subtract()

Difference between the polygon and another polygon.

Usage
cgalPolygonWithHoles$subtract(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# difference
plgList <- plg1$subtract(plg2)
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
  NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1, 
  xlab = NA, ylab = NA, axes = FALSE
)
plgList[[1]]$plot(lwd = 4, col = "red", new = FALSE)
plgList[[2]]$plot(lwd = 4, col = "red", new = FALSE)
par(opar)

Method symdiff()

Symmetric difference of the polygon and another polygon.

Usage
cgalPolygonWithHoles$symdiff(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# symmetric difference
plgList <- plg1$symdiff(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)

Method union()

Union of the polygon with another polygon.

Usage
cgalPolygonWithHoles$union(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# union
plgList <- plg1$union(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)

Examples


## ------------------------------------------------
## Method `cgalPolygonWithHoles$new`
## ------------------------------------------------

library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh

## ------------------------------------------------
## Method `cgalPolygonWithHoles$area`
## ------------------------------------------------

library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$area() # should be 12

## ------------------------------------------------
## Method `cgalPolygonWithHoles$boundingBox`
## ------------------------------------------------

library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$boundingBox()

## ------------------------------------------------
## Method `cgalPolygonWithHoles$convexParts`
## ------------------------------------------------

library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
cxparts <- pwh$convexParts()
pwh$plot(list(), density = 10)
invisible(
  lapply(cxparts, function(cxpart) {
    polygon(cxpart, lwd = 2)
  })
)

## ------------------------------------------------
## Method `cgalPolygonWithHoles$intersection`
## ------------------------------------------------

library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# intersection
plgList <- plg1$intersection(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
  NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1, 
  xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg2$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg$plot(lwd = 4, col = "red", new = FALSE)
par(opar)

## ------------------------------------------------
## Method `cgalPolygonWithHoles$minkowskiSum`
## ------------------------------------------------

library(cgalPolygons)
plg1 <- cgalPolygonWithHoles$new(decagram)
plg2 <- cgalPolygonWithHoles$new(star)
minko <- plg1$minkowskiSum(plg2)
minko$plot(lwd = 2, col = "limegreen")

## ------------------------------------------------
## Method `cgalPolygonWithHoles$plot`
## ------------------------------------------------

library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
  squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$plot(
  outerpars = list(lwd = 2), density = 10
)

## ------------------------------------------------
## Method `cgalPolygonWithHoles$subtract`
## ------------------------------------------------

library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# difference
plgList <- plg1$subtract(plg2)
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
  NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1, 
  xlab = NA, ylab = NA, axes = FALSE
)
plgList[[1]]$plot(lwd = 4, col = "red", new = FALSE)
plgList[[2]]$plot(lwd = 4, col = "red", new = FALSE)
par(opar)

## ------------------------------------------------
## Method `cgalPolygonWithHoles$symdiff`
## ------------------------------------------------

library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# symmetric difference
plgList <- plg1$symdiff(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)

## ------------------------------------------------
## Method `cgalPolygonWithHoles$union`
## ------------------------------------------------

library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
  t <- seq(0, 2, length.out = 100)[-1L]
  t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles with a hole
plg1 <- cgalPolygonWithHoles$new(
  circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
  circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# union
plgList <- plg1$union(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)

cgalPolygons documentation built on May 31, 2023, 8:16 p.m.