The base vector type for geometries in euclid is the euclid_geometry class. While this is a virtual class (you cannot construct it, only its subclasses), many of the common vector operations are defined here. Further it is possible to check whether a vector is a geometry of any type using is_geometry(). While most geometries are atomic, a few are defined by multiples of the same geometry, e.g. segments and triangles which are defined by 2 and 3 points respectively. The cardinality of each geometry in a vector can be queried with cardinality(). The matrix conversion will place each sub-geometry of a geometry on a new row, meaning that the final number of rows in a matrix constructed from x is not length(x) but sum(cardinality(x)).

is_geometry(x)

geometry_type(x)

cardinality(x)

definition_names(x)

Arguments

x

An euclid_geometry object

Vector behaviour

Geometry vectors in euclid are made to behave as closely as possible to what you expect from normal R vectors. However, they are implemented as external pointers to the exact C representation meaning that they cannot be restored across sessions or saved to RData/RDS files. Despite being external pointers they mimick R's copy-on-modify semantics so you should not worry about side effects when changing a geometry vector.

The following is a list of standard R methods defined for geometry vectors:

  • as.matrix() - converts the geometry to a standard R matrix of numerics

  • as.character() - provides a textual representation of the geometry

  • format() - as above

  • as.list() - splits the vector into single elements in a list

  • str() - provides a condensed view of the vector

  • length() - gives the number of geometries in the vector

  • rep() - replicates elements in the vector

  • dim() - gives the dimensionality of the geometry (2 or 3)

  • [[] and [[[] - extract elements from the vector

  • [[<-] and [[[<-] - assigns elements into the vector. Since NA values are not supported it is not possible to assign past the length of the vector

  • $ and $<- - will throw an error since geometry vectors are unnamed

  • c() - combine multiple geometries of the same type

  • unique() - returns the unique elements of the vector keeping the order

  • duplicated() - gives whether an element has been seen before in the vector

  • anyDuplicated() - Tells if any element in the vector is a duplicate

  • == and != - Test for equality between elements.

  • transform() - Transform geometries in the vector according to an affine_transformation

Do note that since geometries with exact representation is not easily hashable, the implementation of unique() and duplicated() is not very efficient (except for points since they can be sorted).

Examples

p <- point(sample(10, 4), sample(10, 4)) p
#> <2D points [4]> #> [1] <x:9, y:5> <x:5, y:4> <x:3, y:3> <x:2, y:9>
p[2]
#> <2D points [1]> #> [1] <x:5, y:4>
p[3] <- point(14, 20) p
#> <2D points [4]> #> [1] <x:9, y:5> <x:5, y:4> <x:14, y:20> <x:2, y:9>
p2 <- c(p, p) p2
#> <2D points [8]> #> [1] <x:9, y:5> <x:5, y:4> <x:14, y:20> <x:2, y:9> <x:9, y:5> #> [6] <x:5, y:4> <x:14, y:20> <x:2, y:9>
unique(p2)
#> <2D points [4]> #> [1] <x:9, y:5> <x:5, y:4> <x:14, y:20> <x:2, y:9>
sort(p)
#> <2D points [4]> #> [1] <x:2, y:9> <x:5, y:4> <x:9, y:5> <x:14, y:20>
rep(p, each = 2)
#> <2D points [8]> #> [1] <x:9, y:5> <x:9, y:5> <x:5, y:4> <x:5, y:4> <x:14, y:20> #> [6] <x:14, y:20> <x:2, y:9> <x:2, y:9>
#> [[1]] #> <2D points [1]> #> [1] <x:9, y:5> #> #> [[2]] #> <2D points [1]> #> [1] <x:5, y:4> #> #> [[3]] #> <2D points [1]> #> [1] <x:14, y:20> #> #> [[4]] #> <2D points [1]> #> [1] <x:2, y:9> #>
#> <2D points [4]> #> [1] <x:0.17, y:10.3> <x:-0.964, y:6.33> <x:-10.3, y:22.1> <x:-6.79, y:6.23>