Transformations is an Elixir library for translating, rotating, reflecting, scaling, shearing, projecting, orthogonalizing, and superimposing arrays of 3D homogeneous coordinates as well as for converting between rotation matrices, Euler angles, and quaternions. Also includes an Arcball control object and functions to decompose transformation matrices.
The package can be installed by adding transformations
to your list of dependencies in mix.exs
:
def deps do
[
{:transformations, "~> 0.1.0"}
]
end
Create a matrix
The following transformations are currently available:
where xt, yt and zt are the x, y and z coordinates for the transform
where xs, ys and zs are the scale factors along the x, y and z axes
where s is the scale factor applied to each of x, y and z axes
where angle refers to the angle of rotation along the specified axes in degrees
where angle refers to the angle of rotation in degrees along a line from x, y, z to p, q, r
where sx, sy and sz refer to the shear coefficients along each axis
mirrors around the yz plane or the x-axis
mirrors around the xz plane or the y-axis
mirrors around the xy plane or the z-axis
projects down to the yz plane
projects down to the xz plane
projects down to the xy plane
Transformations can be applied to points as well as shapes. Points are specified as a 4 x 1 matrix while shapes are specified as a 4 x n matrix. Transformations are also 4 x 1 and 4 x n matrices respectively.
iex(1)> point = Transformations.matrix([[1],[1],[1],[1]])
#Matrex[4×1]
┌ ┐
│ 1.0 │
│ 1.0 │
│ 1.0 │
│ 1.0 │
└ ┘
iex(2)> point |> Transformations.scale(1,2,3)
#Matrex[4×1]
┌ ┐
│ 1.0 │
│ 2.0 │
│ 3.0 │
│ 1.0 │
└ ┘
iex(3)> point |> Transformations.scale(1,2,3) |> Transformations.transition(3,4,7)
#Matrex[4×1]
┌ ┐
│ 4.0 │
│ 6.0 │
│ 10.0 │
│ 1.0 │
└ ┘
iex(4)> shape = Transformations.matrix([
[0,1,1,0,0,1,1,0],
[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[1,1,1,1,1,1,1,1]
])
#Matrex[4×8]
┌ ┐
│ 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 │
│ 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 │
│ 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 │
│ 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 │
└ ┘
iex(5)> shape |> Transformations.scale(1,2,3) |> Transformations.transition(3,4,7)
#Matrex[4×8]
┌ ┐
│ 3.0 4.0 4.0 3.0 3.0 4.0 4.0 3.0 │
│ 4.0 4.0 4.0 4.0 6.0 6.0 6.0 6.0 │
│ 7.0 7.0 10.0 10.0 7.0 7.0 10.0 10.0 │
│ 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 │
└ ┘
mix docs