Skip to content

Latest commit

 

History

67 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Galixir

Galixir Header

galixir

Geometric Algebra implementation in Elixir.

A concrete algebra can be generated via macro:

defmodule Example.PGA3 do
  # e1 squares to 1
  # e2 squares to 1
  # e3 squares to 1
  # e0 squares to 0
  use Galixir.GeometricAlgebra,
    metric: {1, 1, 1, 0},
    bases: {1, 2, 3, 0}

  # ... additional custom functions ...
end

PGA3 is already defined in Galixir.Algebras.PGA3.

Then the PGA3 module can be used to to calculations inside of the generated algebra:

defmodule Example do
  import Galixir.Algebras.PGA3

 def align(ps, qs) do
    # https://observablehq.com/@enkimute/glu-lookat-in-3d-pga
    initial_m = identity = one()
    initial_q = dual(one())

    Enum.zip_reduce(ps, qs, {initial_m, initial_q}, fn p, q, {m, prev_q} ->
      p = prev_q |> join(transform(p, m)) |> normalize!()
      new_q = prev_q |> join(q) |> normalize!() |> inverse!()
      new_m = new_q |> gp(p) |> add(identity) |> gp(m)
      {new_m, new_q}
    end)
    |> elem(0)
  end

  def look_at(
        position \\ point(0, 10, 0),
        target \\ point(0, 0, 0),
        pole \\ ideal_point(0, 0, 1)
      ) do
    align(
      [position, target, pole],
      [point(0, 0, 0), point(0, 0, 1), ideal_point(0, 1, 0)]
    )
  end
end

eye = Galixir.Algebras.PGA3.point(3, 2, 1)
target = Galixir.Algebras.PGA3.point(0, 1, 0)
pole = Galixir.Algebras.PGA3.ideal_point(0, 0, 1)

camera_transform = Example.look_at(eye, target, pole)

point_in_world = Galixir.Algebras.PGA3.point(6, 5, 4)
point_in_screen = {sx,sy,sz} = Galixir.Algebras.PGA3.transform(camera_transform, point_in_world)
  |> Galixir.Algebras.PGA3.point_coordinates()
fov = 2
projected = {sx / sz * fov, sy / sz * fov}

Installation

def deps do
  [
    {:galixir, "~> 0.27.0"}
  ]
end

Some of the concrete algebras are not fully implemented yet. PGA2, PGA3 and CGA2 are pretty complete and provide many manually implemented helper functions and doctests on top of the macro generated core.

Vector2 and Vector3 provide classic Euclidean, non-projective vector algebra helpers on top of the generated core. Complex1 remains a small example algebra generated via use Galixir.GeometricAlgebra.

Contributions are welcome.

Example

This Livebook shows an example of how to used 3D Projective Geometric Algebra (PGA3) to render a 3D scene as SVG.

3d Projective Geometric Algebra

Preview Screenshot for projective geometric algebra in 3d

2d Conformal Geometric Algebra

Preview Screenshot for conformal geometric algebra in 2d

About

Geometric Algebra implementation in Elixir.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages