Coder Social home page Coder Social logo

geometrybasics.jl's Introduction

Build Status Codecov

Documentation: Docs - stable Docs - dev

GeometryBasics.jl

Basic Geometry Types. This package aimes to offer a standard set of Geometry types, which easily work with metadata, query frameworks on geometries and different memory layouts. The aim is to create a solid basis for Graphics/Plotting, finite elements 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

using GeometryBasics

# create some points
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.rainfall
1221.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

# for other geometries metatypes are predefined
julia> multipoi = MultiPointMeta([p1], city="Abuja", rainfall=1221.2)
1-element MultiPointMeta{Point{2,Int64},MultiPoint{2,Int64,Point{2,Int64},Array{Point{2,Int64},1}},(: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},Array{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1}}:
 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{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}}:
 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{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}},Array{LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}},1}}(GeometryBasics.Ngon{2,Int64,2,Point{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{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}}[])

# create a rectangle placed at the origin with unit widths
julia> rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0))
GeometryBasics.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 Array{NgonFace{3,Int64},1}:
 TriangleFace(1, 2, 4)
 TriangleFace(1, 4, 3)

# decompose the rectangle into four vertices
julia> rect_vertices = decompose(Point{2, Float64}, rect)
4-element Array{Point{2,Float64},1}:
 [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])

geometrybasics.jl's People

Contributors

ambyld avatar asinghvi17 avatar blegat avatar daschw avatar eschnett avatar ffreyer avatar juliatagbot avatar juliohm avatar jw3126 avatar mohamed82008 avatar mtsch avatar pauljurczak avatar simondanisch avatar sjkelly avatar sov-trotter avatar timholy avatar tokazama avatar visr avatar yha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.