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))
.
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 numericsas.character()
- provides a textual representation of the geometryformat()
- as aboveas.list()
- splits the vector into single elements in a liststr()
- provides a condensed view of the vectorlength()
- gives the number of geometries in the vectorrep()
- replicates elements in the vectorseq()
- interpolate between two geometries of the same type and dimensionalitydim()
- gives the dimensionality of the geometry (2 or 3)[
[
] and [[[
] - extract elements from the vector[
[<-
] and [[[<-
] - assigns elements into the vector. SinceNA
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 unnamedc()
- combine multiple geometries of the same typeunique()
- returns the unique elements of the vector keeping the orderduplicated()
- gives whether an element has been seen before in the vectoranyDuplicated()
- Tells if any element in the vector is a duplicatetransform()
- 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).
See also
Other Geometry methods:
def()
,
subgeometries
Examples
p <- point(sample(10, 4), sample(10, 4))
p
#> <2D points [4]>
#> [1] <x:7, y:8> <x:10, y:7> <x:6, y:9> <x:4, y:5>
p[2]
#> <2D points [1]>
#> [1] <x:10, y:7>
p[3] <- point(14, 20)
p
#> <2D points [4]>
#> [1] <x:7, y:8> <x:10, y:7> <x:14, y:20> <x:4, y:5>
p2 <- c(p, p)
p2
#> <2D points [8]>
#> [1] <x:7, y:8> <x:10, y:7> <x:14, y:20> <x:4, y:5> <x:7, y:8>
#> [6] <x:10, y:7> <x:14, y:20> <x:4, y:5>
unique(p2)
#> <2D points [4]>
#> [1] <x:7, y:8> <x:10, y:7> <x:14, y:20> <x:4, y:5>
sort(p)
#> <2D points [4]>
#> [1] <x:4, y:5> <x:7, y:8> <x:10, y:7> <x:14, y:20>
rep(p, each = 2)
#> <2D points [8]>
#> [1] <x:7, y:8> <x:7, y:8> <x:10, y:7> <x:10, y:7> <x:14, y:20>
#> [6] <x:14, y:20> <x:4, y:5> <x:4, y:5>
as.list(p)
#> [[1]]
#> <2D points [1]>
#> [1] <x:7, y:8>
#>
#> [[2]]
#> <2D points [1]>
#> [1] <x:10, y:7>
#>
#> [[3]]
#> <2D points [1]>
#> [1] <x:14, y:20>
#>
#> [[4]]
#> <2D points [1]>
#> [1] <x:4, y:5>
#>
transform(p, affine_rotate(pi/3))
#> <2D points [4]>
#> [1] <x:-3.43, y:10.1> <x:-1.06, y:12.2> <x:-10.3, y:22.1> <x:-2.33, y:5.96>