GeometryBasics.jl

Basic geometry types.

This package aims to offer a standard set of geometry types that easily work with metadata, query frameworks on geometries and different memory layouts. The aim is to create a solid basis for graphics/plotting, finite element analysis, geo applications, and general geometry manipulations - while offering a Julian API that still allows performant C-interop.

This package is a replacement for the discontinued GeometryTypes.

Quick start

Create some points:

julia> using GeometryBasics
julia> p1 = Point(3, 1)2-element Point{2, Int64} with indices SOneTo(2): 3 1
julia> p2 = Point(1, 3);
julia> p3 = Point(4, 4);

Geometries can carry metadata:

julia> poi = meta(p1, city="Abuja", rainfall=1221.2)2-element PointMeta{2, Int64, Point{2, Int64}, (:city, :rainfall), Tuple{String, Float64}} with indices SOneTo(2):
 3
 1

Metadata is stored in a NamedTuple and can be retrieved as such:

julia> meta(poi)(city = "Abuja", rainfall = 1221.2)

Specific metadata attributes can be directly retrieved:

julia> poi.rainfall1221.2

To remove the metadata and keep only the geometry, use metafree:

julia> metafree(poi)2-element Point{2, Int64} with indices SOneTo(2):
 3
 1

Geometries have predefined metatypes:

julia> multipoi = MultiPointMeta([p1], city="Abuja", rainfall=1221.2)1-element MultiPointMeta{Point{2, Int64}, MultiPoint{2, Int64, Point{2, Int64}, Vector{Point{2, Int64}}}, (:city, :rainfall), Tuple{String, Float64}}:
 [3, 1]

Connect the points with lines:

julia> l1 = Line(p1, p2)Line([3, 1] => [1, 3])
julia> l2 = Line(p2, p3);

Connect the lines in a linestring:

julia> LineString([l1, l2])2-element LineString{2, Int64, Point{2, Int64}, Vector{Line{2, Int64}}}:
 Line([3, 1] => [1, 3])
 Line([1, 3] => [4, 4])

Linestrings can also be constructed directly from points:

julia> LineString([p1, p2, p3])2-element LineString{2, Int64, Point{2, Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point{2, Int64}, Point{2, Int64}}, TupleView{Tuple{Point{2, Int64}, Point{2, Int64}}, 2, 1, Vector{Point{2, Int64}}}, false}}:
 Line([3, 1] => [1, 3])
 Line([1, 3] => [4, 4])

The same goes for polygons:

julia> Polygon(Point{2, Int}[(3, 1), (4, 4), (2, 4), (1, 2), (3, 1)])Polygon{2, Int64, Point{2, Int64}, LineString{2, Int64, Point{2, Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point{2, Int64}, Point{2, Int64}}, TupleView{Tuple{Point{2, Int64}, Point{2, Int64}}, 2, 1, Vector{Point{2, Int64}}}, false}}, Vector{LineString{2, Int64, Point{2, Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point{2, Int64}, Point{2, Int64}}, TupleView{Tuple{Point{2, Int64}, Point{2, Int64}}, 2, 1, Vector{Point{2, Int64}}}, false}}}}(Line{2, Int64}[Line([3, 1] => [4, 4]), Line([4, 4] => [2, 4]), Line([2, 4] => [1, 2]), Line([1, 2] => [3, 1])], LineString{2, Int64, Point{2, Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point{2, Int64}, Point{2, Int64}}, TupleView{Tuple{Point{2, Int64}, Point{2, Int64}}, 2, 1, Vector{Point{2, Int64}}}, false}}[])

Create a rectangle placed at the origin with unit width and height:

julia> rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0))HyperRectangle{2, Float64}([0.0, 0.0], [1.0, 1.0])

Decompose the rectangle into two triangular faces:

julia> rect_faces = decompose(TriangleFace{Int}, rect)2-element Vector{TriangleFace{Int64}}:
 TriangleFace(1, 2, 4)
 TriangleFace(1, 4, 3)

Decompose the rectangle into four vertices:

julia> rect_vertices = decompose(Point{2, Float64}, rect)4-element Vector{Point{2, Float64}}:
 [0.0, 0.0]
 [1.0, 0.0]
 [0.0, 1.0]
 [1.0, 1.0]

Combine the vertices and faces into a triangle mesh:

julia> mesh = Mesh(rect_vertices, rect_faces)Mesh{2, Float64, Triangle}:
 Triangle([0.0, 0.0], [1.0, 0.0], [1.0, 1.0])
 Triangle([0.0, 0.0], [1.0, 1.0], [0.0, 1.0])

Use GeometryBasics.mesh to get a mesh directly from a geometry:

julia> mesh = GeometryBasics.mesh(rect)Mesh{2, Float64, Triangle}:
 Triangle([0.0, 0.0], [1.0, 0.0], [1.0, 1.0])
 Triangle([0.0, 0.0], [1.0, 1.0], [0.0, 1.0])

Aliases

GeometryBasics exports common aliases for Point, Vec, Mat and Rect:

Vec

T(eltype)Float64Float32IntUInt
N(dim)Vec{N,T}Vecd{N}Vecf{N}Veci{N}Vecui{N}
2Vec2{T}Vec2dVec2fVec2iVec2ui
3Vec3{T}Vec3dVec3fVec3iVec3ui

Point

T(eltype)Float64Float32IntUInt
N(dim)Point{N,T}Pointd{N}Pointf{N}Pointi{N}Pointui{N}
2Point2{T}Point2dPoint2fPoint2iPoint2ui
3Point3{T}Point3dPoint3fPoint3iPoint3ui

Mat

T(eltype)Float64Float32IntUInt
N(dim)Mat{N,T}Matd{N}Matf{N}Mati{N}Matui{N}
2Mat2{T}Mat2dMat2fMat2iMat2ui
3Mat3{T}Mat3dMat3fMat3iMat3ui

Rect

T(eltype)Float64Float32IntUInt
N(dim)Rect{N,T}Rectd{N}Rectf{N}Recti{N}Rectui{N}
2Rect2{T}Rect2dRect2fRect2iRect2ui
3Rect3{T}Rect3dRect3fRect3iRect3ui